<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Build & Program Robots: From Embedded Systems to ROS 2 and Robot Learning</title>
 <link href="https://robotics-self-learning.pages.dev/atom.xml" rel="self"/>
 <link href="https://robotics-self-learning.pages.dev/"/>
 <updated>2026-12-05T04:03:55+00:00</updated>
 <id>https://robotics-self-learning.pages.dev</id>
 <author>
   <name>Nguyen Le Linh</name>
   <email>nglelinh@gmail.com</email>
 </author>

 
 <entry>
   <title>author-details</title>
   <link href="https://robotics-self-learning.pages.dev/home/author-details/"/>
   <updated>2021-05-20T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/home/author-details</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;Nguyen Le Linh&lt;/strong&gt; — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nglelinh@gmail.com&lt;/code&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>reference</title>
   <link href="https://robotics-self-learning.pages.dev/reference/26_reference/"/>
   <updated>2021-03-28T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/reference/26_reference</id>
   <content type="html">&lt;h1 id=&quot;references-and-resources&quot;&gt;References and Resources&lt;/h1&gt;

&lt;p&gt;This page contains references and resources for the course. Add your own references as needed.&lt;/p&gt;

&lt;h2 id=&quot;interactive-3d-labs-supplementary&quot;&gt;Interactive 3D labs (supplementary)&lt;/h2&gt;

&lt;p&gt;Free browser-based labs that complement the hands-on chapters. They run in the browser with Three.js; no hardware is required. Use the board labs to preview a circuit before wiring it, RoverKit to walk through a first robot-car build and an ultrasonic avoidance loop, and Teardown to generate an offline 3D mechanism you can take apart.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;&lt;a href=&quot;https://learn-esp32.vercel.app/&quot;&gt;Learn ESP32&lt;/a&gt;&lt;/em&gt; (Dilum Sanjaya). 20 beginner lessons on a 3D ESP32 board: LEDs, buzzer, Wi-Fi, Bluetooth, ESP-NOW, distance sensing, wheels, ultrasonic radar, pick-and-place. Vietnamese available. Matches the course default MCU (ESP32).&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;a href=&quot;https://learn-arduino-red.vercel.app/&quot;&gt;Learn Arduino&lt;/a&gt;&lt;/em&gt; (Dilum Sanjaya). 16 beginner lessons on a 3D Arduino Uno: LED, button, potentiometer, LDR, HC-SR04, buzzer, DC motor via transistor, TMP36, servo, stepper, soil sensor + OLED, FSR, final distance + traffic-light project. &lt;a href=&quot;https://learn-arduino-red.vercel.app/vi/&quot;&gt;Vietnamese version&lt;/a&gt;. Same API as the Arduino Mega 2560 used as the 5 V bench reference.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;a href=&quot;https://robotics-lab-app.vercel.app/&quot;&gt;Robotics Lab&lt;/a&gt;&lt;/em&gt;. Eight boards (Uno, Nano, Mega 2560, ESP32, NodeMCU, Pico, STM32 Blue Pill, Raspberry Pi 4) in 3D with part-by-part lessons, a “find the part” quiz, take-apart/solder-back assembly, and a comparison table (MCU, logic voltage, price). Useful for choosing a board and remembering 3.3 V vs 5 V logic. The wiring workbench was still under construction as of 2026-11-21.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;a href=&quot;https://www.things-workshop.com/&quot;&gt;Things Workshop&lt;/a&gt;&lt;/em&gt; (Cameron Priest). Interactive 3D mechanism explainers; most relevant: &lt;a href=&quot;https://www.things-workshop.com/explore/how-many-joints-does-a-robot-arm-need/&quot;&gt;How many joints does a robot arm need?&lt;/a&gt; and &lt;a href=&quot;https://www.things-workshop.com/explore/actuator-explorer/&quot;&gt;Inside a robotic actuator&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;a href=&quot;https://roverkit.vercel.app/#parts&quot;&gt;RoverKit&lt;/a&gt;&lt;/em&gt; (Dilum Sanjaya). A ten-step 3D assembly guide and in-browser simulator for a 4WD robot car (Arduino UNO or ESP32, L9110S, four TT motors, HC-SR04 on an SG90 servo). The arena is 3×3 m; drive with WASD or run Auto obstacle avoidance (scan right, then left, turn toward the side with more room). Vietnamese UI available. Intro-level: open-loop, no encoders or IMU, and L9110S is a weaker driver than the TB6612 used in the course. The Auto decision flow is discussed in lesson 04-05. UNO sketches are largely portable to the Arduino Mega 2560 used as the 5 V bench board; the course default brain remains the ESP32.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;a href=&quot;https://github.com/elvarofficial/teardown&quot;&gt;Teardown&lt;/a&gt;&lt;/em&gt; (elvarofficial, MIT). A Claude Code skill that turns a text prompt into a single offline HTML page (~800 KB, three.js): the machine explodes and reassembles, a click opens the working subsystem, and a live data panel follows the motion. Kinematics use real dimensions driven by one input (crank angle, stroke, or wind). Lighting is GTAO + bloom. Built-in demos: a V8 engine, Cessna 337 landing gear (rack-and-pinion), and a 20-leg Strandbeest; official showcase robots include the bee Mellis HB-7, the scorpion Telson SC-8, and the octopus Pelagos OC-8. Install with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npx skills add elvarofficial/teardown -g -a claude-code&lt;/code&gt; (Node.js 20+), then run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm install &amp;amp;&amp;amp; npx playwright install chromium&lt;/code&gt; once in the installed folder. The three official demos can be built with Node alone (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node sims/build.mjs cessna-live&lt;/code&gt;, and likewise for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v8-live&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strandbeest-live&lt;/code&gt;). Video capture uses Playwright + Chromium, then ffmpeg at 60 fps; audio is synthesized in Python. Built on &lt;a href=&quot;https://github.com/whaleyxbt/claude-motion&quot;&gt;whaleyxbt/claude-motion&lt;/a&gt; (MIT). Numbers in the panel come from an AI-written model, so verify them against a textbook or a hand calculation before using them in class. A classroom check against the rack-travel formula is in lesson 06-04. &lt;a href=&quot;https://danielthegoldman.com/guides/teardown/en/&quot;&gt;Usage guide&lt;/a&gt;. &lt;a href=&quot;https://www.facebook.com/reel/1442856421362380/&quot;&gt;Short intro reel&lt;/a&gt;. As of 2026-11-28 the repo had about 4.3k stars.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;open-source-platforms-supplementary&quot;&gt;Open-source platforms (supplementary)&lt;/h2&gt;

&lt;p&gt;Hardware and software platforms you can read, build from, or use as a later project. They complement the Capstone kits; none replaces the course robot.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;&lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt;&lt;/em&gt; (UC Berkeley Hybrid Robotics). An open-source, 3D-printed humanoid, 0.8 m tall and 16 kg, with a hardware cost under 5,000 USD at U.S. market prices (add shipping and tax if you source parts in Vietnam). Two sizes of modular 3D-printed cycloidal gearbox actuators (6512 and 5010); the remaining parts are off-the-shelf from e-commerce. An Intel N95 mini PC in the torso talks to the joints over four 1 Mbps CAN 2.0 buses. Locomotion is trained with reinforcement learning. The RSS 2025 paper reports zero-shot sim-to-real transfer of an Isaac Gym policy; the current codebase trains in Isaac Lab, checks the policy in MuJoCo, then deploys on the robot. Teleoperation uses SteamVR. Code is MIT; other assets are CC BY-SA 4.0. Paper: &lt;a href=&quot;https://arxiv.org/abs/2504.17249&quot;&gt;arXiv:2504.17249&lt;/a&gt;, &lt;a href=&quot;https://www.roboticsproceedings.org/rss21/p062.pdf&quot;&gt;RSS 2025&lt;/a&gt;. Code: &lt;a href=&quot;https://github.com/HybridRobotics/berkeley-humanoid-lite&quot;&gt;HybridRobotics/berkeley-humanoid-lite&lt;/a&gt;. Docs: &lt;a href=&quot;https://berkeley-humanoid-lite.gitbook.io/docs&quot;&gt;GitBook&lt;/a&gt;. &lt;a href=&quot;https://www.youtube.com/watch?v=dIdJGkMDFl4&quot;&gt;Video&lt;/a&gt;. &lt;a href=&quot;https://habr.com/ru/articles/905654/&quot;&gt;Russian overview (Habr)&lt;/a&gt;. Release v1.1.0 is dated 2025-09-07. A 60-hour durability test saw backlash grow from wear; the v1.1.0 notes say a planned V2 is exploring off-the-shelf actuators because the printed cycloidal units are too fragile for high-performance work. A cycloidal-ratio check is in lesson 06-03; a sim-to-real reading activity is in lesson 10-05. As of 2026-11-30 the repo had about 2.3k stars.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;simulation-assets-and-tools-supplementary&quot;&gt;Simulation assets and tools (supplementary)&lt;/h2&gt;

&lt;p&gt;Libraries and generators that feed Isaac Sim or Isaac Lab. They are optional; the Capstone robot and Gazebo path in the core weeks do not depend on them. A practical hardware and cloud guide is the optional lesson &lt;a href=&quot;/contents/en/chapter12/12_08_home_simulation_lab/&quot;&gt;Home simulation lab&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;&lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt;&lt;/em&gt; (NVIDIA, currently Alpha; announced 2026-10-21). A free, ungated library of more than 2,000 simulation-ready OpenUSD assets on Hugging Face, including arms and grippers from FANUC, Franka, Robotiq, Universal Robots, and other OEMs, plus warehouse and AI-factory equipment. A SimReady asset already carries collision geometry, mass and friction, joints with limits and drives, semantic labels, and materials. Profiles and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;simready-validate&lt;/code&gt; tool are in &lt;a href=&quot;https://github.com/NVIDIA/simready-foundation&quot;&gt;SimReady Foundation&lt;/a&gt;. Find assets through the &lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;Hugging Face Space&lt;/a&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;simready-search&lt;/code&gt; package, or the Isaac Sim Asset Search extension (CC-BY-4.0 packages only in that browser). Many assets are CC-BY-4.0, but licenses vary (some FANUC packages are Apache-2.0; some UR packages need a careful read) — check each asset before use. Announcement: &lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-central-assets-robotics-ai-factories&quot;&gt;Hugging Face blog&lt;/a&gt;. Pick-and-place walkthrough: &lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-assets-robot-pick-and-place&quot;&gt;UR17G + Robotiq 2F-140&lt;/a&gt;. Docs: &lt;a href=&quot;https://docs.omniverse.nvidia.com/simready/latest/simready-central.html&quot;&gt;SimReady Central Library&lt;/a&gt;. Classroom uses are noted in lessons 10-05 and 12-01.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;&lt;a href=&quot;https://github.com/NVIDIA-AI-Blueprints/3d-object-generation&quot;&gt;NVIDIA AI Blueprint: 3D Object Generation&lt;/a&gt;&lt;/em&gt; (Apache-2.0). A Windows-only local workflow: a text prompt goes through Qwen3-4B, then SANA Sprint, then TRELLIS, and writes a textured &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.glb&lt;/code&gt; you can import into Blender (4.2+). Official GPU list: RTX 4080 / 4090 / 5080 / 5090 / RTX 6000 Ada; 48 GB of system RAM is recommended. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.glb&lt;/code&gt; is &lt;strong&gt;not&lt;/strong&gt; SimReady: it has no USD physics, collision, mass, joints, or semantic labels. To use it in Isaac Sim you still need a USD conversion and a physics pass, then validation (see &lt;a href=&quot;https://developer.nvidia.com/blog/5-steps-to-create-simready-assets-for-robotics-with-frontier-ai-models/&quot;&gt;Five steps to create SimReady assets&lt;/a&gt;). Useful later for randomizing props, not as a substitute for OEM robot assets from SimReady Central.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;textbooks&quot;&gt;Textbooks&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Author, A. (Year). &lt;em&gt;&lt;a href=&quot;https://example.com&quot;&gt;Book Title&lt;/a&gt;&lt;/em&gt;. Publisher.&lt;/li&gt;
  &lt;li&gt;Author, B. (Year). &lt;em&gt;&lt;a href=&quot;https://example.com&quot;&gt;Another Book Title&lt;/a&gt;&lt;/em&gt;. Publisher.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;online-courses-and-lectures&quot;&gt;Online Courses and Lectures&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Instructor, C. (Year). &lt;em&gt;&lt;a href=&quot;https://example.com&quot;&gt;Course Title&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;University. &lt;em&gt;&lt;a href=&quot;https://example.com&quot;&gt;Lecture Series&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;papers-and-articles&quot;&gt;Papers and Articles&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Author, D. et al. (Year). &lt;em&gt;&lt;a href=&quot;https://example.com&quot;&gt;Paper Title&lt;/a&gt;&lt;/em&gt;. Journal Name, Vol. X, pp. XXX-XXX.&lt;/li&gt;
  &lt;li&gt;Author, E. (Year). &lt;em&gt;&lt;a href=&quot;https://example.com&quot;&gt;Article Title&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;online-resources&quot;&gt;Online Resources&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Wikipedia. &lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Topic&quot;&gt;Topic Name&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Documentation. &lt;em&gt;&lt;a href=&quot;https://example.com&quot;&gt;Tool/Library Name&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;additional-materials&quot;&gt;Additional Materials&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Author, F. (Year). &lt;em&gt;&lt;a href=&quot;https://example.com&quot;&gt;Tutorial/Guide Title&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Organization. &lt;em&gt;&lt;a href=&quot;https://example.com&quot;&gt;Resource Name&lt;/a&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Replace these placeholder references with actual references relevant to your course content.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>makers</title>
   <link href="https://robotics-self-learning.pages.dev/home/makers/"/>
   <updated>2021-02-03T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/home/makers</id>
   <content type="html">&lt;p&gt;Course maintained by Nguyen Le Linh. Contributions welcome via GitHub issues/PRs.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Conventions</title>
   <link href="https://robotics-self-learning.pages.dev/contribution/conventions/"/>
   <updated>2021-02-03T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contribution/conventions</id>
   <content type="html">&lt;h2 id=&quot;1-directory-convention&quot;&gt;1. Directory Convention&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Main content lives under &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;contents/{lang}/chapterXX/&lt;/code&gt; (e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;contents/en/chapter01/&lt;/code&gt;). Images go in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;img/chapter_img/&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;A typical chapter directory looks like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;contents/
├── en/
│   └── chapter01/
│       ├── _posts/
│       │   ├── 21-01-07-01_00_Introduction.md
│       │   ├── 21-01-07-01_01_optimization_problems.md
│       │   └── ...
│       └── index.html
└── vi/
    └── chapter01/
        ├── _posts/
        └── index.html
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Jekyll treats Markdown or HTML files inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts&lt;/code&gt; as blog posts. To add a new post, create a file in the chapter’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts/&lt;/code&gt; directory.&lt;/li&gt;
  &lt;li&gt;Post filenames must follow this naming convention:
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YYYY-MM-DD-post_name.md&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Files outside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;contents/&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;img/&lt;/code&gt; are site configuration. For stability, open an issue instead of editing config directly (config changes can make PR merges difficult).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-posting-convention&quot;&gt;2. Posting Convention&lt;/h2&gt;

&lt;h3 id=&quot;21-header-fields&quot;&gt;2.1. Header Fields&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Every post must include front matter like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
layout: post
title: Quasi-Newton Methods
chapter: &quot;18&quot;
order: 1
owner: &quot;Your Name&quot;
lang: en
categories:
- chapter18
lesson_type: required
---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;layout&lt;/strong&gt; must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;title&lt;/strong&gt; can be any descriptive string.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;chapter&lt;/strong&gt; is the two-digit chapter number as a string (e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;01&quot;&lt;/code&gt; for chapter 1).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;order&lt;/strong&gt; controls sort order within the chapter and prev/next navigation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;owner&lt;/strong&gt; is the post maintainer.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;lang&lt;/strong&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;en&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vi&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;categories&lt;/strong&gt; must match the chapter directory name (e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chapter18&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;lesson_type&lt;/strong&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;required&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;optional&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;22-latex&quot;&gt;2.2. LaTeX&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Write formulas using LaTeX syntax.&lt;/li&gt;
  &lt;li&gt;Use double dollar signs (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$$&lt;/code&gt;) to mark math.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$$\theta x_1 + (1-\theta)x_2 \in C$$
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;renders as:&lt;/p&gt;

\[\theta x_1 + (1-\theta)x_2 \in C\]

&lt;h3 id=&quot;23-image-convention&quot;&gt;2.3. Image Convention&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;When inserting images in a post, use this format:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;figure class=&quot;image&quot; style=&quot;align: center;&quot;&amp;gt;
&amp;lt;p align=&quot;center&quot;&amp;gt;
  &amp;lt;img src=&quot;{image_path}&quot; alt=&quot;{description of image}&quot; width=&quot;{scale_ratio}%&quot; height=&quot;{scale_ratio}%&quot;&amp;gt;
  &amp;lt;figcaption style=&quot;text-align: center;&quot;&amp;gt;{figcaption}&amp;lt;/figcaption&amp;gt;
&amp;lt;/p&amp;gt;
&amp;lt;/figure&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;The figure class must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Replace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{}&lt;/code&gt; placeholders with appropriate values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alternatively, use markdown with the site image URL:&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;![&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Alt text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;sx&quot;&gt;/img/chapter_img/your-image.png&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;24-hyperlink-convention&quot;&gt;2.4 Hyperlink Convention&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;For links to other posts in this site, use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;multilang_post_url&lt;/code&gt; tag. Example:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[Your Post Title](#post-not-found)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;For external URLs:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[External Link](&amp;lt;https://example.com&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-github-convention&quot;&gt;3. GitHub Convention&lt;/h2&gt;

&lt;p&gt;If you have questions or find something to fix, use one of these:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Leave a comment on the post&lt;/li&gt;
  &lt;li&gt;Open an issue in the repository&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To add or edit content, create a new branch, make your changes, then open a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pull Request&lt;/code&gt;. Anyone can contribute new or updated content.&lt;/p&gt;

&lt;h3 id=&quot;31-repository-policy&quot;&gt;3.1. Repository Policy&lt;/h3&gt;

&lt;p&gt;Merging into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; requires approval from at least one reviewer. If CODEOWNERS is configured, chapter reviewers are assigned automatically.&lt;/p&gt;

&lt;h3 id=&quot;32-branch-naming-convention&quot;&gt;3.2. Branch Naming Convention&lt;/h3&gt;

&lt;p&gt;Name branches using this pattern:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[feature|bugfix]/[chapter**|settings]-change-description
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Use two prefixes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;feature&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Migration work&lt;/li&gt;
      &lt;li&gt;Changes to text, formulas, or images&lt;/li&gt;
      &lt;li&gt;New content&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;bugfix&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Typo fixes&lt;/li&gt;
      &lt;li&gt;Broken LaTeX rendering&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feature/chapter01-migration&lt;/code&gt;: chapter01 migration&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feature/chapter01-fix-formula&lt;/code&gt;: update formula in chapter01&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feature/settings-update-branch-convention&lt;/code&gt;: update conventions&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bugfix/chapter01-fix-typo&lt;/code&gt;: fix typo in chapter01&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>how to contribute</title>
   <link href="https://robotics-self-learning.pages.dev/home/link_to_how_to_contribute/"/>
   <updated>2021-01-27T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/home/link_to_how_to_contribute</id>
   <content type="html">&lt;h1 id=&quot;how-to-contribute&quot;&gt;How to Contribute&lt;/h1&gt;

&lt;p&gt;We welcome contributions from the community! Whether you want to fix a typo, add an example, or contribute a whole new chapter, your help is appreciated.&lt;/p&gt;

&lt;h2 id=&quot;ways-to-contribute&quot;&gt;Ways to Contribute&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Fix errors&lt;/strong&gt;: Found a typo or mistake? Submit a fix!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Add examples&lt;/strong&gt;: Share practical examples or exercises&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Improve explanations&lt;/strong&gt;: Make concepts clearer&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Translate content&lt;/strong&gt;: Help make the course available in more languages&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Add resources&lt;/strong&gt;: Suggest additional reading materials or tools&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;getting-started&quot;&gt;Getting Started&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Fork the repository&lt;/strong&gt; on GitHub&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Clone your fork&lt;/strong&gt; to your local machine&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Create a new branch&lt;/strong&gt; for your changes&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Make your changes&lt;/strong&gt; following our style guide&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Test your changes&lt;/strong&gt; locally&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Submit a pull request&lt;/strong&gt; with a clear description&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;style-guidelines&quot;&gt;Style Guidelines&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Use clear, concise language&lt;/li&gt;
  &lt;li&gt;Follow the existing formatting conventions&lt;/li&gt;
  &lt;li&gt;Include examples where appropriate&lt;/li&gt;
  &lt;li&gt;Test all code snippets&lt;/li&gt;
  &lt;li&gt;Use proper LaTeX formatting for mathematical expressions&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;questions&quot;&gt;Questions?&lt;/h2&gt;

&lt;p&gt;If you have questions about contributing, please:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Check our &lt;a href=&quot;/contribution/how_to_contribute/&quot;&gt;Contributing Guide&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Open an issue on GitHub&lt;/li&gt;
  &lt;li&gt;Contact the course maintainers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for helping improve this course!&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Initial Settings</title>
   <link href="https://robotics-self-learning.pages.dev/contribution/initial_settings/"/>
   <updated>2021-01-27T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contribution/initial_settings</id>
   <content type="html">&lt;p&gt;The contents of this repository are hosted as a &lt;a href=&quot;https://convex-optimization-for-all.github.io/&quot;&gt;Github Blog&lt;/a&gt; using Jekyll.
Therefore, to edit existing content or create new content, you must follow Jekyll’s directory structure and content writing conventions.
Additionally, you need to verify that changes are properly reflected in your local environment (your current computer) through a web browser.&lt;/p&gt;

&lt;p&gt;We have compiled environment setup instructions for those who are not familiar with GitHub or Jekyll.
If you have difficulties following the guide, please leave an &lt;a href=&quot;https://github.com/convex-optimization-for-all/convex-optimization-for-all.github.io/issues&quot;&gt;issue&lt;/a&gt; in the repository or contact us via the email below for assistance.&lt;/p&gt;

&lt;p&gt;(Kyeongmin Woo, wgm0601@gmail.com)&lt;/p&gt;

&lt;h2 id=&quot;1-git-installation&quot;&gt;1. Git Installation&lt;/h2&gt;

&lt;p&gt;All work management for this blog is performed through Git and GitHub. Please visit the website below to install Git.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://git-scm.com/downloads&quot;&gt;https://git-scm.com/downloads&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;2-downloading-the-repository&quot;&gt;2. Downloading the Repository&lt;/h2&gt;

&lt;p&gt;To modify the blog, enter the following command in the terminal to download the blog’s source code.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/convex-optimization-for-all/convex-optimization-for-all.github.io.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-local-hosting&quot;&gt;3. Local Hosting&lt;/h2&gt;

&lt;p&gt;Before applying changed or modified content to the blog, you must verify that the work has been performed as intended through local hosting.
If you merge work content that does not follow Jekyll’s required conventions into the repository, the blog hosted on the actual web may not function properly.
For local hosting setup, you can choose between two methods: using a virtual environment (Docker) (Option 1) or directly installing the Jekyll environment locally (Option 2).
After completing the local hosting setup and running the local server, you can check your blog content through the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;127.0.0.1:4000&lt;/code&gt; address in your web browser.&lt;/p&gt;

&lt;h3 id=&quot;3-1-option-1-docker-installation&quot;&gt;3-1. (Option 1) Docker Installation&lt;/h3&gt;

&lt;h3 id=&quot;a-docker-installation&quot;&gt;A. Docker Installation&lt;/h3&gt;

&lt;p&gt;Using Docker enables local hosting without direct environment installation on your local machine.
Please visit the website below to install Docker.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.docker.com/get-docker/&quot;&gt;https://docs.docker.com/get-docker/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;b-local-hosting&quot;&gt;B. Local Hosting&lt;/h3&gt;

&lt;p&gt;Enter the following command in the terminal.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;3-2-option-2-jekyll-environment-installation&quot;&gt;3-2. (Option 2) Jekyll Environment Installation&lt;/h3&gt;

&lt;h3 id=&quot;a-jekyll-and-ruby-package-installation&quot;&gt;A. Jekyll and Ruby Package Installation&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://jekyllrb.com/docs/installation/&quot;&gt;Installing Ruby&lt;/a&gt;: Jekyll is built with Ruby. Therefore, you need to install Ruby to use Jekyll.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://jekyllrb.com/docs/&quot;&gt;Installing Jekyll&lt;/a&gt;: Once Ruby is installed, enter the cloned repository and install Jekyll.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://jekyllrb.com/docs/&quot;&gt;Installing Bundle Gem&lt;/a&gt;: You need to additionally install Ruby packages required for hosting. Run the following command in the repository’s project directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;bundle &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;b-local-hosting-1&quot;&gt;B. Local Hosting&lt;/h3&gt;

&lt;p&gt;Enter the following command in the terminal.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ jekyll serve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If hosting doesn’t work, you can also try the following command.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ bundle exec jekyll serve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If both commands don’t work, the Jekyll environment has not been properly installed.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>How to Contribute</title>
   <link href="https://robotics-self-learning.pages.dev/contribution/how_to_contribute/"/>
   <updated>2021-01-27T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contribution/how_to_contribute</id>
   <content type="html">&lt;hr /&gt;

&lt;h2 id=&quot;1-editing-content-directly&quot;&gt;1. Editing Content Directly&lt;/h2&gt;

&lt;h3 id=&quot;1-open-your-local-repository-directory-if-you-do-not-have-a-local-copy-yet-see-initial-settings&quot;&gt;(1) Open your local repository directory. If you do not have a local copy yet, see &lt;a href=&quot;/contribution/2021/01/27/initial_settings/&quot;&gt;Initial Settings&lt;/a&gt;.&lt;/h3&gt;

&lt;h3 id=&quot;2-sync-with-the-remote-repository&quot;&gt;(2) Sync with the remote repository.&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git checkout main
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git pull &lt;span class=&quot;nt&quot;&gt;--all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;3-create-a-new-branch-for-your-changes-use-prefixchapterreason-see-branch-naming-convention-example&quot;&gt;(3) Create a new branch for your changes. Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[prefix]/[chapter]/[reason]&lt;/code&gt; (see &lt;a href=&quot;/contribution/2021/02/03/conventions/&quot;&gt;Branch Naming Convention&lt;/a&gt;). Example:&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git checkout &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; bugfix/chapter01-fix-typo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;4-edit-files-follow-the-conventions-when-creating-or-updating-content&quot;&gt;(4) Edit files. Follow the &lt;a href=&quot;/contribution/2021/02/03/conventions/&quot;&gt;Conventions&lt;/a&gt; when creating or updating content.&lt;/h3&gt;

&lt;h3 id=&quot;5-push-to-the-remote-example&quot;&gt;(5) Push to the remote. Example:&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git push origin bugfix/chapter01-fix-typo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;6-open-a-pull-request-to-main-on-github-see-github-docs-for-details&quot;&gt;(6) Open a Pull Request to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; on GitHub. See GitHub Docs for details:&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request&quot;&gt;Creating a pull request&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;2-requesting-content-changes&quot;&gt;2. Requesting Content Changes&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;You can open an &lt;a href=&quot;https://github.com/your-username/your-repo-name/issues&quot;&gt;Issue&lt;/a&gt; on the GitHub repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
</content>
 </entry>
 
 <entry>
   <title>Tài nguyên</title>
   <link href="https://robotics-self-learning.pages.dev/home/resources/"/>
   <updated>2021-01-22T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/home/resources</id>
   <content type="html">&lt;h2 id=&quot;tài-nguyên&quot;&gt;Tài nguyên&lt;/h2&gt;

&lt;p&gt;Link dưới đây trả về trang khi kiểm tra tháng 10 năm 2026. Trang shop là cổng tìm kiếm hoặc trang chủ: kết quả đổi theo tồn kho, không phải một món đã chốt giá. Giá điền vào BOM khi bạn mở listing. Đường ROS trên khóa là rclpy và rclcpp. &lt;a href=&quot;https://github.com/tiiuae/rclgo&quot;&gt;rclgo&lt;/a&gt; là đường phụ.&lt;/p&gt;

&lt;h2 id=&quot;tài-liệu-chính&quot;&gt;Tài liệu chính&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/&quot;&gt;ROS 2 Jazzy&lt;/a&gt; là bản đi với Ubuntu 24.04. Cài bằng &lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation/Ubuntu-Install-Debs.html&quot;&gt;Ubuntu deb packages&lt;/a&gt;, rồi làm &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials.html&quot;&gt;tutorials&lt;/a&gt;. Trên Pi, đọc &lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Installing-on-Raspberry-Pi.html&quot;&gt;Installing ROS 2 on a Raspberry Pi&lt;/a&gt; sau khi máy đã là Ubuntu 24.04, không phải Raspberry Pi OS. Ảnh Ubuntu 24.04 nằm ở &lt;a href=&quot;https://cdimage.ubuntu.com/ubuntu/releases/24.04/release/&quot;&gt;cdimage 24.04&lt;/a&gt;. Trang &lt;a href=&quot;https://ubuntu.com/download/raspberry-pi&quot;&gt;Ubuntu cho Raspberry Pi&lt;/a&gt; và &lt;a href=&quot;https://ubuntu.com/tutorials/how-to-install-ubuntu-on-your-raspberry-pi&quot;&gt;hướng dẫn cài&lt;/a&gt; có thể đề 26.04; khóa này vẫn dùng 24.04 vì kho Jazzy.&lt;/p&gt;

&lt;p&gt;Khung và thời gian: &lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-TF2.html&quot;&gt;About TF2&lt;/a&gt;, &lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Domain-ID.html&quot;&gt;ROS_DOMAIN_ID&lt;/a&gt;, &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/URDF-Main.html&quot;&gt;URDF&lt;/a&gt;, &lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt;, &lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Mô phỏng: &lt;a href=&quot;https://gazebosim.org/docs/harmonic/getstarted/&quot;&gt;Gazebo Harmonic, bắt đầu&lt;/a&gt;, &lt;a href=&quot;https://gazebosim.org/docs/harmonic/ros_installation&quot;&gt;cài với ROS&lt;/a&gt;, mã &lt;a href=&quot;https://github.com/gazebosim/ros_gz&quot;&gt;ros_gz&lt;/a&gt;. Docker: &lt;a href=&quot;https://hub.docker.com/_/ros&quot;&gt;ảnh ros&lt;/a&gt; (chọn tag Jazzy đang liệt kê), &lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Run-2-nodes-in-single-or-separate-docker-containers.html&quot;&gt;hai node trong container&lt;/a&gt;, &lt;a href=&quot;https://docs.docker.com/engine/install/ubuntu/&quot;&gt;Docker Engine trên Ubuntu&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Định vị và đi: &lt;a href=&quot;https://docs.nav2.org/&quot;&gt;Nav2&lt;/a&gt;, tutorial Jazzy &lt;a href=&quot;https://docs.nav2.org/jazzy/tutorials/general_tutorials/navigation2_with_slam/navigation2_with_slam/&quot;&gt;Navigation2 with SLAM&lt;/a&gt;, mã &lt;a href=&quot;https://github.com/ros-navigation/navigation2&quot;&gt;navigation2&lt;/a&gt;, &lt;a href=&quot;https://github.com/SteveMacenski/slam_toolbox&quot;&gt;slam_toolbox&lt;/a&gt;. Tay và điều khiển khớp: &lt;a href=&quot;https://moveit.picknik.ai/main/index.html&quot;&gt;MoveIt 2&lt;/a&gt;, &lt;a href=&quot;https://control.ros.org/&quot;&gt;ros2_control&lt;/a&gt;, &lt;a href=&quot;https://github.com/ros-controls/ros2_controllers&quot;&gt;ros2_controllers&lt;/a&gt;. Xe mẫu: &lt;a href=&quot;https://github.com/linorobot/linorobot2&quot;&gt;linorobot2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;micro-ROS: &lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro.ros.org&lt;/a&gt;, &lt;a href=&quot;https://github.com/micro-ROS/micro-ROS-Agent&quot;&gt;micro-ROS-Agent&lt;/a&gt;, &lt;a href=&quot;https://github.com/micro-ROS/micro_ros_setup&quot;&gt;micro_ros_setup&lt;/a&gt;. Đừng giả định gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-micro-ros-agent&lt;/code&gt; có trên apt; tìm bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt-cache&lt;/code&gt;, không có thì build agent. Unit systemd: &lt;a href=&quot;https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html&quot;&gt;systemd.service&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Nhúng: &lt;a href=&quot;https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html&quot;&gt;ESP-IDF get started&lt;/a&gt;, &lt;a href=&quot;https://docs.platformio.org/&quot;&gt;PlatformIO&lt;/a&gt;, &lt;a href=&quot;https://docs.arduino.cc/&quot;&gt;Arduino docs&lt;/a&gt;, &lt;a href=&quot;https://mqtt.org/&quot;&gt;MQTT&lt;/a&gt;. Mức logic: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;SparkFun logic levels&lt;/a&gt;. Driver tham chiếu: &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu 713, TB6612FNG&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;SBC NVIDIA: &lt;a href=&quot;https://docs.nvidia.com/jetson/orin-nano-devkit/user-guide/latest/quick_start.html&quot;&gt;Orin Nano quick start&lt;/a&gt;, &lt;a href=&quot;https://docs.nvidia.com/jetson/jetpack/install-setup/index.html&quot;&gt;JetPack install&lt;/a&gt;. Chủ đề diễn đàn &lt;a href=&quot;https://forums.developer.nvidia.com/t/is-ros2-jazzy-the-correct-version-to-use-with-jetpack-7-2-1-ubuntu-24-04/382439&quot;&gt;Jazzy với JetPack 7.2.1 trên Ubuntu 24.04&lt;/a&gt; (tháng 9 năm 2026) nói Jazzy là bản ROS khớp JetPack 7. JetPack 6 vẫn là Ubuntu 22.04.&lt;/p&gt;

&lt;p&gt;Lidar rẻ trong Capstone B: &lt;a href=&quot;https://www.slamtec.com/en/Lidar/A1&quot;&gt;RPLIDAR A1&lt;/a&gt;, driver &lt;a href=&quot;https://github.com/Slamtec/rplidar_ros&quot;&gt;rplidar_ros nhánh ros2&lt;/a&gt;, &lt;a href=&quot;https://github.com/ldrobotSensorTeam/ldlidar_stl_ros2&quot;&gt;LD06 / ldlidar_stl_ros2&lt;/a&gt;, trang &lt;a href=&quot;https://www.ldrobot.com/&quot;&gt;LDRobot&lt;/a&gt;. README Slamtec liệt kê Humble, Galactic, Foxy; nếu colcon lỗi trên Jazzy thì dừng và ghi log.&lt;/p&gt;

&lt;p&gt;Pin: &lt;a href=&quot;https://batteryuniversity.com/article/bu-205-types-of-lithium-ion&quot;&gt;BU-205 các loại Li-ion&lt;/a&gt;, &lt;a href=&quot;https://batteryuniversity.com/article/bu-409-charging-lithium-ion&quot;&gt;BU-409 sạc Li-ion&lt;/a&gt;, &lt;a href=&quot;https://batteryuniversity.com/article/bu-409b-charging-lithium-iron-phosphate&quot;&gt;BU-409b sạc LiFePO4&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;khóa-và-sách-mở&quot;&gt;Khóa và sách mở&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://modernrobotics.northwestern.edu/&quot;&gt;Modern Robotics&lt;/a&gt; và bản wiki &lt;a href=&quot;https://hades.mech.northwestern.edu/index.php/Modern_Robotics&quot;&gt;Modern Robotics&lt;/a&gt; cho động học, trước và sau bài toán chương 08. &lt;a href=&quot;https://manipulation.mit.edu/&quot;&gt;MIT Manipulation&lt;/a&gt; và &lt;a href=&quot;https://underactuated.mit.edu/&quot;&gt;Underactuated Robotics&lt;/a&gt; là đọc thêm, không nằm trong 24 tuần. PID thực hành: &lt;a href=&quot;https://controlguru.com/table-of-contents/&quot;&gt;Control Guru, mục lục&lt;/a&gt;. Cây hành vi: &lt;a href=&quot;https://www.behaviortree.dev/&quot;&gt;BehaviorTree.CPP&lt;/a&gt;. Duckietown đổi tài liệu sang &lt;a href=&quot;https://docs.duckietown.com/ente/&quot;&gt;docs.duckietown.com/ente&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;robot-learning&quot;&gt;Robot learning&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/huggingface/lerobot&quot;&gt;LeRobot&lt;/a&gt;, &lt;a href=&quot;https://huggingface.co/docs/lerobot/installation&quot;&gt;cài đặt&lt;/a&gt;, &lt;a href=&quot;https://huggingface.co/docs/lerobot/so101&quot;&gt;SO-101&lt;/a&gt;, &lt;a href=&quot;https://huggingface.co/docs/lerobot/il_robots&quot;&gt;imitation trên robot&lt;/a&gt;. Trang imitation dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--policy.type=act&lt;/code&gt; trong ví dụ train. &lt;a href=&quot;https://tonyzhaozh.github.io/aloha/&quot;&gt;ALOHA / ACT&lt;/a&gt;, &lt;a href=&quot;https://diffusion-policy.cs.columbia.edu/&quot;&gt;Diffusion Policy&lt;/a&gt;, bài &lt;a href=&quot;https://arxiv.org/abs/2303.04137&quot;&gt;arxiv 2303.04137&lt;/a&gt;. VLA: &lt;a href=&quot;https://robotics-transformer1.github.io/&quot;&gt;RT-1&lt;/a&gt;, &lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;RT-2, arXiv:2307.15818&lt;/a&gt;, &lt;a href=&quot;https://robotics-transformer-x.github.io/&quot;&gt;Open X-Embodiment&lt;/a&gt;, &lt;a href=&quot;https://openvla.github.io/&quot;&gt;OpenVLA&lt;/a&gt; và &lt;a href=&quot;https://arxiv.org/abs/2406.09246&quot;&gt;arXiv:2406.09246&lt;/a&gt;, &lt;a href=&quot;https://www.pi.website/blog/pi0&quot;&gt;π0&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;youtube&quot;&gt;YouTube&lt;/h2&gt;

&lt;p&gt;Kênh giữ cho tên gói và bản phát hành: &lt;a href=&quot;https://www.youtube.com/@ROSOrg&quot;&gt;ROS&lt;/a&gt;, &lt;a href=&quot;https://www.youtube.com/@gazebosim&quot;&gt;Gazebo&lt;/a&gt;, &lt;a href=&quot;https://www.youtube.com/@NVIDIADeveloper&quot;&gt;NVIDIA Developer&lt;/a&gt;, &lt;a href=&quot;https://www.youtube.com/@raspberrypi&quot;&gt;Raspberry Pi&lt;/a&gt;, &lt;a href=&quot;https://www.youtube.com/@HuggingFace&quot;&gt;Hugging Face&lt;/a&gt;. Đi từng bước Jazzy trên xe nhỏ: &lt;a href=&quot;https://www.youtube.com/c/ArticulatedRobotics&quot;&gt;Articulated Robotics&lt;/a&gt; và trang &lt;a href=&quot;https://articulatedrobotics.xyz/&quot;&gt;articulatedrobotics.xyz&lt;/a&gt;. Video không thay lệnh trong bài; khi lệch bản distro, tin docs và bài lab.&lt;/p&gt;

&lt;h2 id=&quot;cửa-hàng-linh-kiện-ở-việt-nam&quot;&gt;Cửa hàng linh kiện ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Mở trang, đối chiếu ảnh và thông số, rồi tự điền giá. Đã mở được tháng 10 năm 2026:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt; — tìm &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi&quot;&gt;Raspberry Pi&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=lidar&quot;&gt;lidar&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=lipo&quot;&gt;LiPo&lt;/a&gt;. Giá một số món bàn lab trong BOM là snapshot ngày 23 tháng 9 năm 2026 trên chính listing đó.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt;, &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;, &lt;a href=&quot;https://dientu4u.com/&quot;&gt;Điện Tử 4U&lt;/a&gt;, &lt;a href=&quot;https://banlinhkien.com/&quot;&gt;Bán Linh Kiện&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/&quot;&gt;Shopee&lt;/a&gt;: &lt;a href=&quot;https://shopee.vn/search?keyword=raspberry%20pi%205&quot;&gt;Raspberry Pi 5&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=jetson%20orin%20nano&quot;&gt;Jetson Orin Nano&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=lidar&quot;&gt;lidar&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=bms%202s&quot;&gt;BMS 2S&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/&quot;&gt;Lazada&lt;/a&gt;: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=bms%202s&quot;&gt;BMS 2S&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=lidar&quot;&gt;lidar&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Đối chiếu hóa học pin trên tem với sạc. LiFePO4 không sạc bằng hồ sơ 4,20 V/cell của Li-ion.&lt;/p&gt;

&lt;h2 id=&quot;video-theo-chương&quot;&gt;Video theo chương&lt;/h2&gt;

&lt;p&gt;Mỗi chương một video nên xem trước. Tiêu đề và kênh lấy từ oEmbed ngày 26 tháng 10 năm 2026. Các bài trong chương có thêm một đến ba video ở mục Video tham khảo.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Chương 00: &lt;a href=&quot;https://www.youtube.com/watch?v=7TVWlADXwRw&quot;&gt;What Is ROS2? - Framework Overview&lt;/a&gt; — Raymond Andrade. Giới thiệu ROS 2 trước khi chọn lộ trình.&lt;/li&gt;
  &lt;li&gt;Chương 01: &lt;a href=&quot;https://www.youtube.com/watch?v=HsLLq6Rm5tU&quot;&gt;Ohms Law Explained - The basics circuit theory&lt;/a&gt; — The Engineering Mindset. Định luật Ôm, nền của chương điện.&lt;/li&gt;
  &lt;li&gt;Chương 02: &lt;a href=&quot;https://www.youtube.com/watch?v=sJtJJU3c3xE&quot;&gt;Lập trình ESP32 - Bài 1 - Cách nạp chương trình cho ESP32 bằng Arduino IDE - Tự học lập trình IOT&lt;/a&gt; — Điện thông minh E-smart. Nạp chương trình nhấp LED cho ESP32, tiếng Việt.&lt;/li&gt;
  &lt;li&gt;Chương 03: &lt;a href=&quot;https://www.youtube.com/watch?v=-Yicg2TTMPs&quot;&gt;Understanding State Machines, Part 1: What Are They?&lt;/a&gt; — MATLAB. Máy trạng thái, hình của firmware chương 03.&lt;/li&gt;
  &lt;li&gt;Chương 04: &lt;a href=&quot;https://www.youtube.com/watch?v=IyGwvGzrqp8&quot;&gt;Serial Communications Explained: UART, I2C, and SPI&lt;/a&gt; — Electronoobs. UART, I2C, SPI trên một video.&lt;/li&gt;
  &lt;li&gt;Chương 05: &lt;a href=&quot;https://www.youtube.com/watch?v=JPPTRj0KWbg&quot;&gt;TB6612FNG H-Bridge Motor Controller - Better than L298N?&lt;/a&gt; — DroneBot Workshop. TB6612 cạnh L298N, đúng bài nhận driver.&lt;/li&gt;
  &lt;li&gt;Chương 06: &lt;a href=&quot;https://www.youtube.com/watch?v=2QE3LG5h2tw&quot;&gt;Differential Drive &amp;amp; Odometry Explained: How It Works&lt;/a&gt; — Netizen Robotics. Vi sai và odometry, khung của chương cơ khí.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;Chương 07: [BMS (Battery Management System)&lt;/td&gt;
          &lt;td&gt; &lt;/td&gt;
          &lt;td&gt;DIY or Buy&lt;/td&gt;
          &lt;td&gt; &lt;/td&gt;
          &lt;td&gt;Properly protecting Li-Ion/Li-Po Battery Packs](https://www.youtube.com/watch?v=rT-1gvkFj60) — GreatScott!. BMS và pack lithium trước khi lắp xe.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;Chương 08: &lt;a href=&quot;https://www.youtube.com/watch?v=UR0hOmjaHp0&quot;&gt;PID Control - A brief introduction&lt;/a&gt; — Brian Douglas. PID, bài chỉnh số của chương.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;Chương 09: [The ROS Transform System (TF)&lt;/td&gt;
          &lt;td&gt;Getting Ready to Build Robots with ROS #6](https://www.youtube.com/watch?v=QyvHhY4Y_Y8) — Articulated Robotics. Cây TF2, cửa sang chương mô phỏng.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;Chương 10: &lt;a href=&quot;https://www.youtube.com/watch?v=dY8JxldcuqA&quot;&gt;Correctly Install Gazebo Harmonic Inside of ROS2 Jazzy Installation -Fix Gazebo Installation Issues&lt;/a&gt; — Aleksandar Haber PhD. Gazebo Harmonic trong môi trường Jazzy.&lt;/li&gt;
  &lt;li&gt;Chương 11: &lt;a href=&quot;https://www.youtube.com/watch?v=jkoGkAd0GYk&quot;&gt;Making robot navigation easy with Nav2 and ROS!&lt;/a&gt; — Articulated Robotics. Nav2, tour rồi lab Capstone B.&lt;/li&gt;
  &lt;li&gt;Chương 12: &lt;a href=&quot;https://www.youtube.com/watch?v=7tGT8hqRSAo&quot;&gt;How to Train an AI Robotic Arm With No Code (Hugging Face LeRobot + SO-101)&lt;/a&gt; — Brogan M. Pratt. LeRobot và tay SO-101.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-việt-nam-chinbi-robot-ictu&quot;&gt;Video Việt Nam: Chinbi Robot (ICTU)&lt;/h2&gt;

&lt;p&gt;Hoàng Quân, giảng viên Cơ điện tử ICTU, đăng các Facebook Reel ngắn của dự án robot bốn chân Chinbi. Trang dự án ghi trong chú thích là &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;, đã mở được tháng 10 năm 2026. Cột ngày trong danh sách nguồn để trống, nên trang này không ghi ngày đăng. Mỗi dòng dưới đây là một reel đã đưa vào một bài. Câu xem-cái-gì nằm trong bài đó, và chỉ bám chú thích.&lt;/p&gt;

&lt;p&gt;Ba reel không đưa vào khóa: bài đăng lại về công ty robot nước ngoài, chuyến tham quan nhà máy ô tô, và phỏng vấn về startup làm mát trung tâm dữ liệu.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1746806056360278&quot;&gt;Cơ điện tử học làm robot&lt;/a&gt; — bài chào mừng, chương 00.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1381152347317660&quot;&gt;Sinh viên lập trình robot thông minh&lt;/a&gt;, &lt;a href=&quot;https://www.facebook.com/reel/2015462409114750&quot;&gt;Robocon Bắc Ninh 2026&lt;/a&gt;, &lt;a href=&quot;https://www.facebook.com/reel/4471403513091271&quot;&gt;Robocon Bắc Ninh tháng 8 năm 2026&lt;/a&gt; — máy trạng thái, chương 03.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1615249330240344&quot;&gt;Test thiết kế chân&lt;/a&gt;, &lt;a href=&quot;https://www.facebook.com/reel/2974137376266883&quot;&gt;một chân ba bậc tự do&lt;/a&gt; — cơ cấu thanh, chương 06.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/931720623225817&quot;&gt;Test pin drone, ESC cháy&lt;/a&gt; — pin và an toàn nguồn, chương 07.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1092867967065031&quot;&gt;Test hệ thống điều khiển&lt;/a&gt;, &lt;a href=&quot;https://www.facebook.com/reel/1057849570224248&quot;&gt;demo robot&lt;/a&gt; — firmware teleop, chương 07.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/976436878550867&quot;&gt;Flight controller, muốn bay ngay&lt;/a&gt; — PID, chương 08.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/2056881368258089&quot;&gt;Thả ra là bay&lt;/a&gt;, &lt;a href=&quot;https://www.facebook.com/reel/1038211995413233&quot;&gt;UAV vận tải&lt;/a&gt;, &lt;a href=&quot;https://www.facebook.com/reel/1004870148832135&quot;&gt;bước đầu drone vận tải&lt;/a&gt; — chu kỳ vòng điều khiển, chương 08.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/2078352426052273&quot;&gt;Dữ liệu 3D từ một vị trí trên trái đất&lt;/a&gt; — Gazebo Harmonic, chương 10.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1773957523904144&quot;&gt;MuJoCo recovery&lt;/a&gt;, &lt;a href=&quot;https://www.facebook.com/reel/2246938909200876&quot;&gt;training bậc thang rồi sim-to-real&lt;/a&gt; — checklist sim-to-real, chương 10.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1391050009535827&quot;&gt;Thuật toán về nhà&lt;/a&gt; — tour Nav2, chương 11.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/4089347134533915&quot;&gt;Đi bộ có tải, reinforcement learning&lt;/a&gt;, &lt;a href=&quot;https://www.facebook.com/reel/1624642749368249&quot;&gt;đứng hai chân&lt;/a&gt;, &lt;a href=&quot;https://www.facebook.com/reel/2292393271597807&quot;&gt;hai chân trước / handstand&lt;/a&gt;, &lt;a href=&quot;https://www.facebook.com/reel/2191042771623926&quot;&gt;trèo bậc / nhảy lên cầu thang&lt;/a&gt; — mở đầu robot learning, chương 12.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lịch 24 tuần</title>
   <link href="https://robotics-self-learning.pages.dev/home/study-schedule/"/>
   <updated>2021-01-21T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/home/study-schedule</id>
   <content type="html">&lt;h2 id=&quot;lịch-24-tuần&quot;&gt;Lịch 24 tuần&lt;/h2&gt;

&lt;p&gt;Mỗi tuần dành &lt;strong&gt;6–8 giờ&lt;/strong&gt;: giờ bài trong &lt;a href=&quot;/home/contents/&quot;&gt;mục lục&lt;/a&gt; cộng thời gian ghi sổ và sửa lỗi. 96 bài cộng lại khoảng 135 giờ bài; phần sổ kéo mỗi tuần vào khoảng trên. Làm tuần nào xong tuần đó. Bài Lab cần bàn hoặc máy; bài Concept có thể đọc trước rồi quay lại khi có linh kiện.&lt;/p&gt;

&lt;p&gt;Đường chính của ROS 2 là &lt;strong&gt;rclpy&lt;/strong&gt; và &lt;strong&gt;rclcpp&lt;/strong&gt;. &lt;a href=&quot;https://github.com/tiiuae/rclgo&quot;&gt;rclgo&lt;/a&gt; là đường phụ cho người đã quen Go: đọc API sau khi topic bằng Python hoặc C++ đã chạy, không thay bài trên site.&lt;/p&gt;

&lt;p&gt;Cột &lt;strong&gt;Mua&lt;/strong&gt; là tuần cần hàng về trước khi làm phần cứng của tuần đó. Phần mô phỏng không chờ lidar hay tay máy. Giá để trống trong BOM; điền sau khi mở trang shop.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Tuần&lt;/th&gt;
      &lt;th&gt;Trọng tâm&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ&lt;/th&gt;
      &lt;th&gt;Mua trước tuần này&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;Bàn lab và an toàn&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,5&lt;/td&gt;
      &lt;td&gt;Dụng cụ bàn nếu chưa có&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
      &lt;td&gt;Đo điện, linh kiện thụ động&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,0&lt;/td&gt;
      &lt;td&gt;Đợt 1: điện trở, LED, dây&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;3&lt;/td&gt;
      &lt;td&gt;Linh kiện chủ động và datasheet&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,5&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;4&lt;/td&gt;
      &lt;td&gt;Hàn, checklist điện, chọn MCU&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,0&lt;/td&gt;
      &lt;td&gt;ESP32 hoặc Pico&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;5&lt;/td&gt;
      &lt;td&gt;Blink, serial, vào C++ / MicroPython&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6&lt;/td&gt;
      &lt;td&gt;GPIO, PWM, ngắt, máy trạng thái&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7&lt;/td&gt;
      &lt;td&gt;Bus và encoder, kèm sổ I2C&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;8&lt;/td&gt;
      &lt;td&gt;ToF, camera, cầu H&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,0&lt;/td&gt;
      &lt;td&gt;Cảm biến, driver, motor đợt 2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;9&lt;/td&gt;
      &lt;td&gt;Servo, dòng, vòng kín nhỏ&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;10&lt;/td&gt;
      &lt;td&gt;Khung, bánh răng, bánh xe&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;11&lt;/td&gt;
      &lt;td&gt;Vật liệu và checklist cơ khí&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;12&lt;/td&gt;
      &lt;td&gt;Pin rồi mới lắp khung&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt;Pin, cầu chì, buck, BMS, khung&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;13&lt;/td&gt;
      &lt;td&gt;Driver và teleop&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;14&lt;/td&gt;
      &lt;td&gt;Nghiệm thu xe và toán cho TF&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15&lt;/td&gt;
      &lt;td&gt;Serial, MQTT, PID&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt;Encoder nếu motor TT chưa có&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;16&lt;/td&gt;
      &lt;td&gt;Odometry và checklist chương 08&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;17&lt;/td&gt;
      &lt;td&gt;Linux trên SBC và cài Jazzy&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt;Pi 5 hoặc Jetson, hoặc hoãn và dùng laptop&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;18&lt;/td&gt;
      &lt;td&gt;Topic, TF2, URDF&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;8,0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;19&lt;/td&gt;
      &lt;td&gt;Gazebo và micro-ROS&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;20&lt;/td&gt;
      &lt;td&gt;Nối xe thật và sim-to-real&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;21&lt;/td&gt;
      &lt;td&gt;Đọc SLAM, Nav2, BT, MoveIt&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;22&lt;/td&gt;
      &lt;td&gt;Capstone B: SLAM rồi Nav2&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt;Lidar trước nửa xe thật, không trước phần sim&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;23&lt;/td&gt;
      &lt;td&gt;LeRobot, ACT, Diffusion Policy, VLA&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7,0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;24&lt;/td&gt;
      &lt;td&gt;Lab SO-101 hoặc đường giấy&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;6,5&lt;/td&gt;
      &lt;td&gt;Tay SO-101 chỉ nếu làm đường tay thật&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;tuần-1--bàn-lab-75-giờ&quot;&gt;Tuần 1 — bàn lab (7,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter00/00_01_welcome_and_outcomes/&quot;&gt;00_01&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter00/00_02_learning_tracks_budget/&quot;&gt;00_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter00/00_03_safety_mindset_lab/&quot;&gt;00_03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter00/00_04_kit_catalog_tooling/&quot;&gt;00_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;00_05&lt;/a&gt;. Khoảng 7 giờ bài. Sổ 0,5 giờ: liệt kê dụng cụ đang có và món còn thiếu. &lt;strong&gt;Mua&lt;/strong&gt; kính, đồng hồ, mỏ hàn, breadboard nếu bàn trống. Chưa mua lidar, Pi hay Jetson.&lt;/p&gt;

&lt;h2 id=&quot;tuần-2--đo-và-nhận-linh-kiện-thụ-động-7-giờ&quot;&gt;Tuần 2 — đo và nhận linh kiện thụ động (7 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter00/00_06_workspace_toolchain_preview/&quot;&gt;00_06&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter00/00_07_ch00_lab_checklist/&quot;&gt;00_07&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter01/01_01_voltage_current_ohm/&quot;&gt;01_01&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter01/01_02_breadboard_meter/&quot;&gt;01_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter01/01_03_resistor_color_smd/&quot;&gt;01_03&lt;/a&gt;. Khoảng 6,5 giờ bài, sổ 0,5 giờ. &lt;strong&gt;Mua đợt 1&lt;/strong&gt; theo BOM: điện trở, LED, dây nhảy. Checklist chương 00 phải đạt trước khi cắm nguồn bàn.&lt;/p&gt;

&lt;h2 id=&quot;tuần-3--linh-kiện-chủ-động-75-giờ&quot;&gt;Tuần 3 — linh kiện chủ động (7,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter01/01_04_cap_diode_led_id/&quot;&gt;01_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter01/01_05_transistor_mosfet_ic_id/&quot;&gt;01_05&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter01/01_06_connectors_drivers_boards/&quot;&gt;01_06&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter01/01_07_datasheet_pinouts/&quot;&gt;01_07&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter01/01_08_soldering_basics/&quot;&gt;01_08&lt;/a&gt;. Khoảng 7,25 giờ bài, sổ 0,25 giờ: một chân datasheet bạn sẽ dùng ở chương 02.&lt;/p&gt;

&lt;h2 id=&quot;tuần-4--xong-điện-mở-mcu-7-giờ&quot;&gt;Tuần 4 — xong điện, mở MCU (7 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter01/01_09_esd_polarity_brownout/&quot;&gt;01_09&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter01/01_10_component_id_lab/&quot;&gt;01_10&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter01/01_11_ch01_lab_checklist/&quot;&gt;01_11&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter02/02_01_mcu_landscape/&quot;&gt;02_01&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter02/02_02_esp32_platformio/&quot;&gt;02_02&lt;/a&gt;. Khoảng 6,5 giờ bài, sổ 0,5 giờ. &lt;strong&gt;Mua&lt;/strong&gt; một ESP32 có USB-UART trên board, hoặc Pico nếu bạn chọn nhánh MicroPython. Đừng mua module SoC trần chưa có cổng nạp.&lt;/p&gt;

&lt;h2 id=&quot;tuần-5--chương-trình-đầu-tiên-7-giờ&quot;&gt;Tuần 5 — chương trình đầu tiên (7 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter02/02_03_pico_micropython/&quot;&gt;02_03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter02/02_04_blink_serial_hello/&quot;&gt;02_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter02/02_05_pinouts_power_rails/&quot;&gt;02_05&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter02/02_06_ch02_lab_checklist/&quot;&gt;02_06&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter03/03_01_cpp_vs_micropython/&quot;&gt;03_01&lt;/a&gt;. Khoảng 6,5 giờ bài, sổ 0,5 giờ: dán ảnh serial “hello” vào sổ. Pico là để biết MicroPython; ESP32 vẫn là máy của capstone nếu bạn chỉ giữ một board.&lt;/p&gt;

&lt;h2 id=&quot;tuần-6--mẫu-firmware-7-giờ&quot;&gt;Tuần 6 — mẫu firmware (7 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter03/03_02_gpio_patterns/&quot;&gt;03_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter03/03_03_pwm_timers/&quot;&gt;03_03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter03/03_04_interrupts_debounce/&quot;&gt;03_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter03/03_05_state_machines_fw/&quot;&gt;03_05&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter03/03_06_ch03_lab_checklist/&quot;&gt;03_06&lt;/a&gt;. Khoảng 6,75 giờ bài, sổ 0,25 giờ. PWM tuần này là tín hiệu, chưa nối motor.&lt;/p&gt;

&lt;h2 id=&quot;tuần-7--bus-và-đếm-xung-7-giờ&quot;&gt;Tuần 7 — bus và đếm xung (7 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter04/04_01_bus_interfaces/&quot;&gt;04_01&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter04/04_02_sensor_package_id/&quot;&gt;04_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter04/04_03_encoders_counts/&quot;&gt;04_03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter04/04_04_imu_basics/&quot;&gt;04_04&lt;/a&gt;. Khoảng 5,75 giờ bài. Sổ 1,25 giờ: quét địa chỉ I2C nếu đã có IMU, hoặc vẽ sơ đồ chân module bạn định mua tuần sau. Tuần này ngắn hơn các tuần lab vì chưa bắt buộc hàng cảm biến về.&lt;/p&gt;

&lt;h2 id=&quot;tuần-8--khoảng-cách-và-driver-7-giờ&quot;&gt;Tuần 8 — khoảng cách và driver (7 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter04/04_05_ultrasonic_tof/&quot;&gt;04_05&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter04/04_06_cameras_robots/&quot;&gt;04_06&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter04/04_07_ch04_lab_checklist/&quot;&gt;04_07&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter05/05_01_dc_hbridge/&quot;&gt;05_01&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter05/05_02_driver_board_id/&quot;&gt;05_02&lt;/a&gt;. Khoảng 6,5 giờ bài, sổ 0,5 giờ. &lt;strong&gt;Mua đợt 2:&lt;/strong&gt; HC-SR04 hoặc VL53L0X, mạch chuyển mức nếu echo 5 V, driver TB6612 hoặc DRV8833, hai motor TT. Camera để đọc; chưa cần mua.&lt;/p&gt;

&lt;h2 id=&quot;tuần-9--chấp-hành-và-nhiệt-7-giờ&quot;&gt;Tuần 9 — chấp hành và nhiệt (7 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter05/05_03_servos_pwm/&quot;&gt;05_03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter05/05_04_steppers_intro/&quot;&gt;05_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter05/05_05_current_thermal/&quot;&gt;05_05&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter05/05_06_closed_loop_intro/&quot;&gt;05_06&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter05/05_07_ch05_lab_checklist/&quot;&gt;05_07&lt;/a&gt;. Khoảng 6,5 giờ bài, sổ 0,5 giờ. Ghi dòng không tải và dòng khi giữ trục, vì tuần 12 dùng số đó để chọn cầu chì.&lt;/p&gt;

&lt;h2 id=&quot;tuần-10--cơ-cấu-7-giờ&quot;&gt;Tuần 10 — cơ cấu (7 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter06/06_01_chassis_anatomy/&quot;&gt;06_01&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter06/06_02_fasteners_bearings_shafts/&quot;&gt;06_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter06/06_03_gear_trains_ratios/&quot;&gt;06_03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter06/06_04_linkages_belts_cams/&quot;&gt;06_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter06/06_05_wheels_casters_diff/&quot;&gt;06_05&lt;/a&gt;. Khoảng 6,5 giờ bài, sổ 0,5 giờ: đo đường kính bánh bạn sẽ lắp, ghi mét trên một vòng.&lt;/p&gt;

&lt;h2 id=&quot;tuần-11--lắp-được-thì-mới-siết-65-giờ&quot;&gt;Tuần 11 — lắp được thì mới siết (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter06/06_06_materials_print_laser/&quot;&gt;06_06&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter06/06_07_mech_failure_modes/&quot;&gt;06_07&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter06/06_08_mechanisms_lab/&quot;&gt;06_08&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter06/06_09_ch06_lab_checklist/&quot;&gt;06_09&lt;/a&gt;. Khoảng 5 giờ bài. Sổ 1,5 giờ: checklist ốc, backlash, và một tờ nháp vị trí pin, driver, MCU. Chưa cắm pin.&lt;/p&gt;

&lt;h2 id=&quot;tuần-12--nguồn-trước-khung-sau-65-giờ&quot;&gt;Tuần 12 — nguồn trước, khung sau (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter07/07_00_batteries_power_safety/&quot;&gt;07_00&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter07/07_01_assemble_power/&quot;&gt;07_01&lt;/a&gt;. Khoảng 4 giờ bài. Sổ 2,5 giờ: điện áp từng cell, điện áp sau buck &lt;strong&gt;trước&lt;/strong&gt; khi nối ESP32, vị trí cầu chì trên cực dương. &lt;strong&gt;Mua&lt;/strong&gt; pin đúng hóa học (Li-ion / LiPo / LiFePO4), sạc đúng điện áp đầy, BMS nếu pack chưa có, buck, cầu chì 2 A và 3 A, khung nếu chưa có từ đợt motor. Không dùng sạc 4,20 V/cell cho LiFePO4. Không mắc TP4056 ngang pack 2S.&lt;/p&gt;

&lt;h2 id=&quot;tuần-13--xe-nghe-lệnh-serial-65-giờ&quot;&gt;Tuần 13 — xe nghe lệnh serial (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter07/07_02_driver_bringup/&quot;&gt;07_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter07/07_03_teleop_protocol/&quot;&gt;07_03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter07/07_04_teleop_firmware/&quot;&gt;07_04&lt;/a&gt;. Khoảng 5 giờ bài, sổ 1,5 giờ: log một phiên teleop, bánh kê lên trước khi cho xuống sàn.&lt;/p&gt;

&lt;h2 id=&quot;tuần-14--nghiệm-thu-và-toán-khung-65-giờ&quot;&gt;Tuần 14 — nghiệm thu và toán khung (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter07/07_05_field_acceptance/&quot;&gt;07_05&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter07/07_06_ch07_lab_checklist/&quot;&gt;07_06&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter08/08_00_math_for_tf/&quot;&gt;08_00&lt;/a&gt;. Khoảng 5 giờ bài. Sổ 1,5 giờ: chạy lại bài NumPy quay rồi tịnh tiến, ghi một quaternion yaw 90°. Bài toán này đứng trước TF2 ở chương 09.&lt;/p&gt;

&lt;h2 id=&quot;tuần-15--pid-65-giờ&quot;&gt;Tuần 15 — PID (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter08/08_01_serial_protocols/&quot;&gt;08_01&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter08/08_02_wifi_mqtt/&quot;&gt;08_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter08/08_03_pid_tuning/&quot;&gt;08_03&lt;/a&gt;. Khoảng 4,75 giờ bài. Sổ 1,75 giờ: chạy mô phỏng bậc hai, đổi Kp, lưu ảnh bước nhảy đối chiếu bài. &lt;strong&gt;Mua&lt;/strong&gt; đĩa và mạch encoder nếu motor TT chưa có xung; PID trên xe cần số đếm.&lt;/p&gt;

&lt;h2 id=&quot;tuần-16--từ-xung-tới-tư-thế-65-giờ&quot;&gt;Tuần 16 — từ xung tới tư thế (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter08/08_04_diff_kinematics/&quot;&gt;08_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter08/08_05_loop_timing/&quot;&gt;08_05&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter08/08_06_ch08_lab_checklist/&quot;&gt;08_06&lt;/a&gt;. Khoảng 4,25 giờ bài. Sổ 2 giờ: chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pose_from_counts&lt;/code&gt;, so quỹ đạo với hình trong bài, ghi số đếm cho 1 m thẳng và cho quay tại chỗ π/2.&lt;/p&gt;

&lt;h2 id=&quot;tuần-17--máy-trên-xe-và-jazzy-65-giờ&quot;&gt;Tuần 17 — máy trên xe và Jazzy (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter09/09_00_linux_on_sbc/&quot;&gt;09_00&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter09/09_01_jazzy_install_ws/&quot;&gt;09_01&lt;/a&gt;. Khoảng 4 giờ bài. Sổ 2,5 giờ: SSH không màn hình, luật udev ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/esp32&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; giống nhau trên laptop và SBC, một unit systemd cho micro-ROS agent. &lt;strong&gt;Mua&lt;/strong&gt; Raspberry Pi 5 và cài Ubuntu 24.04 (ảnh mới trên trang Ubuntu có thể là 26.04; đừng nhận mặc định đó), hoặc Jetson Orin Nano với JetPack 7 trên Ubuntu 24.04. JetPack 6 là Ubuntu 22.04: không &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt install ros-jazzy-*&lt;/code&gt;. Chưa có SBC thì làm tuần này trên laptop Ubuntu 24.04 và đánh dấu hoãn. Mac: USB serial đi qua máy ảo Ubuntu; Docker Desktop trên Mac không đưa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB&lt;/code&gt; vào container. Viết node bằng rclpy hoặc rclcpp. rclgo chỉ là đọc thêm.&lt;/p&gt;

&lt;h2 id=&quot;tuần-18--graph-ros-8-giờ&quot;&gt;Tuần 18 — graph ROS (8 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter09/09_02_nodes_topics/&quot;&gt;09_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter09/09_03_services_actions/&quot;&gt;09_03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter09/09_04_launch_params_qos/&quot;&gt;09_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter09/09_05_tf2_frames/&quot;&gt;09_05&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter09/09_06_urdf_basics/&quot;&gt;09_06&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter09/09_07_ch09_lab_checklist/&quot;&gt;09_07&lt;/a&gt;. Khoảng 7,75 giờ bài, sổ 0,25 giờ: một cây frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_laser&lt;/code&gt; với số đo thật, chưa lấy số ví dụ của hãng. Tuần dày nhất lịch; nếu trượt, đẩy checklist sang đầu tuần 19 chứ đừng bỏ TF2.&lt;/p&gt;

&lt;h2 id=&quot;tuần-19--mô-phỏng-65-giờ&quot;&gt;Tuần 19 — mô phỏng (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter10/10_01_gazebo_harmonic/&quot;&gt;10_01&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter10/10_02_urdf_sim_bringup/&quot;&gt;10_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter10/10_03_microros_mcu/&quot;&gt;10_03&lt;/a&gt;. Khoảng 4,5 giờ bài, sổ 2 giờ: tên topic sau khi spawn, và agent nói chuyện được với firmware. Gazebo Harmonic đi với Jazzy.&lt;/p&gt;

&lt;h2 id=&quot;tuần-20--cầu-sang-xe-thật-65-giờ&quot;&gt;Tuần 20 — cầu sang xe thật (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter10/10_04_bridge_capstone/&quot;&gt;10_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter10/10_05_sim_to_real/&quot;&gt;10_05&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter10/10_06_ch10_lab_checklist/&quot;&gt;10_06&lt;/a&gt;. Khoảng 4,5 giờ bài, sổ 2 giờ: bảng sim-to-real (khung, dấu encoder, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt;). Checklist chương 10 là cổng đạt, bài 10_05 vẫn là danh sách đối chiếu.&lt;/p&gt;

&lt;h2 id=&quot;tuần-21--đọc-tự-hành-7-giờ&quot;&gt;Tuần 21 — đọc tự hành (7 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter11/11_01_slam_overview/&quot;&gt;11_01&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter11/11_02_nav2_tour/&quot;&gt;11_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter11/11_03_vision_pipeline/&quot;&gt;11_03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter11/11_04_bt_peek/&quot;&gt;11_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter11/11_05_moveit_peek/&quot;&gt;11_05&lt;/a&gt;. Khoảng 6,25 giờ bài, sổ 0,75 giờ: một đoạn bạn sẽ cấu hình ở tuần 22 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;). Chưa cần lidar trên bàn.&lt;/p&gt;

&lt;h2 id=&quot;tuần-22--capstone-b-65-giờ&quot;&gt;Tuần 22 — Capstone B (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter11/11_06_capstone_b_slam/&quot;&gt;11_06&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter11/11_07_capstone_b_nav2/&quot;&gt;11_07&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter11/11_08_ch11_lab_checklist/&quot;&gt;11_08&lt;/a&gt;. Khoảng 5 giờ bài, sổ 1,5 giờ: ảnh map mô phỏng, rồi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo /cmd_vel&lt;/code&gt; trước khi bánh chạm sàn. Làm TurtleBot3 waffle trong sim trước. &lt;strong&gt;Mua&lt;/strong&gt; LD06 hoặc RPLIDAR A1 chỉ trước nửa xe thật. Không có lidar vẫn qua được phần sim và checklist nếu bạn ghi rõ phần thật còn thiếu.&lt;/p&gt;

&lt;h2 id=&quot;tuần-23--đọc-robot-learning-7-giờ&quot;&gt;Tuần 23 — đọc robot learning (7 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter12/12_01_lerobot_intro/&quot;&gt;12_01&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter12/12_02_imitation_act/&quot;&gt;12_02&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter12/12_03_diffusion_policy/&quot;&gt;12_03&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter12/12_04_vla_overview/&quot;&gt;12_04&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter12/12_05_next_paths_refs/&quot;&gt;12_05&lt;/a&gt;. Khoảng 6,25 giờ bài, sổ 0,75 giờ: một câu phân biệt ACT, diffusion policy và VLA. Chưa train.&lt;/p&gt;

&lt;h2 id=&quot;tuần-24--tay-hoặc-đường-giấy-65-giờ&quot;&gt;Tuần 24 — tay hoặc đường giấy (6,5 giờ)&lt;/h2&gt;

&lt;p&gt;Bài: &lt;a href=&quot;/contents/vi/chapter12/12_06_so101_arm_lab/&quot;&gt;12_06&lt;/a&gt;, &lt;a href=&quot;/contents/vi/chapter12/12_07_ch12_lab_checklist/&quot;&gt;12_07&lt;/a&gt;. Khoảng 3 giờ bài. Sổ 3,5 giờ. Đường giấy (không mua tay): đọc dataset card, ghi vì sao ví dụ train trên trang LeRobot là ACT, và chỉ gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--policy.type=diffusion&lt;/code&gt; khi chuỗi đó có trong gói bạn cài. Đường tay: &lt;strong&gt;mua&lt;/strong&gt; follower SO-101 và leader nếu bạn teleoperate; làm từng động cơ, kẹp trước. Không điền giá GPU thuê.&lt;/p&gt;

&lt;p&gt;Nếu một tuần trượt quá 8 giờ, dừng ở bài đang dở và ghi câu lệnh lỗi vào sổ. Tuần sau bắt đầu bằng câu đó, không bởi bài mới.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>introduction</title>
   <link href="https://robotics-self-learning.pages.dev/home/introduction/"/>
   <updated>2021-01-20T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/home/introduction</id>
   <content type="html">&lt;h1 id=&quot;welcome--build--program-robots&quot;&gt;Welcome — Build &amp;amp; Program Robots&lt;/h1&gt;

&lt;p&gt;Bilingual path from electronics literacy and robot assembly to ROS 2 Jazzy and modern robot-learning literacy.&lt;/p&gt;

&lt;h2 id=&quot;you-will-learn&quot;&gt;You will learn&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Component &amp;amp; tool identification on the bench&lt;/li&gt;
  &lt;li&gt;Embedded firmware on ESP32 / Pico&lt;/li&gt;
  &lt;li&gt;Diff-drive assembly and teleop firmware&lt;/li&gt;
  &lt;li&gt;ROS 2 Jazzy, TF/URDF, Gazebo Harmonic, micro-ROS&lt;/li&gt;
  &lt;li&gt;How to read LeRobot / ACT / Diffusion Policy / VLA materials&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Start at Chapter 00. Capstone A is Chapter 07.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>contents</title>
   <link href="https://robotics-self-learning.pages.dev/home/contents/"/>
   <updated>2021-01-20T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/home/contents</id>
   <content type="html">&lt;p&gt;Mục lục tiếng Việt của khóa. Mỗi dòng là một bài, gắn nhãn &lt;strong&gt;Lab&lt;/strong&gt; hoặc &lt;strong&gt;Concept&lt;/strong&gt;, kèm giờ ước lượng cho người học lần đầu. Giờ là kế hoạch đọc và làm, không phải đồng hồ bấm.&lt;/p&gt;

&lt;p&gt;Bản tiếng Anh của các bài gốc dùng cùng slug dưới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/contents/en/&lt;/code&gt;. Các bài mới (toán cho TF, pin và nguồn, Linux trên máy tính của robot, checklist chương 08–12, Capstone B, lab SO-101) có ở tiếng Việt. Xem &lt;a href=&quot;/home/study-schedule/&quot;&gt;lịch 24 tuần&lt;/a&gt; và &lt;a href=&quot;/home/resources/&quot;&gt;tài nguyên&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Tổng &lt;strong&gt;96 bài&lt;/strong&gt;, khoảng &lt;strong&gt;135 giờ&lt;/strong&gt; bài. &lt;a href=&quot;/home/study-schedule/&quot;&gt;Lịch 24 tuần&lt;/a&gt; thêm thời gian ghi sổ và sửa lỗi để mỗi tuần nằm trong khoảng 6–8 giờ.&lt;/p&gt;

&lt;h2 id=&quot;chương-00-giới-thiệu-lộ-trình-bộ-kit--an-toàn&quot;&gt;Chương 00. Giới thiệu, lộ trình, bộ kit &amp;amp; an toàn&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter00/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter00/00_01_welcome_and_outcomes/&quot;&gt;Chào mừng, kết quả học và cách học&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter00/00_02_learning_tracks_budget/&quot;&gt;Hai lộ trình: MCU tiết kiệm và ROS&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter00/00_03_safety_mindset_lab/&quot;&gt;Tư duy an toàn cho lab robot&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter00/00_04_kit_catalog_tooling/&quot;&gt;Danh mục dụng cụ bàn lab&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter00/00_06_workspace_toolchain_preview/&quot;&gt;Không gian làm việc và toolchain&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter00/00_07_ch00_lab_checklist/&quot;&gt;Checklist lab chương 00: bàn đã sẵn sàng&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-01-điện-tử-nhận-diện-linh-kiện--an-toàn-lab&quot;&gt;Chương 01. Điện tử, nhận diện linh kiện &amp;amp; an toàn lab&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter01/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_01_voltage_current_ohm/&quot;&gt;Điện áp, dòng điện, công suất và định luật Ohm&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_02_breadboard_meter/&quot;&gt;Breadboard, đồng hồ vạn năng và cách đo an toàn&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_03_resistor_color_smd/&quot;&gt;Nhận điện trở: vạch màu và mã SMD&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_04_cap_diode_led_id/&quot;&gt;Tụ điện, diode và LED&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_05_transistor_mosfet_ic_id/&quot;&gt;Transistor, MOSFET, IC ổn áp và dạng vỏ&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_06_connectors_drivers_boards/&quot;&gt;Giắc nối, driver động cơ và mạch vi điều khiển&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_07_datasheet_pinouts/&quot;&gt;Đọc datasheet và sơ đồ chân&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_08_soldering_basics/&quot;&gt;Hàn dây và header cho robot&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_09_esd_polarity_brownout/&quot;&gt;ESD, cực tính, cầu chì và sụt áp&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_10_component_id_lab/&quot;&gt;Lab — thông mạch, đo ohm và linh kiện dễ nhầm&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter01/01_11_ch01_lab_checklist/&quot;&gt;Checklist lab chương 01&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-02-vi-điều-khiển-arduino-esp32-pico&quot;&gt;Chương 02. Vi điều khiển: Arduino, ESP32, Pico&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter02/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter02/02_01_mcu_landscape/&quot;&gt;Chọn vi điều khiển cho robot nhỏ&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter02/02_02_esp32_platformio/&quot;&gt;Arduino IDE và PlatformIO với ESP32&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter02/02_03_pico_micropython/&quot;&gt;Raspberry Pi Pico và MicroPython&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter02/02_04_blink_serial_hello/&quot;&gt;Lab — nhấp LED, serial và nhịp tim robot&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter02/02_05_pinouts_power_rails/&quot;&gt;Sơ đồ chân, mức logic và đường nguồn&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter02/02_06_ch02_lab_checklist/&quot;&gt;Checklist lab chương 02&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-03-mẫu-lập-trình-nhúng-cho-robot&quot;&gt;Chương 03. Mẫu lập trình nhúng cho robot&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter03/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter03/03_01_cpp_vs_micropython/&quot;&gt;C++ và MicroPython trên robot&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter03/03_02_gpio_patterns/&quot;&gt;Lab — mẫu GPIO cho nút nhấn và LED&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter03/03_03_pwm_timers/&quot;&gt;PWM, timer và tín hiệu tốc độ động cơ&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter03/03_04_interrupts_debounce/&quot;&gt;Ngắt, chống dội tiếp điểm và ISR ngắn&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter03/03_05_state_machines_fw/&quot;&gt;Máy trạng thái trong firmware robot&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter03/03_06_ch03_lab_checklist/&quot;&gt;Checklist lab chương 03&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-04-cảm-biến--nhận-diện-module-cảm-biến&quot;&gt;Chương 04. Cảm biến &amp;amp; nhận diện module cảm biến&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter04/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter04/04_01_bus_interfaces/&quot;&gt;Giao diện cảm biến: I2C, SPI, UART&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter04/04_02_sensor_package_id/&quot;&gt;Nhận diện module: HC-SR04, ToF, IMU, IR, encoder, camera&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter04/04_03_encoders_counts/&quot;&gt;Encoder và đếm odometry thô&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter04/04_04_imu_basics/&quot;&gt;IMU: gia tốc, gyro, fusion&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter04/04_05_ultrasonic_tof/&quot;&gt;Lab: siêu âm và đo khoảng cách ToF&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter04/04_06_cameras_robots/&quot;&gt;Camera cho robot: USB, CSI, băng thông&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter04/04_07_ch04_lab_checklist/&quot;&gt;Checklist lab chương 04&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-05-cơ-cấu-chấp-hành-driver--nhận-diện&quot;&gt;Chương 05. Cơ cấu chấp hành, driver &amp;amp; nhận diện&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter05/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter05/05_01_dc_hbridge/&quot;&gt;Motor DC và cầu H&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter05/05_02_driver_board_id/&quot;&gt;Nhận diện driver: L298N, TB6612, DRV8833&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter05/05_03_servos_pwm/&quot;&gt;Servo hobby, PWM và mô-men&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter05/05_04_steppers_intro/&quot;&gt;Stepper và microstep&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter05/05_05_current_thermal/&quot;&gt;Giới hạn dòng, stall và nhiệt&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter05/05_06_closed_loop_intro/&quot;&gt;Lab: vòng kín cảm nhận → tính → tác động&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter05/05_07_ch05_lab_checklist/&quot;&gt;Checklist lab chương 05&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-06-cơ-cấu-cơ-khí-lắp-ráp--cad-nhẹ&quot;&gt;Chương 06. Cơ cấu cơ khí, lắp ráp &amp;amp; CAD nhẹ&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter06/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter06/06_01_chassis_anatomy/&quot;&gt;Giải phẫu khung robot vi sai&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter06/06_02_fasteners_bearings_shafts/&quot;&gt;Ốc vít, bạc đạn, trục và khớp nối&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter06/06_03_gear_trains_ratios/&quot;&gt;Bánh răng: trụ, worm, hành tinh, tỉ số, backlash&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter06/06_04_linkages_belts_cams/&quot;&gt;Cơ cấu thanh, cam, đai và bánh răng-thanh răng&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter06/06_05_wheels_casters_diff/&quot;&gt;Bánh xe, caster và sơ đồ cơ cấu vi sai&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter06/06_06_materials_print_laser/&quot;&gt;Vật liệu khung: in 3D vs cắt laser&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter06/06_07_mech_failure_modes/&quot;&gt;Hỏng hóc cơ khí và chống kéo dây&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter06/06_08_mechanisms_lab/&quot;&gt;Lab: đo tỉ số truyền và checklist lắp&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter06/06_09_ch06_lab_checklist/&quot;&gt;Checklist lab chương 06&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-07-capstone-a-robot-vi-sai--firmware-teleop&quot;&gt;Chương 07. Capstone A: Robot vi sai + firmware teleop&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter07/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter07/07_00_batteries_power_safety/&quot;&gt;Pin, nguồn và an toàn trước khi lắp Capstone&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter07/07_01_assemble_power/&quot;&gt;Lắp khung, motor và nguồn&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter07/07_02_driver_bringup/&quot;&gt;Lab: bring-up driver và test quay&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter07/07_03_teleop_protocol/&quot;&gt;Teleop serial: thiết kế giao thức&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter07/07_04_teleop_firmware/&quot;&gt;Vòng teleop firmware (chưa ROS)&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter07/07_05_field_acceptance/&quot;&gt;Test thực địa, log và nghiệm thu&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter07/07_06_ch07_lab_checklist/&quot;&gt;Checklist capstone chương 07&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-08-truyền-thông--vòng-điều-khiển&quot;&gt;Chương 08. Truyền thông &amp;amp; vòng điều khiển&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter08/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter08/08_00_math_for_tf/&quot;&gt;Vector, phép quay và quaternion trước TF2&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter08/08_01_serial_protocols/&quot;&gt;Giao thức serial ngoài println&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter08/08_02_wifi_mqtt/&quot;&gt;Lab: Wi-Fi và MQTT trên ESP32&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter08/08_03_pid_tuning/&quot;&gt;Trực giác PID và chỉnh tham số&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter08/08_04_diff_kinematics/&quot;&gt;Động học robot dẫn động vi sai&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter08/08_05_loop_timing/&quot;&gt;Timing vòng điều khiển và jitter&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter08/08_06_ch08_lab_checklist/&quot;&gt;Checklist lab chương 08&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-09-nền-tảng-ros-2-jazzy&quot;&gt;Chương 09. Nền tảng ROS 2 Jazzy&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter09/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter09/09_00_linux_on_sbc/&quot;&gt;Linux trên máy tính của robot&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter09/09_01_jazzy_install_ws/&quot;&gt;Cài ROS 2 Jazzy và workspace&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter09/09_02_nodes_topics/&quot;&gt;Node, topic và message&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter09/09_03_services_actions/&quot;&gt;Service và action&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter09/09_04_launch_params_qos/&quot;&gt;Launch, parameter và QoS&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter09/09_05_tf2_frames/&quot;&gt;TF2 và hệ tọa độ&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter09/09_06_urdf_basics/&quot;&gt;URDF / xacro cơ bản&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter09/09_07_ch09_lab_checklist/&quot;&gt;Checklist lab chương 09&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-10-mô-phỏng--micro-ros&quot;&gt;Chương 10. Mô phỏng &amp;amp; micro-ROS&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter10/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter10/10_01_gazebo_harmonic/&quot;&gt;Giới thiệu Gazebo Harmonic&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter10/10_02_urdf_sim_bringup/&quot;&gt;Bring-up URDF trong mô phỏng&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter10/10_03_microros_mcu/&quot;&gt;micro-ROS trên MCU&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter10/10_04_bridge_capstone/&quot;&gt;Nối Capstone A với ROS 2&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter10/10_05_sim_to_real/&quot;&gt;Checklist sim-to-real&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter10/10_06_ch10_lab_checklist/&quot;&gt;Checklist lab chương 10&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-11-tự-hành--nhận-thức-mở-đầu&quot;&gt;Chương 11. Tự hành &amp;amp; nhận thức mở đầu&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter11/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter11/11_01_slam_overview/&quot;&gt;Tổng quan SLAM cho người mới&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter11/11_02_nav2_tour/&quot;&gt;Tour stack Nav2&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter11/11_03_vision_pipeline/&quot;&gt;Pipeline thị giác cơ bản&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter11/11_04_bt_peek/&quot;&gt;Nhìn nhanh Behavior Tree&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter11/11_05_moveit_peek/&quot;&gt;Nhìn nhanh MoveIt 2&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter11/11_06_capstone_b_slam/&quot;&gt;Capstone B: lab SLAM, sim rồi lidar thật&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter11/11_07_capstone_b_nav2/&quot;&gt;Capstone B: lab Nav2, sim rồi xe thật&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter11/11_08_ch11_lab_checklist/&quot;&gt;Checklist lab chương 11&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;chương-12-robot-learning-hiện-đại--hướng-đi-tiếp&quot;&gt;Chương 12. Robot learning hiện đại &amp;amp; hướng đi tiếp&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/contents/vi/chapter12/&quot;&gt;Mở trang chương&lt;/a&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bài&lt;/th&gt;
      &lt;th&gt;Loại&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giờ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter12/12_01_lerobot_intro/&quot;&gt;Giới thiệu LeRobot / HF robotics&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter12/12_02_imitation_act/&quot;&gt;Học bắt chước và tổng quan ACT&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter12/12_03_diffusion_policy/&quot;&gt;Trực giác Diffusion Policy&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter12/12_04_vla_overview/&quot;&gt;Tổng quan Vision-Language-Action&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter12/12_05_next_paths_refs/&quot;&gt;Lộ trình tiếp theo và tài liệu&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Concept&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1,25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter12/12_06_so101_arm_lab/&quot;&gt;Lab tay LeRobot SO-101&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;/contents/vi/chapter12/12_07_ch12_lab_checklist/&quot;&gt;Checklist lab chương 12&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Lab&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

</content>
 </entry>
 
 <entry>
   <title>Phòng lab mô phỏng tại nhà</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_08_home_simulation_lab/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_08_home_simulation_lab</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;40–60 phút&lt;/strong&gt; để đọc và chọn một lối đi phù hợp với máy bạn đang có. Bài này không yêu cầu mua máy hay thuê GPU trong tuần học chính.&lt;/p&gt;

&lt;p&gt;Đây là hướng dẫn generic cho người học sau khi đã đi hết lộ trình khóa: khi nào cần Isaac Sim, khi nào chỉ cần Isaac Lab hoặc MuJoCo, và bước phần cứng đầu tiên nếu muốn làm robot hình người. Bài &lt;strong&gt;không&lt;/strong&gt; thay kit Capstone, không bắt buộc cho checklist chương 12, và không phải danh sách mua sắm phải theo từng món.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này bạn phân biệt được bốn lối đi (máy không có GPU NVIDIA / Mac, thuê GPU trên mây, tầng vào với card 16 GB, tầng đủ theo mức “Good” của Isaac Sim), nêu đúng yêu cầu chính thức của Isaac Sim, và viết được một thí nghiệm đầu tiên vừa tầm máy mình. Bạn cũng biết vì sao không nên bắt đầu bằng cả một robot hình người.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Đã đọc bài 12-05 và chọn hướng tháng tới. Nếu quan tâm sim-to-real, xem lại bài 10-05. Không cần đã cài Isaac Sim.&lt;/p&gt;

&lt;h2 id=&quot;bốn-lối-đi-ngắn-gọn&quot;&gt;Bốn lối đi, ngắn gọn&lt;/h2&gt;

&lt;p&gt;Isaac Sim và Isaac Lab &lt;strong&gt;không&lt;/strong&gt; cùng một sàn phần cứng.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Isaac Sim&lt;/strong&gt; (kèm dựng cảnh, render RTX, Asset Search): theo &lt;a href=&quot;https://docs.isaacsim.omniverse.nvidia.com/latest/installation/requirements.html&quot;&gt;yêu cầu chính thức&lt;/a&gt; (docs “latest” tại 30/11/2026), mức tối thiểu trên máy x86_64 là Ubuntu 22.04 hoặc 24.04, hoặc Windows 11; RAM 32 GB; SSD khoảng 50 GB; GPU GeForce &lt;strong&gt;RTX 4080&lt;/strong&gt;, &lt;strong&gt;16 GB&lt;/strong&gt; VRAM. Mức “Good” là RTX 5080 (vẫn 16 GB VRAM trên bảng, nhưng cùng hạng GPU được nêu). GPU &lt;strong&gt;không có RT Core&lt;/strong&gt; (A100, H100) &lt;strong&gt;không được hỗ trợ&lt;/strong&gt;. Ubuntu 22.04 đang bị đánh dấu sẽ bỏ trong một bản sau; Windows 10 không còn được hỗ trợ. Container Isaac Sim chỉ chạy trên Linux. Train Isaac Lab kèm Isaac Sim thường cần thêm RAM/VRAM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Isaac Lab&lt;/strong&gt; (mặc định Newton/Warp, có thể không cài Isaac Sim): theo &lt;a href=&quot;https://docs.nvidia.com/isaaclab/isaac-lab/installation&quot;&gt;hướng dẫn cài&lt;/a&gt;, môi trường train mặc định cần GPU NVIDIA &lt;strong&gt;Turing trở lên&lt;/strong&gt; (compute capability 7.5+). Mô phỏng Newton &lt;strong&gt;headless không đòi RTX&lt;/strong&gt;. Chỉ khi bạn cần render RTX hay workflow Isaac Sim thì mới áp dụng sàn RTX 4080 ở trên.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chọn &lt;strong&gt;một&lt;/strong&gt; lối, đừng dựng cả bốn cùng lúc.&lt;/p&gt;

&lt;h3 id=&quot;1-mac-hoặc-máy-không-có-gpu-nvidia&quot;&gt;1. Mac hoặc máy không có GPU NVIDIA&lt;/h3&gt;

&lt;p&gt;Bạn vẫn học được phần lớn ý tưởng mô phỏng.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Xem và kiểm tra file OpenUSD (khối lượng, khớp, collision) bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usdview&lt;/code&gt; hoặc thư viện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usd-core&lt;/code&gt;; xem mesh trong Blender. Đây là cách đọc asset, không thay Isaac Sim chạy vật lý đầy đủ — hãy coi là bước xem trước.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;strong&gt;sim-to-sim&lt;/strong&gt; trên MuJoCo, ví dụ kiểm tra chính sách của &lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt; theo tài liệu dự án (bài 10-05).&lt;/li&gt;
  &lt;li&gt;Đọc asset &lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt; (siêu dữ liệu, báo cáo profile), rồi ghi chú license từng gói trước khi tải.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Khi cần một phiên Isaac Sim hoặc train nặng, chuyển sang lối mây ở mục 2, theo giờ, rồi tắt máy khi xong.&lt;/p&gt;

&lt;h3 id=&quot;2-thuê-gpu-trên-mây&quot;&gt;2. Thuê GPU trên mây&lt;/h3&gt;

&lt;p&gt;Hợp khi bạn chỉ cần vài buổi Isaac Sim hoặc train Isaac Lab, chưa muốn mua máy.&lt;/p&gt;

&lt;p&gt;Một lựa chọn thực tế là card &lt;strong&gt;L40S&lt;/strong&gt; (48 GB, có RT Core, khác A100/H100). Giá công khai trên &lt;a href=&quot;https://www.runpod.io/pricing&quot;&gt;RunPod&lt;/a&gt; khoảng &lt;strong&gt;0,79 USD/giờ&lt;/strong&gt; (Community Cloud) và &lt;strong&gt;1,09 USD/giờ&lt;/strong&gt; (Secure Cloud), chốt ngày 30/11/2026; các nền tảng khác như Brev thường nằm quanh &lt;strong&gt;1,1–1,2 USD/giờ&lt;/strong&gt;. Giá thay đổi theo ngày và vùng: hãy đối chiếu trang giá ngay trước khi thuê, nhớ tắt máy khi xong, và kiểm tra điều khoản, khu vực dữ liệu cùng yêu cầu tài khoản.&lt;/p&gt;

&lt;p&gt;Đừng chọn A100/H100 cho Isaac Sim. Trên mây vẫn giữ thói quen của khóa: không để tiến trình train chạy vô thời hạn không người trông.&lt;/p&gt;

&lt;h3 id=&quot;3-tầng-vào--card-16-gb-tại-nhà&quot;&gt;3. Tầng vào — card 16 GB tại nhà&lt;/h3&gt;

&lt;p&gt;Sàn VRAM chính thức của Isaac Sim là 16 GB, nhưng GPU được nêu tên là &lt;strong&gt;RTX 4080&lt;/strong&gt;. Một card 16 GB rẻ hơn (ví dụ RTX 5060 Ti 16 GB) &lt;strong&gt;đạt số VRAM&lt;/strong&gt; nhưng &lt;strong&gt;không&lt;/strong&gt; nằm trong cột “Minimum” của NVIDIA; hãy coi đó là máy thử Isaac Lab headless và cảnh nhẹ, không phải lời hứa mọi hướng dẫn Isaac Sim đều chạy được. RTX 5070 12 GB &lt;strong&gt;không&lt;/strong&gt; đạt sàn 16 GB.&lt;/p&gt;

&lt;p&gt;Khung giá niêm yết tham khảo trên GearVN, &lt;strong&gt;khoảng, ngày 30/11/2026&lt;/strong&gt;, luôn phải xem lại trước khi mua:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Khung giá gần đúng (VND)&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;RTX 5060 Ti 16 GB&lt;/td&gt;
      &lt;td&gt;14–21 triệu&lt;/td&gt;
      &lt;td&gt;Đạt VRAM, dưới hạng GPU chính thức của Isaac Sim&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;RTX 4080 Super (nếu còn hàng)&lt;/td&gt;
      &lt;td&gt;32–35 triệu&lt;/td&gt;
      &lt;td&gt;Gần với GPU được nêu ở mức tối thiểu; hàng thay đổi rất nhanh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;RAM DDR5 32 GB&lt;/td&gt;
      &lt;td&gt;3–8 triệu&lt;/td&gt;
      &lt;td&gt;Sàn RAM của Isaac Sim là 32 GB, không phải 16 GB&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SSD 2 TB&lt;/td&gt;
      &lt;td&gt;3–10 triệu&lt;/td&gt;
      &lt;td&gt;Mức “Good” ghi 500 GB; 2 TB thoải hơn cho dataset&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nguồn 850 W&lt;/td&gt;
      &lt;td&gt;2,3–4 triệu&lt;/td&gt;
      &lt;td&gt;Chọn công suất theo card thực tế, không mua dư vô tội vạ&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Một máy bộ sẵn gắn RTX 5070 Ti từng được niêm yết khoảng 79 triệu, nhưng cấu hình kèm RAM 16 GB: muốn làm Isaac Sim thì vẫn phải nâng lên ít nhất 32 GB. Giá trên không gồm thuế, bảo hành khác nhau, hay máy cũ.&lt;/p&gt;

&lt;p&gt;Hệ điều hành: Ubuntu 24.04 là lựa chọn bền hơn (22.04 sắp bị bỏ); Windows 11 cũng nằm trong bảng hỗ trợ. Mac không chạy Isaac Sim.&lt;/p&gt;

&lt;h3 id=&quot;4-tầng-đủ--mức-good-rtx-5080&quot;&gt;4. Tầng đủ — mức “Good”: RTX 5080&lt;/h3&gt;

&lt;p&gt;Nếu bạn dựng cảnh Isaac Sim thường xuyên, render RTX, hoặc train kèm cảm biến, hãy nhắm mức &lt;strong&gt;Good&lt;/strong&gt;: RTX 5080, RAM 64 GB nếu có thể, SSD lớn. Card lẻ RTX 5080 trên GearVN khoảng &lt;strong&gt;39–46 triệu&lt;/strong&gt; (30/11/2026, giá niêm yết, chỉ là khung). Cộng CPU, main, RAM 32–64 GB, SSD và nguồn; một máy hoàn chỉnh sẽ cao hơn nhiều. RTX 5090 (khoảng 104–130 triệu cho card) là mức dư với phần lớn bài học, không phải điểm xuất phát.&lt;/p&gt;

&lt;h2 id=&quot;bước-phần-cứng-một-actuator-cycloid-chưa-phải-cả-robot-hình-người&quot;&gt;Bước phần cứng: một actuator cycloid, chưa phải cả robot hình người&lt;/h2&gt;

&lt;p&gt;Nếu hướng của bạn là robot hình người mã mở (ví dụ Berkeley Humanoid Lite ở bài 06-03 và 10-05), &lt;strong&gt;đừng&lt;/strong&gt; in và lắp cả máy 16 kg ngay. Bước đầu hợp lý là &lt;strong&gt;một&lt;/strong&gt; actuator hộp số cycloid in 3D (cỡ 6512) trên bàn thử: nguồn, driver, encoder, đo góc, mô-men và độ rơ, có công tắc ngắt nguồn trong tầm tay.&lt;/p&gt;

&lt;p&gt;BOM công khai của actuator 6512 (động cơ, driver, bạc, nhựa in) vào khoảng &lt;strong&gt;190 USD&lt;/strong&gt; theo giá Mỹ trên tài liệu dự án; tại Việt Nam phải tính thêm vận chuyển và thay thế linh kiện tương đương. Làm theo &lt;a href=&quot;https://berkeley-humanoid-lite.gitbook.io/docs&quot;&gt;tài liệu phần cứng&lt;/a&gt; và bài kiểm 06-03. Nhớ: hộp in 3D sẽ mòn; phát hành v1.1.0 đã nói hướng V2 xét actuator mua sẵn.&lt;/p&gt;

&lt;h2 id=&quot;thí-nghiệm-đầu-tiên-chọn-một&quot;&gt;Thí nghiệm đầu tiên (chọn một)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Không có GPU NVIDIA:&lt;/strong&gt; tải một gói SimReady, đọc license, mở USD bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usdview&lt;/code&gt; hoặc Blender, ghi lại profile (khớp, khối lượng, collision) có trong gói.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;MuJoCo / sim-to-sim:&lt;/strong&gt; chạy kiểm tra chính sách Berkeley Humanoid Lite trên máy hiện có, đối chiếu với bảng kiểm bài 10-05.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GPU Turing+ (kể cả thuê giờ):&lt;/strong&gt; cài Isaac Lab (Newton headless), mở một env nhỏ (ví dụ Cartpole) hoặc env đi bộ; mục tiêu là đọc observation/action, không bắt buộc train tới hội tụ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đủ sàn Isaac Sim:&lt;/strong&gt; tìm trên &lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt; một tay UR và gripper Robotiq, rồi làm theo bài &lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-assets-robot-pick-and-place&quot;&gt;pick-and-place&lt;/a&gt; (ví dụ UR17G + Robotiq 2F-140 + lon nước) trong Isaac Sim. Kiểm tra khớp, chuyển động nhỏ và tiếp xúc trước khi chạy cả chu trình gắp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bàn thử actuator:&lt;/strong&gt; in và lắp một hộp cycloid, quay có tải nhẹ, ghi độ rơ; chưa chuyển sang lắp chân hay thân.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Tạo props bằng NVIDIA AI Blueprint 3D Object Generation chỉ là việc phụ: file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.glb&lt;/code&gt; &lt;strong&gt;chưa&lt;/strong&gt; phải SimReady (xem trang tham khảo của khóa học).&lt;/p&gt;

&lt;h2 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h2&gt;

&lt;p&gt;Mạch động cơ và pin LiPo của actuator không phải đồ chơi. Giữ công tắc ngắt nguồn phần cứng trong tầm tay, đúng quy tắc chương 00 và 10. Máy in 3D cần thông thoáng. Trên mây, tắt máy ảo khi không dùng.&lt;/p&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Viết một câu: máy bạn đang có thuộc lối 1, 2, 3 hay 4, và thí nghiệm đầu tiên bạn sẽ làm là mục nào ở trên. Nếu chưa đủ phần cứng, câu trả lời hợp lệ là lối 1 hoặc 2.&lt;/li&gt;
  &lt;li&gt;Isaac Sim có chạy được trên A100 không? Vì sao? &lt;em&gt;Gợi ý:&lt;/em&gt; không được hỗ trợ vì không có RT Core.&lt;/li&gt;
  &lt;li&gt;Một máy có RTX 5060 Ti 16 GB và RAM 16 GB thiếu những gì so với bảng tối thiểu của Isaac Sim?&lt;/li&gt;
  &lt;li&gt;Kể một lý do để làm một actuator trên bàn thử trước khi đóng cả robot hình người.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Câu phải nêu đúng lối và đúng một thí nghiệm; không viết “làm tất cả”. 2. Không; A100/H100 không có RT Core, tài liệu Isaac Sim ghi rõ không hỗ trợ. 3. Thiếu hạng GPU RTX 4080 được nêu tên, và thiếu RAM 32 GB. 4. Kiểm tra hộp số, độ rơ và mạch trước khi nhân lên hàng chục khớp; hộp in 3D còn mòn và yếu hơn bản kim loại.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.isaacsim.omniverse.nvidia.com/latest/installation/requirements.html&quot;&gt;Yêu cầu Isaac Sim&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nvidia.com/isaaclab/isaac-lab/installation&quot;&gt;Cài Isaac Lab&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central trên Hugging Face&lt;/a&gt; và &lt;a href=&quot;https://docs.omniverse.nvidia.com/simready/latest/simready-central.html&quot;&gt;docs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-assets-robot-pick-and-place&quot;&gt;Pick-and-place với asset SimReady&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt; — tài liệu phần cứng và sim2sim&lt;/li&gt;
  &lt;li&gt;Trang tham khảo của khóa học (SimReady Central, blueprint tạo vật 3D)&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 12</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_07_ch12_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_07_ch12_lab_checklist</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~40 phút&lt;/strong&gt;. Bất kỳ tệp checkpoint nào tải về cũng không thay thế được phần phân tích bằng văn bản trong sổ thực hành.&lt;/p&gt;

&lt;p&gt;Danh sách nghiệm thu cuối cùng của khóa học tổng kết toàn bộ quy trình thực hành &lt;strong&gt;Robot Learning&lt;/strong&gt; ở Chương 12 — từ việc kiểm tra vòng teleoperation ổn định, xuất bản bộ dữ liệu chuẩn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LeRobotDataset&lt;/code&gt;, huấn luyện thành công chính sách bắt chước (ACT hoặc Diffusion Policy) cho tới đánh giá định lượng tỷ lệ thành công trên các lần thử nghiệm độc lập.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-tiêu-chuẩn-nghiệm-thu-robot-learning-chương-12&quot;&gt;Kiến thức cần nhớ: Ba tiêu chuẩn nghiệm thu Robot Learning (Chương 12)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Dữ liệu Chuẩn hóa &amp;amp; Tái lập được&lt;/strong&gt;:
Dataset ghi đủ số lượt trình diễn yêu cầu, tần số khung hình ổn định, không lệch đồng bộ giữa camera và góc khớp, và đọc lại sạch bằng trình xem của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LeRobot&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Huấn luyện Hội tụ &amp;amp; Theo dõi Loss Minh bạch&lt;/strong&gt;:
Lưu đầy đủ tệp cấu hình siêu tham số, đường cong hàm mất mát (loss curve) và checkpoint mô hình để bất kỳ ai cũng có thể đánh giá lại.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đánh giá Định lượng Công bằng (Multi-Trial Evaluation)&lt;/strong&gt;:
Không chỉ quay 1 video “chọn lọc lúc thành công”: phải chạy đủ số lượt kiểm thử liên tiếp (ví dụ 10 lượt ở các vị trí vật thể khác nhau), ghi rõ tỷ lệ thành công và phân tích nguyên nhân các lượt thất bại.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Bạn cần hoàn thành và xác nhận bốn tiêu chí đọc hiểu cùng một tiêu chí thực hành tay SO-101: dataset card, đoạn phân tích đa phương án (diffusion), bảng đối chiếu VLA, kế hoạch lộ trình tháng tới và log lab tay SO-101 (hoặc ghi chú hoãn có ngày). Việc chạy huấn luyện thành công trên GPU không bù đắp được cho một dataset card thiếu đơn vị.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành các bài từ 12-01 đến 12-06. Xe Capstone vẫn giữ cặp hành động $(v, \omega)$ trừ khi dataset card chỉ rõ đang mô tả cánh tay 6 khớp.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Đây là chốt kiểm tra cuối cùng của toàn khóa học, giúp bạn tránh nhầm lẫn khi ghép kênh đầu ra của OpenVLA sang PWM xe vi sai, đồng thời không hiểu nhầm lỗi thiếu bộ nhớ CUDA khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-train&lt;/code&gt; là trượt bài lab.&lt;/p&gt;

&lt;h2 id=&quot;tiêu-chí-đạt-và-không-đạt&quot;&gt;Tiêu chí đạt và không đạt&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Dataset card.&lt;/strong&gt; Ghi đầy đủ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps&lt;/code&gt;, tên khóa ảnh, tên khóa hành động và đơn vị. Card cho xe Capstone dùng m/s và rad/s; card cho SO-101 ghi đúng số trục thực tế (6 khớp trên follower) và chỉ ghi đơn vị nếu metadata dataset có khai báo (tuyệt đối không tự bịa đơn vị).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diffusion Policy.&lt;/strong&gt; Đoạn phân tích nêu rõ hai phiên mẫu đi hai phía, chứng minh lệnh trung bình đâm vào vật cản và có câu xác nhận không huấn luyện. Đoạn chỉ khen chung chung “xử lý đa mode tốt” là chưa đạt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bảng VLA.&lt;/strong&gt; Điền đủ hai dòng bảng so sánh, không có dòng nào trả lời “có” cho việc huấn luyện tại nhà, kèm một dòng đối chiếu SO-101 khẳng định vector 6 khớp không trùng với hành động của RT-1 hay OpenVLA.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lộ trình tiếp theo.&lt;/strong&gt; Chọn duy nhất một hướng cho tháng tới từ bài 12-05, ghi rõ một đường dẫn đầu tiên sẽ mở và nêu ít nhất một món sẽ không mua theo cảm tính.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thực hành tay SO-101.&lt;/strong&gt; Theo bài 12-06: có log cổng kết nối cùng một lần hiệu chỉnh/teleop, hoặc có ghi chú hoãn ghi rõ ngày (do chưa có phần cứng) đi kèm dataset card trên giấy. Việc mua tay máy không phải điều kiện bắt buộc để đạt.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cách-báo-cáo-kết-quả-đánh-giá-chính-sách-robot-learning-đúng-chuẩn-khoa-học&quot;&gt;Giải thích: Cách báo cáo kết quả đánh giá chính sách Robot Learning đúng chuẩn khoa học&lt;/h3&gt;

&lt;p&gt;Giả sử bạn đánh giá chính sách ACT vừa huấn luyện trên &lt;strong&gt;10 lượt thử liên tiếp&lt;/strong&gt; (đặt khối vật thể ở 5 vị trí trong tập huấn luyện và 5 vị trí dịch lệch mới):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Nhóm kiểm thử&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Số lượt thành công / tổng&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Tỷ lệ thành công&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Ghi chú phân tích lỗi&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trong vùng vị trí huấn luyện (In-Distribution)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4 / 5&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;80%&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1 lượt kẹp hơi lệch mép khi bóng đèn chiếu chói&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Vị trí mới dịch lệch 5 cm (Out-of-Distribution)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2 / 5&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;40%&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Cần bổ sung thêm 20 lượt trình diễn phủ rộng biên bàn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tổng hợp báo cáo&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6 / 10&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;60%&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Trung thực, có hướng cải thiện dữ liệu rõ ràng!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Thói quen đánh giá tách biệt In-Distribution và Out-of-Distribution này là tiêu chuẩn vàng trong mọi bài báo và sản phẩm Robot Learning hiện đại.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Sao chép mẫu sau vào sổ thực hành và điền đầy đủ kết quả:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Ky duyet chuong 12
Card (file):
Doan diffusion (dat/truot):
Bang VLA (2 dong, dat/truot):
Thang toi:
SO-101: log / giay hoan ngay
Train tai nha: khong
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Card ghi hành động là PWM nhưng chú thích lại ghi rad/s&lt;/td&gt;
      &lt;td&gt;Lệch đơn vị giữa hai hệ khác nhau; chọn thống nhất một hệ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dán lỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CUDA out of memory&lt;/code&gt; vào mục kết quả đạt&lt;/td&gt;
      &lt;td&gt;Bài lab không chấm GPU; hoàn thiện đúng phần phân tích văn bản&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điền “có” vào cột &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; trong bảng VLA&lt;/td&gt;
      &lt;td&gt;Xem lại khái niệm hình thái phần cứng (embodiment) và sửa lại thành “không”&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=7tGT8hqRSAo&quot;&gt;How to Train an AI Robotic Arm With No Code (Hugging Face LeRobot + SO-101)&lt;/a&gt; — Brogan M. Pratt (tiếng Anh). Quan sát trực quan cấu trúc tay SO-101 trước khi xác nhận lộ trình phần cứng hoặc lập dataset card.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nếu đoạn phân tích Diffusion Policy đạt chuẩn nhưng dataset card lại thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps&lt;/code&gt;, tiêu chí nào chưa đạt?&lt;/li&gt;
  &lt;li&gt;Một ghi chú hoãn thực hành tay SO-101 nhưng không ghi ngày cụ thể có được tính là đạt không?&lt;/li&gt;
  &lt;li&gt;Vì sao việc tải thành công một checkpoint OpenVLA vẫn không làm tiêu chí thực hành tay SO-101 đạt yêu cầu?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Tiêu chí Dataset card chưa đạt, vì đoạn phân tích lý thuyết không thay thế được thông số tần số lấy mẫu của dữ liệu.&lt;/li&gt;
  &lt;li&gt;Không — ghi chú hoãn bắt buộc có ngày cụ thể và lý do rõ ràng.&lt;/li&gt;
  &lt;li&gt;Checkpoint tải về không chứng minh bạn đã hiệu chỉnh servo STS3215 hay khớp đúng không gian hành động 6 chiều của SO-101.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;LeRobot&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/so101&quot;&gt;SO-101&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter12/12_06_so101_arm_lab/&quot;&gt;Lab tay&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab tay LeRobot SO-101</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_06_so101_arm_lab/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_06_so101_arm_lab</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~180 phút&lt;/strong&gt; nếu đã lắp sẵn cặp tay leader – follower cùng nguồn servo, hoặc &lt;strong&gt;~50 phút&lt;/strong&gt; theo lộ trình thực hành trên giấy nếu bạn chưa có phần cứng (huấn luyện xong policy không phải tiêu chí bắt buộc của bài).&lt;/p&gt;

&lt;p&gt;Mọi lý thuyết về ACT hay Diffusion Policy sẽ chỉ thực sự thấm vào tay nghề khi bạn tự lắp ráp, hiệu chuẩn và dạy kỹ năng cho một cánh tay robot vật lý ngoài đời thực. &lt;strong&gt;SO-101&lt;/strong&gt; — cánh tay robot in 3D mã nguồn mở 6 bậc tự do với chi phí chỉ vài trăm USD — đã dân chủ hóa phòng thí nghiệm Robot Learning: bất kỳ nhóm sinh viên nào cũng có thể tự xây dựng hệ thống teleoperation chủ–tớ (leader–follower) ngay trên bàn học.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-thực-hành-cánh-tay-robot-so-101&quot;&gt;Kiến thức cần nhớ: Ba trụ cột Thực hành Cánh tay Robot SO-101&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Kiến trúc Phần cứng Bus Servo Nối tiếp (Feetech STS3215)&lt;/strong&gt;:
Khác với servo RC đời cũ chỉ nhận xung PWM một chiều, bus servo nối tiếp truyền nhận hai chiều qua UART bán song công (half-duplex), cho phép đọc lại chính xác vị trí góc hiện tại (encoder từ 12-bit = 4096 bước/vòng), vận tốc, dòng điện và nhiệt độ của từng khớp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hiệu chuẩn Khớp &amp;amp; Ánh xạ Chủ–Tớ (Leader–Follower Calibration)&lt;/strong&gt;:
Gán đúng ID từng động cơ (1 đến 6), đặt vị trí chuẩn 0 đồng bộ giữa tay chủ (leader, tháo bánh răng/tắt mô-men để người cầm lái nhẹ tay) và tay tớ (follower) để tránh hiện tượng giật khớp nguy hiểm khi bật nguồn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kỷ luật Thu thập Dữ liệu Sạch (Data Hygiene)&lt;/strong&gt;:
Chất lượng chính sách ACT phụ thuộc tuyệt đối vào tính nhất quán của dữ liệu trình diễn: góc camera cố định, ánh sáng ổn định, phân bố vị trí vật thể đa dạng và loại bỏ ngay các lượt thu bị lỗi.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ phân biệt đúng vai trò giữa tay điều khiển (&lt;strong&gt;leader&lt;/strong&gt;) và tay chấp hành (&lt;strong&gt;follower&lt;/strong&gt;) của cánh tay SO-101, xác định cổng USB bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-find-port&lt;/code&gt; và hiệu chỉnh (calibrate) tay follower (hoặc lập ghi chú hoãn có ngày nếu chưa có phần cứng). Bạn sẽ viết một dataset card cho không gian hành động 6 khớp, đối chiếu trực tiếp với vector 2 chiều của xe Capstone và không gian hành động của các mô hình VLA, đồng thời đọc hiểu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-train&lt;/code&gt; trên trang tài liệu hiện hành (đang dùng ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--policy.type=act&lt;/code&gt;) và biết vị trí đăng ký tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diffusion&lt;/code&gt; trong mã nguồn mà không cần chạy huấn luyện.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành các bài LeRobot (12-01), ACT (12-02), Diffusion Policy (12-03) và VLA (12-04). Bạn đã hiểu vì sao trung bình hai quỹ đạo có thể gây va chạm và nắm rõ rằng cánh tay không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;. Lưu ý cấp nguồn servo bằng bộ nguồn riêng biệt, không đấu chung với khối pin động cơ xe trừ khi đã tính toán kỹ dòng tải và cầu chì phù hợp.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Các bài trước chủ yếu tiếp cận qua tài liệu nghiên cứu; bài này đi vào một phần cứng cụ thể mà hệ sinh thái LeRobot định vị là mẫu cánh tay chi phí thấp chủ lực: &lt;strong&gt;SO-101&lt;/strong&gt;. Sáu động cơ servo bus STS3215 hoàn toàn khác với hai bánh dẫn động, và một checkpoint OpenVLA không tự động xuất ra đúng 6 giá trị khớp đó. Thực hành này giúp bạn kiểm soát chặt chẽ cấu trúc hành động trước khi xảy ra nhầm lẫn khi ghép nối.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_so101_action.png&quot; alt=&quot;Sáu số khớp một bên, action VLA một bên&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Theo tài liệu &lt;a href=&quot;https://huggingface.co/docs/lerobot/so101&quot;&gt;SO-101&lt;/a&gt;, tay &lt;strong&gt;follower&lt;/strong&gt; dùng 6 động cơ STS3215 cùng tỉ số truyền &lt;strong&gt;1/345&lt;/strong&gt;, trong khi tay &lt;strong&gt;leader&lt;/strong&gt; dùng ba tỉ số truyền khác nhau để người cầm kéo nhẹ tay hơn: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pan&lt;/code&gt; 1/191, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lift&lt;/code&gt; 1/345, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elbow&lt;/code&gt; 1/191 và ba khớp cuối 1/147. Bảng thông số này phục vụ trực tiếp cho khâu lắp ráp cơ khí — lắp nhầm động cơ giữa leader và follower sẽ gây lỗi cơ khí ngay lập tức.&lt;/p&gt;

&lt;p&gt;Cài đặt LeRobot theo hướng dẫn &lt;a href=&quot;https://huggingface.co/docs/lerobot/installation&quot;&gt;installation&lt;/a&gt;, kèm SDK điều khiển servo Feetech theo đúng lệnh trên trang SO-101:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;.[feetech]&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trình tự thiết lập phần mềm sau khi đã lắp hoàn chỉnh phần cơ khí:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-find-port&lt;/code&gt; (rút cáp theo hướng dẫn trên màn hình) để xác định chính xác cổng USB của từng bus.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-setup-motors --robot.type=so101_follower --robot.port=...&lt;/code&gt; — bắt buộc cắm &lt;strong&gt;từng động cơ một&lt;/strong&gt; bắt đầu từ tay kẹp (gripper) vì các động cơ mới xuất xưởng đều mang mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id = 1&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Lặp lại bước gán ID cho tay leader với tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--teleop.type=so101_leader&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-calibrate&lt;/code&gt; cho cả follower (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--robot.type=so101_follower --robot.id=...&lt;/code&gt;) và leader (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--teleop.type=so101_leader&lt;/code&gt;) quanh vị trí giữa dải hành trình của từng khớp — bước hiệu chỉnh này giúp tập dữ liệu thu trên một cánh tay có thể đồng bộ và tái sử dụng ổn định.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lệnh điều khiển từ xa (teleop) rút gọn theo trang &lt;a href=&quot;https://huggingface.co/docs/lerobot/il_robots&quot;&gt;imitation trên robot thật&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;lerobot-teleoperate &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--robot&lt;/span&gt;.type&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;so101_follower &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--robot&lt;/span&gt;.port&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/dev/ttyACM0 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--robot&lt;/span&gt;.id&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;my_awesome_follower_arm &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--teleop&lt;/span&gt;.type&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;so101_leader &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--teleop&lt;/span&gt;.port&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/dev/ttyACM1 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--teleop&lt;/span&gt;.id&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;my_awesome_leader_arm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Thay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyACM0&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyACM1&lt;/code&gt; bằng đúng tên cổng vừa tìm được trên Linux (không giữ nguyên tên cổng kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tty.usbmodem...&lt;/code&gt; của macOS).&lt;/p&gt;

&lt;p&gt;Để ghi dữ liệu, trang tài liệu dùng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-record&lt;/code&gt; với các tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--dataset.repo_id&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--dataset.num_episodes&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--dataset.single_task&lt;/code&gt; và tùy chọn camera &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--robot.cameras&lt;/code&gt;. Nếu chưa gắn camera, không khai báo chỉ số camera giả — bạn chỉ ghi trạng thái khớp và ghi chú rõ trên dataset card là không có hình ảnh. Chỉ cần một episode ngắn chạy êm (không kẹt bánh răng) là đủ đạt yêu cầu lab, không cần ghi đủ 5 episode như ví dụ trên trang.&lt;/p&gt;

&lt;p&gt;Về phần huấn luyện, ví dụ mẫu trên trang imitation (tại thời điểm đối chiếu tháng 10/2026) là:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;lerobot-train &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--dataset&lt;/span&gt;.repo_id&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HF_USER&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/so101_test &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--policy&lt;/span&gt;.type&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;act &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--output_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;outputs/train/act_so101_test &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--job_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;act_so101_test &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--policy&lt;/span&gt;.device&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;cuda &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--wandb&lt;/span&gt;.enable&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--policy&lt;/span&gt;.repo_id&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HF_USER&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/my_policy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ví dụ này sử dụng &lt;strong&gt;ACT&lt;/strong&gt; (bài 12-02). Trong mã nguồn LeRobot (nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt;, tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configuration_diffusion.py&lt;/code&gt;), Diffusion Policy đăng ký dưới tên subclass &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;diffusion&quot;&lt;/code&gt;; bạn chỉ nên đổi sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--policy.type=diffusion&lt;/code&gt; sau khi tự kiểm tra tệp đó trong bản cài đặt hiện tại (nếu bản cài từ chối, ghi nhận thông báo lỗi thay vì tự đoán cờ). Tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--policy.device=cuda&lt;/code&gt; đòi hỏi GPU nên laptop thường sẽ dừng ở dataset card và một episode mẫu (dịch vụ chạy job GPU trên Hugging Face qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--job.target&lt;/code&gt; là dịch vụ trả phí, nằm ngoài yêu cầu khóa học).&lt;/p&gt;

&lt;p&gt;Với lệnh chạy thử nghiệm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-rollout&lt;/code&gt;, lưu ý một số đoạn ví dụ trên trang tài liệu vẫn ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;so100_follower&lt;/code&gt; — bạn cần sửa thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;so101_follower&lt;/code&gt; cho đúng phần cứng và chỉ chạy rollout khi đã có checkpoint huấn luyện từ chính tập dữ liệu của mình.&lt;/p&gt;

&lt;p&gt;Không gian hành động của tay follower gồm 6 giá trị ứng với 6 khớp: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shoulder pan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shoulder lift&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elbow flex&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wrist flex&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wrist roll&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gripper&lt;/code&gt;. Tên trường và đơn vị phải trích trực tiếp từ metadata tệp dataset (hoặc ghi rõ chưa có dữ liệu đo nếu làm trên giấy), không suy đoán theo trí nhớ. Xe Capstone vẫn giữ hành động 2 chiều $(v, \omega)$, còn OpenVLA hay RT-1 không xuất ra 6 giá trị servo Feetech này.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An toàn khi thao tác cánh tay:&lt;/strong&gt; nguồn servo phải có công tắc ngắt nhanh; tuyệt đối không đặt ngón tay giữa hai càng kẹp trong lúc hiệu chỉnh vì lực kẹp đủ gây chấn thương; khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup-motors&lt;/code&gt; chỉ cắm duy nhất một động cơ để tránh ghi đè ID hàng loạt lên các động cơ khác.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-độ-phân-giải-góc-và-mô-men-nâng-tải-của-khớp-vai-so-101&quot;&gt;Giải thích: Tính tay độ phân giải góc và mô-men nâng tải của khớp vai SO-101&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Độ phân giải góc của encoder 12-bit trên servo STS3215:&lt;/strong&gt;
\(\Delta \theta = \frac{360^\circ}{2^{12}} = \frac{360^\circ}{4096} \approx \mathbf{0{,}088^\circ/\text{bước}}.\)
Với chiều dài tay máy vươn hết cỡ \(L = 0{,}35\text{ m}\), độ chính xác bước nhỏ nhất ở đầu kẹp đạt \(0{,}35 \times \left(0{,}088 \times \frac{\pi}{180}\right) \approx \mathbf{0{,}54\text{ mm}}\) — đủ tinh tế cho các bài toán gắp lắp ráp nhẹ!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kiểm tra giới hạn tải nâng an toàn:&lt;/strong&gt; Giả sử servo vai có mô-men giữ cực đại \(\tau_{\max} = 30\text{ kg}\cdot\text{cm} \approx 2{,}94\text{ N}\cdot\text{m}\). Để giữ hệ số an toàn \(3\times\) (tránh quá nhiệt động cơ khi giữ lâu), mô-men làm việc liên tục nên giữ dưới \(\tau_{\text{an toàn}} \approx 1{,}0\text{ N}\cdot\text{m}\). Ở tầm vươn ngang \(L = 0{,}35\text{ m}\), trọng lượng vật gắp (đã trừ trọng lượng bản thân cánh tay) nên giữ ở mức nhẹ khoảng &lt;strong&gt;100–200 gram&lt;/strong&gt;!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Luôn tắt nguồn servo trước khi tháo lắp dây 3 chân; không đưa cánh tay lên ngang tầm mắt; cố định tay leader tránh rơi vỡ khi đang gán ID; tách biệt hoàn toàn khỏi xe Capstone để tránh nhầm lẫn cổng hoặc topic.&lt;/p&gt;

&lt;h3 id=&quot;lộ-trình-có-phần-cứng-so-101&quot;&gt;Lộ trình có phần cứng SO-101&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Đọc kỹ danh mục linh kiện trên trang SO-101 và ghi nhận bạn tự lắp từ bộ kit nào (hoặc ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tay đã lắp sẵn&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-find-port&lt;/code&gt; và dán tên cổng của follower cùng leader vào sổ.&lt;/li&gt;
  &lt;li&gt;Nếu động cơ vẫn mang ID mặc định, chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-setup-motors&lt;/code&gt; lần lượt từng chiếc theo đúng thứ tự (nếu mua tay đã gán ID sẵn theo tài liệu người bán thì ghi chú lại, không cần ghi đè EEPROM).&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-calibrate&lt;/code&gt; cho tay follower và chụp ảnh tư thế giữa dải hành trình.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-teleoperate&lt;/code&gt; trong khoảng 10 giây, sau đó chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-record&lt;/code&gt; ghi 1 episode với mô tả tác vụ một câu (nếu chưa có camera thì ghi chú rõ chỉ thực hiện teleop).&lt;/li&gt;
  &lt;li&gt;Mở metadata của dataset (hoặc lập card trên giấy) và chép chính xác kích thước &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt; cùng tên từng phần tử.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;lộ-trình-chưa-có-phần-cứng-thực-hành-trên-giấy&quot;&gt;Lộ trình chưa có phần cứng (thực hành trên giấy)&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Lập dataset card liệt kê đủ 6 tên khớp nêu trên cùng giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps&lt;/code&gt; dự kiến (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10&lt;/code&gt;, cam kết giữ đồng nhất lúc ghi và lúc phát).&lt;/li&gt;
  &lt;li&gt;Viết một đoạn phân tích tình huống gắp một khối vật thể từ hai phía trái và phải: chỉ ra giá trị trung bình của các khớp có thể làm tay đâm xuyên qua vật thể (tương tự ví dụ tránh ghế ở bài 12-03 nhưng trên hình thái 6 khớp).&lt;/li&gt;
  &lt;li&gt;Chép nguyên văn lệnh ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-train&lt;/code&gt; (dùng ACT) từ trang imitation kèm ngày mở trang và câu xác nhận chưa chạy do chưa có tay máy hoặc GPU.&lt;/li&gt;
  &lt;li&gt;Viết một dòng đối chiếu VLA xác nhận vector kích thước 6 này không trùng khớp với đầu ra của OpenVLA, kèm đường dẫn tài liệu đã đọc ở bài 12-04.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Sổ thực hành có thông tin cổng kết nối (hoặc ghi chú hoãn có ngày), dataset card 6 khớp, lệnh mẫu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-train&lt;/code&gt; (ACT) kèm ngày truy cập, và xác nhận tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diffusion&lt;/code&gt; trong mã nguồn (hoặc ghi rõ “chưa cài nên chưa kiểm tra”).&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Không nhận diện được cổng USB&lt;/td&gt;
      &lt;td&gt;Dùng nhầm cáp chỉ sạc, hoặc jumper trên mạch Waveshare chưa đặt đúng chế độ USB theo trang SO-101&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hai động cơ bị trùng ID&lt;/td&gt;
      &lt;td&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup-motors&lt;/code&gt; khi đang nối nối tiếp cả cánh tay; phải tháo ra cắm từng động cơ một&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tay follower giật mạnh hết hành trình khi hiệu chỉnh&lt;/td&gt;
      &lt;td&gt;Sai tư thế giữa dải lúc calibrate, hoặc lắp nhầm động cơ giữa leader và follower (khác tỉ số truyền)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Báo lỗi khi truyền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;policy.type=diffusion&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Phiên bản cài đặt không đăng ký tên subclass này; ghi nhận thông báo lỗi và bám theo tài liệu đọc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Gán một khớp cánh tay sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Sai hình thái phần cứng trên card; xóa ngay ánh xạ này&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=7tGT8hqRSAo&quot;&gt;How to Train an AI Robotic Arm With No Code (Hugging Face LeRobot + SO-101)&lt;/a&gt; — Brogan M. Pratt (tiếng Anh). Minh họa lắp ráp và chạy SO-101 với LeRobot (lưu ý tiêu đề ghi “no-code”, nhưng bài lab của khóa vẫn dùng trực tiếp các lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-*&lt;/code&gt; trong terminal).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ft73x0LfGpM&quot;&gt;ALOHA and ACT: LeRobot Research Presentation #1 by Alexander Soare&lt;/a&gt; — Hugging Face (tiếng Anh). Trình bày về ACT và ALOHA, giải thích cơ sở đằng sau tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--policy.type=act&lt;/code&gt; trong lệnh ví dụ.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Theo bảng cấu hình trên trang SO-101, khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Shoulder Lift&lt;/code&gt; của tay leader và tay follower lần lượt có tỉ số truyền là bao nhiêu?&lt;/li&gt;
  &lt;li&gt;Vì sao lệnh ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-train&lt;/code&gt; trên trang imitation không đủ cơ sở để khẳng định Diffusion Policy là mặc định của LeRobot?&lt;/li&gt;
  &lt;li&gt;Nếu tập dữ liệu ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt; có độ dài 6 nhưng một policy lại xuất ra vector độ dài 7, bạn có triển khai lên tay SO-101 không?&lt;/li&gt;
  &lt;li&gt;Giá trị trung bình từng khớp giữa “gắp từ bên trái” và “gắp từ bên phải” có tạo thành một quỹ đạo gắp an toàn không?&lt;/li&gt;
  &lt;li&gt;Thư viện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclgo&lt;/code&gt; có vai trò gì đối với bus servo STS3215 trong bài lab này?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Ở cả tay leader (hàng Shoulder Lift, động cơ số 2) lẫn tay follower (toàn bộ 6 động cơ), tỉ số truyền của khớp nâng vai đều là 1/345 (tuy nhiên vẫn tuyệt đối không lấy tỉ số của leader suy cho các khớp khác của follower chỉ vì trùng tên).&lt;/li&gt;
  &lt;li&gt;Vì ví dụ mẫu đang dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--policy.type=act&lt;/code&gt;; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diffusion&lt;/code&gt; là một lớp con riêng cần kiểm tra trực tiếp trong phiên bản bạn cài.&lt;/li&gt;
  &lt;li&gt;Không — kích thước vector hành động gắn chặt với cấu trúc cơ khí của robot.&lt;/li&gt;
  &lt;li&gt;Không — giá trị trung bình có thể đưa cánh tay đi xuyên qua vật thể, cùng bản chất với bài toán đa phương án ở bài 12-03.&lt;/li&gt;
  &lt;li&gt;Không có vai trò gì — bài này dùng LeRobot kết hợp SDK Feetech trên Python, không liên quan đến Go hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclgo&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;thực-hành-thêm-mô-phỏng-không-thay-lab-này&quot;&gt;Thực hành thêm (mô phỏng, không thay lab này)&lt;/h2&gt;

&lt;p&gt;Lab SO-101 là tay in 3D trên bàn thật. Nếu sau này bạn muốn đối chiếu với một workcell công nghiệp trong Isaac Sim, thư viện &lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt; có tay UR/Franka và gripper Robotiq đã gắn vật lý; bài &lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-assets-robot-pick-and-place&quot;&gt;pick-and-place&lt;/a&gt; hướng dẫn lắp UR17G + Robotiq 2F-140. Sàn máy và các lối đi không có GPU nằm ở &lt;a href=&quot;/contents/vi/chapter12/12_08_home_simulation_lab/&quot;&gt;phòng lab mô phỏng tại nhà&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/so101&quot;&gt;SO-101&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/il_robots&quot;&gt;Imitation learning trên robot thật&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/installation&quot;&gt;Cài LeRobot&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/huggingface/lerobot&quot;&gt;Kho mã LeRobot&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://diffusion-policy.cs.columbia.edu/&quot;&gt;Diffusion Policy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lộ trình tiếp theo và tài liệu</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_05_next_paths_refs/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_05_next_paths_refs</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~60 phút&lt;/strong&gt; (10 phút xem sơ đồ định hướng, 20 phút chọn một hướng duy nhất và 30 phút lập kế hoạch thực hành một tháng mà chưa cần mua sắm thêm).&lt;/p&gt;

&lt;p&gt;Hoàn thành hành trình 12 chương từ mạch điện cầu H, PID, ROS 2 Jazzy, Gazebo, SLAM/Nav2 đến LeRobot và VLA, bạn đã sở hữu tổng quan hiếm có: hiểu robot từ dòng điện chạy qua cuộn dây động cơ cho tới mô hình AI nền tảng. Bài học này đóng vai trò la bàn chỉ hướng cho chặng đường tiếp theo — dù bạn muốn đi sâu vào kỹ thuật điều khiển, SLAM thị giác, thao tác tay máy hay nghiên cứu Robot Learning.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-nhánh-chuyên-sâu-sau-khóa-học-robotics&quot;&gt;Kiến thức cần nhớ: Bốn nhánh Chuyên sâu sau Khóa học Robotics&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Kỹ thuật Phần mềm Robot &amp;amp; Hệ thống Tự hành (ROS 2 / Nav2 / C++ Real-Time)&lt;/strong&gt;:
Đi sâu vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2_control&lt;/code&gt;, hệ điều hành thời gian thực (PREEMPT_RT), tối ưu hóa hiệu năng C++ và triển khai đội xe (fleet management).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nhận thức &amp;amp; SLAM 3D (Visual-Inertial Odometry, LiDAR-Inertial SLAM, 3D Gaussian Splatting)&lt;/strong&gt;:
Vượt ra khỏi mặt phẳng 2D sang bản đồ 3D dày đặc, tối ưu hóa đồ thị phi tuyến (GTSAM / Ceres) và kết hợp cảm biến đa thể thức.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Điều khiển Nâng cao &amp;amp; Tối ưu Quỹ đạo (LQR, MPC, Whole-Body Control)&lt;/strong&gt;:
Dùng mô hình động lực học đầy đủ (phương trình Euler–Lagrange) và tối ưu hóa lồi thời gian thực cho robot bốn chân (quadruped) và robot hình người (humanoid).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Robot Learning &amp;amp; Embodied AI (Sim-to-Real RL, Isaac Lab, VLA)&lt;/strong&gt;:
Huấn luyện hàng nghìn robot song song trên GPU bằng Học tăng cường (PPO trong Isaac Lab/MuJoCo) và mở rộng chính sách đa nhiệm VLA.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ chọn &lt;strong&gt;duy nhất một&lt;/strong&gt; hướng phát triển cho tháng tới thay vì dàn trải cả ba: &lt;strong&gt;Hướng A&lt;/strong&gt; phát triển tiếp khung xe hiện có (Nav2, lidar khi sẵn sàng, tham khảo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linorobot2&lt;/code&gt; nhưng vẫn giữ nguyên cầu nối động cơ của mình); &lt;strong&gt;Hướng B&lt;/strong&gt; tìm hiểu cánh tay robot và tập dữ liệu LeRobot (chỉ đầu tư phần cứng khi bạn chủ động quyết định về sau); hoặc &lt;strong&gt;Hướng C&lt;/strong&gt; đào sâu lý thuyết với sách miễn phí &lt;em&gt;Modern Robotics&lt;/em&gt; và khóa học &lt;em&gt;Manipulation&lt;/em&gt; của MIT. Bạn sẽ viết ra một kế hoạch cụ thể gắn với chiếc Capstone sẵn có kèm cam kết duy trì các thói quen an toàn, đồng thời sử dụng danh mục tài liệu cuối bài như bản đồ tra cứu dài hạn.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Đã đi qua trình tự chương 08 đến chương 11 (kể cả khi một vài bài lab thực tế còn dang dở): bạn hiểu vai trò watchdog 300 ms, cấu trúc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, lý do policy của tay kẹp khác không áp được cho bánh xe Capstone và cách viết dataset card ở bài 12-01. Bạn không cần mạch mới, cảm biến mới hay bản build Nav2 hoàn chỉnh để lập kế hoạch — chính kế hoạch rõ ràng sẽ giúp bạn tránh mua sắm cảm tính.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Khóa học đã đưa bạn đi từ khung xe vi sai thực tế, qua đồ thị ROS 2 Jazzy và mô phỏng Gazebo, đến việc đọc hiểu các bài báo học máy hiện đại. Bước chuyển tiếp hiệu quả nhất là tập trung theo đuổi một chủ đề duy nhất trong một tháng, thay vì mở cùng lúc quá nhiều bài báo, hướng dẫn và giỏ hàng phần cứng trong một cuối tuần mà không hoàn thiện được mục tiêu nào. Dù chọn hướng nào, các nguyên tắc nền tảng (watchdog, thống nhất đơn vị, ghi chép sổ thực hành và công tắc nguồn phần cứng) vẫn luôn giữ nguyên hiệu lực.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Dưới đây là ba hướng đi để bạn chọn một cho kế hoạch tháng tới, đồng thời hiểu rõ hai hướng còn lại để không bị phân tâm giữa chừng:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hướng A — Phát triển xe di động thực tế.&lt;/strong&gt; Tận dụng ngay khung xe Capstone sẵn có để hoàn thiện những phần mới tham quan ở chương 11: dựng bản đồ, lập kế hoạch và bám quỹ đạo trên ROS 2 Jazzy theo &lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/&quot;&gt;hướng dẫn bắt đầu Nav2 cho Jazzy&lt;/a&gt;. Cảm biến phù hợp khi bạn sẵn sàng là lidar quét 360° cỡ nhỏ (phát &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt;, giá tham khảo khoảng 1,5–3,5 triệu đồng — tự kiểm tra giá mới nhất khi mua; không bắt buộc mua ngay tuần này). Kho mã nguồn &lt;a href=&quot;https://github.com/linorobot/linorobot2&quot;&gt;linorobot2&lt;/a&gt; là tài liệu tham khảo hữu ích để xem cách tổ chức tệp launch bringup, URDF và cấu hình Nav2 cho xe vi sai. Tuy nhiên, không nạp đè firmware của dự án đó lên mạch của bạn: giữ nguyên cầu nối serial, khung dữ liệu và watchdog 300 ms để đảm bảo chỉ có duy nhất một tầng điều khiển ghi xung PWM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hướng B — Cánh tay robot và dữ liệu LeRobot.&lt;/strong&gt; Khi bạn muốn thực hành học bắt chước trong không gian khớp, LeRobot (bài 12-01) là điểm xuất phát với các mẫu phần cứng như SO-100/SO-101, Koch và LeKiwi. Chọn hướng này đồng nghĩa bạn hoàn thiện dataset card (tên khóa, đơn vị, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps&lt;/code&gt;) trước khi chi tiền. Cánh tay là nhánh tùy chọn có chi phí cao (một bộ kit leader-follower đầy đủ gồm servo và vận chuyển thường ở mức nhiều triệu đồng); không nên mua dồn dập cùng lúc cả cánh tay, lidar lẫn máy tính phụ khi chưa chốt rõ vector hành động cần thu thập.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hướng C — Củng cố nền tảng lý thuyết.&lt;/strong&gt; Giáo trình &lt;a href=&quot;https://hades.mech.northwestern.edu/index.php/Modern_Robotics&quot;&gt;Modern Robotics&lt;/a&gt; (Lynch và Park) được phát hành miễn phí trực tuyến. Trong một tháng, hãy chọn đúng một chương liên kết trực tiếp với xe bánh (nơi phân tích rõ \(v\) và \(\omega\)) và chép chính xác tiêu đề từ trang web vào kế hoạch. Nửa còn lại là &lt;a href=&quot;https://manipulation.csail.mit.edu/&quot;&gt;Khóa manipulation của MIT&lt;/a&gt; giúp hiểu sâu về thao tác tay máy, tiếp xúc cơ học và lập kế hoạch — lý giải vì sao các bài báo VLA tập trung nhiều vào tay kẹp. Hãy đọc theo hướng lý thuyết thuần túy, tránh lấy lý do học hướng C để mua phần cứng của hướng B.&lt;/p&gt;

&lt;p&gt;Sai lầm phổ biến nhất ở giai đoạn này là đặt mua cùng lúc cánh tay 6 bậc, lidar và mạch Jetson trong một tuần — dàn trải ngân sách ra ba hướng trong khi chưa khai thác hết chiếc Capstone sẵn có. Những thói quen an toàn bắt buộc phải giữ trên mọi hướng gồm:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Duy trì watchdog 300 ms trong firmware nạp trên vi điều khiển.&lt;/li&gt;
  &lt;li&gt;Luôn nhấc bánh khỏi mặt sàn (hoặc đặt khung lên giá đỡ) ở lần chạy thử đầu tiên của bất kỳ publisher mới nào.&lt;/li&gt;
  &lt;li&gt;Luôn đặt công tắc nguồn phần cứng hoặc E-stop trong tầm tay với để ngắt trực tiếp khi cần, không phụ thuộc bàn phím hay phần mềm.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bên cạnh đó, tính nhất quán về đơn vị luôn được giữ vững: Hướng A dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; (m/s, rad/s), Hướng B dùng góc khớp theo datasheet servo (không ghi đè cột \(v, \omega\)), còn các công thức ở Hướng C luôn đối chiếu về đúng đơn vị vật lý trên dây truyền.&lt;/p&gt;

&lt;h2 id=&quot;hình&quot;&gt;Hình&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_paths.png&quot; alt=&quot;Ba hướng cho tháng tới&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Sơ đồ tóm tắt ba hướng lựa chọn cho một tháng (xanh lá: xe hiện trường với Nav2/lidar/linorobot2; vàng: cánh tay và dữ liệu LeRobot; xanh dương: lý thuyết Modern Robotics và MIT), đặt trên nền tảng chung gồm watchdog, đơn vị, sổ ghi chép và công tắc nguồn. Bạn có thể đọc lướt tiêu đề các hướng khác để biết thêm, nhưng không biến chúng thành dự án song song thứ hai.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đọc-có-số&quot;&gt;Ví dụ đọc có số&lt;/h2&gt;

&lt;p&gt;So sánh một bản kế hoạch chung chung (khó kiểm chứng):&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;path: A và một chút B, và tôi sẽ lướt RT-2
robot: cái gì đó có bánh, có khi mua cái mới
safety: cẩn thận
buy: lidar, arm, board
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;với một bản kế hoạch cụ thể và kiểm chứng được:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;path: A
robot I already have: Capstone diff-drive, cái có cầu serial
safety habit I will not drop: 300 ms không có khung mới thì v = 0 và w = 0
also still true: bánh nhấc lên trước publisher mới; công tắc nguồn trong tầm tay
this month I will not buy: một cánh tay, một bo hạng Jetson
first page I will open: https://docs.nav2.org/jazzy/getting_started/
lidar: không phải tuần này. Nếu một tháng nữa đế vẫn bám cmd_vel, đối lại lidar 360 nhỏ trong khoảng 1,5–3,5 triệu đồng
reference I will read and not flash over my firmware: https://github.com/linorobot/linorobot2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bản kế hoạch thứ hai chốt duy nhất một hướng, chỉ rõ phần cứng sẵn có, nêu thói quen an toàn bằng con số cụ thể (300 ms thay vì nói chung “cẩn thận”) và xác định rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linorobot2&lt;/code&gt; chỉ để tham khảo kiến trúc.&lt;/p&gt;

&lt;p&gt;Khung giá tham khảo để cân nhắc cho Hướng A (chỉ mua khi xe hiện tại đã vận hành ổn định, không mua dồn trong ngày đầu): lidar 360° nhỏ khoảng 1,5–3,5 triệu đồng; Raspberry Pi 5 (nếu cần tách khỏi laptop) khởi điểm gần 2,4 triệu đồng cho bản RAM thấp tại &lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;listing Pi 5 lắp ráp tại Anh trên Hshop&lt;/a&gt; (kiểm tra lại giá biến thể thực tế); cánh tay là nhánh đắt tiền nên không ghép chung giỏ hàng với Hướng A.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-lập-kế-hoạch-chọn-1-dự-án-mở-rộng-8-tuần-phù-hợp-đúng-năng-lực-hiện-có&quot;&gt;Giải thích: Lập kế hoạch chọn 1 dự án mở rộng 8 tuần phù hợp đúng năng lực hiện có&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Hướng bạn thấy hứng thú nhất&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Dự án 8 tuần khả thi ngay sau khóa này&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Bộ công cụ lõi cần đầu tư thêm&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tự hành ngoài trời / trong tòa nhà&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Nâng cấp Capstone B lên &lt;strong&gt;3D LiDAR / RGB-D SLAM + Nav2 Keepout Zones&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RTAB-Map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_localization&lt;/code&gt; (EKF), C++ plugin Nav2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tay máy &amp;amp; Thao tác tinh vi&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Thu thập 100 lượt trên &lt;strong&gt;SO-101&lt;/strong&gt;, so sánh trực tiếp &lt;strong&gt;ACT vs. Diffusion Policy&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LeRobot&lt;/code&gt;, PyTorch, hiệu chuẩn camera–tay máy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Robot đi bộ &amp;amp; Học tăng cường&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Huấn luyện robot 4 chân đi bộ vượt dốc bằng &lt;strong&gt;PPO trong MuJoCo / Isaac Lab&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MuJoCo&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Isaac Lab&lt;/code&gt;, học tăng cường (RL)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Viết bản kế hoạch bốn tuần của bạn vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; theo đầy đủ các trường bắt buộc sau:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;path: A hoặc B hoặc C
robot I already have: Capstone diff-drive
safety habit I will not drop: &amp;lt;một trong: timeout 300 ms, bánh nhấc lên ở lần chạy đầu, E-stop phần cứng&amp;gt;
this month I will not buy:
first link I will actually open:
what I will have in my notes after week one:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Sản phẩm kiểm chứng sau tuần đầu tiên theo từng hướng:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Hướng A:&lt;/strong&gt; một nhận xét về trang tài liệu Nav2 đã mở và nêu rõ thành phần nào của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linorobot2&lt;/code&gt; bạn không sao chép đè lên driver động cơ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hướng B:&lt;/strong&gt; một dataset card cho cánh tay dự kiến (ghi rõ đơn vị khớp “chờ xác nhận từ datasheet servo”) cùng cam kết tách riêng khóa \((v, \omega)\) của Capstone.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hướng C:&lt;/strong&gt; tiêu đề chương hoặc bài giảng trích chính xác từ &lt;em&gt;Modern Robotics&lt;/em&gt; hoặc khóa MIT kèm một phương trình/định nghĩa liên hệ trực tiếp với khung xe Capstone (nếu chưa liên hệ được, ghi rõ câu hỏi cần giải đáp cho tuần sau).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; chỉ chọn duy nhất một chữ cái hướng (A, B hoặc C), ghi đích danh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Capstone diff-drive&lt;/code&gt;, nêu rõ thói quen an toàn cụ thể (vẫn duy trì watchdog 300 ms), liệt kê ít nhất hai món sẽ &lt;em&gt;không&lt;/em&gt; mua trong tháng này và không kèm ảnh giỏ hàng.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Sai sót trong kế hoạch&lt;/th&gt;
      &lt;th&gt;Vấn đề&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Ghi “chọn A + B + C nhẹ nhàng”&lt;/td&gt;
      &lt;td&gt;Thực chất là dàn trải không trọng tâm; bắt buộc chọn một hướng duy nhất&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bước đầu tiên là đặt mua cả cánh tay, lidar và Jetson&lt;/td&gt;
      &lt;td&gt;Gộp ba hướng thành một đơn hàng, trái mục tiêu tập trung một tháng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chọn Hướng C rồi ghi watchdog “không liên quan vì chỉ đọc sách”&lt;/td&gt;
      &lt;td&gt;Đọc lý thuyết không làm thay đổi firmware đang nạp sẵn trên robot; quy tắc an toàn vẫn giữ nguyên&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nạp luôn firmware &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linorobot2&lt;/code&gt; đè lên driver động cơ ngay ngày thứ hai&lt;/td&gt;
      &lt;td&gt;Thay thế cơ chế watchdog bạn nắm rõ bằng mã nguồn chưa đọc kỹ; chỉ dùng để tham khảo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mục tiêu tuần một chỉ ghi “xem vài video”&lt;/td&gt;
      &lt;td&gt;Thiếu đầu ra kiểm chứng; cần nêu cụ thể trang tài liệu và nội dung sẽ tóm tắt&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=GDmhrAHxgQE&quot;&gt;Core Concepts: Imitation Learning&lt;/a&gt; — Sanjiban Choudhury (tiếng Anh). Nền tảng lý thuyết cần nắm nếu bạn cân nhắc hướng học bắt chước trước khi đầu tư cánh tay.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=5mY71rGXAkM&quot;&gt;π0: A Foundation Model for Robotics with Sergey Levine - 719&lt;/a&gt; — The TWIML AI Podcast with Sam Charrington (tiếng Anh). Mở rộng góc nhìn về mô hình nền tảng trong robotics (nằm ngoài phạm vi thực hành 24 tuần của khóa).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;p&gt;Mỗi câu hỏi tương ứng với một hướng cụ thể (không viết câu trả lời chung chung dùng cho cả ba hướng):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Hướng A:&lt;/strong&gt; Nêu một thành phần bạn có thể học hỏi từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linorobot2&lt;/code&gt; và một thành phần tuyệt đối giữ nguyên của riêng mình. &lt;em&gt;Gợi ý:&lt;/em&gt; học cách tổ chức tệp launch bringup hoặc quy ước đặt tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; trong URDF; giữ nguyên cầu nối serial và watchdog 300 ms trên firmware.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hướng B:&lt;/strong&gt; Liệt kê các khóa dữ liệu hành động bắt buộc phải làm rõ trước khi đặt mua cánh tay hạng SO-100. &lt;em&gt;Gợi ý:&lt;/em&gt; tên từng khớp, đơn vị từ datasheet servo (radian hoặc xung encoder), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps&lt;/code&gt;, khóa camera và xác nhận không trộn \((v, \omega)\) của Capstone vào các cột khớp này.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hướng C:&lt;/strong&gt; Tra cứu trang &lt;em&gt;Modern Robotics&lt;/em&gt; (hoặc khóa MIT), trích chính xác tiêu đề phần liên quan đến robot bánh xe và nêu rõ đại lượng nào của Capstone được giải thích trong phần đó. &lt;em&gt;Gợi ý:&lt;/em&gt; liên hệ trực tiếp với \(v\), \(\omega\) và mối quan hệ với vận tốc hai bánh.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ngân sách Hướng A:&lt;/strong&gt; Chỉ khi xe Capstone thực sự bám ổn định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; dưới sàn và watchdog đã kiểm chứng, bạn mới xét mua lidar. Khung giá tham khảo cho lidar 360° nhỏ là bao nhiêu và bạn xử lý thế nào nếu giá niêm yết vượt khung? &lt;em&gt;Gợi ý:&lt;/em&gt; 1,5–3,5 triệu đồng; nếu vượt khung thì chờ hoặc đổi phân khúc cảm biến phù hợp chứ không chuyển sang mua cánh tay cho “đủ bộ”.&lt;/li&gt;
  &lt;li&gt;Với đúng hướng bạn đã chọn trong lab, viết một câu hỏi Có/Không làm tiêu chí tự kiểm tra cuối tuần thứ tư dựa trên sổ ghi chép. &lt;em&gt;Gợi ý:&lt;/em&gt; Hướng A về việc Nav2 nối đúng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; và giữ watchdog 300 ms; Hướng B về dataset card thống nhất đơn vị và chưa mua sắm vội; Hướng C về việc tự phát biểu lại quan hệ động học bánh xe trong khi vẫn duy trì watchdog trên firmware.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;tài-liệu-tuyển&quot;&gt;Tài liệu tuyển&lt;/h2&gt;

&lt;p&gt;Tổng hợp các liên kết cốt lõi của khóa học để tra cứu dài hạn (chỉ mở sâu các mục khớp với hướng bạn chọn):&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ngăn xếp kỹ thuật đã thực hành&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/&quot;&gt;Tài liệu ROS 2 Jazzy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/&quot;&gt;Nav2 bắt đầu, Jazzy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/ros_installation&quot;&gt;Cài Gazebo cùng ROS, Harmonic&lt;/a&gt; — bộ mô phỏng tương thích trực tiếp với Jazzy.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/SteveMacenski/slam_toolbox&quot;&gt;slam_toolbox&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/linorobot/linorobot2&quot;&gt;linorobot2&lt;/a&gt; — kho mã nguồn xe vi sai để tham khảo (vẫn giữ nguyên cầu nối của bạn).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Thư viện và bài báo nghiên cứu&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;Docs LeRobot&lt;/a&gt; và &lt;a href=&quot;https://github.com/huggingface/lerobot&quot;&gt;huggingface/lerobot&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.13705&quot;&gt;Zhao và cộng sự, ACT, arXiv:2304.13705&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2303.04137&quot;&gt;Chi và cộng sự, Diffusion Policy, arXiv:2303.04137&lt;/a&gt; và &lt;a href=&quot;https://diffusion-policy.cs.columbia.edu/&quot;&gt;trang dự án&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://robotics-transformer1.github.io/&quot;&gt;RT-1&lt;/a&gt; và &lt;a href=&quot;https://arxiv.org/abs/2212.06817&quot;&gt;arXiv:2212.06817&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;RT-2, arXiv:2307.15818&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://robotics-transformer-x.github.io/&quot;&gt;Open X-Embodiment&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2406.09246&quot;&gt;OpenVLA, arXiv:2406.09246&lt;/a&gt; và &lt;a href=&quot;https://openvla.github.io/&quot;&gt;openvla.github.io&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pi.website/blog/pi0&quot;&gt;pi0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Mô phỏng nhà / Isaac Lab (đọc sau, không thay kế hoạch tháng tới)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter12/12_08_home_simulation_lab/&quot;&gt;Phòng lab mô phỏng tại nhà&lt;/a&gt; — bài tùy chọn: yêu cầu Isaac Sim/Isaac Lab, lối Mac hoặc thuê GPU theo giờ, khung giá card tại Việt Nam (30/11/2026, chỉ là ước lượng), và bước đầu là một actuator cycloid chứ không phải cả robot hình người.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt; — thư viện asset OpenUSD đã có vật lý (Alpha, hơn 2.000 mẫu). Bài pick-and-place và sim-to-real: 12-01, 10-05.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Robot hình người mã nguồn mở (đọc sau, không thay kế hoạch tháng tới)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt; (Hybrid Robotics, UC Berkeley; RSS 2025, &lt;a href=&quot;https://arxiv.org/abs/2504.17249&quot;&gt;arXiv:2504.17249&lt;/a&gt;) — robot hình người in 3D, mã mở, học tăng cường đi bộ và chuyển sim-to-real zero-shot. Phù hợp nhánh 3–4 ở đầu bài, sau khi đã chọn xong hướng tháng này. Bài tập hộp số cycloid ở 06-03; bài đọc sim-to-real ở 10-05.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Lý thuyết nền tảng (miễn phí)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hades.mech.northwestern.edu/index.php/Modern_Robotics&quot;&gt;Modern Robotics&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://manipulation.csail.mit.edu/&quot;&gt;MIT manipulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Máy tính nhúng (chỉ cân nhắc khi Hướng A cần tách khỏi laptop)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Raspberry Pi 5 trên Hshop&lt;/a&gt; — bản RAM thấp từng có giá khởi điểm khoảng 2,4 triệu đồng (cần đối chiếu lại biến thể và giá hiện tại).&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Tổng quan Vision-Language-Action</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_04_vla_overview/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_04_vla_overview</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~75 phút&lt;/strong&gt;, dành để đọc kỹ hai hệ thống nhằm hoàn thành bảng đối chiếu và nhận diện ba tên tuổi còn lại (không tải hay nạp checkpoint).&lt;/p&gt;

&lt;p&gt;ACT và Diffusion Policy rất giỏi thực hiện &lt;strong&gt;một kỹ năng chuyên biệt&lt;/strong&gt; mà chúng được dạy (ví dụ “gắp khối đỏ bỏ vào cốc”), nhưng nếu bạn đổi câu lệnh thành &lt;em&gt;“hãy đặt quả táo lên chiếc đĩa màu xanh gần nhất”&lt;/em&gt;, bạn phải thu dữ liệu và huấn luyện lại từ đầu. &lt;strong&gt;VLA (Vision–Language–Action)&lt;/strong&gt; — như RT-2, OpenVLA, \(\pi_0\) hay SmolVLA — hợp nhất tri thức ngữ nghĩa khổng lồ của mô hình ngôn ngữ–thị giác (VLM) với đầu ra hành động liên tục của robot để hướng tới chính sách đa nhiệm hiểu ngôn ngữ tự nhiên.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-mô-hình-thị-giácngôn-ngữhành-động-vla&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của Mô hình Thị giác–Ngôn ngữ–Hành động (VLA)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Nền tảng VLM Tiền huấn luyện (Pretrained Vision–Language Backbone)&lt;/strong&gt;:
Thừa hưởng khả năng nhận diện vật thể, màu sắc, quan hệ không gian (“bên trái”, “phía sau”) từ hàng tỷ cặp ảnh–văn bản trên Internet.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đầu ra Hành động (Discrete Action Token vs. Flow Matching / Diffusion Head)&lt;/strong&gt;:
VLA đời đầu (như OpenVLA) lượng tử hóa mỗi chiều hành động thành 256 token rời rạc để sinh giống chữ; thế hệ mới hơn (\(\pi_0\), SmolVLA) gắn thêm &lt;strong&gt;action expert dùng Flow Matching / Diffusion&lt;/strong&gt; để sinh hành động liên tục mượt mà ở tần số cao.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khoảng cách Hiện thân (Embodiment Gap) &amp;amp; Fine-tuning&lt;/strong&gt;:
Mỗi robot có số khớp, góc gắn camera và hệ tọa độ khác nhau. Do đó một mô hình VLA gốc hiếm khi chạy tốt ngay lập tức (zero-shot) trên cánh tay SO-101 của bạn mà thường cần &lt;strong&gt;fine-tune (LoRA)&lt;/strong&gt; trên vài chục đến vài trăm tập dữ liệu đặc thù của đúng cánh tay đó.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đánh đổi Tốc độ – Kích thước (7B Parameters vs. Edge Deployment)&lt;/strong&gt;:
Chạy mô hình 7 tỷ tham số ở 5–10 Hz đòi hỏi GPU mạnh (hoặc suy luận qua mạng LAN), mở ra hướng nén mô hình nhỏ gọn (SmolVLA) và suy luận bất đồng bộ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ định nghĩa được mô hình &lt;strong&gt;Vision-Language-Action (VLA)&lt;/strong&gt; là ánh xạ từ hình ảnh kết hợp câu lệnh ngôn ngữ sang hành động của một robot cụ thể. Bạn đặt được năm hệ thống tiêu biểu — &lt;strong&gt;RT-1&lt;/strong&gt;, &lt;strong&gt;RT-2&lt;/strong&gt;, &lt;strong&gt;Open X-Embodiment&lt;/strong&gt;, &lt;strong&gt;OpenVLA&lt;/strong&gt; và &lt;strong&gt;pi0&lt;/strong&gt; — lên cùng một khung so sánh (đầu vào, không gian hành động và mức độ tương thích với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; hoặc góc khớp của Capstone/SO-101). Bạn cũng sẽ diễn giải rõ khoảng cách hình thái phần cứng (&lt;em&gt;embodiment gap&lt;/em&gt;) qua ví dụ cụ thể, nắm vững nguyên tắc ưu tiên watchdog phần cứng cao hơn mọi câu lệnh ngôn ngữ và đánh giá trung thực khả năng huấn luyện tại nhà trên laptop.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Bạn biết cách đọc trang giới thiệu dự án nghiên cứu. Các bài 12-01 đến 12-03 đã trang bị kiến thức về khóa dataset, hành động dạng chunk và vấn đề đa phương án. Topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; của chương 09 vẫn mang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; (m/s) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; (rad/s); camera ở chương 11 (nếu có) là camera gắn thân xe chứ không phải camera cổ tay trên tay kẹp song song. Bài này thiết kế cho laptop thông thường không có GPU chuyên dụng.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Với ROS 2 Jazzy, Nav2 và cầu nối serial, bạn đã có thể gửi một tọa độ đích để khung xe vi sai tự bám đường. Các nghiên cứu trong bài này tiến thêm một bước: cho phép ra lệnh bằng ngôn ngữ tự nhiên để mạng nơ-ron trực tiếp xuất lệnh điều khiển. Tuy nhiên, rất dễ hiểu lầm rằng “robot hiểu ngôn ngữ tự nhiên nên tầng firmware và kiểm soát an toàn không còn quan trọng”. Thực tế, mọi checkpoint đều gắn chặt với camera và vector hành động của chính phần cứng sinh ra tập dữ liệu huấn luyện (tay máy di động của RT-1 hay tay kẹp trong Open X-Embodiment hoàn toàn khác với xe Capstone). Bài học này giúp bạn đọc các công bố VLA một cách tỉnh táo và đánh giá đúng giới hạn triển khai.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Một mô hình &lt;strong&gt;Vision-Language-Action (VLA)&lt;/strong&gt; nhận đầu vào là hình ảnh (pixel) cùng một chuỗi chỉ dẫn ngôn ngữ (như “nhặt quả táo” hay “đi ra cửa”) và xuất ra một hành động robot (dạng rời rạc hoặc liên tục). Điểm hấp dẫn là giao diện ra lệnh linh hoạt bằng ngôn ngữ tự nhiên; nhưng giới hạn cốt lõi là &lt;strong&gt;hình thái phần cứng (embodiment)&lt;/strong&gt; nằm ngay trong bản chất trọng số mô hình — chuyển sang một phần cứng khác đồng nghĩa mô hình đang xuất lệnh cho sai động cơ.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RT-1&lt;/strong&gt; (&lt;em&gt;Robotics Transformer&lt;/em&gt;, Brohan và cộng sự — trang dự án &lt;a href=&quot;https://robotics-transformer1.github.io/&quot;&gt;robotics-transformer1.github.io&lt;/a&gt;, bài báo &lt;a href=&quot;https://arxiv.org/abs/2212.06817&quot;&gt;arXiv:2212.06817&lt;/a&gt;) là mốc tiêu biểu theo hướng dữ liệu robot quy mô lớn. Kiến trúc transformer được huấn luyện trên tập dữ liệu mẫu lớn thu từ các robot tay máy di động của nhóm tác giả: nhận ảnh camera và câu lệnh, xuất ra vector hành động rời rạc gồm chuyển động cánh tay, đế di động và tay kẹp. Kênh di chuyển đế trong vector đó gắn với đơn vị và động học riêng của phần cứng RT-1, không phải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; trên đồ thị ROS của Capstone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RT-2&lt;/strong&gt; (&lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;arXiv:2307.15818&lt;/a&gt;) khởi nguồn từ mô hình thị giác – ngôn ngữ (VLM) đã tiền huấn luyện trên dữ liệu web quy mô lớn, sau đó đồng tinh chỉnh (co-fine-tune) bằng cách mã hóa hành động robot thành các token trong cùng luồng ngôn ngữ. Nhờ đó mô hình có khả năng chuyển giao tri thức từ ngôn ngữ web sang kỹ năng điều khiển robot. Tuy nhiên, việc tinh chỉnh đòi hỏi hạ tầng trung tâm dữ liệu quy mô lớn chứ không thể thực hiện trên laptop cá nhân, và các token hành động vẫn gắn chặt với phần cứng cánh tay sinh dữ liệu.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open X-Embodiment&lt;/strong&gt; (&lt;a href=&quot;https://robotics-transformer-x.github.io/&quot;&gt;robotics-transformer-x.github.io&lt;/a&gt;) là sáng kiến hợp tác gom tập dữ liệu từ nhiều phòng thí nghiệm và nhiều mẫu robot khác nhau để huấn luyện các mô hình tổng quát (RT-X). Bài học quan trọng cho khóa này là giới hạn của sự đa dạng dữ liệu: camera cổ tay, tay kẹp song song, robot delta và xe hai bánh có góc nhìn và không gian hành động khác hẳn nhau. Chuẩn hóa tên cột trong tệp dữ liệu không làm đồng nhất động lực học vật lý — một mô hình học từ nhiều loại tay kẹp vẫn không tự động nắm được hình học camera, chiều rộng trục bánh và watchdog 300 ms của xe Capstone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenVLA&lt;/strong&gt; (&lt;a href=&quot;https://arxiv.org/abs/2406.09246&quot;&gt;arXiv:2406.09246&lt;/a&gt;, trang dự án &lt;a href=&quot;https://openvla.github.io/&quot;&gt;openvla.github.io&lt;/a&gt;) là một mô hình VLA trọng số mở cùng hướng tiếp cận: lấy ảnh và chỉ dẫn để xuất hành động rời rạc, xây dựng từ xương sống VLM mở huấn luyện trên dữ liệu Open X-Embodiment. “Trọng số mở” cho phép nhà nghiên cứu có phần cứng phù hợp tải về kiểm thử, nhưng không có nghĩa laptop không GPU có thể huấn luyện lại hay chạy suy luận mạng nhiều tỉ tham số ở tần số 50 Hz cho vòng điều khiển serial; hơn nữa, đầu ra mặc định vẫn là chuyển động đầu cuối (end-effector) và tay kẹp chứ không phải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pi0&lt;/strong&gt; (Physical Intelligence — đọc trực tiếp tại bài blog &lt;a href=&quot;https://www.pi.website/blog/pi0&quot;&gt;pi0&lt;/a&gt;) đại diện cho hướng sinh hành động liên tục bằng kỹ thuật &lt;strong&gt;flow matching&lt;/strong&gt; (cùng họ ý tưởng với phương pháp khử nhiễu ở bài 12-03). Ảnh và ngôn ngữ đi vào, chuỗi hành động liên tục cho các robot của nhóm phát triển đi ra. Dù dùng flow matching, quỹ đạo tay kẹp vẫn không tự động biến thành lệnh cho khung xe hai bánh của bạn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nguyên tắc an toàn đứng trên mọi lệnh ngôn ngữ.&lt;/strong&gt; Một câu lệnh chữ như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;stop&quot;&lt;/code&gt; hoàn toàn không phải nút dừng khẩn cấp (E-stop): đối với mạng nơ-ron, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;stop&quot;&lt;/code&gt; chỉ là thêm một token đầu vào; mô hình vẫn có thể xuất ra vận tốc nhỏ, hoàn thành nốt một chunk cũ hoặc phản ứng sai nếu dữ liệu huấn luyện dùng từ khóa khác. Watchdog 300 ms trên firmware cũng không đọc hiểu tiếng Anh mà chỉ giám sát khoảng nghỉ giữa các khung serial. Do đó, lệnh dừng chỉ thực sự có hiệu lực khi mã điều khiển chuyển thành PWM bằng 0, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; bằng 0 hoặc công tắc nguồn phần cứng ngắt điện động cơ — và ở lần chạy thử đầu tiên của bất kỳ node mới nào, bánh xe vẫn phải nhấc khỏi mặt đất.&lt;/p&gt;

&lt;h2 id=&quot;hình&quot;&gt;Hình&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_vla.png&quot; alt=&quot;Ảnh và ngôn ngữ đi qua trọng số thành action&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Bốn khối chính biểu diễn luồng khái niệm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image + language → VLA weights → action&lt;/code&gt; (không phải sơ đồ node ROS). Ghi chú màu đỏ nhấn mạnh khoảng cách phần cứng (&lt;em&gt;embodiment&lt;/em&gt;) là một phần không tách rời của mô hình. Chân hình liệt kê năm hệ thống RT-1, RT-2, Open X-Embodiment, OpenVLA và pi0 với mục tiêu đọc hiểu và đối chiếu bảng, không chạy huấn luyện lại.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đọc-có-số&quot;&gt;Ví dụ đọc có số&lt;/h2&gt;

&lt;p&gt;Dưới đây là dòng mẫu hoàn chỉnh cho &lt;strong&gt;RT-1&lt;/strong&gt; làm chuẩn mức độ chi tiết cho bài lab (nếu trang dự án cập nhật chi tiết mới hơn, ưu tiên theo trang dự án):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hệ thống&lt;/th&gt;
      &lt;th&gt;Đầu vào&lt;/th&gt;
      &lt;th&gt;Không gian hành động&lt;/th&gt;
      &lt;th&gt;Có phải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; trên đồ thị của bạn hay đích khớp?&lt;/th&gt;
      &lt;th&gt;Chạy trên laptop tối nay?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;RT-1&lt;/td&gt;
      &lt;td&gt;Ảnh RGB từ camera robot của nhóm tác giả kèm câu chỉ dẫn&lt;/td&gt;
      &lt;td&gt;Vector rời rạc cho tay máy di động (cánh tay, đế di động, tay kẹp)&lt;/td&gt;
      &lt;td&gt;Chuyển động đế nằm trong vector riêng của họ; không phải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; hay đích khớp của Capstone&lt;/td&gt;
      &lt;td&gt;Đọc trang: có. Huấn luyện: không. Chạy suy luận trên laptop không GPU: không&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Để thấy rõ khoảng cách phần cứng: cầu nối Capstone nhận hai số thực \((v, \omega)\) mỗi nhịp, còn RT-1 rời rạc hóa chuyển động tay, đế và kẹp thành các mức (bin) gắn với chiều cao camera và kích thước khung xe riêng của họ. Ngay cả khi trích hai kênh đế để tự ánh xạ “bin 7 thành \(v = 0.2\) m/s”, đó đã là một bộ điều khiển hoàn toàn mới phải thử nghiệm lại với bánh nhấc khỏi sàn chứ không phải tính chất sẵn có của checkpoint.&lt;/p&gt;

&lt;p&gt;Lưu ý thêm cho cột laptop của &lt;strong&gt;RT-2&lt;/strong&gt;: câu trả lời trung thực cho việc huấn luyện RT-2 tại nhà là &lt;strong&gt;không&lt;/strong&gt;, vì quá trình đồng tinh chỉnh mô hình thị giác – ngôn ngữ cỡ lớn vượt xa khả năng máy tính cá nhân; mục tiêu đúng là đọc &lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;arXiv:2307.15818&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;so-101-không-phải-một-dòng-vla-có-sẵn&quot;&gt;SO-101 không phải một dòng VLA có sẵn&lt;/h2&gt;

&lt;p&gt;Cánh tay follower SO-101 ghi nhận vector hành động 6 giá trị ứng với 6 động cơ Feetech theo đúng tên khóa trong tập dữ liệu bạn tự thu, không có trong đầu ra của RT-1, RT-2, OpenVLA hay pi0. Việc tự ý gán “phần tử thứ tư cho khớp cổ tay” đồng nghĩa tạo ra bộ điều khiển mới và luôn tiềm ẩn rủi ro nếu không tắt mô-men (torque) hoặc cố định giá đỡ khi thử lần đầu.&lt;/p&gt;

&lt;p&gt;Phần thực hành tay máy nằm ở &lt;a href=&quot;/contents/vi/chapter12/12_06_so101_arm_lab/&quot;&gt;bài lab tay SO-101&lt;/a&gt;. Dòng đối chiếu bổ sung cho SO-101 (không thay thế hai dòng VLA yêu cầu ở mục Lab bên dưới):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hệ thống&lt;/th&gt;
      &lt;th&gt;Đầu vào&lt;/th&gt;
      &lt;th&gt;Không gian hành động&lt;/th&gt;
      &lt;th&gt;Có phải 6 khớp SO-101?&lt;/th&gt;
      &lt;th&gt;Chạy trên laptop tối nay?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;SO-101 của bạn&lt;/td&gt;
      &lt;td&gt;Ảnh camera (nếu gắn) kèm trạng thái khớp&lt;/td&gt;
      &lt;td&gt;6 giá trị theo đúng đơn vị trong tệp dataset&lt;/td&gt;
      &lt;td&gt;Có, vì đây là phần cứng bạn trực tiếp thu dữ liệu&lt;/td&gt;
      &lt;td&gt;Đọc tài liệu và hiệu chỉnh (khi có tay trên bàn): có. Huấn luyện không có GPU: không&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-sai-số-lượng-tử-hóa-khi-mã-hóa-hành-động-thành-256-bin-rời-rạc&quot;&gt;Giải thích: Tính tay sai số lượng tử hóa khi mã hóa hành động thành 256 bin rời rạc&lt;/h3&gt;

&lt;p&gt;Giả sử một mô hình VLA rời rạc hóa góc khớp vai \(q_1 \in [-90^\circ, +90^\circ]\) (dải rộng \(180^\circ\)) thành đúng &lt;strong&gt;256 token hành động&lt;/strong&gt; (tương ứng 255 khoảng chia đều):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Độ phân giải góc mỗi bước token:
\(\Delta q = \frac{180^\circ}{255} \approx \mathbf{0{,}706^\circ}.\)&lt;/li&gt;
  &lt;li&gt;Nếu cánh tay dài \(L = 0{,}40\text{ m}\), sai số vị trí đầu kẹp chỉ riêng do làm tròn góc 1 khớp đã là:
\(\Delta s = L \cdot \Delta q_{\text{rad}} = 0{,}40 \times \left(0{,}706 \times \frac{\pi}{180}\right) \approx \mathbf{4{,}9\text{ mm}}!\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sai số 5 mm này không vấn đề gì khi nhặt một quả cam, nhưng sẽ làm &lt;strong&gt;kẹt cứng&lt;/strong&gt; khi cắm một đầu dây USB đòi hỏi dung sai dưới 1 mm! Đó chính là lý do các VLA mới như \(\pi_0\) chuyển từ token rời rạc sang đầu ra liên tục (Flow Matching / Diffusion)!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Chọn &lt;strong&gt;hai&lt;/strong&gt; trong bốn hệ thống sau (không chọn lại RT-1 vì đã làm mẫu) để đọc trang gốc — không tải tệp trọng số:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;RT-2: &lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;arXiv:2307.15818&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Open X-Embodiment: &lt;a href=&quot;https://robotics-transformer-x.github.io/&quot;&gt;robotics-transformer-x.github.io&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;OpenVLA: &lt;a href=&quot;https://arxiv.org/abs/2406.09246&quot;&gt;arXiv:2406.09246&lt;/a&gt; và &lt;a href=&quot;https://openvla.github.io/&quot;&gt;openvla.github.io&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;pi0: &lt;a href=&quot;https://www.pi.website/blog/pi0&quot;&gt;pi.website/blog/pi0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chép khung bảng sau vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; và điền đầy đủ hai dòng bạn chọn:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hệ thống&lt;/th&gt;
      &lt;th&gt;Đầu vào&lt;/th&gt;
      &lt;th&gt;Không gian hành động&lt;/th&gt;
      &lt;th&gt;Có phải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; trên đồ thị của bạn hay đích khớp?&lt;/th&gt;
      &lt;th&gt;Chạy trên laptop tối nay?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; hai dòng đầy đủ thông tin sát nguồn gốc: cột đầu vào nêu rõ ảnh và ngôn ngữ (nếu tài liệu có), cột không gian hành động chỉ rõ phần cứng của bài báo khác với Capstone, cột &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; trả lời &lt;strong&gt;không&lt;/strong&gt; cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; của bạn (kể cả khi robot trong bài có bánh xe), và cột laptop trả lời rõ &lt;strong&gt;không&lt;/strong&gt; với việc huấn luyện tại nhà (không ghi mập mờ kiểu “có thể dùng Colab”).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Nội dung ghi trong bảng&lt;/th&gt;
      &lt;th&gt;Điểm sai&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Ghi “có xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;” vì thấy RT-1 có đế di động&lt;/td&gt;
      &lt;td&gt;Kênh điều khiển đế của họ khác hẳn topic, đơn vị và chiều rộng trục bánh của Capstone&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ghi “OpenVLA trọng số mở nên huấn luyện được tối nay”&lt;/td&gt;
      &lt;td&gt;Trọng số mở không thu nhỏ mô hình nhiều tỉ tham số xuống laptop không GPU để train hay suy luận thời gian thực&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ghi “Open X-Embodiment có nhiều robot nên đã bao phủ Capstone”&lt;/td&gt;
      &lt;td&gt;Bao phủ chỉ đúng khi tập dữ liệu thực sự chứa cảm biến và không gian hành động của chính xe bạn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ghi “có câu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stop&lt;/code&gt; nên không cần lo watchdog”&lt;/td&gt;
      &lt;td&gt;Watchdog chỉ giám sát khung serial, không hiểu ngôn ngữ; nếu policy còn phát lệnh thì động cơ vẫn chạy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bất kỳ dòng nào điền “có” cho việc huấn luyện tại nhà&lt;/td&gt;
      &lt;td&gt;Phải sửa lại thành “không” (chỉ đọc và đối chiếu tài liệu)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-sắm&quot;&gt;Mua sắm&lt;/h2&gt;

&lt;p&gt;Bài học này không yêu cầu mua thêm phần cứng. Không nên vội mua tay kẹp, mạch Jetson hay máy tính mới chỉ vì các bài báo VLA dùng nhiều camera hơn; việc gắn thêm camera lên Capstone cũng không biến nó thành phần cứng của RT-1 hay OpenVLA.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=-0s0v3q7mBk&quot;&gt;OpenVLA: LeRobot Research Presentation #5 by Moo Jin Kim&lt;/a&gt; — Hugging Face (tiếng Anh). Moo Jin Kim trình bày về OpenVLA trên kênh Hugging Face (nhận ảnh và câu lệnh, xuất hành động cho phần cứng cụ thể).&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[RT - 2 Visual Language Action Models&lt;/td&gt;
          &lt;td&gt;Robotics Transformer 2&lt;/td&gt;
          &lt;td&gt;Google DeepMind](https://www.youtube.com/watch?v=yJYchWc9j8A) — MLNews (tiếng Anh). Video ngắn tóm tắt RT-2 để đặt cạnh OpenVLA trước khi đọc bài báo gốc.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=5mY71rGXAkM&quot;&gt;π0: A Foundation Model for Robotics with Sergey Levine - 719&lt;/a&gt; — The TWIML AI Podcast with Sam Charrington (tiếng Anh). Phỏng vấn Sergey Levine về π0, giúp đối chiếu hành động liên tục với hành động rời rạc của OpenVLA.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nếu bảng lab của bạn có dòng RT-2, hãy viết đầy đủ một câu cho cột laptop và nêu rõ hoạt động bạn thực sự được thực hiện với tệp PDF bài báo. &lt;em&gt;Gợi ý:&lt;/em&gt; không huấn luyện tại nhà (kể cả trên máy mượn); chỉ đọc và trích dẫn thông tin từ phần tóm tắt/bài báo.&lt;/li&gt;
  &lt;li&gt;Trọng số OpenVLA được công bố công khai, nhưng vì sao điều đó không đồng nghĩa bạn có thể chạy vòng điều khiển trực tiếp trên chiếc laptop đang nạp ESP32? &lt;em&gt;Gợi ý:&lt;/em&gt; mô hình nhiều tỉ tham số đòi hỏi GPU mạnh, trong khi vòng điều khiển cần nhịp lệnh ổn định cỡ 20 ms trên laptop không GPU.&lt;/li&gt;
  &lt;li&gt;Giả sử một người ánh xạ kênh “mở tay kẹp” của một VLA sang vận tốc góc \(\omega\). Xe Capstone sẽ phản ứng thế nào mỗi lần policy mở kẹp? &lt;em&gt;Gợi ý:&lt;/em&gt; xe sẽ xoay bất ngờ vì gắn nhầm kênh điều khiển không liên quan — dataset card ở bài 12-01 giúp ngăn chặn lỗi này bằng quy định đơn vị rõ ràng.&lt;/li&gt;
  &lt;li&gt;Người vận hành gõ lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;stop&quot;&lt;/code&gt; nhưng node policy vẫn đều đặn gửi \(v = 0.15\) mỗi 20 ms. Watchdog 300 ms có kích hoạt không và yếu tố nào mới thực sự dừng được bánh xe? &lt;em&gt;Gợi ý:&lt;/em&gt; watchdog không kích hoạt vì khung vẫn đến đều; cần gửi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; tường minh trên đường serial hoặc ngắt công tắc nguồn phần cứng.&lt;/li&gt;
  &lt;li&gt;Nêu các thông số đầu vào cụ thể của Capstone mà tập dữ liệu Open X-Embodiment không thể tự suy ra hộ bạn. &lt;em&gt;Gợi ý:&lt;/em&gt; tư thế lắp camera nhìn trước, chiều rộng trục bánh và vector hành động hai chiều \((v, \omega)\) đặc trưng của chính khung xe Capstone.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://robotics-transformer1.github.io/&quot;&gt;Trang RT-1&lt;/a&gt; và &lt;a href=&quot;https://arxiv.org/abs/2212.06817&quot;&gt;bài RT-1&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;RT-2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://robotics-transformer-x.github.io/&quot;&gt;Open X-Embodiment&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2406.09246&quot;&gt;OpenVLA&lt;/a&gt; và &lt;a href=&quot;https://openvla.github.io/&quot;&gt;trang dự án&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pi.website/blog/pi0&quot;&gt;pi0, Physical Intelligence&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Trực giác Diffusion Policy</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_03_diffusion_policy/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_03_diffusion_policy</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~65 phút&lt;/strong&gt;, dành để đọc trang dự án, phân tích phép tính trung bình hành động và viết một đoạn nhận xét (không chạy khử nhiễu hay huấn luyện trên GPU).&lt;/p&gt;

&lt;p&gt;Trong bài toán đẩy một khối chữ T vào đúng vị trí mục tiêu (Push-T), nếu tay máy gặp góc vuông đối xứng, đi vòng sang trái hay đi vòng sang phải đều hoàn hảo — nhưng đi &lt;strong&gt;chính giữa&lt;/strong&gt; (trung bình của trái và phải) thì lại đẩy hỏng! &lt;strong&gt;Diffusion Policy&lt;/strong&gt; mang chính ý tưởng khử nhiễu dần từ mô hình sinh ảnh sang không gian hành động robot: thay vì đoán trực tiếp một điểm trung bình mờ nhạt, mạng học trường vector &lt;strong&gt;khử nhiễu từng bước&lt;/strong&gt; để kéo một chuỗi hành động ngẫu nhiên về đúng một nhánh quỹ đạo sắc nét hợp lệ.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-diffusion-policy&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Diffusion Policy&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Biểu diễn Phân phối Đa đỉnh (Multimodal Action Distribution)&lt;/strong&gt;:
Mô hình hóa phân phối xác suất điều kiện \(p(\mathbf{A}_t \mid \mathbf{O}_t)\) trên cả một khối hành động tương lai (action horizon \(T_p\)), giữ nguyên mọi nhánh rẽ hợp lệ thay vì bị sụp đổ về giá trị trung bình.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quá trình Khuếch tán Thuận (Thêm nhiễu) &amp;amp; Ngược (Khử nhiễu \(K\) bước)&lt;/strong&gt;:
Trong huấn luyện, mạng dự đoán phần nhiễu \(\varepsilon_\theta(\mathbf{A}_t^k, \mathbf{O}_t, k)\) đã thêm vào hành động thật. Khi suy luận trên robot, ta bắt đầu từ nhiễu Gaussian trắng \(\mathbf{A}_t^K \sim \mathcal{N}(0, I)\) và chạy vòng lặp khử nhiễu (DDPM / DDIM) để thu được quỹ đạo sạch \(\mathbf{A}_t^0\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ba Cửa sổ Thời gian (\(T_o, T_p, T_a\) — Receding Horizon Control)&lt;/strong&gt;:
Quan sát \(T_o\) khung hình gần nhất → dự đoán khối \(T_p\) bước hành động tương lai → chỉ thực thi \(T_a\) bước đầu tiên (\(T_a \le T_p\)) rồi quan sát lại và lập kế hoạch mới!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ giải thích được vì sao Diffusion Policy tạo ra quỹ đạo hành động bằng quá trình khử nhiễu lặp thay vì hồi quy một lần duy nhất. Thông qua ví dụ hai phiên mẫu xe Capstone vòng tránh một chiếc ghế, bạn sẽ chứng minh được vì sao lệnh trung bình lại đâm thẳng vào vật cản, đọc hiểu biểu diễn giản lược \(x_{t-1} = f(x_t, \text{observation})\) như sơ đồ tư duy của bộ khử nhiễu đã học (chứ không phải phương trình cập nhật đầy đủ của bài báo), và đối chiếu rõ nét cơ chế của Diffusion Policy với ACT trong hai đoạn ngắn.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành bài 12-01 và 12-02: bạn nắm rõ cấu trúc khóa dataset và cách tính cửa sổ hở vòng \(k/f\) giây của ACT. Bài học này không đòi hỏi GPU, môi trường conda hay checkpoint tải về; bạn chỉ cần truy cập trang dự án công khai.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Ở các chương trước, xe Capstone bám theo tuyến đường do bộ lập kế hoạch tường minh tính ra. &lt;strong&gt;Diffusion Policy&lt;/strong&gt; (Chi và cộng sự) đưa ra một cách tiếp cận khác khi quỹ đạo học từ các phiên điều khiển mẫu mà người lái có nhiều phương án đi khác nhau. Trong hành lang, hai người lái có thể vòng qua một chiếc ghế từ hai phía trái và phải; nếu buộc bộ điều khiển bám theo giá trị trung bình của hai đường đó, xe sẽ lao thẳng vào chiếc ghế — trong khi cả ROS 2 Jazzy lẫn firmware đều sẵn sàng chuyển tiếp và thực thi giá trị trung bình đó mà không hề cảnh báo. Bài học giúp bạn nhận diện bài toán phân phối đa chế độ (multimodal) ngay trong dữ liệu, đồng thời củng cố thói quen kiểm soát cửa sổ hở vòng tương tự như ở ACT.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Một mô hình hồi quy thông thường nhận đầu vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation&lt;/code&gt; và xuất ngay một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt; (hoặc một chunk) trong một lượt tính duy nhất. Cách này hoạt động tốt khi mọi mẫu huấn luyện cùng đi theo một phương án; nhưng khi dữ liệu tách thành hai phương án hợp lệ khác nhau, hàm mất mát bình phương sai số sẽ kéo dự đoán về điểm trung bình — một phương án thỏa hiệp không nằm trong tập mẫu và thường gây va chạm.&lt;/p&gt;

&lt;p&gt;Chi, Feng, Du, Xu, Cousineau, Burchfiel và Song mô hình hóa policy như một quá trình &lt;strong&gt;khử nhiễu có điều kiện&lt;/strong&gt; (conditional denoising): bắt đầu từ một chuỗi nhiễu ngẫu nhiên có cùng kích thước với quỹ đạo hành động, một mạng nơ-ron (lấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation&lt;/code&gt; làm điều kiện) sẽ từng bước khử bớt nhiễu để kéo chuỗi đó tiến dần về một quỹ đạo hợp lệ tương tự tập mẫu. Bốn khối trên sơ đồ minh họa tiến trình này: từ nhiễu thuần, qua các bước giảm nhiễu trung gian, đến quỹ đạo hoàn chỉnh gồm chuỗi \((v, \omega)\) (hoặc góc khớp) theo đúng đơn vị của robot. Các bước trung gian ban đầu tuyệt đối không được gửi tới động cơ, và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation&lt;/code&gt; luôn tham gia ở mọi bước khử nhiễu (nếu che camera, mô hình không còn căn cứ hình ảnh để chọn đường vòng phù hợp).&lt;/p&gt;

&lt;p&gt;Phép cập nhật dạng giản lược dùng để nắm trực giác trong khóa học này là:&lt;/p&gt;

\[x_{t-1} = f(x_t, \text{observation})\]

&lt;p&gt;Trong đó \(x_t\) là quỹ đạo còn nhiễu ở bước khử nhiễu thứ \(t\) (lưu ý chỉ số \(t\) đếm bước khử nhiễu chứ &lt;strong&gt;không phải&lt;/strong&gt; bước thời gian điều khiển), còn \(f\) đại diện cho bộ khử nhiễu đã học. Trong bài báo gốc, bước cập nhật đầy đủ bao gồm dự đoán thành phần nhiễu theo một lịch nhiễu (noise schedule) và cộng thêm nhiễu ngẫu nhiên để các lần lấy mẫu khác nhau có thể hội tụ về các nhánh hành động (mode) khác nhau.&lt;/p&gt;

&lt;p&gt;Đánh đổi của quá trình lặp là độ trễ tính toán cao hơn, nhưng lợi ích cốt lõi là mô hình học được phân phối xác suất trên toàn bộ các phương án hợp lệ: mỗi lần lấy mẫu sẽ rút ra một quỹ đạo nhất quán (hoặc vòng trái, hoặc vòng phải) thay vì trộn hai hướng lại thành cú đâm thẳng. Lưu ý rằng việc lấy trung bình hai mẫu đầu ra của Diffusion Policy sẽ tái tạo lại chính lỗi đâm vật cản mà phương pháp này muốn tránh.&lt;/p&gt;

&lt;p&gt;Tương tự khái niệm chunk ở ACT, Diffusion Policy thường áp dụng chiến lược &lt;strong&gt;chân trời lùi&lt;/strong&gt; (receding horizon): do khử nhiễu quỹ đạo dài khá tốn kém và đoạn cuối quỹ đạo dễ lỗi thời khi môi trường thay đổi, hệ thống chỉ thực thi một đoạn ngắn ở đầu quỹ đạo rồi chạy khử nhiễu lại từ đầu. Nếu 10 bước khử nhiễu tốn 100 ms và bạn thực thi 8 bước hành động đầu ở tần số 50 Hz, cửa sổ hở vòng dài \(8/50 = 0.16\) s — tương đương quy mô của ACT với \(k = 10\). Do đó, watchdog 300 ms vẫn không tự ngắt nếu mô hình tiếp tục phát lệnh tiến khi có người bước vào trước xe; bạn vẫn cần lệnh dừng tường minh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Đối chiếu ngắn gọn hai kiến trúc để tránh nhầm lẫn:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;ACT (Zhao và cộng sự):&lt;/strong&gt; dùng một lượt suy luận duy nhất qua transformer để sinh chunk \(k\) hành động. Biến ẩn của CVAE giúp chọn một phong cách thực hiện nhằm tránh trung bình hóa các mẫu trái ngược. Độ trễ gồm một lượt tính mạng cộng cửa sổ hở vòng của đoạn thực thi; cơ chế temporal ensemble làm mượt lệnh nhưng cũng có thể tạo ra giá trị trung gian nhỏ giữa “tiến” cũ và “dừng” mới.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Diffusion Policy (Chi và cộng sự):&lt;/strong&gt; không hồi quy trực tiếp một lần mà khởi đầu từ nhiễu và chạy lặp bộ khử nhiễu nhiều bước cho đến khi thu được quỹ đạo sạch. Tính đa chế độ đến từ quá trình lấy mẫu ngẫu nhiên có thể hội tụ về các nhánh khác nhau (vòng trái hoặc vòng phải), đổi lại phải trả chi phí nhiều lượt tính mạng cho mỗi lần lập kế hoạch lại. Cả hai phương pháp đều xử lý được hành động đa phương án, và cả hai đều vẫn đòi hỏi watchdog cùng lệnh dừng tường minh khi triển khai trên phần cứng thật.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;hình&quot;&gt;Hình&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_diffusion.png&quot; alt=&quot;Từ nhiễu đến một quỹ đạo&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Từ trái sang phải là tiến trình khử nhiễu: nhiễu thuần (đỏ), giảm nhiễu (vàng), tiệm cận hành động (nhạt) và quỹ đạo hoàn chỉnh (xanh). Các mũi tên biểu diễn bước khử nhiễu chứ không phải trục thời gian chạy của robot. Ghi chú phía dưới tóm tắt trọng tâm bài học: trung bình của “rẽ trái” và “rẽ phải” là đâm thẳng vào vật cản (nguồn: Chi và cộng sự, Diffusion Policy, 2023).&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đọc-có-số&quot;&gt;Ví dụ đọc có số&lt;/h2&gt;

&lt;p&gt;Xét hai phiên điều khiển mẫu trên Capstone tại cùng một thời điểm quyết định khi chiếc ghế nằm chính giữa, cách camera 0,40 m. Cả hai người lái cùng giữ vận tốc tiến 0,15 m/s nhưng chọn hai hướng rẽ ngược nhau:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mẫu&lt;/th&gt;
      &lt;th&gt;\(v\) (m/s)&lt;/th&gt;
      &lt;th&gt;\(\omega\) (rad/s)&lt;/th&gt;
      &lt;th&gt;Hướng đi&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;L&lt;/td&gt;
      &lt;td&gt;0.15&lt;/td&gt;
      &lt;td&gt;\(+0.50\)&lt;/td&gt;
      &lt;td&gt;Vòng sang trái&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;R&lt;/td&gt;
      &lt;td&gt;0.15&lt;/td&gt;
      &lt;td&gt;\(-0.50\)&lt;/td&gt;
      &lt;td&gt;Vòng sang phải&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trung bình&lt;/td&gt;
      &lt;td&gt;0.15&lt;/td&gt;
      &lt;td&gt;\(0.00\)&lt;/td&gt;
      &lt;td&gt;Đâm thẳng vào ghế&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Phép trung bình làm vận tốc góc yaw triệt tiêu hoàn toàn về 0 trong khi vận tốc tiến vẫn giữ nguyên 0,15 m/s. Nếu giữ nguyên lệnh này, thời gian để xe đâm vào chiếc ghế cách 0,40 m là:&lt;/p&gt;

\[\frac{0.40}{0.15} \approx 2.7\,\text{s}\]

&lt;p&gt;Trong khi đó, một bộ khử nhiễu đã học từ cả hai mẫu (dựa trên ảnh có chiếc ghế) sẽ kéo lần lấy mẫu về hẳn nhánh L hoặc hẳn nhánh R. Nếu không chắc chắn về nhánh được chọn, hành động an toàn phải là dừng lại (\(v = 0\)), tuyệt đối không lấy trung bình.&lt;/p&gt;

&lt;p&gt;Liên hệ với biểu diễn \(x_{t-1} = f(x_t, \text{observation})\): ở các bước khử nhiễu gần cuối, \(x_t\) có thể chứa hành động đầu xấp xỉ \((0.15, 0.48)\) và hàm \(f\) (kèm ảnh camera) tinh chỉnh nó về \((0.15, 0.50)\). Ở các bước khử nhiễu sớm, \(x_t\) chỉ là nhiễu thuần nên tuyệt đối không gửi lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; — luôn phân biệt rạch ròi chỉ số bước khử nhiễu \(t\) với nhịp điều khiển 50 Hz.&lt;/p&gt;

&lt;h2 id=&quot;áp-dụng-cùng-trực-giác-cho-cánh-tay-so-101&quot;&gt;Áp dụng cùng trực giác cho cánh tay SO-101&lt;/h2&gt;

&lt;p&gt;Xe Capstone có vector hành động 2 chiều $(v, \omega)$, còn cánh tay follower SO-101 có 6 động cơ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lift&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elbow&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wrist flex&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wrist roll&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gripper&lt;/code&gt;). Hai mẫu gắp một khối vật thể từ hai phía tương ứng hai vector 6 chiều khác nhau; việc lấy trung bình từng khớp có thể tạo ra tư thế cánh tay đâm xuyên qua vật thể — hoàn toàn tương đồng với hiện tượng triệt tiêu yaw khi tránh ghế. Nếu huấn luyện Diffusion Policy trên tập dữ liệu của SO-101, mô hình sẽ chọn một trong hai cách gắp; nó không liên quan gì đến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; của xe trừ khi bạn dùng đúng bộ khóa hành động $(v, \omega)$.&lt;/p&gt;

&lt;p&gt;Trên tài liệu imitation learning của LeRobot tại thời điểm đối chiếu, lệnh ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lerobot-train&lt;/code&gt; đang dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--policy.type=act&lt;/code&gt;, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diffusion&lt;/code&gt; là một lớp con (subclass) đăng ký trong mã nguồn. Ở &lt;a href=&quot;/contents/vi/chapter12/12_06_so101_arm_lab/&quot;&gt;bài lab SO-101&lt;/a&gt;, bạn sẽ kiểm tra tệp cấu hình trước khi thay đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;policy.type&lt;/code&gt; (vẫn không yêu cầu GPU).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_so101_action.png&quot; alt=&quot;Sáu khớp không phải vector của một VLA khác thân&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-ngân-sách-thời-gian-suy-luận-ddim-trên-cánh-tay-robot-chạy-ở-10-hz&quot;&gt;Giải thích: Tính tay ngân sách thời gian suy luận DDIM trên cánh tay robot chạy ở 10 Hz&lt;/h3&gt;

&lt;p&gt;Giả sử cánh tay robot cập nhật lệnh điều khiển ở &lt;strong&gt;10 Hz&lt;/strong&gt; (mỗi nhịp chỉ có đúng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100 ms&lt;/code&gt;), và ta cấu hình Diffusion Policy với \(T_o = 2\) khung quan sát, \(T_p = 16\) bước dự đoán và chỉ thực thi \(T_a = 8\) bước đầu tiên:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Thời gian thực thi 1 khối hành động \(T_a = 8\) bước:&lt;/strong&gt; \(8 \times 100\text{ ms} = \mathbf{800\text{ ms}}\) (robot có cả 0,8 giây quỹ thời gian trước khi cần khối mới!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;So sánh DDPM (100 bước khử nhiễu) vs. DDIM (10 bước khử nhiễu)&lt;/strong&gt; khi mỗi lượt forward mạng U-Net/Transformer tốn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12 ms&lt;/code&gt; trên GPU:
    &lt;ul&gt;
      &lt;li&gt;Dùng DDPM 100 bước: \(100 \times 12\text{ ms} = \mathbf{1.200\text{ ms}} &amp;gt; 800\text{ ms}\) → &lt;strong&gt;Quá hạn!&lt;/strong&gt; Robot bị khựng lại chờ tính toán!&lt;/li&gt;
      &lt;li&gt;Dùng DDIM 10 bước: \(10 \times 12\text{ ms} = \mathbf{120\text{ ms}} \ll 800\text{ ms}\) → &lt;strong&gt;Chạy mượt thời gian thực&lt;/strong&gt;, thậm chí đủ nhanh để giảm \(T_a = 2\) bước giúp phản ứng cực nhạy trước vật cản di động!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Mở trang dự án &lt;a href=&quot;https://diffusion-policy.cs.columbia.edu/&quot;&gt;diffusion-policy.cs.columbia.edu&lt;/a&gt; và trang tóm tắt &lt;a href=&quot;https://arxiv.org/abs/2303.04137&quot;&gt;arXiv:2303.04137&lt;/a&gt; để tìm nội dung mô tả việc mô hình hóa phân phối trên chuỗi hành động cùng hình minh họa bài toán đa phương án (không tải checkpoint hay chạy lệnh huấn luyện).&lt;/p&gt;

&lt;p&gt;Sau đó viết một đoạn phân tích vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; về hiện tượng đa phương án trên xe Capstone: mô tả tình huống hai mẫu vòng tránh chiếc ghế từ hai phía, tính rõ lệnh trung bình cùng hệ quả đâm vào ghế (dùng bộ số trong ví dụ hoặc tự chọn bộ số mới kèm phép tính thời gian va chạm), và kết thúc bằng một câu xác nhận rõ bạn không huấn luyện mô hình hay nạp trọng số trong bài này.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; đoạn văn tự kiểm chứng được gồm đầy đủ hai hướng rẽ, góc yaw bị triệt tiêu, vận tốc tiến khác 0, nguy cơ đâm vào ghế và xác nhận không chạy huấn luyện. Một đoạn chỉ nhận xét chung chung “diffusion xử lý đa mode tốt hơn” mà không chứng minh qua phép trung bình là chưa đạt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Sai sót&lt;/th&gt;
      &lt;th&gt;Nguyên nhân&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Gộp hai mẫu trái/phải thành một tập “trung tính” để làm sạch dữ liệu&lt;/td&gt;
      &lt;td&gt;Vô tình xóa cả hai đường né thành công và tạo ra mẫu đâm vào vật cản&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Coi việc thiếu GPU hoặc lỗi tràn bộ nhớ là trượt lab&lt;/td&gt;
      &lt;td&gt;Bài lab đánh giá đoạn phân tích lý thuyết, không chấm quá trình huấn luyện&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Kết luận Diffusion Policy tự động an toàn hơn vì có nhiều bước lặp&lt;/td&gt;
      &lt;td&gt;Quá trình lặp chỉ sinh ra một quỹ đạo mẫu; an toàn phần cứng vẫn phụ thuộc watchdog và lệnh dừng tường minh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Gửi kết quả bước khử nhiễu trung gian tới động cơ vì thấy có dạng \((v, \omega)\)&lt;/td&gt;
      &lt;td&gt;Bước trung gian vẫn là nhiễu; chỉ quỹ đạo hoàn chỉnh sau bước cuối mới là ứng viên lệnh&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=M03sZFfW-qU&quot;&gt;Diffusion Policy: LeRobot Research Presentation #2 by Cheng Chi&lt;/a&gt; — Hugging Face (tiếng Anh). Cheng Chi trực tiếp trình bày ý tưởng khử nhiễu quỹ đạo của Diffusion Policy trên kênh Hugging Face (xem để nắm trực giác, không cần chạy train).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Giả sử mẫu L là \((v, \omega) = (0.20, +0.80)\) và mẫu R là \((0.20, -0.40)\) (đơn vị m/s và rad/s). Tính lệnh trung bình, cho biết chiều yaw và đánh giá có nên thực thi lệnh trung bình này khi chiếc ghế nằm chính giữa không. &lt;em&gt;Gợi ý:&lt;/em&gt; trung bình là \((0.20, +0.20)\) — dù yaw không triệt tiêu về 0, lệnh này vẫn không thuộc hai mẫu hợp lệ ban đầu.&lt;/li&gt;
  &lt;li&gt;Trong biểu diễn giản lược \(x_{t-1} = f(x_t, \text{observation})\), chỉ số \(t\) biểu thị đại lượng gì? Viết một câu phân biệt nó với chu kỳ điều khiển 20 ms. &lt;em&gt;Gợi ý:&lt;/em&gt; \(t\) đếm bước khử nhiễu nội bộ của mạng, hoàn toàn tách biệt với trục thời gian thực thi lệnh trên robot.&lt;/li&gt;
  &lt;li&gt;Nếu bạn thực thi 8 bước đầu của quỹ đạo đã khử nhiễu ở tần số 50 Hz, cửa sổ hở vòng kéo dài bao lâu và watchdog 300 ms có kích hoạt trong khoảng thời gian đó không? &lt;em&gt;Gợi ý:&lt;/em&gt; \(8/50 = 0.16\) s; watchdog không kích hoạt vì lệnh vẫn đang được phát đều — nếu quỹ đạo hướng vào ghế, bắt buộc phải gửi tường minh lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;So với một lượt suy luận thuận của ACT, cơ chế rút mẫu từ nhiễu của Diffusion Policy phải trả thêm chi phí gì tại mỗi lần lập kế hoạch lại? &lt;em&gt;Gợi ý:&lt;/em&gt; chi phí độ trễ do phải chạy lặp mạng khử nhiễu nhiều lần thay vì một lượt duy nhất.&lt;/li&gt;
  &lt;li&gt;Nếu chiếc ghế chỉ còn cách 0,15 m và lệnh trung bình giữ \(v = 0.15\) m/s, \(\omega = 0\), sau bao lâu xe sẽ chạm ghế và khoảng thời gian đó dài hay ngắn hơn cửa sổ hở vòng 0,16 s? &lt;em&gt;Gợi ý:&lt;/em&gt; \(0.15/0.15 = 1.0\) s — dài hơn cửa sổ 0,16 s nên xe chưa đâm ngay trong một nhịp hở vòng, nhưng đoạn đầu đã đẩy xe hướng thẳng vào vật cản và cần cắt bằng lệnh dừng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://diffusion-policy.cs.columbia.edu/&quot;&gt;Trang dự án Diffusion Policy&lt;/a&gt; — xem ý tưởng cốt lõi và hình minh họa (không chạy phần huấn luyện).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2303.04137&quot;&gt;Chi và cộng sự, arXiv:2303.04137&lt;/a&gt; — đọc phần tóm tắt để nắm rõ tuyên bố của tác giả và lịch nhiễu thực tế đứng sau phác thảo \(f\).&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Học bắt chước và tổng quan ACT</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_02_imitation_act/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_02_imitation_act</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~70 phút&lt;/strong&gt;, tập trung đọc kỹ phần tóm tắt (abstract) của bài báo gốc và phân tích phép tính số học về 0,2 s chuyển động hở vòng (không chạy huấn luyện).&lt;/p&gt;

&lt;p&gt;Nếu bạn huấn luyện một mạng nơ-ron đơn giản kiểu Hành vi Nhân bản (Behavioral Cloning) dự đoán &lt;strong&gt;từng bước hành động một&lt;/strong&gt; (\(o_t \mapsto a_t\)), một căn bệnh chết người lập tức xuất hiện: &lt;strong&gt;sai số cộng dồn (compounding errors)&lt;/strong&gt;. Chỉ cần tay máy lệch 2 mm ở giây thứ 3, nó rơi vào trạng thái chưa từng thấy trong dữ liệu mẫu, rồi lệch thành 1 cm ở giây thứ 4 và gắp trượt hoàn toàn! Kiến trúc &lt;strong&gt;ACT (Action Chunking with Transformers)&lt;/strong&gt; giải quyết căn bệnh đó bằng một ý tưởng cực kỳ thông minh: thay vì dự đoán 1 bước, hãy dự đoán hẳn &lt;strong&gt;một khối \(k\) bước hành động liên tiếp&lt;/strong&gt; và làm mượt bằng tổ hợp thời gian.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-chính-sách-act-action-chunking-with-transformers&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Chính sách ACT (Action Chunking with Transformers)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Phân khối Hành động (Action Chunking — dự đoán \(k\) bước cùng lúc)&lt;/strong&gt;:
Giảm độ dài hiệu dụng của bài toán xuống \(k\) lần (ví dụ với \(k = 100\) bước ở 50 Hz = 2 giây), cắt mạnh sai số tích lũy và xử lý mượt các đoạn dừng tạm tự nhiên của người trình diễn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CVAE (Conditional Variational Autoencoder) xử lý Dữ liệu Đa mẫu (Multimodal)&lt;/strong&gt;:
Cùng một vật trên bàn, người trình diễn lúc thì vòng tay từ trái sang, lúc thì từ phải sang. Nếu chỉ hồi quy MSE thuần túy, mạng sẽ lấy &lt;em&gt;trung bình&lt;/em&gt; hai đường đi và đâm thẳng vào vật cản! Biến ẩn \(z\) của CVAE giúp mô hình biểu diễn trọn vẹn nhiều phong cách hoàn thành khác nhau.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tổ hợp Thời gian (Temporal Ensembling)&lt;/strong&gt;:
Ở mỗi bước \(t\), mô hình kết hợp trung bình có trọng số mũ \(w_i = \exp(-m \cdot i)\) giữa dự đoán mới nhất và các dự đoán còn dư từ những bước trước đó để chuyển động cánh tay êm mượt không giật cục.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ định nghĩa được &lt;strong&gt;học bắt chước hành vi&lt;/strong&gt; (behavior cloning) là ánh xạ từ quan sát (observation) sang hành động (action) học từ dữ liệu mẫu thay vì hàm phần thưởng (reward). Bạn nắm rõ đầu ra của mô hình ACT (Zhao và cộng sự, RSS 2023): một khối (&lt;strong&gt;chunk&lt;/strong&gt;) gồm \(k\) hành động tương lai do kiến trúc transformer sinh ra, được huấn luyện theo khung conditional variational autoencoder (CVAE) để xử lý nhiều cách thực hiện hợp lệ cùng lúc. Bạn tự tính được cửa sổ hở vòng cho \(k = 10\) ở tần số 50 Hz, đối chiếu trực tiếp với watchdog chương 08, và giải thích bản chất của cơ chế kết hợp theo thời gian (temporal ensemble) là phép lấy trung bình các chunk chồng lấp.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Đã hoàn thành dataset card ở bài 12-01 (camera, \(v\) theo m/s, \(\omega\) theo rad/s và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps&lt;/code&gt;), hiểu cơ chế watchdog 300 ms trên firmware tự đưa động cơ về 0 khi mất khung dữ liệu, và biết cách đọc tóm tắt bài báo khoa học. Bài này không yêu cầu GPU, phần cứng ALOHA hay cài đặt thư viện huấn luyện.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Cho tới hết chương Nav2, lệnh trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; luôn là từng message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; đơn lẻ sinh ra ở mỗi nhịp bởi một bộ điều khiển tường minh. Với các policy học máy như ACT, dạng đầu ra thay đổi: thay vì chỉ dự đoán cho 20 ms kế tiếp, mạng xuất ra một chuỗi ngắn các hành động tương lai để robot thực thi liên tiếp trước khi truy vấn lại mạng. Chuỗi đó là một đoạn chuyển động &lt;strong&gt;hở vòng&lt;/strong&gt; (open-loop). Cầu nối Capstone vẫn truyền tiếp được các lệnh này, nhưng tự thân nó không đảm bảo an toàn: watchdog 300 ms chỉ kích hoạt khi &lt;em&gt;mất khung truyền&lt;/em&gt;, trong khi một policy đang chạy bình thường vẫn có thể phát đều đặn phần còn lại của một chunk nguy hiểm. Bài học này giúp bạn phân tích rủi ro đó trước khi bước vào huấn luyện.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Behavior cloning&lt;/strong&gt; là bài toán học có giám sát: con người điều khiển robot để thu thập các cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(observation, action)&lt;/code&gt; mẫu, sau đó mạng nơ-ron được huấn luyện để tái tạo lại hành động tương ứng khi gặp quan sát tương tự — không cần định nghĩa hàm reward và không bắt buộc có bộ mô phỏng. Chất lượng mô hình phụ thuộc trực tiếp vào chất lượng log dữ liệu (đơn vị, đồng hồ và trạng thái watchdog như ở bài 12-01). Nếu dữ liệu huấn luyện thuộc vector khớp của cánh tay SO-101 rồi đem áp lên xe Capstone, lỗi sai hình thái phần cứng vẫn xảy ra y nguyên.&lt;/p&gt;

&lt;p&gt;Một đầu hồi quy đơn giản huấn luyện bằng sai số bình phương gặp hạn chế nghiêm trọng khi có nhiều cách giải quyết hợp lệ cho cùng một tình huống (ví dụ một mẫu đi vòng sang trái chiếc cốc, mẫu kia đi vòng sang phải — giá trị trung bình của hai hành động lại đâm thẳng vào chiếc cốc). Trong bài báo &lt;em&gt;Learning Fine-Grained Bimanual Manipulation with Low-Cost Hardware&lt;/em&gt;, Zhao, Kumar, Levine và Finn giải quyết vấn đề này bằng cách huấn luyện policy như một &lt;strong&gt;conditional variational autoencoder (CVAE)&lt;/strong&gt;: một biến ẩn cho phép mô hình chọn một “phong cách” thực hiện cụ thể, sau đó bộ giải mã transformer dự đoán chuỗi hành động phù hợp với phong cách đó cùng ảnh camera và trạng thái khớp hiện tại. Khi triển khai thực tế, phần encoder dùng lúc huấn luyện được bỏ đi và biến ẩn được lấy từ phân phối tiên nghiệm (thường đặt tại tâm 0). Biến ẩn giúp mô hình cam kết theo một phương án nhất quán chứ không phải chứng nhận an toàn hay cảm biến tránh va chạm.&lt;/p&gt;

&lt;p&gt;Đầu ra của transformer là một &lt;strong&gt;chunk&lt;/strong&gt; gồm \(k\) hành động tương lai liên tiếp — đây là ý nghĩa của tên gọi &lt;strong&gt;Action Chunking&lt;/strong&gt;. Lý do thực tiễn rất rõ ràng: ở tần số điều khiển 50 Hz, mỗi chu kỳ chỉ dài 20 ms; nếu mạng mất 80 ms để suy luận thì việc hỏi mạng từng bước sẽ luôn trễ bốn chu kỳ. Dự đoán một chunk 10 bước giúp che phủ thời gian suy luận và cho robot thực thi liên tục trong khi chờ lượt tính kế tiếp. Tuy nhiên, cái giá phải trả là robot chạy hở vòng suốt khoảng thời gian chưa lập kế hoạch lại. Trong bài báo gốc, tác giả dùng hệ thống phần cứng &lt;strong&gt;ALOHA&lt;/strong&gt; (cặp cánh tay leader-follower giá thấp cho thao tác hai tay); ở đây ta không chế tạo ALOHA mà mượn khái niệm chunking để phân tích tác động lên xe vi sai Capstone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Temporal ensemble&lt;/strong&gt; là kỹ thuật làm mượt ở giai đoạn phát lại: policy được gọi đủ dày sao cho nhiều chunk dự đoán chồng lấp lên cùng một thời điểm tương lai, và lệnh thực thi là trung bình có trọng số của các dự đoán đó (ưu tiên chunk mới hơn theo trọng số suy giảm). Mục đích là giảm giật cục khi hai lần suy luận liên tiếp hơi lệch nhau — không phải cơ chế dừng khẩn cấp, vì trung bình giữa “đang tiến” và “dừng” vẫn là “bò chậm về trước”. Lệnh dừng thực sự phải là một giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; chủ động hoặc ngắt nguồn/timeout.&lt;/p&gt;

&lt;p&gt;Cần đặt rõ mối quan hệ giữa watchdog và chunk: watchdog chương 08 ngắt động cơ sau 300 ms không có khung serial hợp lệ (bảo vệ khi tuột cáp USB hoặc treo tiến trình). Nó &lt;strong&gt;không&lt;/strong&gt; bảo vệ được tình huống tiến trình policy vẫn chạy khỏe và tiếp tục gửi các bước còn lại của một chunk đi thẳng ngay khi có người vừa bước vào trước mặt xe — vì dưới góc nhìn firmware, các khung truyền vẫn đều đặn. Do đó, cơ chế cắt chunk phải nằm ngay ở tầng phát lệnh: lập tức hủy các bước còn lại và xuất bản \((0, 0)\). Ngoài ra, tần suất lập kế hoạch lại cũng quyết định độ rủi ro: chỉ hỏi lại mạng khi hết chunk khiến toàn bộ chunk trở thành hở vòng, trong khi hỏi dày hơn giúp loại bỏ dự đoán cũ sớm hơn nhưng vẫn không thay thế được lệnh dừng tường minh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;hình&quot;&gt;Hình&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_act.png&quot; alt=&quot;ACT biến ảnh và trạng thái khớp thành một chunk&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Khối bên trái biểu diễn đầu vào điều kiện của mô hình (ảnh và trạng thái khớp trong bài báo gốc — tương ứng với ảnh camera và vận tốc đo \((v, \omega)\) trên Capstone). Khối giữa là chunk gồm \(k\) hành động, còn khối phải là quá trình thực thi rồi lập kế hoạch lại. Ghi chú đỏ nhắc nhở điều quan trọng nhất khi ứng dụng lên phần cứng: chunk mang bản chất hở vòng nên luôn cần đi kèm watchdog và cơ chế dừng khẩn tường minh (nguồn tham khảo: Zhao và cộng sự, ACT / ALOHA, 2023).&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đọc-có-số&quot;&gt;Ví dụ đọc có số&lt;/h2&gt;

&lt;p&gt;Xét một chunk minh họa với \(k = 10\) ở tần số điều khiển \(f = 50\) Hz:&lt;/p&gt;

\[T_{\text{open}} = \frac{k}{f} = \frac{10}{50} = 0.2\,\text{s}\]

&lt;p&gt;Nếu thực thi trọn vẹn cả chunk rồi mới quan sát lại, cửa sổ hở vòng dài 0,2 s. Bảng dưới đây là một chunk hành động giả định cho Capstone (bắt đầu từ \(t = 0\), mỗi bước cách nhau 0,02 s):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bước&lt;/th&gt;
      &lt;th&gt;Thời gian (s)&lt;/th&gt;
      &lt;th&gt;\(v\) (m/s)&lt;/th&gt;
      &lt;th&gt;\(\omega\) (rad/s)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
      &lt;td&gt;0.25&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0.02&lt;/td&gt;
      &lt;td&gt;0.25&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;0.04&lt;/td&gt;
      &lt;td&gt;0.25&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;0.06&lt;/td&gt;
      &lt;td&gt;0.25&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;0.08&lt;/td&gt;
      &lt;td&gt;0.24&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;0.10&lt;/td&gt;
      &lt;td&gt;0.24&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;0.12&lt;/td&gt;
      &lt;td&gt;0.20&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;7&lt;/td&gt;
      &lt;td&gt;0.14&lt;/td&gt;
      &lt;td&gt;0.15&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;0.16&lt;/td&gt;
      &lt;td&gt;0.10&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt;0.18&lt;/td&gt;
      &lt;td&gt;0.05&lt;/td&gt;
      &lt;td&gt;0.00&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Giả sử tại \(t = 0.08\) s (bước 4), có người bước vào trước camera. Sáu bước còn lại (từ bước 4 đến bước 9) tương ứng 0,12 s lệnh đã lên lịch sẵn. Lấy vận tốc đại diện khoảng 0,20 m/s, quãng đường xe tiếp tục bị đẩy đi là:&lt;/p&gt;

\[0.20 \times 0.12 = 0.024\,\text{m}\]

&lt;p&gt;tức khoảng 2,4 cm (chưa kể quán tính phanh của bánh xe). Tại thời điểm 0,08 s, watchdog 300 ms hoàn toàn không kích hoạt vì cầu nối vẫn đang đều đặn gửi các bước 4, 5, 6. Để dừng ngay lập tức, hệ thống phải chủ động hủy chunk và xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; (nhanh hơn nhiều so với việc ngừng gửi rồi chờ hết 300 ms).&lt;/p&gt;

&lt;p&gt;Với độ dài chunk lớn hơn — chẳng hạn 100 bước ở 50 Hz thường xuất hiện trong tài liệu ACT:&lt;/p&gt;

\[\frac{100}{50} = 2.0\,\text{s}\]

&lt;p&gt;cửa sổ hở vòng lên tới 2 giây nếu không lập kế hoạch lại sớm — hoàn toàn không an toàn cho xe di động chạy trong hành lang. Do đó không thể áp dụng máy móc độ dài chunk của bài báo gốc sang xe Capstone.&lt;/p&gt;

&lt;p&gt;Xét thêm tác động của temporal ensemble tại một thời điểm chồng lấp: chunk cũ dự đoán \(v = 0.25\) m/s tại \(t = 0.20\) s, còn chunk mới (sau khi thấy vật cản) dự đoán \(v = 0.00\) m/s. Trung bình cộng vẫn cho ra \(0.125\) m/s; ngay cả khi dùng trung bình có trọng số nghiêng về chunk mới (tỉ lệ 3/4 mới và 1/4 cũ), kết quả vẫn là \(0.0625\) m/s — xe vẫn tiến lên. Ensemble chỉ làm mượt lệnh chứ không thay thế được lệnh dừng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-trọng-số-tổ-hợp-thời-gian-temporal-ensembling-trong-act&quot;&gt;Giải thích: Tính tay trọng số Tổ hợp Thời gian (Temporal Ensembling) trong ACT&lt;/h3&gt;

&lt;p&gt;Giả sử tại bước thời gian \(t = 10\), ta đang có &lt;strong&gt;3 dự đoán&lt;/strong&gt; cho góc khớp vai \(q_1(10)\) đến từ 3 lần suy luận gần nhất (với hệ số suy giảm \(m = 0{,}5\)):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Dự đoán từ bước \(t = 10\) (mới nhất, tuổi \(i = 0\)): \(\hat{a}^{(0)} = 42{,}0^\circ\), trọng số \(w_0 = e^{-0{,}5 \times 0} = \mathbf{1{,}000}\).&lt;/li&gt;
  &lt;li&gt;Dự đoán từ bước \(t = 9\) (tuổi \(i = 1\)): \(\hat{a}^{(1)} = 40{,}0^\circ\), trọng số \(w_1 = e^{-0{,}5 \times 1} \approx \mathbf{0{,}607}\).&lt;/li&gt;
  &lt;li&gt;Dự đoán từ bước \(t = 8\) (tuổi \(i = 2\)): \(\hat{a}^{(2)} = 39{,}0^\circ\), trọng số \(w_2 = e^{-0{,}5 \times 2} \approx \mathbf{0{,}368}\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tính hành động thực tế gửi xuống động cơ:&lt;/strong&gt;
\(a_{10}^* = \frac{1{,}000(42{,}0) + 0{,}607(40{,}0) + 0{,}368(39{,}0)}{1{,}000 + 0{,}607 + 0{,}368} = \frac{42{,}00 + 24{,}28 + 14{,}35}{1{,}975} = \frac{80{,}63}{1{,}975} \approx \mathbf{40{,}83^\circ}.\)&lt;/p&gt;

&lt;p&gt;Thay vì nhảy giật từ \(40^\circ\) lên \(42^\circ\) rồi lại tụt xuống, cơ chế trung bình mũ tạo ra quỹ đạo góc khớp cực kỳ mượt mà cho động cơ servo!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Bài thực hành này chỉ yêu cầu đọc hiểu và phân tích (không tải trọng số hay chạy huấn luyện).&lt;/p&gt;

&lt;p&gt;Mở trang tóm tắt bài báo &lt;a href=&quot;https://arxiv.org/abs/2304.13705&quot;&gt;Zhao và cộng sự, arXiv:2304.13705&lt;/a&gt; và ghi bốn mục sau vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Tên tác giả và thông tin hội nghị xuất bản (bài học dùng RSS 2023; nếu trích trực tiếp từ trang arXiv thì giữ nguyên chữ trên trang).&lt;/li&gt;
  &lt;li&gt;Đầu ra của phương pháp là gì: một chunk gồm nhiều hành động tương lai thay vì từng lệnh đơn lẻ (nêu rõ cụm từ “action chunking” từ bài báo gốc hoặc ghi rõ nếu bạn diễn giải lại).&lt;/li&gt;
  &lt;li&gt;Vai trò của phần cứng trong tiêu đề: hệ thống giàn thu mẫu hai tay chi phí thấp ALOHA (không phải phần cứng cần mua cho khóa này).&lt;/li&gt;
  &lt;li&gt;Phân tích độ dài chunk cho xe Capstone với phép tính tường minh: \(k = 10\) ở 50 Hz cho \(T_{\text{open}} = 0.2\) s, kèm một câu phân biệt rõ khi nào watchdog ngắt (khi bên xuất bản ngừng hoạt động) và khi nào bắt buộc gửi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; tường minh (khi bên xuất bản vẫn chạy nhưng chunk hiện tại không còn an toàn).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; bản ghi chép dài khoảng nửa trang có phép chia ra 0,2 s, tách bạch lệnh dừng tường minh với watchdog timeout và không kèm log huấn luyện.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tình huống&lt;/th&gt;
      &lt;th&gt;Cách xử lý&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Tự ý chạy script huấn luyện trên laptop khiến tràn bộ nhớ (OOM)&lt;/td&gt;
      &lt;td&gt;Dừng ngay; bài học không yêu cầu huấn luyện trên máy không có GPU chuyên dụng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ghi chú kết luận chung chung “watchdog sẽ xử lý chunk lỗi”&lt;/td&gt;
      &lt;td&gt;Viết lại rõ: watchdog chỉ phát hiện trạng thái mất tín hiệu, không phát hiện được chunk xấu vẫn đang truyền đều&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chép một số \(k\) từ tệp cấu hình bất kỳ mà thiếu phép chia theo tần số&lt;/td&gt;
      &lt;td&gt;Luôn quy đổi \(k / f\) ra giây để đánh giá quãng đường hở vòng thực tế&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hiểu nhầm ALOHA là thiết bị cần đặt mua tiếp theo&lt;/td&gt;
      &lt;td&gt;ALOHA chỉ là hệ thống thu dữ liệu của bài báo; robot của khóa vẫn là Capstone&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=ft73x0LfGpM&quot;&gt;ALOHA and ACT: LeRobot Research Presentation #1 by Alexander Soare&lt;/a&gt; — Hugging Face (tiếng Anh). Trình bày trực tiếp về bài báo ACT và hệ thống ALOHA trên kênh Hugging Face.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GDmhrAHxgQE&quot;&gt;Core Concepts: Imitation Learning&lt;/a&gt; — Sanjiban Choudhury (tiếng Anh). Giải thích lý do học bắt chước không thể đơn thuần lấy trung bình các mẫu hành động — nền tảng cho kiến trúc của ACT.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Diễn đạt khái niệm behavior cloning trong một câu gắn trực tiếp với các khóa dữ liệu của xe Capstone. &lt;em&gt;Gợi ý:&lt;/em&gt; là ánh xạ nhận ảnh camera (và có thể kèm \(v, \omega\) đo được) để dự đoán cặp lệnh \(v, \omega\) mà người điều khiển đã gửi, không sử dụng hàm reward.&lt;/li&gt;
  &lt;li&gt;Khi hai phiên mẫu đi vòng sang hai phía của cùng một chiếc cốc, mô hình hồi quy bình phương sai số sẽ lấy trung bình hai hướng. Biến ẩn của CVAE giải quyết vấn đề gì mà phép trung bình không làm được? &lt;em&gt;Gợi ý:&lt;/em&gt; giúp mô hình cam kết theo một hướng đi nhất quán thay vì đâm vào giữa (tuy nhiên vẫn không thay thế được cảm biến đo khoảng cách).&lt;/li&gt;
  &lt;li&gt;Tính cửa sổ hở vòng khi \(k = 100\) ở 50 Hz và đánh giá có nên áp dụng nguyên văn cho Capstone chạy trong hành lang hay không. &lt;em&gt;Gợi ý:&lt;/em&gt; \(100/50 = 2.0\) s — quá dài và nguy hiểm cho hành lang; hãy đề xuất một giá trị \(k\) nhỏ hơn kèm thời gian tương ứng.&lt;/li&gt;
  &lt;li&gt;Tại cùng một mốc thời gian, chunk cũ dự đoán \(v = 0.25\) m/s còn chunk mới dự đoán \(v = 0\) m/s. Nếu gửi giá trị trung bình cộng, bánh xe đã dừng chưa và cần gửi gì khi có người xuất hiện trước camera? &lt;em&gt;Gợi ý:&lt;/em&gt; chưa dừng (trung bình là \(0.125\) m/s); cần hủy chunk và xuất bản tường minh \(v = 0, \omega = 0\).&lt;/li&gt;
  &lt;li&gt;Cầu nối vẫn gửi khung serial đều đặn mỗi 20 ms từ phần còn lại của một chunk đi thẳng ngay sau khi phát hiện vật cản. Watchdog 300 ms có tự ngắt động cơ không? &lt;em&gt;Gợi ý:&lt;/em&gt; không, vì các khung liên tục đặt lại bộ định thời; phải chủ động gửi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; hoặc dừng hẳn bên xuất bản.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.13705&quot;&gt;Zhao và cộng sự, Learning Fine-Grained Bimanual Manipulation with Low-Cost Hardware&lt;/a&gt; — đọc phần tóm tắt và sơ đồ kiến trúc phương pháp (bỏ qua phần huấn luyện trong kho mã nguồn).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;Docs LeRobot&lt;/a&gt; — xem mục giới thiệu ACT trong danh sách các policy được hỗ trợ.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Giới thiệu LeRobot / HF robotics</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_01_lerobot_intro/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter12/12_01_lerobot_intro</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~70 phút&lt;/strong&gt; (khoảng 25 phút đọc tài liệu và kho mã nguồn, 30 phút viết dataset card, 15 phút làm bài tập).&lt;/p&gt;

&lt;p&gt;Suốt 11 chương vừa qua, ta lập trình robot theo lối cổ điển: tự viết phương trình động học, tự thiết kế bản đồ và tự chỉnh hàng chục tham số PID/Nav2. Cách tiếp cận đó cực kỳ chính xác với xe chạy trong hành lang, nhưng lại bó tay trước những thao tác tưởng chừng đơn giản với trẻ lên 5: &lt;em&gt;gấp một chiếc áo sơ mi, nhặt một sợi dây cáp mềm hay múc một thìa cà phê&lt;/em&gt;. Chương 12 mở ra làn sóng &lt;strong&gt;Robot Học (Robot Learning)&lt;/strong&gt; với hệ sinh thái mã nguồn mở &lt;strong&gt;LeRobot (Hugging Face)&lt;/strong&gt; — nơi robot học kỹ năng trực tiếp từ dữ liệu trình diễn của con người.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-hệ-sinh-thái-lerobot&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Hệ sinh thái LeRobot&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chuẩn Dữ liệu Tập trình diễn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LeRobotDataset&lt;/code&gt;)&lt;/strong&gt;:
Lưu trữ đồng bộ từng khung hình camera (video nén MP4/AV1), trạng thái góc khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation.state&lt;/code&gt; và hành động mục tiêu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt; theo đúng khung thời gian (ví dụ 30 Hz) dưới dạng Parquet + Video chuẩn hóa, dễ chia sẻ trên Hugging Face Hub.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đường ống Teleoperation → Train → Eval Thống nhất&lt;/strong&gt;:
Dùng cánh tay chủ–tớ (leader–follower như SO-101) ghi lại 50–100 lượt trình diễn sạch, huấn luyện chính sách học bắt chước (ACT, Diffusion Policy) trên GPU rồi triển khai vòng kín ngay trên cùng giao diện phần mềm.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Không thay thế mà Xây trên nền Kỹ thuật Robot Cổ điển&lt;/strong&gt;:
Hiệu chuẩn góc 0 của servo, đồng bộ tốc độ khung hình camera, giới hạn mô-men an toàn và độ trễ vòng lặp từ Chương 08–10 vẫn quyết định 80% sự thành bại của dữ liệu huấn luyện!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ xác định đúng ba thành phần mà hệ sinh thái LeRobot liên kết trên cùng một giao ước dữ liệu: &lt;strong&gt;dataset&lt;/strong&gt;, &lt;strong&gt;policy&lt;/strong&gt; và &lt;strong&gt;robot&lt;/strong&gt;. Bạn nhận diện được các phần cứng tiêu biểu của dự án như SO-100, Koch v1.1 và LeKiwi (các mẫu cánh tay và đế di động chi phí thấp) và giải thích vì sao chúng khác với khung xe vi sai Capstone. Bạn sẽ viết một bảng mô tả tập dữ liệu (dataset card) dài một trang cho một tập mẫu giả định gồm camera, vận tốc dài \(v\) và vận tốc góc \(\omega\) với hệ đơn vị thống nhất giữa lúc ghi và lúc phát lại, đồng thời trích nguyên văn dòng mô tả trên README của kho lưu trữ vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; kèm ngày truy cập.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Bạn chỉ cần trình duyệt web và terminal. Xe Capstone đã nhận vận tốc dài theo m/s và vận tốc góc theo rad/s, với watchdog 300 ms trên firmware chương 08 tự ngắt động cơ khi mất lệnh, và topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; ở chương 09 cũng dùng đúng cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;. Bài này không yêu cầu GPU, tay điều khiển leader hay robot thứ hai; nếu mạng chậm có thể bỏ qua lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; vì sản phẩm chính là trích dẫn README và dataset card.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Capstone A vận hành nhờ firmware nhận \((v, \omega)\), còn ROS 2 Jazzy đưa hai đại lượng đó lên topic. Thư viện học máy không thay thế ngăn xếp điều khiển này mà bổ sung thêm lớp dữ liệu và chính sách (policy). Tệp dữ liệu bắt buộc phải lưu đúng các quan sát (observation) mà robot thực tế tạo ra được và đúng định dạng hành động (action) mà firmware chấp nhận. Nếu khâu ghi lưu giá trị PWM nhưng khâu huấn luyện lại hiểu là radian hay m/s, bạn đang huấn luyện cho một robot hoàn toàn khác với robot chạy thực tế. Các bài 12-02 và 12-03 sẽ đi sâu vào cách policy sinh hành động dựa trên các khóa dữ liệu đã chốt ở đây, còn bài 12-04 mở rộng cùng nguyên tắc đó sang các mô hình lớn đã công bố.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;LeRobot&lt;/a&gt; là một thư viện Python, một chuẩn định dạng tập dữ liệu và tập hợp các cấu hình phần cứng do Hugging Face phát triển. Trang tài liệu chính mô tả quy trình gồm bốn bước: &lt;strong&gt;teleoperate&lt;/strong&gt; (con người điều khiển từ xa), &lt;strong&gt;record&lt;/strong&gt; (ghi lại các phiên mẫu gồm video camera đồng bộ với lệnh điều khiển), &lt;strong&gt;train&lt;/strong&gt; (huấn luyện một policy/mạng nơ-ron bắt chước các hành động đó) và &lt;strong&gt;deploy&lt;/strong&gt; (đưa policy xuất lệnh điều khiển trực tiếp robot). Trong phạm vi bài này chúng ta dừng trước bước &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt;; bốn động từ trên dùng để định vị tài liệu chứ chưa chạy huấn luyện.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dataset&lt;/strong&gt; đồng bộ hóa hình ảnh, trạng thái (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt;) và hành động (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt;) theo cùng một trục thời gian trong cấu trúc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LeRobotDataset&lt;/code&gt;. Dữ liệu cánh tay robot thường chứa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation.images.&amp;lt;camera&amp;gt;&lt;/code&gt;, góc khớp hiện tại và góc khớp đích; trong khi dữ liệu xe vi sai chứa ảnh camera và hành động hai thành phần \((v, \omega)\). Định dạng này hỗ trợ cả hai nhưng sẽ không tự cảnh báo nếu bạn trộn nhầm đơn vị hoặc cấu trúc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Policy&lt;/strong&gt; nhận đầu vào là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation&lt;/code&gt; và xuất ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt; (ví dụ ACT ở bài 12-02). Trọng số của mạng luôn gắn chặt với số chiều và ý nghĩa vật lý của từng phần tử: đúng số chiều nhưng lệch đơn vị vẫn dẫn đến điều khiển sai.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Robot&lt;/strong&gt; trong LeRobot bao gồm phần cứng kèm tệp cấu hình (động cơ, camera và các khóa dữ liệu tương ứng). Trên tài liệu chính thức, SO-101 là mẫu cánh tay chi phí thấp chủ lực (kế nhiệm SO-100), Koch v1.1 là cánh tay cộng đồng được hỗ trợ, còn LeKiwi là đế di động có gắn thêm cánh tay — đó là các hình thái phần cứng (&lt;em&gt;embodiment&lt;/em&gt;) của dự án. Trong khi đó, xe Capstone chỉ có hai bánh dẫn động, không có tay kẹp, nhận lệnh \((v, \omega)\) và có watchdog 300 ms. Vector góc khớp của SO-101 không tương thích &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, và ngay cả LeKiwi cũng chứa thêm chiều hành động của cánh tay nên không thể dùng chung khóa trực tiếp với Capstone.&lt;/p&gt;

&lt;p&gt;Nguyên tắc cốt lõi là sự đồng nhất giữa lúc ghi và lúc phát: lúc ghi, người lái điều khiển và logger lưu lại; lúc phát, policy xuất lệnh và firmware thực thi — cả hai phải dùng chính xác cùng tên khóa và cùng đơn vị. Chỉ cần camera gắn thấp hơn 15 cm hoặc đảo dấu góc yaw, cột dữ liệu vẫn mang tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt; nhưng chuyển động thực tế đã đổi ngược. Ngoài ra, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps: 10&lt;/code&gt; đồng nghĩa khoảng cách giữa các lệnh là 0,1 s; nếu camera chạy 30 Hz mà không đồng bộ dấu thời gian, ảnh sẽ bị ghép lệch với lệnh. Đặc biệt, phải giữ nguyên watchdog 300 ms trong lúc ghi dữ liệu để phản ánh đúng đặc tính an toàn của robot thật (nếu đường truyền bị gián đoạn khiến log xuất hiện các đoạn \((0, 0)\), hãy đánh dấu lọc frame chứ không tắt watchdog).&lt;/p&gt;

&lt;h2 id=&quot;hình&quot;&gt;Hình&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_lerobot.png&quot; alt=&quot;Dataset, policy và robot dùng chung khóa&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Sơ đồ gồm ba khối và hai chiều luồng dữ liệu: khối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dataset&lt;/code&gt; lưu trữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt;; mũi tên đi qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;policy&lt;/code&gt; biểu thị giai đoạn phát lại (đọc quan sát, sinh hành động gửi xuống robot); mũi tên xanh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;record&lt;/code&gt; đi ngược lại biểu thị giai đoạn ghi mẫu (người điều khiển robot, logger lưu đúng các khóa tương ứng). Ghi chú đỏ bên dưới nhấn mạnh yêu cầu đồng nhất khóa và đơn vị trên dataset card trước khi nghĩ đến bước huấn luyện.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đọc-có-số&quot;&gt;Ví dụ đọc có số&lt;/h2&gt;

&lt;p&gt;Dưới đây là một dataset card mẫu mô tả một tập dữ liệu (episode) giả định dài 2 giây ở tần số 10 Hz (tổng cộng 20 khung dữ liệu) cho xe Capstone, đặt ở đầu thư mục dữ liệu để mọi script phía sau đọc thống nhất:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;robot_name: capstone_diff
embodiment: two-wheel diff-drive, no arm
fps: 10
episode_length_s: 2.0
features:
  observation.images.cam:
    dtype: video
    shape: [480, 640, 3]
    names: [height, width, channel]
    info: RGB, forward camera, 10 Hz, same clock as action
  observation.state:
    dtype: float32
    shape: [2]
    names: [v_meas_m_s, w_meas_rad_s]
  action:
    dtype: float32
    shape: [2]
    names: [v_cmd_m_s, w_cmd_rad_s]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Xét từng thời điểm cụ thể: ở dòng 0 (hành lang trống), trạng thái đo \((0.00, 0.00)\) và hành động \((0.20, 0.00)\) — xe tiến thẳng với vận tốc 0,20 m/s (đi được khoảng 2 cm trong một chu kỳ 0,1 s). Ở dòng 8 (ghế xuất hiện phía bên trái khung hình), hành động là \((0.10, -0.40)\) — giảm tốc và quay phải với yaw \(-0.40\) rad/s (theo quy tắc bàn tay phải với trục \(z\) hướng lên, yaw âm là rẽ phải). Đến dòng 19, hành động về \((0.00, 0.00)\) do người lái chủ động dừng.&lt;/p&gt;

&lt;p&gt;Nếu hệ đơn vị bị lệch (ví dụ script teleop ghi giá trị kiểu PWM chương 07 trong \([-100, 100]\) nhưng dataset card lại khai báo m/s và rad/s), bộ nạp dữ liệu sẽ hiểu nhầm mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;70&lt;/code&gt; là 70 m/s mà mạng nơ-ron không hề báo lỗi. Để loại bỏ rủi ro này, cả logger và cầu nối phát lại phải tuân thủ duy nhất một khai báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;names&lt;/code&gt; và đơn vị.&lt;/p&gt;

&lt;p&gt;Kiểm tra tính nhất quán số lượng khung hình:&lt;/p&gt;

\[N = \text{fps} \times T = 10 \times 2.0 = 20\]

&lt;p&gt;Nếu thư mục chỉ có 20 ảnh nhưng card lại ghi nhầm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps: 30&lt;/code&gt;, thời lượng thực tế sẽ bị bóp méo và các thao tác quay sẽ bị kéo giãn sai tốc độ.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-kích-thước-bộ-dữ-liệu-50-lượt-trình-diễn-episodes-trên-lerobot&quot;&gt;Giải thích: Tính tay kích thước bộ dữ liệu 50 lượt trình diễn (episodes) trên LeRobot&lt;/h3&gt;

&lt;p&gt;Giả sử bạn thu thập &lt;strong&gt;50 lượt trình diễn&lt;/strong&gt; gắp một khối lập phương bằng cánh tay &lt;strong&gt;6 khớp (SO-101)&lt;/strong&gt; gắn 2 camera (1 camera cổ tay + 1 camera toàn cảnh), mỗi lượt dài &lt;strong&gt;15 giây&lt;/strong&gt; ở tần số &lt;strong&gt;30 Hz&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Tổng số bước thời gian (frames) trong toàn bộ dataset:
\(N_{\text{frames}} = 50\text{ lượt} \times 15\text{ s} \times 30\text{ Hz} = \mathbf{22.500\text{ bước}}.\)&lt;/li&gt;
  &lt;li&gt;Kích thước bảng trạng thái &amp;amp; hành động (Parquet: 6 float32 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; + 6 float32 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt; = 48 byte/bước):
\(22.500 \times 48\text{ B} \approx \mathbf{1{,}08\text{ MB}}\) (siêu nhẹ!).&lt;/li&gt;
  &lt;li&gt;Kích thước 2 luồng video nén H.264/AV1 (~1,5 Mbps mỗi camera, tổng 750 giây = 12,5 phút):
\(2 \times \frac{1{,}5\text{ Mb/s}}{8} \times 750\text{ s} \approx \mathbf{281\text{ MB}}.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Toàn bộ bộ dữ liệu huấn luyện một kỹ năng thao tác khéo léo chỉ nặng dưới &lt;strong&gt;300 MB&lt;/strong&gt; — hoàn toàn tải được trong vài giây và huấn luyện xong trong 30–60 phút trên 1 GPU tầm trung!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Bài thực hành này thực hiện hoàn toàn trên máy tính (không cần bật xe). Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; cần nộp hai nội dung: một câu trích dẫn có ngày và một dataset card tự viết.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Khảo sát cấu trúc tài liệu.&lt;/strong&gt; Mở &lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;trang index LeRobot&lt;/a&gt;, đọc mục “How it works” và chép lại đúng thứ tự bốn bước: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;teleoperate&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;record&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deploy&lt;/code&gt;, kèm ghi chú rằng bài học này dừng trước bước &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt;. Tiếp theo, ghi lại tên các tiêu đề (heading) thực tế bạn thấy trên trang hiện tại cho các mục cài đặt, dataset, imitation learning và robots, kèm ngày truy cập (nếu trang tài liệu đã đổi tên tiêu đề, ghi đúng tên thực tế):&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;date: 2026-09-23
docs: https://huggingface.co/docs/lerobot/index
verbs: Teleoperate, Record, Train, Deploy
pages I opened:
  - Installation (có lệnh; tôi không cần chạy)
  - (heading dataset tôi thực sự thấy)
  - (heading imitation tôi thực sự thấy)
  - Robots: trang index có tên SO-101, LeKiwi, Koch v1.1
stopped before: any train command
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;2. Trích dẫn mô tả kho mã nguồn.&lt;/strong&gt; Mở &lt;a href=&quot;https://github.com/huggingface/lerobot&quot;&gt;huggingface/lerobot&lt;/a&gt;, chép nguyên văn dòng mô tả ngắn ngay dưới tên kho lưu trữ đặt trong dấu ngoặc kép kèm ngày truy cập (không diễn giải lại).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Viết dataset card.&lt;/strong&gt; Viết lại dataset card cho riêng bạn bằng cách thay đổi ít nhất một thông số nhưng vẫn đảm bảo phép nhân nhất quán (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps: 5&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;episode_length_s: 3.0&lt;/code&gt;, tương ứng 15 dòng). Giữ nguyên khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation.images.cam&lt;/code&gt;, đơn vị m/s cho \(v\) và rad/s cho \(\omega\); bổ sung một dòng dữ liệu mẫu kèm câu giải thích rằng cặp giá trị đó tương ứng trực tiếp với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; trên cầu nối Capstone.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Tùy chọn)&lt;/em&gt; Nếu muốn kiểm tra phiên bản gói trong terminal mà không cài đặt hay huấn luyện:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip index versions lerobot
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; lệnh trên trả về danh sách phiên bản (hoặc thông báo lỗi mạng/phiên bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; cũ đều được chấp nhận) — dừng ngay tại đây, không chạy bất kỳ lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; nào. Phần bài viết phải đảm bảo thống nhất đơn vị m/s và rad/s cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt;, câu trích README đặt trong ngoặc kép, ghi rõ ngày xem tài liệu và không chứa log huấn luyện hay checkpoint.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Sai sót&lt;/th&gt;
      &lt;th&gt;Hệ quả&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Logger lưu PWM nhưng tài liệu policy khai báo m/s và rad/s&lt;/td&gt;
      &lt;td&gt;Lúc phát lại, policy sinh lệnh cho một hình thái robot khác hẳn lúc ghi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tích &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps&lt;/code&gt; và thời lượng không khớp số khung hình&lt;/td&gt;
      &lt;td&gt;Sai lệch tốc độ thời gian, khiến chuyển động rẽ nhẹ thành giật cục hoặc ngược lại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tắt watchdog 300 ms trong lúc ghi dữ liệu teleop&lt;/td&gt;
      &lt;td&gt;Dữ liệu ghi không phản ánh đúng hành vi an toàn của firmware thật khi mất kết nối&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đường truyền rớt gói tạo nhiều đoạn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0, 0)&lt;/code&gt; đột ngột trong log&lt;/td&gt;
      &lt;td&gt;Policy sẽ học theo các lần khựng bất thường; cần sửa đường truyền hoặc lọc frame&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chạy lệnh huấn luyện vì thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; là bước thứ ba trong docs&lt;/td&gt;
      &lt;td&gt;Nằm ngoài phạm vi bài lab; sản phẩm nộp chỉ gồm dataset card và trích dẫn&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;phần-cứng-tùy-chọn-không-yêu-cầu-trong-tuần-này&quot;&gt;Phần cứng tùy chọn (không yêu cầu trong tuần này)&lt;/h2&gt;

&lt;p&gt;Bài học này không yêu cầu mua thêm thiết bị. Các bộ cánh tay hạng SO-100 là kit nhập khẩu (khung in 3D, servo, tay leader, camera), tổng chi phí về Việt Nam thường ở mức nhiều triệu đồng (cần tự kiểm tra giá mới nhất nếu sau này có nhu cầu) và hoàn toàn không bắt buộc trong khóa học. Khung xe bạn đang thiết kế dataset card cho chính là chiếc Capstone đã lắp ráp.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=GDmhrAHxgQE&quot;&gt;Core Concepts: Imitation Learning&lt;/a&gt; — Sanjiban Choudhury (tiếng Anh). Giới thiệu nền tảng học bắt chước (ghi hành động mẫu của người rồi huấn luyện policy tái tạo lại), phân biệt với học tăng cường (reinforcement learning) ở các video ngắn của &lt;a href=&quot;https://chinbi.com&quot;&gt;Chinbi&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7tGT8hqRSAo&quot;&gt;How to Train an AI Robotic Arm With No Code (Hugging Face LeRobot + SO-101)&lt;/a&gt; — Brogan M. Pratt (tiếng Anh). Minh họa hệ sinh thái LeRobot với tay SO-101, giúp phân biệt rõ ba thành phần dataset, policy và robot.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Trên dataset card bạn vừa viết, hai đại lượng nào tạo thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt; và đơn vị của từng đại lượng là gì? &lt;em&gt;Gợi ý:&lt;/em&gt; \(v\) (m/s) và \(\omega\) (rad/s), khớp với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; (không dùng PWM).&lt;/li&gt;
  &lt;li&gt;Hành động của SO-101 là vector góc khớp đích, trong khi cầu nối Capstone nhận hai đại lượng vô hướng \((v, \omega)\). Vì sao không thể cắt lấy hai phần tử đầu của vector SO-101 để gán làm \(v\) và \(\omega\)? &lt;em&gt;Gợi ý:&lt;/em&gt; hai phần tử đó biểu thị góc khớp của một cánh tay cơ khí khác hoàn toàn về hình thái và đơn vị; cắt bớt chiều vector không tạo ra động học xe vi sai.&lt;/li&gt;
  &lt;li&gt;Bạn chọn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps: 5&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;episode_length_s: 3.0&lt;/code&gt; thì có bao nhiêu dòng dữ liệu? Nếu một người bạn có 15 ảnh ghi ở 5 Hz nhưng lại khai báo nhầm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps: 10&lt;/code&gt;, card đó biểu thị thời lượng bao lâu và sai lệch gì xảy ra? &lt;em&gt;Gợi ý:&lt;/em&gt; \(5 \times 3.0 = 15\) dòng; khai báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps: 10&lt;/code&gt; khiến thời lượng trên giấy chỉ còn \(15/10 = 1.5\) s (nén đôi thời gian nên vận tốc góc trông nhanh gấp đôi).&lt;/li&gt;
  &lt;li&gt;Đường truyền serial bị mất 0,5 s trong lúc ghi mẫu khi watchdog 300 ms trên firmware vẫn bật. Log sẽ ghi nhận hành động nào trong khoảng gián đoạn đó, và điều gì xảy ra nếu bạn đã tắt watchdog? &lt;em&gt;Gợi ý:&lt;/em&gt; khi còn watchdog, lệnh về 0 sau khoảng 300 ms; nếu tắt watchdog, lệnh cuối khác 0 bị giữ nguyên suốt khoảng trống — chỉ trường hợp đầu khớp đúng hành vi khi phát lại trên phần cứng thật.&lt;/li&gt;
  &lt;li&gt;Dán câu mô tả README bạn đã trích dẫn và viết thêm một mệnh đề xác nhận phạm vi thực hành tuần này. &lt;em&gt;Gợi ý:&lt;/em&gt; giữ nguyên văn câu trên kho GitHub kèm ghi chú rằng tuần này không chạy huấn luyện.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;thực-hành-thêm-dựng-workcell-pick-and-place-bằng-simready-central&quot;&gt;Thực hành thêm: dựng workcell pick-and-place bằng SimReady Central&lt;/h2&gt;

&lt;p&gt;Khi bạn đã hiểu dataset LeRobot trên giấy và muốn xem một tay máy công nghiệp chạy gắp–đặt trong mô phỏng, hãy dùng &lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt;: tìm một tay UR hoặc Franka và gripper Robotiq đã có khớp, giới hạn và drive, rồi làm theo bài &lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-assets-robot-pick-and-place&quot;&gt;Build a Robot Pick-and-Place Simulation with SimReady Assets&lt;/a&gt; (UR17G + Robotiq 2F-140 + lon nước) trong Isaac Sim. Việc này &lt;strong&gt;không&lt;/strong&gt; thay lab SO-101, cũng không phải bài tập tuần này.&lt;/p&gt;

&lt;p&gt;Isaac Sim đòi sàn phần cứng riêng (RTX 4080, 16 GB VRAM, 32 GB RAM). Không đủ máy thì đọc asset và license, hoặc xem &lt;a href=&quot;/contents/vi/chapter12/12_08_home_simulation_lab/&quot;&gt;phòng lab mô phỏng tại nhà&lt;/a&gt;. Luôn kiểm license từng gói, không giả định mọi asset đều là CC-BY-4.0.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;Tài liệu LeRobot&lt;/a&gt; — xem mục lục và các trang khái niệm chính (bước &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; chỉ đọc để nắm bức tranh tổng thể).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/huggingface/lerobot&quot;&gt;huggingface/lerobot trên GitHub&lt;/a&gt; — nguồn trích dẫn dòng mô tả ngắn cho bài lab.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 11</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_08_ch11_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_08_ch11_lab_checklist</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~50 phút&lt;/strong&gt;. Sau hai bài lab Capstone B, mục kiểm tra này yêu cầu minh chứng thực tế cho các kết quả đã chạy, không lặp lại định nghĩa lý thuyết SLAM.&lt;/p&gt;

&lt;p&gt;Chương 11 là cột mốc lớn đánh dấu chiếc robot của bạn đã chính thức đạt năng lực &lt;strong&gt;tự hành hoàn chỉnh&lt;/strong&gt;: tự lập bản đồ bằng SLAM, tự định vị trên bản đồ bằng AMCL và tự hoạch định lộ trình tránh vật cản bằng Nav2. Danh sách kiểm tra này tổng hợp các tiêu chí định lượng để nghiệm thu Capstone B trước khi bước sang kỷ nguyên học robot hiện đại ở Chương 12.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-tiêu-chuẩn-nghiệm-thu-hệ-thống-tự-hành-chương-11&quot;&gt;Kiến thức cần nhớ: Ba tiêu chuẩn nghiệm thu Hệ thống Tự hành (Chương 11)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chất lượng Bản đồ SLAM&lt;/strong&gt;:
Bản đồ lưu ra có tường sắc nét, không bị nhân đôi (double walls), đóng vòng lặp chuẩn và sai số đo kích thước dưới 3–5%.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Độ tin cậy của Định vị AMCL &amp;amp; Điều hướng Nav2&lt;/strong&gt;:
Đám mây hạt hội tụ nhanh sau khi di chuyển ngắn; robot hoàn thành chuỗi điểm đến (multi-waypoint) mà không đâm vật cản tĩnh hay vật cản mới đặt thêm giữa đường.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khả năng Chẩn đoán &amp;amp; Tái lập bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rosbag&lt;/code&gt;&lt;/strong&gt;:
Ghi lại nhật ký chạy tự hành (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/plan&lt;/code&gt;) để chứng minh kết quả khách quan và phân tích mọi tình huống kích hoạt recovery.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Bạn cần xác nhận đủ các hạng mục: câu tóm tắt chuỗi TF từ phần khái niệm, ảnh bản đồ hoặc log &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt; từ lab SLAM, một lượt gửi goal sinh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; từ lab Nav2, và sự tách bạch rõ ràng giữa phần mô phỏng với phần xe thật. Việc chưa có lidar thật không làm trượt phần mô phỏng, nhưng bỏ trống không ghi chú kế hoạch hoãn cho phần xe thật thì chưa đạt.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành các bài từ 11-01 đến 11-07. Đảm bảo odometry chương 08 đã đúng dấu nếu bạn triển khai trên xe thật, và chuẩn bị lidar LD06 hoặc RPLIDAR A1 khi chạy trên sàn.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Chương 12 sẽ chuyển sang các chính sách học máy (policy), nhưng policy không tự dựng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt; thay cho tầng điều hướng. Checklist này giúp phân biệt rạch ròi giữa việc chạy thành công mô phỏng TurtleBot3 với việc xe tự chế đã sẵn sàng tự hành ngoài thực tế.&lt;/p&gt;

&lt;h2 id=&quot;tiêu-chí-đạt-và-không-đạt&quot;&gt;Tiêu chí đạt và không đạt&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Khái niệm.&lt;/strong&gt; Một câu mô tả đúng chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom → base_link&lt;/code&gt; và một câu giải thích vì sao cảm biến ToF đơn điểm không thay thế được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SLAM mô phỏng.&lt;/strong&gt; Ảnh chụp hoặc mô tả lưới bản đồ khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam:=True&lt;/code&gt; với mô phỏng TurtleBot3 của Nav2 Jazzy (hoặc log lỗi launch nguyên văn nếu máy không đủ tài nguyên). Ghi chung chung “không chạy vì máy yếu” mà không kèm lệnh và log là chưa đạt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SLAM thực tế.&lt;/strong&gt; Ảnh bản đồ phòng thực tế, hoặc ghi chú hoãn có ngày cụ thể và lý do (ví dụ chưa có lidar). Nếu đã chạy trên xe thật mà bản đồ bị cong nhưng không ghi nhận nguyên nhân dấu encoder thì chưa đạt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nav2.&lt;/strong&gt; Một mục tiêu gửi trong mô phỏng tạo ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; khác 0 rồi dừng khi hủy goal hoặc gửi lệnh 0. Trên sàn thực tế: thực hiện tương tự với bước nhấc bánh ban đầu trước khi hạ xuống sàn (nếu chưa chạy sàn thì ghi chú hoãn).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An toàn.&lt;/strong&gt; Ghi nhận rõ có người túc trực ở công tắc nguồn pin trong lần đầu hạ bánh chạy tự hành; nếu đã hạ bánh mà thiếu xác nhận này thì tiêu chí Nav2 thực tế chưa đạt.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-phiếu-chấm-điểm-định-lượng-cho-3-vòng-chạy-tự-hành-capstone-b&quot;&gt;Giải thích: Phiếu chấm điểm định lượng cho 3 vòng chạy tự hành Capstone B&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Chỉ tiêu kiểm chứng&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Ngưỡng ĐẠT&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cách đo trực tiếp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Sai số vị trí khi dừng tại mục tiêu&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(\le 10\text{ cm}\) và góc \(\le 10^\circ\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Đối chiếu tâm xe thực/mô phỏng với mũi tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Goal Pose&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Số lần va chạm vật cản tĩnh &amp;amp; động&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;0 lần&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Đặt thêm 1 thùng các-tông giữa đường sau khi đã lưu bản đồ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Độ ổn định tỉ lệ thành công&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;3/3 lần&lt;/strong&gt; liên tiếp đi qua 2 điểm đích&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Chạy liên tục không chỉnh sửa tham số giữa các lượt&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Sao chép mẫu sau vào sổ thực hành và điền đầy đủ kết quả:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Ky duyet chuong 11
TF ba canh:
SLAM sim (lenh + ket qua):
SLAM that hoac giay hoan:
Nav2 goal sim:
Nav2 that hoac giay hoan:
Cong tat pin khi ha banh: co / chua ha
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Costmap trống dù Gazebo vẫn hiển thị tia laser&lt;/td&gt;
      &lt;td&gt;Kiểm tra QoS của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; hoặc lệch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bản đồ bị xoay cong khi xe chạy thẳng&lt;/td&gt;
      &lt;td&gt;Kiểm tra dấu encoder hoặc thứ tự trái/phải&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hoàn thành goal nhưng bánh xe thật không dừng&lt;/td&gt;
      &lt;td&gt;Còn publisher khác phát lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; hoặc watchdog firmware chưa hoạt động&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=jkoGkAd0GYk&quot;&gt;Making robot navigation easy with Nav2 and ROS!&lt;/a&gt; — Articulated Robotics (tiếng Anh). Minh họa trực quan tuyến đường điều hướng trong mô phỏng cần đạt trước khi xác nhận checklist.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nếu bạn chỉ có ảnh chạy mô phỏng TurtleBot3 và một ghi chú hoãn lidar có ngày cụ thể, những tiêu chí nào được tính là đạt?&lt;/li&gt;
  &lt;li&gt;Vì sao ảnh chụp RViz không thể thay thế cho xác nhận “có người đứng ở công tắc pin”?&lt;/li&gt;
  &lt;li&gt;Giữa hai bản đồ thu được (một bản bị cong, một bản thẳng), bạn xác nhận bản nào và bước khắc phục bản bị cong nằm ở chương nào?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Phần mô phỏng đạt và phần thực tế đạt theo diện hoãn hợp lệ (với điều kiện ghi rõ ngày và lý do).&lt;/li&gt;
  &lt;li&gt;RViz chỉ là giao diện hiển thị, không thể ngắt nguồn điện động cơ khi có sự cố; công tắc pin là lớp bảo vệ phần cứng bắt buộc.&lt;/li&gt;
  &lt;li&gt;Xác nhận bản đồ thẳng sau khi đã sửa lỗi dấu; phần khắc phục nằm ở odometry chương 08, không phải ở tham số SLAM.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/tutorials/general_tutorials/navigation2_with_slam/navigation2_with_slam/&quot;&gt;Nav2 với SLAM, tài liệu Jazzy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/SteveMacenski/slam_toolbox&quot;&gt;slam_toolbox&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Capstone B: lab Nav2, sim rồi xe thật</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_07_capstone_b_nav2/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_07_capstone_b_nav2</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~120 phút&lt;/strong&gt; cho phần mô phỏng, thêm một buổi trên sàn nếu SLAM thật đã dựng được bản đồ không bị cong.&lt;/p&gt;

&lt;p&gt;Tấm bản đồ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab_map.yaml&lt;/code&gt; bạn vừa lưu giờ trở thành trí nhớ dài hạn cho robot. Trong bài Capstone B phần 2 này, bạn nạp bản đồ vào &lt;strong&gt;Nav2&lt;/strong&gt;, cung cấp tư thế khởi đầu cho &lt;strong&gt;AMCL&lt;/strong&gt;, rồi chỉ bằng một cú nhấp chuột đặt mũi tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2D Goal Pose&lt;/code&gt; trên RViz2 — chiếc xe tự tính đường đi, né vật cản phát sinh và tiến gọn vào vị trí đích.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-triển-khai-điều-hướng-tự-hành-nav2-capstone-b&quot;&gt;Kiến thức cần nhớ: Ba trụ cột Triển khai Điều hướng Tự hành Nav2 (Capstone B)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tải Bản đồ &amp;amp; Khởi tạo AMCL (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map_server&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;amcl&lt;/code&gt;)&lt;/strong&gt;:
Đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2D Pose Estimate&lt;/code&gt; gần đúng vị trí xe, sau đó lái nhích nhẹ để đám mây hạt AMCL co cụm chặt quanh đúng vị trí và hướng mũi xe trên bản đồ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tinh chỉnh Bộ tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav2_params.yaml&lt;/code&gt; cho đúng Khung xe Capstone&lt;/strong&gt;:
Cập nhật chính xác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_radius&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;footprint&lt;/code&gt;, giới hạn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_vel_x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_vel_theta&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;acc_lim_x&lt;/code&gt;, và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inflation_radius&lt;/code&gt; theo đúng đặc tính cơ khí xe thật (không để nguyên tham số mặc định của TurtleBot3 nếu xe bạn kích thước khác!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Giám sát An toàn &amp;amp; Đánh giá Chỉ số Chạy tự hành&lt;/strong&gt;:
Theo dõi liên tục Local Costmap trên RViz2, giữ tay sẵn sàng trên nút dừng khẩn cấp / teleop ghi đè, và đo tỷ lệ hoàn thành mục tiêu, độ lệch vị trí cuối và số lần kích hoạt recovery.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ gửi được một mục tiêu điều hướng (goal) trong mô phỏng đang bật SLAM, quan sát &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; chuyển sang giá trị khác 0 và dừng hẳn khi hủy goal. Trên xe thật, bạn thực hiện tương tự với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigation_launch.py&lt;/code&gt; trong khi SLAM giữ cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt; (nhấc bánh khỏi mặt sàn ở lần thử đầu tiên) và lưu bản đồ bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map_saver_cli&lt;/code&gt;. Trọng tâm là kiểm chứng toàn chuỗi hoạt động thay vì sa đà tinh chỉnh tham số MPPI khi lần gửi goal đầu gặp trục trặc.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành lab SLAM (11-06) với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt; trong mô phỏng (hoặc log lỗi launch trên cùng máy đó) và bài tour Nav2 (11-02) để nắm rõ planner, controller cùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;. Firmware từ chương 07–08 vẫn phải đảm bảo tự đưa PWM về 0 khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; ngừng cập nhật.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Bản thân SLAM chỉ dựng bản đồ chứ không điều khiển xe di chuyển; Nav2 mới là tầng chuyển đổi một vị trí đích trên bản đồ thành lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt;. Xe Capstone chỉ thực thi đúng lệnh đó nếu cầu nối serial giữ đúng quy ước dấu. Bài lab này là lần đầu tiên goal điều hướng và cầu nối phần cứng phối hợp trực tiếp với nhau, nên bắt buộc nhấc bánh xe khỏi mặt sàn trong lần chạy thử đầu tiên.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Khi tách khỏi tệp launch gộp sẵn của TurtleBot3, hướng dẫn Nav2 trên Jazzy chia hệ thống thành ba phần riêng biệt: giao diện phần cứng robot, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigation_launch.py&lt;/code&gt; (không chạy kèm AMCL và map server vì SLAM đang cung cấp bản đồ) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;online_async_launch.py&lt;/code&gt; của SLAM Toolbox. Trong mô phỏng, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tb3_simulation_launch.py slam:=True&lt;/code&gt; đã gộp sẵn cả ba; còn trên xe thật bạn cần gọi riêng để tránh tình trạng AMCL và SLAM cùng tranh chấp cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Ngoài nút chọn trên RViz, bạn có thể gửi trực tiếp mục tiêu qua dòng lệnh:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub /goal_pose geometry_msgs/msg/PoseStamped &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;{header: {stamp: {sec: 0}, frame_id: &apos;map&apos;}, pose: {position: {x: 0.2, y: 0.0, z: 0.0}, orientation: {w: 1.0}}}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trong đó &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orientation: {w: 1.0}&lt;/code&gt; biểu thị góc quay bằng 0, và $x = 0{,}2$ m là mục tiêu ngắn phù hợp cho lần thử đầu. Tùy cấu hình thời gian, dấu thời gian &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sec: 0&lt;/code&gt; có thể bị từ chối; khi đó hãy gửi lại bằng nút &lt;strong&gt;Nav2 Goal&lt;/strong&gt; trên RViz sau khi kiểm tra đúng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; trong mô phỏng, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt; trên xe thật).&lt;/p&gt;

&lt;p&gt;Theo dõi lệnh vận tốc trong lúc robot đang hướng về đích:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /cmd_vel
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bạn cần thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; khác 0 khi xe di chuyển và trở về gần 0 khi tới đích hoặc bị hủy. Trên xe thật, chỉ được có duy nhất một publisher điều khiển &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; đi vào cầu nối — nếu quên tắt node teleop bàn phím, cả teleop và Nav2 sẽ cùng tranh quyền lái.&lt;/p&gt;

&lt;p&gt;Lưu bản đồ bằng lệnh tiêu chuẩn:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run nav2_map_server map_saver_cli &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; ~/map
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.yaml&lt;/code&gt; sinh ra sẽ trỏ tới ảnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.pgm&lt;/code&gt; tương ứng; trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resolution&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.yaml&lt;/code&gt; biểu thị kích thước mỗi ô lưới tính bằng mét (không tùy ý sửa giá trị này nếu chưa đo đạc đối chiếu thực tế).&lt;/p&gt;

&lt;p&gt;Lưu ý rằng footprint mặc định của Nav2 không khớp chính xác kích thước khung xe Capstone, nên các mục tiêu sát cửa hẹp có thể bị từ chối. Chỉ nên tinh chỉnh tệp tham số sau khi đã chạy thành công một mục tiêu cơ bản ở khu vực trống.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-nhanh-khoảng-cách-phanh-tối-thiểu-để-đặt-ngưỡng-an-toàn-trong-nav2_paramsyaml&quot;&gt;Giải thích: Tính nhanh khoảng cách phanh tối thiểu để đặt ngưỡng an toàn trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav2_params.yaml&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Giả sử trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav2_params.yaml&lt;/code&gt;, bạn đặt vận tốc tối đa \(v_{\max} = 0{,}40\text{ m/s}\) và gia tốc hãm tối đa \(a_{\text{decel}} = 0{,}50\text{ m/s}^2\), với độ trễ vòng điều khiển tổng \(t_{\text{trễ}} = 0{,}10\text{ s}\):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Quãng đường xe đi thêm trong lúc chờ phản ứng trễ: \(s_{\text{trễ}} = v_{\max} \cdot t_{\text{trễ}} = 0{,}40 \times 0{,}10 = \mathbf{0{,}04\text{ m}}.\)&lt;/li&gt;
  &lt;li&gt;Quãng đường phanh vật lý từ \(0{,}40\text{ m/s}\) về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;:
\(s_{\text{phanh}} = \frac{v_{\max}^2}{2 a_{\text{decel}}} = \frac{0{,}40^2}{2 \times 0{,}50} = \frac{0{,}16}{1{,}00} = \mathbf{0{,}16\text{ m}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tổng khoảng cách dừng an toàn:&lt;/strong&gt; \(s_{\text{dừng}} = 0{,}04 + 0{,}16 = \mathbf{0{,}20\text{ m}}\) (20 cm).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suy ra vùng đệm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inflation_radius&lt;/code&gt; và khoảng nhìn trước của Local Costmap phải rộng hơn \(R_{\text{robot}} + 0{,}20\text{ m}\) để khi một sinh viên bất ngờ bước ngang qua, robot có đủ quãng đường phanh êm mà không va chạm!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Luôn kiểm thử trong mô phỏng trước. Khi chuyển sang xe thật, giữ bánh nhấc khỏi mặt sàn cho đến khi xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; về 0 lúc hủy goal; luôn giữ công tắc pin trong tầm tay, đặt giới hạn vận tốc ngay tại cầu nối (không chỉ phụ thuộc Nav2) và không đứng chắn trước mặt phẳng quét của lidar.&lt;/p&gt;

&lt;h3 id=&quot;phần-mô-phỏng&quot;&gt;Phần mô phỏng&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Khởi chạy lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tb3_simulation_launch.py slam:=True&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TURTLEBOT3_MODEL=waffle&lt;/code&gt; như ở bài 11-06.&lt;/li&gt;
  &lt;li&gt;Đặt một mục tiêu cách trước mặt robot khoảng 0,5 m trong RViz (hoặc gửi bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/goal_pose&lt;/code&gt; nếu khoảng trống cho phép).&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /cmd_vel&lt;/code&gt; và ghi lại khoảng mười dòng vào sổ (gồm cả dòng khác 0 khi đang chạy và dòng về 0 sau khi dừng).&lt;/li&gt;
  &lt;li&gt;Hủy goal hoặc đóng cửa sổ và xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; trở về 0; nếu vẫn có giá trị khác 0, dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /cmd_vel -v&lt;/code&gt; để tìm publisher còn sót lại.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;phần-xe-thật&quot;&gt;Phần xe thật&lt;/h3&gt;

&lt;p&gt;Nếu chưa có bản đồ thực tế, ghi chú kế hoạch hoãn có ngày cụ thể rồi dừng lại.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Nhấc bánh khỏi sàn, khởi chạy SLAM trực tuyến như bài 11-06, sau đó mở riêng tầng điều hướng:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 launch nav2_bringup navigation_launch.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tuyệt đối không bật kèm AMCL; nếu tệp launch trên vô tình khởi động thêm map server chiếm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt;, hãy dừng lại kiểm tra cấu hình vì SLAM phải là nguồn duy nhất phát &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Theo dõi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /cmd_vel&lt;/code&gt; khi gửi một goal ngắn: hai bánh (đang nhấc trên không) phải quay đúng chiều theo bài động học (lệnh tiến làm cả hai bánh quay về phía mũi xe).&lt;/li&gt;
  &lt;li&gt;Hủy goal và ghi rõ bánh dừng nhờ Nav2 gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; hay nhờ watchdog trên firmware (hoặc cả hai).&lt;/li&gt;
  &lt;li&gt;Hạ xe xuống khoảng trống, gửi goal ngắn 0,3 m với một người túc trực sẵn ở công tắc nguồn; dừng ngay nếu xe đi sai hướng.&lt;/li&gt;
  &lt;li&gt;Lưu bản đồ bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map_saver_cli -f ~/capstone_nav_map&lt;/code&gt; sau khi chạy chậm một vòng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Sổ thực hành phần mô phỏng có mẫu log &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; thể hiện rõ trạng thái khác 0 khi chạy và về 0 khi dừng. Phần xe thật có kết quả tương ứng hoặc ghi chú hoãn có ngày, kèm tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.yaml&lt;/code&gt; nếu đã lưu bản đồ.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Goal bị bỏ qua&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; không phải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, hoặc cây TF chưa có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Costmap trống&lt;/td&gt;
      &lt;td&gt;Lệch QoS của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; hoặc frame laser chưa nối tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mô phỏng quay đúng nhưng xe thật quay ngược&lt;/td&gt;
      &lt;td&gt;Sai dấu trong cầu nối serial (không phải lỗi planner)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; không trở về 0&lt;/td&gt;
      &lt;td&gt;Còn node teleop đang chạy song song; tắt node đó&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Controller báo lỗi liên quan đến footprint&lt;/td&gt;
      &lt;td&gt;Thường gặp khi đặt goal quá sát tường; chọn goal ở khoảng trống trước khi sửa tham số&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=jkoGkAd0GYk&quot;&gt;Making robot navigation easy with Nav2 and ROS!&lt;/a&gt; — Articulated Robotics (tiếng Anh). Minh họa gửi goal và bám quỹ đạo trong mô phỏng trước khi triển khai xuống xe thật.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=hMTxb8Y2cxI&quot;&gt;ROS2 Nav2 - Generate a Map with slam_toolbox&lt;/a&gt; — Robotics Back-End (tiếng Anh). Ôn lại bước dựng và lưu bản đồ nếu robot không phản hồi khi gửi goal.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Vì sao bài lab cấm chạy AMCL cùng lúc với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;online_async&lt;/code&gt;)?&lt;/li&gt;
  &lt;li&gt;RViz vẽ ra tuyến đường dẫn nhưng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /cmd_vel&lt;/code&gt; toàn số 0. Bạn nên kiểm tra gì tiếp theo?&lt;/li&gt;
  &lt;li&gt;Với watchdog 300 ms và tần số gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; của Nav2 là mỗi 50 ms, watchdog có ngắt động cơ trong lúc xe đang bám goal không?&lt;/li&gt;
  &lt;li&gt;Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.yaml&lt;/code&gt; ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resolution: 0.05&lt;/code&gt;. Một hành lang dài 2 m tương ứng bao nhiêu ô lưới nếu tỉ lệ bản đồ chính xác?&lt;/li&gt;
  &lt;li&gt;Trong lần đầu hạ bánh chạy tự hành trên sàn, vị trí đứng an toàn là ở đâu?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Cả hai đều xuất bản cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt;, gây xung đột vì mỗi cạnh TF chỉ được có một nguồn phát.&lt;/li&gt;
  &lt;li&gt;Kiểm tra tên topic và namespace mà cầu nối hoặc controller đang dùng bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info -v&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Không, miễn là luồng lệnh cập nhật đều đặn mỗi 50 ms — watchdog chỉ kích hoạt khi mất tín hiệu quá 300 ms.&lt;/li&gt;
  &lt;li&gt;$2 / 0{,}05 = 40$ ô lưới theo chiều dài (nếu tỉ lệ odometry chuẩn).&lt;/li&gt;
  &lt;li&gt;Đứng sát công tắc ngắt nguồn pin của xe để kịp thời can thiệp, không ngồi cố định sau laptop.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/tutorials/general_tutorials/navigation2_with_slam/navigation2_with_slam/&quot;&gt;Nav2 với SLAM (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/&quot;&gt;Tài liệu Nav2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;Twist trong Jazzy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter11/11_06_capstone_b_slam/&quot;&gt;Lab SLAM&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Capstone B: lab SLAM, sim rồi lidar thật</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_06_capstone_b_slam/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_06_capstone_b_slam</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~150 phút&lt;/strong&gt; cho phần mô phỏng, cộng thêm một buổi nếu đã có sẵn lidar và xe trên sàn (trong lần kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; đầu tiên trên xe thật, có thể nhấc bánh xe khỏi sàn).&lt;/p&gt;

&lt;p&gt;Mọi mảnh ghép từ Chương 08 đến Bài 11-01 hội tụ tại đây: lần đầu tiên chiếc xe Capstone của bạn tự chạy quanh một không gian chưa biết (trong Gazebo hoặc phòng lab thật), quét LiDAR liên tục và &lt;strong&gt;tự dựng nên tấm bản đồ 2D hoàn chỉnh&lt;/strong&gt; bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt;. Đây là bài kiểm chứng khắt khe nhất cho chất lượng odometry, cây TF2 và cách lái thu thập dữ liệu của bạn.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-thực-chiến-dựng-bản-đồ-slam-capstone-b&quot;&gt;Kiến thức cần nhớ: Ba trụ cột Thực chiến Dựng bản đồ SLAM (Capstone B)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chuỗi Dữ liệu Đầu vào Sạch (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf&lt;/code&gt;)&lt;/strong&gt;:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; yêu cầu tia &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; gắn đúng khung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_frame&lt;/code&gt; đã nối vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, odometry không bị ngược dấu \(\omega\) và đồng bộ thời gian chuẩn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chiến lược Lái tạo Đóng vòng lặp (Loop-Closure Driving Pattern)&lt;/strong&gt;:
Lái chậm và đều (\(v \le 0{,}25\text{ m/s}\), \(\omega \le 0{,}5\text{ rad/s}\)), tránh xoay giật cục tại chỗ, và luôn chủ động lái quay lại ít nhất một đoạn hành lang cũ để kích hoạt tối ưu hóa đồ thị tư thế.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lưu trữ &amp;amp; Kiểm định Bản đồ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map_saver_cli&lt;/code&gt;)&lt;/strong&gt;:
Xuất cặp tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map.pgm&lt;/code&gt; (ảnh xám lưới chiếm dụng) + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map.yaml&lt;/code&gt; (độ phân giải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resolution&lt;/code&gt;, gốc tọa độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;origin&lt;/code&gt; và ngưỡng chiếm dụng) và đối chiếu kích thước phòng đo trên bản đồ với thước dây thực tế.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ chạy thành công SLAM Toolbox trong mô phỏng TurtleBot3 theo đúng hướng dẫn Nav2 cho Jazzy, rồi áp dụng quy trình tương tự lên xe thật dùng cảm biến LD06 hoặc RPLIDAR A1. Bạn sẽ xác minh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; có kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt;, cây TF đầy đủ cạnh tới frame laser và cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt; do SLAM xuất bản. Nếu chưa có lidar thật, bạn vẫn bắt buộc hoàn thành phần mô phỏng và ghi chú kế hoạch hoãn có ngày cụ thể cho phần cứng.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành bài tổng quan SLAM (11-01) và tour Nav2 (11-02). Chương 10 đã có world Gazebo Harmonic chạy ổn định cùng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; đúng dấu; odometry chương 08 đã kiểm tra không đảo chiều bánh; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Capstone A chứng minh xe di chuyển chính xác theo lệnh vận tốc, còn Capstone B chứng minh xe biết định vị bản thân trong không gian để các mục tiêu điều hướng có ý nghĩa. Các mô hình học máy ở chương 12 không thay thế được bản đồ này. Việc chạy thông suốt phần mô phỏng trước giúp bạn có một chuẩn đối chiếu tin cậy mỗi khi bản đồ thực tế bị méo.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_capstone_b.png&quot; alt=&quot;Ba bước: sim, lidar, Nav2&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; yêu cầu hai đầu vào cốt lõi: dữ liệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt; và odometry liên tục (không nhảy gián đoạn). Nó xuất bản bản đồ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt; cùng phép biến đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt;; robot (hoặc Gazebo) xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;; còn URDF xuất bản phép biến đổi tĩnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → khung laser&lt;/code&gt;. Thiếu bất kỳ cạnh nào, toàn bộ tia quét sẽ dồn về gốc tọa độ.&lt;/p&gt;

&lt;p&gt;Cài đặt các gói cho Jazzy theo &lt;a href=&quot;https://docs.nav2.org/jazzy/tutorials/general_tutorials/navigation2_with_slam/navigation2_with_slam/&quot;&gt;tutorial Nav2 với SLAM&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-slam-toolbox ros-jazzy-nav2-bringup &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  ros-jazzy-turtlebot3 ros-jazzy-turtlebot3-msgs ros-jazzy-turtlebot3-bringup
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Chạy mô phỏng với biến môi trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TURTLEBOT3_MODEL=waffle&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;TURTLEBOT3_MODEL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;waffle
ros2 launch nav2_bringup tb3_simulation_launch.py slam:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;True
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Câu lệnh này khởi chạy trọn bộ mô phỏng gồm bringup, Nav2 và SLAM. Nếu gặp lỗi liên quan đến Gazebo, dán nguyên văn dòng lỗi đầu tiên vào sổ — tuyệt đối không cài thêm bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; Classic vì lộ trình chương 10 đã thống nhất dùng Harmonic. Trường hợp phần cứng máy tính không đủ tải world 3D, bạn có thể hoàn thành bước mô phỏng bằng log lỗi gốc kèm kế hoạch chạy lại trên máy khác thay vì bỏ qua.&lt;/p&gt;

&lt;p&gt;Trên robot thật, tuyệt đối không sao chép &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; (hệ thống phải dùng đồng hồ thực). Sau khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; và cây TF đã hoạt động, khởi chạy riêng SLAM Toolbox:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 launch slam_toolbox online_async_launch.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hai dòng lidar phổ thông phù hợp cho bài thực hành (tự tra giá hiện tại khi mua):&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LD06&lt;/strong&gt; (cùng họ LD19 trong kho mã nguồn chính thức của LDRobot). Biên dịch workspace theo hướng dẫn &lt;a href=&quot;https://github.com/ldrobotSensorTeam/ldlidar_stl_ros2&quot;&gt;ldlidar_stl_ros2&lt;/a&gt; rồi chạy:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 launch ldlidar_stl_ros2 ld06.launch.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tệp launch mặc định đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;topic_name&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_laser&lt;/code&gt;, tốc độ baud &lt;strong&gt;230400&lt;/strong&gt;, và một phép biến đổi tĩnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → base_laser&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z = 0.18&lt;/code&gt;. Giá trị 0,18 m chỉ là ví dụ của hãng — bạn phải chỉnh lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arguments&lt;/code&gt; của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static_transform_publisher&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ld06.launch.py&lt;/code&gt; theo kích thước đo thực tế (tính bằng mét) trên xe mình. Ngoài ra, dù README đề cập &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chmod 777&lt;/code&gt; cho cổng serial, khóa học thống nhất dùng nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; và luật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt; như bài Linux chương 09 (chỉ dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chmod&lt;/code&gt; tạm thời trên đúng cổng đã kiểm tra nếu chưa kịp tạo rule và ghi chú rõ trong sổ).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RPLIDAR A1.&lt;/strong&gt; Lấy mã nguồn từ &lt;a href=&quot;https://github.com/Slamtec/rplidar_ros&quot;&gt;Slamtec rplidar_ros nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; ros2 https://github.com/Slamtec/rplidar_ros.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Biên dịch bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt; sau khi đã nạp Jazzy. Lưu ý README gốc liệt kê đến Humble chứ chưa ghi chính thức Jazzy; nếu quá trình biên dịch phát sinh lỗi API, hãy dán log lỗi và dừng lại (không tự sửa API trong bài lab này). Lệnh khởi chạy kèm RViz theo README:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 launch rplidar_ros view_rplidar_a1_launch.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Kiểm tra ảnh hướng trục quét (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rplidar-frame.png&lt;/code&gt;) trong kho mã nguồn và đảm bảo frame laser được phát lên TF. Lưu ý README có lỗi chính tả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rpldiar_ros&lt;/code&gt; ở đường dẫn script &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt;, nên hãy mở trực tiếp thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scripts/&lt;/code&gt; trong bản bạn vừa clone để chạy đúng tệp.&lt;/p&gt;

&lt;p&gt;Kiểm tra dữ liệu quét bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /scan --once&lt;/code&gt;: message hợp lệ phải có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angle_min&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angle_increment&lt;/code&gt; và mảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ranges&lt;/code&gt;. Việc xuất hiện một số giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inf&lt;/code&gt; là bình thường (tia vượt quá tầm đo tối đa); ngược lại, nếu toàn bộ mảng là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inf&lt;/code&gt; hoặc toàn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; thì cần kiểm tra lại cáp, tốc độ baud hoặc xem cửa sổ quét có bị che chắn không.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-đọc-nhanh-tệp-mapyaml-và-kiểm-tra-sai-số-tỉ-lệ-bản-đồ-bằng-tay&quot;&gt;Giải thích: Đọc nhanh tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map.yaml&lt;/code&gt; và kiểm tra sai số tỉ lệ bản đồ bằng tay&lt;/h3&gt;

&lt;p&gt;Sau khi lưu bản đồ bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map_saver_cli&lt;/code&gt;, bạn nhận được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab_map.pgm&lt;/code&gt; (kích thước &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;400 × 300 pixel&lt;/code&gt;) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab_map.yaml&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resolution: 0.05&lt;/code&gt; (mỗi pixel = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5 cm&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Chiều rộng vật lý của vùng bản đồ: \(400 \times 0{,}05 = \mathbf{20{,}0\text{ m}}\).&lt;/li&gt;
  &lt;li&gt;Nếu bạn đo hành lang phòng lab ngoài đời rộng &lt;strong&gt;3,00 m&lt;/strong&gt;, thì khoảng cách giữa hai vạch tường đen trên ảnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.pgm&lt;/code&gt; phải là \(\frac{3{,}00}{0{,}05} = \mathbf{60\text{ pixel}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chẩn đoán lỗi tức thì:&lt;/strong&gt; Nếu hai bức tường song song bị vẽ &lt;em&gt;chập thành hai đường chéo lệch nhau&lt;/em&gt; (double walls), nguyên nhân thường là: (1) ngược dấu \(\omega\) trong odometry, (2) sai góc lắp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_frame&lt;/code&gt; trong URDF, hoặc (3) lái xoay quá nhanh vượt tần số quét LiDAR!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Phần mô phỏng không dùng pin. Khi thử trên xe thật, lần kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; đầu tiên cần nhấc bánh khỏi sàn hoặc ngắt nguồn động cơ VM; khi hạ bánh và đẩy xe quét bản đồ, luôn có người sẵn sàng ở công tắc nguồn. Lidar có phần quay cơ học và phát tia laser — giữ tóc, dây điện tránh xa bộ phận quay và không nhìn thẳng vào cửa sổ phát tia.&lt;/p&gt;

&lt;h3 id=&quot;các-bước-thực-hiện--phần-mô-phỏng&quot;&gt;Các bước thực hiện — phần mô phỏng&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Cài đặt đầy đủ các gói ở trên (có thể ghi lại phiên bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-slam-toolbox&lt;/code&gt; bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt-cache policy&lt;/code&gt; làm minh chứng).&lt;/li&gt;
  &lt;li&gt;Khởi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tb3_simulation_launch.py slam:=True&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TURTLEBOT3_MODEL=waffle&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Điều khiển robot chạy một vòng ngắn trong môi trường mô phỏng (qua mục tiêu Nav2 hoặc teleop đi kèm), sau đó chụp ảnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt; trong RViz hoặc kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /map --once&lt;/code&gt; thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;width&lt;/code&gt; khác 0.&lt;/li&gt;
  &lt;li&gt;Lưu bản đồ:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run nav2_map_server map_saver_cli &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; ~/tb3_map
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ghi lại đường dẫn hai tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/tb3_map.yaml&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/tb3_map.pgm&lt;/code&gt; vào sổ khi lệnh thành công.&lt;/p&gt;

&lt;h3 id=&quot;các-bước-thực-hiện--phần-phần-cứng-thật&quot;&gt;Các bước thực hiện — phần phần cứng thật&lt;/h3&gt;

&lt;p&gt;Nếu chưa có lidar, bỏ qua mục này và ghi rõ vào sổ: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hoãn SLAM thật, ngày, lý do&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Lắp lidar lên khung xe, đo chính xác tọa độ $x$ và $z$ so với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, cập nhật TF và kiểm tra bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run tf2_ros tf2_echo base_link base_laser&lt;/code&gt; (với LD06) hoặc tên frame của driver A1; tên này phải khớp hoàn toàn với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /scan --once&lt;/code&gt; và dán các trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angle_min&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angle_max&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; vào sổ.&lt;/li&gt;
  &lt;li&gt;Đảm bảo node odometry đang xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt; và không bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; (kiểm tra bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 param get&lt;/code&gt; hoặc xem tệp launch).&lt;/li&gt;
  &lt;li&gt;Khởi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 launch slam_toolbox online_async_launch.py&lt;/code&gt;, đẩy chậm xe đi một vòng hành lang và chụp ảnh bản đồ. Nếu hành lang thẳng nhưng bản đồ bị cong, dừng SLAM ngay để sửa dấu encoder trước.&lt;/li&gt;
  &lt;li&gt;Lưu bản đồ bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map_saver_cli -f ~/capstone_map&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Phần mô phỏng: tệp bản đồ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.yaml&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.pgm&lt;/code&gt; đã lưu (hoặc log lỗi launch nguyên văn nếu máy không tải được Gazebo). Phần phần cứng thật: ảnh bản đồ thực tế hoặc ghi chú hoãn có ngày. Tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; khớp với cây TF.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Các tia quét dồn thành một cụm tại gốc RViz&lt;/td&gt;
      &lt;td&gt;Thiếu cạnh TF nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; của laser với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bản đồ bị xoắn cong khi đẩy xe đi thẳng&lt;/td&gt;
      &lt;td&gt;Ngược dấu encoder, đảo trái/phải, hoặc một bánh bị trượt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; không có dữ liệu&lt;/td&gt;
      &lt;td&gt;Sai tốc độ baud (LD06 dùng 230400), sai cổng serial hoặc lỗi luật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SLAM không xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Đang chạy đồng thời AMCL gây xung đột cạnh; tắt AMCL khi đang dựng bản đồ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bật nhầm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; trên xe thật&lt;/td&gt;
      &lt;td&gt;Dấu thời gian không khớp đồng hồ thực; tắt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=ZaiA3hWaRzE&quot;&gt;Easy SLAM with ROS using slam_toolbox&lt;/a&gt; — Articulated Robotics (tiếng Anh). Hướng dẫn dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; dựng bản đồ từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; (giữ đúng tên gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; của Jazzy như trong bài).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=hMTxb8Y2cxI&quot;&gt;ROS2 Nav2 - Generate a Map with slam_toolbox&lt;/a&gt; — Robotics Back-End (tiếng Anh). Quy trình lưu bản đồ bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map_saver&lt;/code&gt; sau khi chạy một vòng, khớp với bước cuối phần mô phỏng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nếu tệp launch LD06 để mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z = 0.18&lt;/code&gt; trong khi lidar trên xe thực tế chỉ cao 0,06 m, hiện tượng sai lệch nào có thể xuất hiện trên bản đồ?&lt;/li&gt;
  &lt;li&gt;Vì sao khóa học không chọn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chmod 777&lt;/code&gt; làm cách phân quyền cổng serial mặc định?&lt;/li&gt;
  &lt;li&gt;Khi mảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ranges&lt;/code&gt; trả về toàn giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, bạn nên kiểm tra tốc độ baud hay cây TF trước?&lt;/li&gt;
  &lt;li&gt;Nếu phần mô phỏng đã đạt nhưng xe chưa có lidar, tiêu chí nào trong checklist chương 11 vẫn đang chờ xử lý?&lt;/li&gt;
  &lt;li&gt;Khi có hai node cùng xuất bản cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;, bạn cần xử lý gì trước khi nghĩ đến việc tinh chỉnh tham số bộ giải (solver) của SLAM?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Mặt phẳng tia quét bị khai báo sai độ cao, có thể khiến bản đồ nhận nhầm mép bàn hoặc thiếu chi tiết tường; cần sửa lại đúng số đo mét trong tệp launch.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chmod 777&lt;/code&gt; cấp quyền đọc/ghi cho mọi người dùng trên hệ thống; chuẩn an toàn hơn là thêm user vào nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; kết hợp luật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Kiểm tra baud và cổng serial trước: sai TF vẫn có dữ liệu khoảng cách (chỉ vẽ sai vị trí), còn toàn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; chứng tỏ chưa đọc được mẫu hợp lệ từ phần cứng.&lt;/li&gt;
  &lt;li&gt;Tiêu chí SLAM trên phần cứng thật (có thể đóng tạm bằng ghi chú hoãn có ngày cụ thể).&lt;/li&gt;
  &lt;li&gt;Tắt ngay publisher bị trùng lặp — xung đột hai nguồn phát trên cùng một cạnh TF không thể khắc phục bằng tham số SLAM.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/tutorials/general_tutorials/navigation2_with_slam/navigation2_with_slam/&quot;&gt;Nav2: Navigating while Mapping (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/SteveMacenski/slam_toolbox&quot;&gt;slam_toolbox&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ldrobotSensorTeam/ldlidar_stl_ros2&quot;&gt;ldlidar_stl_ros2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/Slamtec/rplidar_ros&quot;&gt;rplidar_ros nhánh ros2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.slamtec.com/en/Lidar/A1&quot;&gt;Trang A1 của Slamtec&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Nhìn nhanh MoveIt 2</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_05_moveit_peek/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_05_moveit_peek</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~40 phút&lt;/strong&gt; đọc tài liệu và viết tổng hợp. Bài này không yêu cầu chạy bản demo MoveIt đầy đủ vì robot của khóa học chưa có mô hình cánh tay.&lt;/p&gt;

&lt;p&gt;Xe tự hành di chuyển trên mặt sàn 2D chỉ có 3 bậc tự do \((x, y, \theta)\), nên Nav2 lập kế hoạch đường đi ngay trên bản đồ phẳng. Nhưng một &lt;strong&gt;cánh tay robot 6 bậc tự do (6-DoF)&lt;/strong&gt; sống trong không gian cấu hình 6 chiều \(\mathcal{C} \subseteq \mathbb{R}^6\): chỉ cần dịch bàn kẹp thêm 5 cm, cả 6 khớp khuỷu và cổ tay phải xoay phối hợp sao cho không tự đập vào thân mình và không đụng vào mặt bàn. &lt;strong&gt;MoveIt 2&lt;/strong&gt; là khung phần mềm tiêu chuẩn của ROS 2 giải quyết trọn gói động học nghịch, kiểm tra va chạm 3D và lập kế hoạch quỹ đạo cho cánh tay robot.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-moveit-2-cho-tay-máy&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của MoveIt 2 cho Tay máy&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Không gian làm việc (Task Space \(SE(3)\)) vs. Không gian khớp (Joint Space \(\mathbb{R}^n\))&lt;/strong&gt;:
Người dùng chỉ định mục tiêu bàn kẹp trong không gian 3D (vị trí + hướng), còn động cơ lại nhận góc khớp \((q_1, \dots, q_n)\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Động học Nghịch (Inverse Kinematics — IK) &amp;amp; Ma trận Jacobian&lt;/strong&gt;:
Tìm bộ góc khớp \(q\) đưa đầu công tác (end-effector) tới đúng tư thế mục tiêu (dùng bộ giải giải tích IKFast hoặc lặp số KDL/TRAC-IK) — lưu ý một tư thế 3D có thể có nhiều nghiệm (khuỷu trên/khuỷu dưới) hoặc rơi vào điểm kỳ dị (singularity).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lập kế hoạch Lấy mẫu trong Không gian Cấu hình (OMPL — RRTConnect / PRM)&lt;/strong&gt;:
Vì không gian 6 chiều quá lớn để chia lưới như A* 2D, MoveIt 2 dùng thuật toán lấy mẫu ngẫu nhiên (như RRTConnect) nối hai cây từ trạng thái đầu và trạng thái đích trong khi kiểm tra va chạm qua &lt;strong&gt;Planning Scene&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hồ sơ Thời gian (Time Parameterization — TOTG) &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2_control&lt;/code&gt;&lt;/strong&gt;:
Biến đường đi hình học thuần túy thành quỹ đạo mượt có vận tốc/gia tốc nằm trong giới hạn vật lý của từng khớp, rồi gửi tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JointTrajectoryController&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ nêu rõ thành phần của &lt;strong&gt;planning scene&lt;/strong&gt; và giải thích vì sao giới hạn khớp cùng vật cản bắt buộc phải được khai báo trước khi lập kế hoạch quỹ đạo. Bạn hiểu rõ vì sao hai khớp bánh xe của Capstone không thuộc phạm vi quản lý của MoveIt dù trong URDF chúng là khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt;, liệt kê được điều kiện tối thiểu của một tệp URDF để chạy demo MoveIt có ý nghĩa, và thực hiện phép kiểm tra cài đặt gọn nhẹ bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg list | grep moveit&lt;/code&gt;. Bạn cũng sẽ nắm được khung ngân sách thực tế cho một cánh tay học tập nhập khẩu.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Nắm vững các kiểu khớp URDF từ chương Gazebo (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fixed&lt;/code&gt;). Hiểu rằng xe Capstone là khung vi sai điều khiển bằng vận tốc qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; (dẫn đường bằng Nav2), không phải điều khiển theo góc đích. Bài này không cần servo, tay kẹp hay cài đặt sẵn MoveIt.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Trọng tâm của khóa học là robot di động bánh xe với chuỗi công cụ Capstone → ROS 2 Jazzy → Gazebo → SLAM/Nav2. MoveIt 2 nằm ở một nhánh ứng dụng khác: lập kế hoạch chuyển động cho cánh tay robot, không dùng để điều khiển mọi loại khớp. Việc đưa bánh xe vào một planning group của MoveIt sẽ buộc bộ lập kế hoạch tìm góc quay đích cho bánh xe — điều hoàn toàn sai bản chất động học. Bài học này giúp bạn nhận diện đúng phạm vi của MoveIt, biết khi nào nên từ chối áp dụng nó cho khung xe hiện tại và cần chuẩn bị gì nếu sau này muốn mở rộng thêm cánh tay.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Planning scene&lt;/strong&gt; là không gian mô tả trạng thái hiện tại của các khớp cánh tay, ma trận va chạm được phép (giữa các link liền kề) và mọi vật cản cần tránh như mặt bàn, cột gắn camera hoặc thân robot. Bộ lập kế hoạch (planner) sẽ tìm một quỹ đạo trong không gian khớp sao cho toàn bộ cánh tay không va chạm và luôn nằm trong giới hạn vật lý: cận dưới – cận trên của khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt; tương ứng góc xoay thực của servo, còn giới hạn vận tốc tránh ra lệnh nhanh vượt quá khả năng đáp ứng của bộ truyền động.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_moveit.png&quot; alt=&quot;MoveIt lập một đường trong không gian khớp cho cánh tay trong planning scene; bánh vi sai đứng ngoài nhóm đó&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Giao diện trung tâm trong hầu hết các ví dụ là &lt;strong&gt;MoveGroup&lt;/strong&gt;: bạn gửi mục tiêu cho một nhóm lập kế hoạch (planning group) có tên cụ thể như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arm&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gripper&lt;/code&gt;. MoveGroup kiểm tra planning scene, gọi bộ lập kế hoạch tìm đường và gửi quỹ đạo xuống bộ điều khiển (trajectory controller) để điều khiển vị trí từng khớp. Cơ chế này xuất ra quỹ đạo góc khớp chứ không phát &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; vi sai cho cầu nối động cơ bánh xe.&lt;/p&gt;

&lt;p&gt;Một cấu hình của nhóm cánh tay gồm \(n\) khớp quay có giới hạn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt;) được biểu diễn bởi vector:&lt;/p&gt;

\[q = (q_1, q_2, \ldots, q_n),\]

&lt;p&gt;trong đó mỗi tọa độ bị ràng buộc bởi giới hạn trong URDF:&lt;/p&gt;

\[q_i^{\min} \le q_i \le q_i^{\max}.\]

&lt;p&gt;Một cánh tay học tập 6 bậc tự do có \(n = 6\). Ngược lại, hai bánh xe dẫn động của Capstone không phù hợp để đưa vào vector \(q\) này: đầu vào điều khiển của chúng là vận tốc dài và vận tốc góc từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;, không phải góc quay tuyệt đối của trục bánh (dù một khớp cổ tay quay tự do có thể dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt;, bánh xe dẫn động của xe vi sai luôn phải đứng ngoài mọi planning group).&lt;/p&gt;

&lt;p&gt;Về mặt mô hình hóa, nếu các link cánh tay bị vẽ chồng lên thân xe ở tư thế gốc (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;home&lt;/code&gt;), bộ lập kế hoạch sẽ báo lỗi va chạm ngay lập tức (hoặc tệ hơn là bỏ qua va chạm nếu thiếu thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collision&lt;/code&gt;). Tương tự, khai báo giới hạn góc rộng hơn thực tế sẽ làm kẹt hộp số servo, còn khai báo hẹp quá sẽ làm mất tầm với — cần đo thực tế trước khi nhập vào URDF.&lt;/p&gt;

&lt;p&gt;Khung xe Capstone hiện tại chưa đáp ứng điều kiện chạy demo MoveIt (vì chưa có chuỗi khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt; kèm giới hạn vị trí và gói cấu hình planning group), do đó việc không chạy demo là hoàn toàn hợp lý. Về phần cứng mở rộng, các cánh tay học tập 6 bậc nhập khẩu (như SO-100 hoặc các mẫu tương thích LeRobot) không phải linh kiện phổ thông giá rẻ: tính cả phí vận chuyển và mạch điều khiển servo bus, chi phí thường vượt khá xa mức 5 triệu đồng (cần kiểm tra giá niêm yết tại thời điểm mua) và không phải yêu cầu bắt buộc để hoàn thiện xe Capstone.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Đọc phần giới thiệu trên trang chủ MoveIt 2 để nắm chắc ba khái niệm: lập kế hoạch (planning), planning scene và MoveGroup. Đoạn mẫu sau minh họa mức độ cô đọng cần đạt cho năm câu tóm tắt của bài lab (hãy tự diễn đạt lại bằng lời của bạn):&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Planning scene lưu trạng thái cánh tay cùng các vật cản xung quanh và cập nhật theo từng khớp. Khớp bánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; của xe vi sai không nằm trong scene này vì chúng nhận lệnh vận tốc chứ không nhận góc đích. Trước khi khởi chạy một demo MoveIt, hệ thống cần tệp URDF có khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt; kèm giới hạn số học và một planning group loại trừ bánh xe. Khung xe Capstone chưa có cánh tay nên ta không chạy demo MoveIt lúc này.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Bước kiểm tra terminal duy nhất cần thực hiện trong bài:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 pkg list | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;moveit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu MoveIt đã được cài đặt, lệnh sẽ liệt kê một số gói chứa từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;moveit&lt;/code&gt; (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;moveit_ros&lt;/code&gt;); nếu chưa cài, lệnh không in ra dòng nào. Cả hai kết quả đều hợp lệ — không cần cài thêm hay chạy thử demo khi chưa có URDF cánh tay.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-giải-tay-động-học-nghịch-ik-cánh-tay-phẳng-2-khớp-2-link-planar-arm&quot;&gt;Giải thích: Giải tay Động học Nghịch (IK) cánh tay phẳng 2 khớp (2-Link Planar Arm)&lt;/h3&gt;

&lt;p&gt;Để cảm nhận trực tiếp những gì bộ giải IK trong MoveIt 2 đang làm, xét cánh tay phẳng 2 khớp có chiều dài khâu \(l_1 = 0{,}30\text{ m}\), \(l_2 = 0{,}20\text{ m}\), cần đưa đầu kẹp tới điểm \((x, y) = (0{,}30;\; 0{,}20)\text{ m}\):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Tính khoảng cách từ gốc vai tới mục tiêu:
\(d^2 = x^2 + y^2 = 0{,}30^2 + 0{,}20^2 = 0{,}09 + 0{,}04 = \mathbf{0{,}13\text{ m}^2} \quad (d \approx 0{,}361\text{ m}).\)
Kiểm tra tầm với: \(\vert l_1 - l_2 \vert = 0{,}10 \le 0{,}361 \le 0{,}50 = l_1 + l_2\) → &lt;strong&gt;Nằm trong không gian làm việc!&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Áp dụng định lý cosin cho góc khuỷu \(q_2\):
\(\cos q_2 = \frac{d^2 - l_1^2 - l_2^2}{2 l_1 l_2} = \frac{0{,}13 - 0{,}09 - 0{,}04}{2(0{,}30)(0{,}20)} = \frac{0}{0{,}12} = \mathbf{0} \implies q_2 = \mathbf{\pm 90^\circ}!\)&lt;/li&gt;
  &lt;li&gt;Hai dấu \(\pm 90^\circ\) tương ứng với &lt;strong&gt;đúng 2 nghiệm hình học hợp lệ&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Khuỷu dưới (\(q_2 = +90^\circ\)):&lt;/strong&gt; đặt \(q_1 = 0^\circ\) (khâu 1 nằm ngang tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0,30; 0)&lt;/code&gt; rồi khâu 2 dựng thẳng đứng lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0,30; 0,20)&lt;/code&gt;).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Khuỷu trên (\(q_2 = -90^\circ\)):&lt;/strong&gt; \(q_1 = 2\arctan(0{,}20/0{,}30) \approx 67{,}4^\circ\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;MoveIt 2 giải chính bài toán này trong 6 chiều và chọn nghiệm nào không vướng vật cản trên bàn!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Mở &lt;a href=&quot;https://moveit.picknik.ai/main/index.html&quot;&gt;tài liệu MoveIt 2&lt;/a&gt; và viết một đoạn gồm năm câu vào tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;moveit-peek.md&lt;/code&gt; phủ đủ ba nội dung sau (thứ tự linh hoạt):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Planning scene là gì và chứa những thông tin nào.&lt;/li&gt;
  &lt;li&gt;Vì sao khớp bánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; trên xe Capstone không thuộc planning group của MoveIt.&lt;/li&gt;
  &lt;li&gt;Điều kiện cần thiết trước khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 launch&lt;/code&gt; cho một demo MoveIt (URDF có khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt; kèm giới hạn góc và một planning group đã loại trừ bánh xe).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sau đó chạy lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg list | grep moveit&lt;/code&gt; và dán kết quả (hoặc ghi rõ “chưa cài đặt”) vào cùng tệp. Không cần cài MoveIt chỉ để có đầu ra và không cần mở demo RViz.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt; không in ra dòng nào&lt;/td&gt;
      &lt;td&gt;MoveIt chưa được cài trên máy&lt;/td&gt;
      &lt;td&gt;Ghi rõ “chưa cài” và hoàn thiện năm câu tóm tắt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chạy demo báo thiếu mô tả robot rồi thoát&lt;/td&gt;
      &lt;td&gt;Xe Capstone chưa có URDF cánh tay&lt;/td&gt;
      &lt;td&gt;Dừng lại; bài lab không yêu cầu chạy demo này&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mọi lần lập kế hoạch đều báo va chạm ngay ở tư thế đầu&lt;/td&gt;
      &lt;td&gt;Các link cánh tay bị vẽ chồng lên khung xe trong URDF&lt;/td&gt;
      &lt;td&gt;Sửa hình học va chạm trước khi đổi thuật toán planner&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Planner phát lệnh góc quay cho bánh xe&lt;/td&gt;
      &lt;td&gt;Đưa nhầm khớp bánh vào planning group&lt;/td&gt;
      &lt;td&gt;Loại bỏ khớp bánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; khỏi planning group&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Phân vân chọn mua cánh tay ngay trong buổi lab&lt;/td&gt;
      &lt;td&gt;Nhầm phạm vi bài giới thiệu tổng quan thành bước mua sắm&lt;/td&gt;
      &lt;td&gt;Hoàn thành năm câu tóm tắt trước; cân nhắc phần cứng về sau&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Bài này không có bảng mua linh kiện trong nước. Nếu sau này muốn tham khảo chi phí cánh tay, hãy dùng khung ước lượng ở mục Khái niệm, đối chiếu giá thực tế và tính thêm mạch servo bus.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=-xDyxxRiW7M&quot;&gt;ROS 2 MoveIt 2 (1H Crash Course)&lt;/a&gt; — Robotics Back-End (tiếng Anh). Tổng quan cách MoveIt 2 lập quỹ đạo khớp (video dài một giờ; tập trung vào điểm khác biệt với điều khiển &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; của xe vi sai).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;h3 id=&quot;bài-1&quot;&gt;Bài 1&lt;/h3&gt;

&lt;p&gt;Liệt kê ba đối tượng thuộc planning scene của một cánh tay gắn trên bàn và một thông số không thuộc planning scene vì không phải yếu tố hình học.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Thuộc scene: các link của cánh tay, mặt bàn (vật cản va chạm) và khung xe nếu nằm trong tầm với (giới hạn khớp là ràng buộc trên \(q\) gắn với mô hình robot). Ngoài scene: tần số PWM (thông số điện, không phải hình học).&lt;/p&gt;

&lt;h3 id=&quot;bài-2&quot;&gt;Bài 2&lt;/h3&gt;

&lt;p&gt;Nếu ai đó thêm hai khớp bánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; vào planning group &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arm&lt;/code&gt; để “quản lý cả robot bằng MoveIt”, lỗi kỹ thuật gì sẽ xảy ra xét theo đầu ra của planner?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Planner sẽ tối ưu góc quay đích của trục bánh, trong khi khung xe vi sai cần vận tốc dài và góc qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;bài-3&quot;&gt;Bài 3&lt;/h3&gt;

&lt;p&gt;Liệt kê các thành phần tối thiểu cần kiểm tra trong URDF trước khi chạy demo MoveIt. Nếu URDF chỉ có khớp bánh xe thì cần quyết định thế nào?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Cần chuỗi khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt;, cận trên – cận dưới từng khớp, tên planning group và loại trừ bánh xe; nếu chỉ có bánh xe thì dừng và tiếp tục dùng Nav2.&lt;/p&gt;

&lt;h3 id=&quot;bài-4&quot;&gt;Bài 4&lt;/h3&gt;

&lt;p&gt;Khi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg list | grep moveit&lt;/code&gt; không trả về kết quả nào, trạng thái bài lab là đạt hay trượt và bạn không cần làm bước nào tiếp theo?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Kết quả hoàn toàn hợp lệ (xác nhận MoveIt chưa cài); bạn không cần cài thêm gói hay cố chạy demo.&lt;/p&gt;

&lt;h3 id=&quot;bài-5&quot;&gt;Bài 5&lt;/h3&gt;

&lt;p&gt;Viết một tin nhắn ngắn tư vấn cho người muốn mua ngay cánh tay 6 bậc gắn lên Capstone trước khi khung xe chạy được Nav2 (nêu rõ khoảng ngân sách và servo bus, không bịa giá chi tiết theo mẫu).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Nhấn mạnh đây là hàng nhập khẩu, tổng chi phí sau vận chuyển thường vượt khá xa 5 triệu đồng kèm servo bus, nên kiểm tra lại sau khi hoàn thiện phần di động với Nav2.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://moveit.picknik.ai/main/index.html&quot;&gt;Tài liệu MoveIt 2&lt;/a&gt; — tài liệu chính thức về planning scene, MoveGroup và cấu trúc gói cấu hình.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/configuration_and_development/first_time_robot_setup_guide/urdf/setup_urdf/&quot;&gt;Ghi chú URDF của Nav2&lt;/a&gt; — tài liệu đối chiếu dành riêng cho robot di động bánh xe như Capstone.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Nhìn nhanh Behavior Tree</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_04_bt_peek/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_04_bt_peek</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~45 phút&lt;/strong&gt;, chủ yếu dành để đọc tệp XML cài sẵn trong Nav2 và ghi nhận chính xác tên các nút cấu thành.&lt;/p&gt;

&lt;p&gt;Khi lập trình hành vi cho robot, mọi người mới đều bắt đầu bằng một cỗ máy trạng thái hữu hạn (FSM) viết bằng hàng trăm dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if / elif / else&lt;/code&gt;. Với 3 trạng thái, FSM rất gọn; nhưng khi robot có 15 trạng thái (kiểm tra pin, đi giao hàng, tránh người, gọi thang máy, xử lý lỗi mất Wi-Fi, quay về sạc), số mũi tên chuyển trạng thái bùng nổ thành một “mạng nhện” không ai dám sửa! &lt;strong&gt;Cây Hành vi (Behavior Tree — BT)&lt;/strong&gt;, trái tim điều phối của Nav2, thay thế mạng nhện đó bằng một cấu trúc cây phân cấp cực kỳ mô-đun và dễ đọc.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-cây-hành-vi-behavior-tree&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Cây Hành vi (Behavior Tree)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Ba trạng thái trả về chuẩn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt;)&lt;/strong&gt;:
Khác với hàm thường chỉ trả Đúng/Sai, mọi nút BT đều nhận nhịp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tick()&lt;/code&gt; định kỳ và có thể trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt; để báo &lt;em&gt;“tác vụ đang chạy, đừng chặn luồng — nhịp sau hỏi lại!”&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ba loại nút điều khiển cốt lõi (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sequence&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fallback / Selector&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Decorator&lt;/code&gt;)&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Sequence (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;→&lt;/code&gt; — logic AND):&lt;/strong&gt; Chạy lần lượt các con từ trái sang phải; dừng và báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt; ngay nếu có 1 con thất bại.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Fallback (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt; — logic OR):&lt;/strong&gt; Thử lần lượt từng phương án dự phòng từ trái sang phải; dừng và báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt; ngay khi có 1 phương án thành công.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Decorator:&lt;/strong&gt; Bọc 1 nút con để biến đổi hành vi (lặp lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Retry&lt;/code&gt;, đảo kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Inverter&lt;/code&gt;, đặt thời gian chờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Timeout&lt;/code&gt;).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính phản ứng (Reactivity) trong Nav2&lt;/strong&gt;:
Mỗi nhịp tick quét lại từ gốc giúp robot lập tức ngắt hành động hiện tại để chuyển sang nhánh ưu tiên cao hơn (ví dụ pin tụt dưới 15% thì dừng giao hàng và ưu tiên chạy về trạm sạc).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ định nghĩa chính xác hai nút điều khiển cơ bản: &lt;strong&gt;Sequence&lt;/strong&gt; (“mọi nút con phải thành công theo thứ tự”) và &lt;strong&gt;Fallback&lt;/strong&gt; (“dừng ở nút con thành công đầu tiên”), kể cả trạng thái của các nút con chưa được thực thi. Bạn liên kết được hai cơ chế này với các hành vi phục hồi (recovery) đã thấy trong bài tour Nav2 (lập lại đường, xoay tại chỗ, lùi lại, xóa costmap), tự tìm đọc tệp XML thực tế bên trong gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav2_bt_navigator&lt;/code&gt;, và giải thích vì sao không được sửa trực tiếp tệp hệ thống cũng như vì sao cây hành vi không điều khiển trực tiếp tín hiệu PWM.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Đã hoàn thành bài tour Nav2 (11-02): bạn hiểu cách một mục tiêu trong RViz chuyển thành tuyến đường rồi thành lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, cũng như việc robot tự xoay hoặc lùi khi gặp vật cản. Bài này không cần robot thật, lidar hay công cụ vẽ đồ họa behavior tree.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Trong bản mô phỏng TurtleBot3 ở bài trước, robot có thể linh hoạt theo đuổi mục tiêu nhờ bộ điều phối của Nav2 được xây dựng trên một cây hành vi (behavior tree). Nếu không có cây này, chỉ cần bộ điều khiển bám đường báo thất bại là robot sẽ đứng yên chờ con người can thiệp; nhờ có cây hành vi, trạng thái thất bại lập tức chuyển sang bước phục hồi rồi thử lập kế hoạch lại. Toàn bộ quy trình vẫn kết thúc ở topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; đi xuống cầu nối của bạn chứ không can thiệp vào mạch cầu H.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Behavior tree&lt;/strong&gt; là một cấu trúc cây được kích hoạt định kỳ (tick) từ nút gốc nhiều lần mỗi giây. Mỗi nút khi được tick sẽ trả về một trong ba trạng thái: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt; (thành công), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt; (thất bại) hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt; (đang thực thi). Các &lt;strong&gt;nút điều khiển&lt;/strong&gt; (control node) không trực tiếp di chuyển robot mà chỉ điều hướng xem nút con nào được tick tiếp theo; các &lt;strong&gt;nút lá&lt;/strong&gt; (action node) mới là nơi gọi vào các action server của ROS như lập đường, bám đường, xoay, chờ hoặc xóa costmap.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_bt.png&quot; alt=&quot;Một behavior tree nhỏ: fallback thử chuỗi lập kế hoạch rồi bám đường, thất bại thì tick một recovery&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sequence&lt;/strong&gt; thực thi các nút con lần lượt từ trái sang phải. Chỉ cần một nút con trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt;, toàn bộ Sequence lập tức thất bại và các nút con phía sau không được chạy; Sequence chỉ trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt; khi mọi nút con đều thành công, và trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt; nếu có nút con đang chạy dở. Ví dụ điển hình là chuỗi “lập đường → bám đường”: nếu bước lập đường thất bại thì bước bám đường sẽ không bao giờ khởi chạy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fallback&lt;/strong&gt; (còn gọi là Selector) cũng duyệt các nút con theo thứ tự nhưng dừng ngay tại nút con đầu tiên trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt; (các nút phía sau không chạy); Fallback chỉ thất bại khi tất cả các nút con đều thất bại. Đây chính là logic “thử dẫn đường trước, nếu hỏng thì chạy phục hồi”. Biến thể &lt;strong&gt;ReactiveFallback&lt;/strong&gt; tiếp tục kiểm tra lại các nút con đứng trước ở mỗi nhịp tick, nhờ vậy nếu lối đi vừa thông thoáng trở lại hoặc có goal mới, hệ thống có thể ngắt ngay hành vi xoay phục hồi đang dang dở.&lt;/p&gt;

&lt;p&gt;Tệp cây dẫn đường thực tế của Nav2 dài hơn sơ đồ minh họa bên dưới, nhưng vẫn tuân theo cùng một cấu trúc: một nhánh tính toán và bám quỹ đạo được bọc trong khối fallback để chuyển sang các hành vi phục hồi khi gặp sự cố (xoay tại chỗ, lùi ngắn, xóa costmap toàn cục hoặc cục bộ khỏi các vật cản ảo). Planner và controller mà bạn đã gặp ở bài 11-02 chính là các nút lá được cây hành vi gọi thực thi.&lt;/p&gt;

&lt;p&gt;Các nút lá giao tiếp với action server của ROS: bước bám đường kết thúc bằng việc controller xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, và các hành vi xoay hay lùi cũng tạo ra lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; có chủ đích trong thời gian ngắn. Không có nút nào trong cây tạo xung PWM — bánh xe Capstone quay trong quá trình phục hồi là nhờ cầu nối của bạn (hoặc plugin mô phỏng) nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;. Nếu xóa bỏ nhánh recovery để “làm gọn” tệp XML, chỉ một vật cản nhỏ chặn đường cũng sẽ khiến robot dừng vĩnh viễn.&lt;/p&gt;

&lt;p&gt;Nav2 sử dụng thư viện &lt;a href=&quot;https://www.behaviortree.dev/&quot;&gt;BehaviorTree.CPP&lt;/a&gt;. Trong phạm vi bài này bạn chưa cần viết mã C++ hay tự biên dịch thư viện. Dưới đây là sơ đồ rút gọn (không phải tệp gốc của Nav2) giúp hình dung luồng điều khiển:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;root&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;BTCPP_format=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;4&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;BehaviorTree&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ID=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;CapstoneNavigateSketch&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;ReactiveFallback&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;NavigateOrRecover&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Sequence&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PlanThenFollow&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- Cây thật đặt ComputePathToPose ở đây và ghi một đường. --&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- Em kế tiếp là FollowPath, bám đường đó. --&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Sequence&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Sequence&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;OneRecovery&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- Cây thật đặt Spin, BackUp, hoặc ClearEntireCostmap ở đây. --&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Sequence&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/ReactiveFallback&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/BehaviorTree&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/root&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanThenFollow&lt;/code&gt; thành công, nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OneRecovery&lt;/code&gt; sẽ không được chạy. Khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanThenFollow&lt;/code&gt; thất bại, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReactiveFallback&lt;/code&gt; chuyển sang tick &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OneRecovery&lt;/code&gt;, và ở nhịp tick kế tiếp nó vẫn kiểm tra lại nhánh dẫn đường. Một nút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fallback&lt;/code&gt; thông thường không kiểm tra lại nút con đứng trước theo cách này — hãy đọc tệp cài sẵn trên máy để xem Nav2 thực tế đang dùng nút nào.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Đảm bảo đã cài hai gói Nav2 như ở bài trước, nạp môi trường Jazzy và tra đường dẫn cài đặt của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav2_bt_navigator&lt;/code&gt; thay vì đoán đường dẫn:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-navigation2 ros-jazzy-nav2-bringup
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 pkg prefix nav2_bt_navigator
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trên bản cài đặt chuẩn từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt;, kết quả sẽ là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt;. Khi đó liệt kê thư mục chứa các cây hành vi:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt; /opt/ros/jazzy/share/nav2_bt_navigator/behavior_trees/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(Nếu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prefix&lt;/code&gt; trả về thư mục khác do bạn đang dùng overlay, hãy liệt kê &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;share/nav2_bt_navigator/behavior_trees/&lt;/code&gt; bên dưới chính thư mục đó.)&lt;/p&gt;

&lt;p&gt;Kết quả mong đợi là danh sách các tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.xml&lt;/code&gt;, trong đó có ít nhất một tệp chứa từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigate&lt;/code&gt;. Mở tệp đó bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;less&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;less /opt/ros/jazzy/share/nav2_bt_navigator/behavior_trees/navigate_to_pose_w_replanning_and_recovery.xml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu tên tệp trên máy bạn hơi khác, hãy mở đúng tệp chứa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigate&lt;/code&gt; từ kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt;. Bên trong thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BehaviorTree&lt;/code&gt;, xác định thẻ điều khiển gốc đầu tiên, sau đó tìm các nút lá phục hồi (thuộc nhóm như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spin&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BackUp&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClearEntireCostmap&lt;/code&gt;) và chép nguyên văn hai tên thẻ (kể cả hậu tố nếu có) từ tệp thật chứ không chép từ sơ đồ minh họa của bài học.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-chạy-tay-nhịp-tick-trên-cây-bt-kiểm-tra-pin--đi-giao-hàng&quot;&gt;Giải thích: Chạy tay nhịp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tick()&lt;/code&gt; trên cây BT “Kiểm tra Pin &amp;amp; Đi Giao Hàng”&lt;/h3&gt;

&lt;p&gt;Xét cây hành vi gồm nút gốc &lt;strong&gt;Fallback (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt;)&lt;/strong&gt; có 2 nhánh:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Nhánh trái — Sequence (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;→&lt;/code&gt;) [An toàn Pin]:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Pin &amp;lt; 15%?] → [Đi về trạm sạc]&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nhánh phải — Sequence (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;→&lt;/code&gt;) [Nhiệm vụ chính]:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Tính đường đi] → [Bám đường tới đích]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Theo dõi kết quả trả về trong 2 tình huống thực tế:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Tình huống&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Pin &amp;lt; 15%?]&lt;/code&gt;&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Nhánh trái trả về&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Nhánh phải chạy không?&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Hành động thực tế của robot&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Pin còn 60%&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt; (ngắt nhánh trái ngay)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;CÓ&lt;/strong&gt; (Fallback chuyển sang nhánh phải)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Robot tính đường và tiếp tục đi giao hàng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Pin tụt còn 14% giữa đường&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt; (đang chạy về sạc)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;BỊ NGẮT NGAY!&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Robot lập tức hủy giao hàng, quay đầu về trạm sạc!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Không cần một dòng mũi tên chằng chịt nào: chỉ cần đặt điều kiện ưu tiên cao hơn sang bên trái của nút Fallback là hệ thống tự động phản ứng an toàn ở mọi thời điểm!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Thực hiện các bước trên máy đã cài Jazzy và ghi kết quả gồm ba phần vào sổ thực hành:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Dán kết quả lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; của thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;behavior_trees&lt;/code&gt; (xác nhận có tệp XML chứa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigate&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Chép chính xác tên thẻ của nút điều khiển gốc bên trong tệp navigate bạn mở (kèm thuộc tính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; nếu có).&lt;/li&gt;
  &lt;li&gt;Chép nguyên văn hai tên thẻ hành động recovery có trong cùng tệp đó.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg prefix nav2_bt_navigator&lt;/code&gt; báo lỗi, hãy cài lại gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; và nạp lại môi trường Jazzy trước khi kết luận thiếu tệp.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Package not found&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Chưa cài gói Nav2 hoặc chưa nạp môi trường Jazzy&lt;/td&gt;
      &lt;td&gt;Chạy lệnh cài &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt; lại rồi kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg prefix&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Không thấy thư mục dưới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Gói nằm trong overlay khác hoặc cài đặt chưa hoàn tất&lt;/td&gt;
      &lt;td&gt;Dùng chính đường dẫn từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg prefix&lt;/code&gt; để chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thấy tệp XML nhưng không có tên nào chứa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigate&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Liệt kê nhầm thư mục&lt;/td&gt;
      &lt;td&gt;Đảm bảo đường dẫn kết thúc bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;behavior_trees&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sửa tệp XML xong khiến mô phỏng không chạy được&lt;/td&gt;
      &lt;td&gt;Chỉnh sửa trực tiếp tệp hệ thống không tạo bản sao&lt;/td&gt;
      &lt;td&gt;Cài lại gói từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt;; luôn sao chép tệp vào workspace trước khi sửa&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot không còn biết xoay/lùi khi kẹt&lt;/td&gt;
      &lt;td&gt;Đã xóa mất các nút recovery trong bản sao&lt;/td&gt;
      &lt;td&gt;Khôi phục lại nhánh recovery chứa các hành động xoay và lùi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Kỳ vọng cây hành vi kích hoạt trực tiếp chân GPIO&lt;/td&gt;
      &lt;td&gt;Cây hành vi chỉ kết thúc ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; như toàn bộ Nav2&lt;/td&gt;
      &lt;td&gt;Việc tạo xung PWM vẫn do node cầu nối đảm nhiệm&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Bài này thực hiện hoàn toàn bằng phần mềm, không cần mua linh kiện hay cài thêm trình chỉnh sửa đồ họa.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=KeShMInMjro&quot;&gt;5 minute Behavior Tree tutorial&lt;/a&gt; — Petter Ögren (tiếng Anh). Tóm tắt trực quan cơ chế tick, thành công và thất bại của cây hành vi trong 5 phút.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=kRp3eA09JkM&quot;&gt;Behavior Trees in Robotics (Part 1 - Concept)&lt;/a&gt; — Hummingbird (tiếng Anh). Giải thích khái niệm cây hành vi trong robotics (có thể dừng trước phần cài đặt thư viện).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;h3 id=&quot;bài-1&quot;&gt;Bài 1&lt;/h3&gt;

&lt;p&gt;Một nút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sequence&lt;/code&gt; có ba nút con. Trong lượt tick hiện tại, con thứ nhất trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt; và con thứ hai trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sequence&lt;/code&gt; sẽ trả về trạng thái nào và con thứ ba có được chạy không?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sequence&lt;/code&gt; dừng ngay tại nút con thất bại đầu tiên: kết quả là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt; và con thứ ba không được tick.&lt;/p&gt;

&lt;h3 id=&quot;bài-2&quot;&gt;Bài 2&lt;/h3&gt;

&lt;p&gt;Một nút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fallback&lt;/code&gt; có ba nút con: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spin&lt;/code&gt; trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BackUp&lt;/code&gt; trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClearCostmap&lt;/code&gt; chưa được chạy. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fallback&lt;/code&gt; trả về trạng thái nào và những nút con nào đã thực thi?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fallback&lt;/code&gt; dừng ở nút thành công đầu tiên: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spin&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BackUp&lt;/code&gt; đã chạy, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClearCostmap&lt;/code&gt; không chạy, và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fallback&lt;/code&gt; trả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;bài-3&quot;&gt;Bài 3&lt;/h3&gt;

&lt;p&gt;Nếu bạn muốn thử bỏ hành động &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Wait&lt;/code&gt; khỏi danh sách phục hồi, vì sao không nên sửa trực tiếp tệp dưới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt; và bản sao chỉnh sửa nên đặt ở đâu?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt; do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; quản lý — các bản cập nhật sẽ ghi đè thay đổi và lỗi cú pháp sẽ làm hỏng toàn bộ hệ thống. Hãy sao chép tệp XML vào một gói trong workspace cá nhân rồi trỏ tham số launch vào bản sao đó.&lt;/p&gt;

&lt;h3 id=&quot;bài-4&quot;&gt;Bài 4&lt;/h3&gt;

&lt;p&gt;Giải thích trong hai đến ba câu vì sao cây hành vi không trực tiếp điều khiển PWM trên xe Capstone dù các hành vi phục hồi vẫn làm TurtleBot3 mô phỏng tự xoay được.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Nút lá của cây chỉ xuất lệnh vận tốc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;; robot mô phỏng quay được nhờ plugin dẫn động đăng ký topic này, còn trên xe thật nhiệm vụ chuyển sang PWM thuộc về node cầu nối của bạn.&lt;/p&gt;

&lt;h3 id=&quot;bài-5&quot;&gt;Bài 5&lt;/h3&gt;

&lt;p&gt;Sau khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; và thấy tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigate_to_pose_w_replanning_and_recovery.xml&lt;/code&gt;, bạn cần trích xuất thông tin nào từ tệp đó và cần tránh sao chép thông tin nào từ bài học này?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Ghi lại chính xác thẻ điều khiển gốc và hai thẻ recovery thực tế trong tệp vừa mở; không nộp tên giả lập &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateOrRecover&lt;/code&gt; từ sơ đồ minh họa trừ khi tệp thật có đúng tên đó.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.behaviortree.dev/&quot;&gt;BehaviorTree.CPP&lt;/a&gt; — tài liệu gốc của thư viện mà Nav2 nhúng (bao gồm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sequence&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fallback&lt;/code&gt; và các biến thể &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Reactive&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/&quot;&gt;Nav2 getting started cho Jazzy&lt;/a&gt; — tài liệu liên kết bộ điều phối cây hành vi với toàn bộ stack Nav2.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Pipeline thị giác cơ bản</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_03_vision_pipeline/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_03_vision_pipeline</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~75 phút&lt;/strong&gt; khi camera USB nhận diện tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video0&lt;/code&gt;, hoặc khoảng 45 phút nếu máy chưa có camera và bạn hoàn thành phần thiết kế pipeline trên giấy.&lt;/p&gt;

&lt;p&gt;LiDAR 2D đo khoảng cách tường cực chuẩn nhưng lại hoàn toàn “mù màu” và “mù ngữ nghĩa”: nó không phân biệt được đâu là một chiếc hộp các-tông cần nhặt, đâu là biển báo dừng, hay đâu là đôi chân của một người đang bước tới. Đưa &lt;strong&gt;Camera (RGB / RGB-D)&lt;/strong&gt; vào đường ống ROS 2 bổ sung đôi mắt ngữ nghĩa cho robot — nhưng cũng đòi hỏi hiểu rõ hình học phép chiếu camera và ngân sách xử lý thời gian thực trên SBC.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-đường-ống-thị-giác-robot-ros-2-vision&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Đường ống Thị giác Robot (ROS 2 Vision)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Mô hình Camera Lỗ kim (Pinhole Camera) &amp;amp; Hiệu chuẩn Nội tại (Intrinsic Matrix \(K\))&lt;/strong&gt;:
Biến đổi điểm 3D \((X, Y, Z)\) trong hệ camera thành tọa độ pixel \((u, v)\) qua tiêu cự \((f_x, f_y)\), tâm ảnh \((c_x, c_y)\) và hệ số méo ống kính (distortion) — phát kèm trên topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/camera/camera_info&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Định vị Marker Tin cậy (AprilTag / ArUco) &amp;amp; Cầu nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cv_bridge&lt;/code&gt;&lt;/strong&gt;:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cv_bridge&lt;/code&gt; chuyển ảnh ROS 2 sang mảng NumPy/OpenCV; thuật toán AprilTag dùng kích thước vật lý đã biết của thẻ vuông để giải bài toán PnP (Perspective-n-Point), trả về trực tiếp tư thế 6-DoF \((x, y, z, \text{roll}, \text{pitch}, \text{yaw})\) gắn lên cây TF2!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quản lý Băng thông &amp;amp; Tách tải Suy luận&lt;/strong&gt;:
Luôn nén ảnh khi truyền qua mạng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image_transport&lt;/code&gt;), giảm độ phân giải phù hợp bài toán (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;640×480&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;320×240&lt;/code&gt;) và tách node chạy mô hình phát hiện vật thể (YOLO/ONNX) để không làm nghẽn vòng điều khiển động cơ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ xây dựng quy trình xử lý thị giác theo đúng thứ tự &lt;strong&gt;ánh sáng → camera → thuật toán&lt;/strong&gt; (tránh chỉnh ngưỡng màu khi ánh sáng môi trường làm cháy sáng thẻ màu). Bạn nắm được kiểu dữ liệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam&lt;/code&gt; xuất bản, tính được tọa độ trọng tâm (centroid), giới hạn tần số ra quyết định ở mức 5–10 Hz thay vì gửi lệnh theo từng khung hình, phân biệt rõ hệ tọa độ quang học với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; theo chuẩn REP-103, và biết cách ghi nhận tình huống thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video0&lt;/code&gt; một cách trung thực.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn chạy được script Python cơ bản và hiểu ảnh số là một mảng hai chiều (hoặc ba chiều theo kênh màu). Từ bài tour Nav2, bạn đã nắm cấu trúc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; gồm thành phần tịnh tiến và thành phần quay. Bài thực hành này không yêu cầu camera chiều sâu hay RealSense.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Trên lộ trình từ khung xe Capstone lên ROS 2 Jazzy, qua Gazebo rồi đến tự hành, xe đã có cầu nối từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; sang PWM, còn ROS 2 cho phép camera xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/Image&lt;/code&gt; song song với dữ liệu lidar. Thử nghiệm thực tế với một tấm thẻ màu trên bàn giúp bạn thấy ngay sự nhạy cảm của ngưỡng màu trước ánh sáng — điều mà camera mô phỏng khó phản ánh hết. Hành động điều khiển trong bài giữ ở mức cơ bản (quay về phía thẻ hoặc dừng lại) nhưng vẫn dùng đúng message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; như Nav2. Duy trì nhịp quyết định 5–10 Hz thay vì chạy hết tốc độ 30 Hz giúp cầu nối hoạt động êm và dễ phối hợp với bộ điều hướng về sau.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Bước 1: Ánh sáng.&lt;/strong&gt; Cùng một tấm thẻ đỏ nhưng đặt dưới đèn trần hay dưới nắng chiều sẽ cho giá trị màu hoàn toàn khác nhau. Nếu mắt thường không thấy sự tách biệt rõ rệt giữa thẻ và nền, OpenCV cũng không thể phân đoạn ổn định; do đó hãy điều chỉnh nguồn sáng hoặc tránh ngược sáng trước khi chỉnh ngưỡng màu (hue).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 2: Camera.&lt;/strong&gt; Sử dụng camera USB chuẩn UVC (thường xuất hiện tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video0&lt;/code&gt;), với driver &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v4l2_camera&lt;/code&gt; xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/Image&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 3: Thuật toán.&lt;/strong&gt; Bài này tập trung vào phân ngưỡng màu kết hợp tính trọng tâm, chưa dùng mạng nơ-ron phát hiện vật thể.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_vision.png&quot; alt=&quot;Ảnh camera thành mặt nạ ngưỡng, rồi thành centroid có thể phát một lệnh yaw chậm&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Trên máy ROS 2 Jazzy đã cài gói nhị phân, cú pháp chạy driver &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam&lt;/code&gt; là:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-usb-cam
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 run usb_cam usb_cam_node_exe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 launch usb_cam camera.launch.py&lt;/code&gt; sẽ mở kèm cửa sổ xem ảnh. Topic mặc định thường là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/image_raw&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/Image&lt;/code&gt;); nếu topic không có dữ liệu, cần kiểm tra đường dẫn thiết bị trước khi đụng tới thuật toán ngưỡng. Bài lab dưới đây gọi trực tiếp OpenCV nên chưa bắt buộc chạy node này.&lt;/p&gt;

&lt;p&gt;Phân ngưỡng trong không gian màu HSV ổn định hơn kênh RGB thô nhưng vẫn phụ thuộc điều kiện chiếu sáng; hãy chốt rõ cận dưới, cận trên cùng điều kiện ánh sáng sử dụng. Sau khi tạo mặt nạ nhị phân (vùng thẻ mang giá trị trắng), ta dùng mô-men ảnh để tính trọng tâm (centroid). Với tập \(N\) điểm ảnh trắng có tọa độ \((x_i, y_i)\):&lt;/p&gt;

\[c_x = \frac{\sum_i x_i}{N}, \qquad c_y = \frac{\sum_i y_i}{N},\]

&lt;p&gt;chính là phép chia &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m10 / m00&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m01 / m00&lt;/code&gt; từ hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cv2.moments&lt;/code&gt;. Ví dụ với một mặt nạ gồm ba điểm ảnh tại \((100, 80)\), \((102, 80)\) và \((101, 82)\):&lt;/p&gt;

\[c_x = \frac{100 + 102 + 101}{3} = 101, \qquad c_y = \frac{80 + 80 + 82}{3} \approx 80{,}67.\]

&lt;p&gt;Với ảnh rộng 320 pixel (tâm ngang tại 160), trọng tâm \(c_x = 101\) nằm về phía bên trái tâm khung hình. Khi camera hướng thẳng về trước, điều này có nghĩa mục tiêu đang ở bên trái, nên xe vi sai cần quay trái (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; dương theo REP-103) để đưa trọng tâm tiến về giữa ảnh. Nếu diện tích &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m00&lt;/code&gt; quá nhỏ (không phát hiện thẻ), cần dừng hoặc phát &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; bằng 0 thay vì để xe đuổi theo nhiễu.&lt;/p&gt;

&lt;p&gt;Tần số ra quyết định nên giữ chậm: dù camera thu 30 khung hình/giây, việc gửi 30 lệnh vận tốc mới mỗi giây sẽ làm rung hộp số và tranh chấp tài nguyên. Thay vào đó, chỉ trích xuất và ra quyết định ở nhịp 5–10 Hz, gửi một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; (hoặc lệnh dừng rõ ràng) rồi giữ đến chu kỳ tiếp theo.&lt;/p&gt;

&lt;p&gt;Theo &lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt;, hệ tọa độ thân xe (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt;) có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; hướng trước, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; sang trái, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; hướng lên; trong khi hệ tọa độ quang học (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_optical_frame&lt;/code&gt; — giá trị ghi vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; của ảnh) có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; hướng dọc trục ống kính ra trước, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; sang phải ảnh và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; hướng xuống dưới ảnh. Phép chuyển tĩnh phổ biến từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_optical_frame&lt;/code&gt; gồm roll \(-\pi/2\) và yaw \(-\pi/2\) (luôn kiểm tra lại trong URDF của bạn). Tọa độ pixel chưa phải tọa độ 3D trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; nếu chưa áp dụng phép chiếu và phép quay này; trong phạm vi bài này ta chỉ cần điều khiển quay trực tiếp theo độ lệch ngang trên ảnh.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Đoạn mã dưới đây cài đặt trọn vẹn thuật toán độc lập với ROS: mở camera đầu tiên, lọc một dải màu cam rộng trong không gian HSV và in tọa độ trọng tâm. Nếu thẻ của bạn có màu khác (ví dụ màu xanh), hãy điều chỉnh cận dưới/trên và ghi lại vào sổ. Chạy lệnh:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python3 card_centroid.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;cap&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;VideoCapture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isOpened&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;error: cannot open /dev/video0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;release&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;error: camera opened but read failed&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;hsv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cvtColor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;COLOR_BGR2HSV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;upper&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;inRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hsv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;moments&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;moments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;area&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;m00&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;area&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;centroid none area=0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;m10&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;m01&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;centroid u=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; v=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; area=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Đưa tấm thẻ cam ra trước ống kính dưới nguồn sáng ổn định. Kết quả thành công có dạng:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;centroid u=312 v=248 area=1840
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Giá trị cụ thể sẽ khác tùy vị trí: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; tăng khi thẻ dịch sang phải, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;area&lt;/code&gt; tăng khi thẻ tiến lại gần. Nếu kết quả là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;centroid none area=0&lt;/code&gt;, hãy kiểm tra lại ánh sáng rồi mới chỉnh ngưỡng HSV. Nếu máy không có camera, dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error: cannot open /dev/video0&lt;/code&gt; là kết quả ghi nhận hợp lệ — tuyệt đối không bịa số liệu trọng tâm.&lt;/p&gt;

&lt;p&gt;Khi chuyển thành node ROS về sau, timer sẽ quyết định ở tần số 5–10 Hz: nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;area &amp;lt; 500&lt;/code&gt; thì phát &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; bằng 0; nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; nằm bên trái tâm thì đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; khoảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.3&lt;/code&gt;; nếu nằm bên phải thì khoảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-0.3&lt;/code&gt;, rồi giữ nguyên lệnh tới chu kỳ kế tiếp.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-khoảng-cách-tới-một-thẻ-apriltag-bằng-mô-hình-camera-lỗ-kim&quot;&gt;Giải thích: Tính tay khoảng cách tới một thẻ AprilTag bằng mô hình Camera Lỗ kim&lt;/h3&gt;

&lt;p&gt;Giả sử bạn đã hiệu chuẩn camera và thu được tiêu cự ngang \(f_x = 600\text{ pixel}\), tâm ảnh \(c_x = 320\text{ pixel}\). Bạn đặt một thẻ &lt;strong&gt;AprilTag cạnh vuông thực tế \(W = 0{,}10\text{ m}\)&lt;/strong&gt; (10 cm) đối diện thẳng góc với camera, và đo được trên ảnh thẻ chiếm bề rộng \(w_{\text{px}} = 75\text{ pixel}\), tâm thẻ nằm ở cột pixel \(u = 380\):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tính độ sâu \(Z\) (khoảng cách từ camera tới thẻ):&lt;/strong&gt;
\(Z = \frac{f_x \cdot W}{w_{\text{px}}} = \frac{600 \times 0{,}10}{75} = \mathbf{0{,}80\text{ m}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính độ lệch ngang \(X\) của tâm thẻ so với trục quang camera:&lt;/strong&gt;
\(X = \frac{(u - c_x) \cdot Z}{f_x} = \frac{(380 - 320) \times 0{,}80}{600} = \frac{48}{600} = \mathbf{+0{,}08\text{ m}}\) (lệch sang phải 8 cm!).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Chỉ bằng hai phép chia hình học đồng dạng cực gọn của mô hình lỗ kim, một camera RGB bình thường (không cần cảm biến độ sâu đắt tiền) đã đo được chính xác tọa độ 3D của mục tiêu để dẫn robot tự động tiến vào trạm sạc!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Lưu đoạn mã trên thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;card_centroid.py&lt;/code&gt; và chạy thử; đối chiếu kết quả theo đúng tình trạng phần cứng của bạn:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Có camera và phát hiện được thẻ:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;centroid u=312 v=248 area=1840
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Có camera nhưng mặt nạ rỗng:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;centroid none area=0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Không tìm thấy thiết bị camera:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;error: cannot open /dev/video0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trong trường hợp không có thiết bị, chạy thêm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls /dev/video*&lt;/code&gt; và dán thông báo lỗi của shell vào sổ. Sau đó viết bốn câu trình bày rõ toàn bộ pipeline (ánh sáng, thiết bị, phân ngưỡng, trọng tâm) và giải thích vì sao không nên xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; ở trọn tốc độ khung hình.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error: cannot open /dev/video0&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Không có camera UVC, hoặc dùng nhầm cáp chỉ sạc&lt;/td&gt;
      &lt;td&gt;Kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls /dev/video*&lt;/code&gt; và đổi cổng USB&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Camera mở được nhưng luôn báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;centroid none&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Ngưỡng hue không hợp với ánh sáng hoặc thẻ ngoài khung hình&lt;/td&gt;
      &lt;td&gt;Kiểm tra độ tương phản thẻ bằng mắt trước khi chỉnh cận&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trọng tâm nhảy hàng chục pixel dù thẻ đứng yên&lt;/td&gt;
      &lt;td&gt;Ánh sáng trộn lẫn (nắng + đèn) hoặc vùng mặt nạ quá nhỏ&lt;/td&gt;
      &lt;td&gt;Dùng một nguồn sáng ổn định và tăng ngưỡng diện tích&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ rung mạnh khi nối với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Xuất bản lệnh theo trọn tốc độ khung hình 30 Hz&lt;/td&gt;
      &lt;td&gt;Dùng timer 5–10 Hz, mỗi nhịp gửi một lệnh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thẻ dịch sang phải ngoài đời nhưng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; giảm&lt;/td&gt;
      &lt;td&gt;Ảnh bị lật gương hoặc đảo dấu góc yaw&lt;/td&gt;
      &lt;td&gt;Đảm bảo trục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; tăng dần từ trái sang phải trên mảng ảnh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tính toán hướng nhắm theo trục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Nhầm quy ước trục (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; quang học hướng xuống, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; thân hướng lên)&lt;/td&gt;
      &lt;td&gt;Đối chiếu chuẩn REP-103 phân biệt hệ quang học và hệ thân&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-camera-ở-việt-nam&quot;&gt;Mua camera ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Camera chiều sâu (như RealSense) nằm ngoài yêu cầu của bài. Bạn chỉ cần tìm trên Shopee với từ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera USB UVC&lt;/code&gt; (giá tham khảo khoảng 150.000–450.000 đồng, lưu ý tránh cáp chỉ sạc). Một lựa chọn khác cho Raspberry Pi là camera dùng cảm biến IMX219 (ví dụ Waveshare IMX219-77 từng niêm yết khoảng 432.000 đồng, cần kiểm tra lại giá hiện tại). Cả hai loại đều dùng tốt cho script OpenCV và topic ảnh ROS.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=sd25t4HmFdU&quot;&gt;Control your Webcam with Python - OpenCV Beginners Tutorial #4&lt;/a&gt; — Sam Westby Tech (tiếng Anh). Hướng dẫn đọc khung hình từ webcam bằng OpenCV — bước khởi đầu trực tiếp cho pipeline trong bài.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=H5qbRTikxI4&quot;&gt;OpenCV Python Camera Calibration (Intrinsic, Extrinsic, Distortion)&lt;/a&gt; — Kevin Wood&lt;/td&gt;
          &lt;td&gt;Robotics &amp;amp; AI (tiếng Anh). Giới thiệu ma trận nội tại và méo ống kính (chỉ cần nắm khái niệm, chưa cần chạy hiệu chỉnh trong bài này).&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;h3 id=&quot;bài-1&quot;&gt;Bài 1&lt;/h3&gt;

&lt;p&gt;Tấm thẻ màu biến mất khỏi mặt nạ mỗi khi mở cửa sổ và xuất hiện lại khi kéo rèm, dù giữ nguyên cận HSV. Bạn cần xử lý tầng nào trước và giữ nguyên tầng nào?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Tầng ánh sáng đứng đầu tiên — điều chỉnh nguồn sáng trong phòng trước, không vội sửa công thức tính mô-men.&lt;/p&gt;

&lt;h3 id=&quot;bài-2&quot;&gt;Bài 2&lt;/h3&gt;

&lt;p&gt;Một mặt nạ chỉ có bốn điểm ảnh trắng tại \((10, 10)\), \((14, 10)\), \((12, 16)\) và \((12, 12)\) trên ảnh rộng 64 pixel. Tính \(c_x\), \(c_y\) và cho biết vùng thẻ nằm bên trái hay bên phải tâm ảnh.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Lấy trung bình cộng bốn hoành độ và bốn tung độ, sau đó so sánh \(c_x\) với tâm ảnh là 32.&lt;/p&gt;

&lt;h3 id=&quot;bài-3&quot;&gt;Bài 3&lt;/h3&gt;

&lt;p&gt;Camera thu 30 khung hình/giây, nhưng vì sao cầu nối chỉ nên nhận lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; mới ở nhịp 5–10 Hz? Giải thích gắn với phần cứng xe Capstone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Giảm rung lắc cho hộp số động cơ và giúp dễ chia sẻ topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; với bộ điều hướng về sau nhờ dùng timer cố định.&lt;/p&gt;

&lt;h3 id=&quot;bài-4&quot;&gt;Bài 4&lt;/h3&gt;

&lt;p&gt;Nêu hướng của ba trục tọa độ trong hệ quang học và trong hệ thân theo REP-103. Hệ nào phải được ghi vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; của ảnh?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Hệ quang học: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; hướng ra khỏi ống kính, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; sang phải, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; xuống dưới. Hệ thân: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; hướng trước, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; sang trái, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; lên trên. Ảnh luôn gắn với hệ quang học.&lt;/p&gt;

&lt;h3 id=&quot;bài-5&quot;&gt;Bài 5&lt;/h3&gt;

&lt;p&gt;Khi script báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error: cannot open /dev/video0&lt;/code&gt;, hãy liệt kê ba bước kiểm tra có thể làm ngay mà không cần mua camera mới, đồng thời nêu phần bài nộp dạng văn bản vẫn cần hoàn thành.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls /dev/video*&lt;/code&gt;, thử cổng USB khác, ghi lại chính xác thông báo lỗi, và hoàn thiện bốn câu mô tả pipeline theo yêu cầu lab.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html&quot;&gt;Bài ngưỡng của OpenCV&lt;/a&gt; — hướng dẫn phân đoạn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inRange&lt;/code&gt; và kiểm tra trực quan mặt nạ trước khi tính trọng tâm.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ros-drivers/usb_cam&quot;&gt;usb_cam&lt;/a&gt; — tài liệu về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam_node_exe&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera.launch.py&lt;/code&gt; cho camera V4L2 trên ROS 2.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; — chuẩn phân tách hệ trục thân và hệ trục quang học của ảnh.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Tour stack Nav2</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_02_nav2_tour/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_02_nav2_tour</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~55 phút&lt;/strong&gt; cho phần trải nghiệm mô phỏng trên máy mở được RViz, hoặc khoảng 40 phút nếu bạn chỉ có terminal và dừng lại sau khi ghi nhận lý do launch giao diện không chạy được.&lt;/p&gt;

&lt;p&gt;Có tấm bản đồ đẹp trong tay mới chỉ là bước một: làm sao robot biết đường đi ngắn nhất từ cửa phòng lab sang hành lang B, đồng thời tự lách qua một chiếc ghế ai đó vừa kéo ra giữa lối đi và lùi lại xoay đầu nếu bị kẹt góc? &lt;strong&gt;Nav2 (Navigation 2)&lt;/strong&gt; là kiến trúc điều hướng tự hành tiêu chuẩn của ROS 2, phối hợp nhịp nhàng giữa định vị (AMCL), lập kế hoạch toàn cục, bộ điều khiển cục bộ và cây hành vi (Behavior Tree).&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-ngăn-xếp-điều-hướng-nav2&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của Ngăn xếp Điều hướng Nav2&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Định vị trên Bản đồ có sẵn (AMCL — Bộ lọc hạt Monte Carlo)&lt;/strong&gt;:
Thả một đám mây hàng trăm “hạt giả thuyết” tư thế \((x, y, \theta)\) trên bản đồ, đối chiếu tia LiDAR thực tế với bản đồ để giữ lại các hạt khớp nhất và hội tụ đúng vị trí robot.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bản đồ Chi phí Toàn cục &amp;amp; Cục bộ (Global / Local Costmap + Inflation Radius)&lt;/strong&gt;:
Phồng dày tường và vật cản thêm một lớp đệm an toàn (&lt;em&gt;inflation layer&lt;/em&gt;) có chi phí giảm dần theo khoảng cách, giúp robot vừa không quẹt thân xe vừa không đi sát mép tường.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hai tầng Lập kế hoạch (Global Planner vs. Local Controller)&lt;/strong&gt;:
&lt;em&gt;Global Planner&lt;/em&gt; (NavFn/Smac A&lt;em&gt;) tìm đường đi tổng thể trên bản đồ lớn; *Local Controller&lt;/em&gt; (DWB / Regulated Pure Pursuit / MPPI) bám đường đó ở tần số 10–20 Hz, tính đến giới hạn vận tốc/gia tốc và vật cản động mới xuất hiện.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hành vi Phục hồi (Recovery Behaviors) dưới điều phối của Behavior Tree&lt;/strong&gt;:
Khi bị kẹt không tiến được, Nav2 tự động xóa costmap cục bộ, xoay tại chỗ quan sát lại hoặc lùi chậm một đoạn thay vì bỏ cuộc ngay lập tức.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ gọi đúng tên năm thành phần chính nằm giữa mục tiêu di chuyển (goal) và lệnh vận tốc, chỉ rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; là giao diện duy nhất Nav2 cung cấp cho lớp điều khiển động cơ, chạy được bản mô phỏng TurtleBot3 trên Jazzy (gồm bước gán tư thế ban đầu) và hiểu vì sao việc mô phỏng chạy thành công không đồng nghĩa xe Capstone thật đã sẵn sàng tự hành. Bạn cũng sẽ chẩn đoán được nguyên nhân khiến costmap trống hoặc hành vi sai lệch do thiếu odometry, sai góc lắp laser, lệch QoS, sai footprint hoặc xuất hiện thành phần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; bất thường.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Hoàn thành bài 11-01 để nắm rõ vai trò ba frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, đồng thời hiểu cơ chế cầu nối Capstone ở chương 10 (đăng ký &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; rồi quy đổi ra lệnh động cơ). Bài thực hành này không cần lidar thật trên bàn.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Trong lộ trình từ khung xe Capstone lên ROS 2 Jazzy, qua Gazebo rồi đến tự hành, tầng tự hành đóng vai trò nhận một mục tiêu vị trí trên bản đồ và xuất ra lệnh vận tốc phù hợp — đó chính là hệ thống Nav2. Nav2 không thay thế cầu nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; và hoàn toàn không biết chân điều khiển phần cứng của động cơ. Thay vì cấu hình ngay cho xe tự chế, ta sẽ quan sát toàn bộ hệ thống vận hành trên mô hình TurtleBot3 đã được dự án Nav2 cấu hình sẵn trong mô phỏng, và chỉ viết tệp tham số cho xe Capstone khi bản đồ, dữ liệu quét và odometry thực tế đã đầy đủ.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Nav2 không phải một node đơn lẻ mà là một tập hợp các mô-đun phối hợp qua action và topic. Theo kiến trúc chuẩn trên Jazzy, hệ thống gồm năm khối chính: bộ điều phối dùng cây hành vi (behavior-tree navigator), bộ lập kế hoạch toàn cục (planner), bộ điều khiển bám quỹ đạo (controller), các bản đồ chi phí (costmap) và các hành vi phục hồi (recovery).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_nav2.png&quot; alt=&quot;Nav2: goal vào navigator behavior tree, planner và controller hỏi costmap, một Twist đi ra phía đế&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Behavior-tree navigator&lt;/strong&gt; đóng vai trò điều phối trung tâm: tiếp nhận tư thế đích (thường gửi từ RViz) và quyết định khi nào cần tính lại đường đi, khi nào bám quỹ đạo và khi nào kích hoạt hành vi phục hồi nếu bị kẹt. Bài 11-04 sẽ đi sâu vào cấu trúc cây hành vi này; hiện tại bạn chỉ cần biết đây là thành phần giúp robot không bị đứng im mãi khi gặp vật cản bất ngờ.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Planner&lt;/strong&gt; tính toán tuyến đường toàn cục trên costmap toàn cục (thường dùng plugin Smac hoặc NavFn truyền thống). &lt;strong&gt;Controller&lt;/strong&gt; bám theo tuyến đường đó trên costmap cục bộ; trên Jazzy phổ biến là DWB (chấm điểm tập quỹ đạo ngắn) và MPPI (lấy mẫu nhiều phương án vận tốc). Cả hai đều xuất ra lệnh vận tốc, không phát trực tiếp tín hiệu PWM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Costmap&lt;/strong&gt; là lưới biểu diễn mức độ nguy hiểm tại từng ô xung quanh robot, được cập nhật từ bản đồ tĩnh và luồng cảm biến. Vì dữ liệu quét laser thường phát theo QoS &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt;, một subscriber đòi hỏi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt; sẽ không kết nối được, dẫn đến costmap trống rỗng dù Gazebo vẫn vẽ tia laser (xem thêm quy tắc tương thích tại &lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Quality-of-Service-Settings.html&quot;&gt;QoS của Jazzy&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Đầu ra của controller là message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; gồm sáu thành phần:&lt;/p&gt;

\[v_x,\; v_y,\; v_z,\; \omega_x,\; \omega_y,\; \omega_z.\]

&lt;p&gt;Khung xe vi sai Capstone chỉ sử dụng \(v_x\) và \(\omega_z\); vận tốc ngang \(v_y\) phải luôn bằng 0 vì xe không thể trượt ngang. Nếu cấu hình nhầm sang bộ điều khiển holonomic (dành cho xe bánh mecanum) sinh ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; khác 0, cầu nối sẽ không có cách nào thực thi trung thực — cần cấu hình controller cho xe non-holonomic và coi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; khác 0 là lỗi cấu hình.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recovery&lt;/strong&gt; là tập hợp các thao tác xử lý sự cố: khi controller báo không thể tiến tiếp, cây hành vi có thể yêu cầu robot xoay tại chỗ, lùi lại một đoạn ngắn hoặc xóa các điểm cản cũ trên costmap. Bạn sẽ thấy chúng tự động kích hoạt trong mô phỏng nếu đặt mục tiêu sau khe hẹp.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Footprint&lt;/strong&gt; khai báo kích thước hình học bao quanh thân xe cho costmap: footprint quá lớn sẽ khiến robot từ chối đi qua cửa hẹp; góc yaw của laser lắp ngược \(180^\circ\) sẽ vẽ vật cản ra phía sau lưng robot; còn thiếu odometry khiến bộ điều khiển cục bộ không theo dõi được dịch chuyển thực tế dù đường đi toàn cục trông vẫn hợp lý.&lt;/p&gt;

&lt;p&gt;Toàn bộ stack Nav2 dừng lại ở topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;: mọi chi tiết về mạch cầu H hay giao thức serial đều nằm tách biệt bên trong cầu nối của bạn. Về phần cảm biến, bản mô phỏng TurtleBot3 đã tự sinh luồng quét laser giả lập; chỉ khi đưa Nav2 lên khung xe thật về sau bạn mới cần lidar 360° (như dòng LD19 nêu ở bài 11-01), còn cảm biến ToF đơn điểm vẫn không thể lấp đầy costmap thay cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Thực hiện theo &lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/quickstart/quickstart/&quot;&gt;Quickstart Jazzy&lt;/a&gt; để cài Nav2 và khởi chạy mô phỏng TurtleBot3 (xem đây là chuyến tham quan kiến trúc, chưa cần tinh chỉnh tham số):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-navigation2 ros-jazzy-nav2-bringup
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 launch nav2_bringup tb3_simulation_launch.py headless:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lưu ý tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless&lt;/code&gt; mặc định là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;True&lt;/code&gt; trong tài liệu gốc; ta bắt buộc đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless:=False&lt;/code&gt; để mở giao diện 3D Gazebo song song với RViz nhằm đối chiếu trực tiếp chuyển động của robot với bản đồ.&lt;/p&gt;

&lt;p&gt;Khi cửa sổ RViz và Gazebo đã mở, nếu bảng điều khiển Nav2 chưa kích hoạt, hãy bấm &lt;strong&gt;Startup&lt;/strong&gt; để bản đồ hiển thị.&lt;/p&gt;

&lt;p&gt;Ban đầu robot chưa biết vị trí của mình trên bản đồ. Bấm &lt;strong&gt;2D Pose Estimate&lt;/strong&gt;, nhấp vào vị trí tương ứng với chỗ robot đang đứng trong Gazebo rồi kéo mũi tên theo hướng mặt xe để AMCL hoàn thiện cây TF. Tiếp theo, bấm nút chọn mục tiêu (&lt;strong&gt;Nav2 Goal&lt;/strong&gt; / &lt;em&gt;Navigation2 Goal&lt;/em&gt; trong tài liệu quickstart) và chọn một vị trí trống trên bản đồ. Mở terminal thứ hai để quan sát lệnh vận tốc:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /cmd_vel
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Khi robot bắt đầu di chuyển, bạn sẽ thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và/hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; khác 0 trong khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; luôn giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0&lt;/code&gt;. Chính luồng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; này là thứ cầu nối sẽ dịch thành lệnh động cơ trên xe thật. Không nối cầu Capstone vào bản mô phỏng TurtleBot3 và chưa cần chỉnh độ phân giải costmap hay hệ số inflation — khi robot tới gần đích là hoàn tất phần tham quan.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-bán-kính-phồng-inflation-radius-để-robot-đi-lọt-cửa-hẹp-080-m&quot;&gt;Giải thích: Tính tay Bán kính Phồng (Inflation Radius) để robot đi lọt cửa hẹp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,80 m&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Giả sử xe Capstone có hình tròn bao ngoài bán kính \(R_{\text{robot}} = \mathbf{0{,}18\text{ m}}\) (đường kính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,36 m&lt;/code&gt;), cần đi qua một khung cửa rộng &lt;strong&gt;\(W_{\text{cửa}} = 0{,}80\text{ m}\)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Vùng va chạm chết (Inscribed lethal zone):&lt;/strong&gt; Mọi ô cách tường \(\le 0{,}18\text{ m}\) có chi phí &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LETHAL (254)&lt;/code&gt; — tâm xe tuyệt đối không được chạm vào.&lt;/li&gt;
  &lt;li&gt;Khoảng trống hành lang an toàn còn lại cho tâm xe lọt qua cửa:
\(W_{\text{lọt}} = W_{\text{cửa}} - 2 \times R_{\text{robot}} = 0{,}80 - 2(0{,}18) = \mathbf{0{,}44\text{ m}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chọn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inflation_radius&lt;/code&gt; hợp lý:&lt;/strong&gt; Phải chọn lớn hơn \(R_{\text{robot}}\) (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,30 m&lt;/code&gt;) với hệ số suy giảm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cost_scaling_factor&lt;/code&gt; đủ dốc để giữa cửa vẫn có rãnh chi phí thấp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lỗi kinh điển:&lt;/strong&gt; Nếu đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;footprint&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_radius&lt;/code&gt; quá lớn (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,42 m&lt;/code&gt;), thì \(2 \times 0{,}42 = 0{,}84\text{ m} &amp;gt; 0{,}80\text{ m}\) → hai lớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LETHAL&lt;/code&gt; từ hai mép cửa chạm nhau bịt kín lối đi, khiến Global Planner báo &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Failed to make a plan&lt;/code&gt;&lt;/strong&gt; dù ngoài đời mắt thường thấy cửa còn rộng thênh thang!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Chạy ba câu lệnh trong mục Ví dụ trên phiên desktop. Kết quả mong đợi gồm hình ảnh mô phỏng và một mẫu dữ liệu topic:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Cả hai cửa sổ Gazebo và RViz đều mở nhờ cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless:=False&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Sau khi bấm Startup (nếu cần), bản đồ hiển thị rõ trong RViz.&lt;/li&gt;
  &lt;li&gt;Sau khi đặt &lt;strong&gt;2D Pose Estimate&lt;/strong&gt;, mô hình robot định vị đúng trên bản đồ kèm đám mây hạt AMCL.&lt;/li&gt;
  &lt;li&gt;Sau khi gửi goal, robot tự hành về đích và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /cmd_vel&lt;/code&gt; in ra mẫu có dạng tương tự:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;linear:
  x: 0.15
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Các con số thực tế sẽ thay đổi theo quỹ đạo, nhưng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; luôn bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0&lt;/code&gt; với mô hình vi sai này. Dán một mẫu echo thực tế vào sổ thực hành.&lt;/p&gt;

&lt;p&gt;Nếu máy không có màn hình hoặc launch lỗi trước khi mở RViz, hãy dán thông báo lỗi vào sổ và vẫn hoàn thành phần liệt kê năm khối Nav2 cùng vai trò của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;. Ghi nhớ rằng xe Capstone thật vẫn cần đủ bản đồ, lidar, odometry, footprint và cầu nối mới có thể tự hành.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Không mở cửa sổ 3D Gazebo&lt;/td&gt;
      &lt;td&gt;Tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless&lt;/code&gt; vẫn để mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;True&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Chạy lại kèm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless:=False&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Gửi goal nhưng robot đứng yên, cây TF chưa đủ&lt;/td&gt;
      &lt;td&gt;Chưa gán tư thế ban đầu&lt;/td&gt;
      &lt;td&gt;Thực hiện &lt;strong&gt;2D Pose Estimate&lt;/strong&gt; trước khi gửi goal&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Có đường toàn cục nhưng costmap cục bộ trống&lt;/td&gt;
      &lt;td&gt;Thiếu odometry hoặc lệch QoS giữa scan &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt; và subscriber &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /odom&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /scan -v&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot đâm xuyên tường hoặc từ chối đi qua cửa rộng&lt;/td&gt;
      &lt;td&gt;Sai góc yaw của frame laser hoặc footprint không khớp kích thước thật&lt;/td&gt;
      &lt;td&gt;Kiểm tra lại frame laser trong URDF và đa giác footprint&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Xuất hiện lệnh yêu cầu trượt ngang&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; khác 0 trên xe vi sai&lt;/td&gt;
      &lt;td&gt;Cấu hình lại controller giữ \(v_y = 0\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mô phỏng chạy bình thường nhưng bánh Capstone thật không quay&lt;/td&gt;
      &lt;td&gt;Nav2 không điều khiển trực tiếp PWM&lt;/td&gt;
      &lt;td&gt;Cần chạy riêng node cầu nối phần cứng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Hai tài liệu &lt;a href=&quot;https://docs.nav2.org/jazzy/configuration_and_development/first_time_robot_setup_guide/&quot;&gt;Hướng dẫn robot lần đầu&lt;/a&gt; và &lt;a href=&quot;https://docs.nav2.org/jazzy/configuration_and_development/first_time_robot_setup_guide/urdf/setup_urdf/&quot;&gt;trang URDF&lt;/a&gt; sẽ dùng khi tích hợp khung xe của bạn ở bước sau, chưa cần làm trong buổi này.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=jkoGkAd0GYk&quot;&gt;Making robot navigation easy with Nav2 and ROS!&lt;/a&gt; — Articulated Robotics (tiếng Anh). Tổng quan cách bản đồ, costmap và goal phối hợp trong Nav2, khớp với nội dung bài học.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=hMTxb8Y2cxI&quot;&gt;ROS2 Nav2 - Generate a Map with slam_toolbox&lt;/a&gt; — Robotics Back-End (tiếng Anh). Minh họa cách dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; dựng bản đồ làm đầu vào cho Nav2 (lưu ý dùng gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; của Jazzy).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1391050009535827&quot;&gt;Thuật toán về nhà&lt;/a&gt; — Facebook Reel của Hoàng Quân (Cơ điện tử ICTU, &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;), sinh viên Cơ điện tử ICTU thử nghiệm thuật toán quay về vị trí xuất phát. Trong Nav2, “về nhà” đơn giản là một goal cụ thể trên bản đồ chứ không cần tạo topic riêng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;h3 id=&quot;bài-1&quot;&gt;Bài 1&lt;/h3&gt;

&lt;p&gt;Liệt kê năm khối chính của Nav2 đã học và nêu kiểu message mà controller xuất ra. Tiến trình nào (của bạn hay của Nav2) chịu trách nhiệm chuyển message đó thành tín hiệu PWM?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Năm khối gồm behavior-tree navigator, planner, controller, costmap và recovery; kiểu message là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt;; việc chuyển sang PWM thuộc về node cầu nối của bạn.&lt;/p&gt;

&lt;h3 id=&quot;bài-2&quot;&gt;Bài 2&lt;/h3&gt;

&lt;p&gt;Bạn bấm chọn goal nhưng robot mô phỏng đứng yên và bạn chưa từng dùng &lt;strong&gt;2D Pose Estimate&lt;/strong&gt;. Cạnh TF nào đang thiếu và thao tác nào khắc phục ngay mà không cần sửa YAML?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; AMCL chưa thể xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt; đáng tin cậy khi chưa có ước lượng vị trí ban đầu; hãy đặt &lt;strong&gt;2D Pose Estimate&lt;/strong&gt; rồi gửi lại goal.&lt;/p&gt;

&lt;h3 id=&quot;bài-3&quot;&gt;Bài 3&lt;/h3&gt;

&lt;p&gt;Một tệp tham số sao chép từ mạng tạo ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y = 0.2&lt;/code&gt; khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x = 0.1&lt;/code&gt;. Vì sao khung xe vi sai Capstone không thể thực thi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; này và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; cần mang giá trị nào?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Xe vi sai không có khả năng trượt ngang; lệnh hợp lệ phải giữ \(v_y = 0\) và dùng vận tốc góc yaw để đổi hướng.&lt;/p&gt;

&lt;h3 id=&quot;bài-4&quot;&gt;Bài 4&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /scan&lt;/code&gt; hiển thị đầy đủ dữ liệu khoảng cách, nhưng costmap cục bộ vẫn trống do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; dùng QoS tương thích &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt; còn node costmap để mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt;. Mô tả ngắn gọn nguyên nhân kỹ thuật mà không đề cập đến driver động cơ.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Chính sách &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliability&lt;/code&gt; thuộc về từng cặp publisher – subscription; một subscriber &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt; không thể nhận dữ liệu từ publisher &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt;, nên cần điều chỉnh QoS của costmap.&lt;/p&gt;

&lt;h3 id=&quot;bài-5&quot;&gt;Bài 5&lt;/h3&gt;

&lt;p&gt;Trong một đoạn ngắn, phân biệt rõ những gì chuyến tham quan TurtleBot3 đã chứng minh với những gì nó chưa chứng minh đối với xe Capstone của bạn (nhắc rõ vai trò cầu nối).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Đã chứng minh: quy trình cài đặt Nav2, gán tư thế ban đầu, gửi goal và sinh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; với mô hình có sẵn. Chưa chứng minh: bản đồ, lidar, footprint và cầu nối động cơ của chính xe Capstone.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/&quot;&gt;Nav2 getting started cho Jazzy&lt;/a&gt; — mục lục dẫn tới quickstart và tài liệu cây hành vi.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/quickstart/quickstart/&quot;&gt;Quickstart&lt;/a&gt; — hướng dẫn chạy mô phỏng TurtleBot3 (lưu ý &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless&lt;/code&gt; mặc định là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/configuration_and_development/first_time_robot_setup_guide/&quot;&gt;First-time robot setup&lt;/a&gt; — tài liệu tích hợp Nav2 cho robot tự chế.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Quality-of-Service-Settings.html&quot;&gt;QoS&lt;/a&gt; — quy tắc tương thích giữa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/ros_installation/&quot;&gt;Gazebo Harmonic với ROS&lt;/a&gt; — tài liệu kiểm tra nếu bản thân gói mô phỏng Jazzy không khởi chạy được.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Tổng quan SLAM cho người mới</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_01_slam_overview/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter11/11_01_slam_overview</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~70 phút&lt;/strong&gt; nếu máy đã cài và nạp sẵn ROS 2 Jazzy, hoặc khoảng 50 phút nếu bạn dừng ở bước kiểm tra giao diện do chưa có lidar thật trên bàn.&lt;/p&gt;

&lt;p&gt;Hãy tưởng tượng bạn bị bịt mắt thả vào một căn phòng lạ hoàn toàn mới, trong tay chỉ có cây gậy dò khoảng cách. Muốn biết mình đang đứng ở đâu, bạn cần một tấm bản đồ; nhưng muốn vẽ được tấm bản đồ chính xác, bạn lại phải biết mình đang đứng ở đâu! Nghịch lý “con gà và quả trứng” nổi tiếng đó chính là bài toán &lt;strong&gt;SLAM (Simultaneous Localization and Mapping — Định vị và Xây dựng Bản đồ Đồng thời)&lt;/strong&gt;, viên gạch nền của mọi robot tự hành hiện đại.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-của-bài-toán-slam-2d-slam_toolbox&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột của Bài toán SLAM 2D (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt;)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Khớp quét (Scan Matching — Front-end)&lt;/strong&gt;:
So khớp hai lát cắt LiDAR liên tiếp (ICP hoặc Correlative Scan Matching) để ước lượng độ dịch chuyển thực tế của xe, sửa sai số trượt bánh của odometry.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đồ thị Tư thế (Pose Graph) &amp;amp; Tối ưu hóa Back-end&lt;/strong&gt;:
Mỗi vị trí robot là một nút (node), mỗi phép đo odometry hay khớp quét là một cạnh lò xo (constraint) nối hai nút. Khi robot đi một vòng lớn, sai số góc nhỏ tích tụ khiến tường vẽ bị lệch đôi.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đóng vòng lặp (Loop Closure)&lt;/strong&gt;:
Khi robot quay lại một góc hành lang từng đi qua, SLAM nhận diện sự trùng khớp hình học, thêm một “cạnh ràng buộc dài” nối quá khứ với hiện tại và tối ưu lại toàn bộ đồ thị tư thế để kéo bản đồ khít lại làm một!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bản đồ Lưới chiếm dụng (Occupancy Grid Map — Log-Odds)&lt;/strong&gt;:
Biểu diễn mặt bằng dưới dạng lưới ô vuông (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5 cm × 5 cm&lt;/code&gt;), mỗi ô lưu xác suất bị vật cản chiếm chỗ (đen = tường, trắng = trống, xám = chưa biết) cập nhật bằng công thức cộng &lt;em&gt;log-odds&lt;/em&gt; siêu nhanh.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ vẽ được chuỗi ba cạnh TF mà &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; yêu cầu, liệt kê đúng các topic phải tồn tại trước khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;online_async_launch.py&lt;/code&gt;, phân biệt được bản đồ hành lang bị cong do lỗi encoder với khu vực chưa được quét, và giải thích rõ vì sao cảm biến đơn điểm TF-Luna không thể cung cấp dữ liệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn sử dụng thành thạo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info&lt;/code&gt; và nắm rõ cấu trúc cây tọa độ TF. Từ phần thực hành xe Capstone, bạn hiểu cách encoder tạo ra odometry và biết rằng chỉ cần một dấu dây bị đảo ngược là lệnh đi thẳng sẽ bị hiểu nhầm thành lệnh quay. Trong bài này, Gazebo chỉ đóng vai trò minh họa nguồn dữ liệu laser giả lập.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Lộ trình của khóa đi tuần tự từ khung xe vi sai Capstone, lên ROS 2 Jazzy, qua mô phỏng Gazebo rồi mới đến tự hành. Bản đồ là sản phẩm tự hành đầu tiên vượt ra ngoài việc gửi lệnh trực tiếp tới động cơ. Khi có dữ liệu lidar phẳng 2D, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; vừa ước lượng vị trí robot vừa dựng bản đồ tường xung quanh. Gazebo có thể tạo luồng quét giả lập, còn xe Capstone cơ bản thì chưa có; nếu thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt; hợp lệ và odometry chính xác, việc khởi chạy bộ dựng bản đồ chỉ báo thiếu giao diện. Nav2 ở bài sau sẽ dựa trên bản đồ này để dẫn đường chứ không tự vẽ bản đồ.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;SLAM (Simultaneous Localization and Mapping) trong phạm vi bài này là bài toán định vị và dựng bản đồ đồng thời trên mặt phẳng 2D. Lidar đo khoảng cách theo nhiều góc quét quanh robot, còn odometry bánh xe ước lượng độ dịch chuyển của robot giữa hai khung quét liên tiếp. Bộ dựng bản đồ sẽ ghép các khung quét thành bản đồ lưới, đồng thời xuất bản phép hiệu chỉnh để bản đồ không bị méo dần do sai số tích lũy của encoder.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_slam.png&quot; alt=&quot;SLAM mặt phẳng: một vòng quét lidar dựng bản đồ, biến đổi map tới odom kéo lại độ trôi của bánh xe&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Chuẩn &lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; tách biệt ba hệ tọa độ chính: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; được phép nhảy đột ngột khi khép vòng (loop closure) để hiệu chỉnh sai số; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; (tích phân từ bánh xe) được phép trôi dần nhưng không được nhảy gián đoạn để tránh làm giật bộ điều khiển bám quỹ đạo; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; gắn cố định vào thân xe. Trong kiến trúc này, SLAM xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt;, odometry của robot xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;, còn URDF xuất bản phép biến đổi tĩnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → khung laser&lt;/code&gt; (driver lidar không tự định nghĩa vị trí giá đỡ).&lt;/p&gt;

&lt;p&gt;Tổng hợp lại, tư thế của thân robot trong hệ tọa độ bản đồ là:&lt;/p&gt;

\[T_{\mathrm{map}\rightarrow\mathrm{base}} = T_{\mathrm{map}\rightarrow\mathrm{odom}} \, T_{\mathrm{odom}\rightarrow\mathrm{base}}.\]

&lt;p&gt;Nếu phép biến đổi ở giữa (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;) bị sai lệch nghiêm trọng, phép hiệu chỉnh từ bản đồ khó có thể cứu vãn. Xét một ví dụ lỗi ngược dấu: giả sử khoảng cách hai bánh \(b = 0{,}20\) m, robot đi thẳng 1 m nhưng encoder bánh phải bị ngược dấu, khiến node odometry đọc được \(\Delta s_l = 1{,}0\) m và \(\Delta s_r = -1{,}0\) m. Theo mô hình vi sai tiêu chuẩn:&lt;/p&gt;

\[\Delta s = \frac{\Delta s_r + \Delta s_l}{2}, \qquad \Delta \theta = \frac{\Delta s_r - \Delta s_l}{b}.\]

&lt;p&gt;Thay số ta thu được \(\Delta s = 0\) và \(\Delta \theta = -10\) rad: robot thực tế đã đi thẳng dọc hành lang, nhưng odometry lại báo nó đang đứng yên xoay tại chỗ. Thuật toán ghép quét sẽ kéo bức tường xoay theo góc quay ảo này, khiến hành lang trên bản đồ bị uốn cong. Do đó luôn phải kiểm tra và sửa dấu encoder (hoặc thứ tự trái/phải) trước khi tinh chỉnh tham số SLAM. Ngược lại, lỗi hệ số tỉ lệ (cả hai bánh cùng sai một hệ số nhân) tạo ra biểu hiện khác: hành lang vẫn thẳng nhưng chiều dài trên bản đồ bị kéo giãn hoặc co ngắn so với thực tế.&lt;/p&gt;

&lt;p&gt;Message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt; chứa các trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angle_min&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angle_max&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angle_increment&lt;/code&gt;, giới hạn tầm đo, mảng khoảng cách &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ranges&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; đã khai báo trên cây TF. Một cảm biến lidar 360° với độ phân giải góc 0,5° cung cấp khoảng 720 điểm đo mỗi vòng quét. Trong khi đó, TF-Luna chỉ đo duy nhất một khoảng cách dọc một tia — phù hợp để phát hiện vật cản phía trước, nhưng không thể cấp dữ liệu đầu vào cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Cảm biến quán tính MPU6050 có thể hỗ trợ bộ lọc giữ ổn định góc yaw giữa các khung quét, nhưng không thể tự vẽ tường. Ngoài ra, driver lidar thường xuất bản theo chính sách QoS &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt;; nếu subscriber yêu cầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt;, hai bên sẽ không ghép nối được — hãy kiểm tra lại cấu hình QoS của Jazzy khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo&lt;/code&gt; vẫn có dữ liệu nhưng bộ dựng bản đồ không nhận được gì.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Cài gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; trên Jazzy, nạp môi trường và nắm cú pháp khởi chạy chuẩn:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-slam-toolbox
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 launch slam_toolbox online_async_launch.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lệnh này mở bộ dựng bản đồ ở chế độ trực tuyến bất đồng bộ (online async) với tham số mặc định. Nếu chưa có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; và cây TF hoàn chỉnh thì hệ thống không thể tạo bản đồ; khi thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;, thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt; hoặc thiếu frame laser, hãy dừng node thay vì tạo dữ liệu giả.&lt;/p&gt;

&lt;p&gt;Trước khi khởi chạy SLAM trên bất kỳ robot nào (kể cả khi tích hợp Capstone về sau), luôn thực hiện ba lệnh kiểm tra đầu vào:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 topic info /scan &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
ros2 topic info /odom &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
ros2 run tf2_ros tf2_echo odom base_link
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; phải có kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt; với ít nhất một publisher. Topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; phải có kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/msg/Odometry&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;child_frame_id&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;; khi đẩy thẳng robot, tọa độ tịnh tiến phải tăng tương ứng, còn khi xoay tại chỗ góc yaw phải thay đổi đúng chiều. Khi các điều kiện này thỏa mãn và SLAM hoạt động bình thường, chính SLAM sẽ tự động xuất bản thêm cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt; (kiểm tra bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run tf2_ros tf2_echo map odom&lt;/code&gt; — đây là đầu ra của SLAM, không phải cạnh bạn tự phát tĩnh).&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-cập-nhật-xác-suất-1-ô-lưới-occupancy-grid-bằng-log-odds&quot;&gt;Giải thích: Tính tay cập nhật xác suất 1 ô lưới Occupancy Grid bằng Log-Odds&lt;/h3&gt;

&lt;p&gt;Xét 1 ô lưới ban đầu chưa biết trạng thái (\(p_0 = 0{,}5 \implies \ell_0 = \ln\frac{0{,}5}{0{,}5} = \mathbf{0}\)). Giả sử mô hình cảm biến LiDAR cho:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Khi tia laser &lt;strong&gt;đập trúng ô&lt;/strong&gt; (occupied): cộng \(\ell_{\text{occ}} = +0{,}85\) (tương ứng \(p \approx 0{,}70\)).&lt;/li&gt;
  &lt;li&gt;Khi tia laser &lt;strong&gt;xuyên qua ô&lt;/strong&gt; (free): cộng \(\ell_{\text{free}} = -0{,}40\) (tương ứng \(p \approx 0{,}40\)).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Theo dõi 3 lần quét liên tiếp đi qua đúng ô đó:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Lần 1 (trúng vật cản):&lt;/strong&gt; \(\ell_1 = 0 + 0{,}85 = +0{,}85 \implies p_1 = \frac{1}{1 + e^{-0{,}85}} \approx \mathbf{0{,}70}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lần 2 (nhiễu giả, tia xuyên qua):&lt;/strong&gt; \(\ell_2 = 0{,}85 - 0{,}40 = +0{,}45 \implies p_2 = \frac{1}{1 + e^{-0{,}45}} \approx \mathbf{0{,}61}\) (không bị 1 tia nhiễu xóa sạch tường!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lần 3 (trúng lại vật cản):&lt;/strong&gt; \(\ell_3 = 0{,}45 + 0{,}85 = +1{,}30 \implies p_3 = \frac{1}{1 + e^{-1{,}30}} \approx \mathbf{0{,}79}\) (khẳng định chắc chắn là tường!).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Chính phép cộng tuyến tính log-odds này giúp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; cập nhật hàng chục nghìn ô lưới mỗi giây mà vẫn kháng nhiễu cực tốt!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Bài thực hành này tập trung vào kiểm tra giao ước đầu vào thay vì chạy đua dựng bản đồ. Chép danh sách sau vào sổ thực hành và đánh dấu từng mục theo tình trạng thực tế (&lt;strong&gt;có&lt;/strong&gt;, &lt;strong&gt;chưa có&lt;/strong&gt;, hoặc &lt;strong&gt;không khả thi với cảm biến hiện tại&lt;/strong&gt;):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; tồn tại với kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; khớp frame laser và mảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ranges&lt;/code&gt; quét theo một góc rộng chứ không chỉ một điểm.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; tồn tại với kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/msg/Odometry&lt;/code&gt;; khi đẩy thẳng robot, tọa độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; tăng lên mà không xuất hiện góc yaw ảo lớn.&lt;/li&gt;
  &lt;li&gt;Cây TF có cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt; từ robot và cạnh tĩnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → laser&lt;/code&gt; từ URDF; cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt; để trống cho SLAM đảm nhiệm.&lt;/li&gt;
  &lt;li&gt;Nếu cảm biến đo khoảng cách duy nhất bạn có là TF-Luna, ghi rõ “cảm biến này không thể cấp dữ liệu cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt;” rồi dừng lại, không chạy bộ dựng bản đồ trên cửa sổ trống.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thực hiện kiểm tra giao diện ngay cả khi bạn biết trước chưa có cảm biến chạy:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 topic info /scan &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
ros2 topic info /odom &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Kết quả mong đợi (và trung thực) trên máy chưa chạy driver lidar hoặc bag:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Unknown topic &apos;/scan&apos;
Unknown topic &apos;/odom&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ghi nguyên văn kết quả này vào sổ để xác nhận cả hai publisher đang vắng mặt. Nếu bạn đang chạy driver hoặc phát lại tệp bag, dán đầy đủ phần kiểu message và số lượng publisher vào cạnh từng mục tương ứng.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Hành lang trên bản đồ bị uốn cong&lt;/td&gt;
      &lt;td&gt;Ngược dấu encoder một bánh hoặc đấu nhầm trái/phải&lt;/td&gt;
      &lt;td&gt;Đẩy thẳng xe và quan sát góc yaw trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hành lang thẳng nhưng sai chiều dài&lt;/td&gt;
      &lt;td&gt;Sai bán kính bánh hoặc khoảng cách hai bánh&lt;/td&gt;
      &lt;td&gt;Đẩy xe đúng 1 m ngoài sàn và đối chiếu với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Launch báo lỗi TF, bản đồ trống&lt;/td&gt;
      &lt;td&gt;Thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;, thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;, hoặc thiếu frame laser&lt;/td&gt;
      &lt;td&gt;Chạy lại ba lệnh kiểm tra đầu vào ở trên&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; thấy dữ liệu quét nhưng SLAM không nhận&lt;/td&gt;
      &lt;td&gt;Lệch QoS (subscriber &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt;, publisher &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt;)&lt;/td&gt;
      &lt;td&gt;Kiểm tra bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /scan -v&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cảm biến chỉ trả về một giá trị khoảng cách&lt;/td&gt;
      &lt;td&gt;Đang dùng TF-Luna hoặc cảm biến ToF đơn điểm&lt;/td&gt;
      &lt;td&gt;Không dùng làm đầu vào cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-linh-kiện-ở-việt-nam&quot;&gt;Mua linh kiện ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Chỉ nên mua cảm biến quét khi bạn sẵn sàng thực hành dựng bản đồ. Các dòng lidar 2D phổ thông phù hợp sinh viên như LD19, LD06 hoặc YDLIDAR X2 có giá tham khảo khoảng 1,5–3,5 triệu đồng (tìm từ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lidar LD19 robot&lt;/code&gt; trên Shopee, đọc kỹ mô tả mới nhất và ưu tiên loại có tài liệu giao thức serial rõ ràng).&lt;/p&gt;

&lt;p&gt;Dòng &lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-dtof-lidar-rplidar-s2m1-r2e-30m-360-laser-range-scanner&quot;&gt;RPLIDAR S2 trên Hshop&lt;/a&gt; là phương án nâng cấp cao cấp hơn (quét 360°, kết nối Ethernet, tầm đo xa) nhưng giá thành cao, không phù hợp làm lựa chọn khởi đầu.&lt;/p&gt;

&lt;p&gt;Máy tính chạy thuật toán dựng bản đồ trên xe là Raspberry Pi 5 (&lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Trang Pi 5 của Hshop&lt;/a&gt;, giá tham khảo gần 2,4 triệu đồng cho bản RAM thấp — kiểm tra lại giá hiện tại), nhưng Pi không thể thay thế cảm biến lidar. Tham khảo &lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-dfrobot-tf-luna-tof-micro-single-point-ranging-lidar&quot;&gt;trang TF-Luna&lt;/a&gt; để thấy rõ đặc tính đơn điểm (&lt;em&gt;single-point&lt;/em&gt; — chỉ phù hợp tránh va chạm, không dùng cho SLAM). Cảm biến &lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;MPU6050&lt;/a&gt; chỉ là tùy chọn hỗ trợ bộ lọc về sau, không thay thế lidar và chưa cần cho bài lab kiểm tra giao diện này.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=BuRCJ2fegcc&quot;&gt;SLAM - 5 Minutes with Cyrill&lt;/a&gt; — Cyrill Stachniss (tiếng Anh). Tóm tắt nguyên lý cốt lõi của SLAM (vừa định vị vừa dựng bản đồ) trong 5 phút.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=-XU54IsG8Vo&quot;&gt;SLAM Robot Mapping - Computerphile&lt;/a&gt; — Computerphile (tiếng Anh). Giải thích trực quan lý do robot cần tự dựng bản đồ khi hoạt động trong môi trường chưa biết trước (không phụ thuộc phiên bản ROS).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;h3 id=&quot;bài-1&quot;&gt;Bài 1&lt;/h3&gt;

&lt;p&gt;Vẽ cây TF của một robot đang dựng bản đồ dọc hành lang thẳng và ghi rõ thành phần nào chịu trách nhiệm xuất bản từng cạnh (SLAM, odometry bánh xe hay URDF tĩnh).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Tối thiểu cần ba cạnh: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;, và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → khung laser&lt;/code&gt;; nếu thêm cạnh thứ tư, giải thích vì sao đó là biến đổi tĩnh.&lt;/p&gt;

&lt;h3 id=&quot;bài-2&quot;&gt;Bài 2&lt;/h3&gt;

&lt;p&gt;Dùng lại số liệu ở mục Khái niệm, tính lại \(\Delta s\) và \(\Delta \theta\) trong trường hợp encoder bánh phải đúng chiều còn encoder bánh trái bị ngược dấu. Góc yaw ảo có đổi chiều không?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Giữ nguyên \(b = 0{,}20\) m, chỉ đảo dấu \(\Delta s_l\) so với phép tính mẫu.&lt;/p&gt;

&lt;h3 id=&quot;bài-3&quot;&gt;Bài 3&lt;/h3&gt;

&lt;p&gt;Viết một đoạn mô tả ngắn về giao ước đầu vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; dành cho người chỉ có cảm biến TF-Luna, gồm tên kiểu message, yêu cầu của mảng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ranges&lt;/code&gt; và lý do từ chối.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Nêu rõ kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt; yêu cầu một mảng khoảng cách theo nhiều góc quét, không thể thay bằng một số thực đơn lẻ.&lt;/p&gt;

&lt;h3 id=&quot;bài-4&quot;&gt;Bài 4&lt;/h3&gt;

&lt;p&gt;Khi đẩy thẳng robot 1 m, giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; của odometry tăng khoảng 1 m và góc yaw lệch dưới 1°, nhưng hành lang trên bản đồ SLAM vẫn bị cong. Bạn cần kiểm tra yếu tố nào tiếp theo và không nên vội làm gì?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Khi odometry đã chính xác, hãy kiểm tra góc lắp (yaw) của lidar và phép biến đổi TF tĩnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → laser&lt;/code&gt; thay vì vội mua thêm IMU.&lt;/p&gt;

&lt;h3 id=&quot;bài-5&quot;&gt;Bài 5&lt;/h3&gt;

&lt;p&gt;Viết đầy đủ các lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info&lt;/code&gt; cần chạy trước khi khởi động &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;online_async_launch.py&lt;/code&gt; và giải thích ý nghĩa khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Publisher count&lt;/code&gt; bằng 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gợi ý.&lt;/strong&gt; Hai lệnh kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; kèm cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-v&lt;/code&gt;; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Publisher count: 0&lt;/code&gt; đồng nghĩa không có dữ liệu đầu vào để ghép bản đồ nên cần dừng trước khi launch.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/SteveMacenski/slam_toolbox&quot;&gt;slam_toolbox&lt;/a&gt; — tài liệu về chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;online_async&lt;/code&gt; và các yêu cầu đối với dữ liệu quét cùng odometry.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; — quy chuẩn định nghĩa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Quality-of-Service-Settings.html&quot;&gt;Quality of service trên Jazzy&lt;/a&gt; — giải thích sự không tương thích giữa subscriber &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt; và publisher &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-dfrobot-tf-luna-tof-micro-single-point-ranging-lidar&quot;&gt;Trang TF-Luna&lt;/a&gt; — thông số kỹ thuật xác nhận đây là cảm biến đo đơn điểm.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 10</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_06_ch10_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_06_ch10_lab_checklist</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~60 phút&lt;/strong&gt;. Sau khi bài 10-05 đã đối chiếu giữa mô phỏng và thực tế, bài kiểm tra cuối chương này tổng hợp toàn bộ kết quả của chương 10: Gazebo Harmonic có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, spawn thành công URDF, cầu nối serial hoặc micro-ROS, và ba dòng kiểm định sim-to-real.&lt;/p&gt;

&lt;p&gt;Hoàn thành Chương 10 nghĩa là bạn đã dựng xong “cặp song sinh số” (Digital Twin) cho robot của mình: một phiên bản chạy trên Gazebo Harmonic và một phiên bản chạy trên phần cứng thật, cùng chia sẻ chung một giao diện ROS 2. Bảng nghiệm thu này giúp xác nhận cả hai phiên bản hoạt động đồng nhất trước khi bước vào bài toán tự hành SLAM và Nav2 ở Chương 11.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-tiêu-chuẩn-nghiệm-thu-mô-phỏng--cầu-nối-chương-10&quot;&gt;Kiến thức cần nhớ: Ba tiêu chuẩn nghiệm thu Mô phỏng &amp;amp; Cầu nối (Chương 10)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chạy song song sạch trên Gazebo Harmonic (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sim_bringup&lt;/code&gt;)&lt;/strong&gt;:
Xe sinh ra đúng tư thế trên mặt đất (không lún, không nổ), RTF ổn định (\(\ge 0{,}7\)), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; hoạt động chuẩn và toàn bộ cảm biến giả lập phát đúng hệ tọa độ TF2.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cầu nối Phần cứng Tin cậy (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;real_bringup&lt;/code&gt;)&lt;/strong&gt;:
Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; điều khiển mượt trên xe thật, Watchdog ngắt động cơ đúng hạn khi rút cáp/tắt node, và sai số odometry vòng hở nằm trong ngưỡng đã hiệu chuẩn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ghi nhận Hồ sơ Sim-to-Real&lt;/strong&gt;:
Lưu trữ rõ các thông số sai lệch đo được giữa mô phỏng và xe thật (deadzone, ma sát, độ trễ) để làm đầu vào tinh chỉnh cho bộ lập kế hoạch Nav2 ở Chương 11.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Bạn cần hoàn thành và xác nhận đủ năm tiêu chí kèm log hoặc ảnh minh chứng: world Harmonic hoạt động, mô hình robot spawn thành công, lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; làm bánh xe mô phỏng lăn tiến, cầu nối tới firmware hoạt động (hoặc có ghi chú hoãn micro-ROS kèm ngày cụ thể) và bảng kiểm sim-to-real đã điền đủ. Nếu thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; thì dù đã mở được cửa sổ Gazebo vẫn chưa đạt.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành các bài từ 10-01 đến 10-05 với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;. Lưu ý bản Classic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; không thể thay thế cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Capstone B ở chương 11 sẽ sử dụng chính môi trường mô phỏng và topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; của chương này. Nếu chương 10 chỉ dừng ở một cửa sổ world trống chưa kiểm chứng, các lỗi phát sinh về sau rất dễ bị đổ nhầm cho SLAM.&lt;/p&gt;

&lt;h2 id=&quot;tiêu-chí-đạt-và-không-đạt&quot;&gt;Tiêu chí đạt và không đạt&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Môi trường mô phỏng (World).&lt;/strong&gt; Câu lệnh launch sử dụng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; (hoặc tên world khác được ghi rõ), có cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; (hoặc giải thích rõ lý do giữ trạng thái tạm dừng), và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; liệt kê được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. Chỉ chụp ảnh cửa sổ GUI mà không ghi tên world là chưa đủ.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mô hình (Model).&lt;/strong&gt; Spawn thành công từ tệp URDF hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; của bạn, cây TF có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;. Nếu mô hình bị rơi xuyên mặt sàn thì chưa đạt cho đến khi ghi rõ đã sửa yếu tố nào (khối lượng, collision hoặc plugin) và spawn lại thành công.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lệnh điều khiển.&lt;/strong&gt; Giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; dương phải làm mô hình tiến dọc theo trục $+x$ của nó. Ghi rõ giá trị đã xuất bản (ví dụ $0{,}2$ m/s) và hướng di chuyển quan sát được.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cầu nối phần cứng.&lt;/strong&gt; Nếu có xe thật: cầu nối serial hoặc micro-ROS agent chuyển &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; thành chuyển động bánh xe (nhấc trên không) rồi tự về 0 khi ngừng gửi lệnh. Nếu chưa có xe: ghi chú hoãn có ngày cụ thể, nhưng tiêu chí gửi lệnh trong mô phỏng vẫn bắt buộc đạt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Đối chiếu sim-to-real.&lt;/strong&gt; Điền đủ ba dòng đạt/không đạt theo bài 10-05, kèm ít nhất một số đo thực tế hoặc xác nhận loại bỏ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; trên tệp launch robot thật.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-bài-test-đổi-công-tắc-không-đổi-mã-simreal-parity-check&quot;&gt;Giải thích: Bài test “Đổi công tắc không đổi mã” (Sim/Real Parity Check)&lt;/h3&gt;

&lt;p&gt;Để chứng minh kiến trúc Chương 10 đã đạt chuẩn, hãy thực hiện đúng bài test 3 bước với cùng một script chạy hình vuông (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;square_test.py&lt;/code&gt; chỉ gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; và đọc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sim_bringup&lt;/code&gt; → chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;square_test.py&lt;/code&gt; → xác nhận xe vẽ hình vuông trong Gazebo và quay về gần điểm xuất phát.&lt;/li&gt;
  &lt;li&gt;Tắt mô phỏng, bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;real_bringup&lt;/code&gt; trên xe thật → chạy &lt;strong&gt;chính xác cùng tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;square_test.py&lt;/code&gt; không sửa một ký tự nào&lt;/strong&gt;!&lt;/li&gt;
  &lt;li&gt;Đo khoảng cách lệch giữa điểm đầu và điểm cuối trên sàn thật (ví dụ lệch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12 cm&lt;/code&gt; và lệch góc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8°&lt;/code&gt;) rồi ghi vào sổ lab làm mốc chuẩn trước khi bật SLAM ở Chương 11.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Sao chép mẫu sau vào sổ thực hành và điền đầy đủ thông tin:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Ky duyet chuong 10
World + /clock:
Model + base_link:
Twist da gui:
Cau serial/micro-ROS hoac giay hoan:
use_sim_time tren robot that: khong
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;World đứng yên, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; bằng 0&lt;/td&gt;
      &lt;td&gt;Thiếu cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; hoặc chưa bấm nút play&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Máy có hai phiên bản Gazebo&lt;/td&gt;
      &lt;td&gt;Trùng lặp giữa Classic và Harmonic; tắt hẳn bản Classic&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh xe mô phỏng quay ngược&lt;/td&gt;
      &lt;td&gt;Sai dấu khớp hoặc dấu trong plugin; sửa nhất quán tại một chỗ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot thật bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Gỡ bỏ ngay; robot thật dùng đồng hồ thực&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=dY8JxldcuqA&quot;&gt;Correctly Install Gazebo Harmonic Inside of ROS2 Jazzy Installation -Fix Gazebo Installation Issues&lt;/a&gt; — Aleksandar Haber PhD (tiếng Anh). Danh sách kiểm tra mô phỏng bắt đầu từ việc đảm bảo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt; khởi chạy đúng trong môi trường Jazzy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tiêu chí nào vẫn chưa đạt nếu GUI hiển thị được mô hình nhưng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; không có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;?&lt;/li&gt;
  &lt;li&gt;Vì sao việc ghi chú hoãn micro-ROS vẫn không miễn trừ tiêu chí kiểm thử &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; trong mô phỏng?&lt;/li&gt;
  &lt;li&gt;Nếu ngừng gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; nhưng bánh xe thật vẫn tiếp tục quay, tiêu chí nào đang không đạt?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Tiêu chí World không đạt, vì ảnh GUI không chứng minh server mô phỏng đang chạy đồng hồ thời gian.&lt;/li&gt;
  &lt;li&gt;Mô phỏng là môi trường kiểm chứng lệnh điều khiển trước khi nối xuống firmware; việc hoãn chỉ áp dụng cho cầu nối phần cứng.&lt;/li&gt;
  &lt;li&gt;Tiêu chí Cầu nối (và mục an toàn bài 10-05) không đạt do cơ chế watchdog chưa đưa vận tốc về 0.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/getstarted/&quot;&gt;Gazebo Harmonic, bắt đầu&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/gazebosim/ros_gz&quot;&gt;ros_gz&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter10/10_05_sim_to_real/&quot;&gt;Bài sim-to-real&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist sim-to-real</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_05_sim_to_real/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_05_sim_to_real</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~80 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Một kịch bản quen thuộc đến ám ảnh với mọi kỹ sư robot: trên Gazebo, chiếc xe chạy bám đường mượt mà, rẽ góc sắc lẹm và đỗ chuẩn xác từng centimet; nhưng vừa thả xuống nền gạch men phòng lab, nó lập tức trượt bánh, rẽ lố góc và đâm sầm vào tường! Hiện tượng đó gọi là &lt;strong&gt;Khoảng cách Mô phỏng – Thực tế (Sim-to-Real Gap)&lt;/strong&gt;, và nhận diện từng nguồn sai lệch để thu hẹp khoảng cách đó là kỹ năng phân biệt người chơi mô phỏng với kỹ sư robot thực thụ.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-nguồn-gốc-của-khoảng-cách-sim-to-real&quot;&gt;Kiến thức cần nhớ: Bốn nguồn gốc của Khoảng cách Sim-to-Real&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Động lực học tiếp xúc &amp;amp; Trượt bánh (Friction &amp;amp; Wheel Slip)&lt;/strong&gt;:
Mô phỏng thường giả sử sàn phẳng đồng nhất và bánh không trượt, trong khi sàn thật có khe gạch, bụi, độ cứng lốp thay đổi và lực ma sát tĩnh (stiction) làm động cơ không nhúc nhích ở vùng PWM nhỏ (deadzone).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Độ trễ hệ thống &amp;amp; Sai lệch cảm biến (Latency &amp;amp; Sensor Noise)&lt;/strong&gt;:
Mô phỏng mặc định trả dữ liệu cảm biến gần như tức thời và sạch; ngoài đời thực, LiDAR bị nhiễu trên mặt kính/gương, IMU bị trôi nhiệt độ (bias drift) và mạng/serial có độ trễ hàng chục mili-giây.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nhận dạng hệ thống (System Identification)&lt;/strong&gt;:
Đo trực tiếp trên xe thật các tham số vật lý (vùng chết PWM, hằng số thời gian động cơ, sai số odometry, độ lệch chuẩn nhiễu IMU) rồi cập nhật ngược lại vào tệp cấu hình Gazebo để mô phỏng “xấu” giống đời thực hơn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ngẫu nhiên hóa miền (Domain Randomization)&lt;/strong&gt;:
Cố tình thay đổi ngẫu nhiên ma sát, khối lượng, nhiễu cảm biến và độ trễ trong mô phỏng để bộ điều khiển hoặc chính sách AI học cách bền vững trước sai lệch thực tế.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ đối chiếu trực tiếp một lệnh điều khiển Capstone trong Gazebo Harmonic với chính lệnh đó trên phần cứng thực tế — đảm bảo thống nhất hệ đơn vị, tên frame chuẩn REP-105 và chiều quay bánh xe. Bạn giữ đúng một nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; trong mô phỏng và loại bỏ hoàn toàn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; trên robot thật (nơi hệ thống dùng đồng hồ thực). Bạn đo được độ trễ (tuổi dấu thời gian) của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;, liên kết chỉ số này với watchdog 300 ms cùng độ trễ phát sinh từ Wi-Fi/MQTT, đồng thời giải thích vì sao nút dừng khẩn (E-stop) bắt buộc phải ngắt đường nguồn động cơ bằng phần cứng thay vì chỉ xuất bản một topic. Cuối bài, bạn hoàn thành ba dòng kiểm định đạt/không đạt kèm minh chứng cụ thể (log lệnh hoặc số đo thực tế).&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Mô hình spawn ở bài 10-02 lăn tiến trong Gazebo Harmonic khi nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x = 0.2&lt;/code&gt;. Cầu nối bài 10-04 chuyển đổi cùng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; đó thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt; và tự trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; khi publisher ngừng gửi. Cả mô phỏng lẫn phần cứng đều dùng thống nhất các tên frame của bài 09-05 (tối thiểu là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;). Quy tắc an toàn nguồn pin có cầu chì, nối chung mass và nhấc bánh khỏi mặt bàn từ chương 07 vẫn áp dụng bắt buộc khi thử mạch thật. Phần ghi chép của lab có thể tận dụng trực tiếp log đã thu ở các bài trước.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Mô phỏng cho phép thử nghiệm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; an toàn mà không lo hỏng động cơ, nhưng khi đưa xuống sàn thực tế, mọi thông số trên topic đó phải mang đúng ý nghĩa tương đương. Nav2 không thể tự động bù sai lệch đơn vị, lệch tên frame hay động cơ đấu ngược dây; các chính sách học máy huấn luyện trong Harmonic về sau cũng chỉ chuyển sang xe thật được khi đơn vị mét, hệ tọa độ, chiều quay và cơ chế dừng an toàn đều khớp. Bạn cần đi qua bảng kiểm này trước khi chạy tự hành trên khung thật, và kiểm lại mỗi khi thay bánh xe, đổi giá đỡ cảm biến hoặc chỉnh hằng số quy đổi trong cầu nối.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch10_sim2real.png&quot; alt=&quot;Cùng một lệnh đi từ world Harmonic sang Capstone trên sàn&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Hệ đơn vị.&lt;/strong&gt; Mọi khoảng cách trong khóa học tính bằng mét; vận tốc trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; tính bằng m/s và rad/s. URDF, plugin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DiffDrive&lt;/code&gt; và khoảng cách hai bánh \(L = 0.16\) m trong cầu nối đều dùng mét. Dải số nguyên −100…100 truyền trên serial không phải hệ đơn vị riêng mà chỉ là kết quả quy đổi ở bài 10-04 (0,5 m/s tương ứng 100). Nếu mô hình xuất theo milimét hoặc cầu nối hiểu sai thang đo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt;, cùng một lệnh sẽ tạo ra hai chuyển động hoàn toàn khác nhau giữa mô phỏng và thực tế.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hệ tọa độ.&lt;/strong&gt; Chuẩn &lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; quy định tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; và các frame cảm biến. Plugin mô phỏng, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; và node odometry đều phải dùng chung chính xác các chuỗi tên này. Nếu URDF khai báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; nhưng bản tin quét lại ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_link&lt;/code&gt;, cây TF sẽ bị đứt, dữ liệu quét dồn về gốc và bản đồ chi phí (costmap) hoạt động sai lệch. Kèm theo đó là quy ước REP-103: \(+x\) hướng trước, \(+y\) sang trái, \(+z\) hướng lên và yaw dương khi quay sang trái.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chiều quay bánh xe.&lt;/strong&gt; Trong Harmonic, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x = 0.2&lt;/code&gt; phải đẩy mô hình tiến dọc \(+x\) với cả hai bánh lăn về trước. Trên bàn thử nghiệm, cùng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; đó phải tạo payload &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt; và làm cả hai bánh thật quay tiến. Nếu một động cơ bị đấu ngược dây khiến bánh quay lùi dù số nguyên dương, hãy đảo lại dây hoặc sửa dấu tại duy nhất một chỗ trong cầu nối rồi kiểm tra lại cả hai phía — không được đảo dấu \(\omega\) tạm bợ trong Nav2 vì lỗi sẽ tái diễn ngay khi chạy node teleop khác.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Đồng hồ thời gian.&lt;/strong&gt; Đây là cấu hình rất dễ sao chép nhầm từ tệp launch mô phỏng sang robot thật. Trong Harmonic có duy nhất một nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; bắc cầu một chiều từ Gazebo sang ROS:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Các node trong mô phỏng đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt;, và bạn cần xác nhận chỉ có đúng một publisher trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /clock&lt;/code&gt;. Ngược lại, trên robot thật tuyệt đối không bắc cầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; và không đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt;: nếu quên bỏ tham số này, node sẽ treo chờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, timer ngừng chạy và động cơ có nguy cơ giữ nguyên lệnh cuối.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Độ trễ truyền tin.&lt;/strong&gt; Mô phỏng lý tưởng thường không phản ánh độ trễ mạng thực tế. Tuổi của dấu thời gian được tính bằng:&lt;/p&gt;

\[\mathrm{age} = t_{\mathrm{now}} - t_{\mathrm{stamp}}\]

&lt;p&gt;(với cả hai mốc lấy trên cùng một đồng hồ). Qua cáp USB trực tiếp, độ trễ chỉ vài mili giây; nhưng qua Wi-Fi hoặc MQTT ở chương 08, độ trễ có thể tăng lên hàng chục đến hàng trăm mili giây, thậm chí vài giây khi router nghẽn. Cầu nối sẽ gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; sau 0,3 s không có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; mới, và firmware cũng tự ngắt nếu mất khung. Nếu tuổi dữ liệu vượt quá 0,3 s thì lệnh đã quá hạn watchdog — không nên nới lỏng watchdog lên hai giây để tránh xe lao đi mất kiểm soát khi mất tín hiệu.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dừng khẩn cấp (E-stop).&lt;/strong&gt; Watchdog phần mềm không thay thế được E-stop, vì nếu tiến trình hoặc vi điều khiển treo, phần mềm có thể ngừng hoạt động đúng lúc cần bảo vệ nhất. E-stop phải là một công tắc phần cứng nối tiếp trên đường nguồn động cơ, ngắt trực tiếp điện cấp cho mạch lái ngay cả khi ESP32 treo. Topic ROS hay chân GPIO chỉ là lớp hỗ trợ phụ; công tắc vật lý cần đặt ở vị trí thuận tay ấn mà không phải với ngang qua bánh xe đang quay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An toàn điện.&lt;/strong&gt; Các quy tắc cơ bản như nối chung mass giữa mạch logic và mạch lái, lắp cầu chì trên đường pin, nhấc bánh khỏi bàn trong lần thử đầu tiên và không bao giờ cấp nguồn động cơ từ cổng USB vẫn phải tuân thủ nghiêm ngặt khi chuyển từ mô phỏng sang phần cứng thật.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Giới hạn cảm biến.&lt;/strong&gt; Cảm biến &lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-dfrobot-tf-luna-tof-micro-single-point-ranging-lidar&quot;&gt;DFRobot TF-Luna&lt;/a&gt; chỉ đo khoảng cách đơn điểm dọc một tia, không phải lidar quét 360° dùng cho SLAM. Bạn có thể mô phỏng lidar 360° trong Gazebo để học Nav2, nhưng không thể kỳ vọng TF-Luna tái tạo được dữ liệu đó trên xe thật. Với IMU như &lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;GY-521&lt;/a&gt;, nguyên tắc thống nhất một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; khớp URDF vẫn giữ nguyên.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Động lực học tiếp xúc và odometry.&lt;/strong&gt; Mô hình ma sát mặc định trong Harmonic khác xa mặt thảm thực tế và plugin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DiffDrive&lt;/code&gt; không thay thế bộ điều khiển PID ở bài 08-03; do đó cần tinh chỉnh lại PID trực tiếp trên phần cứng thật (thử nhấc bánh trước rồi mới đặt xuống sàn). Một lỗi phần mềm phổ biến khác là để hai node odometry cùng xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;: khi đó &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /odom&lt;/code&gt; trông có vẻ mượt nhưng robot lại nhảy giật trong RViz — dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /tf -v&lt;/code&gt; để phát hiện publisher trùng lặp.&lt;/p&gt;

&lt;p&gt;Bảng đối chiếu tổng hợp giữa mô phỏng và thực tế:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Mô phỏng (Harmonic)&lt;/th&gt;
      &lt;th&gt;Capstone thật&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Đơn vị vận tốc dài trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;m/s&lt;/td&gt;
      &lt;td&gt;m/s, qua một hệ số quy đổi duy nhất sang −100…100&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đơn vị vận tốc góc&lt;/td&gt;
      &lt;td&gt;rad/s quanh \(+z\)&lt;/td&gt;
      &lt;td&gt;Cùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;, cùng quy ước dấu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hệ tọa độ (Frame)&lt;/td&gt;
      &lt;td&gt;Tên chuẩn REP-105 từ URDF&lt;/td&gt;
      &lt;td&gt;Trùng khớp tuyệt đối trong cầu nối, scan và IMU&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chiều quay bánh&lt;/td&gt;
      &lt;td&gt;\(+x\) đẩy mô hình tiến&lt;/td&gt;
      &lt;td&gt;Cùng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; làm cả hai bánh thật quay tiến&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ&lt;/td&gt;
      &lt;td&gt;Một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; (Gazebo → ROS), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Đồng hồ thực, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=false&lt;/code&gt;, không có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Độ trễ lệnh&lt;/td&gt;
      &lt;td&gt;Thường vài mili giây&lt;/td&gt;
      &lt;td&gt;Cần đo thực tế (Wi-Fi/MQTT làm tăng trễ)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cơ chế dừng khi mất kết nối&lt;/td&gt;
      &lt;td&gt;Tạm dừng world hoặc gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; bằng 0&lt;/td&gt;
      &lt;td&gt;Watchdog firmware 300 ms kết hợp E-stop phần cứng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Quy trình chạy thử đầu tiên&lt;/td&gt;
      &lt;td&gt;Spawn mô hình rồi gửi một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; ngắn&lt;/td&gt;
      &lt;td&gt;Nhấc bánh khỏi bàn, pin có cầu chì, chung mass, gửi cùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tiếp xúc mặt sàn&lt;/td&gt;
      &lt;td&gt;Va chạm hình học trên mặt phẳng lý tưởng&lt;/td&gt;
      &lt;td&gt;Ma sát sàn/thảm và bánh đa hướng thực tế&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Odometry&lt;/td&gt;
      &lt;td&gt;Duy nhất một publisher cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Duy nhất một publisher (hai nguồn sẽ xung đột TF)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Xác định rõ tiêu chí đạt/không đạt cho lệnh chạy thẳng trước khi chạm vào phần cứng.&lt;/p&gt;

&lt;p&gt;Trong mô phỏng (với hệ thống bài 10-02 đang chạy):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub &lt;span class=&quot;nt&quot;&gt;--times&lt;/span&gt; 20 /cmd_vel geometry_msgs/msg/Twist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;{linear: {x: 0.2, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Đạt:&lt;/em&gt; mô hình trong cửa sổ Gazebo tịnh tiến về trước và cả hai bánh lăn đều. &lt;em&gt;Không đạt:&lt;/em&gt; xe quay tròn, đi lùi hoặc chỉ có RViz thay đổi.&lt;/p&gt;

&lt;p&gt;Trên bàn thử nghiệm (nhấc bánh khỏi mặt đất, cầu nối chạy trên cổng thật) với cùng đợt lệnh rồi dừng: &lt;em&gt;Đạt&lt;/em&gt; khi parser/log hiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt;, cả hai bánh quay tiến trong chưa đầy một giây rồi dừng hẳn cùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; khi hết lệnh. &lt;em&gt;Không đạt&lt;/em&gt; nếu một bánh quay ngược, bánh tiếp tục quay sau khi ngừng gửi, hoặc giá trị số nguyên khác 40.&lt;/p&gt;

&lt;p&gt;Kiểm tra tiêu chí đồng hồ trong mô phỏng:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic info /clock
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Đạt:&lt;/em&gt; chỉ có đúng một publisher (là cầu nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt;) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; chỉ bật cho các node mô phỏng. Trên tệp launch của robot thật, &lt;em&gt;đạt&lt;/em&gt; khi không có bất kỳ tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; nào đặt bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; (nếu chép nhầm tệp launch mô phỏng sang robot thì mục này không đạt kể cả khi chưa cắm động cơ).&lt;/p&gt;

&lt;p&gt;Với tiêu chí độ trễ, ghi log giá trị tuổi dấu thời gian trong callback của cầu nối: qua USB vài mili giây đạt mức “dưới 50 ms”; nếu giá trị tiệm cận 0,3 s thì đó là cảnh báo nguy hiểm chứ không phải trạng thái đạt.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-sai-lệch-tư-thế-khi-bánh-xe-bị-trượt-5-trên-một-đoạn-rẽ&quot;&gt;Giải thích: Tính tay sai lệch tư thế khi bánh xe bị trượt 5% trên một đoạn rẽ&lt;/h3&gt;

&lt;p&gt;Giả sử trong Gazebo xe chạy một cung tròn bán kính \(R = 1{,}0\text{ m}\) góc \(90^\circ\) (quãng đường tâm xe \(s = \frac{\pi}{2} \approx 1{,}571\text{ m}\)). Trên sàn gạch men trơn ngoài đời, bánh ngoài (bánh phải) bị &lt;strong&gt;trượt mất 5%&lt;/strong&gt; lực kéo so với số vòng encoder quay được:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Encoder vẫn báo đúng \(v_R = 0{,}22\text{ m/s}, v_L = 0{,}18\text{ m/s}\) (khổ vết \(L = 0{,}20\text{ m}\)) nên odometry tin rằng \(\omega_{\text{odom}} = \frac{0{,}22 - 0{,}18}{0{,}20} = \mathbf{0{,}20\text{ rad/s}}\).&lt;/li&gt;
  &lt;li&gt;Nhưng tốc độ tịnh tiến thực tế của bánh phải chỉ là \(v_{R,\text{thực}} = 0{,}95 \times 0{,}22 = \mathbf{0{,}209\text{ m/s}}\), nên vận tốc góc thực của xe tụt xuống:
\(\omega_{\text{thực}} = \frac{0{,}209 - 0{,}180}{0{,}20} = \mathbf{0{,}145\text{ rad/s}}!\)&lt;/li&gt;
  &lt;li&gt;Sau \(7{,}85\text{ s}\) (lúc odometry báo đã rẽ đủ \(90^\circ\)), xe thật mới chỉ quay được \(0{,}145 \times 7{,}85 \approx 1{,}14\text{ rad} \approx \mathbf{65^\circ}\) — &lt;strong&gt;lệch tới \(25^\circ\) chỉ sau một khúc cua!&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Con số gây sốc này giải thích vì sao chỉ cần 5% trượt bánh đã phá hỏng odometry bánh xe, và vì sao ở Chương 11 ta bắt buộc phải dùng &lt;strong&gt;IMU + LiDAR SLAM&lt;/strong&gt; để đóng vòng định vị!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chép bảng đối chiếu vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. Trong buổi này chọn kiểm định ba hạng mục: &lt;strong&gt;chiều quay bánh&lt;/strong&gt;, &lt;strong&gt;đồng hồ&lt;/strong&gt;, và chọn thêm một trong ba mục (&lt;strong&gt;độ trễ lệnh&lt;/strong&gt;, &lt;strong&gt;E-stop&lt;/strong&gt;, hoặc &lt;strong&gt;một publisher odometry&lt;/strong&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chiều quay bánh:&lt;/strong&gt; chạy đợt lệnh 0,2 m/s trong Harmonic và ghi nhận đạt/không đạt kèm hiện tượng quan sát được. Trên phần cứng (bánh nhấc trên không), gửi cùng lệnh qua cầu nối, ghi lại dòng parser và chiều quay từng bánh rồi dừng ngay khi xác định xong.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đồng hồ:&lt;/strong&gt; lưu kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /clock&lt;/code&gt; trong phiên mô phỏng, đồng thời kiểm tra mọi khai báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; trong tệp launch dành cho robot thật (nếu còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; thì đánh dấu không đạt).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hạng mục thứ ba tự chọn:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Nếu chọn độ trễ: ghi một kết quả đo thực tế \(t_{\mathrm{now}} - t_{\mathrm{stamp}}\).&lt;/li&gt;
      &lt;li&gt;Nếu chọn E-stop: kiểm tra xem đã có công tắc vật lý ngắt nguồn động cơ hay chưa; nếu chưa, ghi rõ không đạt kèm lý do “cơ chế dừng hiện mới chỉ là một topic”.&lt;/li&gt;
      &lt;li&gt;Nếu chọn odometry: chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /tf -v&lt;/code&gt; và liệt kê toàn bộ node đang xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Với mỗi hạng mục không đạt, viết một hành động khắc phục cụ thể (ví dụ: “Đảo dây động cơ trái, sau đó chạy lại cụm lệnh 0,2 m/s” thay vì ghi chung chung “sửa sau”).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ba dòng kiểm định có đánh giá đạt/không đạt rõ ràng kèm minh chứng cụ thể:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sim: linear.x 0.2, model moved forward in Gazebo, both wheels rolled
bench: parser OK V,40,40, both wheels forward, then OK V,0,0 within 1 s of silence
clock: sim has one /clock publisher; robot launch does not set use_sim_time true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hạng mục thứ ba có thể mang kết quả không đạt một cách trung thực: ghi nhận “không đạt” kèm bằng chứng vẫn hoàn thành bài lab, ngược lại đánh dấu “đạt” mà không có log minh chứng là chưa đạt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Mô phỏng chạy mượt nhưng xuống sàn xe bị rung lắc hoặc chạy chậm&lt;/td&gt;
      &lt;td&gt;Sao chép hệ số mô phỏng/ma sát xuống mặt sàn thật; cần tinh chỉnh lại PID bài 08-03 trên xe thật&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đám mây điểm hoặc tia cảm biến nằm dồn tại gốc tọa độ trong RViz&lt;/td&gt;
      &lt;td&gt;Lệch tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; giữa bản tin (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_link&lt;/code&gt;) và URDF (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot nhảy giật trong RViz dù &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo /odom&lt;/code&gt; trông mượt&lt;/td&gt;
      &lt;td&gt;Có hai publisher cùng phát &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;; kiểm tra bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /tf -v&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Node khởi chạy nhưng timer không hoạt động, bánh giữ nguyên PWM cuối&lt;/td&gt;
      &lt;td&gt;Bật nhầm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; trên robot thật khi không có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nhả nút điều khiển nhưng robot vẫn chạy thêm vài giây&lt;/td&gt;
      &lt;td&gt;Watchdog dài hơn 300 ms, hoặc đường truyền Wi-Fi giữ lệnh cũ mà không gửi lệnh 0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/estop&lt;/code&gt; hiển thị bình thường nhưng không dừng được xe khi MCU treo&lt;/td&gt;
      &lt;td&gt;Cơ chế dừng chỉ dừng ở mức phần mềm/topic, chưa có công tắc ngắt nguồn phần cứng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bạn tận dụng chính robot Capstone sẵn có, không cần mua thêm máy tính. Nếu đường nguồn động cơ chưa có công tắc ngắt vật lý, bạn có thể bổ sung công tắc dừng khẩn (tìm từ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;công tắc khẩn 22mm&lt;/code&gt; trên Shopee/Lazada, giá tham khảo 2026 khoảng 30.000–80.000 đồng — chọn loại tiếp điểm thường đóng NC để khi ấn sẽ ngắt mạch nguồn) và đế cầu chì trên &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; nếu cần. Liên kết TF-Luna ở phần trên chỉ nhằm phân biệt cảm biến đo đơn điểm với lidar quét 360°, không phải đề xuất mua cho SLAM.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=campHHAPtyg&quot;&gt;ROS2 Jazzy Tutorial: Develop and Control Gazebo Model of Mobile Robot in ROS2 and Python&lt;/a&gt; — Aleksandar Haber PhD (tiếng Anh). Minh họa cùng một lệnh điều khiển chạy trong Gazebo để làm cơ sở đối chiếu với robot ngoài thực tế.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=dY8JxldcuqA&quot;&gt;Correctly Install Gazebo Harmonic Inside of ROS2 Jazzy Installation -Fix Gazebo Installation Issues&lt;/a&gt; — Aleksandar Haber PhD (tiếng Anh). Nhấn mạnh việc đồng bộ đúng phiên bản Gazebo Harmonic để bảng kiểm sim-to-real có hiệu lực.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1773957523904144&quot;&gt;MuJoCo recovery test&lt;/a&gt; — Facebook Reel của Hoàng Quân (Cơ điện tử ICTU, &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;), thử nghiệm hồi phục trong MuJoCo. Dù MuJoCo khác Gazebo Harmonic, nguyên tắc đối chiếu sim-to-real vẫn giữ nguyên khi thay đổi bộ mô phỏng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/2246938909200876&quot;&gt;Training bậc thang, rồi sim-to-real&lt;/a&gt; — Facebook Reel của Hoàng Quân (Cơ điện tử ICTU, &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;), huấn luyện địa hình bậc thang nhà C4 ICTU trước khi triển khai sim-to-real lên robot thật, minh họa đúng trình tự mô phỏng trước rồi đối chiếu trên sàn sau.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Bạn sao chép tệp launch từ mô phỏng (có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt;) sang chạy trên robot thật mà quên chỉnh sửa. Thành phần nào sẽ ngừng cập nhật và topic nào đang bị thiếu? &lt;em&gt;Gợi ý:&lt;/em&gt; các bộ định thời (timer) và truy vấn TF sẽ đứng yên do robot thật không phát &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, khiến động cơ có thể giữ nguyên lệnh cuối.&lt;/li&gt;
  &lt;li&gt;Cùng một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; khiến xe mô phỏng đi thẳng nhưng trên bàn thử nghiệm bánh trái lại quay lùi. Hạng mục nào trong bảng đối chiếu không đạt và cần xử lý gì đầu tiên? &lt;em&gt;Gợi ý:&lt;/em&gt; hạng mục chiều quay bánh; cần đảo dây động cơ trái hoặc sửa dấu tại duy nhất một chỗ trong cầu nối rồi thử lại đợt lệnh 0,2 m/s ở cả hai phía.&lt;/li&gt;
  &lt;li&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /odom&lt;/code&gt; cho thấy dữ liệu mượt nhưng mô hình robot trong RViz liên tục giật. Lệnh nào giúp phát hiện nguồn phát trùng lặp? &lt;em&gt;Gợi ý:&lt;/em&gt; chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /tf -v&lt;/code&gt; để tìm hai node đang cùng xuất bản cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Một đường truyền Wi-Fi giao gói tin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; có dấu thời gian chậm hơn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_now&lt;/code&gt; 0,45 s. Tính tuổi dấu thời gian và cho biết cầu nối có nên tiếp tục thực thi lệnh đó không. &lt;em&gt;Gợi ý:&lt;/em&gt; tuổi là \(t_{\mathrm{now}} - t_{\mathrm{stamp}} = 0.45\) s, vượt quá ngưỡng timeout 0,3 s nên cầu nối phải đưa lệnh về 0.&lt;/li&gt;
  &lt;li&gt;Vì sao một topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/estop&lt;/code&gt; vẫn không đủ để gọi là hệ thống dừng khẩn cấp ngay cả khi cầu nối đăng ký nghe nó? &lt;em&gt;Gợi ý:&lt;/em&gt; khi node hoặc MCU bị treo, thông điệp trên topic không còn tác dụng; bắt buộc phải có công tắc phần cứng ngắt trực tiếp nguồn động cơ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;thực-hành-thêm-berkeley-humanoid-lite--chuyển-sim-to-real-không-chỉnh-thêm-zero-shot&quot;&gt;Thực hành thêm: Berkeley Humanoid Lite — chuyển sim-to-real không chỉnh thêm (zero-shot)&lt;/h2&gt;

&lt;p&gt;Cùng nền tảng &lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt; (Hybrid Robotics, UC Berkeley), nhóm tác giả huấn luyện bộ điều khiển đi bộ bằng học tăng cường rồi chuyển thẳng từ mô phỏng sang robot thật, không dùng thêm bộ ước lượng trạng thái. Bài &lt;a href=&quot;https://www.roboticsproceedings.org/rss21/p062.pdf&quot;&gt;RSS 2025&lt;/a&gt; (&lt;a href=&quot;https://arxiv.org/abs/2504.17249&quot;&gt;arXiv:2504.17249&lt;/a&gt;) báo chính sách huấn luyện trên Isaac Gym; &lt;a href=&quot;https://github.com/HybridRobotics/berkeley-humanoid-lite&quot;&gt;kho mã hiện tại&lt;/a&gt; dùng Isaac Lab, kiểm tra sim-to-sim trên MuJoCo, rồi triển khai trên máy tính Intel N95 qua bốn bus CAN 2.0. Teleoperation dùng SteamVR.&lt;/p&gt;

&lt;p&gt;Nếu bạn có GPU NVIDIA, hãy làm theo &lt;a href=&quot;https://berkeley-humanoid-lite.gitbook.io/docs/getting-started-with-software&quot;&gt;hướng dẫn Training Environment&lt;/a&gt; để dựng môi trường đi bộ trong Isaac Lab: mục tiêu là mở được env và đọc observation/action, không nhất thiết huấn luyện tới hội tụ. Không có GPU thì đọc mục locomotion trong bài báo và đối chiếu với bảng kiểm sim-to-real của bài này — mô hình, độ trễ, và những gì không được mang từ mô phỏng sang phần cứng. Phần hộp số cycloid nằm ở bài 06-03.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành-thêm-simready-central--asset-openusd-đã-có-vật-lý&quot;&gt;Thực hành thêm: SimReady Central — asset OpenUSD đã có vật lý&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt; (NVIDIA, đang Alpha, blog 21/10/2026) là thư viện hơn 2.000 asset OpenUSD trên Hugging Face, dùng để dựng môi trường train và kiểm tra. “SimReady” nghĩa là đã có sẵn collision, khối lượng, ma sát, khớp kèm giới hạn và drive, nhãn semantic và vật liệu — không chỉ là mesh đẹp. Spec, profile và lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;simready-validate&lt;/code&gt; nằm trong &lt;a href=&quot;https://github.com/NVIDIA/simready-foundation&quot;&gt;SimReady Foundation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Tìm asset bằng Space trên Hugging Face, gói Python &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;simready-search&lt;/code&gt;, hoặc extension Asset Search trong Isaac Sim (extension này chỉ hiện gói CC-BY-4.0). Nhiều gói dùng CC-BY-4.0, nhưng không phải tất cả (ví dụ một số gói FANUC là Apache-2.0; một số gói UR cần đọc license riêng): luôn kiểm từng asset. Tay máy và gripper của FANUC, Franka, Robotiq, Universal Robots và các OEM khác có trong thư viện.&lt;/p&gt;

&lt;p&gt;Gợi ý: nếu máy đạt &lt;a href=&quot;https://docs.isaacsim.omniverse.nvidia.com/latest/installation/requirements.html&quot;&gt;sàn Isaac Sim&lt;/a&gt; (tối thiểu RTX 4080, 16 GB VRAM, 32 GB RAM; không dùng A100/H100), hãy làm bài &lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-assets-robot-pick-and-place&quot;&gt;pick-and-place&lt;/a&gt; (UR17G + Robotiq 2F-140). Máy yếu hơn thì đọc USD bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usdview&lt;/code&gt; hoặc Blender, hoặc theo &lt;a href=&quot;/contents/vi/chapter12/12_08_home_simulation_lab/&quot;&gt;hướng dẫn phòng lab mô phỏng tại nhà&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; — quy ước trục tọa độ và chiều quay dùng cho mục kiểm định dấu bánh.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; — danh sách tên frame tiêu chuẩn cần giữ đồng nhất giữa mô phỏng và cầu nối.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/gazebosim/ros_gz/blob/ros2/ros_gz_bridge/README.md&quot;&gt;README ros_gz_bridge&lt;/a&gt; — cấu hình cầu nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; một chiều (chỉ dùng trong mô phỏng, không chép sang robot thật).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Using-ros2-param.html&quot;&gt;Tham số Jazzy&lt;/a&gt; — cách kiểm tra và gỡ bỏ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; trong tệp launch của robot thật.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Nối Capstone A với ROS 2</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_04_bridge_capstone/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_04_bridge_capstone</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~90 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Mục tiêu tối thượng của một kiến trúc phần mềm robot chuyên nghiệp là &lt;strong&gt;tính trong suốt giữa mô phỏng và phần cứng thật (Sim/Real Parity)&lt;/strong&gt;: cùng một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic pub /cmd_vel ...&lt;/code&gt; hay cùng một ngăn xếp Nav2 phía trên phải chạy y hệt nhau mà &lt;em&gt;không cần biết&lt;/em&gt; bên dưới là Gazebo Harmonic hay chiếc xe Capstone thật đặt trên sàn phòng lab. Bài học này xây dựng hoàn chỉnh tầng cầu nối (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bridge_capstone&lt;/code&gt;) biến điều đó thành hiện thực.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-cầu-nối-phần-cứng-capstone&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Cầu nối Phần cứng Capstone&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Hợp đồng Giao diện Đồng nhất (Identical Topic &amp;amp; TF Contract)&lt;/strong&gt;:
Cả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sim_bringup&lt;/code&gt; lẫn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;real_bringup&lt;/code&gt; đều nhận đúng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;), xuất đúng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Odometry&lt;/code&gt;) và phát đúng cây TF &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt; theo chuẩn REP-103/105.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đóng gói Giao thức &amp;amp; Xử lý lỗi Kết nối (Reconnection &amp;amp; Watchdog)&lt;/strong&gt;:
Node cầu nối trên SBC kiểm soát mở/đóng cổng serial an toàn, phát hiện mất gói tin, tự gửi lệnh vận tốc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; khi tắt node (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;on_shutdown&lt;/code&gt;) và xuất trạng thái chẩn đoán (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/diagnostics&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hoán đổi tầng thấp bằng Launch Argument (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim:=true/false&lt;/code&gt;)&lt;/strong&gt;:
Tách rạch ròi tầng phần cứng/mô phỏng khỏi tầng định vị và điều hướng, giúp bạn phát triển thuật toán ở nhà trên Gazebo rồi sáng hôm sau chạy thẳng trên xe thật tại phòng lab.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ chạy được một node &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt; đăng ký nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, chuyển đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; thành vận tốc dài vành bánh trái và bánh phải với khoảng cách hai bánh \(L = 0.16\) m, rồi ánh xạ sang dải số nguyên của chương 08. Bạn khai báo tập trung hệ số quy đổi (0,5 m/s tương ứng số nguyên 100) để kiểm chứng rằng lệnh chạy thẳng 0,2 m/s tạo ra payload &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt;, đóng gói thành khung dữ liệu chuẩn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STX 0xAA&lt;/code&gt;, byte độ dài, payload ASCII, byte kiểm tra XOR và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0A&lt;/code&gt;), đồng thời tự động gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; khi quá 0,3 s không nhận được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; mới. Bạn kiểm thử toàn bộ node mà không cần robot thật thông qua cặp cổng giả lập PTY (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt;) cùng một bộ giải mã (parser) nhỏ, phân biệt được cơ chế nào vẫn dừng động cơ nếu tiến trình cầu nối bị tắt đột ngột, và luôn nhấc bánh xe khỏi mặt đất khi thử với phần cứng thật.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Nắm lại cấu trúc khung serial và watchdog firmware 300 ms ở bài 08-01, động học nghịch ở bài 08-04 và có sẵn môi trường Jazzy cài &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt;. Cài đặt thư viện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyserial&lt;/code&gt; qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3-serial&lt;/code&gt;) để dùng chung trình thông dịch Python với ROS, cùng công cụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; để tạo cặp cổng serial ảo. Bài này không cần chạy Gazebo và không để micro-ROS serial agent chiếm giữ cổng. Thử nghiệm với xe Capstone thật là bước tùy chọn ở cuối bài (bắt buộc nhấc bánh khỏi sàn).&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Node cầu nối này là điểm giao nhau đầu tiên giữa đồ thị ROS 2 và firmware Capstone A. Mô phỏng Gazebo đã lắng nghe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;; về sau, cả Nav2 lẫn các mô hình học máy cũng xuất bản đúng kiểu message này mà không cần biết đến khung byte &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xAA&lt;/code&gt;, trong khi firmware ESP32 hiện tại không hiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;. Cầu nối đóng vai trò điểm chuyển đổi duy nhất từ vận tốc mét/giây sang số nguyên −100…100. Chỉ sau khi cầu nối này chạy ổn định với log kiểm chứng từ parser (đúng tên topic, đúng hệ số quy đổi và đúng hành vi watchdog), bạn mới nên chuyển sang micro-ROS.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch10_serial_bridge.png&quot; alt=&quot;Twist đi vào, khung chương 08 đi ra, telemetry quay lại&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; chứa một vector vận tốc dài và một vector vận tốc góc, nhưng khung xe vi sai chỉ sử dụng hai trong sáu thành phần đó: vận tốc tiến \(v\) là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; (m/s) và vận tốc góc yaw \(\omega\) là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; (rad/s, dương khi quay sang trái theo REP-103). Thành phần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; biểu thị chuyển động trượt ngang mà cơ cấu vi sai không thực hiện được, nên hàm callback sẽ bỏ qua trường này thay vì trộn vào lệnh bánh xe.&lt;/p&gt;

&lt;p&gt;Áp dụng công thức động học nghịch từ bài 08-04 với khoảng cách hai bánh \(L\) (m):&lt;/p&gt;

\[v_R = v + \omega \frac{L}{2}, \qquad v_L = v - \omega \frac{L}{2}\]

&lt;p&gt;Với khung xe khóa học có \(L = 0.16\) m (\(L/2 = 0.08\) m), \(v_L\) và \(v_R\) là vận tốc tiếp tuyến của hai bánh (m/s, dương khi tiến), chưa phải giá trị PWM hay số nguyên truyền trên dây.&lt;/p&gt;

&lt;p&gt;Để đưa về dải số nguyên −100…100 của chương 08, ta định nghĩa hằng số quy đổi toàn thang (full scale) bằng 0,5 m/s:&lt;/p&gt;

\[n = \mathrm{clip}\left(\mathrm{round}\left(v_{\mathrm{wheel}} \cdot \frac{100}{0.5}\right), -100, 100\right)\]

&lt;p&gt;Theo đó, 0,5 m/s tương ứng 100 và 0,2 m/s tương ứng 40. Hằng số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.5&lt;/code&gt; được khai báo rõ ở đầu tệp; nếu phép đo thực tế ở chương 07 cho tốc độ vành bánh khác tại mức 100, hãy cập nhật đồng thời hằng số và ghi chú ở cả hai phía. Tránh lỗi ép kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; trước khi nhân tỉ lệ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int(0.2)&lt;/code&gt; bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;), vì khi đó robot sẽ đứng yên dù dữ liệu trên topic hoàn toàn đúng.&lt;/p&gt;

&lt;p&gt;Payload có định dạng ASCII &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,{left},{right}&lt;/code&gt; (bánh trái đứng trước), bọc trong khung cố định gồm byte mở đầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xAA&lt;/code&gt;, một byte độ dài payload, chuỗi payload, một byte checksum XOR trên toàn bộ payload và byte kết thúc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0A&lt;/code&gt;:&lt;/p&gt;

\[c = b_0 \oplus b_1 \oplus \cdots \oplus b_{n-1}\]

&lt;p&gt;Cầu nối gửi lặp lại khung lệnh mới nhất ở tần số khoảng 20 Hz. Do firmware có timeout 300 ms, việc chỉ gửi một khung duy nhất rồi ngừng sẽ khiến mạch tự dừng dù ROS vẫn muốn xe tiếp tục chạy. Đồng thời, một timer bên trong cầu nối theo dõi thời điểm nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; gần nhất: nếu quá 0,3 s không có lệnh mới, khung gửi đi sẽ tự động chuyển thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Cả hai lớp bảo vệ đều bắt buộc phải có: nếu node teleop tắt nhưng cầu nối vẫn chạy, cầu nối chịu trách nhiệm gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt;; nếu chính cầu nối bị tắt đột ngột hoặc cáp USB bị rút, watchdog trên firmware sẽ dừng động cơ (khối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;finally&lt;/code&gt; của Python sẽ không kịp chạy khi tiến trình bị ngắt cưỡng bức hoặc mất cáp). Khi kiểm thử, cần phân biệt rõ phép thử dừng publisher với phép thử rút cáp.&lt;/p&gt;

&lt;p&gt;Dữ liệu telemetry phản hồi dạng văn bản từ MCU (nếu có) sẽ được node đọc không chặn và xuất bản thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std_msgs/String&lt;/code&gt; lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/capstone_telem&lt;/code&gt;, đảm bảo không làm trễ nhịp gửi 20 Hz. Trong bước chạy thử nghiệm với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt;, parser chỉ đọc một chiều nên bằng chứng kiểm thử là các dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt; in ra trên terminal parser.&lt;/p&gt;

&lt;p&gt;Lưu mã nguồn sau thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bridge.py&lt;/code&gt;. Hai tham số cấu hình là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;port&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;baud&lt;/code&gt;: khi chạy thử không có xe, trỏ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;port&lt;/code&gt; vào một đầu PTY; khi nối mạch thật, trỏ vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; (sau khi đã thêm user vào nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;). Giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; ở mặc định (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;False&lt;/code&gt;) để phép đo timeout 0,3 s chạy theo đồng hồ thực thay vì bị treo chờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/env python3
&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Capstone A bridge: Twist in, chapter 08 frame out.

Scale: 0.5 m/s of tread speed -&amp;gt; integer 100. Track L = 0.16 m.
No Twist for 0.3 s -&amp;gt; V,0,0 on the wire. Firmware watchdog is the
backup if this process dies.
&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rclpy&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rclpy.node&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Node&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;geometry_msgs.msg&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Twist&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std_msgs.msg&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;TRACK_M&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.16&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;FULL_SCALE_MPS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;WATCHDOG_S&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.3&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;V,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ascii&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;xor_byte&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;xor_byte&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xAA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xor_byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wheel_ints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TRACK_M&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;v_l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;v_r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FULL_SCALE_MPS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;100.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CapstoneBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;capstone_bridge&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;declare_parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/dev/ttyUSB0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;declare_parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;baud&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_parameter_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_value&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;baud&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;baud&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_parameter_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integer_value&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_twist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_logged&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_subscription&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Twist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;cmd_vel&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;on_twist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_publisher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;capstone_telem&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_timer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_twist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Twist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wheel_ints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;angular&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_twist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_twist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nanoseconds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-9&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WATCHDOG_S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;if &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_logged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;wire V,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_logged&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ascii&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;publish&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rclpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CapstoneBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;rclpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;spin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;KeyboardInterrupt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;destroy_node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;rclpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;shutdown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;__main__&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lưu ý rằng lệnh đi thẳng không phát hiện được lỗi đảo chiều trái/phải vì hai bánh có cùng giá trị; cần kiểm tra thêm bằng lệnh quay với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; dương (giá trị bánh phải phải lớn hơn bánh trái).&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Khi chạy thẳng (\(v = 0.2\), \(\omega = 0\), \(L = 0.16\)):&lt;/p&gt;

\[v_L = v_R = 0.2\]

\[n = \mathrm{round}\left(0.2 \cdot \frac{100}{0.5}\right) = 40\]

&lt;p&gt;Payload thu được là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt;. Khi quay trái tại chỗ (\(v = 0\), \(\omega = +0.5\)):&lt;/p&gt;

\[v_L = -0.5 \times 0.08 = -0.04, \qquad v_R = 0.04\]

\[n_L = \mathrm{round}(-0.04 \times 200) = -8, \qquad n_R = 8\]

&lt;p&gt;Payload thu được là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,-8,8&lt;/code&gt;. Nếu parser hiển thị ngược thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,8,-8&lt;/code&gt;, thứ tự trả về trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_ints&lt;/code&gt; đang bị đảo và xe sẽ quay sai chiều trên thực tế.&lt;/p&gt;

&lt;p&gt;Tạo cặp cổng serial ảo bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;socat python3-serial
socat &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; pty,raw,echo&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0 pty,raw,echo&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; sẽ in ra hai đường dẫn PTY (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/pts/3&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/pts/4&lt;/code&gt;). Trỏ cầu nối vào một đầu và dùng parser từ bài 08-01 lắng nghe ở đầu kia. Lưu mã parser thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pty_parser.py&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytearray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;xor_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xAA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0A&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;check&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;xor_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;DROP&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Chạy parser trên đầu PTY thứ hai:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python3 pty_parser.py /dev/pts/4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Chạy cầu nối trên đầu PTY thứ nhất:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
python3 capstone_bridge.py &lt;span class=&quot;nt&quot;&gt;--ros-args&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; port:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/dev/pts/3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở terminal thứ tư và gửi một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 topic pub &lt;span class=&quot;nt&quot;&gt;--once&lt;/span&gt; /cmd_vel geometry_msgs/msg/Twist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;{linear: {x: 0.2, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Parser phải in ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,40,40&lt;/code&gt; vài lần ở nhịp 20 Hz, sau đó (trong vòng chưa đầy một giây kể từ khi ngừng gửi) tự chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,0,0&lt;/code&gt;. Log của node cầu nối cũng hiển thị chính xác hai chuỗi này khi giá trị thay đổi.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-kiểm-chứng-độ-trễ-khứ-hồi-round-trip-latency-trên-cầu-nối-sbc--mcu&quot;&gt;Giải thích: Kiểm chứng độ trễ khứ hồi (Round-Trip Latency) trên cầu nối SBC ↔ MCU&lt;/h3&gt;

&lt;p&gt;Để biết cầu nối phần cứng có đủ nhanh cho vòng điều hướng 20 Hz (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50 ms/nhịp&lt;/code&gt;) hay không, ta cộng từng chặng trễ của 1 lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Chặng xử lý trên đường đi của lệnh&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Độ trễ ước lượng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1. ROS 2 DDS truyền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; nội bộ trên SBC&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,5 ms&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2. Node cầu nối Python đóng gói khung tin + ghi xuống USB-Serial (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200 bps&lt;/code&gt;, 30 byte)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3,0 ms&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;3. MCU chờ đến nhịp vòng lặp điều khiển tiếp theo (50 Hz, trung bình \(T_s/2\))&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10,0 ms&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;4. MCU gửi ngược số đếm encoder (30 byte) về lại SBC và xuất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3,5 ms&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tổng độ trễ khứ hồi (Round-Trip)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;~&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;17,0 ms&lt;/code&gt;&lt;/strong&gt; (&amp;lt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50 ms&lt;/code&gt; → Đạt chuẩn!)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nếu tổng độ trễ vượt quá &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;40–50 ms&lt;/code&gt; (ví dụ do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep()&lt;/code&gt; chặn luồng hoặc buffer USB bị tích tụ), xe thật sẽ xuất hiện hiện tượng &lt;em&gt;lắc đuôi (oscillation)&lt;/em&gt; khi Nav2 cố bám đường cong!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Cài &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3-serial&lt;/code&gt;, nạp môi trường Jazzy và xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Khởi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; như ví dụ, ghi lại hai đường dẫn PTY và giữ tiến trình hoạt động.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pty_parser.py&lt;/code&gt; trên đường dẫn PTY thứ hai.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bridge.py&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-p port:=&lt;/code&gt; trỏ vào đường dẫn PTY thứ nhất. Nếu gặp traceback khi mở cổng, kiểm tra xem bạn có nhập nhầm đường dẫn hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; đã thoát chưa.&lt;/li&gt;
  &lt;li&gt;Xuất bản một message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x = 0.2&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z = 0&lt;/code&gt;, theo dõi đầu ra của parser.&lt;/li&gt;
  &lt;li&gt;Chờ khoảng một giây không gửi thêm lệnh nào: parser phải chuyển từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt; về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; trong khi cầu nối vẫn chạy bình thường.&lt;/li&gt;
  &lt;li&gt;Xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z = 0.5&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x = 0&lt;/code&gt; và xác nhận parser in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,-8,8&lt;/code&gt; (nếu hiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,8,-8&lt;/code&gt;, sửa ngay hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_ints&lt;/code&gt; trước khi thử với động cơ thật).&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;(Tùy chọn với phần cứng thật)&lt;/em&gt; Đặt xe Capstone lên bàn với hai bánh nhấc khỏi mặt đất, cấp nguồn động cơ từ pin riêng và chỉ dùng cáp USB cho serial. Nếu Python báo lỗi quyền mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;, thêm user vào nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; rồi đăng xuất/đăng nhập lại. Chạy cầu nối với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-p port:=/dev/ttyUSB0&lt;/code&gt;: hai bánh phải nhích tiến khi có lệnh thẳng rồi dừng ngay sau khi ngừng gửi. Kiểm tra thêm một lần tình huống rút cáp USB (vẫn giữ bánh trên không) để xác nhận watchdog firmware tự đưa PWM về 0.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ngay sau lệnh chạy thẳng, parser in:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;OK V,40,40
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Khoảng một giây sau khi ngừng gửi (cầu nối vẫn hoạt động):&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;OK V,0,0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Phép kiểm tra góc quay in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,-8,8&lt;/code&gt;. Khi thử trên phần cứng thật, cả hai bánh chỉ quay tiến trong chưa đầy một giây rồi dừng hẳn, không được tiếp tục quay sau khi publisher kết thúc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,8,-8&lt;/code&gt; khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; dương&lt;/td&gt;
      &lt;td&gt;Bị đảo thứ tự trái/phải trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_ints&lt;/code&gt; hoặc trong chuỗi payload&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,0,0&lt;/code&gt; (hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,20,20&lt;/code&gt;) cho lệnh 0,2 m/s&lt;/td&gt;
      &lt;td&gt;Ép vận tốc mét/giây sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; quá sớm, hoặc đặt sai hệ số full scale là 1,0 m/s thay vì 0,5 m/s&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Parser tiếp tục in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt; sau khi ngừng gửi&lt;/td&gt;
      &lt;td&gt;Logic watchdog của cầu nối không nằm trong vòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tick&lt;/code&gt; gửi khung (và firmware cũng sẽ chạy mãi nếu thiếu timeout)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh vẫn quay sau khi tắt cầu nối&lt;/td&gt;
      &lt;td&gt;Watchdog trên firmware chưa hoạt động hoặc có ngưỡng thời gian lớn hơn 300 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Permission denied&lt;/code&gt; khi mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Tài khoản chưa thuộc nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; hoặc chưa đăng nhập lại sau khi cập nhật nhóm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Parser không in gì dù cầu nối đang chạy&lt;/td&gt;
      &lt;td&gt;Mở nhầm cùng một đường PTY, hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo=0&lt;/code&gt; / có tiến trình khác đang đọc chung cổng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài thực hành không cần mua thêm linh kiện mới: phần chạy thử dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; và Python, phần kiểm thử thật dùng xe Capstone và cáp USB sẵn có. Chỉ cần tìm mua thay thế bo “ESP32” trên &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 DevKit V1 30 chân&lt;/code&gt; trên Shopee (giá tham khảo 2026 khoảng 80.000–180.000 đồng) nếu mạch cũ bị hỏng. Tuyệt đối không nuôi động cơ từ nguồn cổng USB.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=48SxC6LBVs8&quot;&gt;How to start develop micro-ROS on ESP32 quickly!&lt;/a&gt; — stepbystep-robotics (tiếng Anh). Minh họa kết nối giữa firmware ESP32 và đồ thị ROS 2, tương ứng với phía vi điều khiển của Capstone A.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7TVWlADXwRw&quot;&gt;What Is ROS2? - Framework Overview&lt;/a&gt; — Raymond Andrade (tiếng Anh). Nhắc lại cơ chế truyền nhận topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, giúp kiểm tra đồ thị trước khi nghi ngờ lỗi động cơ.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tính cặp số nguyên truyền trên dây khi \(v = 0.2\) m/s, \(\omega = 0.5\) rad/s với \(L = 0.16\) m và hệ số toàn thang 0,5 m/s. &lt;em&gt;Gợi ý:&lt;/em&gt; vận tốc vành trái là 0,16 m/s (tương ứng 32), vành phải là 0,24 m/s (tương ứng 48), nên payload là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,32,48&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Sau khi publisher dừng một giây trong khi cầu nối vẫn chạy, payload nào đang được truyền trên dây và bộ định thời nào phát ra nó? &lt;em&gt;Gợi ý:&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt;, phát ra từ watchdog 0,3 s theo dõi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; trong cầu nối (không phải từ firmware, vì các khung serial vẫn đến đều ở 20 Hz).&lt;/li&gt;
  &lt;li&gt;Nếu tiến trình cầu nối bị tắt đột ngột khi động cơ đang quay (bánh nhấc trên không), cơ chế nào sẽ dừng bánh và sau bao lâu? &lt;em&gt;Gợi ý:&lt;/em&gt; watchdog trên firmware chương 08 sẽ ngắt sau khoảng 300 ms kể từ khung hợp lệ cuối cùng, vì khối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;finally&lt;/code&gt; của cầu nối không kịp thực thi.&lt;/li&gt;
  &lt;li&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls -l /dev/ttyUSB0&lt;/code&gt; hiển thị nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; nhưng Python vẫn báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Permission denied&lt;/code&gt;. Bạn cần xử lý thế nào và vì sao phải đăng nhập lại? &lt;em&gt;Gợi ý:&lt;/em&gt; thêm user vào nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;; danh sách nhóm của người dùng chỉ được nạp lại khi bắt đầu phiên đăng nhập mới nên mở terminal mới trong phiên cũ chưa có tác dụng.&lt;/li&gt;
  &lt;li&gt;Khi nhận một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y = 0.3&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x = 0&lt;/code&gt;, cầu nối sẽ gửi giá trị số nguyên nào và bỏ qua thành phần nào? &lt;em&gt;Gợi ý:&lt;/em&gt; gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0, 0&lt;/code&gt; vì chỉ tính từ \(v\) và \(\omega\); thành phần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; bị bỏ qua do cơ cấu vi sai không thể trượt ngang.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Using-ros2-param.html&quot;&gt;Tham số Jazzy&lt;/a&gt; — hướng dẫn chi tiết đằng sau cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-p port:=&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;declare_parameter&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; — chuẩn quy ước dấu của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; được cài đặt trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_ints&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; — chuẩn tên frame giữ thống nhất khi dùng chung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; giữa mô phỏng và cầu nối thực tế.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; — giải pháp thay thế cầu nối về sau, chỉ nên quay lại sau khi parser đã xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,40,40&lt;/code&gt; và hành vi watchdog.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>micro-ROS trên MCU</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_03_microros_mcu/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_03_microros_mcu</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~75 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Ở Chương 08, ta đã tự viết giao thức serial văn bản nối MCU (ESP32) với máy tính chủ — gọn gàng nhưng mỗi lần thêm một cảm biến mới bạn lại phải tự định nghĩa cú pháp chuỗi, tự parse và tự viết node ROS 2 trung gian. &lt;strong&gt;micro-ROS&lt;/strong&gt; mang chính mô hình Node–Topic–Service của ROS 2 xuống tận vi điều khiển có vài trăm kilobyte RAM, biến ESP32 thành một “công dân hạng nhất” ngay trên đồ thị ROS 2.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-micro-ros-trên-vi-điều-khiển-mcu&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của micro-ROS trên Vi điều khiển (MCU)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Kiến trúc Client–Agent trên chuẩn XRCE-DDS&lt;/strong&gt;:
Vì DDS đầy đủ quá nặng cho MCU, micro-ROS chạy thư viện nhẹ &lt;strong&gt;XRCE-DDS Client&lt;/strong&gt; trên ESP32, nói chuyện qua UART/USB hoặc Wi-Fi UDP với một tiến trình &lt;strong&gt;micro-ROS Agent&lt;/strong&gt; trên SBC/laptop — chính Agent làm cầu nối đại diện cho MCU tham gia mạng DDS của ROS 2.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lập trình không cấp phát động (Static Allocation &amp;amp; Executor)&lt;/strong&gt;:
Trên MCU không có bộ nhớ ảo, gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc()&lt;/code&gt; liên tục trong vòng lặp sẽ gây phân mảnh RAM và treo máy sau vài giờ. micro-ROS cấp phát tĩnh toàn bộ vùng nhớ message ngay lúc khởi động và điều phối callback bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclc_executor&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đánh đổi giữa Custom Serial Bridge và micro-ROS&lt;/strong&gt;:
Custom serial gọn nhẹ, dễ gỡ lỗi bằng công cụ terminal thường; micro-ROS tốn thời gian thiết lập toolchain hơn nhưng chuẩn hóa hoàn toàn thông điệp và dễ mở rộng cho hệ thống nhiều cảm biến.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ giải thích được kiến trúc XRCE-DDS gồm một client chạy trên vi điều khiển (MCU) kết hợp với một agent trên máy tính — trong đó agent mới là thực thể (participant) DDS trực tiếp tham gia đồ thị ROS 2. Bạn sẽ dự đoán chính xác rằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; không hiển thị topic của MCU khi agent tắt, và vẫn chưa hiển thị khi agent đã bật nhưng chưa có client kết nối. Bạn khởi chạy được container micro-ROS agent phiên bản Jazzy qua giao thức UDP, viết đúng cú pháp lệnh tương ứng cho chế độ serial theo tài liệu của agent, đồng thời nắm rõ lộ trình khóa học: hoàn thiện cầu nối serial bằng Python ở bài 10-04 trước, sau đó mới chuyển sang micro-ROS khi đơn vị vận tốc và watchdog 300 ms đã được kiểm chứng. Cuối bài, bạn lập bản kế hoạch topic cho client (dùng lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, topic telemetry và tên frame đồng bộ với mô phỏng) ngay cả khi chưa nạp mạch thật.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Nắm vững mô hình node – topic ở bài 09-02, khung dữ liệu serial ở bài 08-01 và môi trường Jazzy ở bài 10-01. Bài lab yêu cầu cài sẵn Docker để chạy container agent. Trong buổi này bạn chưa cần tệp firmware micro-ROS hoàn chỉnh và không nên vội viết lại phần teleop của Capstone sang micro-ROS trước khi hoàn tất bài 10-04. Bo mạch được hỗ trợ trong bài là ESP32 DevKit (không áp dụng cho Raspberry Pi Pico).&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Firmware Capstone hiện tại nhận khung dữ liệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,left,right&lt;/code&gt; và tự động ngắt lực động cơ khi mất tín hiệu. ROS 2 không hiểu trực tiếp định dạng khung đó, còn ESP32 không đủ tài nguyên để chạy toàn bộ ngăn xếp DDS đầy đủ. Vì vậy, node cầu nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt; ở bài 10-04 là bước triển khai đầu tiên: dễ gỡ lỗi bằng Python, tái sử dụng giao thức đã kiểm chứng và vẫn có watchdog trên firmware bảo vệ nếu tiến trình cầu bị tắt đột ngột. micro-ROS là bước nâng cấp tiếp theo: client chạy ngay trên MCU, agent trên máy tính chuyển đổi XRCE-DDS sang DDS, cho phép &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; đi thẳng tới firmware mà không cần lớp quy đổi Python trung gian. Dù dùng cách nào, Nav2 và các mô hình học máy sau này vẫn chỉ xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; — tuy nhiên chúng không thể giúp bạn gỡ lỗi một agent chưa từng kết nối với client.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch10_microros.png&quot; alt=&quot;Client micro-ROS trên ESP32, agent trên máy tính, rồi đồ thị ROS 2&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;DDS là lớp middleware nằm bên dưới các topic ROS 2, và mỗi node &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt; thông thường là một DDS participant đầy đủ. Các vi điều khiển tài nguyên hạn chế không thể chạy trực tiếp ngăn xếp này một cách hiệu quả về RAM và CPU. Chuẩn XRCE-DDS ra đời để giải quyết giới hạn đó: vi điều khiển chạy một micro-ROS client nhẹ và không tự xuất hiện trên đồ thị ROS; thay vào đó, tiến trình &lt;strong&gt;agent&lt;/strong&gt; trên máy tính đóng vai trò participant đại diện. Khi client tạo subscriber hoặc publisher, agent sẽ khởi tạo thực thể ROS 2 tương ứng và chuyển tiếp dữ liệu giữa hai bên.&lt;/p&gt;

&lt;p&gt;Cơ chế này giải thích vì sao danh sách topic ban đầu bị trống: khi agent chưa chạy thì không có đại diện nào cho MCU; còn khi agent đã bật nhưng ở trạng thái chờ, chưa có phiên làm việc (session) nào từ client nên các topic của MCU chưa được tạo. Việc chỉ thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/parameter_events&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/rosout&lt;/code&gt; chứng tỏ agent đang chờ bình thường chứ không phải lỗi cài đặt.&lt;/p&gt;

&lt;p&gt;Khóa học dùng image Docker chính thức gắn đúng thẻ phiên bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;microros/micro-ros-agent:jazzy&lt;/code&gt;. Trong bài lab không cần mạch thật, ta chạy agent theo giao thức UDP trên laptop:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--net&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;host microros/micro-ros-agent:jazzy udp4 &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt; 8888
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--net=host&lt;/code&gt; đưa container vào chung không gian mạng của máy chủ để khi có client kết nối, hệ thống DDS trên laptop nhận diện được agent (mạng bridge mặc định của Docker sẽ ẩn agent khỏi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; nếu không cấu hình DDS bổ sung). Cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--rm&lt;/code&gt; tự dọn container sau khi thoát và không can thiệp vào hệ thống ROS Jazzy cài trên máy.&lt;/p&gt;

&lt;p&gt;Khi kết nối ESP32 thật qua cổng USB-serial, Docker cần được cấp quyền truy cập thiết bị của máy chủ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev&lt;/code&gt;) và truyền tham số serial cho agent:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--privileged&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /dev:/dev &lt;span class=&quot;nt&quot;&gt;--net&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;host &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  microros/micro-ros-agent:jazzy serial &lt;span class=&quot;nt&quot;&gt;--dev&lt;/span&gt; /dev/ttyUSB0 &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; 115200
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Các cờ đứng trước tên image thuộc về Docker, còn các tham số sau từ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;serial&lt;/code&gt; thuộc về agent (đường dẫn cổng và tốc độ baud khớp với chương 08; nếu có thay đổi cú pháp, ưu tiên theo README chính thức của agent). Tuyệt đối không chạy đồng thời container này với cầu nối Python ở bài 10-04 vì tại một thời điểm chỉ một tiến trình được quyền mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Về phần cứng, ESP32 nằm trong nhóm vi điều khiển được micro-ROS hỗ trợ chính thức thông qua ESP-IDF. Ngược lại, Raspberry Pi Pico chỉ có bản chuyển đổi từ cộng đồng (thường theo distro khác và không đảm bảo tương thích với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;microros/micro-ros-agent:jazzy&lt;/code&gt;). Hãy dành Pico cho MicroPython và dùng ESP32 DevKit cho micro-ROS.&lt;/p&gt;

&lt;p&gt;Khi nạp client về sau, cần giữ đồng nhất quy ước tên ROS với môi trường mô phỏng: subscribe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt;), publish dữ liệu phản hồi lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/capstone_telem&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std_msgs/msg/String&lt;/code&gt;), và nếu xuất bản odometry thì dùng đúng cặp frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; theo REP-105 (bài 09-05). Bên trong firmware, lệnh bánh xe vẫn quy đổi về số nguyên trong đoạn −100…100 theo đúng hệ số ở bài 10-04; micro-ROS không làm thay đổi mô hình động học.&lt;/p&gt;

&lt;p&gt;Quan trọng nhất, cơ chế watchdog không được chuyển lên agent. Agent có thể sập, Docker có thể mất quyền thiết bị hoặc mạng Wi-Fi UDP có thể gián đoạn; do đó firmware trên MCU vẫn bắt buộc đưa vận tốc bánh về 0 nếu quá 300 ms không nhận được lệnh mới:&lt;/p&gt;

\[\Delta t &amp;gt; 0.3\,\mathrm{s} \Rightarrow (n_L, n_R) = (0, 0)\]

&lt;p&gt;Đây là cùng một ngưỡng an toàn như cầu nối serial. Việc giữ nguyên lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; cuối chỉ vì tin rằng “DDS đáng tin cậy” sẽ dẫn đến trạng thái hỏng nguy hiểm khi mất kết nối: độ tin cậy là đặc tính của kênh truyền, còn dừng an toàn phải là trách nhiệm tại chỗ của vi điều khiển.&lt;/p&gt;

&lt;p&gt;Ngoài ra, không bao giờ cấp nguồn cho mạch điều khiển động cơ từ cổng USB đang nạp ESP32 hoặc chạy agent, vì sụt áp sẽ làm chip khởi động lại và ngắt phiên làm việc giữa chừng. Khi chuyển sang chạy phần cứng thật, động cơ phải dùng khối pin riêng có cầu chì và nối chung mass (GND).&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Trước khi chạy Docker, hãy viết bản kế hoạch topic ra sổ thực hành (đây là một nửa yêu cầu của bài lab):&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Node name I will look for: /capstone_mcu
Subscribes: /cmd_vel   geometry_msgs/msg/Twist
Publishes:  /capstone_telem   std_msgs/msg/String
Frames if odometry is added later: odom -&amp;gt; base_link
Local rule: no fresh command for 300 ms =&amp;gt; wheel integers 0,0
Not expected tonight: that node name in ros2 node list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tiếp theo, kiểm tra phiên bản ROS và khởi chạy agent không gắn mạch:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;printenv &lt;/span&gt;ROS_DISTRO
docker run &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--net&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;host microros/micro-ros-agent:jazzy udp4 &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt; 8888
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Đảm bảo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DISTRO&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt; và giữ container chạy ở cửa sổ chính. Agent hoạt động bình thường sẽ báo đang lắng nghe trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udp4&lt;/code&gt; cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8888&lt;/code&gt; và không hiển thị phiên client nào.&lt;/p&gt;

&lt;p&gt;Mở terminal thứ hai để kiểm tra đồ thị ROS:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 node list
ros2 topic list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Kết quả không được chứa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/capstone_mcu&lt;/code&gt; — sự vắng mặt này chính là bằng chứng cho thấy agent chưa tạo thực thể khi chưa có client kết nối.&lt;/p&gt;

&lt;p&gt;Nhấn Ctrl-C để dừng container, tránh chiếm giữ cổng 8888 cho các lần chạy sau. Khi kết nối mạch thật ở buổi thực hành phần cứng, log của agent sẽ ghi nhận phiên mới và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 node list&lt;/code&gt; sẽ hiện đúng tên node bạn đã lên kế hoạch.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-kiểm-tra-băng-thông-uart-115200-bps-cho-cầu-nối-micro-ros&quot;&gt;Giải thích: Kiểm tra băng thông UART &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200 bps&lt;/code&gt; cho cầu nối micro-ROS&lt;/h3&gt;

&lt;p&gt;Giả sử ESP32 nối với Raspberry Pi qua UART ở tốc độ truyền mặc định &lt;strong&gt;115.200 baud&lt;/strong&gt; (chuẩn 8N1 = 10 bit/byte, nên tốc độ tối đa lý thuyết là \(115.200 / 10 = \mathbf{11.520\text{ byte/s}}\)):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Nếu ESP32 xuất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; (~120 byte đã tuần tự hóa CDR + header XRCE) ở &lt;strong&gt;50 Hz&lt;/strong&gt;: tốn \(120 \times 50 = \mathbf{6.000\text{ byte/s}}\) (~52% băng thông).&lt;/li&gt;
  &lt;li&gt;Nếu đồng thời xuất thêm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/imu/data&lt;/code&gt; (~150 byte) ở &lt;strong&gt;50 Hz&lt;/strong&gt;: tốn thêm \(150 \times 50 = \mathbf{7.500\text{ byte/s}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tổng yêu cầu = \(13.500\text{ byte/s} &amp;gt; 11.520\text{ byte/s}\) → NGHẼN CỔ CHAI UART&lt;/strong&gt;, gây rớt gói và độ trễ tích lũy!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cách xử lý chuẩn:&lt;/strong&gt; Nâng tốc độ baud lên &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;921600 bps&lt;/code&gt;&lt;/strong&gt; (cho phép ~92 KB/s, gấp \(8\times\)) hoặc dùng kết nối USB-CDC / Wi-Fi UDP!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đọc lại kiến trúc micro-ROS kèm README của agent; xác nhận sử dụng đúng thẻ image &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt; (không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iron&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Chép bản kế hoạch topic mẫu vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, đảm bảo ghi rõ luật dừng cục bộ 300 ms và cặp frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Chạy container agent UDP theo đúng lệnh đã nêu và chờ log xác nhận đang lắng nghe trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udp4&lt;/code&gt; cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8888&lt;/code&gt; (lần đầu tải image có thể mất vài phút).&lt;/li&gt;
  &lt;li&gt;Mở terminal khác, nạp Jazzy, chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 node list&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt;, dán kết quả vào sổ kèm câu giải thích vì sao node MCU chưa xuất hiện.&lt;/li&gt;
  &lt;li&gt;Chưa cần nạp firmware trong buổi này (trừ khi bạn đã có sẵn ảnh micro-ROS muốn thử thêm); log agent đang chờ kèm bản kế hoạch là đủ tiêu chuẩn hoàn thành lab.&lt;/li&gt;
  &lt;li&gt;Dừng container. Nếu ghi chú thêm lệnh chạy chế độ serial, phải liệt kê đủ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--privileged&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-v /dev:/dev&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--dev /dev/ttyUSB0&lt;/code&gt; và lưu ý không chạy song song với cầu nối Python.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Container chạy ổn định ở foreground, log xác nhận lắng nghe UDP cổng 8888 và chưa có client; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 node list&lt;/code&gt; không chứa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/capstone_mcu&lt;/code&gt;; sổ thực hành ghi đầy đủ thông tin subscribe, publish và quy tắc watchdog 300 ms:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;agent waiting on udp4 port 8888
ros2 node list: no MCU node
lab-notes: cmd_vel in, capstone_telem out, frames odom and base_link
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker: command not found&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Chưa cài đặt Docker Engine trên máy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lỗi từ chối quyền truy cập socket Docker&lt;/td&gt;
      &lt;td&gt;User chưa thuộc nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker&lt;/code&gt; hoặc chưa đăng nhập lại (có thể dùng tạm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Kéo nhầm thẻ image &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Không tương thích phiên bản ROS trên laptop; đổi sang thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:jazzy&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Không thấy node MCU trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 node list&lt;/code&gt; nên cài lại ROS&lt;/td&gt;
      &lt;td&gt;Chưa có client kết nối — trạng thái trống là hoàn toàn bình thường&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cả agent serial và cầu Python cùng mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Xung đột quyền chiếm cổng serial; phải tắt một trong hai tiến trình&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 tự reset khi cấp lệnh quay bánh&lt;/td&gt;
      &lt;td&gt;Động cơ lấy chung nguồn USB với mạch; cần tách sang nguồn pin riêng của xe&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lỗi biên dịch theo hướng dẫn micro-ROS cho Pico&lt;/td&gt;
      &lt;td&gt;Pico chỉ có bản hỗ trợ không chính thức từ cộng đồng, không thuộc lộ trình ESP32&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Buổi chạy thử nghiệm này chỉ cần Docker trên laptop, không cần phần cứng mới. Khi chuyển sang nạp mạch thật, dùng đúng bo ESP32 DevKit của Capstone A (tìm từ khóa “ESP32” trên &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 DevKit V1 30 chân&lt;/code&gt; trên Shopee/Lazada, giá tham khảo 2026 khoảng 80.000–180.000 đồng — tránh mua module trần không có chip USB). Không mua Pico cho bài này và không cấp nguồn động cơ từ cáp USB.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=aD3Lf-9cb0A&quot;&gt;Intro to micro-ROS&lt;/a&gt; — Marine Robotics Group at Georgia Tech (tiếng Anh). Giải thích tổng quan cách MCU giao tiếp DDS thông qua agent.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=48SxC6LBVs8&quot;&gt;How to start develop micro-ROS on ESP32 quickly!&lt;/a&gt; — stepbystep-robotics (tiếng Anh). Thực hành micro-ROS trên ESP32 và agent; nếu video dùng thẻ image khác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;, hãy giữ đúng thẻ của bài học này.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Khi container agent đang chạy nhưng bạn chưa nạp firmware cho mạch, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; chắc chắn không hiển thị điều gì và vì sao? &lt;em&gt;Gợi ý:&lt;/em&gt; không hiển thị các topic của MCU, vì agent chỉ khởi tạo chúng sau khi thiết lập được phiên làm việc với client.&lt;/li&gt;
  &lt;li&gt;Dù bài này nằm trước bài 10-04 về thứ tự đánh số, vì sao khóa học vẫn khuyến nghị hoàn thiện cầu nối serial Python trước? &lt;em&gt;Gợi ý:&lt;/em&gt; firmware Capstone đã hỗ trợ sẵn khung truyền chương 08; xây dựng cầu nối Python giúp kiểm chứng đơn vị vận tốc và watchdog 300 ms trước khi chuyển sang tầng phức tạp hơn là micro-ROS.&lt;/li&gt;
  &lt;li&gt;Viết lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run&lt;/code&gt; cho agent serial trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; và phân biệt rõ những cờ nào thuộc về Docker, những cờ nào thuộc về agent. &lt;em&gt;Gợi ý:&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--privileged&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-v /dev:/dev&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--net=host&lt;/code&gt; thuộc Docker; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--dev&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-b&lt;/code&gt; đứng sau từ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;serial&lt;/code&gt; thuộc về agent.&lt;/li&gt;
  &lt;li&gt;Tóm tắt giao ước của client trong bốn dòng: topic subscribe, topic publish, tên frame và timeout. &lt;em&gt;Gợi ý:&lt;/em&gt; subscribe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;), publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/capstone_telem&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;), frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, đưa lệnh về 0 sau 300 ms không có dữ liệu mới.&lt;/li&gt;
  &lt;li&gt;Nếu một người bạn muốn tận dụng bo Raspberry Pi Pico có sẵn để làm bài micro-ROS này, bạn sẽ nhận xét ngắn gọn trong hai câu như thế nào? &lt;em&gt;Gợi ý:&lt;/em&gt; ESP32 là dòng mạch được hỗ trợ chính thức cho agent Jazzy của khóa học; Pico chỉ có bản chuyển đổi cộng đồng nên không đảm bảo tương thích và không nằm trong lộ trình bài thực hành.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; — tài liệu tổng quan về kiến trúc client – XRCE-DDS – agent.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/micro-ROS/micro-ROS-Agent&quot;&gt;Kho agent micro-ROS&lt;/a&gt; — nơi lưu các câu lệnh Docker chuẩn cho cả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udp4&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;serial&lt;/code&gt; (ưu tiên kiểm tra tại đây nếu cú pháp cờ thay đổi).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; — chuẩn tên frame cần giữ đồng nhất giữa mô phỏng và vi điều khiển.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Bring-up URDF trong mô phỏng</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_02_urdf_sim_bringup/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_02_urdf_sim_bringup</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~90 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Có tệp URDF hiển thị đẹp trong RViz2 mới chỉ là một nửa chặng đường: nếu ném thẳng URDF đó vào Gazebo Harmonic, chiếc robot sẽ nằm im bất động như một bức tượng nhựa vì nó chưa có &lt;strong&gt;plugin động cơ vi sai&lt;/strong&gt;, chưa có &lt;strong&gt;plugin cảm biến LiDAR/IMU&lt;/strong&gt; và chưa được nối qua cầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt;. Bài học này hướng dẫn cách thổi “sự sống vật lý” vào mô hình URDF để tạo nên gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sim_bringup&lt;/code&gt; hoàn chỉnh.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-đưa-urdf-vào-mô-phỏng-gazebo&quot;&gt;Kiến thức cần nhớ: Ba trụ cột Đưa URDF vào Mô phỏng Gazebo&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Thẻ mở rộng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;gazebo&amp;gt;&lt;/code&gt; &amp;amp; Plugin hệ thống (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz-sim-diff-drive-system&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz-sim-sensors-system&lt;/code&gt;)&lt;/strong&gt;:
Gắn bộ truyền động vi sai nhận lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; tác động lực lên khớp bánh trái/phải và xuất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf&lt;/code&gt;, đồng thời gắn cảm biến LiDAR 2D/camera có &lt;strong&gt;nhiễu Gaussian thực tế&lt;/strong&gt; thay vì dữ liệu lý tưởng 100%.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đơn giản hóa hình học &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;collision&amp;gt;&lt;/code&gt; &amp;amp; Tham số ma sát (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mu1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mu2&lt;/code&gt;)&lt;/strong&gt;:
Dùng khối hộp/trụ đơn giản cho va chạm để giữ RTF cao; đặt ma sát cao cho 2 bánh chủ động và ma sát gần bằng 0 cho bánh đa hướng (caster wheel) để xe không bị kẹt khi xoay tại chỗ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Một lệnh Bringup duy nhất (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sim.launch.py&lt;/code&gt;)&lt;/strong&gt;:
Gom cả 4 thành phần — khởi động Gazebo, sinh robot (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;create&lt;/code&gt;), bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; và chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt; (kèm YAML ánh xạ topic) — vào một launch file duy nhất với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time=True&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ đưa được mô hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; từ chương 09 vào một world Gazebo Harmonic đang chạy theo đúng trình tự: biên dịch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; ra URDF, khởi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf -r&lt;/code&gt;, spawn mô hình bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_sim create&lt;/code&gt;, rồi bắc cầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; một chiều và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; hai chiều. Bạn sẽ cấu hình tên khớp bánh xe, khoảng cách hai bánh (0,16 m) và bán kính bánh (0,033 m) trong plugin diff-drive khớp hoàn toàn với tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt;, sau đó gửi một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; chậm để quan sát bánh xe lăn trực tiếp trong Gazebo. Đồng thời, bạn nắm được năm lỗi bring-up phổ biến (khiến RViz vẫn hiển thị bình thường nhưng robot trong mô phỏng đứng yên hoặc rơi xuyên sàn) và biết đồng bộ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; để timer và TF dùng chung một mốc thời gian.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành bài 10-01: world trống chạy ổn định và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; hiển thị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; từ bài 09-06 đã sẵn sàng với thân hộp, hai bánh trụ, khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; và đầy đủ thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt;. Nắm lại phép động học nghịch ở bài 08-04 để hiểu ý nghĩa của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; (\(v\) tiến và \(\omega\) quay quanh \(+z\)). Mọi cửa sổ terminal mới đều phải nạp Jazzy; RViz chỉ là tùy chọn vì tiêu chí đạt yêu cầu nằm ở chuyển động thực tế trong Gazebo.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Ở chương 09, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; mới chỉ cung cấp bản vẽ hình học và cây TF tĩnh; sang Harmonic, mô hình lần đầu tiên có khối lượng, va chạm vật lý và đồng hồ mô phỏng. Về sau, Nav2 sẽ xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;: trong mô phỏng, bộ nhận lệnh là plugin diff-drive của Gazebo, còn trên xe Capstone thật là cầu nối serial ở bài 10-04. Nếu thông số khoảng cách hai bánh hoặc bán kính trong plugin lệch với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt;, mọi dữ liệu bag ghi lại sau này đều bị sai tỉ lệ. Ngoài ra, thứ tự khởi chạy các tiến trình là kỹ năng cốt lõi vì việc bật ngược trình tự thường tạo ra hiện tượng giống hệt lỗi vật lý.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch10_sim_bringup.png&quot; alt=&quot;Thứ tự từ xacro tới mô hình lăn trong Harmonic&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Bring-up là một quy trình theo đúng trình tự, không phải một câu lệnh đơn lẻ.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 1: Tạo mô tả URDF.&lt;/strong&gt; Công cụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; khai triển các thuộc tính thành tệp URDF chuẩn để cả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; lẫn công cụ spawn đọc được. Mô hình khóa học gồm thân hộp, hai bánh trụ với khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left_wheel_joint&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right_wheel_joint&lt;/code&gt;, kèm thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt; trên mọi link có va chạm. Hai kích thước chuẩn là khoảng cách hai bánh 0,16 m và bán kính bánh 0,033 m; các giá trị này phải đồng bộ giữa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; và plugin để hình dáng hiển thị khớp với động học lăn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 2: Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;.&lt;/strong&gt; Node này xuất bản cây TF từ URDF (các khớp cố định vẫn hiển thị dù &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joint_states&lt;/code&gt; chưa có dữ liệu). Trong môi trường mô phỏng, node này cần sử dụng đồng hồ Gazebo sau khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; đã được bắc cầu, tránh tình trạng dấu thời gian lệch pha so với dữ liệu cảm biến và odometry thêm vào sau này.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 3: Mở world mô phỏng.&lt;/strong&gt; Khởi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; kèm cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; để server chạy ngay thay vì tạm dừng. Tên nội bộ của world trong tệp này là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 4: Spawn mô hình.&lt;/strong&gt; Công cụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;create&lt;/code&gt; đưa tệp URDF vào world đang chạy, trong đó tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-world&lt;/code&gt; phải khớp chính xác tên world:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_sim create &lt;span class=&quot;nt&quot;&gt;-world&lt;/span&gt; empty &lt;span class=&quot;nt&quot;&gt;-file&lt;/span&gt; /tmp/capstone.urdf &lt;span class=&quot;nt&quot;&gt;-name&lt;/span&gt; capstone
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu truyền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-world shapes&lt;/code&gt; khi server đang chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt;, lệnh sẽ báo lỗi không tìm thấy world. Tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-file&lt;/code&gt; yêu cầu tệp URDF đã khai triển chứ không nhận trực tiếp tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.xacro&lt;/code&gt; thô, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-name capstone&lt;/code&gt; là tên mô hình hiển thị trong giao diện Gazebo.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 5: Bắc cầu topic.&lt;/strong&gt; Nếu cần kiểm tra lại ký hiệu chiều truyền từ bài 10-01, chạy:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Đồng hồ chỉ đi một chiều từ Gazebo sang ROS (dùng ký hiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; giữa kiểu ROS và kiểu Gazebo):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lệnh vận tốc dùng dạng hai chiều (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt;) để &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; phát từ ROS đi tới plugin và dữ liệu từ phía Gazebo cũng phản hồi được về ROS:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge /cmd_vel@geometry_msgs/msg/Twist@gz.msgs.Twist
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bạn có thể truyền cả hai đối số vào chung một tiến trình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge&lt;/code&gt;. Không bao giờ dùng cầu nối hai chiều cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; để tránh tình trạng một node ROS khác phát đè mốc thời gian lên đồng hồ của Gazebo.&lt;/p&gt;

&lt;p&gt;Để chuyển đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; thành chuyển động quay của bánh xe trong Gazebo Sim, ta thêm khối plugin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DiffDrive&lt;/code&gt; vào tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; với các thông số khớp hoàn toàn với thuộc tính mô hình:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;gazebo&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;plugin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;filename=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;gz-sim-diff-drive-system&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;gz::sim::systems::DiffDrive&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;left_joint&amp;gt;&lt;/span&gt;left_wheel_joint&lt;span class=&quot;nt&quot;&gt;&amp;lt;/left_joint&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;right_joint&amp;gt;&lt;/span&gt;right_wheel_joint&lt;span class=&quot;nt&quot;&gt;&amp;lt;/right_joint&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;wheel_separation&amp;gt;&lt;/span&gt;0.16&lt;span class=&quot;nt&quot;&gt;&amp;lt;/wheel_separation&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;wheel_radius&amp;gt;&lt;/span&gt;0.033&lt;span class=&quot;nt&quot;&gt;&amp;lt;/wheel_radius&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;max_linear_velocity&amp;gt;&lt;/span&gt;0.5&lt;span class=&quot;nt&quot;&gt;&amp;lt;/max_linear_velocity&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/gazebo&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lưu ý &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left_joint&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right_joint&lt;/code&gt; phải là tên &lt;strong&gt;joint&lt;/strong&gt; trong URDF (không phải tên link), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_separation&lt;/code&gt; là khoảng cách hai bánh \(L\) ở bài 08-04, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt; là bán kính bánh xe. Nếu bản Harmonic bạn cài sử dụng tên thẻ khác đôi chút, hãy giữ nguyên ý nghĩa tương ứng và tránh sao chép thêm các khối cấu hình odometry dài dòng chưa cần thiết.&lt;/p&gt;

&lt;p&gt;Plugin tính toán vận tốc hai bánh theo cùng mô hình động học nghịch của cầu nối firmware:&lt;/p&gt;

\[v_R = v + \omega \frac{L}{2}, \qquad v_L = v - \omega \frac{L}{2}\]

&lt;p&gt;với \(L = 0.16\). Quãng đường đi được sau mỗi vòng quay bánh là:&lt;/p&gt;

\[s = 2 \pi r\]

&lt;p&gt;với \(r = 0.033\), tương đương khoảng 0,207 m mỗi vòng. Nếu khai báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt; là 0,05 trong plugin trong khi hình trụ URDF có bán kính 0,033, quãng đường di chuyển thực tế trong mô phỏng sẽ sai lệch hoàn toàn so với tính toán từ URDF.&lt;/p&gt;

&lt;p&gt;Tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; là cam kết đồng bộ với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. Sau khi cầu nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; đã hoạt động, hãy bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-p use_sim_time:=true&lt;/code&gt; để mọi cảm biến mô phỏng dùng chung một trục thời gian. Nếu một node dùng đồng hồ thực trong khi Gazebo dùng thời gian mô phỏng, bạn sẽ gặp lỗi ngoại suy TF hoặc thấy xe chạy trong Gazebo nhưng đứng yên trên RViz. Ngược lại, nếu bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; trước khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; tồn tại, mọi timer sẽ bị đứng chờ. Do đó luôn bắc cầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, kiểm tra bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; rồi mới dùng tham số này.&lt;/p&gt;

&lt;p&gt;Cần phân biệt rõ: RViz chỉ là công cụ hiển thị, không phải bộ mô phỏng vật lý — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RobotModel&lt;/code&gt; vẫn vẽ URDF bình thường ngay cả khi quá trình spawn vào Gazebo thất bại. Tiêu chí kiểm chứng của bài là robot thực sự lăn bánh trong cửa sổ Gazebo (hoặc nếu chạy headless, kiểm tra topic odometry từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; thấy vị trí thay đổi khi gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Cuối cùng, va chạm và quán tính là hai yếu tố giữ robot đứng vững trên mặt sàn. Một link chỉ có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visual&lt;/code&gt; sẽ rơi xuyên qua sàn; một link có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collision&lt;/code&gt; nhưng thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt; sẽ bị bộ mô phỏng từ chối hoặc gây mất ổn định số học. Do đó cả thân xe và hai bánh đều bắt buộc có đủ hai thẻ này.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Giả sử tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; ở bài 09-06 đặt tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws/src/capstone_description/urdf/capstone.urdf.xacro&lt;/code&gt;, đã bổ sung khối plugin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DiffDrive&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track = 0.16&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_r = 0.033&lt;/code&gt;. Khai triển ra URDF:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
xacro ~/ros2_ws/src/capstone_description/urdf/capstone.urdf.xacro &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; /tmp/capstone.urdf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Terminal A — chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; với đồng hồ mô phỏng:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 run robot_state_publisher robot_state_publisher &lt;span class=&quot;nt&quot;&gt;--ros-args&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; use_sim_time:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; robot_description:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /tmp/capstone.urdf&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Terminal B — khởi chạy world trống:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 launch ros_gz_sim gz_sim.launch.py gz_args:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;empty.sdf -r&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Terminal C — sau khi cửa sổ hoặc log server đã sẵn sàng, spawn mô hình:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 run ros_gz_sim create &lt;span class=&quot;nt&quot;&gt;-world&lt;/span&gt; empty &lt;span class=&quot;nt&quot;&gt;-file&lt;/span&gt; /tmp/capstone.urdf &lt;span class=&quot;nt&quot;&gt;-name&lt;/span&gt; capstone
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bạn sẽ thấy khối hộp thân xe cùng hai bánh trụ xuất hiện và đứng vững trên mặt phẳng mà không bị lún. Tiếp theo, bật cầu nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge&lt;/code&gt; (đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; trước để dễ nhớ ký hiệu một chiều &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 run ros_gz_bridge parameter_bridge &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  /cmd_vel@geometry_msgs/msg/Twist@gz.msgs.Twist
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở một terminal khác kiểm tra đồng hồ:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /clock &lt;span class=&quot;nt&quot;&gt;--once&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trường giây (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sec&lt;/code&gt;) phải lớn hơn 0 nhờ cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;; nếu bằng 0 nghĩa là world vẫn đang tạm dừng.&lt;/p&gt;

&lt;p&gt;Gửi một đợt lệnh chạy thẳng tốc độ chậm:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub &lt;span class=&quot;nt&quot;&gt;--times&lt;/span&gt; 20 /cmd_vel geometry_msgs/msg/Twist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;{linear: {x: 0.2, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hai mươi message chỉ đủ cho xe lăn một đoạn ngắn. Trong cửa sổ Gazebo, khung xe sẽ tiến dọc theo \(+x\) với cả hai bánh quay cùng chiều, và khi publisher kết thúc, xe không còn nhận lực dẫn động nữa. Trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; giữ bằng 0 vì xe vi sai không trượt ngang; với \(L/2 = 0.08\), \(v = 0.2\) và \(\omega = 0\), vận tốc dài vành hai bánh đều bằng 0,2 m/s.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-nhanh-lực-kéo-tối-đa-và-kiểm-tra-ma-sát-bánh-caster&quot;&gt;Giải thích: Tính nhanh lực kéo tối đa và kiểm tra ma sát bánh caster&lt;/h3&gt;

&lt;p&gt;Giả sử xe Capstone trong Gazebo nặng tổng cộng \(m = 2{,}0\text{ kg}\) (trọng lực \(P = m g \approx 19{,}6\text{ N}\)), phân bố 70% tải lên 2 bánh chủ động (\(N_{\text{drive}} = 13{,}7\text{ N}\)) và 30% lên bánh đa hướng phía sau (\(N_{\text{caster}} = 5{,}9\text{ N}\)):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Nếu đặt hệ số ma sát bánh chủ động \(\mu_{\text{drive}} = 1{,}0\) và bánh caster \(\mu_{\text{caster}} = 0{,}01\):
    &lt;ul&gt;
      &lt;li&gt;Lực bám tối đa của 2 bánh chủ động: \(F_{\text{bám}} = 1{,}0 \times 13{,}7 = \mathbf{13{,}7\text{ N}}\).&lt;/li&gt;
      &lt;li&gt;Lực cản ngang của bánh caster khi xoay: \(F_{\text{cản}} = 0{,}01 \times 5{,}9 = \mathbf{0{,}06\text{ N}}\) (không đáng kể, xe xoay mượt!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Ngược lại, nếu quên chỉnh để mặc định \(\mu_{\text{caster}} = 1{,}0\), bánh caster (mô phỏng bằng hình cầu không xoay trục) sẽ tạo lực cản \(5{,}9\text{ N}\) khiến xe &lt;strong&gt;rung giật và quay lệch tâm&lt;/strong&gt; ngay cả trên mặt sàn phẳng!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Thêm khối plugin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DiffDrive&lt;/code&gt; vào tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt;, gán &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_separation&lt;/code&gt; bằng thuộc tính khẩu độ và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt; bằng thuộc tính bán kính bánh. Sau mỗi lần chỉnh sửa, chạy lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; để cập nhật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp/capstone.urdf&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Khởi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; và tệp URDF đã khai triển.&lt;/li&gt;
  &lt;li&gt;Mở world bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf -r&lt;/code&gt; và xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Spawn mô hình với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-world empty&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-file /tmp/capstone.urdf&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-name capstone&lt;/code&gt;. Quan sát trực tiếp trong Gazebo (không chỉ nhìn RViz) để chắc chắn xe nằm vững trên sàn.&lt;/li&gt;
  &lt;li&gt;Chạy một tiến trình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt; cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, rồi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo /clock --once&lt;/code&gt; xác nhận thời gian đang chạy.&lt;/li&gt;
  &lt;li&gt;Xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x = 0.2&lt;/code&gt; trong một cụm ngắn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--times 20&lt;/code&gt; hoặc vài giây) và quan sát xe di chuyển trong Gazebo.&lt;/li&gt;
  &lt;li&gt;Dừng publisher và xác nhận xe ngừng được dẫn động. Sau đó nhấn Ctrl-C tắt cầu nối rồi thử xuất bản lại: xe phải đứng yên hoàn toàn, chứng minh luồng điều khiển đi qua cầu nối.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Mô hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone&lt;/code&gt; hiển thị trên mặt sàn trong world Harmonic; khi gửi lệnh, cả hai bánh lăn đều và thân xe tiến về trước; khi dừng lệnh, xe không còn lực đẩy và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; trả về thời gian lớn hơn 0. Nếu chỉ thấy mô hình hiển thị trong RViz còn trong Gazebo xe đứng yên hoặc không tồn tại thì bài lab chưa đạt.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;capstone visible in the empty world
/clock sec &amp;gt; 0
wheels rolling in Gazebo while linear.x is 0.2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Lỗi spawn hoặc không thấy mô hình&lt;/td&gt;
      &lt;td&gt;Sai tên world (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shapes&lt;/code&gt; thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt;), hoặc truyền tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.xacro&lt;/code&gt; thô cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;create&lt;/code&gt; thay vì URDF đã khai triển&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mô hình rơi xuyên qua mặt sàn&lt;/td&gt;
      &lt;td&gt;Link chỉ có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visual&lt;/code&gt; mà thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collision&lt;/code&gt;, không có bề mặt tiếp xúc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mô hình văng ra hoặc bị từ chối nạp&lt;/td&gt;
      &lt;td&gt;Có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collision&lt;/code&gt; nhưng thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt;, hoặc ma trận quán tính bằng 0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh quay ngang như đồng xu hoặc xe trượt không lăn&lt;/td&gt;
      &lt;td&gt;Khai báo sai trục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;axis&amp;gt;&lt;/code&gt; của khớp; với khớp đã xoay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rpy=&quot;-1.5708 0 0&quot;&lt;/code&gt; ở bài 09-06, trục đúng là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;axis xyz=&quot;0 0 1&quot;/&amp;gt;&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; xuất hiện trên ROS nhưng xe trong Gazebo không chạy&lt;/td&gt;
      &lt;td&gt;Cầu nối chưa chạy, hoặc tên khớp trong plugin không khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left_wheel_joint&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right_wheel_joint&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dữ liệu TF cũ hoặc node &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; bị đứng&lt;/td&gt;
      &lt;td&gt;Chưa bắc cầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, world bị tạm dừng vì thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;, hoặc chỉ một phần hệ thống bật thời gian mô phỏng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài này hoàn toàn là mô phỏng phần mềm trên laptop Ubuntu 24.04 từ bài 10-01. Không cần mua thêm mạch, lidar hay kết nối robot thật (hãy tắt nguồn Capstone và không cắm cáp serial).&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [How do we describe a robot? With URDF!&lt;/td&gt;
          &lt;td&gt;Getting Ready to Build Robots with ROS #7](https://www.youtube.com/watch?v=CwdbsvcpOHM) — Articulated Robotics (tiếng Anh). Giải thích cấu trúc khớp trong URDF cần khớp chính xác trước khi đưa vào Gazebo.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=campHHAPtyg&quot;&gt;ROS2 Jazzy Tutorial: Develop and Control Gazebo Model of Mobile Robot in ROS2 and Python&lt;/a&gt; — Aleksandar Haber PhD (tiếng Anh). Hướng dẫn đưa mô hình vào world trên Jazzy và gửi lệnh điều khiển; đối chiếu tên topic với lệnh launch trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=XH6Fvv7p5n0&quot;&gt;ROS2 - Create a Xacro Macro&lt;/a&gt; — Robotics Back-End (tiếng Anh). Cách dùng macro &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; cho từng bánh xe để tránh lặp mã khi mô hình phức tạp hơn.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; khai báo khoảng cách hai bánh là 0,16 m nhưng plugin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DiffDrive&lt;/code&gt; lại đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_separation&lt;/code&gt; là 0,20 m, mô phỏng sẽ dùng thông số nào và cần sửa ở đâu? &lt;em&gt;Gợi ý:&lt;/em&gt; động học mô phỏng chạy theo thông số trong plugin; cần sửa lại về 0,16 m rồi spawn lại chứ không điều chỉnh trên RViz.&lt;/li&gt;
  &lt;li&gt;Khi gửi lệnh \(v = 0\) và \(\omega = +0.5\) rad/s, bánh nào nhận vận tốc lớn hơn theo quy ước dấu của khóa học? &lt;em&gt;Gợi ý:&lt;/em&gt; bánh phải, vì với trục \(+x\) hướng trước và \(+y\) sang trái, góc yaw dương tương ứng quay sang trái.&lt;/li&gt;
  &lt;li&gt;World đang chạy là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; nhưng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;create&lt;/code&gt; lại truyền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-world shapes&lt;/code&gt;. Lỗi gì xảy ra và cần sửa cờ nào? &lt;em&gt;Gợi ý:&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;create&lt;/code&gt; tìm world theo đúng tên đang hoạt động, nên phải đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-world&lt;/code&gt; thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Tạo một bản sao URDF, xóa thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt; của thân xe rồi thử spawn để đọc thông báo lỗi của bộ mô phỏng, sau đó khôi phục lại thẻ này. &lt;em&gt;Gợi ý:&lt;/em&gt; Harmonic không thể tính tích phân động lực học cho vật có va chạm nhưng không có khối lượng; thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visual&lt;/code&gt; không thể thay thế.&lt;/li&gt;
  &lt;li&gt;Vì sao &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; dùng ký hiệu cầu nối một chiều &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; thay vì hai chiều &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt;? &lt;em&gt;Gợi ý:&lt;/em&gt; theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge -h&lt;/code&gt;, ký hiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; chỉ cho phép dữ liệu đi từ Gazebo sang ROS, ngăn chặn việc một node ROS khác vô tình xuất bản thêm mốc thời gian gây xung đột.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/gazebosim/ros_gz/blob/ros2/ros_gz_bridge/README.md&quot;&gt;README ros_gz_bridge&lt;/a&gt; — tài liệu chuẩn về cú pháp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; khi bắc cầu topic.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/getstarted/&quot;&gt;Bắt đầu với Gazebo Harmonic&lt;/a&gt; — tổng quan về tiến trình world nơi mô hình được spawn.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/ros_installation/&quot;&gt;Gazebo với ROS&lt;/a&gt; — hướng dẫn launch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_sim&lt;/code&gt; và tích hợp với Jazzy.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; — chuẩn quy ước hệ trục tọa độ và chiều quay yaw dương.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; — chuẩn hệ thống frame mà &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; xuất bản từ URDF.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Giới thiệu Gazebo Harmonic</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_01_gazebo_harmonic/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter10/10_01_gazebo_harmonic</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~70 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Nếu mỗi lần sửa một dòng thuật toán tránh vật cản bạn lại phải nạp firmware, sạc pin, đặt robot xuống sàn và chạy theo đỡ khi xe lao vào chân bàn, tốc độ nghiên cứu sẽ chậm đi hàng chục lần. &lt;strong&gt;Gazebo Harmonic&lt;/strong&gt; là phòng thí nghiệm vật lý số giúp bạn kiểm thử hàng trăm kịch bản va chạm, trơn trượt và cảm biến giả lập ngay trên máy tính trước khi chạm vào phần cứng thật.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-mô-phỏng-vật-lý-gazebo-harmonic&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Mô phỏng Vật lý Gazebo Harmonic&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Động cơ vật lý (Physics Engine — DART) &amp;amp; Bước thời gian mô phỏng&lt;/strong&gt;:
Tính toán trọng lực, ma sát lốp–sàn, mô-men động cơ và va chạm ở từng bước rời rạc (thường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_step_size = 0.001 s&lt;/code&gt;, tức 1000 Hz) rồi xuất thời gian mô phỏng lên topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cầu nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt; &amp;amp; Tách biệt đồng hồ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt;)&lt;/strong&gt;:
Gazebo Harmonic dùng hệ thống truyền tin riêng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz-transport&lt;/code&gt;). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt; ánh xạ hai chiều giữa topic Gazebo và ROS 2 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;). Mọi node ROS 2 khi chạy mô phỏng &lt;strong&gt;bắt buộc&lt;/strong&gt; đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; để không bị lệch đồng hồ tường!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hệ số Thời gian Thực (RTF — Real-Time Factor)&lt;/strong&gt;:
\(\text{RTF} = \frac{\text{Thời gian mô phỏng trôi qua}}{\text{Thời gian thực ngoài đời}}\). Nếu \(\text{RTF} = 1{,}0\), 1 giây trong Gazebo đúng bằng 1 giây thật; nếu \(\text{RTF} &amp;lt; 0{,}5\) do máy yếu hoặc mesh quá nặng, thuật toán điều khiển thời gian thực sẽ bị méo nhịp.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ mở được một môi trường mô phỏng (world) của Gazebo Harmonic từ terminal ROS 2 Jazzy và phân biệt rõ lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt; với tệp thực thi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; của dòng Classic cũ. Bạn sẽ giải thích được vì sao khóa học kết hợp Jazzy với Harmonic, và vì sao việc nạp Humble hoặc cài gói Classic sẽ dẫn sang một bộ mô phỏng khác. Bạn dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; để xác nhận world đang chạy đã có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, hiểu vai trò của cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; gắn với nút chạy (play) để không nhầm trạng thái tạm dừng là lỗi cài đặt, đồng thời đọc hiểu ký hiệu chiều truyền dữ liệu của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt; (chưa chạy cầu nối trong bài này). Cuối cùng, bạn lưu lại ảnh cửa sổ giao diện hoặc log chạy server không màn hình làm minh chứng world trống hoạt động bình thường.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành bài 09-01: máy chạy Ubuntu 24.04 đã cài ROS 2 Jazzy từ gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.deb&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;. Bạn cần thao tác quen với hai cửa sổ terminal song song và biết dùng Ctrl-C để dừng tiến trình đang chạy. Bài này chưa cần robot thật, chưa cần URDF hay firmware micro-ROS. Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; đang trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt;, hãy khắc phục dứt điểm trước khi cài bất kỳ gói nào bên dưới.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Capstone A đã vận hành bằng khung truyền chương 08, còn chương 09 đã chuẩn bị sẵn topic, TF và tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; trên laptop. Bài này mở ra môi trường mô phỏng mà Nav2 sẽ dùng để lập kế hoạch đường đi và các thuật toán học máy về sau có thể ghi dữ liệu bag để đối chiếu với thực tế. Thành phần kết nối giữa ROS và Gazebo là họ gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz&lt;/code&gt;, không phải gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo_ros&lt;/code&gt; của bản Classic. Việc làm quen đúng tên tiến trình khi world còn trống giúp bạn tách bạch lỗi: nếu bài sau gặp trục trặc khi đưa robot vào mô phỏng (spawn), bạn sẽ biết chắc đó là lỗi mô hình chứ không phải cài nhầm phiên bản Gazebo.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch10_gazebo_stack.png&quot; alt=&quot;Gazebo Harmonic cạnh ROS 2 Jazzy, ros_gz ở giữa&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Gazebo Classic và Gazebo Sim là hai hệ thống mã nguồn khác nhau dù mang tên gần giống nhau. Classic dùng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; thường xuất hiện trong các hướng dẫn ROS 1 cũ. Gazebo Sim là dòng phát triển hiện hành: dùng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;, định dạng world là SDF và liệt kê topic bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic&lt;/code&gt;. Harmonic là phiên bản Gazebo Sim được biên dịch đồng bộ với ROS 2 Jazzy (trong khi Humble đi cùng Fortress). Các hướng dẫn yêu cầu chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt install gazebo&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-humble-gazebo-ros-pkgs&lt;/code&gt; không phù hợp cho chương này.&lt;/p&gt;

&lt;p&gt;Tên gói tổng hợp (metapackage) trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; cho Jazzy là:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-ros-gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Gói này cài đặt sẵn các thành phần khóa học sử dụng, gồm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_sim&lt;/code&gt; (để khởi chạy và spawn mô hình) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt; (để chuyển tiếp topic giữa hai đồ thị ROS và Gazebo). Cài đặt xong không tự động chạy world mà chỉ đưa các tệp thực thi vào đường dẫn ROS sau khi bạn nạp Jazzy.&lt;/p&gt;

&lt;p&gt;Trong bài này ta dùng hai world có sẵn: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shapes.sdf&lt;/code&gt; chứa vài khối hình học cơ bản để kiểm tra khả năng dựng hình đồ họa, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; là world trống mà các bài sau sẽ đưa robot Capstone vào. Câu lệnh khởi chạy theo tài liệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 launch ros_gz_sim gz_sim.launch.py gz_args:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;empty.sdf -r&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Chuỗi trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz_args&lt;/code&gt; được truyền trực tiếp cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;. Cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; yêu cầu mô phỏng bắt đầu chạy ngay; nếu thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;, server chỉ nạp world rồi dừng ở trạng thái tạm dừng (pause) khiến đồng hồ mô phỏng đứng yên tại 0 — điều này sẽ làm mọi node bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; về sau bị đứng vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; không tăng.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; ban đầu là topic nội bộ của Gazebo chứ chưa xuất hiện trên ROS. Khi server đang chạy, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; sẽ liệt kê &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; ngay cả khi chưa bật cầu nối. Ký hiệu chiều truyền tin trong cầu nối rất dễ nhầm, nên ở bài này ta chỉ đọc phần trợ giúp mà chưa chạy cầu:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ký hiệu nằm giữa kiểu dữ liệu ROS và kiểu dữ liệu Gazebo quy định chiều truyền. Hai dạng khóa học sẽ dùng (theo README của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt;) là cầu nối hai chiều cho lệnh vận tốc và cầu nối một chiều cho đồng hồ. Dạng hai chiều:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge /cmd_vel@geometry_msgs/msg/Twist@gz.msgs.Twist
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Dạng một chiều cho đồng hồ (chỉ từ Gazebo sang ROS):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tuyệt đối không đảo ngược chiều đồng hồ, vì nếu có thêm một publisher thứ hai phát lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; sẽ gây xung đột với bộ mô phỏng. Chúng ta sẽ thực sự chạy hai lệnh này ở bài 10-02.&lt;/p&gt;

&lt;p&gt;Thời gian mô phỏng và thời gian thực (wall time) không phải lúc nào cũng trùng nhau. Tỉ số giữa chúng gọi là hệ số thời gian thực (Real-Time Factor, RTF):&lt;/p&gt;

\[\mathrm{RTF} = \frac{\Delta t_{\mathrm{sim}}}{\Delta t_{\mathrm{wall}}}\]

&lt;p&gt;Khi giao diện báo hệ số gần bằng 1, một giây mô phỏng tương ứng xấp xỉ một giây đồng hồ thực. Nếu hệ số tụt xuống thấp, lệnh vận tốc 0,2 m/s vẫn tạo ra quãng đường 0,2 m trên mỗi giây mô phỏng, nhưng thời gian thực bên ngoài sẽ trôi nhanh hơn. Trên một chiếc laptop bình thường, world trống cần đạt RTF gần bằng 1.&lt;/p&gt;

&lt;p&gt;Chế độ không giao diện (headless) chạy cùng một server mô phỏng nhưng không mở cửa sổ GUI. Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv DISPLAY&lt;/code&gt; trả về rỗng, bạn không thể mở giao diện đồ họa mà cần thêm cờ chỉ chạy server (thường là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-s&lt;/code&gt;, kiểm tra trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim --help&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 launch ros_gz_sim gz_sim.launch.py gz_args:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;empty.sdf -r -s&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bài lab vẫn đạt yêu cầu trên chế độ này nếu tiến trình không tự thoát và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; liệt kê được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. Hãy thực hành bài này trên laptop; Raspberry Pi 5 chỉ phù hợp chạy các tác vụ nhẹ hơn về sau, không dùng để thử giao diện Gazebo.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Thực hiện lần lượt ba lệnh kiểm tra để xác định đúng phiên bản Gazebo sắp chạy:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;printenv &lt;/span&gt;ROS_DISTRO
gz sim &lt;span class=&quot;nt&quot;&gt;--help&lt;/span&gt;
apt-cache policy ros-jazzy-ros-gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Dòng đầu phải trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt; và nội dung trợ giúp phải thuộc về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;. Nếu shell báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz: command not found&lt;/code&gt;, hãy nạp lại Jazzy rồi kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt-cache policy&lt;/code&gt;. Nếu không thấy gói ứng viên nào, nghĩa là máy chưa thêm nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; của Jazzy hoặc không chạy Ubuntu 24.04 — không được “chữa cháy” bằng cách cài gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; vì bản Classic không tương thích với tệp launch của chương này.&lt;/p&gt;

&lt;p&gt;Khi gói đã sẵn sàng, khởi chạy world trống ở terminal thứ nhất và giữ tiến trình hoạt động:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 launch ros_gz_sim gz_sim.launch.py gz_args:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;empty.sdf -r&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở terminal thứ hai, nạp Jazzy rồi liệt kê các topic bên phía Gazebo:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
gz topic &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Kiểm tra sự xuất hiện của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. Lúc này &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; vẫn gần như trống vì chưa có cầu nối giữa hai đồ thị; Gazebo đang xuất bản trên hệ thống nội bộ của riêng nó. Nếu bạn nhấn Ctrl-C ở terminal đầu tiên, server tắt và danh sách topic Gazebo cũng biến mất.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-ảnh-hưởng-của-rtf--04-và-lỗi-quên-use_sim_time&quot;&gt;Giải thích: Tính tay ảnh hưởng của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RTF = 0,4&lt;/code&gt; và lỗi quên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Giả sử bạn chạy mô phỏng một thế giới phức tạp trên laptop không có GPU rời nên &lt;strong&gt;RTF tụt xuống &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,4&lt;/code&gt;&lt;/strong&gt; (mất 2,5 giây ngoài đời mới mô phỏng xong 1 giây trong Gazebo):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Nếu bạn ra lệnh cho xe chạy với \(v = 0{,}5\text{ m/s}\) trong đúng &lt;strong&gt;4 giây thời gian mô phỏng&lt;/strong&gt;, xe đi đúng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2,0 m&lt;/code&gt; trong thế giới ảo (nhưng ngoài đời bạn phải ngồi nhìn màn hình mất \(4 / 0{,}4 = \mathbf{10\text{ giây}}\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thảm họa nếu quên bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; trên một node điều khiển:&lt;/strong&gt; Gazebo gán dấu thời gian &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t = 12,0 s&lt;/code&gt;), trong khi node quên cờ lại dùng đồng hồ hệ thống Unix (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t = 1.732.000.000 s&lt;/code&gt;) → TF2 lập tức báo lỗi &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Extrapolation Error&lt;/code&gt;&lt;/strong&gt; và loại bỏ toàn bộ dữ liệu cảm biến!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Kiểm tra phiên bản ROS bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt;. Nếu chưa phải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;, nạp lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy/setup.bash&lt;/code&gt; rồi kiểm tra lại (nếu máy cài cả Humble và Jazzy, lần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt; sau cùng sẽ có hiệu lực).&lt;/li&gt;
  &lt;li&gt;Cài gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt install ros-jazzy-ros-gz&lt;/code&gt;. Tuyệt đối không cài thêm gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-humble-gazebo-ros-pkgs&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Ở terminal A, khởi chạy world trống với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz_args:=&quot;empty.sdf -r&quot;&lt;/code&gt; và để tiến trình chạy ở chế độ foreground.&lt;/li&gt;
  &lt;li&gt;Nếu không thấy cửa sổ hiện ra, kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv DISPLAY&lt;/code&gt;. Nếu biến này trống, dừng lệnh trước và khởi chạy lại với cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-s&lt;/code&gt; như hướng dẫn chế độ headless ở trên.&lt;/li&gt;
  &lt;li&gt;Ở terminal B, nạp Jazzy và chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; để xác nhận có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. Chưa chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge&lt;/code&gt; ở bài này.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run ros_gz_bridge parameter_bridge -h&lt;/code&gt;, ghi lại ký hiệu cho chiều hai hướng và chiều từ Gazebo sang ROS, rồi thoát trợ giúp. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; lúc này chưa cần hiển thị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;(Tùy chọn, ~5 phút)&lt;/em&gt; Chạy thử &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shapes.sdf&lt;/code&gt; thay cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; để quan sát các khối hình học, sau đó quay lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf -r&lt;/code&gt; (bài 10-02 sẽ spawn mô hình vào world tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cửa sổ Gazebo Sim mở ra ở trạng thái đang chạy nhờ cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; (hoặc tiến trình server chạy ổn định khi dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-s&lt;/code&gt;). Terminal thứ hai in danh sách topic chứa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, và không có tiến trình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge&lt;/code&gt; nào đang chạy:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/clock
/stats
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Danh sách thực tế sẽ dài hơn, nhưng bắt buộc phải có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. Khi chưa bật cầu nối, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; chưa cần hiển thị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Unable to locate package ros-jazzy-ros-gz&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Không phải Ubuntu 24.04, hoặc chưa thêm nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; của ROS 2 Jazzy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; mở cửa sổ khác, không có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Đang cài bản Gazebo Classic; chương này sử dụng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lệnh launch thoát ngay hoặc không mở được GUI&lt;/td&gt;
      &lt;td&gt;Biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DISPLAY&lt;/code&gt; trống; chuyển sang cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-s&lt;/code&gt; hoặc chạy trên máy có màn hình&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; in ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Shell đã nạp nhầm Humble sau cùng nên không dùng được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz&lt;/code&gt; của Jazzy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; không kết nối được&lt;/td&gt;
      &lt;td&gt;Terminal khởi chạy world đã bị đóng khiến server ngừng hoạt động&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nút play ở trạng thái tạm dừng, thời gian đứng tại 0&lt;/td&gt;
      &lt;td&gt;Quên cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;; bấm play để kiểm tra thời gian chạy, sau đó launch lại có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài lab này thực hiện hoàn toàn bằng phần mềm trên laptop đã cài Jazzy, không cần cảm biến hay ESP32. Raspberry Pi 5 chỉ là tùy chọn cho máy tính đặt trên robot về sau, không dùng để chạy GUI Gazebo. Giá tham khảo năm 2026 trên Hshop cho bản RAM nhỏ khởi điểm khoảng 2,4 triệu đồng (cần kiểm tra lại giá hiện tại trước khi mua): &lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Raspberry Pi 5 made in UK&lt;/a&gt; và &lt;a href=&quot;https://hshop.vn/combo-raspberry-pi-5-ram-4-8gb-basic-kit&quot;&gt;basic kit&lt;/a&gt;. Nếu đường dẫn thay đổi, tìm trực tiếp “Raspberry Pi 5” trên &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt;. Trên Shopee hoặc Lazada, tìm với từ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Raspberry Pi 5 4GB&lt;/code&gt; và lưu ý các gói bán rẻ hơn đáng kể thường không kèm bộ nguồn chuẩn.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=dY8JxldcuqA&quot;&gt;Correctly Install Gazebo Harmonic Inside of ROS2 Jazzy Installation -Fix Gazebo Installation Issues&lt;/a&gt; — Aleksandar Haber PhD (tiếng Anh). Hướng dẫn cài Gazebo Harmonic cùng ROS 2 Jazzy và tránh cài nhầm các gói Gazebo Classic.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=campHHAPtyg&quot;&gt;ROS2 Jazzy Tutorial: Develop and Control Gazebo Model of Mobile Robot in ROS2 and Python&lt;/a&gt; — Aleksandar Haber PhD (tiếng Anh). Ví dụ đưa mô hình xe vào Gazebo và điều khiển từ ROS 2 (video khá dài, nên tập trung vào đoạn spawn và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/2078352426052273&quot;&gt;Lấy dữ liệu 3D từ một vị trí trên trái đất&lt;/a&gt; — Facebook Reel ngắn của Hoàng Quân (Cơ điện tử ICTU, &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;) về cách lấy dữ liệu địa hình 3D từ một vị trí thực tế. Trong Gazebo, mặt đất là một mô hình có đơn vị và gốc tọa độ riêng, nên dữ liệu 3D chỉ đưa vào world chính xác sau khi đồng bộ hai yếu tố này.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim --help&lt;/code&gt;, rồi giải thích trong hai câu vì sao việc máy không có tệp thực thi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; là hoàn toàn bình thường. &lt;em&gt;Gợi ý:&lt;/em&gt; bộ mô phỏng tương thích với Jazzy là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt; thuộc dòng Harmonic, còn tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; thuộc bản Classic cũ.&lt;/li&gt;
  &lt;li&gt;Khởi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; một lần không có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; và một lần có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;. Ghi lại trạng thái nút play và đồng hồ mô phỏng trong mỗi trường hợp. &lt;em&gt;Gợi ý:&lt;/em&gt; khi thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;, server nạp world ở trạng thái tạm dừng nên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; không tăng cho tới khi bạn bấm play.&lt;/li&gt;
  &lt;li&gt;Trong một phiên chạy, thời gian mô phỏng tăng 2,0 s trong khi đồng hồ thực trôi qua 4,0 s. Tính hệ số thời gian thực (RTF) và nhận xét xem kết quả này có bình thường với world trống trên laptop không. &lt;em&gt;Gợi ý:&lt;/em&gt; RTF bằng thời gian mô phỏng chia thời gian thực, tức là 0,5; với world trống trên laptop, giá trị hợp lý cần gần 1 hơn.&lt;/li&gt;
  &lt;li&gt;Nêu tên gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; cần cài trong bài này và hai tên gói dễ cài nhầm kéo theo sai bộ mô phỏng. &lt;em&gt;Gợi ý:&lt;/em&gt; cài &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-ros-gz&lt;/code&gt;; tránh cài &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; hoặc gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-humble-gazebo-ros&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Ghi lại chính xác câu lệnh launch sẽ dùng lại ở bài sau (gồm tên tệp world và cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;). &lt;em&gt;Gợi ý:&lt;/em&gt; tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz_args&lt;/code&gt; phải chứa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf -r&lt;/code&gt;, vì công cụ spawn sẽ tìm đúng world có tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/getstarted/&quot;&gt;Bắt đầu với Gazebo Harmonic&lt;/a&gt; — giới thiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;, world hình khối và cơ chế điều khiển chạy/tạm dừng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/ros_installation/&quot;&gt;Gazebo Harmonic với ROS&lt;/a&gt; — bảng ghép đôi chính thức giữa Jazzy, Harmonic và họ gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation/Ubuntu-Install-Debs.html&quot;&gt;Cài deb Jazzy trên Ubuntu&lt;/a&gt; — kiểm tra lại nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; nếu không tìm thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-ros-gz&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/gazebosim/ros_gz/blob/ros2/ros_gz_bridge/README.md&quot;&gt;README ros_gz_bridge&lt;/a&gt; — giải thích chi tiết các ký hiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; trước khi áp dụng ở bài 10-02.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 09</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_07_ch09_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_07_ch09_lab_checklist</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~70 phút&lt;/strong&gt;. Mục kiểm tra cuối chương này yêu cầu bằng chứng thực tế rằng ROS 2 Jazzy đang chạy ổn định trên Ubuntu 24.04, đồ thị ROS có topic hoạt động và cây TF có ít nhất một cạnh tĩnh mà bạn giải thích được.&lt;/p&gt;

&lt;p&gt;Kết thúc Chương 09, bạn đã chuyển từ lập trình đơn lẻ trên MCU sang vận hành trọn vẹn một ngăn xếp phần mềm &lt;strong&gt;ROS 2 Jazzy&lt;/strong&gt; trên Linux: có workspace sạch, có launch file tham số hóa, có cây hệ tọa độ TF2 và mô hình URDF hiển thị trực tiếp trên RViz2. Danh sách nghiệm thu này đảm bảo mọi mảnh ghép đã khớp nhau chặt chẽ trước khi bước sang mô phỏng Gazebo và cầu nối phần cứng ở Chương 10.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-tiêu-chuẩn-nghiệm-thu-ngăn-xếp-ros-2-chương-09&quot;&gt;Kiến thức cần nhớ: Ba tiêu chuẩn nghiệm thu Ngăn xếp ROS 2 (Chương 09)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tính tự động &amp;amp; Tái lập bằng một lệnh Launch&lt;/strong&gt;:
Toàn bộ hệ thống (driver/mô phỏng, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, RViz2) khởi chạy sạch sẽ từ một tệp launch duy nhất kèm cấu hình YAML.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính nhất quán của Đồ thị Node–Topic–TF2&lt;/strong&gt;:
Không có khung TF bị đứt gãy (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;view_frames&lt;/code&gt; tạo đúng một cây liên thông duy nhất), không có cảnh báo xung đột QoS và mọi tên topic tuân thủ quy ước chuẩn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính hợp lệ của Mô hình URDF/Xacro&lt;/strong&gt;:
Kiểm tra qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check_urdf&lt;/code&gt;, bánh xe xoay đúng chiều khi đẩy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/joint_states&lt;/code&gt; và mọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;link&amp;gt;&lt;/code&gt; đều có thông số quán tính hợp lý cho bước mô phỏng kế tiếp.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Bạn cần hoàn thành và xác nhận đủ sáu tiêu chí: đúng phiên bản Ubuntu, workspace đã nạp đúng thứ tự, đọc được dữ liệu từ một topic, gọi được một service hoặc action, đồng bộ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; (nếu dùng SBC) và kiểm chứng cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; tới frame cảm biến bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt;. Bất kỳ mục nào thiếu log minh chứng đều chưa đạt.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành các bài từ 09-00 đến 09-06; máy tính (laptop hoặc SBC) đã kiểm tra được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt;. Sổ thực hành chương 08 cần đã hoàn tất, hoặc ghi rõ những hạng mục còn tồn đọng của chương 08 vì cây TF không thể bù đắp cho thông số chiều rộng trục bánh bị đo sai.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Gazebo Harmonic ở chương 10 sẽ khởi chạy trực tiếp trên đồ thị ROS hiện tại. Nếu thêm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; lên một môi trường ROS vốn đã lỗi, số lượng topic bất thường sẽ tăng lên và rất khó gỡ. Do đó, cần xác nhận đồ thị ROS vận hành chuẩn xác ngay từ khi chưa có mô phỏng.&lt;/p&gt;

&lt;h2 id=&quot;tiêu-chí-đạt-và-không-đạt&quot;&gt;Tiêu chí đạt và không đạt&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Phiên bản cài đặt.&lt;/strong&gt; Trên máy cài gói qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; phải trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsb_release -sc&lt;/code&gt; phải là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noble&lt;/code&gt;. Nếu mạch Jetson vẫn đang ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jammy&lt;/code&gt;, tiêu chí này chưa đạt cho đến khi bạn không cố cài Jazzy lên đó nữa. Lưu ảnh chụp màn hình terminal làm minh chứng.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Workspace.&lt;/strong&gt; Biên dịch thành công ít nhất một gói trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws&lt;/code&gt; bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt;, và khi mở terminal mới hệ thống phải tự động nạp underlay trước rồi đến overlay. Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg list&lt;/code&gt; không hiển thị gói của bạn ở terminal mới thì tiêu chí này chưa đạt (trừ khi bạn đã cấu hình đúng trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt; và dán dòng cấu hình đó vào sổ).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topic.&lt;/strong&gt; Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt; hoặc node tự viết, dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo&lt;/code&gt; lấy ít nhất một bản ghi kèm tên kiểu message. Danh sách trống khi bạn chưa chạy bất kỳ publisher nào là chưa thực hiện, không phải “lỗi DDS”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Service hoặc action.&lt;/strong&gt; Thực hiện thành công một lệnh gọi service/action, hoặc ghi lại rõ một lỗi đã gặp kèm cách khắc phục. Không có câu lệnh và log lưu trong sổ là chưa đạt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Máy tính nhúng (SBC).&lt;/strong&gt; Nếu có Raspberry Pi hoặc Jetson: cần có liên kết &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/esp32&lt;/code&gt; (hoặc ghi chú kế hoạch hoãn có ngày cụ thể), kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemctl status&lt;/code&gt; của unit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd&lt;/code&gt; và xác nhận hai máy dùng chung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt;. Nếu chưa dùng SBC, ghi rõ “chỉ dùng laptop” là đủ để đạt tiêu chí này.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cây tọa độ TF.&lt;/strong&gt; Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run tf2_ros tf2_echo base_link &amp;lt;frame&amp;gt;&lt;/code&gt; phải in ra phép tịnh tiến theo đơn vị mét. Nếu xuất hiện hai publisher trên cùng một cạnh thì chưa đạt. Nếu bạn chủ động xoay góc yaw $90^\circ$, giá trị quaternion in ra phải xấp xỉ $(0,\ 0,\ 0{,}707,\ 0{,}707)$.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-bộ-4-lệnh-kiểm-tra-nhanh-sức-khỏe-ros-2-trong-60-giây&quot;&gt;Giải thích: Bộ 4 lệnh kiểm tra nhanh “Sức khỏe ROS 2” trong 60 giây&lt;/h3&gt;

&lt;p&gt;Trước khi ký duyệt Chương 09, chạy đúng 4 lệnh sau và đối chiếu kỳ vọng:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Lệnh kiểm tra&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Kết quả đạt chuẩn&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Dấu hiệu lỗi cần sửa ngay&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run tf2_tools view_frames&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1 cây duy nhất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link → ...&lt;/code&gt; không đứt nhánh&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Xuất hiện 2 cây rời nhau hoặc thời gian trễ TF âm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic hz /odom&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tần số ổn định (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;20–50 Hz&lt;/code&gt;), độ lệch chuẩn nhỏ&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;no new messages&lt;/code&gt; hoặc rớt nhịp thất thường&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /scan --verbose&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;QoS Publisher và Subscriber tương thích&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt; phát vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt; nhận&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check_urdf &amp;lt;(xacro robot.urdf.xacro)&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Successfully Parsed XML&lt;/code&gt; và đúng số lượng link/joint&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Lỗi cú pháp XML hoặc thiếu thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Sao chép mẫu sau vào sổ thực hành, điền đầy đủ kết quả thực tế (xóa các dòng không có minh chứng):&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Ky duyet chuong 09
Ngay:
ROS_DISTRO=
Ubuntu codename=
pkg da build=
topic echo (lenh + 1 dong):
service/action:
SBC: co / khong. DOMAIN=
tf2_echo base_link -&amp;gt; :
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Hướng kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt; xuất hiện trong cùng shell&lt;/td&gt;
      &lt;td&gt;Xóa bớt một dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bashrc&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; không có dữ liệu&lt;/td&gt;
      &lt;td&gt;Publisher chưa chạy, khác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt;, hoặc không tương thích QoS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; chờ mãi không in&lt;/td&gt;
      &lt;td&gt;Nhập sai tên frame; kiểm tra lại bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run tf2_tools view_frames&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Raspberry Pi chạy Ubuntu 26.04&lt;/td&gt;
      &lt;td&gt;Không khớp kho gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; của Jazzy; nạp lại Ubuntu 24.04 hoặc dừng ở tiêu chí bản cài&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [The ROS Transform System (TF)&lt;/td&gt;
          &lt;td&gt;Getting Ready to Build Robots with ROS #6](https://www.youtube.com/watch?v=QyvHhY4Y_Y8) — Articulated Robotics (tiếng Anh). Danh sách kiểm tra của chương yêu cầu một cây tọa độ hiển thị rõ ràng; video này giúp hình dung trực quan trước khi hoàn thành bước xác nhận.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tiêu chí nào không đạt nếu các topic vẫn giao tiếp được với nhau nhưng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsb_release&lt;/code&gt; trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jammy&lt;/code&gt;?&lt;/li&gt;
  &lt;li&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; báo $x = 10$ trong khi cảm biến lidar chỉ cách tâm xe 10 cm. Bạn đang nhập sai đơn vị nào?&lt;/li&gt;
  &lt;li&gt;Vì sao ghi nhận “chỉ dùng laptop” vẫn được tính là đạt tiêu chí SBC?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Tiêu chí phiên bản cài đặt không đạt, vì các topic hoạt động không chứng minh bạn đang dùng đúng bản Ubuntu 24.04 cho Jazzy.&lt;/li&gt;
  &lt;li&gt;Sai đơn vị mét: hệ thống hiểu $10$ là 10 m thay vì $0{,}10$ m.&lt;/li&gt;
  &lt;li&gt;SBC chỉ cần khi đưa hệ thống lên xe thật, không phải điều kiện bắt buộc để học đồ thị ROS; việc ghi nhận minh bạch phạm vi thực hành được chấp nhận, còn bỏ trống không báo cáo thì không.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation/Ubuntu-Install-Debs.html&quot;&gt;Cài Jazzy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-TF2.html&quot;&gt;Về TF2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter09/09_00_linux_on_sbc/&quot;&gt;Bài Linux trên SBC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>URDF / xacro cơ bản</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_06_urdf_basics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_06_urdf_basics</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~90 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Làm thế nào để cả bộ hiển thị RViz2, bộ mô phỏng vật lý Gazebo lẫn thuật toán tránh va chạm MoveIt 2 đều “biết” robot của bạn dài rộng bao nhiêu, bánh xe gắn ở đâu và nặng mấy kilôgam mà không phải khai báo lại ba lần ở ba nơi khác nhau? Câu trả lời là &lt;strong&gt;URDF (Unified Robot Description Format)&lt;/strong&gt; kết hợp với macro &lt;strong&gt;Xacro&lt;/strong&gt; — bản vẽ kỹ thuật số duy nhất làm nguồn chân lý cho toàn bộ hệ sinh thái ROS 2.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-khối-cốt-lõi-của-mô-hình-urdf--xacro&quot;&gt;Kiến thức cần nhớ: Bốn khối cốt lõi của Mô hình URDF / Xacro&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Đồ thị Khâu (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;link&amp;gt;&lt;/code&gt;) và Khớp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;joint&amp;gt;&lt;/code&gt;)&lt;/strong&gt;:
Robot được mô tả như một cây nối các vật rắn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;link&lt;/code&gt;) bằng các khớp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joint&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fixed&lt;/code&gt; cho cảm biến cố định, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; cho bánh xe quay tự do, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt; cho khớp cánh tay có giới hạn góc).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ba lớp hình học trong mỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;link&amp;gt;&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visual&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collision&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inertial&lt;/code&gt;)&lt;/strong&gt;:
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;visual&amp;gt;&lt;/code&gt;: hình dáng đẹp để hiển thị trên RViz2.&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;collision&amp;gt;&lt;/code&gt;: khối hình học đơn giản (hộp, trụ) để tính va chạm nhanh.&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt;: khối lượng \(m\) và ten-xơ quán tính \(I\) quyết định vật lý trong Gazebo (thiếu phần này, robot sẽ bay lơ lửng hoặc nổ mô phỏng!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cầu nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;&lt;/strong&gt;:
Đọc tệp URDF và lắng nghe góc khớp từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/joint_states&lt;/code&gt; để tự động phát toàn bộ cây biến đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf_static&lt;/code&gt; cho toàn hệ thống.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Macro &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt;&lt;/strong&gt;:
Tránh sao chép lặp lại XML cho bánh trái/phải hay nhiều khớp bằng biến tham số và biểu thức toán học (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;${wheel_radius}&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ viết được tệp URDF mô tả khung xe hai bánh dẫn động kèm bánh đa hướng, phân tách rõ ba thành phần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visual&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collision&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inertial&lt;/code&gt;, rồi nạp qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; để tạo ra cây TF của bài 09-05 mà không cần khai báo thủ công từng static publisher. Bạn sẽ đưa các kích thước hình học (chiều rộng trục bánh, bán kính bánh) vào thuộc tính (property) của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; để khi chỉnh một tham số thì cả hai khớp bánh xe cùng cập nhật theo. Bạn cũng sẽ nắm rõ vì sao bánh dẫn động dùng khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt;, bánh đa hướng dùng khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fixed&lt;/code&gt;, và vì sao một mô hình chỉ có hình hiển thị (visual) mà thiếu khối va chạm (collision) sẽ xuyên qua vật cản trong Gazebo.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Nắm vững quy ước tên frame ở bài 09-05, thông số chiều rộng trục bánh \(L\) và bán kính bánh ở bài 08-04, đồng thời có bản ROS 2 Jazzy desktop để hiển thị mô hình trên RViz. Tệp XML phải đúng cú pháp tuyệt đối vì chỉ cần thiếu một thẻ đóng như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;/link&amp;gt;&lt;/code&gt; là toàn bộ mô hình không thể nạp. Bạn không cần máy quét 3D — các khối hình học cơ bản như hình hộp và hình trụ là cách khởi đầu chuẩn xác nhất.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Node &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; đọc tệp URDF và tự động xuất bản cây TF tĩnh mà toàn bộ hệ thống ROS dựa vào. Ở chương 10, Gazebo Harmonic sẽ nạp chính tệp mô tả này để bổ sung tính toán vật lý. Vùng bao va chạm (footprint) của Nav2 cũng xuất phát từ hình học va chạm bạn khai báo ở đây chứ không dựa vào ảnh chụp khung xe. Ngay cả các mô hình học máy ở chương 12 tuy không đọc trực tiếp URDF nhưng vẫn vận hành trên robot có cấu trúc khớp, giới hạn góc và hệ tọa độ gốc tương ứng. Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; trong URDF lệch so với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; của cầu nối điều khiển, mọi tọa độ cảm biến phía sau đều bị sai lệch cố định và rất dễ bị chẩn đoán nhầm thành lỗi SLAM. Sử dụng một tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; duy nhất với các thuộc tính tham số hóa giúp bạn tránh việc sao chép thủ công bánh trái sang bánh phải rồi quên đổi dấu tọa độ \(y\).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_urdf.png&quot; alt=&quot;Link và joint của khung vi sai nhỏ&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Link&lt;/strong&gt; đại diện cho một vật rắn trong cấu trúc robot, còn &lt;strong&gt;joint&lt;/strong&gt; (khớp) liên kết một link con với link cha và quy định bậc tự do chuyển động giữa chúng. Cây cấu trúc có duy nhất một link gốc — với khung xe này là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Ba kiểu khớp chính cần nắm:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fixed&lt;/code&gt;: khớp cố định không chuyển động, dùng cho cụm bánh đa hướng, giá gắn lidar hoặc mạch IMU.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt;: khớp quay liên tục không giới hạn góc, dùng cho hai bánh xe dẫn động. Không nên dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt; với giới hạn góc thật lớn vì bánh xe vẫn có thể bị chạm giới hạn trong mô phỏng.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt;: khớp quay bản lề có cận dưới và cận trên, phù hợp cho khớp cánh tay robot (sẽ gặp ở bài giới thiệu MoveIt trong chương 11), không dùng cho bánh xe.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mỗi link có thể khai báo ba thuộc tính hình học – vật lý tách biệt: &lt;strong&gt;Visual&lt;/strong&gt; quyết định hình dáng hiển thị trong RViz; &lt;strong&gt;Collision&lt;/strong&gt; là hình khối dùng để tính toán va chạm trong bộ mô phỏng; &lt;strong&gt;Inertial&lt;/strong&gt; khai báo khối lượng, tâm khối và tenxơ quán tính để Gazebo mô phỏng động lực học. Một link chỉ có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visual&lt;/code&gt; sẽ hiển thị bình thường nhưng không có trọng lượng; còn bánh xe có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collision&lt;/code&gt; nhưng thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inertial&lt;/code&gt; thường bị bộ mô phỏng từ chối hoặc gây mất ổn định số học khi chạy. Bạn có thể ước lượng các giá trị hợp lý ban đầu (ví dụ thân xe nhỏ khoảng 1,5 kg, mỗi bánh 0,05 kg): sai số khoảng hai lần ở giai đoạn đầu vẫn chấp nhận được, nhưng sai lệch một nghìn lần (như nhập nhầm gam thành kilôgam, hoặc dùng mesh xuất theo milimét thay vì mét) sẽ làm hỏng mô phỏng ngay lập tức.&lt;/p&gt;

&lt;p&gt;Thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;origin&amp;gt;&lt;/code&gt; của joint xác định tư thế của link con trong hệ tọa độ link cha (đơn vị mét và radian). Bánh trái đặt tại \(y = +L/2\), bánh phải tại \(y = -L/2\). Trong URDF, hình trụ mặc định có trục đối xứng dọc theo \(z\). Khi ta xoay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;origin&amp;gt;&lt;/code&gt; của joint một góc roll \(-\pi/2\) quanh trục \(x\) (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rpy=&quot;-1.5708 0 0&quot;&lt;/code&gt;), trục \(z\) cục bộ của joint sẽ xoay trùng với trục \(+y\) của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;. Vì thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;axis&amp;gt;&lt;/code&gt; trong URDF được biểu diễn trong &lt;strong&gt;hệ tọa độ của joint&lt;/strong&gt; (sau khi đã áp dụng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rpy&lt;/code&gt;), nên trục quay đúng để bánh lăn là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;axis xyz=&quot;0 0 1&quot;/&amp;gt;&lt;/code&gt; (trục \(z\) cục bộ của joint, tương ứng trục \(y\) của thân xe). Nếu chọn sai trục quay, khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; sẽ làm bánh xoay ngang như đồng xu thay vì lăn tiến.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;xacro&lt;/strong&gt; mở rộng XML bằng thuộc tính (property) và macro. Ví dụ khai báo thuộc tính:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;track&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.16&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wheel_r&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.033&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wheel_w&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.02&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bạn có thể tham chiếu giá trị bằng biểu thức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;${track/2}&lt;/code&gt; và chỉ cần sửa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; tại một chỗ duy nhất mỗi khi đo lại kích thước xe. Hướng dẫn chính thức &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Using-Xacro-to-Clean-Up-a-URDF-File.html&quot;&gt;dùng xacro&lt;/a&gt; trình bày chi tiết cách viết macro để rút gọn phần lặp lại khi tệp mô tả dài dần.&lt;/p&gt;

&lt;p&gt;Node &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; lắng nghe topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joint_states&lt;/code&gt; và xuất bản TF cho mọi khớp. Các khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fixed&lt;/code&gt; luôn được xuất bản ngay cả khi chưa có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joint_states&lt;/code&gt;, còn khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; sẽ giữ ở góc 0 cho đến khi có node phát &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/JointState&lt;/code&gt;. Ở lần kiểm tra đầu tiên trong RViz, góc 0 là đủ để xác nhận bánh xe đã hiển thị đúng vị trí; plugin vi sai ở chương 10 sẽ đảm nhiệm việc làm bánh xe quay.&lt;/p&gt;

&lt;p&gt;Kiểm tra tính hợp lệ của tệp trước khi khởi chạy:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;liburdfdom-tools
check_urdf &amp;lt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;xacro src/capstone_description/urdf/capstone.urdf.xacro&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; in ra danh sách đầy đủ các link và joint với gốc là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, không có lỗi XML dạng “not well-formed”.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Tạo gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_description&lt;/code&gt; bằng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg create capstone_description --build-type ament_cmake&lt;/code&gt; (khai báo phụ thuộc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;urdf&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; và cài đặt thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;urdf&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CMakeLists.txt&lt;/code&gt;), sau đó lưu tệp sau tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws/src/capstone_description/urdf/capstone.urdf.xacro&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot;?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;robot&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:xacro=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://www.ros.org/wiki/xacro&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;capstone&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;track&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.16&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wheel_r&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.033&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wheel_w&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.02&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;link&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;base_link&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;visual&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;origin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 0.05&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;rpy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;geometry&amp;gt;&amp;lt;box&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;size=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.18 0.12 0.06&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&amp;lt;/geometry&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/visual&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;collision&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;origin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 0.05&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;rpy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;geometry&amp;gt;&amp;lt;box&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;size=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.18 0.12 0.06&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&amp;lt;/geometry&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/collision&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;mass&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1.5&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;inertia&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixx=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.004&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;iyy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.006&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;iyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;izz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.008&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/inertial&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/link&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:macro&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wheel&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;params=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;prefix y_sign&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;link&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${prefix}_wheel&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;visual&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;geometry&amp;gt;&amp;lt;cylinder&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;radius=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${wheel_r}&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;length=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${wheel_w}&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&amp;lt;/geometry&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/visual&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;collision&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;geometry&amp;gt;&amp;lt;cylinder&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;radius=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${wheel_r}&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;length=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${wheel_w}&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&amp;lt;/geometry&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/collision&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;mass&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.05&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;inertia&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixx=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1e-4&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;iyy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1e-4&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;iyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;izz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1e-4&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/inertial&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/link&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;joint&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${prefix}_wheel_joint&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;continuous&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;parent&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;link=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;base_link&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;child&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;link=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${prefix}_wheel&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;origin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 ${y_sign * track/2} ${wheel_r}&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;rpy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-1.5708 0 0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;axis&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/joint&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/xacro:macro&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:wheel&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;prefix=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;left&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;y_sign=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:wheel&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;prefix=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;right&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;y_sign=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;link&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;caster&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;joint&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;caster_joint&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;fixed&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;parent&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;link=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;base_link&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;child&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;link=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;caster&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;origin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-0.08 0 0.02&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;rpy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/joint&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/robot&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Link &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caster&lt;/code&gt; được chủ động để trống nhằm minh họa khớp cố định trên cây TF mà chưa cần tạo hình phức tạp (bạn có thể bổ sung khối cầu visual nếu muốn quan sát trực quan hơn). Tuy nhiên, không được để trống thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;collision&amp;gt;&lt;/code&gt; cho hai bánh dẫn động vì Gazebo cần bề mặt tiếp xúc tại đó để xe chạy được.&lt;/p&gt;

&lt;p&gt;Khung tệp launch sau khi gói đã được cài đặt:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LaunchDescription&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch.substitutions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PathJoinSubstitution&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch_ros.actions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Node&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch_ros.substitutions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FindPackageShare&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;generate_launch_description&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;xacro_file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PathJoinSubstitution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;FindPackageShare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;capstone_description&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;urdf&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;capstone.urdf.xacro&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;xacro &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xacro_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LaunchDescription&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;robot_state_publisher&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;executable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;robot_state_publisher&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;robot_description&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rviz2&lt;/code&gt;, chọn Fixed Frame là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; và thêm hiển thị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RobotModel&lt;/code&gt;. &lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; một thân hình hộp cùng hai bánh hình trụ hai bên trái – phải có trục đối xứng song song với trục \(y\). Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run tf2_tools view_frames&lt;/code&gt; sẽ thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; là cha của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left_wheel&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right_wheel&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caster&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thử đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.16&lt;/code&gt; thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.20&lt;/code&gt; rồi khởi chạy lại: khoảng cách giữa hai bánh phải giãn rộng đối xứng. Nếu chỉ một bên thay đổi, nghĩa là bạn đã ghi cứng tọa độ ở một bánh thay vì dùng thuộc tính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-ten-xơ-quán-tính-inertial-cho-bánh-xe-hình-trụ&quot;&gt;Giải thích: Tính tay ten-xơ quán tính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt; cho bánh xe hình trụ&lt;/h3&gt;

&lt;p&gt;Rất nhiều người mới gõ đại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ixx=&quot;1.0&quot; iyy=&quot;1.0&quot; izz=&quot;1.0&quot;&lt;/code&gt; vào URDF và ngạc nhiên khi xe trong Gazebo rung lắc hoặc không bẻ lái được. Hãy tính đúng theo công thức cơ học vật rắn cho &lt;strong&gt;1 bánh xe hình trụ đặc&lt;/strong&gt; khối lượng \(m = 0{,}20\text{ kg}\), bán kính \(r = 0{,}04\text{ m}\), bề rộng \(h = 0{,}02\text{ m}\) (trục quay là trục \(z\) của hình trụ):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Quán tính quanh trục quay (\(I_{zz}\)):
\(I_{zz} = \frac{1}{2} m r^2 = \frac{1}{2} (0{,}20)(0{,}04)^2 = \mathbf{1{,}6 \times 10^{-4}\text{ kg}\cdot\text{m}^2}.\)&lt;/li&gt;
  &lt;li&gt;Quán tính quanh hai trục vuông góc (\(I_{xx} = I_{yy}\)):
\(I_{xx} = I_{yy} = \frac{1}{12} m (3r^2 + h^2) = \frac{0{,}20}{12}\left(3(0{,}0016) + 0{,}0004\right) = \frac{0{,}20 \times 0{,}0052}{12} \approx \mathbf{8{,}67 \times 10^{-5}\text{ kg}\cdot\text{m}^2}.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So sánh giá trị thật (\(0{,}00016\)) với con số gõ đại (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.0&lt;/code&gt;): sai lệch tới hơn &lt;strong&gt;\(6.000\times\)&lt;/strong&gt;! Chỉ cần đặt đúng công thức này vào macro &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt;, mô phỏng Gazebo lập tức ổn định và sát thực tế.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check_urdf&lt;/code&gt; trên tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; đã khai triển và dán danh sách các link vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Khởi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, mở RViz để kiểm tra hình dáng mô hình và ghi nhận ngắn gọn: “hai bánh, thân hộp, fixed frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.”&lt;/li&gt;
  &lt;li&gt;Cập nhật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; bằng đúng giá trị \(L\) đã đo ở bài 08-04, rồi xác minh tọa độ \(y\) của bánh trái bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo base_link left_wheel&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Trên một bản sao thử nghiệm, xóa thẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;collision&amp;gt;&lt;/code&gt; của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; và tự trả lời trong một câu xem Gazebo sẽ xử lý thân xe ra sao (ý cốt lõi cần rút ra trước chương 10: vật thể chỉ có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visual&lt;/code&gt; mà không có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collision&lt;/code&gt; sẽ đi xuyên qua tường).&lt;/li&gt;
  &lt;li&gt;Xác định kiểu khớp phù hợp cho một tay đòn servo chỉ quay trong khoảng \(-90^\circ\) đến \(90^\circ\) (kiểu khớp này sẽ gặp ở phần MoveIt, không dùng cho bánh xe).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_description&lt;/code&gt; bị rỗng&lt;/td&gt;
      &lt;td&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Command([&quot;xacro &quot;, path])&lt;/code&gt; chạy lỗi, hoặc tệp chưa được cài vào thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;share/&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh xe bị lồng vào trong thân hộp&lt;/td&gt;
      &lt;td&gt;Nhập nhầm tọa độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;origin xyz&lt;/code&gt; theo centimét, hoặc đặt \(z = 0\) khiến trục bánh nằm sát đáy hộp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh xe quay lệch trục trong mô phỏng&lt;/td&gt;
      &lt;td&gt;Vector &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;axis&amp;gt;&lt;/code&gt; không trùng với trục hình trụ sau phép quay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rpy&lt;/code&gt; của joint&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check_urdf&lt;/code&gt; báo thiếu link con&lt;/td&gt;
      &lt;td&gt;Sai tên tham số trong macro; chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro file.urdf.xacro&lt;/code&gt; để kiểm tra XML đã khai triển&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;RViz báo lỗi Fixed Frame&lt;/td&gt;
      &lt;td&gt;Đang chọn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; trong khi mô hình chưa có SLAM xuất bản frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài thực hành mô hình hóa này không cần mua thêm linh kiện. Nếu bạn dự định chế tạo khung xe mới và muốn URDF khớp sát với phần cứng, một cặp bánh đồng nhất về kích thước quan trọng hơn việc nâng cấp máy tính:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Linh kiện&lt;/th&gt;
      &lt;th&gt;Từ khóa Shopee hoặc Lazada&lt;/th&gt;
      &lt;th&gt;Khoảng giá 2026&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh cao su robot khoảng 65 mm (nên mua theo cặp)&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh xe robot 65mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Khoảng 20.000–60.000 đồng mỗi bánh (nên mua cùng lúc hai chiếc)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh đa hướng&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh đa hướng robot 30mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Khoảng 15.000–40.000 đồng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thước kẹp điện tử (nếu cần đo chính xác đường kính bánh hơn thước dây ở bài 08-04)&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thước kẹp điện tử 150mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Khoảng 80.000–200.000 đồng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Luôn đo đường kính rồi chia đôi sang đơn vị mét trước khi gán &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_r&lt;/code&gt;: bánh đường kính 65 mm tương ứng \(r = 0,0325\) m, không phải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;65&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [How do we describe a robot? With URDF!&lt;/td&gt;
          &lt;td&gt;Getting Ready to Build Robots with ROS #7](https://www.youtube.com/watch?v=CwdbsvcpOHM) — Articulated Robotics (tiếng Anh). Giới thiệu cách mô tả link và joint bằng một tệp URDF tối giản trước khi bổ sung bánh xe.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=DoT3iAgY9Vc&quot;&gt;ROS2 URDF Tutorial Xacro Files (Simplify URDF with Xacro Propety and Xacro Macro)&lt;/a&gt; — Kevin Wood&lt;/td&gt;
          &lt;td&gt;Robotics &amp;amp; AI (tiếng Anh). Cách dùng property và macro trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; để loại bỏ mã lặp; chú ý đặt đúng tên macro khi tách tệp.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; cho bánh trái trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y = -0.08&lt;/code&gt; dù bạn đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y_sign&lt;/code&gt; của bánh trái là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+1&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.16&lt;/code&gt;. Bạn đã nhầm lẫn ở đâu? &lt;em&gt;Gợi ý:&lt;/em&gt; kiểm tra lại dấu trong lời gọi macro hoặc kiểm tra xem bạn có đang echo nhầm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right_wheel&lt;/code&gt; không (theo REP-103, chiều \(+y\) hướng sang trái).&lt;/li&gt;
  &lt;li&gt;Vì sao khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caster&lt;/code&gt; được khai báo là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fixed&lt;/code&gt; dù trên xe thật bánh đa hướng có thể xoay tự do? &lt;em&gt;Gợi ý:&lt;/em&gt; trong phạm vi khóa học, bánh đa hướng không được dẫn động và ta chưa cần mô phỏng cơ cấu xoay của nó; khớp cố định chỉ giữ vị trí frame tham chiếu.&lt;/li&gt;
  &lt;li&gt;Mô hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visual&lt;/code&gt; tải từ tệp mesh hiển thị lớn gấp 1000 lần thực tế. Nguyên nhân là gì và cần chỉnh thẻ nào? &lt;em&gt;Gợi ý:&lt;/em&gt; tệp mesh được xuất theo đơn vị milimét thay vì mét; cần thêm hệ số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale&lt;/code&gt; cho cả mesh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visual&lt;/code&gt; lẫn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collision&lt;/code&gt; để vùng va chạm khớp đúng hình hiển thị.&lt;/li&gt;
  &lt;li&gt;Nếu hệ thống không nhận được topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joint_states&lt;/code&gt;, hai bánh xe sẽ hiển thị như thế nào trong RViz? &lt;em&gt;Gợi ý:&lt;/em&gt; hai bánh vẫn xuất hiện tại vị trí góc quay bằng 0 của khớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt;; chuyển động quay chỉ xuất hiện khi có node xuất bản trạng thái khớp ở chương 10.&lt;/li&gt;
  &lt;li&gt;Tên thuộc tính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; nào cần giữ đồng nhất với cấu hình YAML của cầu nối để tránh sai lệch dữ liệu? &lt;em&gt;Gợi ý:&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; (tương ứng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt; trong YAML) — cùng một kích thước đo một lần, dùng ở hai nơi và đều theo đơn vị mét.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Building-a-Visual-Robot-Model-with-URDF-from-Scratch.html&quot;&gt;Dựng mô hình robot bằng URDF&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Using-Xacro-to-Clean-Up-a-URDF-File.html&quot;&gt;Dùng xacro để gọn URDF&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://wiki.ros.org/urdf/XML&quot;&gt;Đặc tả XML của URDF&lt;/a&gt; — tham khảo chi tiết sự khác nhau giữa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt; (dù là trang wiki cũ nhưng cú pháp thẻ XML vẫn giữ nguyên).&lt;/li&gt;
  &lt;li&gt;Hướng dẫn của Nav2 cho Jazzy về &lt;a href=&quot;https://docs.nav2.org/jazzy/configuration_and_development/first_time_robot_setup_guide/urdf/setup_urdf/&quot;&gt;dựng URDF&lt;/a&gt; khi bạn cần bổ sung footprint sau khi mô hình cơ bản đã vượt qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check_urdf&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>TF2 và hệ tọa độ</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_05_tf2_frames/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_05_tf2_frames</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~90 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Cảm biến LiDAR gắn trên nóc xe báo thấy một chướng ngại vật ở tọa độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(1,2 m; 0,0 m)&lt;/code&gt; tính từ tâm LiDAR. Nhưng tâm LiDAR lại đặt lệch về phía trước tâm trục bánh xe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,15 m&lt;/code&gt; và cao hơn mặt đất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,22 m&lt;/code&gt;; trong khi bản đồ phòng học lại đặt gốc ở góc cửa ra vào! Nếu tự nhân ma trận xoay bằng tay rải rác trong mọi tệp mã nguồn, sai một dấu trừ là robot đâm thẳng vào tường. &lt;strong&gt;TF2 (Transform Library 2)&lt;/strong&gt; chuẩn hóa toàn bộ hình học không gian của robot thành một &lt;strong&gt;cây hệ tọa độ có gắn dấu thời gian&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-cây-hệ-tọa-độ-tf2&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Cây Hệ tọa độ TF2&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cây biến đổi chuẩn REP-105 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_frame&lt;/code&gt;)&lt;/strong&gt;:
Mỗi hệ tọa độ chỉ có &lt;strong&gt;đúng 1 cha&lt;/strong&gt; (tạo thành cây, không có chu trình).
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;: mượt, liên tục nhưng trôi dần theo thời gian (từ encoder/IMU).&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt;: do SLAM/AMCL hiệu chỉnh (có thể nhảy nhẹ khi nhận diện vòng lặp) để giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → base_link&lt;/code&gt; chính xác toàn cục.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Biến đổi Tĩnh (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf_static&lt;/code&gt;) vs. Biến đổi Động (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf&lt;/code&gt;)&lt;/strong&gt;:
Vị trí LiDAR hay camera bắt ốc cố định lên khung xe phát trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf_static&lt;/code&gt; (chỉ gửi 1 lần với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRANSIENT_LOCAL&lt;/code&gt;); còn chuyển động của xe và các khớp cánh tay phát liên tục trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Phép hợp thành ma trận biến đổi đồng nhất \(T \in SE(3)\)&lt;/strong&gt;:
Muốn đổi điểm \(p\) từ hệ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; sang hệ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, TF2 tự động nhân chuỗi ma trận dọc theo đường đi trên cây: \(T_{\text{map}}^{\text{laser}} = T_{\text{map}}^{\text{odom}} \cdot T_{\text{odom}}^{\text{base}} \cdot T_{\text{base}}^{\text{laser}}\) tại đúng thời điểm \(t\)!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ xuất bản được một phép biến đổi tĩnh (static transform) từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;, kiểm tra nó bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; và vẽ lại chính xác chuỗi hệ tọa độ theo chuẩn REP-105 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom → base_link → cảm biến&lt;/code&gt;) kèm tên node phụ trách từng cạnh. Bạn cũng sẽ hiểu vì sao hai publisher cùng phát một cạnh là lỗi nghiêm trọng, đồng thời thiết lập đúng hệ tọa độ quang học cho camera thay vì giả định camera hướng dọc theo trục \(+x\). Khi nắm vững hệ thuật ngữ này, dữ liệu odometry bánh xe từ chương 08 mới có thể trở thành cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt; hoàn chỉnh.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Nắm kiến thức về topic ở bài 09-02 và quy ước hệ tọa độ thân xe ở bài 08-04: trục \(+x\) hướng về phía trước, \(+y\) sang trái, \(+z\) hướng lên trên, góc yaw dương khi quay sang trái. Bạn cần chạy được hai cửa sổ terminal đã nạp môi trường Jazzy và có sẵn RViz (đi kèm gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-desktop&lt;/code&gt;). Chưa cần lidar thật vì chúng ta sẽ dùng một static publisher đại diện cho vị trí lắp cảm biến.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Mỗi điểm đo laser chỉ là cặp giá trị khoảng cách và góc nằm trong hệ tọa độ của chính cảm biến laser, còn mục tiêu di chuyển lại là một pose trong hệ tọa độ bản đồ. Các mô-đun như Nav2, SLAM hay thị giác máy tính không thể “tự đoán” vị trí gắn cảm biến trên thân xe. TF2 đóng vai trò cây biến đổi dùng chung, trả lời câu hỏi: “Hệ tọa độ A nằm ở đâu trong hệ tọa độ B tại thời điểm \(t\)?”. Nếu URDF đặt tên cảm biến là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_link&lt;/code&gt; trong khi dữ liệu quét lại khai báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scan&lt;/code&gt;, các điểm đo sẽ không thể quy chiếu vào không gian robot. Tương tự, nếu hai node cùng xuất bản cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;, vị trí robot trong RViz sẽ nhảy loạn xạ và bản đồ chi phí (costmap) bị nhòe. Cả bộ mô phỏng ở chương 10 lẫn SLAM ở chương 11 đều dựa vào cây TF này, nên mọi độ lệch vị trí cảm biến trên xe Capstone (tính bằng mét) cần được xác định chuẩn ngay từ đây trước khi tinh chỉnh bộ lập kế hoạch.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_tf_tree.png&quot; alt=&quot;Chuỗi frame REP-105&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Một &lt;strong&gt;frame&lt;/strong&gt; là một hệ tọa độ thuận tay phải có tên xác định. Một &lt;strong&gt;transform&lt;/strong&gt; gồm phép tịnh tiến và phép quay dùng để chuyển tọa độ điểm từ frame con sang frame cha. TF2 lưu trữ các phép biến đổi này kèm dấu thời gian (timestamp) để các node khác truy vấn khi cần, thay vì ghi cứng “lidar đặt phía trước 8 cm” lặp đi lặp lại trong cả node SLAM lẫn node nhận diện.&lt;/p&gt;

&lt;p&gt;Chuẩn &lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; quy định tên các hệ tọa độ cho robot di động mà khóa học sử dụng:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;: hệ tọa độ thế giới do SLAM hoặc mô-đun định vị (localization) duy trì; được phép nhảy đột ngột khi hệ thống hiệu chỉnh sai số trôi tích lũy.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt;: hệ tọa độ liên tục, đặt gốc tại vị trí robot khởi hành; có thể trôi dần theo thời gian nhưng tuyệt đối không được nhảy gián đoạn.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;: gắn cố định vào thân xe, thường đặt tại trung điểm trục hai bánh với xe vi sai, trục \(+x\) hướng về phía trước.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_footprint&lt;/code&gt;: hình chiếu của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; xuống mặt sàn; hữu ích cho điều hướng nhưng chưa bắt buộc với mô hình đầu tiên.&lt;/li&gt;
  &lt;li&gt;Các frame cảm biến (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imu_link&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt;): gắn dưới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; và thường là biến đổi tĩnh.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Phân công node xuất bản từng cạnh trong cây TF:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Cạnh&lt;/th&gt;
      &lt;th&gt;Node xuất bản&lt;/th&gt;
      &lt;th&gt;Đặc tính&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;SLAM hoặc AMCL (ở chương sau)&lt;/td&gt;
      &lt;td&gt;Được phép nhảy để hiệu chỉnh trôi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Odometry bánh xe (cầu nối hoặc bộ điều khiển)&lt;/td&gt;
      &lt;td&gt;Liên tục, không nhảy gián đoạn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → laser&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;URDF qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, hoặc static publisher&lt;/td&gt;
      &lt;td&gt;Cố định theo vị trí lắp cơ khí&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nguyên tắc bắt buộc là mỗi frame con chỉ có một frame cha và mỗi cạnh chỉ do duy nhất một nguồn xuất bản. Nếu bạn chạy thêm một node odometry thứ hai để đối chiếu, hai node sẽ xung đột trực tiếp trừ khi bạn tách dữ liệu đối chiếu sang một nhánh khác (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom_raw&lt;/code&gt;), tuyệt đối không cùng xuất bản vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Dấu thời gian là yếu tố quan trọng trong TF2. Khi robot đang quay, một khung quét laser ghi nhận cách đây 200 ms phải được quy đổi bằng transform tại đúng thời điểm cách đây 200 ms chứ không phải thời điểm hiện tại. TF2 duy trì một bộ đệm lịch sử cho mục đích này; nếu mốc thời gian truy vấn không có trong bộ đệm, hệ thống sẽ báo lỗi ngoại suy (extrapolation error). Nguyên nhân phổ biến là odometry đã ngừng cập nhật, các node dùng lệch đồng hồ (một số node bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt;, số khác không), hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; trong dữ liệu quét không tồn tại trên cây TF.&lt;/p&gt;

&lt;p&gt;Hệ tọa độ camera tuân theo một quy ước riêng. Theo &lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt;, hệ tọa độ thân hướng dọc theo \(+x\), nhưng hệ quang học (optical frame) lại hướng dọc theo \(+z\), với \(+x\) sang phải và \(+y\) hướng xuống dưới để khớp với thứ tự hàng – cột pixel trên ảnh. Do đó, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; theo quy ước thân xe, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_optical_frame&lt;/code&gt; là tên ghi trong trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; của message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt;. Nếu nhầm lẫn hai hệ này, đối tượng nằm bên trái khung hình có thể khiến robot rẽ nhầm sang phải. Phép chuyển từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_optical_frame&lt;/code&gt; là một phép quay cố định (roll \(-\pi/2\), sau đó yaw \(-\pi/2\)), chỉ cần khai báo một lần trong cây TF.&lt;/p&gt;

&lt;p&gt;Toàn bộ đơn vị trong TF2 là mét và radian. Nếu bạn nhập nhầm số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5&lt;/code&gt; với ý định biểu diễn 5 cm, hệ thống sẽ hiểu là 5 m và toàn bộ dữ liệu cảm biến sẽ bị lệch hoàn toàn.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Giả sử xe Capstone gắn một lidar nhỏ nằm phía trước tâm trục 5 cm, cao 8 cm so với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, hướng thẳng về trước (không có góc nghiêng roll, pitch, yaw). Ta xuất bản transform tĩnh như sau:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run tf2_ros static_transform_publisher &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--x&lt;/span&gt; 0.05 &lt;span class=&quot;nt&quot;&gt;--y&lt;/span&gt; 0.0 &lt;span class=&quot;nt&quot;&gt;--z&lt;/span&gt; 0.08 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--roll&lt;/span&gt; 0 &lt;span class=&quot;nt&quot;&gt;--pitch&lt;/span&gt; 0 &lt;span class=&quot;nt&quot;&gt;--yaw&lt;/span&gt; 0 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--frame-id&lt;/span&gt; base_link &lt;span class=&quot;nt&quot;&gt;--child-frame-id&lt;/span&gt; laser
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở terminal thứ hai để kiểm tra:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run tf2_ros tf2_echo base_link laser
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi&lt;/strong&gt; (in lặp lại vài lần mỗi giây):&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;At time ...
- Translation: [0.050, 0.000, 0.080]
- Rotation: in Quaternion [0.000, 0.000, 0.000, 1.000]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Thứ tự tham số của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; là frame cha trước, frame con sau (trả lời câu hỏi “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; nằm ở đâu trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;”). Nếu đảo ngược thứ tự hai tên, giá trị tịnh tiến sẽ đổi dấu — đây vừa là cách kiểm tra nhanh, vừa là lỗi dễ nhầm.&lt;/p&gt;

&lt;p&gt;Xuất sơ đồ cây TF ra tệp PDF:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run tf2_tools view_frames
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lệnh này tạo ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frames.pdf&lt;/code&gt; (và tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.gv&lt;/code&gt;) tại thư mục hiện tại. Mở tệp PDF lên: bạn phải thấy đúng một mũi tên duy nhất từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;. Nếu xuất hiện hai mũi tên trỏ vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;, nghĩa là đang có hai publisher xung đột.&lt;/p&gt;

&lt;p&gt;Kiểm tra nhanh bằng phép tính tay: một điểm nằm phía trước lidar 1 m có tọa độ \((1, 0, 0)\) trong frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;, nên trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; nó sẽ ở khoảng \((1,05, 0, 0,08)\). Khi chạy Nav2 bạn không cần cộng tay mà sẽ truy vấn trực tiếp từ TF2; phép cộng tay ở đây chỉ giúp xác nhận các thông số tĩnh đã nhập đúng đơn vị mét.&lt;/p&gt;

&lt;p&gt;Nếu có camera và muốn khai báo sẵn hệ quang học:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run tf2_ros static_transform_publisher &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--x&lt;/span&gt; 0 &lt;span class=&quot;nt&quot;&gt;--y&lt;/span&gt; 0 &lt;span class=&quot;nt&quot;&gt;--z&lt;/span&gt; 0 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--roll&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;.5708 &lt;span class=&quot;nt&quot;&gt;--pitch&lt;/span&gt; 0 &lt;span class=&quot;nt&quot;&gt;--yaw&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;.5708 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--frame-id&lt;/span&gt; camera_link &lt;span class=&quot;nt&quot;&gt;--child-frame-id&lt;/span&gt; camera_optical_frame
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lưu ý nối tiếp đúng chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → camera_link → camera_optical_frame&lt;/code&gt;; không gắn song song cả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_optical_frame&lt;/code&gt; trực tiếp vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; với hai độ lệch riêng biệt.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-tọa-độ-vật-cản-từ-hệ-laser-sang-hệ-map-trong-mặt-phẳng-2d&quot;&gt;Giải thích: Tính tay tọa độ vật cản từ hệ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; sang hệ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; trong mặt phẳng 2D&lt;/h3&gt;

&lt;p&gt;Giả sử tại thời điểm \(t\):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Robot (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;) đang đứng ở tọa độ \((x, y) = (2{,}0\text{ m},\; 1{,}0\text{ m})\) trong hệ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, mũi xe quay hướng \(\theta = 90^\circ\) (nhìn dọc theo trục \(+y\) của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;LiDAR (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_frame&lt;/code&gt;) gắn phía trước tâm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; đúng \(0{,}20\text{ m}\) (không xoay lệch).&lt;/li&gt;
  &lt;li&gt;LiDAR đo được một cột trụ nằm ngay phía trước nó ở tọa độ cục bộ \(p_{\text{laser}} = (1{,}0;\; 0{,}0)\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tính tọa độ cột trụ trong hệ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; qua 2 bước:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Đổi từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;:
\(p_{\text{base}} = (1{,}0 + 0{,}20,\; 0{,}0) = \mathbf{(1{,}20;\; 0{,}0)}\text{ m}.\)&lt;/li&gt;
  &lt;li&gt;Áp dụng phép xoay \(90^\circ\) (\(\cos 90^\circ = 0, \sin 90^\circ = 1\)) rồi tịnh tiến theo vị trí robot \((2{,}0;\; 1{,}0)\):
\(\begin{pmatrix} x_{\text{map}} \\ y_{\text{map}} \end{pmatrix} = \begin{pmatrix} 0 &amp;amp; -1 \\ 1 &amp;amp; 0 \end{pmatrix} \begin{pmatrix} 1{,}20 \\ 0{,}0 \end{pmatrix} + \begin{pmatrix} 2{,}0 \\ 1{,}0 \end{pmatrix} = \begin{pmatrix} 0 \\ 1{,}20 \end{pmatrix} + \begin{pmatrix} 2{,}0 \\ 1{,}0 \end{pmatrix} = \mathbf{\begin{pmatrix} 2{,}0 \\ 2{,}20 \end{pmatrix}}\text{ m}.\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Kiểm tra trực giác: xe ở \((2{,}0;\; 1{,}0)\) hướng thẳng lên trục \(+y\), vật cản cách tâm xe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1,20 m&lt;/code&gt; phía trước nên nằm ở \((2{,}0;\; 2{,}20)\) — khớp tuyệt đối!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Xuất bản transform tĩnh của lidar theo độ lệch đo thực tế trên xe của bạn. Nếu chưa đo kịp, dùng ví dụ 5 cm / 8 cm và ghi chú “chưa đo thực tế” vào sổ, sau đó đo lại bằng thước dây đã dùng ở bài 08-04.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; cho đến khi xác nhận giá trị tịnh tiến khớp chính xác từng milimét với thông số đã nhập.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;view_frames&lt;/code&gt; và lưu tệp PDF kết quả vào thư mục lab.&lt;/li&gt;
  &lt;li&gt;Thử chạy thêm một static publisher thứ hai có cùng frame cha và frame con nhưng đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--x 0.5&lt;/code&gt;, rồi kiểm tra lại bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt;. &lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; giá trị tịnh tiến nhảy qua lại giữa 0,05 và 0,5 (hoặc trục tọa độ trong RViz giật liên tục). Sau khi quan sát xong, tắt ngay publisher dư thừa này — đây chính là biểu hiện đặc trưng khi hai node odometry xung đột.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;(Tùy chọn)&lt;/em&gt; Mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rviz2&lt;/code&gt;, thêm mục hiển thị TF và chọn Fixed Frame là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; để quan sát trục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; nằm phía trước 5 cm. Nếu đổi Fixed Frame sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; khi chưa chạy SLAM, RViz sẽ báo lỗi vì hệ thống chưa có frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; chờ mãi không in&lt;/td&gt;
      &lt;td&gt;Gõ lệch tên frame hoặc publisher đã dừng; dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /tf_static --once&lt;/code&gt; để kiểm tra dữ liệu thô&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trục tọa độ hướng ngược về sau&lt;/td&gt;
      &lt;td&gt;Góc yaw bị đặt là \(\pi\), hoặc đo nhầm chiều “phía trước” về phía bánh đa hướng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lỗi ngoại suy ở các node xử lý sau&lt;/td&gt;
      &lt;td&gt;Dấu thời gian lệch sang tương lai hoặc chỉ một số node bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt;; đối chiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;header.stamp&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; khi có Gazebo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cây TF xuất hiện hai nhánh tới cùng một frame&lt;/td&gt;
      &lt;td&gt;Có hai publisher cùng phát một cạnh; kiểm tra bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;view_frames&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /tf -v&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Camera nhận diện bị ngược hướng trái/phải&lt;/td&gt;
      &lt;td&gt;Trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; của ảnh đang gắn vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_optical_frame&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài lab này không đòi hỏi phần cứng mới. Khi lắp cảm biến thật lên xe, độ chính xác của cây TF phụ thuộc trực tiếp vào độ chắc chắn của giá đỡ cơ khí:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Linh kiện&lt;/th&gt;
      &lt;th&gt;Nơi mua&lt;/th&gt;
      &lt;th&gt;Từ khóa tìm kiếm&lt;/th&gt;
      &lt;th&gt;Khoảng giá 2026&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cảm biến MPU6050 (nếu sau này cần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imu_link&lt;/code&gt;, chưa cần cho bài này)&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;GY-521 trên Hshop&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Shopee/Lazada &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Khoảng 30.000–70.000 đồng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lidar 360° cỡ nhỏ (họ LD19 / LD06 / YDLIDAR X2), chỉ cần khi tới chương 11&lt;/td&gt;
      &lt;td&gt;Tìm “lidar” trên &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; (lưu ý RPLIDAR S2 là dòng tầm xa hơn nhiều)&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lidar LD19 robot&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YDLIDAR X2&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Khoảng 1.500.000–3.500.000 đồng cho dòng nhỏ (họ S2 đắt hơn nhiều, cần xem giá trực tiếp)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trụ đồng / giá đỡ cơ khí&lt;/td&gt;
      &lt;td&gt;Các cửa hàng linh kiện điện tử&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trụ đồng M3 20mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Khoảng 15.000–40.000 đồng một vỉ&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Lưu ý rằng cảm biến đo khoảng cách đơn điểm TF-Luna (&lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-dfrobot-tf-luna-tof-micro-single-point-ranging-lidar&quot;&gt;trang Hshop&lt;/a&gt;) không phải là lidar quét tạo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt; cho SLAM, nên không mua loại này cho lộ trình bài học.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [The ROS Transform System (TF)&lt;/td&gt;
          &lt;td&gt;Getting Ready to Build Robots with ROS #6](https://www.youtube.com/watch?v=QyvHhY4Y_Y8) — Articulated Robotics (tiếng Anh). Trình bày cấu trúc cây TF2 và vai trò của từng node xuất bản phép biến đổi, đối chiếu trực tiếp với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; và cảm biến laser trong bài.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=zjMuIxRvygQ&quot;&gt;Quaternions and 3d rotation, explained interactively&lt;/a&gt; — 3Blue1Brown (tiếng Anh). Giải thích trực quan về quaternion mà TF2 dùng để biểu diễn góc quay (thứ tự &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x, y, z, w&lt;/code&gt; như đã học ở chương 08).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; trả về tịnh tiến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[0.05, 0, 0.08]&lt;/code&gt; trong khi bạn muốn lidar nằm chính giữa đường tâm xe. Thành phần nào biểu thị độ lệch ngang? &lt;em&gt;Gợi ý:&lt;/em&gt; thành phần thứ hai (\(y\)); giá trị 0 cho thấy cảm biến đã nằm đúng giữa trục dọc.&lt;/li&gt;
  &lt;li&gt;Vì sao cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt; được phép nhảy gián đoạn còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt; thì không? &lt;em&gt;Gợi ý:&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt; phản ánh bước hiệu chỉnh sai số tích lũy từ định vị toàn cục, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt; là đầu vào liên tục cho bộ điều khiển cục bộ; nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt; nhảy đột ngột, bộ lập kế hoạch cục bộ sẽ hiểu nhầm robot bị dịch chuyển tức thời và dừng hoạt động.&lt;/li&gt;
  &lt;li&gt;Bạn muốn chạy hai thuật toán ước lượng odometry cùng lúc để so sánh mà không gây xung đột trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;. Bạn nên đặt tên frame cho nhánh thứ hai như thế nào? &lt;em&gt;Gợi ý:&lt;/em&gt; không xuất bản trùng vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;; chỉ giữ một node chính xuất bản cạnh thật (ví dụ từ bộ lọc EKF), còn luồng thô dùng để so sánh gắn sang một frame riêng như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom_enc&lt;/code&gt; hoặc một frame gỡ lỗi độc lập.&lt;/li&gt;
  &lt;li&gt;Dữ liệu quét laser khai báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_frame&lt;/code&gt; trong khi URDF đặt tên là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;. Hệ thống TF sẽ xảy ra vấn đề gì? &lt;em&gt;Gợi ý:&lt;/em&gt; cây TF bị đứt kết nối vì tên frame chính là giao diện liên kết; cần thống nhất dùng một tên duy nhất ở cả hai nơi.&lt;/li&gt;
  &lt;li&gt;Vận tốc dài \(v\) và vận tốc góc \(\omega\) ở bài 08-04 đi vào cây TF ở cạnh nào? &lt;em&gt;Gợi ý:&lt;/em&gt; chúng được tích phân theo thời gian thành pose của robot và xuất bản lên cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;; các cạnh cảm biến tĩnh không mang thông tin vận tốc.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Tf2/Introduction-To-Tf2.html&quot;&gt;Giới thiệu TF2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Tf2/Writing-A-Tf2-Static-Broadcaster-Py.html&quot;&gt;Static transform publisher&lt;/a&gt; — hướng dẫn viết node Python thay vì chạy bằng cờ dòng lệnh.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; và &lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Xem hướng dẫn trực tiếp của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;view_frames&lt;/code&gt; trên máy; lưu ý cú pháp cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--x&lt;/code&gt; là chuẩn mới được hỗ trợ trong Jazzy.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Launch, parameter và QoS</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_04_launch_params_qos/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_04_launch_params_qos</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~90 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Một robot tự hành thực thụ thường có từ 8 đến 20 node chạy song song. Bạn không thể mở 15 cửa sổ terminal để gõ tay từng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run&lt;/code&gt; kèm chục tham số mỗi lần khởi động! &lt;strong&gt;Launch files&lt;/strong&gt; (viết bằng Python/YAML), hệ thống &lt;strong&gt;Parameters&lt;/strong&gt; động và chính sách chất lượng dịch vụ &lt;strong&gt;QoS (Quality of Service)&lt;/strong&gt; chính là bộ ba công cụ biến các node rời rạc thành một hệ thống robot vận hành tin cậy chỉ bằng một lệnh duy nhất.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-vận-hành-hệ-thống-ros-2&quot;&gt;Kiến thức cần nhớ: Ba trụ cột Vận hành Hệ thống ROS 2&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Launch System (Khởi chạy đồng bộ &amp;amp; Remapping)&lt;/strong&gt;:
Khởi chạy nhiều node, nạp tệp cấu hình YAML chung và đổi tên topic (&lt;em&gt;remapping&lt;/em&gt;) mà không phải sửa lại mã nguồn C++/Python.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Parameters (Tham số hóa động)&lt;/strong&gt;:
Tách toàn bộ hằng số tinh chỉnh (bán kính bánh \(r\), hệ số PID, ngưỡng vận tốc tối đa) ra tệp YAML để có thể hiệu chỉnh ngay tại hiện trường mà không cần biên dịch lại.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chính sách QoS (Reliability, Durability, History)&lt;/strong&gt;:
Quyết định hành vi truyền tin trên mạng không hoàn hảo: dữ liệu cảm biến tần số cao ưu tiên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt; (thà bỏ 1 khung cũ còn hơn chờ truyền lại làm trễ khung mới), còn bản đồ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt; hay lệnh cấu hình cần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRANSIENT_LOCAL&lt;/code&gt; (người nghe vào sau vẫn nhận được bản đồ!).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ khởi chạy được hai node cùng lúc từ một tệp launch Python, ghi đè một tham số (parameter) từ cả tệp launch lẫn dòng lệnh, và chủ động tạo ra sự không tương thích QoS để quan sát tình huống &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/chatter&lt;/code&gt; vẫn hoạt động nhưng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo&lt;/code&gt; không nhận được dữ liệu. Bạn cũng sẽ đọc được đầu ra của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info -v&lt;/code&gt; để xác định bên nào đang dùng chính sách &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt; và bên nào yêu cầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt; — kỹ năng chẩn đoán quan trọng khi kết nối driver lidar với RViz ở chương 11.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành bài 09-01 và 09-02: môi trường Jazzy đã được nạp và bạn đã chạy thử &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo /chatter&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;demo_nodes_cpp&lt;/code&gt;. Bạn cần biết tạo tệp bên trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws/src&lt;/code&gt;; bài này chưa cần robot thật. Lưu ý rằng “parameter” trong bài là biến cấu hình có tên gắn với một node (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt;, cổng serial, chiều rộng trục bánh), không phải tùy chọn dòng lệnh của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Chạy riêng lẻ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run&lt;/code&gt; ở nhiều terminal chỉ phù hợp cho các ví dụ thử nghiệm ban đầu, không phải cách vận hành khung xe Capstone. Cầu nối điều khiển, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, teleop và sau này là Nav2 cần được khởi động đồng bộ, trong đó khoảng cách hai bánh đo ở bài 08-04 được khai báo dưới dạng parameter tập trung thay vì ghi cứng rải rác ở nhiều tệp.&lt;/p&gt;

&lt;p&gt;Mảnh ghép còn lại là QoS (Quality of Service). Dữ liệu quét laser là luồng liên tục có thể chấp nhận mất gói nhỏ, còn bản đồ là dữ liệu cần truyền đủ tới nơi. ROS 2 không tự động ghép một publisher &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt; với một subscriber &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt;, và sự từ chối kết nối này thường diễn ra âm thầm. Rất nhiều người mất hàng giờ gỡ lỗi Nav2 trong khi nguyên nhân thực sự chỉ là hai đầu topic không tương thích QoS. Thực hành trước hiện tượng này trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/chatter&lt;/code&gt; sẽ giúp bạn nhận ra ngay khi gặp lại trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_qos.png&quot; alt=&quot;QoS tương thích và không tương thích&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Tệp launch&lt;/strong&gt; mô tả tập hợp các tiến trình cần khởi chạy cùng nhau. Trong ROS 2 Jazzy, khóa học viết tệp launch bằng Python với hai thư viện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;launch&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;launch_ros&lt;/code&gt; để khởi chạy node, truyền parameter và lồng ghép các tệp launch khác. Dù định dạng XML vẫn được hỗ trợ, Python là chuẩn trong tài liệu nhập môn chính thức và cho phép ghép đường dẫn động bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PathJoinSubstitution&lt;/code&gt; thay vì phụ thuộc vào đường dẫn tương đối dễ lỗi sau khi biên dịch bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameter&lt;/strong&gt; là biến cấu hình có kiểu dữ liệu rõ ràng thuộc về một node (bool, int, double, string hoặc mảng của các kiểu này). Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 param list /talker&lt;/code&gt; liệt kê các tham số mà node đã khai báo, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 param get&lt;/code&gt; đọc giá trị hiện tại. Truyền tham số qua dòng lệnh (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--ros-args -p name:=value&lt;/code&gt;) chỉ ghi đè giá trị mặc định cho riêng phiên chạy đó. Để quản lý toàn bộ cấu hình cho một robot, ta lưu chúng trong tệp YAML:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;capstone_bridge&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ros__parameters&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/dev/ttyUSB0&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;baud&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;115200&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;track_width&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0.16&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;cmd_timeout_ms&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;300&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros__parameters&lt;/code&gt; là bắt buộc; nếu đặt thẳng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width: 0.16&lt;/code&gt; ở cấp cao nhất, node sẽ không nạp được giá trị này, và tên node ở dòng đầu tiên phải khớp chính xác. Giá trị khoảng cách hai bánh \(L\) đo ở bài 08-04 chỉ cần khai báo một lần duy nhất tại đây để cầu nối đọc vào, tránh tình trạng lưu trùng lặp ở nhiều nơi dẫn đến sai lệch đơn vị.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;QoS&lt;/strong&gt; là tập quy ước truyền thông gắn trên từng publisher và subscriber. Ba chính sách người mới thường gặp vướng mắc nhất là độ tin cậy (reliability), độ bền dữ liệu (durability) và độ sâu lịch sử (history depth).&lt;/p&gt;

&lt;p&gt;Về &lt;strong&gt;reliability&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt; đảm bảo gửi lại gói tin nếu mất, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt; thì không. Một subscriber &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt; chỉ ghép được với publisher &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt;; subscriber &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt; nhận được từ cả hai; còn subscriber &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt; &lt;strong&gt;không&lt;/strong&gt; tương thích với publisher &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt;. Các driver cảm biến (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/LaserScan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt;) thường phát theo chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt; với hàng đợi ngắn (hồ sơ &lt;em&gt;sensor data&lt;/em&gt;), trong khi hiển thị LaserScan trên RViz đôi khi mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt;. Khi gặp lỗi này, cách xử lý là điều chỉnh lại QoS của hiển thị hoặc subscriber thay vì khởi động lại lidar.&lt;/p&gt;

&lt;p&gt;Về &lt;strong&gt;durability&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; không lưu lại message cũ cho các node tham gia muộn, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transient_local&lt;/code&gt; có giữ lại bản ghi gần nhất — nhờ đó bản đồ chỉ cần xuất bản một lần nhưng các node khởi động sau vẫn nhận được. Một publisher &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; sẽ không đáp ứng được subscriber yêu cầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transient_local&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Về &lt;strong&gt;history depth&lt;/strong&gt;: đây là kích thước hàng đợi lưu message. Độ sâu bằng 1 nghĩa là chỉ giữ lại dữ liệu mới nhất — rất hợp lý với cảm biến lidar 10 Hz, nhưng không phù hợp cho một chuỗi lệnh ngắn liên tiếp nếu hệ thống không có cơ chế watchdog xử lý hết hạn.&lt;/p&gt;

&lt;p&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /scan -v&lt;/code&gt; sẽ hiển thị song song hồ sơ QoS mà publisher cung cấp và subscriber yêu cầu. Nếu hai bên không tương thích, chúng sẽ không ghép nối với nhau và lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; dùng QoS mặc định sẽ không in ra bất kỳ dữ liệu nào.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Tạo gói mới và tệp launch trong workspace đã nạp môi trường:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~/ros2_ws/src
ros2 pkg create capstone_bringup &lt;span class=&quot;nt&quot;&gt;--build-type&lt;/span&gt; ament_python &lt;span class=&quot;nt&quot;&gt;--dependencies&lt;/span&gt; launch_ros
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; capstone_bringup/launch capstone_bringup/config
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tạo tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bringup/launch/talker_listener.launch.py&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LaunchDescription&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch_ros.actions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Node&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;generate_launch_description&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LaunchDescription&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;demo_nodes_cpp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;executable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;talker&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;talker&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;use_sim_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}],&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;demo_nodes_cpp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;executable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;listener&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;listener&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Quy tắc đóng gói: trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup.py&lt;/code&gt;, danh sách &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data_files&lt;/code&gt; bắt buộc phải khai báo thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;launch&lt;/code&gt;, nếu không &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 launch&lt;/code&gt; sẽ không tìm thấy tệp sau khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon build&lt;/code&gt;. Cú pháp tiêu chuẩn như sau:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glob&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glob&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# bên trong data_files:
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;share&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;package_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;launch&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;glob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;launch/*.launch.py&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tiếp theo, biên dịch, nạp overlay và khởi chạy:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~/ros2_ws
colcon build &lt;span class=&quot;nt&quot;&gt;--packages-select&lt;/span&gt; capstone_bringup
&lt;span class=&quot;nb&quot;&gt;source install&lt;/span&gt;/setup.bash
ros2 launch capstone_bringup talker_listener.launch.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; trong cùng một terminal xuất hiện cả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Publishing: &apos;Hello World: N&apos;&lt;/code&gt; lẫn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I heard: [Hello World: N]&lt;/code&gt;, và nhấn Ctrl-C sẽ dừng đồng thời cả hai node.&lt;/p&gt;

&lt;p&gt;Kiểm tra tham số khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt; đang chạy:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 param get /talker use_sim_time
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Boolean value is: False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thử nghiệm cố ý tạo lệch QoS: mở Terminal A và xuất bản theo chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub /chatter std_msgs/msg/String &lt;span class=&quot;s2&quot;&gt;&quot;{data: hi}&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--qos-reliability&lt;/span&gt; best_effort
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở Terminal B và chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; với cấu hình mặc định (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /chatter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; phía publisher vẫn đếm số lần gửi nhưng phía &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; hoàn toàn im lặng. Kiểm tra chi tiết bằng lệnh:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic info /chatter &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bạn sẽ thấy reliability của publisher là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;, của subscription là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt;, và hai đầu không ghép nối được với nhau. Bây giờ chạy lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; với chính sách khớp:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /chatter &lt;span class=&quot;nt&quot;&gt;--qos-reliability&lt;/span&gt; best_effort
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data: hi&lt;/code&gt; lập tức xuất hiện. Hãy ghi lại cặp lệnh này vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; để làm mẫu chẩn đoán cho tình huống “RViz không hiển thị dữ liệu quét laser” về sau.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-ma-trận-tương-thích-qos--vì-sao-hai-node-cùng-tên-topic-nhưng-không-nhận-được-tin&quot;&gt;Giải thích: Ma trận tương thích QoS — Vì sao hai node cùng tên Topic nhưng KHÔNG nhận được tin?&lt;/h3&gt;

&lt;p&gt;Một lỗi “tàng hình” rất hay gặp trong ROS 2 là Publisher và Subscriber trùng tên topic 100%, không báo lỗi biên dịch, nhưng hoàn toàn không truyền tin cho nhau do &lt;strong&gt;xung đột QoS Reliability&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Publisher (Bên phát)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Subscriber (Bên nhận)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Kết nối có khớp không?&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Giải thích&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;KHỚP&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Cả hai cam kết truyền tin cậy có gửi lại gói mất&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;KHỚP&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Bên phát cho chất lượng cao hơn mức bên nhận yêu cầu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;KHỚP&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Cả hai ưu tiên độ trễ thấp (chuẩn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SensorDataQoS&lt;/code&gt; cho LiDAR/Camera)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;KHÔNG KHỚP (TỊT!)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Bên nhận đòi hỏi tin cậy tuyệt đối nhưng bên phát không đảm bảo → DDS từ chối ghép kênh!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Mẹo chẩn đoán trong 3 giây: gõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /scan --verbose&lt;/code&gt; để xem trực tiếp bảng QoS của cả Publisher và Subscriber!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Biên dịch gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bringup&lt;/code&gt; và chạy tệp launch gồm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listener&lt;/code&gt;. Lưu lại đoạn log xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listener&lt;/code&gt; đã nhận được dữ liệu từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Tạo tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bringup/config/bridge.yaml&lt;/code&gt; chứa bốn khóa cấu hình như ở phần khái niệm (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;port&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;baud&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_timeout_ms&lt;/code&gt;). Ở bước này chưa cần node cầu nối; kiểm tra cú pháp YAML từ thư mục gói bằng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3 -c &quot;import yaml; print(yaml.safe_load(open(&apos;config/bridge.yaml&apos;)))&quot;&lt;/code&gt;. &lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; một từ điển Python có khóa cấp cao nhất duy nhất là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bridge&lt;/code&gt;, bên trong chứa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros__parameters&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Thực hiện thí nghiệm lệch QoS giữa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt;. Ghi lại kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info -v&lt;/code&gt; cho cả trường hợp lỗi lẫn trường hợp đã sửa khớp.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;(Tùy chọn)&lt;/em&gt; Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic hz /chatter&lt;/code&gt; khi đang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt;. Nếu không có cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--rate&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic pub&lt;/code&gt; gửi khá chậm; hãy thêm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r 10&lt;/code&gt; và kiểm tra xem tần số đo được có gần 10 Hz hay không.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file not found&lt;/code&gt; khi chạy launch&lt;/td&gt;
      &lt;td&gt;Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup.py&lt;/code&gt; chưa cài thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;launch/&lt;/code&gt;, hoặc quên nạp lại overlay sau khi build&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listener&lt;/code&gt; không in gì dù không lệch QoS&lt;/td&gt;
      &lt;td&gt;Khai báo nhầm thành hai node &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt;, hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listener&lt;/code&gt; bị thoát do sai kiểu parameter&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tệp YAML hợp lệ nhưng node bỏ qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Thiếu khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros__parameters&lt;/code&gt;, hoặc tên node trong YAML không khớp tên thực tế&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; chạy ở terminal này nhưng không chạy ở terminal kia&lt;/td&gt;
      &lt;td&gt;Một bên có truyền cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--qos-reliability&lt;/code&gt; khác với mặc định&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;True&lt;/code&gt; khiến node bị đứng về sau&lt;/td&gt;
      &lt;td&gt;Không có node nào xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;; cần giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;False&lt;/code&gt; cho đến khi dùng Gazebo ở chương 10&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài thực hành này không cần linh kiện mới; laptop từ bài 09-01 là đủ. Nếu bạn đã chuẩn bị sẵn Raspberry Pi 5 cho robot, hãy hoàn thành bài lab trên laptop trước rồi mới lặp lại bước kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; trên Pi, tránh phải gỡ đồng thời cả lỗi QoS lẫn sự cố thẻ nhớ microSD.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=xJ3WAs8GndA&quot;&gt;ROS2 - Create a Launch File with Python&lt;/a&gt; — Robotics Back-End (tiếng Anh). Hướng dẫn viết tệp launch Python để khởi chạy nhiều node cùng lúc, tương ứng với cấu trúc trong bài.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[ROS 2 on Two Computers: Connect Laptop and Robot with DDS + QoS&lt;/td&gt;
          &lt;td&gt;08](https://www.youtube.com/watch?v=HEKwLJC5a2o) — Adaptive AgroTech (tiếng Anh). Giải thích cấu hình DDS, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; và chính sách reliability của QoS khi kết nối hai máy tính trên cùng mạng.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tệp YAML của cầu nối đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_timeout_ms: 300&lt;/code&gt; và firmware trên MCU cũng có watchdog 300 ms. Nếu tiến trình trên laptop bị treo đột ngột nhưng cáp USB vẫn cắm, cơ chế nào sẽ dừng động cơ? &lt;em&gt;Gợi ý:&lt;/em&gt; watchdog trên firmware. Timeout trong YAML chỉ giúp cầu nối quyết định chủ động &lt;em&gt;gửi&lt;/em&gt; lệnh 0; khi cầu nối đã ngừng hoạt động thì nó không gửi được gì nữa, và bộ định thời trên MCU mới là lớp bảo vệ dừng bánh xe.&lt;/li&gt;
  &lt;li&gt;Một bạn cùng lớp xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; ở chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt; nhưng mở RViz với cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt;. Thông tin nào trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info -v&lt;/code&gt; chứng minh ngay nguyên nhân? &lt;em&gt;Gợi ý:&lt;/em&gt; publisher báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;, subscription báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt;, và hai đầu không kết nối được với nhau.&lt;/li&gt;
  &lt;li&gt;Vì sao chính sách &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transient_local&lt;/code&gt; phù hợp cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt; nhưng lại nguy hiểm nếu dùng cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;? &lt;em&gt;Gợi ý:&lt;/em&gt; RViz mở sau vẫn cần nhận lại bản đồ mới nhất, nhưng cầu nối động cơ khởi động sau tuyệt đối không nên thực thi lại một lệnh vận tốc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; từ mười phút trước.&lt;/li&gt;
  &lt;li&gt;Nếu bạn muốn tệp launch từ chối khởi chạy khi thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt;, đây có phải vấn đề của QoS không? &lt;em&gt;Gợi ý:&lt;/em&gt; không, đây là cơ chế khai báo parameter. Node cần khai báo bắt buộc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt; thay vì gán ngầm giá trị mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.0&lt;/code&gt;, hoặc tệp launch phải truyền rõ tham số này.&lt;/li&gt;
  &lt;li&gt;Hãy xác định trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone.launch.py&lt;/code&gt; về sau (gồm cầu nối serial, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;teleop_twist_keyboard&lt;/code&gt;), thông số nào bắt buộc phải đưa ra thành parameter thay vì ghi cứng? &lt;em&gt;Gợi ý:&lt;/em&gt; cổng serial và chiều rộng trục bánh (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt;); riêng teleop chỉ xuất bản lệnh nên có thể khởi động sau.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Launch/Launch-Main.html&quot;&gt;Launch nhiều node&lt;/a&gt; và hướng dẫn &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Launch/Creating-Launch-Files.html&quot;&gt;tạo file launch&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Parameters/Understanding-ROS2-Parameters.html&quot;&gt;Tìm hiểu parameter&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Quality-of-Service-Settings.html&quot;&gt;Về thiết lập Quality of Service&lt;/a&gt; — nên đọc kỹ bảng tương thích giữa các chính sách, không chỉ đọc tên.&lt;/li&gt;
  &lt;li&gt;Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo -h&lt;/code&gt; trực tiếp trên máy để xem đầy đủ các cờ QoS đã dùng trong bài lab.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Service và action</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_03_services_actions/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_03_services_actions</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~75 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Topic dạng dòng chảy liên tục rất tuyệt cho cảm biến LiDAR hay vận tốc bánh xe, nhưng lại cực kỳ bất tiện khi bạn muốn hỏi &lt;em&gt;“Lưu bản đồ này xong chưa?”&lt;/em&gt; hoặc ra lệnh &lt;em&gt;“Hãy di chuyển tới phòng 302, báo tiến độ mỗi giây và dừng ngay nếu tôi bấm Hủy!”&lt;/em&gt;. Đó là lý do ROS 2 cung cấp thêm hai mẫu giao tiếp bậc cao hơn: &lt;strong&gt;Service&lt;/strong&gt; (hỏi–đáp đồng bộ ngắn) và &lt;strong&gt;Action&lt;/strong&gt; (tác vụ dài có phản hồi tiến độ và hỗ trợ hủy).&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-so-sánh-ba-mẫu-giao-tiếp-trong-ros-2&quot;&gt;Kiến thức cần nhớ: So sánh ba mẫu giao tiếp trong ROS 2&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Topic (Luồng một chiều, nhiều–nhiều)&lt;/strong&gt;:
Dành cho dữ liệu liên tục có tần suất (cảm biến, odometry, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;). Không xác nhận đã nhận từng gói.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Service (Yêu cầu – Phản hồi ngắn, 1–1)&lt;/strong&gt;:
Dành cho thao tác nhanh, rời rạc cần kết quả xác nhận (reset odometry, đổi chế độ lái, bật/tắt rơ-le). Không được dùng cho tác vụ kéo dài nhiều giây vì sẽ chặn luồng xử lý.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Action (Goal – Feedback – Result + Cancel)&lt;/strong&gt;:
Xây dựng từ bộ ba kênh (gửi mục tiêu, nhận tiến độ định kỳ, nhận kết quả cuối và cho phép hủy ngang an toàn). Đây là chuẩn bắt buộc cho &lt;strong&gt;Nav2&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateToPose&lt;/code&gt;) và &lt;strong&gt;MoveIt 2&lt;/strong&gt; (điều khiển cánh tay).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ gọi được một service cộng hai số để nhận phản hồi trực tiếp, đồng thời gửi mục tiêu (goal) cho action Fibonacci và phân biệt rõ phản hồi tiến độ (feedback) với kết quả cuối cùng (result). Bạn cũng sẽ chọn đúng cơ chế topic, service hoặc action cho bốn tác vụ phổ biến: truyền liên tục vận tốc bánh xe, đặt lại bộ đếm encoder, di chuyển tới một tư thế (pose) đích và xuất bản dữ liệu quét laser. Nhờ đó, khi gặp action &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateToPose&lt;/code&gt; của Nav2 ở chương sau, bạn sẽ nhận ra nó có cùng cấu trúc với ví dụ Fibonacci nhưng được bổ sung thêm bản đồ.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Đã làm quen với ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt; ở bài 09-02 và biết cách chạy song song các tiến trình ở nhiều cửa sổ terminal khác nhau. Bài này chưa cần robot thật.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Việc truyền liên tục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; phù hợp với topic vì lệnh mới nhất luôn thay thế lệnh cũ và hệ thống không cần đợi phản hồi trước chu kỳ 50 ms kế tiếp. Ngược lại, bài toán “đã tới đích chưa?” sẽ rất bất tiện nếu dùng topic vì cần theo dõi tiến độ và hỗ trợ hủy giữa chừng — đó chính là lý do action ra đời và là cách Nav2 nhận mục tiêu di chuyển ở chương 11. Trong khi đó, service dành cho các tác vụ ngắn dạng hỏi-đáp: bật/tắt một cờ logic, kích hoạt hiệu chuẩn hoặc tính một tổng nhỏ. Nếu đặt một chuyến di chuyển kéo dài năm phút vào service, cách hủy duy nhất của bạn sẽ là ngắt cưỡng bức bằng Ctrl-C. Dù dùng cơ chế nào, watchdog của Capstone vẫn là lớp bảo vệ timeout trên firmware; khi hủy một action, hệ thống vẫn cần gửi kèm lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; bằng 0 để bánh xe không tiếp tục quay theo lệnh cũ.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_svc_action.png&quot; alt=&quot;Một trả lời service so với dòng thời gian action&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;minh-hoạ-tại-sao-dùng-ros-2-cho-robot-tự-hành&quot;&gt;Minh hoạ: Tại sao dùng ROS 2 cho robot tự hành&lt;/h3&gt;

&lt;div class=&quot;reel-video&quot; style=&quot;text-align:center;margin:1.5rem 0;&quot;&gt;
&lt;video controls=&quot;&quot; playsinline=&quot;&quot; preload=&quot;metadata&quot; style=&quot;width:100%;max-width:360px;height:auto;aspect-ratio:9/16;border-radius:12px;background:#10121a;&quot; src=&quot;/public/videos/reels/ros2-why-amr_vi.mp4&quot;&gt;&lt;/video&gt;
&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Video khoảng 3 phút 56 giây (bản đầu chưa có lời).&lt;/em&gt; Một robot trong kho đi từ A tới B qua năm việc: LiDAR quét, bản đồ ô lưới tô dần, định vị (SLAM), A* tìm đường và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; điều khiển. Khi tự viết, năm khối cần 10 đường nối riêng (8 khối cần 28), đổi LiDAR là gãy. Với ROS 2, video cho thấy ba kênh: topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; một người đăng hai người nghe, service &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map_server/load_map&lt;/code&gt; hỏi rồi đáp, action &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/navigate_to_pose&lt;/code&gt; gửi mục tiêu, nhận feedback quãng đường còn lại, hủy giữa chừng rồi gửi lại tới khi thành công. Đổi LiDAR A sang B vẫn chạy vì cùng kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt;, nhưng khi LiDAR lắp quay ngược mà TF vẫn khai yaw 0° thì điểm quét lệch khỏi tường; sửa bằng TF &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → laser&lt;/code&gt; yaw 180° (xem &lt;a href=&quot;/contents/vi/chapter09/09_05_tf2_frames/&quot;&gt;09_05 TF2 và hệ tọa độ&lt;/a&gt;). Cuối video là SLAM Toolbox dựng bản đồ, Nav2 lập lại đường khi có thùng chắn, và RViz2 (dữ liệu minh họa). Đọc tiếp &lt;a href=&quot;/contents/vi/chapter11/11_01_slam_overview/&quot;&gt;11_01 Tổng quan SLAM&lt;/a&gt; và &lt;a href=&quot;/contents/vi/chapter11/11_02_nav2_tour/&quot;&gt;11_02 Tour stack Nav2&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Service&lt;/strong&gt; hoạt động theo mô hình yêu cầu – phản hồi (request – response): client gửi một message yêu cầu, server xử lý rồi trả về một message kết quả và kết thúc lời gọi. Ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;example_interfaces/srv/AddTwoInts&lt;/code&gt; nhận hai số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; và trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sum&lt;/code&gt;. Cơ chế này chỉ phù hợp cho các tác vụ ngắn mà bên gọi có thể chờ; tuyệt đối không dùng cho luồng dữ liệu liên tục, vì một lời gọi service bị chặn bên trong vòng điều khiển động cơ sẽ làm trượt chu kỳ thời gian thực của bài 08-05.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Action&lt;/strong&gt; bao gồm mục tiêu (goal), luồng phản hồi tiến độ (feedback), kết quả cuối (result) và cơ chế hủy (cancel). Trong ví dụ Fibonacci, client gửi giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order&lt;/code&gt; và nhận liên tục dãy số đang được tính dần; nếu quá lâu, client có thể gửi yêu cầu hủy. Action điều hướng của Nav2 có cấu trúc hoàn toàn tương tự: đầu vào là pose đích, dọc đường trả về feedback (quãng đường còn lại, pose hiện tại) và cuối cùng trả về kết quả (thành công hoặc bị hủy). Các hành vi phục hồi sự cố (recovery) nằm trong cây hành vi (behavior tree) sẽ được giới thiệu sơ lược ở chương 11, nhưng bạn không cần đến nó để hiểu vai trò của action.&lt;/p&gt;

&lt;p&gt;Bảng phân loại thực tế cho robot trong khóa học:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tác vụ&lt;/th&gt;
      &lt;th&gt;Cơ chế phù hợp&lt;/th&gt;
      &lt;th&gt;Lý do&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Gửi lệnh vận tốc bánh ở 10–50 Hz&lt;/td&gt;
      &lt;td&gt;Topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Chỉ cần lệnh mới nhất, không cần phản hồi từng gói&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dữ liệu quét laser (Laser scan)&lt;/td&gt;
      &lt;td&gt;Topic&lt;/td&gt;
      &lt;td&gt;Luồng dữ liệu liên tục, thường dùng QoS best-effort&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;“Xóa bộ đếm encoder”&lt;/td&gt;
      &lt;td&gt;Service&lt;/td&gt;
      &lt;td&gt;Một yêu cầu, một phản hồi, hoàn thành trong vài mili giây&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;“Chạy tới pose đích này”&lt;/td&gt;
      &lt;td&gt;Action&lt;/td&gt;
      &lt;td&gt;Kéo dài nhiều giây hoặc phút, cần theo dõi tiến độ và hủy được&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điện áp pin&lt;/td&gt;
      &lt;td&gt;Topic&lt;/td&gt;
      &lt;td&gt;Luồng dữ liệu định kỳ, tần số thấp vẫn phù hợp&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Lưu ý rằng việc hủy một action không tự động ngắt dòng điện cấp cho động cơ. Cầu nối điều khiển vẫn phải coi trạng thái “không có lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; mới” là lệnh dừng — quy tắc này vốn đã được cài đặt trong timeout serial và cần được giữ nguyên khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; bắt đầu đến từ bộ điều khiển của action server thay vì bàn phím.&lt;/p&gt;

&lt;h2 id=&quot;hủy-tác-vụ-là-một-message-hợp-lệ-không-phải-ngắt-cưỡng-bức&quot;&gt;Hủy tác vụ là một message hợp lệ, không phải ngắt cưỡng bức&lt;/h2&gt;

&lt;p&gt;Server Fibonacci sẽ tiếp tục tính toán cho đến khi hoàn thành mục tiêu hoặc nhận được lệnh hủy. Khi bạn gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order: 20&lt;/code&gt; và quan sát cửa sổ server, dãy số feedback sẽ dài dần. Nhấn Ctrl-C trên cửa sổ &lt;em&gt;client&lt;/em&gt; sẽ gửi yêu cầu hủy goal qua dòng lệnh, trong khi tiến trình server vẫn chạy bình thường và lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 action list&lt;/code&gt; vẫn thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/fibonacci&lt;/code&gt;. Nếu cách duy nhất để dừng một tác vụ là tắt hẳn tiến trình server, hệ thống đó không có cơ chế hủy mà chỉ đang bị sập (crash).&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 action send_goal /fibonacci action_tutorials_interfaces/action/Fibonacci &lt;span class=&quot;s2&quot;&gt;&quot;{order: 20}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ngắt lệnh trên bằng Ctrl-C, rồi kiểm tra ngay:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 action list
ros2 node list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/fibonacci&lt;/code&gt; và node server vẫn tồn tại. Nếu bạn gửi tiếp một goal ngắn hơn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order: 5&lt;/code&gt;), server vẫn phải tính và trả về kết quả bình thường. Đây chính là đặc tính cần có ở Nav2 sau này: một lượt điều hướng bị hủy hoặc thất bại không buộc bạn phải khởi động lại toàn bộ hệ thống. Service không có khả năng này — không tồn tại lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 service cancel&lt;/code&gt;; client hoặc phải chờ, hoặc bị tắt ngang trong khi server có thể vẫn đang kẹt giữa chừng.&lt;/p&gt;

&lt;p&gt;Ghi chú thêm về hệ quả đối với động cơ: hủy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateToPose&lt;/code&gt; chỉ làm action server ngừng gửi feedback mới, chứ không tự động đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; cuối về 0 nếu bộ điều khiển hoặc cầu nối không chủ động làm việc đó. Watchdog 300 ms trên firmware (chương 08) đóng vai trò lưới an toàn cuối cùng. Một cầu nối thiết kế tốt sẽ chủ động xuất bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; bằng 0 cả khi nhận lệnh hủy &lt;em&gt;lẫn&lt;/em&gt; khi mất luồng dữ liệu, tránh tình trạng xe vẫn tiếp tục trôi sau khi bạn bấm nút dừng trên RViz.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-chọn-đúng-mẫu-giao-tiếp-cho-4-tình-huống-thực-tế-trên-robot&quot;&gt;Giải thích: Chọn đúng mẫu giao tiếp cho 4 tình huống thực tế trên robot&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Yêu cầu trên robot&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Chọn mẫu nào?&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Lý do kỹ thuật&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Đọc góc nghiêng từ cảm biến IMU ở 100 Hz&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Topic&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Dữ liệu liên tục tần suất cao, nhiều node (EKF, điều khiển) cùng cần nghe&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Xóa sạch bộ đếm odometry về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0, 0, 0)&lt;/code&gt; trước khi chạy test&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Service&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Thao tác rời rạc dưới 5 ms, cần biết chắc MCU đã thực hiện xong&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Ra lệnh robot tự hành đi 15 mét qua hành lang tới phòng lab&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Action&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Kéo dài 30 giây, cần xem khoảng cách còn lại (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feedback&lt;/code&gt;) và phải hủy được ngay khi gặp sự cố&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Nhấn nút dừng khẩn cấp (E-Stop)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Phần cứng ngắt nguồn + Topic ưu tiên&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Không bao giờ chờ phản hồi đồng bộ khi cần cắt điện khẩn cấp!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Thực hành service trên hai cửa sổ terminal theo hướng dẫn &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Services/Understanding-ROS2-Services.html&quot;&gt;Tìm hiểu service&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run demo_nodes_cpp add_two_ints_server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 service list
ros2 service &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; /add_two_ints
ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts &lt;span class=&quot;s2&quot;&gt;&quot;{a: 2, b: 3}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;example_interfaces/srv/AddTwoInts
response:
example_interfaces.srv.AddTwoInts_Response(sum=5)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu terminal chạy server chưa được bật, lệnh gọi service sẽ bị treo chờ phản hồi — đó cũng là lý do nếu đặt lệnh động cơ vào service, chỉ cần server gặp sự cố là cả hệ thống điều khiển sẽ bị đứng. Nhấn Ctrl-C để dừng client.&lt;/p&gt;

&lt;p&gt;Thực hành action trên hai cửa sổ terminal theo hướng dẫn &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Actions/Understanding-ROS2-Actions.html&quot;&gt;Tìm hiểu action&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run action_tutorials_py fibonacci_action_server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 action list
ros2 action info /fibonacci
ros2 action send_goal /fibonacci action_tutorials_interfaces/action/Fibonacci &lt;span class=&quot;s2&quot;&gt;&quot;{order: 5}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; các dòng feedback hiển thị dãy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sequence&lt;/code&gt; tăng dần độ dài, sau đó trả về kết quả cuối cùng kèm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;goal id&lt;/code&gt;. Hãy đọc trực tiếp khối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;result&lt;/code&gt; in ra trên terminal. Tiếp theo, gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order: 20&lt;/code&gt; rồi nhấn Ctrl-C ở client để hủy: kiểm tra lại từ một terminal khác sẽ thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/fibonacci&lt;/code&gt; vẫn hiển thị trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 action list&lt;/code&gt;, chứng tỏ bạn có thể hủy mục tiêu mà không cần tắt server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Lệnh gọi service bị treo mãi&lt;/td&gt;
      &lt;td&gt;Server chưa chạy, hoặc gõ sai tên service&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package &apos;action_tutorials_py&apos; not found&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Thiếu gói mẫu; cài thêm bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt install ros-jazzy-action-tutorials-py&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chỉ thấy kết quả cuối mà không thấy feedback&lt;/td&gt;
      &lt;td&gt;Client không bật chế độ in feedback; công cụ CLI &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;send_goal&lt;/code&gt; mặc định hiển thị đầy đủ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đưa tác vụ “đi 2 m” vào service&lt;/td&gt;
      &lt;td&gt;Không có cơ chế hủy và báo tiến độ; cần chuyển sang dùng action và vẫn duy trì timeout động cơ&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Không cần mua thêm phần cứng. Đây là bài thực hành hoàn toàn trên laptop.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=WzOopOkrowA&quot;&gt;ROS/ROS 2 - What are messages, services and actions?&lt;/a&gt; — Hummingbird (tiếng Anh). So sánh sự khác biệt giữa message, service và action dựa trên việc có cần chờ kết quả hay không, khớp với bảng phân loại trong bài.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tác vụ “xuất bản điện áp pin mười lần mỗi giây” nên dùng cơ chế nào? &lt;em&gt;Gợi ý:&lt;/em&gt; topic.&lt;/li&gt;
  &lt;li&gt;Tác vụ “yêu cầu Nav2 di chuyển vào bếp” nên dùng cơ chế nào? &lt;em&gt;Gợi ý:&lt;/em&gt; action (tên cụ thể bạn sẽ gặp ở chương sau là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateToPose&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Một lệnh gọi service bị treo suốt 30 giây. Bước kiểm tra đầu tiên bạn nên làm là gì? &lt;em&gt;Gợi ý:&lt;/em&gt; kiểm tra tiến trình server qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 service list&lt;/code&gt; và cửa sổ terminal của server, thay vì vội cài lại ROS.&lt;/li&gt;
  &lt;li&gt;Vì sao khi hủy action, hệ thống Capstone vẫn cần gửi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; bằng 0? &lt;em&gt;Gợi ý:&lt;/em&gt; firmware giữ vận tốc bánh theo lệnh cuối cho tới khi hết thời gian chờ 300 ms của watchdog; việc gửi rõ giá trị 0 (hoặc ngắt luồng kịp thời) giúp xe dừng ngay thay vì tiếp tục trôi theo mức PWM cũ.&lt;/li&gt;
  &lt;li&gt;Trong ví dụ minh họa Fibonacci, trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order: 5&lt;/code&gt; tương ứng với khái niệm nào trên robot thực tế? &lt;em&gt;Gợi ý:&lt;/em&gt; tương ứng với pose đích cần di chuyển tới, không phải giá trị PWM trực tiếp.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Services/Understanding-ROS2-Services.html&quot;&gt;Tìm hiểu service&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Actions/Understanding-ROS2-Actions.html&quot;&gt;Tìm hiểu action&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/&quot;&gt;Bắt đầu với Nav2 (Jazzy)&lt;/a&gt; — giới thiệu action điều hướng sẽ dùng ở chương sau.&lt;/li&gt;
  &lt;li&gt;Xem trực tiếp định nghĩa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action_tutorials_interfaces/action/Fibonacci&lt;/code&gt; bằng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 interface show&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Node, topic và message</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_02_nodes_topics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_02_nodes_topics</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~75 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Nếu bạn viết toàn bộ mã đọc LiDAR, chạy SLAM, tính đường đi và điều khiển động cơ vào một tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt; khổng lồ, chỉ cần camera bị treo một nhịp là cả động cơ cũng đứng hình theo. &lt;strong&gt;ROS 2&lt;/strong&gt; giải quyết triệt để vấn đề đó bằng kiến trúc đồ thị tính toán phân tán: chia robot thành hàng chục tiến trình nhỏ độc lập gọi là &lt;strong&gt;Node&lt;/strong&gt;, nói chuyện với nhau qua các kênh dữ liệu có kiểu định nghĩa chặt chẽ gọi là &lt;strong&gt;Topic&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-node-và-topic-trong-ros-2&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Node và Topic trong ROS 2&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tách rời hoàn toàn (Decoupling) bằng Publish / Subscribe&lt;/strong&gt;:
Node phát (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Publisher&lt;/code&gt;) chỉ gửi thông điệp lên tên topic (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;) mà không cần biết có bao nhiêu node đang nghe; node nhận (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Subscriber&lt;/code&gt;) đăng ký topic đó mà không phụ thuộc ngôn ngữ lập trình của bên phát (C++ nói chuyện tự nhiên với Python).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hợp đồng kiểu dữ liệu (Message Interface) &amp;amp; Tần số xuất bản&lt;/strong&gt;:
Mỗi topic gắn chặt với một cấu trúc tin chuẩn (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt;) và một tần số cam kết (Hz). Tần số là một phần của hợp đồng thời gian thực!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Middleware DDS (Khám phá ngang hàng không cần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;roscore&lt;/code&gt;)&lt;/strong&gt;:
Khác với ROS 1 phụ thuộc một máy chủ trung tâm dễ hỏng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;roscore&lt;/code&gt;), ROS 2 chạy trên chuẩn công nghiệp &lt;strong&gt;DDS&lt;/strong&gt; cho phép các node tự phát hiện lẫn nhau trong mạng LAN.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ chạy được cặp node mẫu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listener&lt;/code&gt; trong ROS 2 Jazzy, xác định được tên topic cùng kiểu message kết nối chúng và đo tần số truyền tin bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic hz&lt;/code&gt;. Bạn cũng sẽ xuất bản thủ công một message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt;, chỉ ra đúng hai trường dữ liệu mà khung xe vi sai thực sự sử dụng, đồng thời hiểu vai trò của gói định nghĩa giao diện (interface package) để về sau các message bánh xe tự định nghĩa luôn có cấu trúc rõ ràng thay vì truyền dữ liệu kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dict&lt;/code&gt; Python rời rạc.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Hoàn thành bài 09-01 sao cho lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt; trong terminal sắp dùng. Nắm lại mô hình động học ở chương 08: trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; tương ứng với vận tốc dài \(v\) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; tương ứng với vận tốc góc \(\omega\). Bài thực hành này chưa cần robot thật.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Trong hệ thống Capstone, cầu nối điều khiển là một node, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; là một topic và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; là một kiểu message. Nếu chưa nắm vững ba khái niệm nền tảng này, bạn sẽ rất khó hình dung cách hoạt động của Gazebo, Nav2 hay pipeline xử lý ảnh camera (thực chất cũng chỉ là các node trao đổi dữ liệu qua topic). Ngay cả các mô hình học máy xuất bản hành động (action) cũng tuân theo cùng cơ chế này với kiểu message khác. Việc không vẽ được đồ thị kết nối giữa publisher, topic và subscriber sẽ khiến bạn lúng túng khi gỡ lỗi robot không phản hồi.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_ros_graph.png&quot; alt=&quot;Talker, topic, listener&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;minh-hoạ-tại-sao-dùng-ros-2-cho-robot-tự-hành&quot;&gt;Minh hoạ: Tại sao dùng ROS 2 cho robot tự hành&lt;/h3&gt;

&lt;div class=&quot;reel-video&quot; style=&quot;text-align:center;margin:1.5rem 0;&quot;&gt;
&lt;video controls=&quot;&quot; playsinline=&quot;&quot; preload=&quot;metadata&quot; style=&quot;width:100%;max-width:360px;height:auto;aspect-ratio:9/16;border-radius:12px;background:#10121a;&quot; src=&quot;/public/videos/reels/ros2-why-amr_vi.mp4&quot;&gt;&lt;/video&gt;
&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Video khoảng 3 phút 56 giây (bản đầu chưa có lời).&lt;/em&gt; Phần 1:35–1:57 là topic: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lidar_driver&lt;/code&gt; đăng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/LaserScan&lt;/code&gt; lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rviz2&lt;/code&gt; cùng nghe mà người đăng không cần biết; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;planner&lt;/code&gt; gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;. Service và action ở &lt;a href=&quot;/contents/vi/chapter09/09_03_services_actions/&quot;&gt;09_03 Service và action&lt;/a&gt;, khung tọa độ ở &lt;a href=&quot;/contents/vi/chapter09/09_05_tf2_frames/&quot;&gt;09_05 TF2 và hệ tọa độ&lt;/a&gt;, SLAM và Nav2 ở &lt;a href=&quot;/contents/vi/chapter11/11_01_slam_overview/&quot;&gt;11_01 Tổng quan SLAM&lt;/a&gt; và &lt;a href=&quot;/contents/vi/chapter11/11_02_nav2_tour/&quot;&gt;11_02 Tour stack Nav2&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Một &lt;strong&gt;node&lt;/strong&gt; là một tiến trình (hoặc một thực thể độc lập bên trong tiến trình) có tên xác định trên đồ thị ROS. Một &lt;strong&gt;topic&lt;/strong&gt; hoạt động như một kênh truyền dữ liệu (bus) có tên riêng. &lt;strong&gt;Publisher&lt;/strong&gt; gửi các message theo một kiểu dữ liệu cố định lên kênh đó, còn &lt;strong&gt;subscription&lt;/strong&gt; (subscriber) đăng ký nhận dữ liệu từ kênh. Publisher không cần gọi đích danh subscriber và nhiều subscriber có thể cùng lắng nghe một topic, nhờ vậy cả bộ ghi nhật ký (logger) lẫn cầu nối động cơ đều có thể nhận đồng thời &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Kiểu message là một phần của giao ước dữ liệu. Ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std_msgs/msg/String&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; là hai kiểu hoàn toàn khác nhau; subscriber của kiểu này sẽ không thể kết nối với publisher của kiểu kia dù tên topic giống hệt nhau. Các định nghĩa giao diện nằm trong những gói riêng, và lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 interface show geometry_msgs/msg/Twist&lt;/code&gt; sẽ liệt kê chi tiết các trường dữ liệu. Với khung xe vi sai trong khóa học, hai trường được sử dụng là:&lt;/p&gt;

\[v = \texttt{linear.x}, \qquad \omega = \texttt{angular.z}\]

&lt;p&gt;Trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; luôn giữ bằng 0, vì giá trị khác 0 biểu thị yêu cầu chuyển động trượt ngang mà cơ cấu vi sai không thể thực hiện (bài 08-04).&lt;/p&gt;

&lt;p&gt;Trên một máy tính cục bộ, quá trình khám phá node thường diễn ra trong khoảng một giây (trừ trường hợp mạng bị tường lửa hoặc cấu hình chặn). Bốn lệnh cần luyện thành thói quen gồm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 node list&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /chatter&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /chatter&lt;/code&gt;. Bên cạnh đó, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic hz /chatter&lt;/code&gt; cho biết tần số xuất bản thực tế, tương tự việc kiểm tra chu kỳ vòng lặp ở bài 08-05.&lt;/p&gt;

&lt;h2 id=&quot;tần-số-là-một-phần-của-giao-ước-truyền-tin&quot;&gt;Tần số là một phần của giao ước truyền tin&lt;/h2&gt;

&lt;p&gt;Khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic hz /chatter&lt;/code&gt; với node &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt; mẫu, tần số đo được xấp xỉ 1 Hz. Con số này mang ý nghĩa kỹ thuật trực tiếp: ở bài 08-05, ta đã thấy vòng điều khiển động cơ thiết kế cho 50 Hz nhưng thực tế chỉ chạy ở 12 Hz sẽ làm sai lệch thành phần đạo hàm. Các topic ROS cũng đòi hỏi sự đảm bảo tương tự về tần số. Khi xây dựng cầu nối Capstone, bạn sẽ đăng ký nghe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; và kiểm tra tần số bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hz&lt;/code&gt;. Nếu dùng bộ điều khiển từ bàn phím (teleop) chỉ gửi message khi phím thay đổi, tần số có thể rơi về 0 Hz lúc bạn không thao tác; khi đó watchdog trên firmware sẽ dừng bánh xe, dễ gây nhầm lẫn nếu bạn cho rằng “topic vẫn tồn tại” đồng nghĩa với “lệnh điều khiển vẫn đang được cập nhật liên tục”.&lt;/p&gt;

&lt;p&gt;Hãy thử các lệnh sau khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt; đang chạy:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic hz /chatter
ros2 topic bw /chatter
ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /chatter &lt;span class=&quot;nt&quot;&gt;--field&lt;/span&gt; data
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;average rate: 1.000&lt;/code&gt; (hoặc rất gần 1 Hz), băng thông chỉ vài byte mỗi giây, và tùy chọn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--field data&lt;/code&gt; chỉ in nội dung chuỗi mà không kèm cấu trúc YAML bao ngoài. Về sau, băng thông của một topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; cũng rất nhỏ, nhưng băng thông của luồng ảnh camera thì rất lớn; dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic bw&lt;/code&gt; giúp bạn phát hiện sớm hiện tượng nghẽn đường truyền trước khi vội kết luận lỗi nằm ở thuật toán nhận diện.&lt;/p&gt;

&lt;p&gt;Tiếp theo, hãy thử xuất bản liên tục message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; ở một tần số xác định. Mở terminal thứ nhất:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; 5 /cmd_vel geometry_msgs/msg/Twist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;{linear: {x: 0.0}, angular: {z: 0.0}}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở terminal thứ hai:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic hz /cmd_vel
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; tần số xấp xỉ 5 Hz và robot không di chuyển vì chưa có node điều khiển động cơ nào đăng ký nghe topic này. Để chạy khoảng mười giây rồi nhấn Ctrl-C để dừng và ghi lại tần số đo được. Cầu nối ở chương 10 sẽ quy ước dừng xe nếu không nhận được message nào trong 300 ms. Ở tần số 5 Hz, chu kỳ giữa hai message là 200 ms — nằm gọn trong ngưỡng an toàn; ngược lại, ở 1 Hz chu kỳ lên tới 1000 ms khiến watchdog liên tục ngắt lệnh làm xe chạy giật cục. Đây là bài toán về thời gian và động học mà bạn hoàn toàn có thể kiểm chứng trước khi đặt robot xuống sàn.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-ngân-sách-băng-thông-mạng-wi-fi-cho-đồ-thị-3-topic-của-robot&quot;&gt;Giải thích: Tính tay ngân sách băng thông mạng Wi-Fi cho đồ thị 3 Topic của robot&lt;/h3&gt;

&lt;p&gt;Giả sử robot phát đồng thời 3 topic về laptop giám sát qua Wi-Fi:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Topic&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Kiểu thông điệp&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Kích thước / tin&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Tần số&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Lưu lượng (KB/s)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;48 byte&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;20 Hz&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(48 \times 20 \approx \mathbf{1\text{ KB/s}}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~700 byte&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;50 Hz&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(700 \times 50 = \mathbf{35\text{ KB/s}}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/LaserScan&lt;/code&gt; (720 tia)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;~6 KB&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;10 Hz&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(6 \times 10 = \mathbf{60\text{ KB/s}}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Tổng cộng&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;~96 KB/s (&amp;lt; 0,8 Mbps)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Toàn bộ dữ liệu điều khiển và LiDAR 2D chỉ tốn chưa tới &lt;strong&gt;1 Mbps&lt;/strong&gt; — cực kỳ nhẹ nhàng trên Wi-Fi 5 GHz. Ngược lại, chỉ cần mở thêm 1 topic ảnh thô &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/camera/image_raw&lt;/code&gt; (~27 MB/s = 216 Mbps) là toàn bộ mạng Wi-Fi tắc nghẽn ngay lập tức!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Mở Terminal A (sau khi đã nạp môi trường Jazzy):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run demo_nodes_cpp talker
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi&lt;/strong&gt; (in lặp lại):&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[INFO] [talker]: Publishing: &apos;Hello World: 1&apos;
[INFO] [talker]: Publishing: &apos;Hello World: 2&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở Terminal B:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /chatter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;data: &apos;Hello World: 3&apos;
---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở Terminal C:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 node list
ros2 topic info /chatter
ros2 topic hz /chatter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi có dạng:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/talker
Type: std_msgs/msg/String
average rate: 1.000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Node &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt; xuất bản ở tần số khoảng 1 Hz. Nếu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hz&lt;/code&gt; báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;no messages&lt;/code&gt;, hãy kiểm tra lại tên topic hoặc xem terminal chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt; có bị tắt hay không.&lt;/p&gt;

&lt;p&gt;Dừng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talker&lt;/code&gt; bằng Ctrl-C, sau đó xuất bản thử một message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; duy nhất (bước chạy thử nghiệm trước khi có cầu nối):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub &lt;span class=&quot;nt&quot;&gt;--once&lt;/span&gt; /cmd_vel geometry_msgs/msg/Twist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;{linear: {x: 0.20, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.10}}&quot;&lt;/span&gt;
ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--once&lt;/span&gt; /cmd_vel
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Chưa có thiết bị nào chuyển động vì chưa có subscriber điều khiển động cơ — trạng thái yên tĩnh này là hoàn toàn bình thường. Ghi vào sổ thực hành: “0,20 m/s tiến, 0,10 rad/s quay trái, chưa có cầu nối.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Package &apos;demo_nodes_cpp&apos; not found&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Chưa cài bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-desktop&lt;/code&gt;, hoặc terminal hiện tại chưa nạp môi trường ROS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; không in dữ liệu&lt;/td&gt;
      &lt;td&gt;Gõ sai tên topic, hoặc cấu hình QoS không tương thích (xem bài sau; các ví dụ mẫu đã khớp sẵn)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tần số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hz&lt;/code&gt; lệch xa 1 Hz&lt;/td&gt;
      &lt;td&gt;Có publisher khác cùng gửi lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/chatter&lt;/code&gt;, hoặc máy ảo đang quá tải&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Báo lỗi cú pháp YAML khi pub &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Sai dấu nháy; hãy gộp toàn bộ lệnh thành một dòng duy nhất&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chờ ESP32 quay bánh nhưng không thấy phản ứng&lt;/td&gt;
      &lt;td&gt;ESP32 chưa được nối vào đồ thị ROS; phần subscriber cầu nối sẽ được xây dựng ở chương 10&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài này không yêu cầu phần cứng. Tùy chọn về sau là Raspberry Pi 5 (đã nêu ở bài 09-01) nếu bạn muốn đưa đồ thị ROS ra khỏi laptop. Không cần mua các bộ “kit robot ROS” chỉ để chạy thử &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/chatter&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=7TVWlADXwRw&quot;&gt;What Is ROS2? - Framework Overview&lt;/a&gt; — Raymond Andrade (tiếng Anh). Giải thích rõ hai khái niệm node và topic cùng cách hoạt động của một publisher và một subscriber.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[ROS2 &amp;amp; F1TENTH #01&lt;/td&gt;
          &lt;td&gt;ROS2 là gì? Workspace, Package, Node](https://www.youtube.com/watch?v=UJ7zkUJnVKw) — Nhat Tran (tiếng Việt). Giới thiệu workspace, package và node bằng tiếng Việt; lưu ý bản ROS bạn đang cài trên máy vẫn là Jazzy.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[ROS 2 Series #4&lt;/td&gt;
          &lt;td&gt;Viết template ROS2 Python section&lt;/td&gt;
          &lt;td&gt;TeslaEdison](https://www.youtube.com/watch?v=03kUlBnH7gg) — TeslaEdison (tiếng Việt). Hướng dẫn tạo khung một node Python; giữ đúng API &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt; của Jazzy nếu video dùng cú pháp phiên bản cũ hơn.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Giải thích ngắn gọn trong một câu: vì sao hai cửa sổ terminal có thể cùng chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo /chatter&lt;/code&gt; một lúc? &lt;em&gt;Gợi ý:&lt;/em&gt; topic hoạt động theo cơ chế bus phát tán, không phải lời gọi điểm-tới-điểm.&lt;/li&gt;
  &lt;li&gt;Trường nào trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; phải giữ bằng 0 trên khung xe Capstone và vì sao? &lt;em&gt;Gợi ý:&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt;, vì cơ cấu xe vi sai không có khả năng di chuyển ngang.&lt;/li&gt;
  &lt;li&gt;Sau khi chạy lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--once&lt;/code&gt;, kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /cmd_vel&lt;/code&gt; không thấy publisher nào. Đây có phải là lỗi không? &lt;em&gt;Gợi ý:&lt;/em&gt; không, vì lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--once&lt;/code&gt; tự kết thúc ngay sau khi gửi, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;info&lt;/code&gt; chỉ phản ánh các node đang hoạt động tại thời điểm truy vấn.&lt;/li&gt;
  &lt;li&gt;Vẽ sơ đồ đồ thị ROS dự kiến: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;teleop_twist_keyboard&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bridge&lt;/code&gt; → khung serial. Kiểu message truyền trên topic này là gì? &lt;em&gt;Gợi ý:&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 interface show&lt;/code&gt; giúp bạn tránh sai sót nào khi lập trình? &lt;em&gt;Gợi ý:&lt;/em&gt; tránh đoán mò tên trường dữ liệu (ví dụ góc quay quanh trục đứng là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;, không phải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.yaw&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Nodes/Understanding-ROS2-Nodes.html&quot;&gt;Tìm hiểu node&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html&quot;&gt;Tìm hiểu topic&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/msg/Twist.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Bài 08-04 của khóa học về phép động học nghịch chuyển đổi message &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; thành vận tốc hai bánh xe.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Cài ROS 2 Jazzy và workspace</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_01_jazzy_install_ws/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_01_jazzy_install_ws</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~90 phút&lt;/strong&gt; (có thể lâu hơn nếu bạn vừa cài mới Ubuntu).&lt;/p&gt;

&lt;p&gt;Một lỗi kinh điển của người mới học ROS 2 là gõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run ...&lt;/code&gt; rồi nhận thông báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Package not found&lt;/code&gt; dù vừa biên dịch xong không một lỗi nào. Nguyên nhân gần như luôn nằm ở một khái niệm cốt lõi: &lt;strong&gt;chuỗi chồng lớp không gian làm việc (underlay / overlay workspace)&lt;/strong&gt; và cách &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt; nạp biến môi trường vào shell hiện tại.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-cài-đặt-ros-2-jazzy--workspace-colcon&quot;&gt;Kiến thức cần nhớ: Ba trụ cột Cài đặt ROS 2 Jazzy &amp;amp; Workspace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt;&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Kiến trúc Underlay vs. Overlay&lt;/strong&gt;:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy/setup.bash&lt;/code&gt; là lớp nền (&lt;em&gt;underlay&lt;/em&gt;) chứa các gói chuẩn của hệ thống; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws/install/setup.bash&lt;/code&gt; là lớp phủ (&lt;em&gt;overlay&lt;/em&gt;) chứa mã nguồn của riêng bạn. Luôn source underlay trước khi build và source overlay trước khi chạy.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Công cụ biên dịch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt; &amp;amp; Cấu trúc gói (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package.xml&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup.py&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CMakeLists.txt&lt;/code&gt;)&lt;/strong&gt;:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon build --symlink-install&lt;/code&gt; giúp thay đổi tệp Python hay tệp YAML có hiệu lực ngay mà không cần biên dịch lại toàn bộ không gian làm việc.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quản lý phụ thuộc bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rosdep&lt;/code&gt; &amp;amp; Cô lập mạng bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt;&lt;/strong&gt;:
Trong phòng lab có 20 nhóm sinh viên dùng chung Wi-Fi, đặt mỗi nhóm một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; riêng (từ 0 đến 101) giúp robot của bạn không nhận nhầm lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; từ bàn bên cạnh!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ cài đặt được ROS 2 Jazzy Jalisco trên Ubuntu 24.04 từ gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.deb&lt;/code&gt; chính thức, tạo workspace overlay bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt; và kiểm chứng rằng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; chỉ hoạt động trong terminal mới sau khi nạp (source) đúng thứ tự underlay rồi đến overlay. Bạn cũng sẽ nhận diện được lỗi trộn môi trường giữa Humble và Jazzy qua thông báo hệ thống, đồng thời xác định rõ vai trò của máy tính chạy ROS so với vi điều khiển (MCU) trên xe Capstone.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Nếu máy tính đặt trên xe của bạn là Raspberry Pi 5 hoặc Jetson, hãy thực hiện bài &lt;a href=&quot;/contents/vi/chapter09/09_00_linux_on_sbc/&quot;&gt;Linux trên máy tính của robot&lt;/a&gt; trước hoặc song song: hệ điều hành phải là Ubuntu 24.04, và mạch Jetson đang chạy Jammy (22.04) thì chưa thể cài Jazzy. Bạn cần biết dùng các lệnh terminal cơ bản như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cd&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt&lt;/code&gt; và một trình soạn thảo văn bản. Ubuntu 24.04 (Noble) là nền tảng hỗ trợ bậc 1 (Tier-1) của Jazzy; một máy ảo hoặc máy tính mini cũ là đủ để thực hành. Lưu ý rằng ESP32 từ chương 02–08 nằm trên robot và không chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt;; máy tính này sẽ giao tiếp với ESP32 qua khung dữ liệu serial của chương 08 ở các bài sau. Ở bài này bạn chưa cần lidar hay Raspberry Pi.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Capstone A đã chứng minh khung xe vận hành ổn định bằng firmware. ROS 2 Jazzy đóng vai trò là lớp truyền thông và công cụ hỗ trợ đặt phía trên, không thay thế cơ chế watchdog bảo vệ động cơ trên vi điều khiển. Mọi câu lệnh từ chương 09 đến chương 12 đều giả định một bản ROS đã được nạp đúng cách. Nếu workspace bị lẫn lộn giữa Humble và Jazzy, bạn sẽ mất rất nhiều thời gian gỡ lỗi thay vì tập trung vào TF và cầu nối điều khiển. Phần mô phỏng ở chương 10 cũng sử dụng chính bản cài đặt này kết hợp với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-ros-gz&lt;/code&gt;. Trước khi chuyển sang Nav2 hay LeRobot, việc thao tác thành thạo với môi trường và lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; là bước nền tảng bắt buộc.&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;ROS 2 là tập hợp các thư viện và công cụ dòng lệnh xây dựng trên nền tảng truyền thông DDS. Mỗi &lt;strong&gt;distro&lt;/strong&gt; là một phiên bản phát hành cố định theo hệ điều hành: Jazzy Jalisco dành cho Ubuntu 24.04, còn Humble dành cho Ubuntu 22.04. Hai bản này không thể thay thế lẫn nhau, và việc nạp đồng thời cả hai trong cùng một shell sẽ làm hỏng môi trường làm việc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Underlay&lt;/strong&gt; là thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt; do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; cài đặt. &lt;strong&gt;Overlay&lt;/strong&gt; là workspace cá nhân mà bạn biên dịch bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt;, thường đặt tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws&lt;/code&gt;. Bạn luôn phải nạp underlay trước rồi mới nạp overlay; tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup.bash&lt;/code&gt; của overlay sẽ ghi nhớ underlay tại thời điểm nó được biên dịch. Khi mở một cửa sổ terminal mới, môi trường sẽ quay về trạng thái trống cho đến khi tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt; tự động nạp lại các thiết lập này.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2&lt;/code&gt; là công cụ dòng lệnh chính; lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;topic list&lt;/code&gt; giao tiếp với một tiến trình nền (daemon) làm nhiệm vụ khám phá các node trong mạng. Danh sách rỗng vẫn là kết quả hợp lệ (chỉ cho thấy chưa có topic nào đang hoạt động chứ không phải lỗi cài đặt), miễn là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 doctor&lt;/code&gt; hoặc ví dụ talker ở bài tiếp theo chạy bình thường.&lt;/p&gt;

&lt;p&gt;Hệ thống cần được thiết lập bảng mã UTF-8 trước khi thêm kho gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; của ROS. Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;locale&lt;/code&gt; đang là POSIX, quá trình cài đặt chính thức sẽ dừng sớm với thông báo lỗi rõ ràng, do đó hãy cấu hình dứt điểm một lần ngay từ đầu.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_workspace.png&quot; alt=&quot;Underlay rồi mới overlay&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Khóa học này không hỗ trợ gỡ lỗi trên Windows hay macOS. Nếu bạn chỉ có máy tính Windows, hãy cài Ubuntu 24.04 trong máy ảo (tối thiểu 4 GB RAM và 30 GB ổ cứng) hoặc dùng một máy tính phụ. WSL2 có thể chạy được nhưng thường phát sinh nhiều vấn đề ngoài phạm vi bài học (như đồ họa, truyền cổng serial USB, Gazebo). Cài trực tiếp Ubuntu là phương án khớp hoàn toàn với tài liệu thực hành.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-truy-vết-nhanh-3-biến-môi-trường-khi-ros2-pkg-list-không-thấy-gói-của-bạn&quot;&gt;Giải thích: Truy vết nhanh 3 biến môi trường khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg list&lt;/code&gt; không thấy gói của bạn&lt;/h3&gt;

&lt;p&gt;Khi gặp lỗi không tìm thấy gói hoặc xung đột giữa hai nhóm trong phòng lab, hãy kiểm tra đúng 3 dòng lệnh theo thứ tự:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo $AMENT_PREFIX_PATH&lt;/code&gt; → Nếu chỉ thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt; mà &lt;strong&gt;thiếu&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/home/.../ros2_ws/install/...&lt;/code&gt;, bạn quên chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source ~/ros2_ws/install/setup.bash&lt;/code&gt; trong cửa sổ terminal mới mở!&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo $ROS_DOMAIN_ID&lt;/code&gt; → Nếu máy tính và robot có hai số khác nhau (ví dụ máy tính là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, robot là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;17&lt;/code&gt;), hai bên sẽ hoàn toàn “điếc” trước topic của nhau vì DDS dùng cổng UDP khác nhau theo công thức \(\text{Port} = 7400 + 250 \times \text{DOMAIN\_ID}\).&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon build --packages-select my_pkg --symlink-install&lt;/code&gt; → Biên dịch riêng 1 gói thay vì build lại cả 20 gói trong workspace, tiết kiệm từ 3 phút xuống còn vài giây.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Kiểm tra phiên bản hệ điều hành và thiết lập ngôn ngữ trước khi cài ROS:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; /etc/os-release
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$VERSION_ID&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
locale
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VERSION_ID&lt;/code&gt; bắt buộc phải là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;24.04&lt;/code&gt;. Nếu máy trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;22.04&lt;/code&gt;, hãy dừng lại: hệ thống đó chỉ phù hợp với Humble hoặc cần cài lại Ubuntu 24.04, không nên ép cài Jazzy.&lt;/p&gt;

&lt;p&gt;Cấu hình UTF-8 theo hướng dẫn &lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation/Ubuntu-Install-Debs.html&quot;&gt;bản cài deb Ubuntu&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt update &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;locales
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;locale-gen en_US en_US.UTF-8
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;update-locale &lt;span class=&quot;nv&quot;&gt;LC_ALL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;en_US.UTF-8 &lt;span class=&quot;nv&quot;&gt;LANG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;en_US.UTF-8
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;LANG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;en_US.UTF-8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở một cửa sổ terminal mới để thiết lập locale có hiệu lực, sau đó thêm nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; và cài bản desktop. Vì lệnh cài gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2-apt-source&lt;/code&gt; có thể thay đổi khi trang chính thức cập nhật, hãy sao chép đúng khối lệnh từ trang cài đặt Jazzy tại thời điểm bạn thực hành. Quy trình tổng quát như sau:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;curl &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# làm đúng khối &quot;ros2-apt-source&quot; trên trang cài Jazzy&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt update
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt upgrade
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-desktop
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;python3-colcon-common-extensions python3-rosdep
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-desktop&lt;/code&gt; bao gồm RViz, các ví dụ mẫu và công cụ dòng lệnh. Gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-ros-base&lt;/code&gt; gọn nhẹ hơn, phù hợp cho máy tính trên robot không gắn màn hình. Với máy tính dùng để học, hãy cài bản desktop.&lt;/p&gt;

&lt;p&gt;Nạp môi trường và lưu vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bashrc&lt;/code&gt; để tự động kích hoạt mỗi khi mở terminal mới:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;source /opt/ros/jazzy/setup.bash&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.bashrc
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; ~/.bashrc
&lt;span class=&quot;nb&quot;&gt;printenv &lt;/span&gt;ROS_DISTRO
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Tạo workspace theo hướng dẫn &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-Client-Libraries/Creating-A-Workspace/Creating-A-Workspace.html&quot;&gt;bài tạo workspace&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; ~/ros2_ws/src
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~/ros2_ws
colcon build
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;source &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;HOME/ros2_ws/install/setup.bash&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.bashrc
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Dấu gạch chéo ngược trước &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME&lt;/code&gt; trong cặp ngoặc kép giúp giữ nguyên biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME&lt;/code&gt; thay vì ghi cứng một đường dẫn tĩnh vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt; (cả đường dẫn tuyệt đối lẫn biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME&lt;/code&gt; được thoát đúng cách đều hoạt động ổn định). Kiểm tra lại cuối tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;tail&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; 5 ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Dòng nạp underlay (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy/setup.bash&lt;/code&gt;) bắt buộc phải nằm &lt;strong&gt;phía trên&lt;/strong&gt; dòng nạp overlay (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2_ws/install/setup.bash&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Kiểm tra khả năng khám phá topic:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi&lt;/strong&gt; khi chưa chạy node nào khác:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/parameter_events
/rosout
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Một số bản cài đặt chỉ hiển thị hai topic mặc định trên, và daemon mới khởi động có thể trả về danh sách trống trong giây lát. Hãy chạy lại lệnh lần thứ hai, rồi kiểm tra thêm:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 pkg list | &lt;span class=&quot;nb&quot;&gt;head
&lt;/span&gt;which ros2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;which ros2&lt;/code&gt; phải trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy/bin/ros2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Unable to locate package ros-jazzy-desktop&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Không phải Ubuntu 24.04, hoặc bỏ qua bước thêm nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; của ROS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DISTRO&lt;/code&gt; bị trống ở terminal mới&lt;/td&gt;
      &lt;td&gt;Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt; không tự chạy; thêm dòng tương ứng vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.profile&lt;/code&gt; hoặc mở lại terminal từ menu hệ thống&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo $CMAKE_PREFIX_PATH&lt;/code&gt; chứa cả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Đã nạp lẫn hai bản ROS; đóng toàn bộ terminal và chỉ giữ lại một dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt; duy nhất&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon: command not found&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Chưa cài &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3-colcon-common-extensions&lt;/code&gt;, hoặc chạy build trước khi nạp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; bị treo&lt;/td&gt;
      &lt;td&gt;Do tường lửa hoặc daemon cũ bị kẹt; chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 daemon stop&lt;/code&gt; rồi thử lại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hết dung lượng đĩa khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt install&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Bản desktop cần vài gigabyte trống; giải phóng ổ đĩa hoặc chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-ros-base&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Kiểm tra tổng thể môi trường (tùy chọn):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 doctor
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trên laptop, một vài cảnh báo liên quan đến card mạng là bình thường, nhưng không được có lỗi liên quan đến phiên bản ROS distro.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Ở bài này bạn chưa cần mua phần cứng mới, trừ khi muốn chuẩn bị sớm máy tính độc lập cho robot thay vì dùng laptop. Một chiếc PC văn phòng cũ cài Ubuntu 24.04 là phương án tiết kiệm nhất để học. Khi cần gắn máy tính lên robot, khóa học chọn Raspberry Pi 5 (bản 4 GB đủ dùng cho Jazzy và camera; bản 8 GB thoải mái hơn khi chạy Nav2 về sau).&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Linh kiện&lt;/th&gt;
      &lt;th&gt;Nơi mua&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
      &lt;th&gt;Khoảng giá 2026&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Raspberry Pi 5&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Hshop, board sản xuất tại Anh&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Chọn dung lượng RAM trên trang; giá thay đổi theo phiên bản&lt;/td&gt;
      &lt;td&gt;Từ khoảng 2,4 triệu đồng cho bản RAM nhỏ; bản 4 GB và 8 GB cao hơn (cần kiểm tra lại giá hiện tại)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ kit Pi 5 (nguồn, vỏ, quạt, thẻ nhớ)&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/combo-raspberry-pi-5-ram-4-8gb-basic-kit&quot;&gt;Kit cơ bản trên Hshop&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Nguồn USB-C 27 W rất quan trọng vì sạc điện thoại thường dễ gây sụt áp&lt;/td&gt;
      &lt;td&gt;Cao hơn mua mạch trần&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thẻ microSD 64 GB chuẩn A2&lt;/td&gt;
      &lt;td&gt;Shopee/Lazada (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thẻ nhớ microSD 64GB A2&lt;/code&gt;)&lt;/td&gt;
      &lt;td&gt;Dùng cho Pi, không cần cho bài lab trên laptop&lt;/td&gt;
      &lt;td&gt;Khoảng 150.000–350.000 đồng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nếu Hshop hết hàng, bạn có thể tìm trên Shopee với từ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Raspberry Pi 5 4GB chính hãng&lt;/code&gt; và lưu ý tránh các tin đăng chỉ bán vỏ hộp (case). Trong kiến trúc hệ thống, ESP32 vẫn đảm nhiệm điều khiển động cơ, còn Pi sẽ đóng vai trò máy tính chạy ROS về sau.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=08o46x5SfJM&quot;&gt;How to Correctly Install ROS2 Jazzy Jalisco in Linux Ubuntu 24.04 (Noble) - ROS2 Tutorial&lt;/a&gt; — Aleksandar Haber PhD (tiếng Anh). Hướng dẫn cài đặt Jazzy trên Ubuntu 24.04; đối chiếu từng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; với bài học và không nạp thêm distro khác.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=CSsp7LcBD-Y&quot;&gt;ROS 2 Jazzy + Ubuntu 24.04: Complete Installation Guide!&lt;/a&gt; — Robotics Unboxed (tiếng Anh). Nội dung tương tự nhưng ngắn gọn hơn, hữu ích để kiểm tra thứ tự &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt; trước khi tạo workspace.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Dán kết quả lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;which ros2&lt;/code&gt; vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. &lt;em&gt;Gợi ý:&lt;/em&gt; kết quả phải hiển thị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt; và đường dẫn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy/...&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt; chỉ nạp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws/install/setup.bash&lt;/code&gt; mà chưa từng nạp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt; thì vấn đề gì sẽ xảy ra? &lt;em&gt;Gợi ý:&lt;/em&gt; tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup.bash&lt;/code&gt; của overlay liên kết với underlay tại thời điểm biên dịch; nếu biên dịch khi chưa nạp Jazzy, chuỗi liên kết sẽ rỗng và hệ thống không tìm thấy lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2&lt;/code&gt;. Cách sửa là nạp underlay, biên dịch lại rồi mới nạp overlay.&lt;/li&gt;
  &lt;li&gt;Một người bạn đang dùng Ubuntu 22.04 muốn sao chép bộ lệnh cài đặt của bạn. Bạn sẽ khuyên họ thế nào? &lt;em&gt;Gợi ý:&lt;/em&gt; các lệnh này dành riêng cho Ubuntu 24.04 và ROS 2 Jazzy; Ubuntu 22.04 phải dùng ROS 2 Humble, không cài lẫn.&lt;/li&gt;
  &lt;li&gt;Vì sao mạch ESP32 không “nằm trong workspace” này? &lt;em&gt;Gợi ý:&lt;/em&gt; workspace là cây thư mục biên dịch của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt; trên Linux, còn vi điều khiển chạy firmware từ chương 08. Đến chương 10 chúng ta mới kết nối hai bên qua cầu nối serial hoặc micro-ROS.&lt;/li&gt;
  &lt;li&gt;Sau khi khởi động lại máy, terminal đầu tiên mở lên không nhận diện được lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2&lt;/code&gt;. Bạn cần kiểm tra tệp nào? &lt;em&gt;Gợi ý:&lt;/em&gt; kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bashrc&lt;/code&gt; và xác minh xem terminal có tự động đọc tệp này khi mở hay không.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation/Ubuntu-Install-Debs.html&quot;&gt;Gói deb Ubuntu cho Jazzy&lt;/a&gt; — sử dụng khối lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2-apt-source&lt;/code&gt; trên trang này thay vì dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sources.list&lt;/code&gt; cũ trên các bài blog.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-Client-Libraries/Creating-A-Workspace/Creating-A-Workspace.html&quot;&gt;Tạo workspace&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-2000.html&quot;&gt;REP-2000&lt;/a&gt; — bảng tương thích giữa các phiên bản ROS 2 và Ubuntu.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://colcon.readthedocs.io/en/released/user/quick-start.html&quot;&gt;Tài liệu colcon&lt;/a&gt; — tham khảo tùy chọn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon build --symlink-install&lt;/code&gt;, rất hữu ích khi làm việc với các gói Python.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Linux trên máy tính của robot</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_00_linux_on_sbc/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter09/09_00_linux_on_sbc</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;~150 phút&lt;/strong&gt; nếu bạn đã có sẵn thẻ nhớ và mạch SBC; khoảng 40 phút nếu bạn chỉ ghi chú cấu hình cho laptop Ubuntu 24.04 và tạm hoãn phần SBC.&lt;/p&gt;

&lt;p&gt;Khi chuyển từ vi điều khiển (ESP32/Arduino) lên máy tính nhúng một bo mạch (SBC như Raspberry Pi 4/5 hay Jetson), bạn không còn nạp một vòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt; duy nhất nữa mà đang quản trị cả một hệ điều hành &lt;strong&gt;Linux đa nhiệm&lt;/strong&gt;. Nắm vững hệ thống tệp thiết bị (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev&lt;/code&gt;), quyền truy cập cổng nối tiếp, quản lý tiến trình và SSH từ xa là điều kiện tiên quyết để không bị kẹt hàng giờ vì lỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Permission denied&lt;/code&gt; khi chạy ROS 2.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-trụ-cột-linux-trên-máy-tính-nhúng-sbc-cho-robotics&quot;&gt;Kiến thức cần nhớ: Bốn trụ cột Linux trên Máy tính Nhúng (SBC) cho Robotics&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Hệ thống tệp &amp;amp; Thiết bị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev&lt;/code&gt; (Everything is a File)&lt;/strong&gt;:
Cảm biến LiDAR, camera và vi điều khiển cắm qua USB đều hiện ra thành tệp thiết bị (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyACM0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video0&lt;/code&gt;). Dùng &lt;strong&gt;quy tắc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt;&lt;/strong&gt; gán tên cố định (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/robot_mcu&lt;/code&gt;) giúp không bị tráo cổng mỗi lần khởi động lại.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quyền người dùng &amp;amp; Nhóm phần cứng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;video&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gpio&lt;/code&gt;)&lt;/strong&gt;:
Thay vì chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo&lt;/code&gt; bừa bãi cho toàn bộ ROS 2 (gây lỗi biến môi trường và bảo mật), ta thêm tài khoản vào đúng nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; để đọc/ghi serial an toàn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mạng SSH &amp;amp; Quản lý dịch vụ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd&lt;/code&gt;)&lt;/strong&gt;:
Robot di động không kéo theo màn hình HDMI. Quản trị qua SSH, gán IP tĩnh/mDNS (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.local&lt;/code&gt;) và tự động khởi chạy node bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd&lt;/code&gt; là quy chuẩn vận hành ngoài hiện trường.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tài nguyên phần cứng &amp;amp; Giám sát hiệu năng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;htop&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;free&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vcgencmd&lt;/code&gt;)&lt;/strong&gt;:
Theo dõi nhiệt độ CPU (tránh thermal throttling), bộ nhớ RAM và độ bền thẻ nhớ SD khi ghi rosbag.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ cài đặt được Ubuntu 24.04 không giao diện (headless) lên Raspberry Pi 5, hoặc kiểm tra được phiên bản Ubuntu trên Jetson Orin Nano trước khi chọn bản phân phối (distro) ROS phù hợp. Bạn sẽ kết nối SSH vào mạch, cấu hình luật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt; để gán cố định cổng ESP32 thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/esp32&lt;/code&gt;, viết một service &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd&lt;/code&gt; cho micro-ROS agent và thiết lập cùng một giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; trên cả laptop lẫn robot. Ngoài ra, bạn sẽ hiểu rõ vì sao trên macOS cần dùng máy ảo có tính năng truyền thiết bị USB (USB passthrough) khi agent cần truy cập trực tiếp cổng serial.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Bạn cần sử dụng thành thạo terminal trên Ubuntu với các lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemctl&lt;/code&gt;. Bài cài đặt ROS 2 Jazzy trên laptop (09-01) có thể thực hiện song song. Lưu ý rằng bài này tập trung vào máy tính đặt &lt;strong&gt;trên xe&lt;/strong&gt;, không phải lần cài đặt đầu tiên trên laptop. Mạch ESP32 từ chương 02 cần đã từng được nhận diện trên máy dưới dạng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyACM0&lt;/code&gt;. Ở bước này bạn chưa cần đến Nav2.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclcpp&lt;/code&gt; là hai thư viện chính của khóa học, chạy trên Ubuntu 24.04 vì các gói ROS 2 Jazzy được biên dịch sẵn cho phiên bản hệ điều hành này. Laptop phù hợp để học lệnh và thử nghiệm ban đầu, còn Raspberry Pi 5 hoặc Jetson Orin Nano giúp robot vận hành độc lập mà không phải mang theo laptop sau khi cây tọa độ TF đã ổn định.&lt;/p&gt;

&lt;p&gt;Trên Linux, tên cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; có thể thay đổi mỗi khi bạn cắm thêm bộ chuyển đổi USB-UART khác; nếu micro-ROS agent ghi cứng đường dẫn đó, hệ thống sẽ mất kết nối ngay khi cần trình diễn. Bên cạnh đó, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclgo&lt;/code&gt; chỉ là tài liệu đọc thêm dành cho người đã quen lập trình Go, không thay thế các bài học &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclcpp&lt;/code&gt; và không nằm trong danh sách kiểm tra của chương này.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_sbc_network.png&quot; alt=&quot;Laptop và SBC cùng domain, ESP32 chỉ là serial&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Raspberry Pi 5.&lt;/strong&gt; Khóa học này yêu cầu bản cài đặt Ubuntu 24.04 LTS (Noble) 64-bit, không dùng phiên bản mới hơn ngay cả khi trang tải gợi ý Ubuntu 26.04, vì ROS 2 Jazzy gắn liền với Ubuntu 24.04. Trong khi đó, Raspberry Pi OS dựa trên Debian nên không phù hợp để cài trực tiếp các gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-*&lt;/code&gt; (dễ phát sinh lỗi phụ thuộc kéo dài). Cách ghi thẻ nhớ thuận tiện nhất là dùng Raspberry Pi Imager, chọn Ubuntu Server 24.04 cho Pi 5, bật sẵn SSH và tạo tài khoản người dùng. Bạn có thể xem hướng dẫn tại &lt;a href=&quot;https://ubuntu.com/tutorials/how-to-install-ubuntu-on-your-raspberry-pi&quot;&gt;Ubuntu trên Raspberry Pi&lt;/a&gt;, trang &lt;a href=&quot;https://ubuntu.com/download/raspberry-pi&quot;&gt;tải ảnh&lt;/a&gt; và danh sách tệp cài đặt 24.04 tại &lt;a href=&quot;https://cdimage.ubuntu.com/ubuntu/releases/24.04/release/&quot;&gt;cdimage 24.04&lt;/a&gt;. Nếu Raspberry Pi Imager tự động chọn mặc định bản 26.04, hãy chuyển lại về 24.04. Hướng dẫn cài Jazzy trên Pi của ROS có tại &lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Installing-on-Raspberry-Pi.html&quot;&gt;Installing on Raspberry Pi&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Cài đặt headless nghĩa là không cần gắn màn hình. Trong lần kết nối đầu tiên khi Pi và laptop ở cùng mạng LAN, bạn truy cập bằng lệnh:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ssh ubuntu@&amp;lt;địa-chỉ-ip&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tên miền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.local&lt;/code&gt; chỉ hoạt động nếu dịch vụ mDNS đang chạy; nếu không phân giải được, hãy tra địa chỉ IP trong bảng DHCP của router thay vì đoán tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;raspberrypi.local&lt;/code&gt; trên bản Ubuntu. Tên người dùng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ubuntu&lt;/code&gt; trong ví dụ trên) phải khớp với tên bạn đã khai báo trong Raspberry Pi Imager.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Jetson Orin Nano.&lt;/strong&gt; Hai thế hệ phần mềm JetPack sử dụng hai phiên bản Ubuntu khác nhau nên không dùng chung một quy trình cài đặt ROS:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;JetPack 6.2 sử dụng rootfs Ubuntu 22.04, khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsb_release -sc&lt;/code&gt; sẽ trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jammy&lt;/code&gt;. Các gói cài qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; của khóa học (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-*&lt;/code&gt;) không hỗ trợ Jammy; bản ROS tương ứng với Ubuntu 22.04 là Humble, không được cài lẫn hai bản này.&lt;/li&gt;
  &lt;li&gt;JetPack 7.2 trên Orin Nano sử dụng Ubuntu 24.04. Vào tháng 9 năm 2026, kỹ sư NVIDIA trên diễn đàn phát triển đã xác nhận Jazzy là bản ROS phù hợp với JetPack 7.2.1. Bạn nên kiểm tra lại chủ đề &lt;a href=&quot;https://forums.developer.nvidia.com/t/is-ros2-jazzy-the-correct-version-to-use-with-jetpack-7-2-1-ubuntu-24-04/382439&quot;&gt;Jazzy với JetPack 7.2.1&lt;/a&gt; khi nạp hệ điều hành vì số phiên bản vá có thể thay đổi.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trước khi chạy bất kỳ lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt install ros-&lt;/code&gt; nào, bắt buộc kiểm tra phiên bản hệ điều hành:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;lsb_release &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu kết quả là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noble&lt;/code&gt;, bạn có thể tiếp tục cài Jazzy. Nếu là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jammy&lt;/code&gt;, hãy dừng lại, ghi chú vào sổ thực hành, sau đó chọn giữ nguyên Humble trên mạch đó hoặc nạp lại JetPack 7 theo hướng dẫn &lt;a href=&quot;https://docs.nvidia.com/jetson/orin-nano-devkit/user-guide/latest/quick_start.html&quot;&gt;quick start Orin Nano&lt;/a&gt; và &lt;a href=&quot;https://docs.nvidia.com/jetson/jetpack/install-setup/index.html&quot;&gt;cài JetPack&lt;/a&gt;. Các container Docker trong &lt;a href=&quot;https://github.com/dusty-nv/jetson-containers&quot;&gt;dusty-nv/jetson-containers&lt;/a&gt; là một phương án thay thế khi cần dùng CUDA, nhưng không bắt buộc trong tuần học này.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt;.&lt;/strong&gt; Mạch ESP32 dùng chip cầu USB-UART CH340 thường có mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1a86:7523&lt;/code&gt;, còn CP2102 thường là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10c4:ea60&lt;/code&gt;. Tuy nhiên, bạn cần kiểm tra trực tiếp trên máy của mình thay vì sao chép cứng theo ví dụ:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;udevadm info &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; /dev/ttyUSB0 | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-E&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;idVendor|idProduct&apos;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;head&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tạo tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/udev/rules.d/99-esp32.rules&lt;/code&gt; (thay đổi mã vendor và product theo kết quả vừa đo):&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SUBSYSTEM==&quot;tty&quot;, ATTRS{idVendor}==&quot;1a86&quot;, ATTRS{idProduct}==&quot;7523&quot;, SYMLINK+=&quot;esp32&quot;, GROUP=&quot;dialout&quot;, MODE=&quot;0660&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Sau đó, thêm người dùng vào nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;, nạp lại luật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt; và kiểm tra liên kết mềm (symlink):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;usermod &lt;span class=&quot;nt&quot;&gt;-aG&lt;/span&gt; dialout &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$USER&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;udevadm control &lt;span class=&quot;nt&quot;&gt;--reload-rules&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;udevadm trigger
&lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; /dev/esp32
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bạn cần đăng xuất khỏi phiên SSH rồi đăng nhập lại để quyền nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; có hiệu lực. Đường dẫn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/esp32&lt;/code&gt; phải hiển thị là một symlink trỏ tới cổng serial thực. Tránh dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chmod 777&lt;/code&gt; như một số tài liệu hướng dẫn lidar vì cách đó mở quyền quá rộng.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd&lt;/code&gt; và micro-ROS agent.&lt;/strong&gt; Micro-ROS agent đóng vai trò trung gian: giao tiếp qua cổng serial với firmware trên ESP32 và truyền nhận dữ liệu DDS với laptop. Tệp thực thi có tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;micro_ros_agent&lt;/code&gt;, nguồn tại &lt;a href=&quot;https://github.com/micro-ROS/micro-ROS-Agent&quot;&gt;micro-ROS-Agent&lt;/a&gt;. Trên máy đã cài Jazzy, hãy thử tìm gói bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt-cache search ros-jazzy-micro-ros-agent&lt;/code&gt;; nếu có sẵn thì cài trực tiếp, còn nếu không thì biên dịch từ mã nguồn theo hướng dẫn README của kho lưu trữ đó rồi cập nhật lại đường dẫn trong dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecStart&lt;/code&gt;. Tạo tệp unit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/systemd/system/microros-agent.service&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-ini highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;[Unit]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;Description&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;micro-ROS agent on /dev/esp32&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;After&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;network-online.target&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;Wants&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;network-online.target&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;[Service]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;simple&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ubuntu&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;Environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ROS_DOMAIN_ID=42&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;ExecStart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/bin/bash -lc &apos;source /opt/ros/jazzy/setup.bash &amp;amp;&amp;amp; ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/esp32 -b 115200&apos;&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;Restart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;on-failure&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;RestartSec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;[Install]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;WantedBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;multi-user.target&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User=&lt;/code&gt; phải là tài khoản đã thuộc nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;, và tốc độ baud &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200&lt;/code&gt; phải khớp với firmware trên ESP32. Nếu agent do bạn tự biên dịch không nằm trong workspace overlay mà &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run&lt;/code&gt; nhận diện được, hãy gọi trực tiếp đường dẫn đến tệp nhị phân trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecStart&lt;/code&gt; thay vì dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl daemon-reload
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl &lt;span class=&quot;nb&quot;&gt;enable&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--now&lt;/span&gt; microros-agent.service
systemctl status microros-agent.service &lt;span class=&quot;nt&quot;&gt;--no-pager&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Mạng và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt;.&lt;/strong&gt; Bạn có thể xem dải giá trị hợp lệ của domain ID tại &lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Domain-ID.html&quot;&gt;Tài liệu domain ID của Jazzy&lt;/a&gt;. Trong môi trường phòng thí nghiệm có nhiều người cùng thực hành, khóa học dùng một giá trị khác 0 (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;42&lt;/code&gt;), đặt giống hệt nhau trên cả laptop và SBC:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-q&lt;/span&gt; ROS_DOMAIN_ID ~/.bashrc &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;export ROS_DOMAIN_ID=42&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hai thiết bị cần nằm cùng lớp mạng (subnet) và điểm truy cập Wi-Fi không được chặn gói tin multicast (một số mạng Wi-Fi khách bật tính năng cô lập thiết bị client). Nếu hai thiết bị dùng khác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt;, đồ thị ROS của hai máy sẽ tách biệt và không nhìn thấy nhau; khi đó việc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; trả về rỗng là vấn đề mạng/domain chứ không phải lỗi cài đặt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker trên robot.&lt;/strong&gt; Ảnh Docker chính thức của ROS nằm tại [hub.docker.com/&lt;em&gt;/ros](https://hub.docker.com/&lt;/em&gt;/ros). Hãy kiểm tra thẻ (tag) Jazzy hiện có trên trang đó trước khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker pull&lt;/code&gt;. Cài đặt Docker theo &lt;a href=&quot;https://docs.docker.com/engine/install/ubuntu/&quot;&gt;tài liệu Docker cho Ubuntu&lt;/a&gt;. Để container truy cập được mạng DDS của máy chủ và cổng serial:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run &lt;span class=&quot;nt&quot;&gt;--rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--network&lt;/span&gt; host &lt;span class=&quot;nt&quot;&gt;--device&lt;/span&gt; /dev/esp32 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ROS_DOMAIN_ID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;42 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  ros:jazzy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hãy thay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros:jazzy&lt;/code&gt; nếu thẻ trên Docker Hub có hậu tố khác. Tùy chọn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--network host&lt;/code&gt; trên Linux cho phép container dùng chung ngăn xếp mạng của máy chủ để quá trình khám phá DDS không bị cản trở bởi NAT. Xem thêm hướng dẫn chạy node trong Docker tại &lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Run-2-nodes-in-single-or-separate-docker-containers.html&quot;&gt;Run 2 nodes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Người dùng macOS.&lt;/strong&gt; Docker Desktop trên macOS không hỗ trợ truyền trực tiếp thiết bị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; của Linux vào trong container. Do đó, khi làm bài có kết nối ESP32, bạn cần cài một máy ảo Ubuntu 24.04 (dùng UTM, VMware hoặc Parallels) có hỗ trợ USB passthrough, rồi cài ROS 2 Jazzy bên trong máy ảo như hướng dẫn ở bài 09-01. Docker trên Mac vẫn phù hợp để thực hành dòng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2&lt;/code&gt; không dùng phần cứng; còn chạy Gazebo có giao diện đồ họa trong Docker trên Mac là một bài toán phức tạp hơn. Tóm lại, dùng macOS không cản trở chương 09, chỉ phát sinh giới hạn khi cần giao tiếp serial mà bạn chỉ có Docker Desktop.&lt;/p&gt;

&lt;p&gt;Ngoài ra, kho lưu trữ &lt;a href=&quot;https://github.com/tiiuae/rclgo&quot;&gt;tiiuae/rclgo&lt;/a&gt; cung cấp thư viện gắn kết (binding) cho ngôn ngữ Go. Bạn chỉ nên tham khảo sau khi đã chạy được node talker bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt;; nội dung này không làm thay đổi danh sách kiểm tra của bài.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-băng-thông-thẻ-nhớ-khi-ghi-rosbag-trên-raspberry-pi&quot;&gt;Giải thích: Tính tay băng thông thẻ nhớ khi ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rosbag&lt;/code&gt; trên Raspberry Pi&lt;/h3&gt;

&lt;p&gt;Giả sử SBC gắn camera RGB độ phân giải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;640 × 480&lt;/code&gt;, 3 kênh màu (RGB, 3 byte/pixel) chạy ở &lt;strong&gt;30 khung hình/giây&lt;/strong&gt; chưa nén (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Image&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Dung lượng 1 khung hình thô: \(640 \times 480 \times 3 = 921.600\text{ byte} \approx \mathbf{0{,}92\text{ MB}}.\)&lt;/li&gt;
  &lt;li&gt;Lưu lượng ghi mỗi giây: \(0{,}92 \times 30 \approx \mathbf{27{,}6\text{ MB/s}}.\)&lt;/li&gt;
  &lt;li&gt;Sau &lt;strong&gt;5 phút&lt;/strong&gt; (300 giây) quay thử nghiệm: \(27{,}6 \times 300 \approx \mathbf{8{,}28\text{ GB}}\)!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Một thẻ nhớ microSD phổ thông (tốc độ ghi duy trì ~20 MB/s) sẽ bị &lt;strong&gt;nghẽn I/O ngay lập tức&lt;/strong&gt; và làm rớt khung hình toàn hệ thống. Giải pháp Linux/ROS 2 chuẩn: nén sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image_transport/compressed&lt;/code&gt; (JPEG giảm ~\(10\times\) xuống còn ~2,8 MB/s) hoặc gắn ổ SSD NVMe!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Trong bài thực hành này, tuyệt đối chưa nối SBC trên xe với nguồn động cơ (VM). Cấp nguồn 5 V cho Raspberry Pi hoặc nguồn cho Jetson bằng đúng bộ nguồn và giắc cắm của hãng; không lấy trực tiếp điện áp từ cực pin 2S để cấp vào cổng 5 V của mạch.&lt;/p&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Ghi rõ máy chủ sẽ chạy Jazzy của bạn (laptop, Pi 5 hay Jetson) và dán kết quả lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsb_release -sc&lt;/code&gt;. Nếu là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noble&lt;/code&gt; thì tiếp tục; nếu là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jammy&lt;/code&gt; trên Jetson thì ghi nhận rõ và không cài Jazzy.&lt;/li&gt;
  &lt;li&gt;Nếu sử dụng Pi 5: nạp Ubuntu Server 24.04, kết nối qua SSH và lưu địa chỉ IP vào sổ thực hành.&lt;/li&gt;
  &lt;li&gt;Cắm ESP32, tạo luật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt; và dán kết quả lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls -l /dev/esp32&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Tạo và chạy unit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd&lt;/code&gt; cho micro-ROS agent. Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemctl status&lt;/code&gt; cần báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;active&lt;/code&gt;; hoặc nếu máy chưa có sẵn tệp nhị phân agent, hãy ghi lại thông báo lỗi cụ thể cùng kế hoạch biên dịch từ kho lưu trữ micro-ROS. Một unit có sai đường dẫn nhưng đã được chạy thử và đọc log vẫn đạt yêu cầu bước ghi chép; ngược lại, unit chưa từng được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;start&lt;/code&gt; thì chưa đạt.&lt;/li&gt;
  &lt;li&gt;Thiết lập cùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; trên cả hai máy. Từ laptop (sau khi source Jazzy), chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; để kiểm tra topic của agent, &lt;strong&gt;hoặc&lt;/strong&gt; ghi nhận rõ trường hợp agent chưa có client kết nối (danh sách chưa hiện topic micro-ROS, nhưng giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; in ra trên cả hai máy giống nhau).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; cần ghi đủ các thông tin: phiên bản Ubuntu, địa chỉ IP, đường dẫn symlink &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/esp32&lt;/code&gt; (hoặc lý do chưa có mạch), trạng thái dịch vụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd&lt;/code&gt; và hai dòng giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; trên hai máy.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân cần kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2&lt;/code&gt; không thấy node trên SBC&lt;/td&gt;
      &lt;td&gt;Khác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt;, khác subnet hoặc Wi-Fi bật chế độ cô lập client&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/esp32&lt;/code&gt; biến mất sau khi rút cắm lại&lt;/td&gt;
      &lt;td&gt;Luật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt; không khớp mã vendor/product; chạy lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udevadm info&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Agent tự khởi động lại liên tục&lt;/td&gt;
      &lt;td&gt;Sai tốc độ baud, user chưa thuộc nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;, hoặc tệp nhị phân không có trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PATH&lt;/code&gt; của unit&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt install ros-jazzy-desktop&lt;/code&gt; báo lỗi trên Jetson&lt;/td&gt;
      &lt;td&gt;Hệ điều hành đang là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jammy&lt;/code&gt;; dừng cài đặt Jazzy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Docker trên Mac không nhận USB&lt;/td&gt;
      &lt;td&gt;Giới hạn của Docker Desktop trên macOS; chuyển agent sang máy ảo hoặc Pi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pi Imager nạp nhầm Ubuntu 26.04&lt;/td&gt;
      &lt;td&gt;ROS 2 Jazzy không hỗ trợ bản này; cần ghi lại thẻ nhớ với Ubuntu 24.04&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=fgonBE4KWGw&quot;&gt;Ubuntu 24.04 on the Raspberry Pi - How to, Setup, and Demo&lt;/a&gt; — Gary Explains (tiếng Anh). Hướng dẫn cài Ubuntu 24.04 trên Raspberry Pi, khớp với phiên bản khóa học yêu cầu; nếu trình cài đặt đề xuất 26.04 thì chọn lại 24.04 như trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=BOxWrMNXKpU&quot;&gt;Udev Rules Linux: Ensure USB (and other) Devices Always Keep Same Port&lt;/a&gt; — Practical Robotics (tiếng Anh). Cách dùng luật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt; để giữ cố định tên cổng USB (tạo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/esp32&lt;/code&gt; độc lập với thứ tự cắm).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=N1vgvhiyq0E&quot;&gt;systemd on Linux 1: Intro and Unit Files&lt;/a&gt; — tutoriaLinux (tiếng Anh). Giới thiệu tệp unit của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd&lt;/code&gt;, mô hình dùng để cấu hình dịch vụ micro-ROS agent trong bài.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Vì sao nên dùng luật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SYMLINK&lt;/code&gt; thay vì viết một script chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chmod&lt;/code&gt; mỗi lần khởi động máy?&lt;/li&gt;
  &lt;li&gt;Nếu đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User=root&lt;/code&gt; trong tệp unit cho tiện, bạn bỏ qua cơ chế phân quyền nhóm nào và phát sinh thêm rủi ro gì?&lt;/li&gt;
  &lt;li&gt;Laptop dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID=42&lt;/code&gt;, Pi dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID=7&lt;/code&gt; trong cùng mạng Wi-Fi khiến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; trên laptop trả về trống. Cần sửa biến môi trường nào?&lt;/li&gt;
  &lt;li&gt;Nếu thẻ nhớ trên Pi đang cài Ubuntu 26.04, bạn có thể làm theo nguyên văn bài 09-01 được không?&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclgo&lt;/code&gt; hỗ trợ được bước nào trong bài lab này và không giải quyết được những bước nào?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Luật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt; gán tên cổng ổn định theo mã vendor/product và cấp quyền gọn qua nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;, không cần chạy lệnh thủ công sau mỗi lần rút cắm.&lt;/li&gt;
  &lt;li&gt;Dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt; bỏ qua cơ chế phân quyền của nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; một cách thiếu an toàn và khiến mọi lỗi của agent đều chạy dưới đặc quyền quản trị cao nhất; nên giữ tài khoản thường thuộc nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Đặt biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; trên cả hai máy về cùng một giá trị.&lt;/li&gt;
  &lt;li&gt;Không. Bài 09-01 cài ROS 2 Jazzy dành cho Ubuntu 24.04; bạn cần nạp lại thẻ nhớ hoặc chấp nhận rời khỏi lộ trình cài qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclgo&lt;/code&gt; không giải quyết việc cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udev&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;systemd&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt;; nó chỉ là một lựa chọn ngôn ngữ để viết node sau khi hệ thống DDS đã hoạt động. Lộ trình chính của khóa vẫn là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclcpp&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation/Ubuntu-Install-Debs.html&quot;&gt;Cài Jazzy bằng deb trên Ubuntu&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Installing-on-Raspberry-Pi.html&quot;&gt;Jazzy trên Raspberry Pi&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Domain-ID.html&quot;&gt;Domain ID&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/micro-ROS/micro-ROS-Agent&quot;&gt;micro-ROS Agent&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html&quot;&gt;systemd.service&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nvidia.com/jetson/orin-nano-devkit/user-guide/latest/quick_start.html&quot;&gt;Quick start Jetson Orin Nano&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 08</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_06_ch08_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_06_ch08_lab_checklist</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~60 phút&lt;/strong&gt;. Đây là bài tổng kết nghiệm thu (cổng qua chương), không giới thiệu giao thức mới. Bạn chỉ nên chuyển sang Chương 09 khi cả năm hạng mục dưới đây đều có bằng chứng cụ thể, hoặc đã chỉ rõ lỗi cần khắc phục mà không bỏ qua.&lt;/p&gt;

&lt;p&gt;Trong kỹ thuật robot, sự khác biệt giữa “chạy thử trên bàn thấy quay” và “nền tảng sẵn sàng cho ROS 2” nằm ở &lt;strong&gt;kiểm thử có định lượng&lt;/strong&gt;. Trước khi bàn giao khung xe cấp thấp lên tầng phần mềm ROS 2 ở Chương 09, bạn cần một danh sách kiểm tra nghiêm ngặt xác nhận từng mắt xích: giao tiếp serial, encoder, PID, động học vi sai và cơ chế watchdog.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-nghiệm-thu-nền-tảng-cấp-thấp-chương-08&quot;&gt;Kiến thức cần nhớ: Ba trụ cột nghiệm thu Nền tảng Cấp thấp (Chương 08)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Hợp đồng giao tiếp &amp;amp; An toàn phần cứng&lt;/strong&gt;:
Mọi gói tin vận tốc giữa MCU và máy tính chủ phải có định dạng rõ ràng, kiểm tra checksum/biên giá trị và kích hoạt Watchdog dừng động cơ khi mất tín hiệu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hiệu chuẩn Động học &amp;amp; Độ lặp lại&lt;/strong&gt;:
Đo sai số thực tế trên quãng đường thẳng (ví dụ chạy 1,00 m) và góc quay tại chỗ (quay 360°) để tinh chỉnh bán kính bánh \(r\) và khổ vết \(L\) trước khi dùng odometry cho SLAM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sổ nhật ký kỹ thuật tái lập được (Reproducible Lab Log)&lt;/strong&gt;:
Ghi lại hệ số PID, tần số vòng lặp, jitter p99 và các ngưỡng giới hạn tốc độ để mọi thành viên trong nhóm đều tái hiện được đúng hành vi xe.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Bạn tự đánh giá sổ tay Chương 08 theo năm tiêu chí: toán biến đổi TF chạy kiểm chứng thành công, khung truyền serial có timeout an toàn, vòng PID có tệp log CSV, động học vi sai có khổ vết đo thực tế, và vòng điều khiển có số đo chu kỳ định thời. Mỗi tiêu chí chỉ chấm đạt hoặc trượt — mọi ô để trống đều tính là trượt.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Toàn bộ kết quả từ Bài 08-00 đến Bài 08-05. Bạn cần chuẩn bị sẵn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;math_tf_lab.py&lt;/code&gt;, dữ liệu CSV của PID (nếu đã có encoder) và số đo khổ vết $L$ bằng thước. Nếu chưa có phần cứng encoder, tiêu chí PID trên xe thật có thể được ghi hoãn có ngày kèm kết quả mô phỏng, tuyệt đối không thay thế bằng nhận xét cảm tính “bánh quay thấy đều”.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Chương 09 sẽ ánh xạ $v$ và $\omega$ sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; của ROS 2, dựa trên giả định rằng quy ước dấu hai bánh và đơn vị mét đã chuẩn xác từ chương này. Chẳng hạn, ghi nhầm quaternion cho yaw $90^\circ$ thành $(0, 0, 1, 0)$ sẽ xoay ngược khung LiDAR tới $180^\circ$. Hoàn tất nghiệm thu trước khi mở workspace ROS 2 Jazzy giúp bạn tránh nhầm lẫn giữa lỗi toán hình học và lỗi truyền thông DDS.&lt;/p&gt;

&lt;h2 id=&quot;đạt-và-trượt&quot;&gt;Đạt và trượt&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Toán TF:&lt;/strong&gt; Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;math_tf_lab.py&lt;/code&gt; chạy không lỗi, in ra quaternion xấp xỉ $(0,\ 0,\ 0{,}7071,\ 0{,}7071)$ và tọa độ điểm $(0{,}30,\ 0)$ khi LiDAR đặt tại $x = 0{,}10$ m với điểm quét cách $0{,}20$ m. Thiếu kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;assert&lt;/code&gt; hoặc thiếu trường hợp yaw $\pi$ tính là trượt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Giao thức serial:&lt;/strong&gt; Nhật ký ghi nhận rõ trường hợp bên nhận bỏ qua khung lỗi/quá hạn và đưa PWM về 0. Việc chỉ dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println&lt;/code&gt; in ra Serial Monitor cho dễ đọc không đạt chuẩn giao thức.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Vòng điều khiển PID:&lt;/strong&gt; Tệp CSV lưu đủ cột thời gian &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt;, setpoint, tốc độ đo và tín hiệu lệnh. Khi kê bánh trên không, lệnh phải dương lúc tốc độ thấp hơn setpoint. Nếu chưa có encoder thật, bắt buộc có dòng ghi hoãn kèm ngày thực hiện, đồng thời chạy thành công đoạn mã mô phỏng và dán kết quả đỉnh bộ P xấp xỉ $0{,}29$ m/s; thiếu cả CSV lẫn mô phỏng là trượt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Động học vi sai:&lt;/strong&gt; Khổ vết $L$ được đo ba lần giữa hai tâm vết tiếp xúc và lưu theo đơn vị mét. Hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_speeds&lt;/code&gt; chuyển đổi một lệnh thử thành tốc độ hai bánh rồi tính ngược lại khớp đúng lệnh ban đầu. Dùng chiều ngang tấm mica làm $L$ tính là trượt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Định thời vòng lặp:&lt;/strong&gt; Có số đo chu kỳ thực tế (p50, p95, max), hoặc có cấu trúc định thời rõ ràng chứng minh vòng PID không chạy tùy tiện theo nhịp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt; ngẫu nhiên. Thiếu cả số đo lẫn cấu trúc định thời là trượt.&lt;/p&gt;

&lt;h2 id=&quot;một-sổ-trông-xong-mà-vẫn-trượt&quot;&gt;Một sổ trông xong mà vẫn trượt&lt;/h2&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;PID: bánh quay ổn, không cần CSV.
L = 16 cm. Hàm dùng 16 vì thước ghi vậy.
Quaternion: z = 1 cho góc vuông, cho dễ nhớ.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Cả ba dòng trên đều trượt: “quay ổn” không phải bằng chứng đo đạc; nhập $16$ vào công thức đơn vị mét tương đương khổ vết $16$ m; và $z = 1$ (với $w = 0$) là góc quay $180^\circ$ chứ không phải $90^\circ$.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-bài-kiểm-tra-hiệu-chuẩn-r-và-l-bằng-đúng-2-phép-đo-thước-dây&quot;&gt;Giải thích: Bài kiểm tra hiệu chuẩn \(r\) và \(L\) bằng đúng 2 phép đo thước dây&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Hiệu chuẩn bán kính bánh \(r\) (chạy thẳng 1,00 m):&lt;/strong&gt; Ra lệnh xe chạy thẳng đúng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1,000 m&lt;/code&gt; theo odometry, rồi dùng thước đo quãng đường thực tế trên sàn. Nếu xe thực đi được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,960 m&lt;/code&gt; (hụt 4%), ta chỉnh bán kính hiệu dụng:
\(r_{\text{mới}} = r_{\text{cũ}} \times \frac{0{,}960}{1{,}000} = 0{,}96 \cdot r_{\text{cũ}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hiệu chuẩn khổ vết \(L\) (xoay tại chỗ đúng 1 vòng \(360^\circ\)):&lt;/strong&gt; Ra lệnh xoay tại chỗ \(2\pi\) rad, quan sát góc thực tế xe xoay được. Nếu xe chỉ xoay được \(342^\circ\) (hụt 5%), nghĩa là \(L\) thực tế đang lớn hơn \(L\) nhập trong mã:
\(L_{\text{mới}} = L_{\text{cũ}} \times \frac{360^\circ}{342^\circ} \approx 1{,}053 \cdot L_{\text{cũ}}.\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Chỉ cần 5 phút đo thước dây và chỉnh hai hệ số này, sai số odometry vòng hở thường giảm từ ~10% xuống dưới 1–2%!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chép đủ năm tiêu chí vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, chấm rõ “đạt” hoặc “trượt” kèm tên tệp bằng chứng tương ứng.&lt;/li&gt;
  &lt;li&gt;Với mỗi tiêu chí trượt, viết một câu cụ thể nêu rõ hành động khắc phục (không ghi chung chung kiểu “sẽ xem lại”).&lt;/li&gt;
  &lt;li&gt;Chỉ ghi xác nhận “đủ điều kiện bắt đầu Chương 09” khi cả năm tiêu chí đều đạt (hoặc bốn tiêu chí đạt kèm mục hoãn encoder có ngày cụ thể và đã chạy đạt phần mô phỏng PID).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Cần kiểm&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Đủ đề mục, không có file&lt;/td&gt;
      &lt;td&gt;Chưa làm. Dán đường dẫn file thật&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;$L = 16$&lt;/td&gt;
      &lt;td&gt;Đổi thành $0{,}16$ nếu thước là 16 cm, rồi chạy lại assert&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;CSV toàn số 0&lt;/td&gt;
      &lt;td&gt;Encoder chưa vào ngắt, hoặc $\Delta t$ sai đơn vị&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mô phỏng P không vọt&lt;/td&gt;
      &lt;td&gt;Bạn đang chạy plant khác bài. Chạy lại script của bài PID&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=UR0hOmjaHp0&quot;&gt;PID Control - A brief introduction&lt;/a&gt; — Brian Douglas, tiếng Anh. Ôn lại đặc tính đáp ứng bước nhảy của PID trước khi hoàn tất checklist kiểm tra chương này.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tiêu chí nào bị trượt nếu quaternion yaw $90^\circ$ đã đúng nhưng tọa độ điểm ứng với góc yaw $\pi$ vẫn ra số dương?&lt;/li&gt;
  &lt;li&gt;Khổ vết thực đo được $L = 0{,}158$ m nhưng trong mã nguồn nhập nhầm thành $15{,}8$. Bán kính quay $R = v/\omega$ bị sai lệch khoảng bao nhiêu lần?&lt;/li&gt;
  &lt;li&gt;Vì sao tiêu chí PID trên phần cứng thật có thể tạm hoãn có ngày (kèm mô phỏng), còn tiêu chí động học thì không?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Tiêu chí Toán TF bị trượt vì phép quay $180^\circ$ chưa được áp dụng đúng vào ma trận biến đổi.&lt;/li&gt;
  &lt;li&gt;Sai lệch 100 lần, vì $15{,}8 / 0{,}158 = 100$.&lt;/li&gt;
  &lt;li&gt;PID trên xe thật phụ thuộc phần cứng encoder (có thể thay thế tạm bằng mô phỏng đã kiểm chứng), còn động học chỉ cần thước đo khung xe và viết hàm Python nên không có lý do để trì hoãn.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter08/08_00_math_for_tf/&quot;&gt;Bài toán TF&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter08/08_03_pid_tuning/&quot;&gt;Bài PID&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter08/08_04_diff_kinematics/&quot;&gt;Bài động học&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Timing vòng điều khiển và jitter</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_05_loop_timing/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_05_loop_timing</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~75 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Một thuật toán PID hoàn hảo trên giấy vẫn có thể làm robot rung bần bật hoặc lao thẳng vào tường nếu vòng lặp điều khiển bị trễ thất thường: nhịp này chạy sau &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 ms&lt;/code&gt;, nhịp sau bỗng bị lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print()&lt;/code&gt; hay cấp phát bộ nhớ kéo dài thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;47 ms&lt;/code&gt;. Trong kỹ thuật điều khiển thời gian thực, &lt;strong&gt;độ ổn định của chu kỳ lấy mẫu (determinism &amp;amp; low jitter)&lt;/strong&gt; quan trọng không kém bản thân công thức toán học.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-định-thời-vòng-lặp-điều-khiển&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Định thời Vòng lặp Điều khiển&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chu kỳ lấy mẫu \(T_s\), Băng thông &amp;amp; Định lý Nyquist&lt;/strong&gt;:
Vòng điều khiển vận tốc bánh xe thường cần tần số lấy mẫu gấp 10–20 lần băng thông cơ học của động cơ (ví dụ 50–100 Hz, tức \(T_s = 10\text{–}20\text{ ms}\)). Nếu \(T_s\) quá lớn, độ trễ pha phá vỡ dự trữ ổn định của PID.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Độ lệch nhịp (Jitter) &amp;amp; Tác hại lên khâu Vi phân (D) / Tích phân (I)&lt;/strong&gt;:
Khi \(dt\) dao động thất thường mà mã nguồn lại giả định \(dt\) hằng số, khâu \(D = K_d \Delta e / dt\) khuếch đại nhiễu thành xung giật động cơ, còn bộ đếm encoder cho vận tốc ước lượng sai lệch.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cơ chế an toàn Watchdog (Deadman Switch)&lt;/strong&gt;:
Khi mất kết nối USB/Wi-Fi từ máy tính cấp trên (SBC), vi điều khiển không được giữ mãi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; cũ mà phải tự đặt vận tốc về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; sau ngưỡng timeout (ví dụ 300–500 ms).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn biết thay thế &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; trong vòng điều khiển động cơ bằng cơ chế định thời theo mốc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;, ghi đo chu kỳ thực tế qua vài trăm nhịp (tick) và dựa vào phân vị 95 (p95) để đánh giá xem hệ thống có thực sự đạt 50 Hz hay không. Bạn biết tách &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt;, tác vụ Wi-Fi và phân tích JSON ra khỏi luồng điều khiển nhanh, đồng thời giải thích được ảnh hưởng của một chu kỳ PID bị trễ lên thành phần \(K_d\). Nhật ký định thời trong bài này là hạng mục bắt buộc trước khi tin cậy bất kỳ cầu nối ROS 2 nào.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Các bài 08-01 đến 08-04 đã cung cấp khung giao tiếp serial, client MQTT tùy chọn, bộ điều khiển PID và hàm động học. Bạn biết dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; trên ESP32/Pico và hiểu rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; chỉ tạm dừng thêm 20 ms &lt;em&gt;sau khi&lt;/em&gt; xử lý xong tác vụ chứ không giữ chu kỳ cố định khi thời gian tính toán biến thiên. Bài thực hành chỉ cần Serial Monitor trên laptop; nên ngắt nguồn động cơ khi đo thời gian để đảm bảo an toàn cho đến khi chu kỳ chạy ổn định.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Thành phần đạo hàm trong PID chia trực tiếp cho \(\Delta t\). Nếu mã nguồn mặc định \(\Delta t = 0,02\) s nhưng chu kỳ thực tế bị kéo dài tới 0,08 s do MQTT tự kết nối lại, khâu \(K_d\) sẽ bị khuếch đại gấp bốn lần ở mẫu đó và khiến bánh xe rung rè. Tương tự, tính odometry bằng cách nhân vận tốc với “20 ms” khi vòng lặp thực mất 35 ms sẽ làm sai lệch hoàn toàn tỉ lệ bản đồ. ROS 2 không thể tự sửa một đồng hồ sai; tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; ở Chương 10 cũng phục vụ đúng nguyên tắc thống nhất một nguồn thời gian duy nhất. Ngay cả các chính sách học máy chạy ở 10 Hz cũng đòi hỏi vòng điều khiển cấp dưới giữ nhịp đều đặn.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_loop_timing.png&quot; alt=&quot;Hạn cố định so với jitter của delay&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Chu kỳ điều khiển là khoảng thời gian giữa hai &lt;em&gt;mẫu&lt;/em&gt; liên tiếp, không phải thời gian nghỉ của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt;. Nếu đọc encoder tốn 1 ms, tính PID tốn 0,2 ms rồi mới gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt;, chu kỳ thực tế sẽ là 21,2 ms và tiếp tục giãn ra mỗi khi có lệnh in log. &lt;strong&gt;Jitter&lt;/strong&gt; là độ biến thiên (độ phân tán) của các chu kỳ đó: một vòng lặp thường chạy 20 ms nhưng thỉnh thoảng vọt lên 90 ms vẫn cho giá trị trung bình rất đẹp, nhưng phần đuôi trễ 90 ms mới chính là nguyên nhân làm bão hòa khâu D và rơi byte USB.&lt;/p&gt;

&lt;p&gt;Mẫu định thời chuẩn theo mốc kế tiếp:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;next = millis() + PERIOD_MS
mỗi vòng:
    now = millis()
    if (int32_t)(now - next) &amp;lt; 0: return   # chưa tới hạn
    dt_ms = now - prev
    prev = now
    next += PERIOD_MS
    if dt_ms &amp;gt; 5 * PERIOD_MS: dt_ms = PERIOD_MS
    control_step(dt_ms / 1000.0)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Phép trừ ép kiểu có dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(int32_t)&lt;/code&gt; đảm bảo vòng lặp vẫn chạy đúng khi bộ đếm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; bị tràn sau khoảng 49 ngày. Tăng mốc bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next += PERIOD_MS&lt;/code&gt; (thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next = now + PERIOD_MS&lt;/code&gt;) giúp giữ chuẩn tần số trung bình khi một tick bị trễ nhẹ; tuy nhiên, nếu bị trễ quá nhiều (vượt hẳn một chu kỳ), nên đặt lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next = now + PERIOD_MS&lt;/code&gt; để tránh chạy dồn hàng loạt bước PID liên tiếp.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phân bổ tác vụ trong một tick 20 ms:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Được phép thực hiện:&lt;/strong&gt; đọc encoder, tính động học nếu có lệnh mới, cập nhật PID, xuất PWM, kiểm tra watchdog.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Không được đặt trong tick nhanh:&lt;/strong&gt; in nhiều dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt;, gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;client.connect()&lt;/code&gt;, phân tích JSON dung lượng lớn, gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; hoặc chờ kết nối Wi-Fi.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Gói telemetry chỉ nên gửi ngắn gọn mỗi 5 hoặc 10 tick (tương ứng 10 Hz hoặc 5 Hz). Hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;client.loop()&lt;/code&gt; của MQTT có thể chạy xen kẽ giữa các mốc hạn, còn việc kết nối lại nên giới hạn ở 1 Hz bằng bộ đếm riêng. Nếu quá trình kết nối lại bị chặn lâu, watchdog của Bài 08-02 vẫn sẽ đưa động cơ về 0, còn log thời gian giúp bạn nhận diện chính xác khoảng gián đoạn đó.&lt;/p&gt;

&lt;p&gt;Ngân sách tần số thực tế cho robot Capstone là &lt;strong&gt;50 Hz (20 ms)&lt;/strong&gt; cho vòng điều khiển động cơ và &lt;strong&gt;10 Hz&lt;/strong&gt; cho telemetry. Bạn chỉ nên nâng lên 100 Hz nếu nhật ký chứng minh phân vị p95 dưới khoảng 12 ms (với đích 10 ms) — chạy tần số danh nghĩa cao hơn khả năng đo đạc thực tế không mang lại lợi ích.&lt;/p&gt;

&lt;p&gt;Khi đánh giá hiệu năng vòng lặp, đừng chỉ nhìn giá trị trung bình: hãy sắp xếp tập chu kỳ đo được để báo cáo trung vị (&lt;strong&gt;p50&lt;/strong&gt;), phân vị 95 (&lt;strong&gt;p95&lt;/strong&gt;) và giá trị cực đại (&lt;strong&gt;max&lt;/strong&gt;). Trong khóa học này, vòng lặp đạt yêu cầu khi p95 nằm trong khoảng \(0,8\) đến \(1,2\) lần chu kỳ đích. Một gai trễ 200 ms dù “trung bình vẫn 21 ms” vẫn tính là chưa đạt cho tới khi tìm ra lời gọi gây nghẽn.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Xét 40 chu kỳ với đích 20 ms: 38 chu kỳ nằm ở mức 20–21 ms, một chu kỳ 28 ms và một chu kỳ vọt lên 74 ms do bộ đệm serial bị đẩy đầy khi in gỡ lỗi. Trung bình khoảng 22,5 ms và p95 là 28 ms trông có vẻ tạm ổn, nhưng giá trị cực đại lên tới 74 ms. Tại mẫu 74 ms đó, khâu \(K_d\) nhận một khoảng \(\Delta t\) lớn gần gấp 4 lần thiết kế (hoặc tính sai đạo hàm gấp 4 lần nếu cố giữ cứng \(\Delta t = 0,02\) s). Bài thực hành yêu cầu ghi nhận rõ cả giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max&lt;/code&gt; và dòng mã gây ra nó.&lt;/p&gt;

&lt;p&gt;Đoạn mã Python nhỏ để thống kê p50, p95 và cực đại trên máy tính:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;statistics&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;74&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;p50&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;median&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;p95~&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.95&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trên vi điều khiển, bạn không cần lưu toàn bộ mảng biểu đồ: chỉ cần duy trì biến lưu giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max&lt;/code&gt; và bộ đếm số tick vượt ngưỡng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PERIOD_MS + 4&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-sai-số-khoảng-cách-do-jitter-và-ngưỡng-watchdog&quot;&gt;Giải thích: Tính tay sai số khoảng cách do Jitter và ngưỡng Watchdog&lt;/h3&gt;

&lt;p&gt;Giả sử robot đang chạy thẳng với vận tốc \(v = 0{,}60\text{ m/s}\) trên chu kỳ danh định \(T_s = 20\text{ ms}\) (50 Hz):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Ảnh hưởng của độ trễ lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt;:&lt;/strong&gt; Nếu một hàm in debug dài làm chu kỳ bị kéo giãn từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;20 ms&lt;/code&gt; lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;55 ms&lt;/code&gt; (jitter \(\Delta T = +35\text{ ms}\)):
    &lt;ul&gt;
      &lt;li&gt;Quãng đường xe trượt thêm ngoài dự tính trong 1 nhịp: \(0{,}60 \times 0{,}035 = \mathbf{21\text{ mm}}\).&lt;/li&gt;
      &lt;li&gt;Nếu công thức tính vận tốc vẫn chia cho hằng số cố định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.02 s&lt;/code&gt; thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt&lt;/code&gt; thực đo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.055 s&lt;/code&gt;, vận tốc ước lượng bị phóng đại sai lên \(\frac{0{,}055}{0{,}020} = \mathbf{2{,}75\times}\), khiến bộ PID đạp phanh đột ngột và gây rung lắc!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chọn ngưỡng Watchdog:&lt;/strong&gt; Nếu ROS 2 gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; ở tần số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 Hz&lt;/code&gt; (mỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100 ms&lt;/code&gt; 1 gói), đặt Watchdog ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;300 ms&lt;/code&gt; nghĩa là cho phép rớt tối đa 2 gói liên tiếp; nếu gói thứ 3 không tới, xe phanh dừng trong tối đa \(0{,}60 \times 0{,}30 = \mathbf{18\text{ cm}}\) thay vì lao tiếp không kiểm soát!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Ngắt nguồn động cơ ở lượt đo đầu tiên. Cho firmware in một dòng CSV mỗi tick trong 5 giây rồi dừng. Vì bản thân việc in mỗi tick đã tạo thêm độ trễ, hãy thực hiện thêm lượt đo thứ hai chỉ in một dòng tóm tắt duy nhất sau 250 tick:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dt_ms
20
21
20
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Dòng tóm tắt sau 250 tick:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;n=250 p50=20 p95=22 max=27 over=3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tiếp theo, bật thêm tải thực tế (như chạy client MQTT hoặc cố tình đặt chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt; vào trong tick) và chụp lại dòng tóm tắt để so sánh trực tiếp.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bản&lt;/th&gt;
      &lt;th&gt;p95&lt;/th&gt;
      &lt;th&gt;max&lt;/th&gt;
      &lt;th&gt;Kết luận&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Vòng hạn, chỉ in tóm tắt&lt;/td&gt;
      &lt;td&gt;≤ 24 ms&lt;/td&gt;
      &lt;td&gt;≤ 30 ms&lt;/td&gt;
      &lt;td&gt;đạt cho 50 Hz&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; cộng in 40 ký tự mỗi tick&lt;/td&gt;
      &lt;td&gt;thường &amp;gt; 30 ms&lt;/td&gt;
      &lt;td&gt;phần in thống trị&lt;/td&gt;
      &lt;td&gt;trượt, đưa lệnh in ra&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nối lại MQTT bên trong tick&lt;/td&gt;
      &lt;td&gt;một mẫu hàng trăm ms&lt;/td&gt;
      &lt;td&gt;chính lần nối lại&lt;/td&gt;
      &lt;td&gt;trượt, đưa nối lại ra khỏi tick&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Khi hỏng thì thường vì&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Nguyên nhân hay gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Mọi chu kỳ là 0 hoặc 1&lt;/td&gt;
      &lt;td&gt;Quên đợi hạn; vòng đang quay không nghỉ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chu kỳ tăng mãi: 20, 40, 60&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next&lt;/code&gt; không được cập nhật, hoặc bạn chồng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;max rất lớn chỉ khi cáp lỏng&lt;/td&gt;
      &lt;td&gt;Serial USB bị chặn vì bộ đệm đầy. Rút ngắn dòng in&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;PID rít chỉ khi bật telemetry&lt;/td&gt;
      &lt;td&gt;Log nằm trong tick. Hệ số được chỉnh trên một lời nói dối&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pico và ESP32 không giống nhau&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; cả hai đều ổn; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep&lt;/code&gt; trong MicroPython là cùng lỗi với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Ghi chú cho MicroPython: sử dụng cặp hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff()&lt;/code&gt; để xử lý an toàn khi bộ đếm tràn; gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep_ms(20)&lt;/code&gt; sau phần xử lý cũng gây ra lỗi jitter tương tự &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài này không yêu cầu linh kiện mới — chính bo ESP32 bạn đang có là dụng cụ đo. Nếu đường truyền USB serial hay rơi ký tự, hãy thay một sợi cáp truyền dữ liệu tốt (tìm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cáp micro USB truyền dữ liệu&lt;/code&gt; trên Shopee/Lazada, khoảng 20.000–50.000 đồng) và tránh dùng cáp chỉ sạc.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=UR0hOmjaHp0&quot;&gt;PID Control - A brief introduction&lt;/a&gt; — Brian Douglas, tiếng Anh. Vòng PID rời rạc giả định các chu kỳ lấy mẫu đều nhau; jitter sẽ làm sai lệch thành phần D ngay cả khi công thức không đổi.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[ESP32 + PWM + Android&lt;/td&gt;
          &lt;td&gt;What is PWM?&lt;/td&gt;
          &lt;td&gt;Pulse Width Modulation](https://www.youtube.com/watch?v=vzdAOaAk5JQ) — Slavko’s Hobby Channel, tiếng Anh. Minh họa việc cập nhật duty PWM theo nhịp; nếu chu kỳ cập nhật bị trễ, tốc độ thực tế của động cơ sẽ lệch khỏi tính toán.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Các video ngắn (Facebook Reel) dưới đây của Hoàng Quân (Cơ điện tử ICTU, &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/2056881368258089&quot;&gt;Thả ra là bay&lt;/a&gt; — Chú thích: thả ra là bay ngay. Minh họa yêu cầu vòng lặp định thời của bộ điều khiển phải hoạt động ổn định trước khi thả thiết bị.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1038211995413233&quot;&gt;Sinh viên làm UAV vận tải&lt;/a&gt; — Chú thích: sinh viên cơ điện tử ICTU làm UAV vận tải. Các hệ thống bay mang tải đều phụ thuộc chặt vào chu kỳ vòng điều khiển; độ trễ trong vòng lặp sẽ biểu hiện trực tiếp khi đang trên không.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1004870148832135&quot;&gt;Những bước đầu làm drone vận tải&lt;/a&gt; — Chú thích: những bước đầu tiên làm drone vận tải. Tương tự clip trên: cần đo kiểm chu kỳ vòng lặp ổn định trước khi tăng tải trọng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chu kỳ thực tế \(\Delta t\) của một tick bị kéo dài thành 0,08 s nhưng mã nguồn vẫn chia đạo hàm cho 0,02 s. Thành phần D bị sai lệch bao nhiêu lần? Gợi ý: lớn gấp 4 lần vì tốc độ biến thiên thực tế nhỏ hơn 4 lần so với giả định.&lt;/li&gt;
  &lt;li&gt;Vì sao giá trị cực đại (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max&lt;/code&gt;) cảnh báo lỗi vòng lặp tốt hơn giá trị trung bình? Gợi ý: trung bình che giấu các lần bị chặn đột xuất, trong khi động cơ và bộ điều khiển cảm nhận trực tiếp cú giật ở lần chặn đó.&lt;/li&gt;
  &lt;li&gt;Viết lại đoạn mã giả định thời sao cho nếu một tick bị trễ tới 200 ms, hệ thống sẽ không chạy dồn liên tiếp nhiều bước PID bù. Gợi ý: khi độ trễ vượt quá một chu kỳ, đặt lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next = now + PERIOD_MS&lt;/code&gt; và kẹp giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Nên đặt xử lý truyền thông Mosquitto ở đâu so với mốc hạn điều khiển? Gợi ý: đặt xen kẽ giữa các mốc hạn (không chặn trong tick đến hạn), và đảm bảo watchdog vẫn kịp ngắt động cơ về 0 nếu kết nối bị treo.&lt;/li&gt;
  &lt;li&gt;Bạn sẽ ghi nhận định nào cho cầu nối Chương 10 nếu p95 đo được là 45 ms trên chu kỳ đích 20 ms? Gợi ý: “Chưa tinh chỉnh Nav2 khi p95 của vòng điều khiển nền còn lệch quá 20% chu kỳ đích.”&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/time/millis/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis&lt;/code&gt; của Arduino&lt;/a&gt; — nguyên lý tràn bộ đếm và cách dùng phép trừ có dấu.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.micropython.org/en/latest/library/time.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_ms&lt;/code&gt; của MicroPython&lt;/a&gt; — tương đương khi lập trình bằng Python nhúng.&lt;/li&gt;
  &lt;li&gt;Nguyên tắc thống nhất nguồn thời gian qua tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; trong &lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Using-ros2-param.html&quot;&gt;hướng dẫn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 param&lt;/code&gt; của Jazzy&lt;/a&gt; (áp dụng cho mô phỏng ở Chương 10).&lt;/li&gt;
  &lt;li&gt;Thành phần đạo hàm ở Bài 08-03 — công thức phụ thuộc trực tiếp vào độ ổn định của chu kỳ \(\Delta t\) này.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Động học robot dẫn động vi sai</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_04_diff_kinematics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_04_diff_kinematics</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~75 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Một robot hai bánh không hề hiểu “rẽ trái với bán kính 0,5 m” là gì — hai động cơ DC bên dưới chỉ biết quay ở vận tốc bao nhiêu vòng/phút. Cầu nối toán học biến ý định chuyển động của thân xe \((v, \omega)\) thành lệnh cho từng bánh xe \((v_L, v_R)\), và ngược lại biến số xung encoder thành vị trí \((x, y, \theta)\) trên sàn, chính là &lt;strong&gt;động học dẫn động vi sai (differential-drive kinematics)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trụ-cột-của-động-học-robot-vi-sai&quot;&gt;Kiến thức cần nhớ: Ba trụ cột của Động học Robot Vi sai&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Hệ tọa độ thân robot chuẩn REP-103&lt;/strong&gt;:
Trục \(+x\) hướng về mũi xe, \(+y\) sang trái, \(+z\) hướng lên trên; quy tắc bàn tay phải cho \(\omega &amp;gt; 0\) là quay ngược chiều kim đồng hồ (rẽ trái, tức bánh phải chạy nhanh hơn bánh trái).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Động học thuận, nghịch &amp;amp; Ràng buộc phi holonomic&lt;/strong&gt;:
Hai phương trình tuyến tính \(v = (v_R + v_L)/2\) và \(\omega = (v_R - v_L)/L\) nối vận tốc bánh với vận tốc thân. Vì không có thành phần trượt ngang \(v_y = 0\), robot là hệ &lt;em&gt;phi holonomic&lt;/em&gt;: muốn dịch ngang bắt buộc phải thực hiện chuỗi quay–tiến–quay.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tích phân Odometry (Từ xung encoder đến tư thế \((x, y, \theta)\))&lt;/strong&gt;:
Cộng dồn quãng đường vi phân \(\Delta s\) và góc quay \(\Delta \theta\) ở từng chu kỳ lấy mẫu \(\Delta t\) giúp ước lượng vị trí liên tục — nhưng sai số trôi góc \(\Delta \theta\) sẽ tích lũy theo thời gian nếu không có cảm biến ngoài (IMU/LiDAR) hiệu chỉnh.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn đo chính xác khổ vết (khẩu độ bánh) của khung Capstone, chuyển đổi vận tốc thân robot \((v, \omega)\) thành tốc độ vành bánh trái và phải, rồi tính ngược lại để kiểm chứng. Bạn biết dự đoán bán kính quay của một lệnh điều khiển trước khi robot di chuyển và giải thích được vì sao cơ cấu vi sai không thể trượt ngang. Hai phương trình động học này là phần cốt lõi của mọi hàm xử lý &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; từ Chương 10 trở đi.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Bạn xác định đúng bánh trái và bánh phải theo hướng nhìn từ trên thân robot về phía mũi xe, và đã hoàn thành bài teleop vòng hở ở Chương 07. Dụng cụ mới duy nhất cần dùng là thước hoặc thước dây. Vòng PID ở Bài 08-03 là tùy chọn trong bài này: động học tạo ra tốc độ đặt mục tiêu, còn chạy thử bằng PWM vòng hở khi kê bánh trên không vẫn đủ để kiểm chứng chiều quay.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Thông điệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; không gửi trực tiếp lệnh “động cơ trái” hay “động cơ phải”. Nav2, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;teleop_twist_keyboard&lt;/code&gt; và các chính sách điều khiển về sau đều xuất vận tốc tịnh tiến và vận tốc góc trong hệ tọa độ thân robot; tầng điều khiển dưới phải giải động học nghịch để tính tốc độ hai bánh. Nếu nhập sai khổ vết bánh, bản đồ Chương 11 sẽ sai tỉ lệ dù thuật toán SLAM hoạt động chuẩn xác; nếu ngược dấu \(\omega\), robot sẽ quay ra xa mục tiêu khiến bộ lập kế hoạch thất bại. Hãy viết hàm động học nghịch một lần duy nhất, ghi rõ đơn vị và gọi lại thống nhất từ node cầu nối serial.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_diff_kinematics.png&quot; alt=&quot;Nhìn từ trên: khẩu độ và tốc độ hai bánh&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Theo quy ước REP-103, trục \(+x\) hướng về phía trước, \(+y\) sang trái và \(+z\) hướng lên trên. Vận tốc góc \(\omega\) dương tương ứng với chuyển động rẽ trái (bánh phải chạy nhanh hơn bánh trái). Gọi \(v_R\) và \(v_L\) là vận tốc dài tại vành bánh phải và bánh trái (m/s, dương khi tiến), còn \(L\) là khổ vết bánh tính bằng mét — đo giữa hai tâm vết tiếp xúc mặt lốp, không phải chiều rộng tấm mica.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Động học thuận&lt;/strong&gt; (từ tốc độ hai bánh suy ra vận tốc thân):&lt;/p&gt;

\[v = \frac{v_R + v_L}{2}\]

\[\omega = \frac{v_R - v_L}{L}\]

&lt;p&gt;&lt;strong&gt;Động học nghịch&lt;/strong&gt; (từ lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; suy ra tốc độ hai bánh):&lt;/p&gt;

\[v_R = v + \omega \frac{L}{2}\]

\[v_L = v - \omega \frac{L}{2}\]

&lt;p&gt;Khi \(v = 0\) và \(\omega \neq 0\), hai bánh quay ngược chiều với cùng độ lớn vận tốc, làm robot xoay tại chỗ quanh tâm trục. Khi \(v \neq 0\) và \(\omega \neq 0\), bán kính quỹ đạo quay là:&lt;/p&gt;

\[R = \frac{v}{\omega}\]

&lt;p&gt;Với \(\omega &amp;gt; 0\) và \(v &amp;gt; 0\), tâm quay tức thời nằm về phía bên trái robot một khoảng \(R\). Lưu ý rằng khung vi sai không có thành phần vận tốc ngang \(v_y\) (ràng buộc phi holonomic); mọi bộ điều khiển cố ra lệnh trượt ngang sẽ gây dao động — nguyên tắc này sẽ gặp lại khi cấu hình Nav2.&lt;/p&gt;

&lt;p&gt;Bán kính bánh \(r\) chỉ xuất hiện khi quy đổi sang tốc độ góc trục bánh hoặc số xung encoder:&lt;/p&gt;

\[\dot{\theta} = \frac{v_{\text{wheel}}}{r}\]

&lt;p&gt;Không đưa \(r\) vào phương trình tính \(v, \omega\) để tránh nhầm lẫn giữa đơn vị vòng/giây (hoặc rad/s) và m/s trong cùng một biến.&lt;/p&gt;

&lt;p&gt;Cần phân biệt rõ ba thang giá trị: tín hiệu trục joystick (không có đơn vị m/s) phải được quy đổi có chủ đích — ví dụ đẩy hết cần tiến tương ứng \(v = 0,3\) m/s và gạt hết cần quay tương ứng \(\omega = 1,0\) rad/s — và ghi chú rõ bên cạnh phương trình. Sau khi qua động học nghịch ra m/s ở vành bánh, một hàm quy đổi riêng sẽ ánh xạ sang thang số nguyên −100…100 của Bài 08-01 (dựa trên tốc độ đo thực tế ở Chương 07), tránh đặt hệ số cảm tính.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Đo trực tiếp giữa hai tâm vết tiếp xúc được \(L = 0,16\) m và bán kính bánh \(r = 0,033\) m. Xét lệnh rẽ trái nhẹ với \(v = 0,20\) m/s và \(\omega = 0,50\) rad/s:&lt;/p&gt;

\[\frac{L}{2} = 0,08\]

\[v_R = 0,20 + 0,50 \times 0,08 = 0,24 \text{ m/s}\]

\[v_L = 0,20 - 0,50 \times 0,08 = 0,16 \text{ m/s}\]

&lt;p&gt;Kiểm tra ngược lại bằng động học thuận:&lt;/p&gt;

\[v = \frac{0,24 + 0,16}{2} = 0,20\]

\[\omega = \frac{0,24 - 0,16}{0,16} = 0,50\]

&lt;p&gt;Bán kính quỹ đạo là \(R = 0,20 / 0,50 = 0,40\) m. Trên mặt sàn, trung điểm trục bánh sẽ vẽ cung tròn bán kính khoảng 0,40 m vì bánh phải chạy trên vòng ngoài lớn hơn nên có tốc độ cao hơn bánh trái.&lt;/p&gt;

&lt;p&gt;Nếu vòng PID cần vận tốc góc của trục bánh phải:&lt;/p&gt;

\[\dot{\theta}_R = \frac{0,24}{0,033} \approx 7,27 \text{ rad/s}\]

&lt;p&gt;Đoạn mã Python sau có thể dùng trực tiếp trong node cầu nối Chương 10:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wheel_speeds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;omega&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;omega&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;omega&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# trái, phải
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;body_from_wheels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;body_from_wheels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wheel_speeds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Kiểm tra trường hợp xoay tại chỗ với \(v = 0\), \(\omega = 1,0\) rad/s và \(L = 0,16\) m: ta được \(v_R = 0,08\) m/s và \(v_L = -0,08\) m/s. Nếu cả hai giá trị đều dương thì dấu cộng/trừ trong công thức đang bị sai.&lt;/p&gt;

&lt;h2 id=&quot;từ-số-đếm-encoder-tới-tư-thế&quot;&gt;Từ số đếm encoder tới tư thế&lt;/h2&gt;

&lt;p&gt;Các phương trình trên viết theo vận tốc vành bánh, còn cảm biến encoder trả về số xung đếm. Gọi $N$ là số xung trên mỗi vòng trục bánh, $r$ là bán kính lăn (mét), $\Delta c_L$ và $\Delta c_R$ là độ biến thiên số xung có dấu trong một chu kỳ cập nhật:&lt;/p&gt;

\[\Delta s_L = \frac{2\pi r}{N}\Delta c_L, \qquad
\Delta s_R = \frac{2\pi r}{N}\Delta c_R\]

\[\Delta s = \frac{\Delta s_R + \Delta s_L}{2}, \qquad
\Delta \psi = \frac{\Delta s_R - \Delta s_L}{L}\]

&lt;p&gt;Trong mỗi chu kỳ ngắn, quỹ đạo xe xấp xỉ một cung tròn. Để giảm sai số rời rạc hóa, ta chiếu quãng đường $\Delta s$ theo hướng ở &lt;strong&gt;giữa&lt;/strong&gt; chu kỳ (phương pháp điểm giữa — midpoint) thay vì hướng ở đầu chu kỳ:&lt;/p&gt;

\[x \leftarrow x + \Delta s \cos\left(\psi + \frac{\Delta\psi}{2}\right)\]

\[y \leftarrow y + \Delta s \sin\left(\psi + \frac{\Delta\psi}{2}\right)\]

\[\psi \leftarrow \psi + \Delta\psi\]

&lt;p&gt;Nếu chỉ dùng góc $\psi$ đầu chu kỳ, mỗi đoạn quay sẽ bị trễ pha nửa bước. Khi đi thẳng ($\Delta\psi = 0$) hai cách cho kết quả giống nhau; khi xoay tại chỗ ($\Delta s = 0$) tọa độ $x, y$ đứng yên và chỉ góc $\psi$ thay đổi.&lt;/p&gt;

&lt;p&gt;Xét ví dụ chuẩn với $r = 0{,}033$ m, $L = 0{,}16$ m và $N = 1440$ xung/vòng. Mỗi xung tương ứng quãng đường:&lt;/p&gt;

\[\frac{2\pi \cdot 0{,}033}{1440} \approx 1{,}440 \times 10^{-4}~\mathrm{m}\]

&lt;p&gt;Để đi thẳng 1 m, mỗi bánh quay khoảng 6945 xung cùng chiều. Để xoay trái tại chỗ một góc $\pi/2$, mỗi bánh đi cung dài $(L/2)\cdot(\pi/2)$, tương ứng khoảng 873 xung (bánh phải dương, bánh trái âm). Nếu robot đi thẳng 1 m, xoay trái $\pi/2$ rồi đi tiếp 0,5 m, tư thế cuối sẽ rất gần $(1{,}00~\mathrm{m},\ 0{,}50~\mathrm{m},\ \pi/2)$ (sai lệch dưới 1 mm chỉ do làm tròn số xung nguyên).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_odom_path.png&quot; alt=&quot;Đường odometry: thẳng, quay trái, thẳng&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pose_from_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;counts_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counts_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counts_per_rev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Từng phần tử là số đếm mới trong một nhịp, có dấu. Tư thế trả về theo mét và radian.&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;meters&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;radius&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counts_per_rev&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;psi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_r&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;counts_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counts_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ds_l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meters&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ds_r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meters&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ds_r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ds_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dpsi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ds_r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ds_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;psi_mid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;psi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dpsi&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;psi_mid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;psi_mid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;psi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dpsi&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;psi&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.033&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1440&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;meters&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c_straight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# ~6945
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_turn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# ~873
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_short&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.50&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;# ~3472
# 50 nhịp thẳng, 20 nhịp quay, 25 nhịp thẳng. Tổng đếm giữ nguyên.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ticks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_straight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_straight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_turn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_turn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_short&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_short&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;psi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pose_from_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ticks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ticks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;psi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.02&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;psi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Kết quả in ra phải xấp xỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.0 0.5 1.5708&lt;/code&gt;. Nếu giá trị $y$ bị âm, bạn đã đảo nhầm bánh trái và bánh phải khi quay.&lt;/p&gt;

&lt;p&gt;Hàm trên chính là phần lõi tạo nên thông điệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt; sau này: $x, y$ và góc yaw (chuyển sang quaternion theo Bài 08-00) điền vào trường tư thế, còn $\Delta s / \Delta t$ và $\Delta\psi / \Delta t$ điền vào trường vận tốc — tuyệt đối không ghi trực tiếp số xung thô vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-tính-tay-quỹ-đạo-odometry-sau-2-bước-thời-gian-delta-t--05text-s&quot;&gt;Giải thích: Tính tay quỹ đạo Odometry sau 2 bước thời gian \(\Delta t = 0{,}5\text{ s}\)&lt;/h3&gt;

&lt;p&gt;Giả sử robot có khổ vết \(L = 0{,}20\text{ m}\), xuất phát tại gốc \((x_0, y_0, \theta_0) = (0, 0, 0)\). Trong mỗi nhịp \(\Delta t = 0{,}5\text{ s}\), bộ mã hóa đo được vận tốc vành bánh \(v_L = 0{,}20\text{ m/s}\) và \(v_R = 0{,}30\text{ m/s}\):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tính vận tốc thân xe:&lt;/strong&gt;
\(v = \frac{0{,}30 + 0{,}20}{2} = \mathbf{0{,}25\text{ m/s}}, \qquad \omega = \frac{0{,}30 - 0{,}20}{0{,}20} = \mathbf{0{,}50\text{ rad/s}}.\)
Bán kính cung quay là \(R = v / \omega = 0{,}25 / 0{,}50 = \mathbf{0{,}50\text{ m}}\) về phía bên trái.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cập nhật tư thế theo tích phân điểm giữa (Midpoint Odometry) mỗi bước \(\Delta s = v\Delta t = 0{,}125\text{ m}\), \(\Delta\theta = \omega\Delta t = 0{,}25\text{ rad}\):&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Bước \(k\)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Góc giữa \(\theta_{k-1} + \Delta\theta/2\)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;\(x_k\) (m)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;\(y_k\) (m)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;\(\theta_k\) (rad)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;—&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}000\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}000\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}000\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}125\text{ rad}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0 + 0{,}125\cos(0{,}125) \approx \mathbf{0{,}124}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0 + 0{,}125\sin(0{,}125) \approx \mathbf{0{,}016}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}250\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}375\text{ rad}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}124 + 0{,}125\cos(0{,}375) \approx \mathbf{0{,}240}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}016 + 0{,}125\sin(0{,}375) \approx \mathbf{0{,}062}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}500\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Chú ý: nếu đo nhầm khổ vết \(L\) sai 10% (ví dụ nhập \(0{,}18\text{ m}\) thay vì \(0{,}20\text{ m}\)), góc quay tính ra sai lệch ngay 11% ở mỗi vòng quay — đó là lý do hiệu chuẩn thực nghiệm \(L\) cực kỳ quan trọng trước khi vào SLAM!&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đặt robot trên bàn (kê bánh trên không hoặc trên giá đỡ), đo khổ vết \(L\) ba lần và ghi giá trị trung bình theo đơn vị mét.&lt;/li&gt;
  &lt;li&gt;Đánh dấu một điểm trên mỗi lốp bằng phấn hoặc băng dính. Gửi lệnh cùng tốc độ cho cả hai bánh trong 2 giây để xác nhận cả hai dấu đều quay tiến, chốt chắc dấu của \(v_L\) và \(v_R\) trong firmware.&lt;/li&gt;
  &lt;li&gt;Gửi lệnh xoay trái tại chỗ ở mức PWM thấp (tương ứng mức “tiến chậm” từ Chương 07) và xác nhận bánh phải quay tiến, bánh trái quay lùi.&lt;/li&gt;
  &lt;li&gt;(Tùy chọn trên sàn trống, tốc độ chậm): chạy lệnh \(v = 0,2\) m/s, \(\omega = 0,5\) rad/s khoảng một phần tư vòng và so sánh bán kính thực đo bằng thước với giá trị dự đoán \(R = v/\omega\) tính theo \(L\) thực tế của xe bạn.&lt;/li&gt;
  &lt;li&gt;Lưu hàm động học cùng giá trị \(L\) đo được vào cùng một tệp Python để sẵn sàng import cho cầu nối ở Chương 10.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Ghi chú mong đợi&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;L_mean = 0.158 m
lệnh bằng nhau: cả hai bánh tiến
v=0, w&amp;gt;0: phải tiến, trái lùi
R dự đoán = 0.40 m, R đo = 0.45 m  (trượt, đừng &quot;sửa&quot; bằng hệ số ngẫu nhiên)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Sai lệch bán kính khoảng 10% trên nền thảm là do trượt lốp, không phải lỗi công thức; nhưng nếu bánh trái quay sai chiều thì chắc chắn là lỗi dấu.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Khi hỏng thì thường vì&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Nguyên nhân hay gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot cua nhầm phía&lt;/td&gt;
      &lt;td&gt;Dấu \(\omega\), hoặc tuple trái/phải bị đảo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lệnh đi thẳng lại thành vòng&lt;/td&gt;
      &lt;td&gt;Thang PWM một bánh mềm hơn, hoặc một lốp nhỏ hơn. Động học không giấu được việc đó; PID thì có thể&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bán kính bằng một nửa dự đoán&lt;/td&gt;
      &lt;td&gt;Bạn đo chiều ngang tấm, hoặc dùng đường kính làm \(L\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cả hai bánh lùi khi cua trái&lt;/td&gt;
      &lt;td&gt;Bạn đặt \(v_L = v + \omega L/2\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Về sau tỉ lệ bản đồ vô nghĩa&lt;/td&gt;
      &lt;td&gt;\(L\) hoặc \(r\) đang là centimét trong công thức mét&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài này không cần mua thêm linh kiện điện tử; một thước dây 3 m (tìm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thước dây 3m&lt;/code&gt;, khoảng 15.000–40.000 đồng trên Shopee) là đủ. Nếu lốp bị méo hoặc xẹp không đều, nên thay đồng bộ cả cặp để bán kính \(r\) khớp nhau (tìm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh xe robot cao su 65mm&lt;/code&gt;, khoảng 20.000–60.000 đồng/chiếc). Không trộn hai lốp khác đường kính trên cùng một trục.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=2QE3LG5h2tw&quot;&gt;Differential Drive &amp;amp; Odometry Explained: How It Works&lt;/a&gt; — Netizen Robotics, tiếng Anh. Chuyển đổi từ vận tốc hai bánh sang vận tốc tiến và vận tốc góc — đúng cặp phương trình trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=eQ9E0Zvp9jw&quot;&gt;Modern Robotics, Chapter 13.4:  Odometry&lt;/a&gt; — Northwestern Robotics, tiếng Anh. Tích phân từng bước từ số đo bánh xe thành tư thế $(x, y, \psi)$.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Khổ vết đo được là 0,20 m và lệnh joystick yêu cầu \(v = 0\), \(\omega = -0,8\) rad/s. Tính \(v_L\), \(v_R\) và gọi tên chuyển động. Gợi ý: bánh phải âm, bánh trái dương — robot xoay phải tại chỗ (yaw âm).&lt;/li&gt;
  &lt;li&gt;Cả hai bánh đều chạy ở 0,3 m/s. Tính \(v\) và \(\omega\). Gợi ý: \(v = 0,3\) m/s, \(\omega = 0\) rad/s, không phụ thuộc vào \(L\).&lt;/li&gt;
  &lt;li&gt;Vì sao không tồn tại cặp \((v_L, v_R)\) nào thỏa mãn \(v_x = 0\), \(v_y = 0,2\) m/s và \(\omega = 0\)? Gợi ý: cấu trúc bánh vi sai chỉ tạo ra chuyển động dọc trục \(x\) và quay quanh tâm, nên \(v_y\) luôn bằng 0.&lt;/li&gt;
  &lt;li&gt;Bạn đo \(L = 15,5\) cm nhưng nhập &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.155&lt;/code&gt; ở một tệp và nhầm thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;15.5&lt;/code&gt; ở node cầu nối. Hiện tượng gì sẽ xảy ra với bán kính quay \(R\)? Gợi ý: bán kính quay thực tế nhỏ đi 100 lần (sai lệch 100 lần), khiến robot gần như xoay tại chỗ với hầu hết các lệnh rẽ.&lt;/li&gt;
  &lt;li&gt;Hàm động học này nằm ở vị trí nào trong chuỗi xử lý cùng với PID? Gợi ý: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; → hàm động học nghịch → hai tốc độ đặt (m/s) → hai vòng PID → hai mức PWM. Nếu bỏ qua PID, các giá trị m/s đầu ra chỉ mang tính ước lượng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103, đơn vị và quy ước hệ tọa độ&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; về hệ tọa độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; nơi các phương trình này được định nghĩa.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hades.mech.northwestern.edu/index.php/Modern_Robotics&quot;&gt;Modern Robotics, chương 13&lt;/a&gt; (bản mở) — tài liệu chuyên sâu về động học robot bánh xe.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; trong &lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/msg/Twist.html&quot;&gt;tài liệu message Jazzy&lt;/a&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; là \(v\), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; là \(\omega\), bốn trường còn lại bằng 0 trên khung vi sai.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Trực giác PID và chỉnh tham số</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_03_pid_tuning/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_03_pid_tuning</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~90 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Nếu phải chọn ra một thuật toán duy nhất đã âm thầm vận hành từ máy bay không người lái, cánh tay robot nhà máy cho tới nồi ủ nhiệt và hệ thống giữ làn ô tô suốt gần một thế kỷ qua, đó chắc chắn là &lt;strong&gt;bộ điều khiển PID (Proportional – Integral – Derivative)&lt;/strong&gt;. Điều làm PID vừa quyến rũ vừa đầy thử thách là: công thức của nó chỉ dài đúng một dòng, nhưng ba hệ số \(K_p, K_i, K_d\) lại đại diện cho ba “tính cách” điều khiển hoàn toàn khác nhau — nhìn vào &lt;strong&gt;hiện tại (P)&lt;/strong&gt;, nhớ lại &lt;strong&gt;quá khứ (I)&lt;/strong&gt;, và dự đoán &lt;strong&gt;tương lai (D)&lt;/strong&gt;. Chỉnh khéo thì robot bám quỹ đạo êm như lụa; chỉnh vụng thì xe rung bần bật và văng khỏi đường chạy!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-vai-trò-của-3-khâu-pid-và-hai-cạm-bẫy-thực-chiến&quot;&gt;Kiến thức cần nhớ: Vai trò của 3 khâu P–I–D và Hai cạm bẫy thực chiến&lt;/h2&gt;

&lt;h3 id=&quot;1-trực-giác-vật-lý-của-3-khâu-trong-pid-rời-rạc&quot;&gt;1. Trực giác vật lý của 3 khâu trong PID rời rạc&lt;/h3&gt;
&lt;p&gt;Với sai số tại bước \(k\) là \(e_k = r_k - y_k\) và chu kỳ lấy mẫu \(\Delta t\):
\(u_k = \underbrace{K_p\, e_k}_{\text{Hiện tại}} + \underbrace{K_i \sum_{j=1}^{k} e_j \Delta t}_{\text{Quá khứ}} + \underbrace{K_d\,\frac{e_k - e_{k-1}}{\Delta t}}_{\text{Tương lai}}.\)&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Khâu tỷ lệ (\(P\)):&lt;/strong&gt; sai số càng lớn, đẩy càng mạnh. Nhưng chỉ có \(P\) thường để lại &lt;strong&gt;sai số xác lập (steady-state error)&lt;/strong&gt; vì khi \(e_k \to 0\) thì lực đẩy cũng tiến về 0 không thắng nổi ma sát/trọng lực.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khâu tích phân (\(I\)):&lt;/strong&gt; gom dần sai số nhỏ kéo dài trong quá khứ để tạo đủ lực đẩy kéo sai số xác lập về đúng &lt;strong&gt;0&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khâu vi phân (\(D\)):&lt;/strong&gt; đóng vai trò &lt;strong&gt;giảm chấn (phanh sớm)&lt;/strong&gt; — khi thấy sai số đang giảm rất nhanh (tức robot đang lao nhanh về đích), đạo hàm mang dấu âm sẽ chủ động hãm lại để triệt tiêu hiện tượng &lt;strong&gt;vọt lố (overshoot)&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-hai-cạm-bẫy-kinh-điển-khi-viết-code-pid-thực-tế&quot;&gt;2. Hai cạm bẫy kinh điển khi viết code PID thực tế&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Bão hòa tích phân (Integral Windup):&lt;/strong&gt; khi bánh xe bị kẹt, sai số \(e_k\) giữ dương mãi khiến tổng tích phân phình to khổng lồ; đến khi hết kẹt, xe phóng điên cuồng rất lâu mới xả hết. &lt;em&gt;Cách sửa:&lt;/em&gt; luôn kẹp giới hạn (clamp) thành phần \(I\) hoặc ngưng tích phân khi đầu ra \(u_k\) đã chạm trần PWM!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khuếch đại nhiễu ở khâu D (Derivative Kick &amp;amp; Noise):&lt;/strong&gt; đạo hàm chia cho \(\Delta t\) rất nhỏ nên khuếch đại nhiễu encoder lên hàng chục lần. &lt;em&gt;Cách sửa:&lt;/em&gt; lọc thông thấp nhẹ cho khâu D và tính đạo hàm trên &lt;em&gt;giá trị đo&lt;/em&gt; \((-\frac{y_k - y_{k-1}}{\Delta t})\) thay vì trên sai số để tránh cú giật khi đổi mục tiêu đột ngột.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-quy-trình-chỉnh-tham-số-thủ-công-manual-tuning-an-toàn&quot;&gt;3. Quy trình chỉnh tham số thủ công (Manual Tuning) an toàn&lt;/h3&gt;
&lt;p&gt;Luôn đặt \(K_i = 0,\; K_d = 0\) trước. Tăng dần \(K_p\) cho tới khi hệ phản ứng nhanh và bắt đầu hơi vọt lố nhẹ → tăng \(K_d\) để dập tắt dao động vọt lố → cuối cùng mới nhích nhẹ \(K_i\) để xóa sạch sai số tĩnh còn sót lại.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-chạy-3-bước-pid-rời-rạc-để-thấy-khâu-d-đạp-phanh-sớm-chống-vọt-lố&quot;&gt;Giải thích: Cầm tay chạy 3 bước PID rời rạc để thấy khâu D “đạp phanh sớm” chống vọt lố&lt;/h3&gt;

&lt;p&gt;Xét một vòng điều khiển tốc độ chạy ở chu kỳ &lt;strong&gt;\(\Delta t = 0{,}1\text{ s}\)&lt;/strong&gt; (10 Hz) với mục tiêu &lt;strong&gt;\(r = 10{,}0\)&lt;/strong&gt; và bộ tham số &lt;strong&gt;\(K_p = 2{,}0,\; K_i = 1{,}0,\; K_d = 0{,}1\)&lt;/strong&gt;. Giả sử ban đầu (\(k = 0\)) xe đứng yên: \(y_0 = 0{,}0 \implies e_0 = 10{,}0\), tích phân ban đầu \(I_0 = 0{,}0\).&lt;/p&gt;

&lt;p&gt;Nay ta theo dõi 3 bước liên tiếp khi tốc độ đo được tiến nhanh về đích: \(y_1 = 4{,}0\), \(y_2 = 8{,}0\), \(y_3 = 9{,}5\):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Bước \(k\)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Đo \(y_k\)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Sai số \(e_k = 10 - y_k\)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Khâu \(P = 2 e_k\)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Khâu \(I_k = I_{k-1} + 1 \cdot e_k (0{,}1)\)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Khâu \(D = 0{,}1 \frac{e_k - e_{k-1}}{0{,}1}\)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Tổng lệnh \(u_k = P + I + D\)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;1&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(4{,}0\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(6{,}0\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(12{,}0\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0 + 0{,}6 = 0{,}60\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}1 \times \frac{6 - 10}{0{,}1} = -4{,}0\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(12{,}0 + 0{,}60 - 4{,}0 = \mathbf{8{,}60}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;2&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(8{,}0\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(2{,}0\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(4{,}0\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}60 + 0{,}20 = 0{,}80\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}1 \times \frac{2 - 6}{0{,}1} = -4{,}0\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(4{,}0 + 0{,}80 - 4{,}0 = \mathbf{0{,}80}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(9{,}5\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}5\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(1{,}0\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}80 + 0{,}05 = 0{,}85\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(0{,}1 \times \frac{0{,}5 - 2}{0{,}1} = -1{,}5\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(1{,}0 + 0{,}85 - 1{,}5 = \mathbf{0{,}35}\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Hãy dừng lại ngắm hàng &lt;strong&gt;Bước 2&lt;/strong&gt;: dù xe vẫn còn cách đích \(2{,}0\) đơn vị (nên khâu \(P\) vẫn đòi đẩy lực \(+4{,}0\)), nhưng vì tốc độ đang lao tới rất nhanh (tăng vọt từ \(4{,}0\) lên \(8{,}0\)), khâu &lt;strong&gt;\(D = -4{,}0\)&lt;/strong&gt; lập tức triệt tiêu lực đẩy của \(P\), hạ tổng lệnh \(u_2\) xuống êm ái còn \(0{,}80\) để xe hạ cánh mượt mà về đích \(10{,}0\) mà &lt;strong&gt;không bị vọt lố&lt;/strong&gt;! Đồng thời, khi \(e_k \to 0\) ở bước 3, chính khâu tích phân \(I_3 = 0{,}85\) đứng ra giữ lực duy trì giúp xe không tụt tốc độ do ma sát!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn lập trình được một bước cập nhật PID rời rạc cho vòng điều khiển tốc độ bánh xe dựa trên xung encoder, và xác định chính xác dấu của từng thành phần P, I, D khi có bước nhảy giá trị đặt (setpoint). Bạn thực hành tinh chỉnh tham số theo trình tự P → D → I khi kê bánh trên không, áp dụng giới hạn khâu tích phân (anti-windup) để bánh không vọt mạnh khi bị kẹt, và ghi nhật ký setpoint, tốc độ đo cùng tín hiệu điều khiển để chẩn đoán nguyên nhân nếu hệ thống dao động hoặc không đạt đích.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Tín hiệu đếm xung encoder từ Chương 04 đã hoạt động tin cậy để quy đổi độ biến thiên số xung sang vận tốc, và mạch driver Chương 05 nhận lệnh PWM có dấu (hoặc thang −100…100). Vòng PID cần chạy theo chu kỳ cố định \(\Delta t\) đã biết (sẽ đo kiểm ở Bài 08-05), không gọi tùy tiện theo nhịp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt; không đều. Về mặt toán học, bạn chỉ cần nắm trực giác về “tốc độ thay đổi” (đạo hàm) và “tổng tích lũy theo thời gian” (tích phân).&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Điều khiển PWM vòng hở ở Chương 07 chỉ đủ dùng để lái thử bằng tay trên sàn phẳng khi pin còn đầy. Khi điện áp pin giảm, một bánh đi vào bề mặt ma sát cao hoặc Nav2 yêu cầu 0,20 m/s nhưng xe chỉ chạy 0,11 m/s, vòng hở không tự bù được. Hơn nữa, hai động cơ cùng loại vốn luôn có sai lệch đặc tính nhất định: &lt;a href=&quot;/contents/vi/chapter04/04_03_encoders_counts/&quot;&gt;bài 04-03&lt;/a&gt; cho thấy cùng một mức PWM nhưng số xung hai bánh vẫn lệch nhau vài phần trăm, khiến odometry ở Chương 11 tích lũy thành bản đồ bị cong. Các chính sách điều khiển ở Chương 12 cũng mặc định bánh xe bám sát vận tốc đặt. Vòng PID tốc độ bánh chính là lớp điều khiển nền nằm ngay bên dưới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; — hãy làm chủ bài toán giữ tốc độ một bánh trước khi nghĩ đến các hệ phức tạp hơn như xe tự cân bằng.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_pid_step.png&quot; alt=&quot;P, rồi D, rồi I trên một bước nhảy&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Sai số điều khiển là hiệu giữa giá trị đặt (setpoint) và giá trị đo được, bắt buộc cùng đơn vị ở cả hai vế. Nếu vận tốc đặt ở vành bánh là 0,30 m/s và encoder đo được 0,20 m/s thì \(e = 0,10\) m/s (sai số dương nghĩa là cần chạy nhanh hơn về phía trước). Nếu đảo ngược dấu sai số, bộ điều khiển sẽ đẩy tốc độ trôi ra xa mục tiêu — luôn kiểm tra đúng dấu trước khi chỉnh hệ số.&lt;/p&gt;

\[e(t) = r(t) - y(t)\]

\[u(t) = K_p e(t) + K_i \int_0^{t} e(\tau)\, d\tau + K_d \frac{de}{dt}\]

&lt;p&gt;Trong đó \(u\) là lệnh gửi xuống driver (ví dụ thang −100…100) và phải được giới hạn bão hòa trong đúng dải đó. Ba hệ số PID đều mang thứ nguyên cụ thể: chẳng hạn \(K_p\) có đơn vị “mức lệnh trên mỗi (m/s)”, nên việc sao chép một giá trị \(K_p\) không thứ nguyên từ bài hướng dẫn điều khiển góc hay nhiệt độ sang robot này sẽ không có ý nghĩa.&lt;/p&gt;

&lt;p&gt;Trên vi điều khiển, tích phân và đạo hàm được rời rạc hóa thành tổng tích lũy và sai phân trên chu kỳ \(\Delta t\):&lt;/p&gt;

\[I_k = \mathrm{clamp}\big(I_{k-1} + e_k \Delta t,\; -I_{\max},\; I_{\max}\big)\]

\[u_k = \mathrm{sat}\Big(K_p e_k + K_i I_k + K_d \frac{e_k - e_{k-1}}{\Delta t}\Big)\]

&lt;p&gt;Hàm \(\mathrm{sat}\) kẹp tín hiệu \(u\) trong ngưỡng chịu tải của driver, còn phép kẹp \(I\) trong \(\pm I_{\max}\) đóng vai trò chống bão hòa tích phân (anti-windup). Nếu không kẹp \(I\), chỉ cần giữ tay làm kẹt bánh trong 2 giây, khâu tích phân sẽ phình rất lớn và khiến bánh giật mạnh lên mức cực đại ngay khi thả tay. Ngoài việc kẹp \(I\), bạn cũng có thể áp dụng tích phân có điều kiện (đóng băng \(I\) khi \(u\) đã chạm biên bão hòa và sai số vẫn cùng dấu); hãy triển khai ít nhất một trong hai cơ chế và ghi rõ vào nhật ký.&lt;/p&gt;

&lt;p&gt;Thành phần đạo hàm trên sai số có thể tạo xung nhọn khi setpoint thay đổi đột ngột (dù bánh xe chưa kịp đổi tốc độ). Biến thể mượt hơn là lấy đạo hàm trên giá trị đo kèm dấu trừ (\(-K_d \frac{y_k - y_{k-1}}{\Delta t}\)) để tránh cú giật khi đổi setpoint. Trong lần tinh chỉnh đầu tiên với bước nhảy vừa phải, dùng \(K_d\) nhỏ trên sai số vẫn chấp nhận được.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trình tự chỉnh tham số (bánh xe kê trên không):&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Đặt \(K_i = 0\) và \(K_d = 0\). Tăng dần \(K_p\) cho tới khi đáp ứng bước nhảy từ 0 lên 0,3 m/s tăng nhanh, bắt đầu có chút vọt lố hoặc tiếng rè nhẹ, sau đó lùi \(K_p\) lại một nấc.&lt;/li&gt;
  &lt;li&gt;Tăng dần \(K_d\) để giảm vọt lố mà không làm tăng tiếng rè. Vì nhiễu xung encoder bị khuếch đại mạnh bởi \(K_d\), nếu nghe tiếng rít PWM khi bánh đứng yên thì \(K_d\) đang quá lớn hoặc phép đo tốc độ quá thô (nên lấy trung bình 2–3 cửa sổ đếm trước khi tăng D).&lt;/li&gt;
  &lt;li&gt;Thêm dần \(K_i\) để triệt tiêu sai số xác lập (do sụt áp pin hoặc ma sát) trong khoảng 1–2 giây. Nếu bánh dao động chậm theo chu kỳ cỡ 1 giây, giảm một nửa \(K_i\).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Phương pháp Ziegler–Nichols trong giáo trình đòi hỏi đẩy hệ tới sát ngưỡng mất ổn định, không nên áp dụng trực tiếp trên động cơ giảm tốc gắn khung robot; hãy tuân thủ trình tự tinh chỉnh thủ công trên.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Xét một chu kỳ cập nhật với \(\Delta t = 0,02\) s (50 Hz), setpoint \(r = 0,30\) m/s, tốc độ đo \(y_k = 0,18\) m/s (nên \(e_k = 0,12\)), sai số bước trước \(e_{k-1} = 0,20\) và tích phân tích lũy \(I_{k-1} = 0,04\). Các hệ số là \(K_p = 80\), \(K_i = 40\), \(K_d = 0,5\), giới hạn lệnh ±100 và \(I_{\max} = 0,5\):&lt;/p&gt;

\[I_k = 0,04 + 0,12 \times 0,02 = 0,0424\]

\[P = 80 \times 0,12 = 9,6\]

\[I_{\text{term}} = 40 \times 0,0424 = 1,696\]

\[D = 0,5 \times \frac{0,12 - 0,20}{0,02} = 0,5 \times (-4) = -2,0\]

\[u = 9,6 + 1,696 - 2,0 = 9,3\]

&lt;p&gt;Thành phần D mang dấu âm vì sai số đang thu hẹp nhanh: bánh đang đuổi kịp setpoint nên khâu D chủ động hãm bớt lệnh để giảm vọt lố. Nếu log cho thấy D dương khi sai số đang giảm, công thức tính hiệu đang bị đảo dấu. Khi xuất ra PWM số nguyên, giá trị 9,3 được làm tròn thành 9 nhưng vẫn nên lưu số thực trong nhật ký.&lt;/p&gt;

&lt;p&gt;Kiểm tra thêm trường hợp vượt đích (\(e &amp;lt; 0\)): tín hiệu \(u\) phải giảm xuống (hoặc đảo sang âm). Nếu cả sai số dương lẫn âm đều làm PWM tăng lên, chiều đếm encoder đang ngược với chiều tiến của driver — hãy sửa dấu phần cứng hoặc phần mềm trước khi chỉnh hệ số (tránh đảo đồng thời cả dây động cơ lẫn dây encoder vì hai lần đảo sẽ triệt tiêu nhau).&lt;/p&gt;

&lt;h2 id=&quot;suy-ra-dạng-rời-rạc&quot;&gt;Suy ra dạng rời rạc&lt;/h2&gt;

&lt;p&gt;Trên vi điều khiển không có phép tích phân liên tục. Trong một chu kỳ \(\Delta t\) đủ nhỏ mà sai số \(e\) gần như không đổi, diện tích dưới đường cong sai số được xấp xỉ bằng hình chữ nhật:&lt;/p&gt;

\[\int_{t_{k-1}}^{t_k} e(\tau)\,d\tau \approx e_k \Delta t\]

&lt;p&gt;và cộng dồn vào biến $I$. Đạo hàm được xấp xỉ bằng độ dốc giữa hai mẫu liên tiếp:&lt;/p&gt;

\[\frac{de}{dt}\approx \frac{e_k - e_{k-1}}{\Delta t}\]

&lt;p&gt;Nếu quên nhân $\Delta t$ ở khâu I, thành phần tích phân sẽ bị phóng đại gấp $1/\Delta t$ lần (50 lần ở tần số 50 Hz); nếu quên chia $\Delta t$ ở khâu D, thành phần đạo hàm sẽ bị thu nhỏ đúng bằng hệ số đó. Hai lỗi lập trình này là nguyên nhân phổ biến khiến các bộ hệ số tham khảo trên mạng không chạy được trên robot của bạn.&lt;/p&gt;

&lt;p&gt;Mô hình hệ thống (plant) trong hình minh họa dưới đây là một hệ bậc hai chuẩn có nhiễu lệch tải $b$ (dùng để minh họa bài học, không phải thông số đo thực tế của động cơ TT), đủ để bộ điều khiển P thuần còn sai số xác lập và dễ vọt lố khi $K_p$ lớn:&lt;/p&gt;

\[\ddot y + 2\zeta\omega_n \dot y + \omega_n^2 y = \omega_n^2 (u - b)\]

&lt;p&gt;với $\omega_n = 8~\mathrm{rad/s}$, $\zeta = 0{,}9$, $b = 0{,}08$, setpoint $r = 0{,}25$ m/s và $u$ bão hòa trong $\pm 2$. Trên robot thật, bạn vẫn phải tinh chỉnh lại các hệ số trên bánh xe thực tế.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_pid_step_response.png&quot; alt=&quot;P vọt lố, PD đỡ vọt, PID kéo về setpoint dù có lệch tải&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Kết quả mô phỏng ba bộ điều khiển bằng phương pháp tích phân Euler với bước thời gian $5$ ms và giới hạn $I$ trong $\pm 1$:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Luật&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;$K_p$&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;$K_i$&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;$K_d$&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Đỉnh $y$&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;$y$ lúc $t = 6$ s&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;P&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;8&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0,291&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0,213&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;PD&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;8&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0,15&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0,246&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0,213&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;PID&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;4&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0,08&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0,245&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0,245&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Có thể thấy bộ P vọt vượt 0,25 rồi dừng ở 0,213 do không bù được lệch tải $b$; bộ PD triệt tiêu vọt lố nhưng vẫn còn nguyên sai số xác lập; còn bộ PID kéo đầu ra tiến sát 0,25. Hãy chạy lại đoạn mã sau để kiểm chứng (nếu đỉnh của đường P lệch quá 0,01 so với 0,291 thì bước thời gian hoặc phương trình mô phỏng đã bị nhập sai):&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zeta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.005&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;8.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.08&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;6.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ki&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;e_prev&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;peak&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ki&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e_prev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;yd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zeta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yd&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;e_prev&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;peak&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peak&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;peak&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gains&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;PD&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;PID&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.08&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;peak&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peak&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Kê bánh xe rời khỏi mặt đất, cấp nguồn pin đầy đủ như Chương 07 và đảm bảo nguồn logic driver, nguồn động cơ cùng vi điều khiển nối chung mass.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;In biến đếm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; của encoder ở tần số 10 Hz và xoay tay bánh xe đúng một vòng; xác nhận dấu đếm tăng cùng chiều tiến đã quy ước.&lt;/li&gt;
  &lt;li&gt;Quy đổi sang vận tốc dài vành bánh: \(y = \Delta \mathrm{counts} \times \frac{2\pi r}{N \Delta t}\), với \(r\) là bán kính bánh (mét) và \(N\) là số xung trên mỗi vòng bánh xe. Xoay tay chậm để kiểm tra giá trị \(y\) hợp lý (vài phần mười m/s, không phải vài chục m/s).&lt;/li&gt;
  &lt;li&gt;Chạy thử bước nhảy setpoint từ 0 lên 0,25 m/s trong 2 giây rồi về 0 với \(K_i = K_d = 0\).&lt;/li&gt;
  &lt;li&gt;Xuất dữ liệu dạng CSV: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms,r,y,e,P,I,D,u&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Lần lượt thêm khâu D rồi khâu I theo đúng trình tự tinh chỉnh. Dừng ngay nếu hộp số phát tiếng kêu bất thường.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Hình dạng log mong đợi&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;t_ms,r,y,u
0,0.25,0.00,20
200,0.25,0.16,14
800,0.25,0.24,8
2000,0.00,0.22,-15
2400,0.00,0.02,0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Con số cụ thể trên xe bạn sẽ khác, nhưng quy luật phải đúng: \(u\) dương khi \(y &amp;lt; r\), \(y\) tiến sát \(r\) trước khi kết thúc bước nhảy, \(u\) đổi dấu khi setpoint giảm về 0, và \(u\) gần bằng 0 khi bánh đứng yên. Thử dùng tay giữ nhẹ bánh 1 giây rồi thả ra: bánh không được giật vọt lên toàn thang (nếu giật mạnh, khâu \(I\) chưa được kẹp chống windup).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Khi hỏng thì thường vì&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Nguyên nhân hay gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh bỏ chạy ngay khi bật PID&lt;/td&gt;
      &lt;td&gt;Sai dấu sai số, hoặc encoder ngược chiều động cơ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Rè đều ở đích&lt;/td&gt;
      &lt;td&gt;\(K_p\) hoặc \(K_d\) quá cao, hoặc \(\Delta t\) bị giật vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt; mỗi vòng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Không tới setpoint, \(u\) kẹt ở 40&lt;/td&gt;
      &lt;td&gt;Không có I và P bị bão hòa bởi hệ số thấp trước ma sát. Bánh trên không thì ma sát nhỏ; nếu trên không vẫn cần \(u\) rất lớn, đơn vị tốc độ đang sai&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trên không thì ổn, trên thảm thì bò&lt;/td&gt;
      &lt;td&gt;Đó là tải, không phải PID hỏng. Tăng I nhẹ hoặc chấp nhận tốc độ tối đa thấp hơn. Đừng chép \(K_p\) trên không xuống sàn mà không ghi log mới&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tích phân nổ khi bạn giữ bánh&lt;/td&gt;
      &lt;td&gt;Thiếu \(I_{\max}\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Nếu khung Capstone A đã lắp sẵn động cơ giảm tốc kèm encoder thì không cần mua thêm. Nếu đang dùng động cơ TT không encoder, bạn cần bổ sung cặp động cơ có encoder mới thực hành được vòng kín.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Cửa hàng&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá 2026&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ giảm tốc JGA25 hoặc TT có encoder Hall, mua cặp cùng loại&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; tìm “encoder”&lt;/td&gt;
      &lt;td&gt;Shopee/Lazada: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ giảm tốc encoder Hall JGA25&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;khoảng 90.000–250.000 đồng mỗi cái&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Driver kép TB6612 nếu sụt áp L298N quá lớn với pack 2S&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; tìm “TB6612”&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;module TB6612FNG&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;khoảng 30.000–90.000 đồng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MPU6050 không cần cho PID bánh&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;GY-521 trên Hshop&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;khoảng 30.000–70.000 đồng, để sau cho góc yaw&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Lưu ý mức điện áp logic của encoder phải tương thích với vi điều khiển: nhiều mạch cảm biến Hall cấp nguồn VCC 5 V nhưng ngõ ra xung chịu được kéo lên 3,3 V — hãy kiểm tra kỹ thông số mạch trước khi nối vào chân 3,3 V của ESP32 hoặc Pico. Tránh dùng các loại “cảm biến tốc độ” chỉ cho 1 xung mỗi vòng vì quá thô cho vòng điều khiển 50 Hz.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=UR0hOmjaHp0&quot;&gt;PID Control - A brief introduction&lt;/a&gt; — Brian Douglas, tiếng Anh. Giới thiệu vai trò ba thành phần P, I, D trong hệ vòng kín; nên xem kỹ phần đáp ứng bước nhảy trước khi thay đổi tham số mô phỏng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=XfAt6hNV8XM&quot;&gt;Simple Examples of PID Control&lt;/a&gt; — Brian Douglas, tiếng Anh. Ba ví dụ số trực quan để đối chiếu độ vọt lố và sai số xác lập với bảng P, PD, PID trong bài.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Video ngắn (Facebook Reel) dưới đây của Hoàng Quân (Cơ điện tử ICTU, &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/976436878550867&quot;&gt;Flight controller, muốn bay ngay&lt;/a&gt; — Chú thích: phát triển flight controller lâu, sốt ruột, muốn bay ngay. Bộ điều khiển bay là một hệ PID nhạy cảm; khi đáp ứng bước nhảy vẫn còn vọt lố mạnh thì chưa thể cho cất cánh.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tính lại ví dụ trong bài với \(e_k = -0,05\) và \(e_{k-1} = 0,12\). Dấu của tín hiệu \(u\) sẽ là gì khi thành phần \(I\) còn nhỏ? Gợi ý: cả P lẫn D đều âm vì sai số đã vượt qua 0 về phía âm, nên \(u\) sẽ ra lệnh giảm tốc hoặc đảo chiều.&lt;/li&gt;
  &lt;li&gt;Dữ liệu CSV cho thấy tốc độ đo \(y\) kẹt ở 0 nhưng tín hiệu \(u\) tăng dần tới 100 rồi nằm im ở biên. Bộ phận nào đang thiếu giới hạn? Gợi ý: khâu tích phân (anti-windup); nếu chỉ có P thuần thì \(u = K_p e\) là hằng số khi \(y\) không đổi.&lt;/li&gt;
  &lt;li&gt;Vì sao vòng PID tốc độ bánh đơn lẻ không phải công cụ phù hợp cho mục tiêu “chạy chính xác 1,00 m dọc hành lang”? Gợi ý: đó là bài toán vòng vị trí bên ngoài — PID tốc độ chỉ điều khiển tốc độ vành bánh, còn đi đúng quãng đường và hướng đòi hỏi tích phân quãng đường và phối hợp cả hai bánh.&lt;/li&gt;
  &lt;li&gt;Một bài viết trên mạng dùng \(K_p = 2\) để điều khiển lò nhiệt tính bằng độ C. Thí nghiệm nào thay thế cho việc sao chép con số đó sang robot? Gợi ý: thí nghiệm đáp ứng bước nhảy khi kê bánh trên không, tăng dần \(K_p\) từ mức nhỏ cho tới khi sườn lên dứt khoát.&lt;/li&gt;
  &lt;li&gt;Viết một câu chốt giao ước giữa bài này và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;: tầng nào xuất vận tốc m/s, tầng nào xuất PWM? Gợi ý: khối động học (Bài 08-04) xuất vận tốc đặt cho hai bánh theo m/s, vòng PID bài này chuyển đổi sang PWM; node cầu nối không được bỏ qua tầng giữa.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ni.com/en/shop/labview/pid-theory-explained.html&quot;&gt;Bài PID của NI&lt;/a&gt; — giải thích trực quan ba thành phần P, I, D.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://fbswiki.org/wiki/index.php/Feedback_Systems&quot;&gt;Åström và Murray, Feedback Systems&lt;/a&gt; — giáo trình mở chuyên sâu về điều khiển phản hồi và thiết kế anti-windup (tham khảo Chương 10 khi muốn đào sâu).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://controlguru.com/table-of-contents/&quot;&gt;Mục lục Control Guru&lt;/a&gt; — tài liệu kỹ thuật điều khiển công nghiệp cũng bắt đầu từ thực nghiệm đáp ứng bước nhảy.&lt;/li&gt;
  &lt;li&gt;Bài đếm xung encoder ở Chương 04 — nguồn đo trực tiếp quyết định chất lượng vòng lặp PID này.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab: Wi-Fi và MQTT trên ESP32</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_02_wifi_mqtt/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_02_wifi_mqtt</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~90 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Rút sợi cáp USB ra khỏi robot và điều khiển nó chạy khắp phòng qua Wi-Fi luôn là khoảnh khắc đầy phấn khích. Nhưng sóng Wi-Fi khác xa dây đồng: gói tin có thể đến chậm, đến sai thứ tự hoặc biến mất vài trăm mili-giây khi robot đi qua góc khuất cột bê tông. Để kết nối nhiều robot, cảm biến và bảng điều khiển web mà không phải tự viết lại máy chủ socket dễ vỡ, giới IoT và robotics dùng giao thức &lt;strong&gt;MQTT (Message Queuing Telemetry Transport)&lt;/strong&gt; — một mô hình xuất bản–đăng ký (Pub/Sub) siêu nhẹ giúp bạn làm quen đúng tư duy &lt;strong&gt;topic&lt;/strong&gt; ngay trước khi bước sang hệ sinh thái ROS 2!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kiến-trúc-pubsub-các-mức-qos-và-last-will--testament&quot;&gt;Kiến thức cần nhớ: Kiến trúc Pub/Sub, Các mức QoS và Last Will &amp;amp; Testament&lt;/h2&gt;

&lt;h3 id=&quot;1-mô-hình-broker--publisher--subscriber-tách-rời-không-gian-và-thời-gian&quot;&gt;1. Mô hình Broker – Publisher – Subscriber (Tách rời không gian và thời gian)&lt;/h3&gt;
&lt;p&gt;Thay vì để ESP32 phải nhớ địa chỉ IP của từng chiếc laptop hay điện thoại muốn xem dữ liệu, mọi thiết bị chỉ kết nối tới một trạm trung chuyển gọi là &lt;strong&gt;MQTT Broker&lt;/strong&gt; (ví dụ Mosquitto). ESP32 &lt;em&gt;xuất bản (publish)&lt;/em&gt; dữ liệu lên các &lt;strong&gt;topic&lt;/strong&gt; phân cấp như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot01/telemetry/battery&lt;/code&gt;, còn bất kỳ dashboard hay script nào quan tâm chỉ việc &lt;em&gt;đăng ký (subscribe)&lt;/em&gt; đúng topic đó (hoặc dùng ký tự đại diện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot01/#&lt;/code&gt;).&lt;/p&gt;

&lt;h3 id=&quot;2-ba-mức-chất-lượng-dịch-vụ-qos-0-1-2--chọn-đúng-theo-loại-dữ-liệu&quot;&gt;2. Ba mức Chất lượng dịch vụ (QoS 0, 1, 2) — chọn đúng theo loại dữ liệu&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;QoS 0 (At most once — gửi và quên):&lt;/strong&gt; 1 gói TCP, nhanh nhất, độ trễ thấp nhất — lý tưởng cho dữ liệu cảm biến tốc độ cao (nếu mất 1 mẫu, 50 ms sau đã có mẫu mới!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;QoS 1 (At least once — gửi có xác nhận PUBACK):&lt;/strong&gt; 2 bước bắt tay, đảm bảo gói tới đích nhưng có thể lặp — lý tưởng cho lệnh rời rạc quan trọng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STOP&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;START_MISSION&lt;/code&gt;, yêu cầu bên nhận xử lý lũy đẳng).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;QoS 2 (Exactly once — bắt tay 4 bước):&lt;/strong&gt; quá chậm cho điều khiển thời gian thực, hiếm dùng trong robot di động.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-giữ-lại-trạng-thái-retained-message-và-di-chúc-cuối-cùng-last-will--testament--lwt&quot;&gt;3. Giữ lại trạng thái (Retained Message) và Di chúc cuối cùng (Last Will &amp;amp; Testament — LWT)&lt;/h3&gt;
&lt;p&gt;Tính năng &lt;strong&gt;LWT&lt;/strong&gt; cực kỳ hữu ích cho an toàn robot: khi ESP32 kết nối Broker, nó đăng ký sẵn một “bản di chúc” (ví dụ: gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;offline&lt;/code&gt; lên topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot01/status&lt;/code&gt;). Nếu robot bất ngờ hết pin hoặc sập nguồn mà không kịp chào tạm biệt, Broker tự động phát thông báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;offline&lt;/code&gt; cho toàn bộ màn hình giám sát sau chu kỳ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keepalive&lt;/code&gt;!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-băng-thông-telemetry-mqtt-và-chi-phí-độ-trễ-giữa-qos-0-với-qos-1&quot;&gt;Giải thích: Cầm tay tính băng thông Telemetry MQTT và chi phí độ trễ giữa QoS 0 với QoS 1&lt;/h3&gt;

&lt;p&gt;Giả sử một robot ESP32 gửi bản tin trạng thái JSON dài &lt;strong&gt;100 byte&lt;/strong&gt; lên MQTT Broker ở tần số &lt;strong&gt;10 Hz&lt;/strong&gt; (10 bản tin/giây) qua mạng Wi-Fi nội bộ có thời gian khứ hồi trung bình &lt;strong&gt;\(\mathrm{RTT} = 25\text{ ms}\)&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Lưu lượng dữ liệu telemetry thuần:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Mỗi giây: \(100\text{ B} \times 10 = 1\,000\text{ B/s} = 8\text{ kbps}\) (rất nhỏ so với băng thông Wi-Fi hàng chục Mbps!).&lt;/li&gt;
      &lt;li&gt;Sau 1 giờ chạy liên tục (\(3\,600\text{ s}\)): \(1\,000 \times 3\,600 = 3{,}6\text{ MB/giờ}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;So sánh độ trễ và số gói mạng giữa QoS 0 và QoS 1:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Với QoS 0 (cho dòng telemetry 10 Hz):&lt;/strong&gt;
Mỗi bản tin chỉ đi &lt;strong&gt;1 chiều&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PUBLISH&lt;/code&gt;) → thời gian tới Broker \(\approx \mathrm{RTT}/2 = 12{,}5\text{ ms}\), tổng cộng &lt;strong&gt;10 gói/giây&lt;/strong&gt;. Nếu Wi-Fi nghẽn nhẹ, không có hàng đợi xác nhận bị dồn ứ.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Với QoS 1 (cho lệnh khẩn cấp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;E-STOP&lt;/code&gt;):&lt;/strong&gt;
Mỗi lệnh cần &lt;strong&gt;2 bước&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PUBLISH&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PUBACK&lt;/code&gt;) → tăng gấp đôi số gói mạng (20 gói/giây nếu lạm dụng cho telemetry) và tốn ít nhất &lt;strong&gt;\(25\text{ ms}\)&lt;/strong&gt; để bên gửi nhận được xác nhận chắc chắn lệnh đã tới Broker.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Quy tắc thiết kế sạch đẹp: dùng &lt;strong&gt;QoS 0&lt;/strong&gt; cho luồng cảm biến liên tục tốc độ cao, và dùng &lt;strong&gt;QoS 1&lt;/strong&gt; kết hợp &lt;strong&gt;LWT&lt;/strong&gt; cho các lệnh điều khiển trạng thái và dừng khẩn cấp!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn cài đặt và chạy được broker Mosquitto trên laptop, xuất bản lệnh vận tốc lên một topic riêng và nhận dữ liệu telemetry từ ESP32 trên cùng mạng LAN 2,4 GHz. Bạn phân biệt rõ sự khác nhau giữa QoS 0 và QoS 1, giải thích được rủi ro cụ thể của cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retain&lt;/code&gt; (lệnh “chạy” bị lưu giữ làm robot tự chạy ngay khi khởi động lại), đồng thời duy trì cơ chế timeout để động cơ luôn dừng an toàn khi mất kết nối Wi-Fi. Nhật ký thực hành cần ghi nhận tối thiểu một lệnh JSON nhận thành công và một lần watchdog dừng động cơ.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Bạn đã nắm cấu trúc khung và cơ chế timeout từ Bài 08-01, biết kết nối ESP32 vào mạng Wi-Fi 2,4 GHz và đọc địa chỉ IP cục bộ. Hệ thống động cơ từ Chương 07 vẫn phải kê bánh xe rời khỏi mặt đất. Lưu ý rằng Raspberry Pi Pico bản thường không có Wi-Fi; Pico W có thể làm được, nhưng hướng dẫn dưới đây dùng Arduino / PlatformIO trên ESP32 vì đây là bo mạch phổ biến nhất trong các bộ kit Capstone tại Việt Nam.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;ROS 2 không sử dụng giao thức MQTT, nhưng tư duy thiết kế truyền thông hoàn toàn tương đồng: tên topic là giao ước dữ liệu, lệnh cũ bị lưu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retain&lt;/code&gt; tiềm ẩn nguy cơ mất an toàn, và mọi kênh không dây đều phải có phanh tự động (watchdog) khi rớt mạng. Khi lên tới Nav2, topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; cũng có thể bị gián đoạn ở góc khuất sóng Wi-Fi; cơ chế watchdog bạn viết hôm nay tương đương với deadline của ROS 2 hoặc lớp cầu nối tự phát lệnh 0. Tương tự, ở Chương 12, dù chính sách học máy xuất ra cả chuỗi chuyển động, tầng firmware bên dưới vẫn bắt buộc phải nhả lực ngay khi mất liên kết.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_mqtt_robot.png&quot; alt=&quot;Topic MQTT và đường serial dự phòng&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Giao thức MQTT gồm ba thành phần: &lt;strong&gt;Broker&lt;/strong&gt; (máy chủ trung gian nhận kết nối TCP và phân phối bản tin), &lt;strong&gt;Publisher&lt;/strong&gt; (bên gửi dữ liệu lên một tên topic) và &lt;strong&gt;Subscriber&lt;/strong&gt; (bên đăng ký nhận dữ liệu từ các topic quan tâm). Trong bài lab này, broker là Mosquitto chạy trên laptop trong mạng LAN nội bộ tại cổng 1883 — không mở cổng ra internet và không cấu hình chuyển tiếp cổng (port forwarding) 1883 trên router.&lt;/p&gt;

&lt;p&gt;Cấu trúc topic cho robot Capstone:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;capstone/cmd      {&quot;v&quot;: 0.20, &quot;w&quot;: 0.00}
capstone/telem    {&quot;vl&quot;: 0.18, &quot;vr&quot;: 0.21, &quot;batt&quot;: 11.6, &quot;age_ms&quot;: 40}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trong đó &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt; là vận tốc tịnh tiến (m/s) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt; là vận tốc góc quay (rad/s), khớp hoàn toàn với ý nghĩa của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; trong ROS 2. Bên trong firmware ESP32, các giá trị này có thể được quy đổi sang thang −100…100 bằng một hàm duy nhất để giữ đồng bộ với cầu nối ở Chương 10.&lt;/p&gt;

&lt;p&gt;Mức chất lượng dịch vụ (&lt;strong&gt;QoS&lt;/strong&gt;) của MQTT quy định cam kết chuyển phát giữa client và broker:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;QoS 0&lt;/strong&gt; (nhiều nhất một lần): phù hợp với dữ liệu telemetry gửi lặp liên tục mỗi 100 ms.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;QoS 1&lt;/strong&gt; (ít nhất một lần): broker có thể gửi trùng lặp bản tin, do đó hàm xử lý lệnh phải có tính lũy đẳng (áp dụng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v = 0.2&lt;/code&gt; hai lần liên tiếp không gây lỗi, khác với thao tác tăng biến đếm hai lần).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;QoS 2&lt;/strong&gt; (đúng một lần): phát sinh nhiều bước bắt tay không cần thiết cho bài toán robot này.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cờ &lt;strong&gt;retain&lt;/strong&gt; là bẫy an toàn rất phổ biến. Một bản tin gửi lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone/cmd&lt;/code&gt; có bật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retain&lt;/code&gt; sẽ được broker lưu trữ và tự động đẩy xuống cho bất kỳ subscriber nào vừa kết nối. Nếu bản tin cuối cùng bị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retain&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;v&quot;: 0.3, &quot;w&quot;: 0.0}&lt;/code&gt; và ESP32 khởi động lại, robot sẽ lập tức lao đi ngay khi vừa kết nối mạng. Vì vậy, với mọi topic lệnh điều khiển, luôn xuất bản với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retain = false&lt;/code&gt; và gửi lệnh dừng 0 tường minh khi kết thúc phiên chạy; chỉ nên dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retain&lt;/code&gt; cho các topic cấu hình tĩnh.&lt;/p&gt;

&lt;p&gt;Một số giới hạn phần cứng cần lưu ý: ESP32 DevKit đời cũ chỉ hỗ trợ băng tần Wi-Fi 2,4 GHz (cố kết nối vào SSID 5 GHz sẽ báo lỗi giống như sai mật khẩu). Ngoài ra, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MQTT_MAX_PACKET_SIZE&lt;/code&gt; mặc định trên các bản PubSubClient cũ chỉ là 256 byte, khiến chuỗi JSON quá dài bị lặng lẽ bỏ qua — hãy giữ bản tin telemetry thật gọn.&lt;/p&gt;

&lt;p&gt;Nguyên tắc firmware quan trọng hơn cả việc chọn thư viện: trong hàm callback MQTT, luôn cập nhật mốc thời gian &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last_cmd_ms&lt;/code&gt;; trong vòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt;, nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis() - last_cmd_ms &amp;gt; 300&lt;/code&gt; thì lập tức đưa lệnh động cơ về 0. Quá trình phân tích JSON hay hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;client.loop()&lt;/code&gt; (đặc biệt khi tự động kết nối lại) không được phép chặn vòng điều khiển động cơ hàng chục miligiây — ảnh hưởng của độ trễ này sẽ được đo chi tiết ở Bài 08-05.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Cài đặt Mosquitto và kiểm thử trên máy tính trước khi cấp nguồn cho ESP32:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt update
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; mosquitto mosquitto-clients
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl &lt;span class=&quot;nb&quot;&gt;enable&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--now&lt;/span&gt; mosquitto
ss &lt;span class=&quot;nt&quot;&gt;-ltnp&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;1883
mosquitto_sub &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; 127.0.0.1 &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;capstone/#&apos;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Mở một cửa sổ terminal thứ hai để gửi thử bản tin:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mosquitto_pub &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; 127.0.0.1 &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; capstone/cmd &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;{&quot;v&quot;:0.2,&quot;w&quot;:0.0}&apos;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Cửa sổ subscriber sẽ in ra tên topic và nội dung JSON một lần. Nếu bạn thử gửi lại với tùy chọn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; (retain) rồi khởi chạy lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mosquitto_sub&lt;/code&gt;, bản tin cũ sẽ xuất hiện ngay lập tức mà không cần lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pub&lt;/code&gt; mới — đó chính là hiện tượng cần tránh đối với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone/cmd&lt;/code&gt;. Để xóa bản tin đang bị lưu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retain&lt;/code&gt;, hãy gửi một tin nhắn rỗng kèm cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r -n&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mosquitto_pub &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; 127.0.0.1 &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; capstone/cmd &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; mẫu cho ESP32 DevKit thông dụng:&lt;/p&gt;

&lt;div class=&quot;language-ini highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;[env:esp32dev]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;platform&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;espressif32&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;board&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;esp32dev&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;framework&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;arduino&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;lib_deps&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;knolleary/PubSubClient@^2.8, bblanchon/ArduinoJson@^7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Cấu trúc hàm callback chỉ sao chép giá trị và mốc thời gian, còn vòng lặp điều khiển động cơ sẽ đọc các biến này:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;on_message(topic, payload):
    if topic != &quot;capstone/cmd&quot;: return
    parse v, w as floats
    if |v| &amp;gt; 0.5 or |w| &amp;gt; 1.5: return
    left, right = diff_drive(v, w)      # bài 08-04
    set_pending(left, right)
    last_cmd_ms = millis()

loop():
    if millis() - last_cmd_ms &amp;gt; 300: set_pending(0, 0)
    apply_pending_to_driver()
    client.loop()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lấy địa chỉ IP mạng LAN của laptop bằng lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ip -4 addr&lt;/code&gt; rồi nhập địa chỉ đó vào mã ESP32; tuyệt đối không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;127.0.0.1&lt;/code&gt; vì đối với ESP32, localhost chính là bản thân vi điều khiển.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Khởi chạy Mosquitto và kiểm tra bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ss&lt;/code&gt; rằng broker đang lắng nghe trên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0:1883&lt;/code&gt; hoặc địa chỉ LAN của laptop (không mở cổng công khai ngoài kiểm soát).&lt;/li&gt;
  &lt;li&gt;Mở cửa sổ theo dõi: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mosquitto_sub -h &amp;lt;ip-laptop&amp;gt; -t &apos;capstone/#&apos; -v&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Nạp firmware cho ESP32 với đúng SSID (2,4 GHz), mật khẩu và địa chỉ IP broker, sau đó mở log serial ở 115200 baud.&lt;/li&gt;
  &lt;li&gt;Gửi lần lượt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;v&quot;:0.0,&quot;w&quot;:0.0}&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;v&quot;:0.2,&quot;w&quot;:0.0}&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Quan sát gói tin telemetry gửi về trên cửa sổ subscriber, sau đó ngừng gửi lệnh trong 1 giây để kiểm tra watchdog.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Log serial mong đợi (đúng dạng cấu trúc, giá trị cụ thể có thể khác)&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wifi ip 192.168.1.42
mqtt connected
cmd v=0.20 w=0.00 -&amp;gt; L=28 R=28
telem published
watchdog stop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Dòng subscriber mong đợi&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;capstone/telem {&quot;vl&quot;:0.00,&quot;vr&quot;:0.00,&quot;batt&quot;:11.8,&quot;age_ms&quot;:320}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Khi kê bánh xe trên không, hai động cơ quay nhẹ rồi dừng hẳn ngay sau khi bạn ngừng xuất bản lệnh. Nếu động cơ vẫn còn tiếng rên hoặc tiếp tục quay, watchdog chưa nằm đúng trên luồng xuất PWM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Khi hỏng thì thường vì&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Nguyên nhân hay gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 không có IP&lt;/td&gt;
      &lt;td&gt;SSID 5 GHz, sai mật khẩu, hoặc AP cách ly client (mạng khách)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Có IP, MQTT thất bại&lt;/td&gt;
      &lt;td&gt;Tường lửa, hoặc Mosquitto chỉ nghe localhost&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot chạy ngay khi vừa boot&lt;/td&gt;
      &lt;td&gt;Lệnh retain. Xóa bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mosquitto_pub -r -n&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Không thấy telemetry&lt;/td&gt;
      &lt;td&gt;Gói lớn hơn giới hạn thư viện, hoặc bạn chỉ subscribe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone/cmd&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ giật khi vẫn nối mạng&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;client.loop()&lt;/code&gt; và nối lại DHCP chặn vòng 50 Hz. Bài 08-05&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Nếu đã có bo ESP32 từ Capstone A, bạn không cần mua thêm linh kiện. Chỉ cần mua thêm một DevKit thứ hai nếu muốn thử nghiệm broker riêng trong lúc khung robot đang lắp ráp.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Cửa hàng&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá 2026&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 DevKit có Wi-Fi (tránh ESP32-C3 nếu bạn chưa muốn sơ đồ chân khác)&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; tìm “ESP32”&lt;/td&gt;
      &lt;td&gt;Shopee/Lazada: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 DevKit V1 30 chân&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;khoảng 80.000–180.000 đồng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;USB Wi-Fi 2,4 GHz, chỉ khi laptop không có sóng 2,4&lt;/td&gt;
      &lt;td&gt;tùy chọn&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;USB wifi 2.4 GHz&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;khoảng 80.000–200.000 đồng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Bạn có thể dùng điểm phát sóng (hotspot) 2,4 GHz trên điện thoại thay cho router trong phòng lab (miễn là laptop chạy broker và ESP32 cùng nằm trên một mạng LAN cục bộ, không định tuyến qua mạng di động). Máy tính nhúng Raspberry Pi 5 (dự kiến cho Chương 09) có thể đảm nhận vai trò chạy Mosquitto về sau nhưng chưa cần trong bài này; tham khảo giá theo phiên bản RAM tại &lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Hshop&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=OAoM5IV393o&quot;&gt;Getting Started with ESP32 &amp;amp; MQTT: The Complete Beginner’s Guide&lt;/a&gt; — IoT Bhai, tiếng Anh. Hướng dẫn kết nối ESP32 với MQTT (publish và subscribe); hãy giữ nguyên tên topic của bài và dùng broker cục bộ thay vì các broker công cộng bên ngoài.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Thử xuất bản lệnh có cờ retain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;v&quot;:0.3,&quot;w&quot;:0.0}&lt;/code&gt;, khởi động lại riêng ESP32 và ghi nhận phản ứng của bánh xe, sau đó xóa bản tin retain. Gợi ý: nếu bánh tự chạy ngay khi vừa khởi động tức là do retain; sau khi chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r -n&lt;/code&gt;, khởi động lại phải đứng yên.&lt;/li&gt;
  &lt;li&gt;Giải thích vì sao QoS 1 chấp nhận được cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone/cmd&lt;/code&gt;, còn cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retain&lt;/code&gt; thì không. Gợi ý: nhận trùng một giá trị đặt (setpoint) hiện tại là vô hại, còn một lệnh cũ lưu từ phiên trước là rủi ro an toàn.&lt;/li&gt;
  &lt;li&gt;Nếu một chương trình dùng khóa JSON &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; còn chương trình kia dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt;, bên nào sai? Gợi ý: cả hai đều thiếu nhất quán cho tới khi chốt chung một lược đồ dữ liệu — chọn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt; và từ chối các khóa lạ.&lt;/li&gt;
  &lt;li&gt;Giả sử ESP32 mất 2 giây để kết nối lại Wi-Fi sau khi nhận một lệnh khác 0. Độ trễ của lệnh cũ kéo dài bao lâu nếu không có watchdog và nếu có watchdog 300 ms? Gợi ý: không có watchdog sẽ giữ PWM cũ suốt 2 giây; có watchdog 300 ms thì chỉ sau tối đa 300 ms lệnh sẽ về 0.&lt;/li&gt;
  &lt;li&gt;Viết hai dòng chú thích chuẩn bị cho cầu nối ROS 2: “MQTT là tùy chọn. Timeout serial là bắt buộc.” Gợi ý: câu này tóm tắt chính xác vai trò an toàn của sơ đồ đầu bài.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://mosquitto.org/man/mosquitto-8.html&quot;&gt;Trang hướng dẫn Mosquitto&lt;/a&gt; về cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listener&lt;/code&gt; và giao diện mạng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/knolleary/pubsubclient&quot;&gt;API PubSubClient&lt;/a&gt; về các hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;connect&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt; và giới hạn kích thước gói tin.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html&quot;&gt;MQTT 3.1.1, mục QoS&lt;/a&gt; — đặc tả kỹ thuật chi tiết về QoS.&lt;/li&gt;
  &lt;li&gt;QoS trong ROS 2 tại &lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Quality-of-Service-Settings.html&quot;&gt;trang khái niệm Jazzy&lt;/a&gt; (sẽ học ở Chương 09 — cùng khái niệm nhưng khác tầng truyền dẫn).&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Giao thức serial ngoài println</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_01_serial_protocols/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_01_serial_protocols</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~90 phút&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Ai học vi điều khiển cũng bắt đầu bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.println(sensor_value)&lt;/code&gt; để in chữ ra màn hình cho người đọc. Nhưng khi hai cỗ máy nói chuyện với nhau ở tần số 100 Hz — ví dụ ESP32 gửi vận tốc, quaternion IMU và số xung encoder lên Raspberry Pi chạy ROS 2 — việc chuyển đổi số thực sang chuỗi văn bản ASCII/JSON rồi lại phân tích ngược thành số thực ở đầu kia vừa chậm chạp, vừa tốn gấp đôi băng thông, lại dễ vỡ định dạng khi dính nhiễu. Bài học này đưa bạn vượt qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println&lt;/code&gt; sang &lt;strong&gt;giao thức nhị phân đóng khung (binary framing), mã hóa COBS và kiểm lỗi CRC-16&lt;/strong&gt; — tiêu chuẩn thực thụ của truyền thông nhúng chuyên nghiệp.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-giao-thức-nhị-phân-byte-stuffing-cobs-và-kiểm-lỗi-crc&quot;&gt;Kiến thức cần nhớ: Giao thức nhị phân, Byte-Stuffing (COBS) và Kiểm lỗi CRC&lt;/h2&gt;

&lt;h3 id=&quot;1-văn-bản-csv--json-vs-nhị-phân-đóng-gói-packed-binary-struct&quot;&gt;1. Văn bản (CSV / JSON) vs. Nhị phân đóng gói (Packed Binary Struct)&lt;/h3&gt;
&lt;p&gt;Định dạng văn bản dễ đọc bằng mắt khi gỡ lỗi nhanh, nhưng mỗi chữ số tốn 1 byte và hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sscanf&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSON.parse&lt;/code&gt; ngốn nhiều chu kỳ CPU. Định dạng &lt;strong&gt;nhị phân&lt;/strong&gt; đóng gói trực tiếp các biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float32&lt;/code&gt; (4 byte) hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int32&lt;/code&gt; (4 byte) theo quy ước thứ tự byte rõ ràng (&lt;strong&gt;little-endian&lt;/strong&gt;) giúp giảm kích thước gói từ 2–4 lần và giải mã trong vài nano-giây bằng một phép &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memcpy&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;2-bài-toán-trùng-byte-đánh-dấu-và-giải-pháp-cobs-consistent-overhead-byte-stuffing&quot;&gt;2. Bài toán trùng byte đánh dấu và giải pháp COBS (Consistent Overhead Byte Stuffing)&lt;/h3&gt;
&lt;p&gt;Trong gói nhị phân, giả sử bạn chọn byte &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt; làm dấu kết thúc gói tin. Chuyện gì xảy ra nếu chính giá trị cảm biến của bạn bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; (chứa byte &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt;)? Bên nhận sẽ cắt ngang gói tin giữa chừng! Thuật toán &lt;strong&gt;COBS&lt;/strong&gt; giải quyết triệt để vấn đề này bằng cách loại bỏ hoàn toàn mọi byte &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt; khỏi phần thân gói với chi phí phụ cực nhỏ (chỉ thêm đúng &lt;strong&gt;1 byte&lt;/strong&gt; cho mỗi 254 byte dữ liệu!).&lt;/p&gt;

&lt;h3 id=&quot;3-tại-sao-crc-16-vượt-trội-hơn-checksum-cộngxor-đơn-giản&quot;&gt;3. Tại sao CRC-16 vượt trội hơn Checksum cộng/XOR đơn giản?&lt;/h3&gt;
&lt;p&gt;Checksum cộng hoặc XOR không phát hiện được lỗi hoán vị hai byte hay lỗi hai bit cùng vị trí bị lật đồng thời. &lt;strong&gt;CRC-16 (Cyclic Redundancy Check)&lt;/strong&gt; dùng phép chia đa thức modulo 2 trong trường hữu hạn \(GF(2)\), phát hiện được 100% lỗi đơn bit, lỗi 2 bit và mọi cụm nhiễu liên tiếp dài tới 16 bit.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-so-sánh-băng-thông-và-chi-phí-đóng-gói-giữa-json-và-nhị-phân-ở-100-hz&quot;&gt;Giải thích: Cầm tay so sánh băng thông và chi phí đóng gói giữa JSON và Nhị phân ở 100 Hz&lt;/h3&gt;

&lt;p&gt;Giả sử ESP32 cần gửi đều đặn 3 giá trị số thực dấu phẩy động &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(vx, vy, wz)&lt;/code&gt; lên máy tính chủ ở tần số &lt;strong&gt;100 Hz&lt;/strong&gt; trên đường UART &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200 8N1&lt;/code&gt; (trần băng thông \(11\,520\text{ byte/giây}\)):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cách 1 — Gửi chuỗi JSON (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;vx&quot;:0.1234,&quot;vy&quot;:0.0000,&quot;wz&quot;:-0.5678}\n&lt;/code&gt;):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Độ dài mỗi gói: &lt;strong&gt;40 byte&lt;/strong&gt;.&lt;/li&gt;
      &lt;li&gt;Lưu lượng ở 100 Hz: \(40 \times 100 = 4\,000\text{ byte/giây}\) (chiếm &lt;strong&gt;\(34{,}7\%\)&lt;/strong&gt; đường UART, chưa kể thời gian CPU chuyển số thực sang chuỗi và parse ngược!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cách 2 — Gửi gói Nhị phân đóng khung (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[SOF 2B] + [LEN 1B] + [3 × float32 = 12B] + [CRC16 2B]&lt;/code&gt;):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Độ dài mỗi gói cố định: \(2 + 1 + 12 + 2 = 17\text{ byte}\).&lt;/li&gt;
      &lt;li&gt;Lưu lượng ở 100 Hz: \(17 \times 100 = 1\,700\text{ byte/giây}\) (chỉ chiếm &lt;strong&gt;\(14{,}8\%\)&lt;/strong&gt; đường UART!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết luận định lượng:&lt;/strong&gt; Gói nhị phân nhỏ hơn &lt;strong&gt;\(2{,}35\times\)&lt;/strong&gt;, giải mã nhanh gấp hàng chục lần, và tích hợp sẵn mã kiểm lỗi &lt;strong&gt;CRC-16&lt;/strong&gt; — lý do mọi giao thức robotics nổi tiếng (MAVLink của drone, Dynamixel, micro-ROS) đều dùng framing nhị phân!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn thiết kế được một định dạng khung lệnh bánh xe bền vững ngay cả khi cổng USB giao dữ liệu bị cắt mảnh, cài đặt được phía gửi bằng Python và phía nhận trên ESP32/Pico, đồng thời giải thích được vì sao dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println&lt;/code&gt; gửi chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;40,-15&quot;&lt;/code&gt; chưa phải là một giao thức tin cậy. Bạn cũng tính được byte kiểm tra XOR, xác định ngưỡng timeout để đưa động cơ về 0, và ghi lại cả một khung hợp lệ lẫn một khung cố ý làm hỏng vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. Đây chính là giao ước truyền thông mà node cầu nối ROS 2 ở Chương 10 sẽ sử dụng để &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; không bao giờ biến thành các byte rời rạc trong bộ đệm.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Bạn đã biết điều khiển LED và in dữ liệu qua cổng nối tiếp (Chương 02), cũng như hoàn thiện phiên bản teleop Capstone A ở Chương 07. Bạn hiểu 1 byte gồm 8 bit và cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200 8N1&lt;/code&gt; nghĩa là tốc độ 115200 bit/giây, 8 bit dữ liệu, không bit chẵn lẻ, 1 bit dừng. Máy tính cài Python 3 là đủ cho phần chạy mô phỏng (dry-run); bo ESP32 hoặc Pico dùng cho phiên bản có phần cứng.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Chương 07 chứng minh robot có thể chạy khi người dùng gõ lệnh qua cổng nối tiếp. Chương 08 nâng cấp đường truyền đó thành giao thức mà chương trình máy tính có thể vận hành tự động và tin cậy. Sang Chương 09, ROS 2 giới thiệu các topic cũng dựa trên thông điệp có cấu trúc và kiểu dữ liệu rõ ràng; node cầu nối ở Chương 10 chỉ làm nhiệm vụ phiên dịch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; thành đúng khung lệnh này. Nếu khung truyền thiếu chặt chẽ, mọi tầng phía trên như Nav2, vòng thị giác hay chính sách điều khiển học máy (Chương 12) đều sẽ gặp tình trạng robot giật cục mỗi khi rơi mất một byte.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_serial_frame.png&quot; alt=&quot;Các trường của khung serial&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Giao tiếp UART truyền từng byte riêng lẻ chứ không tự động gom theo gói tin. Chip chuyển đổi USB-serial trả về bất kỳ lượng dữ liệu nào đã đến bộ đệm: có thể là nửa dòng, hai dòng dính liền hoặc phần dư từ lần đọc trước. Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.println(&quot;40,-15&quot;)&lt;/code&gt; nhìn có vẻ ổn trên Serial Monitor chỉ vì phần mềm đó chờ ký tự xuống dòng mới hiển thị; còn bộ phân tích (parser) trong vòng lặp điều khiển tốc độ cao buộc phải trả lời rõ ba câu hỏi: bản tin bắt đầu ở đâu, dài bao nhiêu byte và dữ liệu có toàn vẹn không.&lt;/p&gt;

&lt;p&gt;Khóa học này sử dụng hai kiểu đóng khung: &lt;strong&gt;khung phân cách&lt;/strong&gt; (kết thúc bằng ký tự &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt; — byte &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0A&lt;/code&gt;, tiện gõ tay trên terminal) và &lt;strong&gt;khung theo độ dài&lt;/strong&gt; (gồm byte mở đầu, byte độ dài, đúng số byte tải tương ứng và byte kiểm tra). Khung theo độ dài vẫn hoạt động chính xác kể cả khi phần tải chứa dấu phẩy hay ký tự xuống dòng. Node cầu nối ở Chương 10 sẽ dùng khung theo độ dài, còn khung phân cách có thể giữ lại để gỡ lỗi thủ công.&lt;/p&gt;

&lt;p&gt;Phần tải (payload) vẫn giữ dạng văn bản dễ đọc bằng mắt:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;V,40,-15
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trong đó &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; là mã lệnh vận tốc, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;40&lt;/code&gt; là lệnh bánh trái và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-15&lt;/code&gt; là lệnh bánh phải (theo thang −100…100). Quy ước đơn vị phải được ghi rõ ở chú thích đầu cả hai chương trình, tránh việc một bên tự ý đổi sang m/s mà bên kia không biết.&lt;/p&gt;

&lt;p&gt;Phép kiểm tra toàn vẹn trong bài này là phép XOR tất cả các byte trong phần tải thành một byte duy nhất, giúp phát hiện phần lớn lỗi lật bit đơn và khung bị cắt thiếu. Đây là cơ chế kiểm tra lỗi truyền dẫn chứ không phải mã hóa bảo mật (nếu triển khai ngoài môi trường lab, bạn có thể nâng cấp lên CRC sau khi đã làm chủ máy trạng thái):&lt;/p&gt;

\[c = b_0 \oplus b_1 \oplus \cdots \oplus b_{n-1}\]

&lt;p&gt;Cấu trúc đầy đủ trên đường dây là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STX | LEN | payload | c | END&lt;/code&gt;, với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STX = 0xAA&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;END = 0x0A&lt;/code&gt;. Trường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEN&lt;/code&gt; chỉ độ dài của riêng phần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;payload&lt;/code&gt; (không tính các byte khung) và cần giới hạn tối đa 32 byte để một byte nhiễu không khiến vi điều khiển cấp phát bộ đệm quá lớn.&lt;/p&gt;

&lt;p&gt;Bên nhận được cài đặt như một máy trạng thái gọn: tìm byte mở đầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xAA&lt;/code&gt;, đọc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEN&lt;/code&gt;, đọc đủ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEN&lt;/code&gt; byte tải, đọc byte kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt;, đọc byte kết thúc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;END&lt;/code&gt;, đối chiếu giá trị XOR rồi mới áp dụng lệnh. Bất kỳ sai lệch nào đều đưa máy trạng thái quay về bước tìm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xAA&lt;/code&gt;. Quan trọng nhất là cơ chế an toàn: nếu quá 300 ms không nhận được khung hợp lệ nào, lệnh động cơ phải tự động đặt về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,0&lt;/code&gt;. Một bộ phân tích khung đẹp nhưng thiếu timeout sẽ để robot chạy mãi theo lệnh cuối khi tuột cáp USB.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Đoạn mã Python sau dựng khung truyền cho lệnh bánh trái = 40 và bánh phải = −15:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;wheel command out of range&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;V,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ascii&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xAA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Kết quả in ra bắt đầu bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aa&lt;/code&gt;, kết thúc bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0a&lt;/code&gt;, và phần tải giải mã thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,-15&lt;/code&gt;. Khi gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame(40, -15)&lt;/code&gt; hai lần, chuỗi hex thu được phải giống hệt nhau. Nếu đổi bánh phải thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-16&lt;/code&gt;, chỉ phần tải và byte XOR thay đổi; nếu tự ý sửa một byte tải mà giữ nguyên byte XOR cũ, phép kiểm tra sẽ phát hiện sai lệch ngay.&lt;/p&gt;

&lt;p&gt;Trên vi điều khiển, tuyệt đối không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; để chờ đủ byte. Hãy gom các byte đọc được từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read()&lt;/code&gt; vào một bộ đệm nhỏ và chạy máy trạng thái trong mỗi chu kỳ vòng lặp (tốc độ vòng lặp này sẽ được đo ở Bài 08-05).&lt;/p&gt;

&lt;p&gt;Ở phía máy tính, gửi khung bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyserial&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/dev/ttyUSB0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu chưa nối robot thật, bạn có thể thay tên cổng bằng cặp thiết bị giả lập PTY tạo bởi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; trong phần Lab. Khi đã nối mạch driver động cơ thật, luôn kê bánh xe rời khỏi mặt bàn.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Thực hành chạy khô trên máy tính (chưa cần phần cứng robot):&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python3 &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;pyserial
socat &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; pty,raw,echo&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0 pty,raw,echo&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; sẽ in ra hai tên thiết bị nối thông nhau (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/pts/3&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/pts/4&lt;/code&gt;). Bên gửi mở một cổng, bộ phân tích mở cổng còn lại:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/dev/pts/4&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytearray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;xor_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xAA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0A&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;check&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;xor_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;DROP&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Kết quả mong đợi&lt;/strong&gt; sau khi gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;send(40, -15)&lt;/code&gt; từ cổng PTY bên kia:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;OK V,40,-15
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hãy gửi liên tiếp 10 khung hợp lệ: kết quả phải hiện đủ 10 dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt; và không có dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DROP&lt;/code&gt;. Sau đó thử gửi một khung bị cắt thiếu byte kiểm tra như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&quot;\xAA\x05V,40&quot;&lt;/code&gt;: bộ phân tích tuyệt đối không được in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt;. Để mô phỏng tình huống rút cáp, hãy ngừng bên gửi; trên firmware thực tế, nhật ký serial phải ghi nhận hai kênh PWM tự đưa về 0 trong khoảng 300 ms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Khi hỏng thì thường vì&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Nguyên nhân hay gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Rác vô hạn, không có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Lệch baud, thường 9600 với 115200, hoặc mở trùng một PTY&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Không bao giờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt;, bộ đệm phình&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEN&lt;/code&gt; không khớp phần tải thực sự gửi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ chạy tiếp sau khi rút cáp&lt;/td&gt;
      &lt;td&gt;Chưa có timeout; lệnh cuối bị giữ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Serial Monitor được, Python thì không&lt;/td&gt;
      &lt;td&gt;Monitor tự thêm xuống dòng; script quên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;END&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flush&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 reset khi động cơ quay&lt;/td&gt;
      &lt;td&gt;Động cơ ăn nguồn USB. Dùng pack chương 07 và mass chung&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Phần thực hành chạy khô chỉ cần máy tính; phần chạy mạch thật sử dụng lại đúng bo mạch của Capstone A.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Cửa hàng&lt;/th&gt;
      &lt;th&gt;Từ khóa Shopee hoặc Lazada&lt;/th&gt;
      &lt;th&gt;Khoảng giá 2026, kiểm tra lúc mua&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 DevKit V1 (30 chân, WROOM-32)&lt;/td&gt;
      &lt;td&gt;Tìm “ESP32” trên &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 DevKit V1 30 chân&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;khoảng 80.000–180.000 đồng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;USB-TTL CP2102 hoặc CH340, mức 3,3 V&lt;/td&gt;
      &lt;td&gt;cùng chỗ&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CP2102 USB TTL 3.3V&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;khoảng 25.000–60.000 đồng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pico hoặc Pico W nếu bạn ở MicroPython&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; tìm “Raspberry Pi Pico”&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Raspberry Pi Pico&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;khoảng 120.000–250.000 đồng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Lưu ý: mạch chuyển USB-TTL cấp nguồn 5 V vẫn phải xuất mức logic 3,3 V khi nối vào chân RX của ESP32. Nguồn động cơ luôn lấy từ pack pin robot, không lấy từ cổng USB máy tính. Khoảng giá trên chỉ mang tính tham khảo và có thể thay đổi theo thời điểm.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=sTHckUyxwp8&quot;&gt;Understanding UART&lt;/a&gt; — Rohde &amp;amp; Schwarz, tiếng Anh. Giải thích cấu trúc khung byte trên đường truyền UART để thấy sự khác biệt với việc gửi chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println&lt;/code&gt; đơn thuần.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=IyGwvGzrqp8&quot;&gt;Serial Communications Explained: UART, I2C, and SPI&lt;/a&gt; — Electronoobs, tiếng Anh. Đặt UART cạnh I2C và SPI để hiểu vì sao giao tiếp teleop giữa máy tính và vi điều khiển chọn UART.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tính tay giá trị XOR của các byte ASCII trong chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt;, sau đó đối chiếu với hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame()&lt;/code&gt;. Gợi ý: phép XOR có tính giao hoán nên thứ tự không đổi kết quả, và kết quả luôn nằm trong một byte &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt;–&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xFF&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Viết ba câu mô tả một tình huống mà bộ phân tích theo ký tự xuống dòng chấp nhận nhầm dữ liệu nhưng bộ phân tích theo độ dài sẽ từ chối. Gợi ý: phần tải chứa sẵn ký tự &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt; hoặc hai khung bị dính vào nhau trước khi kịp đọc xong khung đầu.&lt;/li&gt;
  &lt;li&gt;Nhật ký hiển thị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,40,-15&lt;/code&gt; rồi im lặng suốt 2 giây nhưng bánh xe vẫn quay. Firmware đang thiếu thành phần nào? Gợi ý: lưu mốc thời gian của khung hợp lệ cuối cùng và so sánh với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Nếu sửa giao thức sao cho các lệnh nằm ngoài dải −100…100 bị loại bỏ hoàn toàn thay vì kẹp giá trị, bên nào bắt buộc phải kiểm tra chặn — máy tính hay vi điều khiển? Gợi ý: vi điều khiển bắt buộc phải chặn để đảm bảo an toàn phần cứng; kiểm tra ở phía máy tính chỉ mang tính hỗ trợ.&lt;/li&gt;
  &lt;li&gt;Viết một câu chú thích rõ ràng để dán vào đầu mã nguồn cầu nối Chương 10, nêu đủ: đơn vị trên đường truyền serial, đơn vị trong ROS và thời gian timeout. Gợi ý: phía ROS dùng m/s và rad/s, đường truyền serial vẫn giữ thang −100…100 cho tới khi được quy đổi tập trung trong một hàm duy nhất.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://pyserial.readthedocs.io/en/latest/shortintro.html&quot;&gt;Tài liệu pyserial&lt;/a&gt; về mở cổng, thiết lập timeout và hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flush&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/communication/serial/&quot;&gt;Tham chiếu Serial của Arduino&lt;/a&gt; dành cho firmware Capstone viết bằng C++.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; về quy ước chiều trục tọa độ sẽ ánh xạ lên khung lệnh này.&lt;/li&gt;
  &lt;li&gt;Ghi chú giao thức teleop ở Chương 07 và Bài 04 Chương 10 — nơi trực tiếp tiêu thụ khung truyền bạn vừa thiết kế.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Vector, phép quay và quaternion trước TF2</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_00_math_for_tf/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter08/08_00_math_for_tf</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~120 phút&lt;/strong&gt;, bao gồm một bài thực hành viết mã với NumPy. Bài này chưa cần phần cứng robot và chưa cần chạy ROS.&lt;/p&gt;

&lt;p&gt;Khi một chiếc camera gắn trên đầu robot nhìn thấy một chiếc cốc cách ống kính 40 cm về phía bên phải, làm sao cánh tay robot gắn ở thân dưới biết phải vươn tới tọa độ nào để gắp đúng chiếc cốc đó? Trong robotics, câu hỏi chuyển đổi tọa độ giữa hàng chục hệ trục (bản đồ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; → thân xe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gripper_link&lt;/code&gt;) là cơm bữa hằng ngày — và hệ thống &lt;strong&gt;TF2&lt;/strong&gt; của ROS 2 thực chất là một cỗ máy nhân ma trận biến đổi thuần nhất và &lt;strong&gt;quaternion&lt;/strong&gt; tự động chạy hàng trăm lần mỗi giây. Hiểu rõ toán học đằng sau phép quay trước khi chạm vào TF2 giúp bạn không bao giờ bị “tẩu hỏa nhập ma” trước những con số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(x, y, z, w)&lt;/code&gt; kỳ bí!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-hệ-tọa-độ-nhóm-quay-so3--se3-và-quaternion-đơn-vị&quot;&gt;Kiến thức cần nhớ: Hệ tọa độ, Nhóm quay \(SO(3) / SE(3)\) và Quaternion đơn vị&lt;/h2&gt;

&lt;h3 id=&quot;1-ma-trận-quay-r-in-so3-và-ma-trận-biến-đổi-thuần-nhất-t-in-se3&quot;&gt;1. Ma trận quay \(R \in SO(3)\) và Ma trận biến đổi thuần nhất \(T \in SE(3)\)&lt;/h3&gt;
&lt;p&gt;Một ma trận quay 3×3 trực giao thỏa \(R^T R = I\) và \(\det(R) = +1\) mô tả thuần túy tư thế quay. Để gộp cả phép quay \(R\) và phép tịnh tiến \(\mathbf{t} \in \mathbb{R}^3\) thành &lt;strong&gt;một phép nhân ma trận duy nhất&lt;/strong&gt;, ta mở rộng sang tọa độ thuần nhất 4×4:
\(T = \begin{bmatrix} R &amp;amp; \mathbf{t} \\ \mathbf{0}^T &amp;amp; 1 \end{bmatrix} \in SE(3).\)
Nhờ đó, chuỗi biến đổi qua nhiều khớp nối chỉ đơn giản là nhân liên tiếp các ma trận: \(T_{\text{map}\to\text{cam}} = T_{\text{map}\to\text{base}} \, T_{\text{base}\to\text{cam}}\).&lt;/p&gt;

&lt;h3 id=&quot;2-góc-euler-rollpitchyaw-và-cái-bẫy-khóa-khớp-vạn-năng-gimbal-lock&quot;&gt;2. Góc Euler (Roll–Pitch–Yaw) và cái bẫy Khóa khớp vạn năng (Gimbal Lock)&lt;/h3&gt;
&lt;p&gt;Góc Euler (quay lần lượt quanh 3 trục X, Y, Z) rất trực quan với mắt người, nhưng phụ thuộc chặt vào thứ tự quay và dính lỗi toán học gọi là &lt;strong&gt;Gimbal Lock&lt;/strong&gt;: khi góc pitch đạt \(\pm 90^\circ\), trục roll và trục yaw trùng nhau, làm mất một bậc tự do và khiến phép nội suy tư thế bị giật cục kỳ dị.&lt;/p&gt;

&lt;h3 id=&quot;3-quaternion-đơn-vị--cách-biểu-diễn-quay-3d-không-bao-giờ-bị-gimbal-lock&quot;&gt;3. Quaternion đơn vị — cách biểu diễn quay 3D không bao giờ bị Gimbal Lock&lt;/h3&gt;
&lt;p&gt;Một &lt;strong&gt;quaternion đơn vị&lt;/strong&gt; \(q = w + x\mathbf{i} + y\mathbf{j} + z\mathbf{k}\) (với \(w^2 + x^2 + y^2 + z^2 = 1\)) biểu diễn phép quay một góc \(\theta\) quanh trục đơn vị \(\hat{\mathbf{u}} = (u_x, u_y, u_z)\) qua công thức gọn đẹp:
\(q = \cos\!\left(\frac{\theta}{2}\right) + \sin\!\left(\frac{\theta}{2}\right)\big(u_x\mathbf{i} + u_y\mathbf{j} + u_z\mathbf{k}\big).\)
Nó chỉ tốn 4 số thực (nhẹ hơn 9 số của ma trận quay), hoàn toàn miễn nhiễm với Gimbal Lock và cho phép nội suy tư thế mượt mà (SLERP) — lý do ROS 2/TF2 chọn quaternion làm chuẩn lưu trữ bắt buộc!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-quay-vector-mathbfp--1-0-0t-góc-90circ-quanh-trục-z-bằng-cả-ma-trận-quay-lẫn-quaternion&quot;&gt;Giải thích: Cầm tay quay vector \(\mathbf{p} = (1, 0, 0)^T\) góc \(90^\circ\) quanh trục Z bằng cả Ma trận quay lẫn Quaternion&lt;/h3&gt;

&lt;p&gt;Ta muốn quay vector đơn vị trên trục X là \(\mathbf{p} = [1,\; 0,\; 0]^T\) ngược chiều kim đồng hồ một góc &lt;strong&gt;\(\theta = 90^\circ\)&lt;/strong&gt; quanh trục Z (\(\hat{\mathbf{u}} = [0,\; 0,\; 1]^T\)). Trực giác hình học cho biết kết quả phải rơi đúng vào trục Y dương \([0,\; 1,\; 0]^T\). Hãy kiểm chứng bằng cả hai công cụ:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Cách 1 — Dùng Ma trận quay 2D/3D quanh trục Z:&lt;/strong&gt;
\(R_z(90^\circ)\,\mathbf{p} = \begin{bmatrix} \cos 90^\circ &amp;amp; -\sin 90^\circ &amp;amp; 0 \\ \sin 90^\circ &amp;amp; \cos 90^\circ &amp;amp; 0 \\ 0 &amp;amp; 0 &amp;amp; 1 \end{bmatrix} \begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 &amp;amp; -1 &amp;amp; 0 \\ 1 &amp;amp; 0 &amp;amp; 0 \\ 0 &amp;amp; 0 &amp;amp; 1 \end{bmatrix} \begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix}.\)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Cách 2 — Dùng Quaternion đơn vị \(q\) (chú ý góc chia đôi \(\theta/2 = 45^\circ\)!):&lt;/strong&gt;&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;Lập quaternion: \(q = \cos 45^\circ + \sin 45^\circ\,\mathbf{k} = \frac{\sqrt{2}}{2}(1 + \mathbf{k})\), liên hợp \(q^* = \frac{\sqrt{2}}{2}(1 - \mathbf{k})\).&lt;/li&gt;
      &lt;li&gt;Kiểm tra chuẩn đơn vị: \(\|q\|^2 = \left(\frac{\sqrt{2}}{2}\right)^2 + \left(\frac{\sqrt{2}}{2}\right)^2 = 0{,}5 + 0{,}5 = 1\).&lt;/li&gt;
      &lt;li&gt;Viết vector \(\mathbf{p}\) dưới dạng quaternion thuần ảo \(p = \mathbf{i}\), áp công thức kẹp bánh mì \(p&apos; = q\,p\,q^*\) (nhớ quy tắc \(\mathbf{k}\mathbf{i} = \mathbf{j},\; \mathbf{i}\mathbf{k} = -\mathbf{j},\; \mathbf{j}\mathbf{k} = \mathbf{i}\)):
\(p&apos; = \frac{1}{2}(1 + \mathbf{k})\,\mathbf{i}\,(1 - \mathbf{k}) = \frac{1}{2}(\mathbf{i} + \mathbf{j})(1 - \mathbf{k}) = \frac{1}{2}(\mathbf{i} + \mathbf{j} + \mathbf{j} - \mathbf{i}) = \mathbf{j} = [0,\; 1,\; 0]^T!\)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Cả hai cách đều ra chính xác \([0, 1, 0]^T\)! Từ nay khi thấy ROS 2 in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orientation: {x: 0.0, y: 0.0, z: 0.707, w: 0.707}&lt;/code&gt;, bạn đọc ngay trong đầu: &lt;em&gt;“À, robot đang quay đúng \(90^\circ\) quanh trục Z!”&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn biết biến đổi tọa độ một điểm trong mặt phẳng bằng phép quay và phép tịnh tiến, viết được ma trận biến đổi đồng nhất $3 \times 3$ và $4 \times 4$, và giải thích được vì sao quaternion biểu diễn góc quay yaw lại sử dụng nửa góc. Bạn cũng sẽ kiểm chứng bằng NumPy rằng phép quay yaw $90^\circ$ đưa vector $(1, 0)$ thành $(0, 1)$, với thứ tự thành phần quaternion chuẩn của ROS là $(x, y, z, w)$. Toàn bộ nền tảng toán học này chính là phần tính toán mà TF2 sẽ đảm nhận ở Chương 09.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Bạn cần biết nhân ma trận cơ bản trên giấy hoặc bằng NumPy; Chương 06 đã cung cấp khổ vết bánh xe và bán kính lăn. Bài này chưa cần sử dụng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt;. Nếu máy chưa cài NumPy, chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3 -m pip install numpy&lt;/code&gt; (đây là thư viện Python độc lập, chưa liên quan tới node ROS).&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Bài động học vi sai trong chương này tách vận tốc thân $v$ và $\omega$ thành vận tốc hai bánh xe, còn bài TF2 ở Chương 09 giải quyết câu hỏi một điểm đo trong hệ tọa độ laser sẽ nằm ở đâu trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;. Cả hai bài toán đều quy về cùng một mô hình hình học: vector, gốc tọa độ và hướng. Người học nhảy thẳng sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static_transform_publisher&lt;/code&gt; thường mắc hai lỗi kinh điển: đảo ngược thứ tự tịnh tiến – quay, hoặc nhập trực tiếp góc yaw vào thành phần $z$ của quaternion thay vì $\sin(\theta/2)$, khiến bản đồ ở Chương 11 bị xoay lệch. Làm chủ các phép tính số trên máy trước sẽ giúp bạn biết chắc mình đang kiểm tra hình học chứ không phải lỗi truyền thông DDS khi cây TF gặp sự cố.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_math_frames.png&quot; alt=&quot;Hai hệ: base đứng yên trên giấy, laser quay 90° và dịch đi&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Vector cột trong bài này biểu diễn một đại lượng có độ lớn và hướng trong không gian, không đơn thuần là một mảng số. Đối với robot mặt đất, tiêu chuẩn REP-103 quy ước trục $+x$ hướng về phía trước, $+y$ hướng sang trái và $+z$ hướng lên trên. Góc yaw dương quay từ $+x$ sang $+y$ (ngược chiều kim đồng hồ khi nhìn từ trên xuống, tức robot rẽ trái).&lt;/p&gt;

&lt;p&gt;Phép quay một vector trong mặt phẳng một góc $\theta$ (radian) quanh trục $z$:&lt;/p&gt;

\[\begin{bmatrix} x&apos; \\ y&apos; \end{bmatrix}
=
\begin{bmatrix}
\cos\theta &amp;amp; -\sin\theta \\
\sin\theta &amp;amp; \cos\theta
\end{bmatrix}
\begin{bmatrix} x \\ y \end{bmatrix}\]

&lt;p&gt;Lưu ý dấu trừ nằm ở phần tử hàng trên bên phải; nếu đảo dấu, lệnh rẽ trái sẽ biến thành rẽ phải. Với $\theta = \pi/2$, ta có $\cos(\pi/2) = 0$ và $\sin(\pi/2) = 1$, đưa vector $(1, 0)$ thành $(0, 1)$.&lt;/p&gt;

&lt;p&gt;Phép tịnh tiến không thể biểu diễn trực tiếp bằng phép nhân ma trận $2 \times 2$. Ma trận biến đổi đồng nhất gộp cả phép quay lẫn phép tịnh tiến vào một phép nhân duy nhất. Trong mặt phẳng 2D:&lt;/p&gt;

\[\begin{bmatrix} x&apos; \\ y&apos; \\ 1 \end{bmatrix}
=
\begin{bmatrix}
\cos\theta &amp;amp; -\sin\theta &amp;amp; t_x \\
\sin\theta &amp;amp; \cos\theta &amp;amp; t_y \\
0 &amp;amp; 0 &amp;amp; 1
\end{bmatrix}
\begin{bmatrix} x \\ y \\ 1 \end{bmatrix}\]

&lt;p&gt;Số $1$ ở hàng cuối là phần tử phụ trợ để phép nhân ma trận tự động cộng thêm vector tịnh tiến $\mathbf{t}$. Trong không gian 3D, cấu trúc hoàn toàn tương tự với khối ma trận quay $3 \times 3$ và cột tịnh tiến:&lt;/p&gt;

\[T =
\begin{bmatrix}
R &amp;amp; \mathbf{t} \\
\mathbf{0}^\top &amp;amp; 1
\end{bmatrix}\]

&lt;p&gt;Ma trận $T$ chuyển đổi tọa độ một điểm từ hệ tọa độ con sang hệ tọa độ cha, với $R$ và $\mathbf{t}$ mô tả tư thế và vị trí của hệ con quan sát từ hệ cha. Phép nhân hai ma trận biến đổi tương ứng với việc nối hai cạnh liên tiếp trong cây tọa độ: $T_{A \leftarrow C} = T_{A \leftarrow B}\, T_{B \leftarrow C}$. Thứ tự nhân ma trận phải khớp chặt chẽ với thứ tự hệ tọa độ; đảo thứ tự nhân sẽ đặt cảm biến LiDAR sai hoàn toàn vị trí thực tế.&lt;/p&gt;

&lt;p&gt;Trong ROS, quaternion được biểu diễn theo thứ tự $(x, y, z, w)$ — phần vector đứng trước, phần vô hướng đứng cuối. Một phép quay góc $\theta$ quanh trục đơn vị $\mathbf{u} = (u_x, u_y, u_z)$ có các thành phần:&lt;/p&gt;

\[x = u_x \sin\frac{\theta}{2},\quad
y = u_y \sin\frac{\theta}{2},\quad
z = u_z \sin\frac{\theta}{2},\quad
w = \cos\frac{\theta}{2}\]

&lt;p&gt;Với phép quay yaw quanh trục $+z$, ta có $\mathbf{u} = (0, 0, 1)$. Chi tiết nửa góc $\theta/2$ rất dễ bị nhầm lẫn: quay yaw $90^\circ$ không tạo ra $z = 1$, mà cho:&lt;/p&gt;

\[z = w = \sin\frac{\pi}{4} = \cos\frac{\pi}{4} = \frac{\sqrt{2}}{2} \approx 0{,}7071\]

&lt;p&gt;cùng với $x = y = 0$. Mọi quaternion đơn vị đều thỏa mãn $x^2+y^2+z^2+w^2 = 1$. Lưu ý rằng bộ $(0, 0, 1, 0)$ ứng với góc quay $180^\circ$, không phải $90^\circ$.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_quaternion_yaw.png&quot; alt=&quot;Yaw 90°: vector (1, 0) thành (0, 1), quaternion dùng nửa góc&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hai quaternion đối dấu nhau là $(x, y, z, w)$ và $(-x, -y, -z, -w)$ biểu diễn cùng một phép quay trong không gian. Đôi khi TF trả về bộ đối dấu; bạn không cần cố “sửa” bằng cách cộng thêm $2\pi$ vào góc yaw rồi phát lại nếu chưa quy đổi về góc thực.&lt;/p&gt;

&lt;p&gt;Hiện tượng khóa gimbal (gimbal lock) và sự thiếu nhất quán về thứ tự trục là lý do ROS không truyền ba góc Euler trên các topic chuẩn. Dù robot vi sai trong khóa học chỉ quay theo góc yaw, ta vẫn phải xuất bản quaternion vì thông điệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Quaternion&lt;/code&gt; luôn dùng đủ bốn thành phần.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-từng-bước&quot;&gt;Ví dụ làm từng bước&lt;/h2&gt;

&lt;p&gt;Giả sử LiDAR gắn phía trước tâm trục bánh xe 0,10 m, cao hơn 0,08 m và không xoay hướng so với thân xe. Trong hệ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, ta có:&lt;/p&gt;

\[\mathbf{t} = \begin{bmatrix} 0{,}10 \\ 0 \\ 0{,}08 \end{bmatrix},\quad R = I\]

&lt;p&gt;Một điểm vật cản nằm cách LiDAR 0,20 m dọc theo trục $+x$ của chính LiDAR có tọa độ $(0{,}20,\ 0,\ 0)$ trong hệ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;. Khi quy đổi về hệ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;:&lt;/p&gt;

\[\begin{bmatrix} 0{,}30 \\ 0 \\ 0{,}08 \end{bmatrix}
=
\begin{bmatrix} 0{,}20 \\ 0 \\ 0 \end{bmatrix}
+
\begin{bmatrix} 0{,}10 \\ 0 \\ 0{,}08 \end{bmatrix}\]

&lt;p&gt;Nếu bạn khai báo góc yaw của LiDAR là $\pi$ (lắp ngược, trục $+x$ của LiDAR hướng về phía đuôi xe), điểm $(0{,}20, 0, 0)$ sẽ được quay $180^\circ$ thành $(-0{,}20, 0, 0)$ trước khi cộng $\mathbf{t}$, cho kết quả $(-0{,}10,\ 0,\ 0{,}08)$. Khi đó bức tường phía trước lại hiện ra ở phía sau robot vì thuật toán SLAM không thể tự biết bạn lắp ngược cảm biến.&lt;/p&gt;

&lt;p&gt;Đoạn mã NumPy dưới đây kiểm chứng phép quay yaw $90^\circ$ và phép nhân ma trận đồng nhất; hãy chạy nguyên khối mã này:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;rot_z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;rot_z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;allclose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;quat_yaw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# ROS order: x, y, z, w. Axis is +z.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;half&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;half&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;quat_yaw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;allclose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-9&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;homogeneous_2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Điểm (0.2, 0) trong frame lidar, lidar ở x=0.10, yaw=0.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;homogeneous_2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;allclose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;quat_90&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Kết quả in ra phải là quaternion &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[0. 0. 0.7071 0.7071]&lt;/code&gt; và tọa độ điểm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[0.3 0. 1.]&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Bài thực hành này không cần phần cứng điện tử; chỉ cần máy tính cài sẵn Python 3 và NumPy.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Tạo tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;math_tf_lab.py&lt;/code&gt; với đoạn mã trên, chạy chương trình và dán dòng kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print&lt;/code&gt; vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Đổi góc yaw của LiDAR trong mã thành $\pi$ rồi in ra tọa độ điểm mới. Giá trị $x$ phải âm (bằng $-0{,}10$ như tính tay ở trên); nếu vẫn dương thì dấu trong hàm quay đang bị sai.&lt;/li&gt;
  &lt;li&gt;Thực hiện phép nhân ghép biến đổi: từ thân xe tới LiDAR ($t_x = 0{,}10$, yaw 0), rồi từ LiDAR tới điểm quét ($0{,}20$ theo $+x$ của LiDAR). Kết quả tích $T_{\mathrm{base}\leftarrow\mathrm{laser}}\, p_{\mathrm{laser}}$ phải trùng khớp với bước 1.&lt;/li&gt;
  &lt;li&gt;Ghi lại một câu ghi nhớ cho Chương 09: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static_transform_publisher&lt;/code&gt; nhận tham số tịnh tiến theo đơn vị mét và quaternion theo thứ tự $(x, y, z, w)$, không nhập trực tiếp góc yaw tính bằng độ vào ô $z$.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Chương trình chạy không phát sinh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AssertionError&lt;/code&gt;; sổ tay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; lưu đầy đủ quaternion ứng với yaw $90^\circ$ và hai tọa độ điểm (khi yaw bằng 0 và khi yaw bằng $\pi$).&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Cần kiểm&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;$(1, 0)$ quay $90^\circ$ thành $(0, -1)$&lt;/td&gt;
      &lt;td&gt;Dấu trong ma trận: ô trên bên phải phải là $-\sin$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Quaternion yaw $90^\circ$ là $(0, 0, 1, 0)$&lt;/td&gt;
      &lt;td&gt;Bạn đã bỏ nửa góc. $(0, 0, 1, 0)$ là $180^\circ$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điểm ra hàng chục mét&lt;/td&gt;
      &lt;td&gt;$t$ đang là centimét. TF dùng mét&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tích hai ma trận đúng từng cái nhưng điểm cuối sai&lt;/td&gt;
      &lt;td&gt;Thứ tự nhân. Frame cha đứng bên trái&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; cài NumPy xong mà &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3&lt;/code&gt; không thấy&lt;/td&gt;
      &lt;td&gt;Hai bản Python. Gọi đúng interpreter bạn sẽ dùng ở chương 09&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [Linear transformations and matrices&lt;/td&gt;
          &lt;td&gt;Chapter 3, Essence of linear algebra](https://www.youtube.com/watch?v=kYB8IZa5AuE) — 3Blue1Brown, tiếng Anh. Giải thích trực quan ý nghĩa hình học của phép nhân ma trận quay và biến đổi vector.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=zjMuIxRvygQ&quot;&gt;Quaternions and 3d rotation, explained interactively&lt;/a&gt; — 3Blue1Brown, tiếng Anh. Trực quan hóa quaternion trong phép quay 3D; lưu ý ROS sử dụng thứ tự &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(x, y, z, w)&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vlb3P7arbkU&quot;&gt;Modern Robotics, Chapter 3.3.1:  Homogeneous Transformation Matrices&lt;/a&gt; — Northwestern Robotics, tiếng Anh. Trình bày ma trận biến đổi đồng nhất $4 \times 4$ trong giáo trình &lt;em&gt;Modern Robotics&lt;/em&gt;, nền tảng trực tiếp cho TF2.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Với góc quay yaw $-90^\circ$ (rẽ phải), quaternion $(x, y, z, w)$ có giá trị bằng bao nhiêu và vector $(1, 0)$ biến đổi thành vector nào?&lt;/li&gt;
  &lt;li&gt;Xét phép biến đổi đồng nhất $T$ có tịnh tiến $(0{,}10,\ 0)$ và góc quay $\pi/2$. Tọa độ điểm $(1, 0)$ trong hệ con chuyển sang hệ cha là bao nhiêu? Hãy chú ý đúng thứ tự nhân.&lt;/li&gt;
  &lt;li&gt;Vì sao không thể tạo ra vận tốc ngang $v_y$ cho robot vi sai chỉ bằng cách thêm một hệ số vào ma trận quay?&lt;/li&gt;
  &lt;li&gt;Hai publisher cùng phát phép biến đổi từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;, một bên gửi $t_x = 0{,}10$ và bên kia gửi $t_x = -0{,}10$. Phép biến đổi nào sẽ được chọn?&lt;/li&gt;
  &lt;li&gt;Trong sổ tay TF, một sinh viên chuyển giá trị $0{,}7071$ thành “góc $45^\circ$”. Con số $45^\circ$ này là nửa góc hay góc yaw thực tế?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;$z = -\sqrt{2}/2$, $w = \sqrt{2}/2$, $x = y = 0$; vector $(1, 0)$ biến thành $(0, -1)$.&lt;/li&gt;
  &lt;li&gt;Theo cấu trúc ma trận đồng nhất $x’ = Rp + t$, phép quay thực hiện trước: $(1, 0)$ quay thành $(0, 1)$, sau đó cộng tịnh tiến $t$ ra $(0{,}10,\ 1)$. Nếu cộng $t$ trước rồi mới quay thì $(1{,}10,\ 0)$ sẽ thành $(0,\ 1{,}10)$ — hai thứ tự cho hai điểm hoàn toàn khác nhau.&lt;/li&gt;
  &lt;li&gt;Bánh xe vi sai không có khả năng tự trượt ngang; ma trận quay chỉ mô tả hướng của thân xe chứ không tạo ra bậc tự do vật lý mới.&lt;/li&gt;
  &lt;li&gt;Không có nguồn nào “thắng” hợp lệ; TF2 sẽ báo xung đột vì cùng một cạnh cây có hai nguồn phát. Cần tắt một trong hai publisher.&lt;/li&gt;
  &lt;li&gt;Giá trị $0{,}7071$ là $\sin$ và $\cos$ của nửa góc ($45^\circ$), nên góc yaw thực tế là $90^\circ$.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; — quy ước trục tọa độ và đơn vị chuẩn trong ROS.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; — hệ tọa độ chuẩn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-TF2.html&quot;&gt;TF2 trong tài liệu Jazzy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://modernrobotics.northwestern.edu/&quot;&gt;Modern Robotics, bản mở&lt;/a&gt; — chương biến đổi không gian cứng nâng cao.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist capstone chương 07</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_06_ch07_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_06_ch07_lab_checklist</id>
   <content type="html">&lt;p&gt;Đây là biên bản nghiệm thu cho giai đoạn hoàn thiện khung robot vận hành đầu tiên. Bạn chỉ chuyển sang Chương 08 khi có đủ bằng chứng cho cả bốn nhóm tiêu chí, hoặc ghi rõ một lỗi cụ thể cần khắc phục trước khi tiếp tục.&lt;/p&gt;

&lt;p&gt;Capstone Chương 07 là cột mốc chuyển giao quan trọng nhất của nửa đầu khóa học: lần đầu tiên bạn sở hữu một con robot vật lý hoàn chỉnh có thể chạy ổn định, nhận lệnh có kiểm soát và tự dừng an toàn khi có sự cố — sẵn sàng để ở Chương 08–10 ta lắp thêm toán học TF2, điều khiển PID và hệ điều hành ROS 2 lên trên. Checklist Capstone giúp bạn nghiệm thu toàn diện cả 4 mặt: &lt;strong&gt;an toàn điện, cơ khí, truyền thông teleop và hồ sơ log&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-đánh-giá-độ-tin-cậy-hệ-thống-tích-hợp-system-integration&quot;&gt;Kiến thức cần nhớ: Đánh giá độ tin cậy hệ thống tích hợp (System Integration)&lt;/h2&gt;

&lt;h3 id=&quot;1-thống-kê-độ-tin-cậy-kênh-truyền-thông-packet-delivery-ratio--jitter&quot;&gt;1. Thống kê độ tin cậy kênh truyền thông (Packet Delivery Ratio &amp;amp; Jitter)&lt;/h3&gt;
&lt;p&gt;Trong hệ thống điều khiển không dây hoặc qua cáp USB gần động cơ DC, tỷ lệ mất gói trung bình (PDR) chưa kể hết câu chuyện — thứ gây kích hoạt Watchdog giả là &lt;strong&gt;độ trễ biến thiên (jitter)&lt;/strong&gt; và số gói mất &lt;em&gt;liên tiếp&lt;/em&gt; tối đa. Theo dõi cả PDR lẫn khoảng trống tối đa giữa hai gói (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_gap_ms&lt;/code&gt;) cho bức tranh sức khỏe kênh truyền đầy đủ.&lt;/p&gt;

&lt;h3 id=&quot;2-kiểm-tra-ranh-giới-an-toàn-kép-phần-cứng--phần-mềm&quot;&gt;2. Kiểm tra ranh giới an toàn kép (Phần cứng + Phần mềm)&lt;/h3&gt;
&lt;p&gt;Một robot Capstone đạt chuẩn luôn vượt qua đồng thời hai bài thử: (1) rút nguồn / gạt công tắc E-Stop vật lý ngắt toàn bộ động cơ ngay lập tức, và (2) đóng cửa sổ chương trình điều khiển trên máy tính thì Watchdog phần mềm tự phanh xe trong chưa đầy nửa giây.&lt;/p&gt;

&lt;h3 id=&quot;3-tính-tái-lập-reproducibility-và-tài-liệu-bàn-giao&quot;&gt;3. Tính tái lập (Reproducibility) và tài liệu bàn giao&lt;/h3&gt;
&lt;p&gt;Một hệ thống chỉ thực sự “xong” khi người khác (hoặc chính bạn sau 1 tháng) đọc sơ đồ nối dây, bảng phân chân GPIO và tệp cấu hình là có thể bật lên chạy lại y hệt trong 5 phút.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-kiểm-tra-chỉ-số-chất-lượng-kênh-teleop-từ-tệp-log-10-giây&quot;&gt;Giải thích: Cầm tay kiểm tra chỉ số chất lượng kênh Teleop từ tệp log 10 giây&lt;/h3&gt;

&lt;p&gt;Trong bài chạy nghiệm thu kéo dài &lt;strong&gt;10 giây&lt;/strong&gt;, máy tính gửi lệnh ở tần số &lt;strong&gt;20 Hz&lt;/strong&gt; (chu kỳ danh định \(50\text{ ms}\), tổng cộng &lt;strong&gt;200 gói&lt;/strong&gt; đã gửi). Firmware đặt ngưỡng Watchdog &lt;strong&gt;\(T_{\text{wd}} = 300\text{ ms}\)&lt;/strong&gt; và ghi nhận từ tệp log:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Tổng số gói hợp lệ (đúng checksum) nhận được: &lt;strong&gt;198 gói&lt;/strong&gt; (mất 2 gói riêng lẻ không liên tiếp).&lt;/li&gt;
  &lt;li&gt;Khoảng thời gian dài nhất đo được giữa hai gói hợp lệ liên tiếp: &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_gap_ms = 104 ms&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Đánh giá định lượng 2 tiêu chí nghiệm thu:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tỷ lệ nhận gói thành công (Packet Delivery Ratio — PDR):&lt;/strong&gt;
\(\mathrm{PDR} = \frac{198}{200} = 99{,}0\% \ge 98\% \implies \textbf{ĐẠT}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Biên an toàn chống kích hoạt Watchdog giả:&lt;/strong&gt;
\(\frac{T_{\text{wd}}}{\text{max\_gap\_ms}} = \frac{300\text{ ms}}{104\text{ ms}} \approx 2{,}88\times\)
&lt;em&gt;(Khoảng trống lớn nhất chỉ bằng \(35\%\) ngưỡng Watchdog, chứng tỏ hệ thống còn dư biên chịu đựng gần 3 lần — vừa đủ nhạy để dừng an toàn khi đứt mạng thật, vừa không bị giật cục do kích hoạt nhầm!)&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đánh giá sổ tay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; theo đủ bốn tiêu chí hoàn thành: điều khiển teleop bốn hướng, dừng an toàn khi máy chủ quá hạn, cố định pin an toàn không có đầu hàn lithium trần, và đầy đủ hồ sơ ghi chú (ảnh đi dây, giao thức, bảng dấu, điện áp).&lt;/li&gt;
  &lt;li&gt;Phân tích một biên bản trượt (ví dụ xe vẫn trôi sau khi laptop ngủ, hoặc bánh phải ngược chiều) và chỉ rõ đúng một hạng mục bắt buộc phải sửa trước khi học PID hoặc ROS.&lt;/li&gt;
  &lt;li&gt;Phân biệt rõ một bản nghiệm thu đạt thực sự với một sổ tay chép đủ tiêu đề nhưng bỏ trống số liệu đo.&lt;/li&gt;
  &lt;li&gt;Liệt kê gọn đúng các linh kiện còn thiếu để hoàn tất Capstone A kèm nguồn mua tham khảo của cả chương.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Toàn bộ bằng chứng từ Bài 07-01 đến 07-05: lắp ráp nguồn, bảng dấu trên hộp kê, khung lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, bước kéo PWM và timeout, cùng nhật ký chạy thực địa. Chương 05 cung cấp nền tảng cầu H, chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;, jumper 7805 và dòng kẹt sau cầu chì 2–3 A; Chương 06 cung cấp \(b\) và \(r\) (ví dụ \(b = 0{,}15~\mathrm{m}\) hoặc \(148~\mathrm{mm}\) đo thực tế, và \(r = 31{,}2~\mathrm{mm}\)). Chương 08 sẽ xây dựng vòng điều khiển PID và Chương 09 chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;). Không chương sau nào tự sửa hộ lỗi thiếu timeout hay ngược cực động cơ. Bài này chưa cài đặt ROS.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Bộ điều khiển PID khuếch đại phản hồi từ phần cứng: nếu bánh phải bị ngược chiều, sai số dương sẽ biến thành lực đẩy sai hướng ngày càng lớn. Trên ROS, nếu firmware vẫn giữ mức PWM cuối khi mất gói tin, việc phát topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; cũng gặp đúng rủi ro như cổng serial. Do đó, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; chỉ được phép thay thế giao thức văn bản sau khi toàn bộ bốn tiêu chí dưới đây đều đạt. Đồng thời, trước khi sang Chương 08, tuyệt đối không gán cứng PWM 80 tương đương \(0{,}2~\mathrm{m/s}\).&lt;/p&gt;

&lt;h2 id=&quot;định-nghĩa-xong-việc&quot;&gt;Định nghĩa xong việc&lt;/h2&gt;

&lt;p&gt;Cả bốn tiêu chí sau đều bắt buộc:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Teleop khớp bảng dấu (Bài 02):&lt;/strong&gt; Hai nấc dương trong khoảng \(\pm 180\) đẩy cả hai lốp tiến về phía mũi xe; hai nấc âm làm xe lùi; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; quay trái (bánh phải nhanh hơn cho yaw dương); &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt; quay phải. Máy chủ gửi lặp lại ít nhất mỗi \(200~\mathrm{ms}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cơ chế quá hạn (timeout) hoạt động thực sự:&lt;/strong&gt; Sau \(250~\mathrm{ms}\) không nhận dòng hợp lệ, mục tiêu lập tức về 0 và bộ bước kéo (15 nấc mỗi \(20~\mathrm{ms}\)) hạ duty xuống 0 (khoảng \(330~\mathrm{ms}\) từ lệnh cuối bằng 60, nằm gọn trong \(0{,}5~\mathrm{s}\) khi ESP32 vẫn ăn nguồn từ mạch buck). Công tắc nguồn pin luôn sẵn sàng trong tay khi chạy trên sàn — công tắc vật lý và timeout bổ trợ cho nhau, cái này không thay thế cái kia.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nguồn và pin an toàn:&lt;/strong&gt; Pin được cố định chắc chắn giữa trục bánh và bánh caster, đặt trong đế pin hoặc pack có BMS, không để đầu hàn trần lòng thòng. Cực dương đi qua công tắc, cầu chì 2–3 A rồi tách sang VM và mạch buck. Đầu ra buck đã chỉnh \(5{,}0~\mathrm{V}\) trước khi gắn ESP32; dây động cơ bắt trực tiếp trên cầu đấu vít; cực âm pin, GND driver, GND buck và GND MCU nối chung về một nút.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hồ sơ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; đầy đủ:&lt;/strong&gt; Có ảnh đi dây, đặc tả giao thức (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, 115200 8N1, \(200~\mathrm{ms}\), \(250~\mathrm{ms}\)), bảng dấu, điện áp pack không tải, điện áp đầu ra buck và kích thước \(b\), \(r\). Mọi ô để trống đều tính là thiếu bằng chứng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;một-sổ-trượt-và-chỗ-sửa-chặn-chương-08&quot;&gt;Một sổ trượt, và chỗ sửa chặn chương 08&lt;/h2&gt;

&lt;p&gt;Dưới đây là một ví dụ nhật ký &lt;strong&gt;không được ký nghiệm thu&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Giờ&lt;/th&gt;
      &lt;th&gt;Lệnh&lt;/th&gt;
      &lt;th&gt;Robot làm gì&lt;/th&gt;
      &lt;th&gt;Điện áp&lt;/th&gt;
      &lt;th&gt;Giả thuyết&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;0:10&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;thẳng 2 s&lt;/td&gt;
      &lt;td&gt;nghỉ 8,10 V, sau 7,95 V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0:20&lt;/td&gt;
      &lt;td&gt;gập laptop&lt;/td&gt;
      &lt;td&gt;còn lăn, chạm băng cuối&lt;/td&gt;
      &lt;td&gt;7,9 V&lt;/td&gt;
      &lt;td&gt;timeout chưa viết; LEDC giữ 80&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Hàng đi thẳng có thể rất tốt, nhưng hàng thứ hai đã đánh trượt toàn bộ biên bản. Cả Chương 08 và Chương 09 đều nhận lệnh từ một máy chủ có thể treo hoặc mất kết nối bất kỳ lúc nào. Nếu thiếu đoạn kiểm tra \(250~\mathrm{ms}\) trong firmware, thanh ghi LEDC sẽ giữ nguyên mức duty cuối. Bạn cần bổ sung timeout, kiểm chứng lại trên hộp kê rằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; dừng trong \(0{,}5~\mathrm{s}\) khi ngắt máy gửi rồi mới chạy lại trên sàn; thói quen nhanh tay rút pin không thể thay thế cơ chế an toàn phần mềm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lỗi chặn thứ hai:&lt;/strong&gt; Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; làm xe xoay tròn và quan sát trên hộp kê cho thấy bánh phải quay ngược. Cách khắc phục là đảo hai dây động cơ phải &lt;strong&gt;hoặc&lt;/strong&gt; đảo cực tính chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt; bên phải (chỉ làm một lần và ghi rõ vào sổ). Nếu làm cả hai, dấu \(-1\) vẫn còn nguyên, khiến PID cộng sai chiều và Chương 09 hiểu sai hướng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lỗi chặn thứ ba:&lt;/strong&gt; Hai cell 18650 có đầu hàn trần chỉ dán băng dính lên khung. Một cú chạm chập xảy ra trước cầu chì (hoặc khi không có cầu chì) sẽ vượt ngoài khả năng can thiệp của timeout. Bắt buộc phải lắp đế pin hoặc pack 2S có BMS, công tắc nguồn, cầu chì 2–3 A và chụp ảnh xác nhận. Lưu ý không dùng mạch sạc 1S TP4056 (đầy gần \(4{,}2~\mathrm{V}\)) cho pack 2S (đầy gần \(8{,}4~\mathrm{V}\)).&lt;/p&gt;

&lt;p&gt;Chỉ ký nghiệm thu khi nhật ký mới ghi đủ bốn hướng, thời gian dừng dưới \(0{,}5~\mathrm{s}\), điện áp nghỉ trước và sau ổn định (ví dụ \(8{,}15~\mathrm{V}\) xuống \(8{,}05~\mathrm{V}\) sau lượt ngắn), ảnh đi dây và số đo hình học. Nếu điện áp pack sụt từ \(8{,}20~\mathrm{V}\) xuống \(6{,}90~\mathrm{V}\) ở dòng \(0{,}60~\mathrm{A}\):&lt;/p&gt;

\[R \approx \frac{1{,}30}{0{,}60} \approx 2{,}2~\Omega,\]

&lt;p&gt;bạn chưa được ký nghiệm thu vì đây là lỗi phần cứng nguồn cần khắc phục ngay.&lt;/p&gt;

&lt;h2 id=&quot;chương-08-và-chương-09-được-phép-giả-định-gì&quot;&gt;Chương 08 và chương 09 được phép giả định gì&lt;/h2&gt;

&lt;p&gt;Chương 08 mặc định rằng lệnh trái dương và lệnh phải dương đều đẩy xe tiến về phía mũi, PWM 0 làm xe dừng và hai thông số \(b\), \(r\) đã có sẵn trong sổ tay. Chương 08 tập trung xây dựng vòng điều khiển tốc độ, không quay lại dò chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Chương 09 mặc định rằng khung xe tự dừng khi luồng lệnh bị ngắt. Thông điệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; mang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; sẽ thay cho hai nấc PWM, quy đổi qua phương trình Chương 06 và phép hiệu chuẩn Chương 08 (không ghi “80 nấc = \(0{,}2~\mathrm{m/s}\)” vào biên bản). Đặc tả giao thức trong sổ tay chính là chuẩn tối thiểu mà lớp tích hợp ROS sau này phải duy trì: quan sát được, có giới hạn an toàn và dừng khi mất kết nối máy chủ.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/teleop_states.png&quot; alt=&quot;Chỉ chạy khi lệnh còn tươi; im lặng là dừng&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Nếu nhật ký chưa có số đo thực tế chứng minh hệ thống chuyển sang trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STOP&lt;/code&gt; khi quá hạn thì checklist chưa hoàn thành.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Chỉ chạy lại chuỗi thử nghiệm trên sàn sau khi đã khắc phục xong lỗi bị trượt; nếu không, chỉ rà soát nhật ký trên giấy. Luôn giữ công tắc pin trong tầm với mỗi khi lốp chạm sàn. Không ký nghiệm thu cho robot chưa kiểm chứng dừng bằng timeout và ngừng thao tác ngay nếu phát hiện đầu pin lithium trần.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Việc&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; bài 01–05&lt;/td&gt;
      &lt;td&gt;Bằng chứng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot, chỉ khi một hàng phải chạy lại&lt;/td&gt;
      &lt;td&gt;Lượt thử lại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ&lt;/td&gt;
      &lt;td&gt;Kiểm buck 5,0 V và điện áp pack nếu ảnh đã cũ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Checklist này&lt;/td&gt;
      &lt;td&gt;Bốn dòng bạn ký tắt&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Kiểm tra lại ảnh đi dây trong sổ: phải thấy rõ công tắc, cầu chì, dây buộc pin, điểm mass chung, dây động cơ bắt vít và số đo mạch buck xấp xỉ \(5{,}0~\mathrm{V}\) trước khi gắn ESP32.&lt;/li&gt;
  &lt;li&gt;Ký xác nhận bốn hướng (tiến, lùi, quay trái, quay phải) chỉ khi dòng nhật ký khớp đúng với lệnh đã gửi.&lt;/li&gt;
  &lt;li&gt;Đối chiếu hàng kiểm tra timeout: ngắt máy gửi hoặc rút USB trong khi MCU vẫn ăn nguồn buck, xe dừng hẳn trong \(0{,}5~\mathrm{s}\). Thiếu dòng này đồng nghĩa chương chưa xong.&lt;/li&gt;
  &lt;li&gt;Rà soát sự có mặt của \(b\), \(r\), đặc tả giao thức và các mức điện áp. Ghi rõ ngày đủ điều kiện bắt đầu Chương 08, hoặc ghi tên một lỗi cụ thể đang chặn.&lt;/li&gt;
  &lt;li&gt;Nếu phải chạy lại, tuân thủ đúng quy trình làn sàn của Bài 05: sửa đúng một nguyên nhân, chạy lại toàn bộ chuỗi và ghi nhật ký mới.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Một đoạn xác nhận ngắn gọn trong sổ tay gồm: ngày đạt đủ bốn hướng, thời gian dừng timeout đo được, điện áp pack, giá trị \(b\) và \(r\), kèm kết luận “đủ điều kiện bắt đầu Chương 08” hoặc tên lỗi cụ thể đang cần xử lý. Các bảng để trống không được tính.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Đề mục chép đủ, ô trống&lt;/td&gt;
      &lt;td&gt;Chương chưa làm; điền từ một lượt thật&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;“Dừng” chỉ vì rút pin&lt;/td&gt;
      &lt;td&gt;Timeout chưa được thử; rút pin là nút giết, không phải hạn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cả dây lẫn firmware đều “đã sửa”&lt;/td&gt;
      &lt;td&gt;Hai lần đảo triệt nhau; quay về một lần&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thiếu hình học&lt;/td&gt;
      &lt;td&gt;Chương 08 sẽ bịa bề rộng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;PWM 80 bị viết thành \(0{,}2~\mathrm{m/s}\)&lt;/td&gt;
      &lt;td&gt;Hiệu chuẩn giả; xóa trước PID&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Chỉ đặt mua đúng các hạng mục còn thiếu trong bảng nghiệm thu. Giá tham khảo khoảng năm 2026 (có thể thay đổi): khung 2WD 60.000–150.000 đồng, động cơ TT 25.000–45.000 đồng/chiếc, TB6612 25.000–70.000 đồng, mạch buck LM2596 10.000–25.000 đồng, pack 2S hoặc đế 18650 80.000–180.000 đồng, công tắc 8.000–20.000 đồng, đế chì kèm chì 2–3 A 10.000–25.000 đồng, ESP32 devkit 70.000–150.000 đồng. Có thể dùng L298N thay TB6612 khi hết hàng; lưu ý không mua TP4056 để sạc pack 2S.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;L298N đã kiểm, khoảng 45.000 đồng: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;Hshop TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=LM2596&quot;&gt;LM2596&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=ESP32&quot;&gt;ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=khung%20xe%202WD&quot;&gt;Shopee khung 2WD&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=c%E1%BA%A7u%20ch%C3%AC%202A&quot;&gt;cầu chì&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=pin%202S%2018650&quot;&gt;pin 2S&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=TB6612&quot;&gt;Lazada TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=ESP32&quot;&gt;ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=LM2596&quot;&gt;Thế Giới IC LM2596&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=JPPTRj0KWbg&quot;&gt;TB6612FNG H-Bridge Motor Controller - Better than L298N?&lt;/a&gt; — DroneBot Workshop, tiếng Anh. Ôn lại việc kiểm soát chiều và tốc độ động cơ qua cầu H — yêu cầu nền tảng tối thiểu của checklist Capstone A.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nhật ký ghi nhận lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; đi thẳng tốt, nhưng xe vẫn tiếp tục lăn sau khi gập laptop. Tiêu chí hoàn thành nào bị trượt, và cần chứng minh điều gì trên hộp kê trước khi chuyển sang Chương 08 hoặc 09?&lt;/li&gt;
  &lt;li&gt;Bốn hướng chạy thử trước đó đều đạt, nhưng ghi chú bánh phải cho thấy người lắp vừa đảo dây vừa đảo thêm dấu trong firmware “cho chắc”. Lệnh PWM dương hiện tại sẽ làm bánh phải quay thế nào và cần gỡ bước nào?&lt;/li&gt;
  &lt;li&gt;Điện áp nghỉ đo được \(8{,}20~\mathrm{V}\), khi có tải \(0{,}60~\mathrm{A}\) sụt còn \(6{,}90~\mathrm{V}\), đồng thời ảnh chụp cho thấy hai cell pin có đầu hàn trần. Tiêu chí nào bị trượt và nội trở ước lượng \(R\) bằng bao nhiêu?&lt;/li&gt;
  &lt;li&gt;Một sinh viên tự đặt PWM 80 bằng \(0{,}20~\mathrm{m/s}\) rồi áp cùng hệ số đó cho lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; với \(b = 0{,}15~\mathrm{m}\). Họ tính ra \(\omega\) bằng bao nhiêu, và vì sao hệ số quy đổi này không hợp lệ?&lt;/li&gt;
  &lt;li&gt;Từ lệnh PWM 60, xe mất \(1{,}2~\mathrm{s}\) mới dừng hẳn sau khi ngắt máy gửi. Với bước kéo 15 nấc mỗi \(20~\mathrm{ms}\) và hạn timeout \(250~\mathrm{ms}\), thời gian dừng đúng thiết kế phải là bao nhiêu?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Tiêu chí “dừng khi máy chủ quá hạn” bị trượt. Cần kê xe lên hộp, gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; rồi ngắt máy gửi và xác nhận cả hai lốp dừng trong \(0{,}5~\mathrm{s}\) (khi MCU vẫn ăn nguồn buck) trước khi chạy lại trên sàn.&lt;/li&gt;
  &lt;li&gt;Hai lần đảo triệt tiêu nhau khiến lệnh dương vẫn làm bánh phải quay lùi. Cần kê bánh trên không và gỡ bỏ một trong hai lần đảo.&lt;/li&gt;
  &lt;li&gt;Đầu hàn trần làm trượt tiêu chí an toàn pin, và tổng trở \(R \approx 1{,}30/0{,}60 \approx 2{,}2~\Omega\) làm trượt tiêu chí nguồn. Cần lắp đế pin hoặc pack có BMS, công tắc nguồn và cầu chì 2–3 A.&lt;/li&gt;
  &lt;li&gt;Hệ số \(0{,}20/80 = 0{,}0025\) m/s mỗi nấc cho \(v_l = -0{,}15\) m/s và \(v_r = +0{,}15\) m/s, suy ra \(v = 0\) và \(\omega = 0{,}30/0{,}15 = 2\) rad/s. Con số này không hợp lệ vì hệ số quy đổi chỉ là tự gán, chưa hề đo hiệu chuẩn.&lt;/li&gt;
  &lt;li&gt;Thời gian dừng đúng thiết kế phải là \(250 + 4\times 20 = 330~\mathrm{ms}\). Việc xe trôi tới \(1{,}2~\mathrm{s}\) cho thấy cơ chế timeout đang bị thiếu hoặc đặt quá dài — không được ký nghiệm thu.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/&quot;&gt;Mục lục gói geometry_msgs (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;Datasheet L298 của ST&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Test thực địa, log và nghiệm thu</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_05_field_acceptance/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_05_field_acceptance</id>
   <content type="html">&lt;p&gt;Đã kết thúc giai đoạn thử trên hộp kê. Trong bài này, bạn đặt robot xuống một làn sàn trống, chạy thử đủ bốn hướng ở mức PWM đã giới hạn và lập nhật ký (log) đủ tin cậy làm cơ sở cho chương sau. Tiêu chuẩn đạt dựa trên tập các quan sát đo được cụ thể, không dựa vào cảm tính “xe chạy được là ổn”.&lt;/p&gt;

&lt;p&gt;Trong thế giới phần mềm, bạn viết unit test để biết code đúng hay sai. Trong thế giới robotics, unit test trên laptop không bao giờ đủ — một robot chỉ được coi là hoàn thiện khi vượt qua &lt;strong&gt;bài kiểm tra nghiệm thu thực địa (Field Acceptance Test)&lt;/strong&gt; trên mặt sàn thật với các tiêu chí &lt;strong&gt;đạt / không đạt (Pass/Fail) định lượng&lt;/strong&gt; rõ ràng và có tệp nhật ký (log) chứng minh! Nếu không có bài test định lượng, bạn sẽ không bao giờ biết lần sửa code mới nhất giúp robot chạy thẳng hơn hay thực ra làm nó tệ đi.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kiểm-thử-thực-địa-ghi-nhật-ký-đồng-bộ-và-tiêu-chí-nghiệm-thu&quot;&gt;Kiến thức cần nhớ: Kiểm thử thực địa, Ghi nhật ký đồng bộ và Tiêu chí nghiệm thu&lt;/h2&gt;

&lt;h3 id=&quot;1-bộ-4-bài-test-thực-địa-kinh-điển-cho-robot-vi-sai&quot;&gt;1. Bộ 4 bài test thực địa kinh điển cho robot vi sai&lt;/h3&gt;
&lt;p&gt;Một quy trình nghiệm thu chuẩn gồm 4 kịch bản lặp lại được: (1) &lt;strong&gt;Chạy thẳng 3–5 m&lt;/strong&gt; đo độ lệch ngang (đánh giá cân bằng bánh &amp;amp; odometry); (2) &lt;strong&gt;Quay tại chỗ \(360^\circ\)&lt;/strong&gt; đo sai số góc (đánh giá hiệu chuẩn khoảng cách hai bánh \(L\)); (3) &lt;strong&gt;Test ngắt kết nối đột ngột&lt;/strong&gt; kiểm tra Watchdog dừng đúng ngưỡng; và (4) &lt;strong&gt;Test chạy bền 10 phút&lt;/strong&gt; giám sát nhiệt độ driver và đường cong tụt áp pin.&lt;/p&gt;

&lt;h3 id=&quot;2-ghi-log-có-dấu-thời-gian-timestamped-telemetry-csv&quot;&gt;2. Ghi log có dấu thời gian (Timestamped Telemetry CSV)&lt;/h3&gt;
&lt;p&gt;Mắt thường rất dễ đánh lừa: bạn thấy xe “hơi giật”, nhưng không biết do mất gói UART, do điện áp pin tụt, hay do encoder nhảy xung. Ghi log định dạng CSV gồm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(timestamp_ms, cmd_v, cmd_w, enc_L, enc_R, v_bat)&lt;/code&gt; giúp bạn vẽ đồ thị phân tích hậu kiểm chính xác đến từng mili-giây.&lt;/p&gt;

&lt;h3 id=&quot;3-nghiệm-thu-dựa-trên-ngưỡng-định-lượng-acceptance-criteria&quot;&gt;3. Nghiệm thu dựa trên ngưỡng định lượng (Acceptance Criteria)&lt;/h3&gt;
&lt;p&gt;Mọi yêu cầu phải chuyển thành con số đo được trước khi chạy test (ví dụ: lệch ngang \(&amp;lt; 5\%\) quãng đường, sai số quay \(360^\circ\) nhỏ hơn \(\pm 10^\circ\), thời gian dừng khẩn cấp \(&amp;lt; 0{,}5\text{ s}\)).&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-đánh-giá-kết-quả-bài-test-chạy-thẳng-5-mét-và-truy-ngược-sai-số-đường-kính-bánh&quot;&gt;Giải thích: Cầm tay đánh giá kết quả bài test chạy thẳng 5 mét và truy ngược sai số đường kính bánh&lt;/h3&gt;

&lt;p&gt;Bạn dán một đường băng keo thẳng dài &lt;strong&gt;\(D = 5{,}00\text{ m}\)&lt;/strong&gt; trên sàn hành lang, đặt robot ngay vạch xuất phát rồi cho chạy thẳng \(5{,}00\text{ m}\) với tiêu chí nghiệm thu &lt;strong&gt;độ lệch ngang \(\le 5\%\) (\(\le 25\text{ cm}\))&lt;/strong&gt; và &lt;strong&gt;sai số quãng đường \(\le 3\%\) (\(\pm 15\text{ cm}\))&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Kết quả đo thực địa bằng thước dây:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Xe dừng ở tọa độ dọc \(x = 4{,}92\text{ m}\) và lệch sang trái \(\Delta y = 0{,}15\text{ m}\;(15\text{ cm})\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Kiểm tra các chỉ số nghiệm thu:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Sai số quãng đường dọc:&lt;/strong&gt;
\(\frac{\vert 4{,}92 - 5{,}00 \vert}{5{,}00} = \frac{0{,}08}{5{,}00} = 1{,}6\% \le 3\% \implies \textbf{ĐẠT}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Độ lệch ngang và góc lệch hướng tương đương:&lt;/strong&gt;
\(\text{Tỷ lệ lệch ngang} = \frac{0{,}15}{5{,}00} = 3{,}0\% \le 5\% \implies \textbf{ĐẠT}.\)
Góc lệch hướng trung bình: \(\theta_{\text{lệch}} \approx \arctan\!\left(\frac{0{,}15}{5{,}00}\right) \approx 1{,}72^\circ.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chẩn đoán nếu lần sau xe lệch tới \(\Delta y = 0{,}45\text{ m}\) (\(9\% \to \textbf{KHÔNG ĐẠT}\)):&lt;/strong&gt;
Vì xe đi theo cung tròn bán kính \(R \approx \frac{D^2}{2 \Delta y} = \frac{25}{0{,}90} \approx 27{,}8\text{ m}\), với track width \(L = 0{,}20\text{ m}\), hai bánh đang chênh nhau khoảng \(\frac{L}{R} = \frac{0{,}20}{27{,}8} \approx 0{,}72\%\) — chỉ cần chỉnh lại hệ số bù đường kính bánh trái/phải \(0{,}7\%\) trong cấu hình là xe lập tức chạy thẳng tắp!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Thực hiện chuỗi thử nghiệm trên làn sàn trống dài \(2~\mathrm{m}\): tiến khoảng \(2~\mathrm{s}\), dừng, lùi, quay trái và quay phải trong giới hạn \(\pm 180\), luôn giữ tay sẵn sàng tại công tắc nguồn pin.&lt;/li&gt;
  &lt;li&gt;Chứng minh rằng khi ngắt chương trình gửi lệnh hoặc rút cáp USB (trong khi ESP32 vẫn ăn nguồn từ mạch buck), robot dừng hẳn chuyển động trong vòng \(0{,}5~\mathrm{s}\).&lt;/li&gt;
  &lt;li&gt;Đo điện áp pin trước và sau lượt chạy; nếu thấy sụt áp bất thường khi có tải, xác định ngay là lỗi pack pin, dây dẫn hoặc cầu chì.&lt;/li&gt;
  &lt;li&gt;Ghi chép đầy đủ nhật ký gỡ lỗi (thời điểm, lệnh gửi, hành vi thực tế, điện áp, giả thuyết lỗi) và chỉ nghiệm thu xe khi bảng dấu, timeout, cố định pin và kích thước hình học Chương 06 đều có đầy đủ trong sổ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Các bài 07-01 đến 07-04 đã hoàn thiện toàn bộ hệ thống cơ sở: pin buộc chắc chắn, cực dương qua cầu chì, mạch buck 5,0 V, bảng dấu chiều quay, giao thức chữ, bước kéo PWM và timeout. Chương 05 cung cấp kiến thức về dòng kẹt và kiểm soát nhiệt mạch cầu. Hai kích thước \(b\) và \(r\) từ Chương 06 đã được lưu trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; (ví dụ mẫu \(b = 0{,}15~\mathrm{m}\) hoặc \(148~\mathrm{mm}\) đo thực tế, và \(r = 31{,}2~\mathrm{mm}\)). Bài này vẫn chưa dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;; bạn có thể ghi lại quãng đường và thời gian chạy như một số liệu quan sát, nhưng không được gán cứng thành hệ số quy đổi cho PWM — phần hiệu chuẩn sang m/s thuộc Chương 08.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Bộ điều khiển PID ở Chương 08 hoạt động dựa trên giả định chiều quay hai bánh đã đúng dấu và bánh tự dừng khi mất lệnh. Nếu bánh phải bị đảo chiều, khâu tỉ lệ sẽ khuếch đại sai số quay theo hướng ngược lại. Tương tự, Chương 09 giả định khung xe luôn dừng an toàn khi node phát lệnh bị tắt (hoặc laptop rơi vào chế độ ngủ). Buổi nghiệm thu này là cổng kiểm tra cuối cùng của phần cứng cơ bản: bốn hướng khớp bảng dấu, dừng an toàn khi mất tín hiệu, pin cố định an toàn và sổ tay lưu đủ ảnh đi dây, đặc tả giao thức, điện áp cùng \(b\), \(r\).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;Hình học log phải dẫn: $$b$$ giữa hai vết lốp, $$r$$ từ phép lăn&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/diff_drive_kinematics.png&quot; alt=&quot;Tiến, quay, và trường hợp trộn mà bạn sắp xin bằng PWM, không bằng m/s&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Các mũi tên trong hình thứ hai biểu diễn trực quan từng trạng thái chuyển động: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; đẩy cả hai bánh tiến, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; cho bánh phải tiến và bánh trái lùi để quay trái khi bảng dấu chính xác. Lưu ý rằng công thức trên hình viết theo đơn vị vận tốc, trong khi lệnh bạn gửi hiện tại vẫn đang ở đơn vị nấc PWM.&lt;/p&gt;

&lt;h2 id=&quot;làn-công-tắc-giết-và-điện-áp-biết-động&quot;&gt;Làn, công tắc giết, và điện áp biết động&lt;/h2&gt;

&lt;p&gt;Dọn một làn sàn trống dài ít nhất \(2~\mathrm{m}\) (không có mép bậc hay vật cản trong quãng phanh) và luôn giữ công tắc pin trong tầm với. Thiết lập máy chủ gửi lặp lại mỗi \(200~\mathrm{ms}\), firmware quá hạn \(250~\mathrm{ms}\) và giữ mức duty ở 60 hoặc 80 (không dùng 255).&lt;/p&gt;

&lt;p&gt;Thực hiện tuần tự chuỗi năm bước sau (duy trì gửi lặp cho tới khi chuyển lệnh):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; trong khoảng \(2~\mathrm{s}\): cả hai lốp quay tiến, xe bám thẳng làn.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 0 0&lt;/code&gt; (hoặc ngừng gửi để kích hoạt timeout): xe dừng lại.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -80 -80&lt;/code&gt; trong khoảng \(2~\mathrm{s}\): xe lùi thẳng.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; trong một đoạn ngắn: mũi xe quay sang trái.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt; trong một đoạn ngắn: mũi xe quay sang phải.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Tiếp theo, tắt đột ngột tiến trình máy gửi hoặc rút cáp USB trong khi ESP32 vẫn nhận nguồn từ mạch buck. Xe phải dừng hẳn trong vòng \(0{,}5~\mathrm{s}\) (theo tính toán ở Bài 04 là khoảng \(330~\mathrm{ms}\) sau lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; cuối). Nếu xe vẫn trôi tới 3 giây thì bài nghiệm thu trượt, dù bốn hướng chạy đều đẹp.&lt;/p&gt;

&lt;p&gt;Đo điện áp nghỉ trước và sau lượt chạy: một pack pin tốt sau lượt thử ngắn thường chỉ giảm nhẹ, ví dụ từ \(8{,}15~\mathrm{V}\) xuống \(8{,}05~\mathrm{V}\). Nếu đo được khi đang có tải — giả sử nghỉ \(8{,}20~\mathrm{V}\) nhưng sụt xuống \(6{,}90~\mathrm{V}\) ở dòng \(0{,}60~\mathrm{A}\) — ta ước lượng tổng nội trở mạch nguồn là:&lt;/p&gt;

\[R \approx \frac{1{,}30}{0{,}60} \approx 2{,}2~\Omega.\]

&lt;p&gt;Giá trị \(2{,}2~\Omega\) là rất lớn so với mức vài trăm miliôm của một pack pin khỏe. Nguyên nhân thường nằm ở cell đã chai, dây dẫn quá mảnh, cầu chì tự phục hồi (polyfuse) bị nóng hoặc tiếp xúc đế pin kém. Cần xử lý triệt để lỗi nguồn này trước khi nghiệm thu vì nguồn yếu sẽ gây sụt áp và làm reset MCU giữa làn chạy (lưu ý hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt; đã đặt PWM về 0 trước khi kéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; cao).&lt;/p&gt;

&lt;p&gt;Nếu bạn bấm giờ một đoạn chạy thử — ví dụ xe đi \(1{,}6~\mathrm{m}\) trong \(4{,}0~\mathrm{s}\) — tốc độ trung bình quan sát được là:&lt;/p&gt;

\[v_{obs} = \frac{1{,}6}{4{,}0} = 0{,}40~\mathrm{m/s}.\]

&lt;p&gt;Hãy ghi nhận chính xác bối cảnh: “ở PWM 80, lượt thử trên sàn này đạt khoảng \(0{,}40~\mathrm{m/s}\)”, tuyệt đối không quy ước cứng “PWM 80 = \(0{,}40~\mathrm{m/s}\)” như một hằng số cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;log-là-dụng-cụ-nghiệm-thu&quot;&gt;Log là dụng cụ nghiệm thu&lt;/h2&gt;

&lt;p&gt;Mỗi hàng trong bảng nhật ký tương ứng với một sự kiện thực tế. Cột “Giả thuyết” để trống khi robot hành xử đúng bảng dấu, và bắt buộc phải ghi chẩn đoán khi có sai lệch:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Giờ&lt;/th&gt;
      &lt;th&gt;Lệnh&lt;/th&gt;
      &lt;th&gt;Robot làm gì&lt;/th&gt;
      &lt;th&gt;Điện áp&lt;/th&gt;
      &lt;th&gt;Giả thuyết&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;0:00&lt;/td&gt;
      &lt;td&gt;nghỉ&lt;/td&gt;
      &lt;td&gt;đứng, pin đã buộc&lt;/td&gt;
      &lt;td&gt;8,15 V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0:12&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; trong 2 s&lt;/td&gt;
      &lt;td&gt;thẳng theo làn&lt;/td&gt;
      &lt;td&gt;7,9 V có tải&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0:15&lt;/td&gt;
      &lt;td&gt;giết máy gửi&lt;/td&gt;
      &lt;td&gt;dừng rõ trong 0,5 s&lt;/td&gt;
      &lt;td&gt;8,12 V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0:30&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;mũi trái&lt;/td&gt;
      &lt;td&gt;8,0 V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0:40&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;mũi phải&lt;/td&gt;
      &lt;td&gt;8,0 V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Không được bỏ qua một hàng lỗi để chấm đạt chung. Nếu ngắt máy gửi mà xe vẫn chạy tiếp tới vạch băng dính thì cơ chế timeout chưa hoạt động (thanh ghi LEDC vẫn giữ PWM cũ); bạn phải quay lại Bài 04 để kiểm chứng dừng trên hộp kê trước khi chạy lại trên sàn. Nếu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; khiến xe xoay tròn thì một bên động cơ bị ngược dấu — chỉ đảo dây hoặc đảo cực tính firmware một lần duy nhất. Không chuyển sang Chương 08 khi còn bất kỳ lỗi nào chưa khép lại.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tiêu chí nghiệm thu đạt:&lt;/strong&gt; Đủ cả bốn hướng khớp bảng dấu; cơ chế dừng theo timeout hoạt động trong \(0{,}5~\mathrm{s}\); pin buộc chắc chắn và không có đầu hàn lithium trần; sổ tay lưu đầy đủ ảnh đi dây, đặc tả giao thức (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, \(200~\mathrm{ms}\), \(250~\mathrm{ms}\)), bảng dấu, các mức điện áp và kích thước \(b\), \(r\).&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Chuẩn bị làn sàn trống dài \(2~\mathrm{m}\) có thêm khoảng trống phanh và giữ công tắc pin sẵn sàng trong tay. Duy trì mức duty đã kiểm thử; ngắt công tắc ngay nếu xe lao về phía người hoặc mép bậc. Nếu xe kẹt vào tường, tối đa 1 giây phải ngắt nguồn; dừng ngay toàn bộ thử nghiệm nếu thấy mùi khét hoặc mạch driver quá nóng.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Việc&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cây nguồn và firmware đã nhận&lt;/td&gt;
      &lt;td&gt;Bài 01–04&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sàn trống, băng ở \(2~\mathrm{m}\)&lt;/td&gt;
      &lt;td&gt;Làn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ&lt;/td&gt;
      &lt;td&gt;Điện áp trước và sau&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Log&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Công tắc pin trong tay&lt;/td&gt;
      &lt;td&gt;Nút giết&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Kiểm tra bánh xe đã chạm sàn, bánh caster bám tốt, pin buộc chắc chắn và ảnh đi dây đã lưu trong sổ. Đo điện áp nghỉ của pack pin.&lt;/li&gt;
  &lt;li&gt;Sẵn sàng tay ở công tắc nguồn. Chạy lần lượt chuỗi: tiến khoảng \(2~\mathrm{s}\), dừng, lùi, quay trái và quay phải; đọc to hoặc ghi ngay lệnh vừa gửi để nhật ký trung thực.&lt;/li&gt;
  &lt;li&gt;Ngắt tiến trình máy gửi hoặc rút cáp USB (trong khi ESP32 vẫn ăn nguồn từ buck) và bấm giờ thời gian dừng; yêu cầu dưới \(0{,}5~\mathrm{s}\).&lt;/li&gt;
  &lt;li&gt;Đo lại điện áp nghỉ sau lượt chạy. Nếu phát hiện sụt áp mạnh khi có tải, tính ước lượng nội trở \(R\) và ghi giả thuyết nguyên nhân.&lt;/li&gt;
  &lt;li&gt;Chấm đạt hoặc trượt dựa theo đủ bốn tiêu chí nghiệm thu. Nếu trượt, sửa đúng một lỗi gốc rồi chạy lại toàn bộ chuỗi thử.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Một bảng nhật ký đã điền đầy đủ, xác nhận dừng hẳn trong nửa giây khi mất tín hiệu, điện áp nghỉ ổn định không sụt sâu, và chỉ ghi “đạt” khi hội tụ đủ hướng chạy, timeout, an toàn pin và bộ hồ sơ ghi chú (ảnh, giao thức, bảng dấu, \(b\), \(r\)). Mọi số đo tốc độ đều được gắn nhãn là số liệu quan sát riêng lẻ, không gắn cứng vào giao thức.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Lệnh thẳng mà xe vẽ vòng&lt;/td&gt;
      &lt;td&gt;Một dấu còn ngược; sửa một lần&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Còn lăn sau khi laptop ngủ&lt;/td&gt;
      &lt;td&gt;Sketch không có timeout&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mũi nhấc lúc tăng tốc&lt;/td&gt;
      &lt;td&gt;Khối pin nằm sau trục; quay lại bài 01&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điện áp sụt một volt trở lên ở dòng vừa phải&lt;/td&gt;
      &lt;td&gt;Cell, đế, dây, hoặc polyfuse&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chỉ dừng khi rút USB đồng thời reset chip&lt;/td&gt;
      &lt;td&gt;Buck không phải nguồn logic; thử lại khi MCU còn nuôi&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Các buổi nghiệm thu thường trượt do thiếu dây buộc cố định pin, đế pin tiếp xúc kém hoặc chưa lắp cầu chì. Giá tham khảo khoảng năm 2026 (có thể thay đổi): pack 2S hoặc đế 18650 80.000–180.000 đồng, công tắc 8.000–20.000 đồng, đế chì kèm cầu chì 2–3 A 10.000–25.000 đồng, mạch buck LM2596 10.000–25.000 đồng, TB6612 25.000–70.000 đồng (L298N giá khoảng 45.000 đồng là phương án thay thế tạm).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;L298N đã kiểm, khoảng 45.000 đồng: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=cau+chi&quot;&gt;Hshop cầu chì&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=LM2596&quot;&gt;LM2596&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=pin%202S%2018650&quot;&gt;Shopee pin 2S&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=c%C3%B4ng%20t%E1%BA%AFc%20ngu%E1%BB%93n&quot;&gt;công tắc&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=holder%2018650%202S&quot;&gt;Lazada holder 18650&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=cau%20chi&quot;&gt;Thế Giới IC cầu chì&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=2QE3LG5h2tw&quot;&gt;Differential Drive &amp;amp; Odometry Explained: How It Works&lt;/a&gt; — Netizen Robotics, tiếng Anh. Minh họa trực quan chuyển động đi thẳng khi hai bánh cùng tốc độ và quay khi có chênh lệch tốc độ — đúng các hành vi cần xác nhận trong buổi nghiệm thu.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Pack pin đo lúc nghỉ được \(8{,}20~\mathrm{V}\), khi cả hai động cơ chạy sụt xuống \(6{,}90~\mathrm{V}\) ở dòng khoảng \(0{,}60~\mathrm{A}\). Ước lượng tổng điện trở \(R\) và đánh giá hệ nguồn này có đạt chuẩn nghiệm thu hay không.&lt;/li&gt;
  &lt;li&gt;Xe chạy thẳng \(1{,}6~\mathrm{m}\) trong \(4{,}0~\mathrm{s}\) ở mức PWM 80. Bạn ghi tốc độ quan sát là bao nhiêu, và nhận định nào không được phép đưa vào ghi chú &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;?&lt;/li&gt;
  &lt;li&gt;Khi rút cáp USB trong lúc ESP32 vẫn được nuôi bằng mạch buck, xe vẫn lăn tiếp khoảng \(3~\mathrm{s}\). Tiêu chí nghiệm thu nào bị trượt và phần mã nguồn ở bài nào đang thiếu?&lt;/li&gt;
  &lt;li&gt;Xe đã hoàn thành chuỗi chạy trên sàn, sổ tay có đủ điện áp và ảnh đi dây nhưng để trống \(b\) và \(r\). Vì sao vẫn chưa thể nghiệm thu để sang Chương 08?&lt;/li&gt;
  &lt;li&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt; làm mũi xe quay sang trái thay vì quay phải. Nêu một cách sửa đúng và cho biết thao tác thứ hai nào sẽ vô tình hủy bỏ cách sửa đó.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(R \approx 1{,}30/0{,}60 \approx 2{,}2~\Omega\) — quá lớn so với mức vài trăm miliôm của pack pin khỏe. Cần dừng nghiệm thu để kiểm tra cell pin, tiếp xúc đế pin, tiết diện dây hoặc polyfuse đang nóng.&lt;/li&gt;
  &lt;li&gt;Tốc độ quan sát \(v_{obs} = 1{,}6/4{,}0 = 0{,}40~\mathrm{m/s}\) cho riêng lượt thử đó. Không được viết “PWM 80 = \(0{,}40~\mathrm{m/s}\)” như hằng số quy đổi cố định vì bước hiệu chuẩn thuộc Chương 08.&lt;/li&gt;
  &lt;li&gt;Tiêu chí dừng theo timeout bị trượt. Đoạn kiểm tra quá hạn \(250~\mathrm{ms}\) ở Bài 04 đang bị thiếu hoặc không được thực thi, khiến thanh ghi PWM giữ nguyên mức duty cuối.&lt;/li&gt;
  &lt;li&gt;Mô hình động học và vòng điều khiển ở Chương 08 bắt buộc cần khổ vết \(b\) và bán kính lăn \(r\) đã đo ở Chương 06; để trống sẽ buộc bộ điều khiển phải dùng số ước đoán.&lt;/li&gt;
  &lt;li&gt;Đảo hai dây động cơ phải trên cầu vít &lt;strong&gt;hoặc&lt;/strong&gt; đảo cực tính điều khiển bánh phải trong firmware (chỉ chọn một). Nếu làm cả hai cùng lúc thì hai lần đảo triệt tiêu nhau và lỗi quay ngược sẽ tái diễn; sau khi sửa cần chạy lại đủ bốn hướng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;Datasheet L298 của ST&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://pyserial.readthedocs.io/en/latest/shortintro.html&quot;&gt;pySerial, phần mở đầu&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Vòng teleop firmware (chưa ROS)</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_04_teleop_firmware/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_04_teleop_firmware</id>
   <content type="html">&lt;p&gt;Bài 03 đã chốt đặc tả khung lệnh. Bài này trình bày chương trình (sketch) cho ESP32 để đọc chuỗi lệnh, kẹp giá trị an toàn, tăng giảm PWM mượt theo bước (slew rate) và tự động đưa về 0 khi máy chủ ngừng gửi. Hệ thống vẫn chưa dùng ROS và hai bánh xe vẫn phải kê trên không.&lt;/p&gt;

&lt;p&gt;Viết phần mềm cho robot khác hoàn toàn với viết một ứng dụng web hay bài tập cấu trúc dữ liệu: trên vi điều khiển, &lt;strong&gt;thời gian thực là luật thép&lt;/strong&gt;. Nếu bạn đặt một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; trong hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt; để chờ chớp đèn LED, robot của bạn sẽ “điếc” hoàn toàn trong nửa giây — không đọc encoder, không nhận lệnh dừng khẩn cấp, và lao thẳng vào chân bàn! Bài học về &lt;strong&gt;vòng lặp teleop firmware&lt;/strong&gt; giới thiệu kiến trúc chuẩn công nghiệp trên vi điều khiển (trước khi bước sang ROS): vòng lặp không chặn (&lt;strong&gt;non-blocking loop&lt;/strong&gt;), máy trạng thái an toàn và cơ chế &lt;strong&gt;chó canh cửa (watchdog timeout)&lt;/strong&gt; tự dừng xe khi mất kết nối.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-lập-trình-thời-gian-thực-không-chặn-trên-vi-điều-khiển&quot;&gt;Kiến thức cần nhớ: Lập trình thời gian thực không chặn trên vi điều khiển&lt;/h2&gt;

&lt;h3 id=&quot;1-vòng-lặp-không-chặn-non-blocking-scheduler-với-millis--micros&quot;&gt;1. Vòng lặp không chặn (Non-Blocking Scheduler với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;micros()&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Thay vì gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt;, firmware chia công việc theo các chu kỳ cố định dựa trên đồng hồ phần cứng: đọc bộ đệm UART liên tục ở mỗi vòng lặp, chạy vòng điều khiển động cơ ở chu kỳ cứng \(50\text{–}100\text{ Hz}\) (10–20 ms), và gửi bản tin telemetry ở \(10\text{–}20\text{ Hz}\) (50–100 ms). Nhờ vậy, không tác vụ nào làm nghẽn phản xạ an toàn của robot.&lt;/p&gt;

&lt;h3 id=&quot;2-cơ-chế-watchdog-timeout-dead-mans-switch&quot;&gt;2. Cơ chế Watchdog Timeout (Dead-Man’s Switch)&lt;/h3&gt;
&lt;p&gt;Trong điều khiển từ xa (teleop), câu hỏi quan trọng nhất không phải là &lt;em&gt;“nhấn W xe có chạy không?”&lt;/em&gt; mà là &lt;strong&gt;&lt;em&gt;“nếu máy tính treo, Wi-Fi rớt hoặc cáp USB tuột giữa lúc xe đang lao đi, chuyện gì xảy ra?”&lt;/em&gt;&lt;/strong&gt;. Watchdog lưu mốc thời gian &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last_cmd_ms&lt;/code&gt; của gói lệnh hợp lệ gần nhất; hễ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - last_cmd_ms &amp;gt; T_wd&lt;/code&gt; (thường 200–500 ms), firmware lập tức chuyển sang trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SAFE_STOP&lt;/code&gt; và đưa PWM cả hai bánh về 0!&lt;/p&gt;

&lt;h3 id=&quot;3-giới-hạn-gia-tốc-slew-rate-limiter-bảo-vệ-hộp-số-và-nguồn-pin&quot;&gt;3. Giới hạn gia tốc (Slew-Rate Limiter) bảo vệ hộp số và nguồn pin&lt;/h3&gt;
&lt;p&gt;Nhảy vọt lệnh từ tiến 100% sang lùi -100% trong 1 mili-giây sẽ tạo dòng xung ngược cực lớn làm reset nguồn và vỡ bánh răng nhựa. Một bộ giới hạn tốc độ thay đổi (&lt;em&gt;slew-rate limiter&lt;/em&gt;) ép vận tốc tăng/giảm tối đa \(\Delta v_{\max} = a_{\max} \cdot \Delta t\) mỗi chu kỳ, giúp xe đề-pa và phanh mượt mà.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-quãng-đường-dừng-khẩn-cấp-khi-mất-kết-nối-theo-ngưỡng-watchdog&quot;&gt;Giải thích: Cầm tay tính quãng đường dừng khẩn cấp khi mất kết nối theo ngưỡng Watchdog&lt;/h3&gt;

&lt;p&gt;Giả sử robot Capstone đang chạy thẳng với vận tốc &lt;strong&gt;\(v = 0{,}80\text{ m/s}\)&lt;/strong&gt;, máy tính gửi lệnh teleop ở tần số \(20\text{ Hz}\) (mỗi \(50\text{ ms}\) một gói), và gia tốc phanh an toàn của xe là &lt;strong&gt;\(a_{\text{phanh}} = 2{,}0\text{ m/s}^2\)&lt;/strong&gt;. Đột ngột đường truyền bị đứt hoàn toàn! Hãy so sánh quãng đường xe lao đi với hai cấu hình thời gian Watchdog:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cấu hình an toàn (\(T_{\text{wd}} = 300\text{ ms} = 0{,}30\text{ s}\), tương đương 6 gói mất liên tiếp):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Quãng đường trôi trong lúc chờ Watchdog kích hoạt:
\(d_{\text{chờ}} = v \cdot T_{\text{wd}} = 0{,}80 \times 0{,}30 = 0{,}24\text{ m}\;(24\text{ cm}).\)&lt;/li&gt;
      &lt;li&gt;Quãng đường giảm tốc về 0 sau khi phanh kích hoạt:
\(d_{\text{phanh}} = \frac{v^2}{2 a_{\text{phanh}}} = \frac{0{,}80^2}{2 \times 2{,}0} = \frac{0{,}64}{4{,}0} = 0{,}16\text{ m}\;(16\text{ cm}).\)&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Tổng quãng đường dừng:&lt;/strong&gt; \(d_{\text{tổng}} = 0{,}24 + 0{,}16 = 0{,}40\text{ m}\;(40\text{ cm})\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cấu hình quá lỏng (\(T_{\text{wd}} = 2{,}0\text{ s}\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;\(d_{\text{chờ}} = 0{,}80 \times 2{,}0 = 1{,}60\text{ m} \implies d_{\text{tổng}} = 1{,}60 + 0{,}16 = 1{,}76\text{ m}!\)
Chỉ một tham số cấu hình sai lệch đã biến cú dừng gọn trong 40 cm thành cú đâm tường ở cách đó gần 2 mét!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Phân tích lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sscanf&lt;/code&gt;, kẹp từng giá trị mục tiêu vào khoảng \(\pm 180\) và bỏ qua hoàn toàn các dòng sai cú pháp.&lt;/li&gt;
  &lt;li&gt;Tự động đặt cả hai mục tiêu về 0 sau \(250~\mathrm{ms}\) không nhận lệnh mới, đồng thời điều chỉnh PWM thực tế tiến dần về mục tiêu tối đa 15 nấc mỗi \(20~\mathrm{ms}\).&lt;/li&gt;
  &lt;li&gt;Điều khiển hai kênh cầu H với chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; ở mức cao, và cập nhật số chân ví dụ khớp với bảng dấu đã lập ở Bài 02.&lt;/li&gt;
  &lt;li&gt;Gửi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V l r\n&lt;/code&gt; từ Python, ngắt tiến trình đột ngột và xác nhận cả hai lốp dừng hẳn trong vòng \(0{,}5~\mathrm{s}\) trong khi vi điều khiển vẫn được cấp nguồn từ mạch buck.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Cây nguồn từ Bài 07-01, bảng dấu từ Bài 07-02 và giao thức từ Bài 07-03 (115200 8N1, văn bản kết thúc bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;, chu kỳ máy chủ \(200~\mathrm{ms}\), quá hạn firmware \(250~\mathrm{ms}\), kẹp \(-180..180\)). Trên TB6612, khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; ở mức thấp thì ngõ ra bị ngắt; khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; cao, việc đặt PWM bằng 0 (khi hai chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt; khác mức) sẽ hãm ngắn mạch, còn đặt cả hai chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt; xuống thấp sẽ ngắt cầu (trở kháng cao). Hai kích thước \(b\) và \(r\) từ Chương 06 được lưu sẵn trong sổ tay nhưng chưa dùng trong sketch này. Quy ước dương vẫn là chiều lốp lăn về phía mũi xe. Thông điệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;) sẽ được tích hợp sau khi hiệu chuẩn ở Chương 08 và mở topic ở Chương 09; hôm nay chưa tạo workspace ROS.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Lỗi nguy hiểm nhất khi điều khiển robot là lệnh bị giữ kẹt: máy chủ ngừng hoạt động nhưng thanh ghi PWM (LEDC) vẫn duy trì mức duty cũ khiến xe tiếp tục chạy bằng nguồn pin. Cơ chế quá hạn (timeout) bắt buộc phải nằm trực tiếp trong firmware. Về sau, luồng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; trên ROS 2 cũng đòi hỏi đúng cơ chế dừng này khi node phát bị tắt; Chương 09 chỉ thay bộ phân tích chuỗi bằng bộ nhận topic chứ không bỏ cơ chế dừng an toàn.&lt;/p&gt;

&lt;p&gt;Trong sơ đồ vòng lặp cảm biến – tính toán – chấp hành dưới đây, khối cảm biến (encoder) và khối tính toán (PID) sẽ học ở chương sau; bài này hoàn thiện khối chấp hành gồm xuất PWM và ép về 0 khi quá hạn.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sense_compute_act.png&quot; alt=&quot;Hành động là PWM tới driver; timeout ép về không đã nằm trong ô ấy&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;kẹp-hạn-và-bước-kéo-đếm-được&quot;&gt;Kẹp, hạn, và bước kéo đếm được&lt;/h2&gt;

&lt;p&gt;Giá trị &lt;strong&gt;target&lt;/strong&gt; là mục tiêu mà dòng lệnh hợp lệ gần nhất yêu cầu (sau khi đã kẹp trong \(\pm 180\)). Giá trị &lt;strong&gt;đang áp&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;applied&lt;/code&gt;) là mức PWM thực tế đang cấp cho mạch cầu, được phép thay đổi từ từ theo sau &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt;. Cứ mỗi \(20~\mathrm{ms}\), giá trị đang áp tiến thêm tối đa 15 nấc về phía &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt;:&lt;/p&gt;

\[\text{số bước} = \left\lceil \frac{|\text{target} - \text{đang áp}|}{15} \right\rceil, \qquad t = \text{số bước} \times 20~\mathrm{ms}.\]

&lt;p&gt;Như vậy, tăng từ 0 lên 60 cần 4 bước (\(80~\mathrm{ms}\)), còn từ 0 lên 180 cần 12 bước (\(240~\mathrm{ms}\)), giúp tránh sốc dòng đột ngột lên cuộn dây động cơ và nguồn pin.&lt;/p&gt;

&lt;p&gt;Nếu khoảng thời gian tính từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; đến mốc nhận dòng hợp lệ cuối cùng vượt quá \(250~\mathrm{ms}\), cả hai &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt; lập tức đặt về 0 và bộ giới hạn tốc độ thay đổi (slew) sẽ hạ dần duty về 0. Ví dụ, sau lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; cuối cùng mà không có lệnh mới, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt; rơi về 0 ở mốc \(250~\mathrm{ms}\) và PWM thực tế về 0 sau thêm \(80~\mathrm{ms}\):&lt;/p&gt;

\[250~\mathrm{ms} + 4 \times 20~\mathrm{ms} = 330~\mathrm{ms}.\]

&lt;p&gt;Thời gian này nằm gọn trong giới hạn dừng \(0{,}5~\mathrm{s}\) của bài thực hành. Ngay cả khi đang chạy ở trần 180, tổng thời gian dừng cũng chỉ là \(250 + 240 = 490~\mathrm{ms}\) — đó là lý do trần kẹp và bước kéo được chọn đồng bộ với nhau. Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; lập tức đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt; về 0 để giảm tốc mềm về dừng, còn các dòng sai cú pháp bị bỏ qua và không làm mới bộ đếm thời gian quá hạn.&lt;/p&gt;

&lt;p&gt;Các chân ví dụ trong mã nguồn (bạn cần chỉnh lại theo bảng dấu Bài 02): &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY = 4&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1 = 16&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2 = 17&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA = 18&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN1 = 19&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN2 = 21&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMB = 22&lt;/code&gt;. Nếu cần đảo chiều một bánh, chỉ đảo cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt; trong mã nguồn hoặc đảo dây trên cầu vít động cơ, không làm cả hai. Lưu ý sự khác biệt phiên bản: Arduino-ESP32 core 3.x dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach(pin, freq, resolution)&lt;/code&gt;, trong khi core 2.x dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcSetup&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttachPin&lt;/code&gt; (cả hai đều có chú thích trong mã). Hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.setTimeout(10)&lt;/code&gt; đảm bảo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readBytesUntil&lt;/code&gt; không làm treo vòng lặp bước kéo tới 1 giây nếu thiếu ký tự xuống dòng.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Edit pins and IN polarity to the lesson 07-02 sign map.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Positive rolls that tire toward the nose.&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_STBY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_AIN1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_AIN2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_PWMA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_BIN1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_BIN2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_PWMB&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_FREQ&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_RES&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_LIM&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SLEW_STEP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SLEW_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;appliedL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;appliedR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastSlew&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;approach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SLEW_STEP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SLEW_STEP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;drive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pwmPin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pwmPin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_STBY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_AIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_AIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_BIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_BIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_AIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_AIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_BIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_BIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Arduino-ESP32 3.x:&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcAttach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWMA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_FREQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_RES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcAttach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWMB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_FREQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_RES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Core 2.x: đừng gọi ledcAttach / ledcWrite(pin, duty).&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ledcSetup(0, PWM_FREQ, PWM_RES); ledcAttachPin(PIN_PWMA, 0);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ledcSetup(1, PWM_FREQ, PWM_RES); ledcAttachPin(PIN_PWMB, 1);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// rồi ledcWrite(0, duty) và ledcWrite(1, duty) — số kênh, không phải số chân.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWMA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWMB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_STBY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastRx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastSlew&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;available&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readBytesUntil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;&apos;\n&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;\0&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;\r&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;\0&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;S&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;\0&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;targetL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;targetR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lastRx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sscanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;V %d %d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;targetL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;constrain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PWM_LIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_LIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;targetR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;constrain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PWM_LIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_LIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lastRx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Any other line is ignored.&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;targetL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;targetR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastSlew&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SLEW_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastSlew&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;appliedL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;approach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;appliedL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;appliedR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;approach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;appliedR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;drive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_AIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_AIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_PWMA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;appliedL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;drive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_BIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_BIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_PWMB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;appliedR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Khi máy tính mở cổng nối tiếp, tín hiệu DTR thường làm ESP32 khởi động lại. Vì hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt; luôn đặt PWM về 0 trước khi kéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; lên cao, lần khởi động lại đó luôn an toàn. Tuy nhiên, không được trông chờ vào DTR khi đóng cổng; việc dừng robot khi máy chủ ngừng gửi phải do cơ chế timeout thực hiện trong lúc ESP32 vẫn đang được nuôi bởi mạch buck. Rút cáp USB chỉ kiểm chứng đúng cơ chế timeout nếu ESP32 đang ăn nguồn buck — nếu chỉ nuôi qua USB, rút dây sẽ làm mất nguồn MCU và các chân tín hiệu bị thả nổi.&lt;/p&gt;

&lt;p&gt;Trên máy tính, dùng đúng tên cổng từ Chương 02 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyACM0&lt;/code&gt;). Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleep(0.2)&lt;/code&gt; tạo nhịp gửi \(200~\mathrm{ms}\) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;close()&lt;/code&gt; sẽ ngắt chuỗi lệnh:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/dev/ttyUSB0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;V 60 60&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ở lượt chạy thứ hai, đổi lệnh thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60\n&lt;/code&gt; trong 1 giây: bánh trái tiến, bánh phải lùi tương ứng với quay phải theo bảng dấu Bài 02. Nếu chiều quay thực tế không khớp, hãy sửa cặp chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt; của kênh đó trong mã nguồn, không đảo thêm dây trên cầu vít.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Kê cả hai lốp rời khỏi mặt bàn, đặt xe xa mép bàn và giữ một tay trong tầm công tắc pin. Lệnh thử đầu tiên là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt;, không dùng 255; bài này chưa đặt robot xuống sàn. Ngắt công tắc ngay nếu có mùi khét hoặc tản nhiệt quá nóng. Không để đầu dây pin lithium trần trên bàn trong khi thử nghiệm.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Việc&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot đã lắp, bánh trên không&lt;/td&gt;
      &lt;td&gt;Phần bị điều khiển&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 ăn buck 5,0 V, USB để nạp&lt;/td&gt;
      &lt;td&gt;Logic còn sống khi giết script&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bảng dấu bài 07-02&lt;/td&gt;
      &lt;td&gt;Cực tính chân&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Python 3 và pyserial&lt;/td&gt;
      &lt;td&gt;Máy chủ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ hoặc phép millis&lt;/td&gt;
      &lt;td&gt;Mức dừng 0,5 s&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Cập nhật số chân và cực tính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt; trong sketch khớp với bảng dấu Bài 02 rồi nạp vào ESP32. Xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; cao và PWM khởi đầu bằng 0.&lt;/li&gt;
  &lt;li&gt;Đảm bảo bánh treo trên không, chạy script Python gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; trong khoảng 1 giây và quan sát cả hai mảnh băng dính đều quay về phía mũi xe.&lt;/li&gt;
  &lt;li&gt;Ngắt tiến trình Python (hoặc để script đóng cổng). Xác nhận cả hai bánh dừng hẳn trong vòng \(0{,}5~\mathrm{s}\) trong khi công tắc nguồn pin vẫn đang bật.&lt;/li&gt;
  &lt;li&gt;Chạy lại với lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt; trong 1 giây, xác nhận chiều quay phải (trái tiến, phải lùi) khớp bảng dấu rồi ngắt tiến trình để kiểm tra dừng.&lt;/li&gt;
  &lt;li&gt;Gửi thử &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, sau đó gửi riêng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60&lt;/code&gt;. Xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; dừng xe ngay, còn dòng thiếu tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60&lt;/code&gt; không làm giật hay quay riêng một bánh.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Sổ tay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; ghi rõ sơ đồ chân thực tế đã dùng, thời gian dừng đo được (khoảng \(330~\mathrm{ms}\) sau lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; cuối cùng) và chiều quay của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt;. Robot vẫn đặt trên hộp kê trong suốt bài. Nếu bánh vẫn tiếp tục quay sau khi tiến trình Python kết thúc, bài thực hành chưa đạt.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Không gì chuyển động&lt;/td&gt;
      &lt;td&gt;STBY còn thấp, sai chân PWM, hoặc VM tắt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Còn chạy sau khi giết Python&lt;/td&gt;
      &lt;td&gt;Thiếu timeout, hoặc MCU không thực sự còn chạy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chỉ lốp trái khớp bảng&lt;/td&gt;
      &lt;td&gt;Cặp IN phải lệch bài 02; sửa firmware hoặc vít, không cả hai&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60&lt;/code&gt; giật một bánh&lt;/td&gt;
      &lt;td&gt;Không bắt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sscanf&lt;/code&gt; trả về 2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lỗi biên dịch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Core 2.x; dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcSetup&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttachPin&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Không cần mua thêm linh kiện nếu đã hoàn thành đầy đủ Bài 01 và 02; thứ hay thiếu nhất ở bước này chỉ là cáp USB có đường truyền dữ liệu (data) hoặc thư viện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyserial&lt;/code&gt;. Giá tham khảo (có thể thay đổi): ESP32 devkit 70.000–150.000 đồng, TB6612 25.000–70.000 đồng, L298N khoảng 45.000 đồng (nếu dùng L298N, nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENA&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENB&lt;/code&gt; vào chân PWM và vẫn cấp nguồn ESP32 qua mạch buck).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;L298N đã kiểm, khoảng 45.000 đồng: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=ESP32&quot;&gt;Hshop ESP32&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=ESP32%20devkit&quot;&gt;Shopee ESP32&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=cap%20micro%20USB%20data&quot;&gt;Lazada cáp USB&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=ESP32&quot;&gt;ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=ESP32&quot;&gt;Thế Giới IC ESP32&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=sTHckUyxwp8&quot;&gt;Understanding UART&lt;/a&gt; — Rohde &amp;amp; Schwarz, tiếng Anh. Giải thích cách truyền byte từ máy tính xuống vi điều khiển; firmware bài này chỉ chấp nhận khung đúng cú pháp và tự tắt PWM khi mất tín hiệu.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=JPPTRj0KWbg&quot;&gt;TB6612FNG H-Bridge Motor Controller - Better than L298N?&lt;/a&gt; — DroneBot Workshop, tiếng Anh. Minh họa cách PWM điều khiển tốc độ cầu H ở đầu cuối của vòng teleop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Các video ngắn (Facebook Reel) dưới đây của Hoàng Quân (Cơ điện tử ICTU, &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1092867967065031&quot;&gt;Test hệ thống điều khiển Chinbi&lt;/a&gt; — Chú thích: test hệ thống điều khiển Chinbi Robot. Đối chiếu với firmware bài này ở cơ chế nhận lệnh chạy và tự đưa lệnh về 0 khi ngừng điều khiển.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1057849570224248&quot;&gt;Demo robot&lt;/a&gt; — Chú thích: “Demo robot”. Minh họa bước đưa robot ra chạy thử sau khi vòng teleop đã có cơ chế quá hạn an toàn.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;PWM đang áp là 0 và mục tiêu đổi thành 180. Cần bao nhiêu bước kéo và bao nhiêu miligiây để mạch cầu đạt mức 180?&lt;/li&gt;
  &lt;li&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; cuối cùng đến tại \(t = 0\) và sau đó máy chủ ngừng gửi. Tại thời điểm nào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt; về 0, và tại thời điểm nào PWM đang áp về hẳn 0?&lt;/li&gt;
  &lt;li&gt;Khi nhận dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 200 -300&lt;/code&gt;, hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constrain&lt;/code&gt; lưu giá trị mục tiêu nào? Sau đó nếu nhận thêm dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60&lt;/code&gt;, hai giá trị mục tiêu bị ảnh hưởng ra sao?&lt;/li&gt;
  &lt;li&gt;Giả sử bạn xóa phần kiểm tra timeout nhưng vẫn giữ bộ bước kéo. Nếu tiến trình Python bị tắt đột ngột mà cổng serial không reset chip, bánh xe sẽ làm gì và vì sao Bài 05 sẽ đánh trượt lỗi này?&lt;/li&gt;
  &lt;li&gt;Theo bảng dấu Bài 02, bánh phải tiến khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN1&lt;/code&gt; thấp và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN2&lt;/code&gt; cao. Bạn cần sửa phần nào trong firmware, và vì sao phải giữ nguyên dây trên cầu vít động cơ?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Cần \(180/15 = 12\) bước, tương ứng \(12 \times 20 = 240~\mathrm{ms}\).&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target&lt;/code&gt; về 0 tại \(t = 250~\mathrm{ms}\); PWM đang áp cần thêm 4 bước (\(80~\mathrm{ms}\)) nên về hẳn 0 vào khoảng \(330~\mathrm{ms}\).&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constrain&lt;/code&gt; kẹp thành \(180\) và \(-180\). Dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60&lt;/code&gt; sai cú pháp nên bị bỏ qua hoàn toàn, hai mục tiêu giữ nguyên cho tới khi hết hạn timeout.&lt;/li&gt;
  &lt;li&gt;Mức duty cuối cùng vẫn được giữ nguyên trong thanh ghi PWM nên bánh xe tiếp tục quay vô thời hạn. Bài 05 sẽ đánh trượt tiêu chí dừng an toàn khi ngắt máy gửi.&lt;/li&gt;
  &lt;li&gt;Đảo mức ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN1&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN2&lt;/code&gt; trong hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;drive()&lt;/code&gt; (hoặc truyền lệnh đảo dấu cho kênh phải) và giữ nguyên hai dây trên cầu vít động cơ; nếu đảo cả dây lẫn code thì hai lần đảo sẽ triệt tiêu nhau.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/communication/serial/&quot;&gt;Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://pyserial.readthedocs.io/en/latest/shortintro.html&quot;&gt;pySerial, phần mở đầu&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Teleop serial: thiết kế giao thức</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_03_teleop_protocol/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_03_teleop_protocol</id>
   <content type="html">&lt;p&gt;Bài 02 đã chốt ý nghĩa chiều quay của giá trị dương trên từng bánh xe. Bài này thiết kế định dạng bản tin (giao thức) để gửi hai con số đó qua cổng nối tiếp; phần lập trình vòng lặp firmware sẽ thực hiện ở Bài 04.&lt;/p&gt;

&lt;p&gt;Ở những bài tập Arduino đầu tiên, ai cũng từng dùng kiểu điều khiển đơn giản: gõ chữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;w&apos;&lt;/code&gt; trên bàn phím để xe chạy thẳng, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;s&apos;&lt;/code&gt; để lùi, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;x&apos;&lt;/code&gt; để dừng. Cách làm đó chạy vui trên bàn học nhưng sụp đổ ngay trên robot thực thụ: không gửi được vận tốc liên tục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(v, ω)&lt;/code&gt;, không phát hiện được byte bị nhiễu giữa đường, và nếu cáp USB bị nhiễu mất một ký tự thì robot chạy theo một con số rác! Thiết kế một &lt;strong&gt;giao thức teleop serial&lt;/strong&gt; chuẩn chỉnh — có khung gói rõ ràng (framing), phân tách trường và mã kiểm tra lỗi (checksum) — là bước chuyển mình từ “đồ chơi” sang “hệ thống điều khiển tin cậy”.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-đóng-khung-gói-tin-framing-checksum-và-băng-thông-uart&quot;&gt;Kiến thức cần nhớ: Đóng khung gói tin (Framing), Checksum và Băng thông UART&lt;/h2&gt;

&lt;h3 id=&quot;1-framing--làm-sao-bên-nhận-biết-đâu-là-đầu-và-cuối-một-gói-lệnh&quot;&gt;1. Framing — làm sao bên nhận biết đâu là đầu và cuối một gói lệnh?&lt;/h3&gt;
&lt;p&gt;Đường truyền UART là một dòng byte liên tục không có ranh giới tự nhiên. Giao thức tốt luôn quy định &lt;strong&gt;ký tự bắt đầu (SOF)&lt;/strong&gt; và &lt;strong&gt;ký tự kết thúc (EOF)&lt;/strong&gt; rõ ràng — ví dụ kiểu bản tin NMEA của GPS: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$V,0.50,-0.25*43\n&lt;/code&gt; (bắt đầu bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt;, thân lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0.50,-0.25&lt;/code&gt;, dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;, mã kiểm tra hex &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;43&lt;/code&gt;, kết thúc bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;). Nhờ framing, dù bên nhận bật lên giữa chừng của một gói cũ, nó vẫn bỏ qua phần rác và đồng bộ lại ngay ở dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt; tiếp theo!&lt;/p&gt;

&lt;h3 id=&quot;2-kiểm-tra-toàn-vẹn-bằng-checksum-xor--crc&quot;&gt;2. Kiểm tra toàn vẹn bằng Checksum XOR / CRC&lt;/h3&gt;
&lt;p&gt;Nhiễu điện từ từ động cơ DC rất dễ lật 1 bit trên đường dây UART (ví dụ biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.10&lt;/code&gt; thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9.10&lt;/code&gt;!). Gắn thêm &lt;strong&gt;checksum XOR&lt;/strong&gt; (hoặc CRC-8/CRC-16) cuối mỗi gói tin giúp firmware loại bỏ ngay mọi gói hỏng thay vì thực thi lệnh sai gây tai nạn.&lt;/p&gt;

&lt;h3 id=&quot;3-tính-toán-băng-thông-uart-8n1&quot;&gt;3. Tính toán băng thông UART 8N1&lt;/h3&gt;
&lt;p&gt;Chuẩn UART &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200 8N1&lt;/code&gt; truyền 8 bit dữ liệu kèm 1 bit start và 1 bit stop — tức tốn đúng &lt;strong&gt;10 bit cho mỗi byte&lt;/strong&gt;. Vì vậy, tốc độ tối đa là \(115\,200 / 10 = 11\,520\text{ byte/giây}\). Giữ lưu lượng thực tế dưới 50% trần này giúp bộ đệm UART không bao giờ bị tràn!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-băng-thông-gói-lệnh-50-hz-và-tự-tay-tính-checksum-xor&quot;&gt;Giải thích: Cầm tay tính băng thông gói lệnh 50 Hz và tự tay tính Checksum XOR&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Kiểm tra độ thoáng băng thông UART &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200 8N1&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Dung lượng đường truyền: \(115\,200 / 10 = 11\,520\text{ byte/giây}\).&lt;/li&gt;
  &lt;li&gt;Mỗi gói lệnh teleop &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$V,0.50,-0.25*43\n&lt;/code&gt; dài &lt;strong&gt;17 byte&lt;/strong&gt;, gửi ở tần số mượt &lt;strong&gt;50 Hz&lt;/strong&gt; (mỗi 20 ms):
\(\text{Lưu lượng lệnh} = 17 \times 50 = 850\text{ byte/giây} \implies \text{Tỷ lệ chiếm dụng} = \frac{850}{11\,520} \approx 7{,}4\%!\)
Còn dư hơn 92% băng thông để robot gửi ngược telemetry (encoder, điện áp pin, IMU) về máy tính!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Tự tay tính mã kiểm tra XOR (Checksum) trên 3 byte dữ liệu mẫu:&lt;/strong&gt;
Giả sử phần thân gói tin gồm 3 byte thập lục phân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x56&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0A&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x1F&lt;/code&gt;. Ta thực hiện phép toán &lt;strong&gt;XOR từng bit&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt;):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x56&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0101 0110&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0A&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0000 1010&lt;/code&gt;
\(\to \text{0x56} \oplus \text{0x0A} = \texttt{0101 1100} = \text{0x5C}.\)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x1F&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0001 1111&lt;/code&gt;
\(\to \text{0x5C} \oplus \text{0x1F} = \texttt{0100 0011} = \text{0x43}.\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Bên gửi đính kèm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;43&lt;/code&gt; vào cuối gói. Nếu nhiễu động cơ làm lật chỉ 1 bit bất kỳ trong thân gói, phép XOR ở bên nhận sẽ ra khác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x43&lt;/code&gt; và firmware lập tức vứt bỏ gói lỗi đó!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đặc tả giao thức văn bản UTF-8 kết thúc bằng ký tự xuống dòng trên chuẩn 115200 8N1: lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V &amp;lt;trái&amp;gt; &amp;lt;phải&amp;gt;&lt;/code&gt; gồm hai số nguyên và lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; dùng để dừng khẩn cấp.&lt;/li&gt;
  &lt;li&gt;Nêu rõ khoảng giới hạn (kẹp giá trị) mà firmware sẽ áp dụng là \(-180\) đến \(+180\), và giải thích vì sao trong giai đoạn học tập không dùng toàn thang \(0\)–\(255\).&lt;/li&gt;
  &lt;li&gt;Quy định máy chủ phải gửi lặp lại lệnh tối thiểu mỗi \(200~\mathrm{ms}\) và ngưỡng quá hạn (timeout) của firmware là \(250~\mathrm{ms}\), đảm bảo khi mất tín hiệu thì động cơ tự động về PWM 0 thay vì giữ nguyên tốc độ cũ.&lt;/li&gt;
  &lt;li&gt;Loại bỏ hoàn toàn các dòng sai cú pháp, đồng thời xác định đúng chuyển động tương ứng với năm khung lệnh mẫu theo quy ước dấu của Bài 02.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bài 07-01 đã hoàn chỉnh nguồn và mass chung; Bài 07-02 đã chốt bảng dấu (lệnh dương đẩy cả hai lốp tiến về phía mũi xe, chỉ đảo chiều ở một nơi duy nhất). Chương 05 giải thích cách các giá trị này chuyển thành điện áp cầu H, còn Chương 06 cung cấp hai phương trình hình học:&lt;/p&gt;

\[v = \frac{v_r + v_l}{2}, \qquad \omega = \frac{v_r - v_l}{b},\]

&lt;p&gt;với khổ vết \(b\) đã đo (ví dụ \(b = 0{,}15~\mathrm{m}\), \(r = 31{,}2~\mathrm{mm}\)). Bạn chỉ cần biết mở Serial Monitor; phần viết bộ phân tích (parser) nằm ở Bài 04 và bài này chưa dùng ROS.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Teleop qua cổng nối tiếp là kênh giao tiếp khép kín đầu tiên với hai động cơ. Giao thức dạng văn bản giúp bạn dễ dàng đọc, quan sát và tự gõ thử trên Serial Monitor; định dạng nhị phân chỉ nên cân nhắc sau này khi hệ thống đã ổn định. Về bản chất, cơ chế này tương đồng với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; trong ROS: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; là vận tốc tiến, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; là tốc độ quay yaw và driver tách thành lệnh cho hai bánh theo hai phương trình trên. Tuy nhiên, trong chương này hai số nguyên vẫn chỉ là nấc PWM chứ chưa phải m/s — bước hiệu chuẩn sang m/s sẽ thực hiện ở Chương 08. Gán vội “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; tương đương \(0{,}2~\mathrm{m/s}\)” khi chưa đo là hiệu chuẩn giả.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/teleop_states.png&quot; alt=&quot;Dòng `V` hợp lệ thì chạy; quá 250 ms hoặc dòng `S` thì dừng&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy đọc sơ đồ trạng thái trên như một bản hợp đồng vận hành: ở trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;, động cơ tắt và chờ lệnh. Khi nhận dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; hợp lệ, hệ thống chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUN&lt;/code&gt; và đặt lại bộ đếm thời gian quá hạn. Nếu quá \(250~\mathrm{ms}\) không có lệnh mới hoặc nhận được lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, hệ thống lập tức chuyển về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STOP&lt;/code&gt; và đưa PWM về 0. Một dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; hợp lệ mới sẽ đưa xe trở lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUN&lt;/code&gt;. Khi đường truyền im lặng, mạch tuyệt đối không được duy trì mức duty cũ.&lt;/p&gt;

&lt;h2 id=&quot;byte-khoảng-kẹp-và-hạn&quot;&gt;Byte, khoảng kẹp, và hạn&lt;/h2&gt;

&lt;p&gt;Cấu hình cổng nối tiếp là 115200 baud, 8 bit dữ liệu, không bit chẵn lẻ, 1 bit dừng (8N1). Mỗi khung lệnh là một dòng văn bản UTF-8 kết thúc bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;. Ký tự &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\r&lt;/code&gt; (nếu có trước &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;) chỉ là nhiễu định dạng mà bộ phân tích cần bỏ qua, không xem như lệnh thứ hai.&lt;/p&gt;

&lt;p&gt;Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V &amp;lt;trái&amp;gt; &amp;lt;phải&amp;gt;&lt;/code&gt; chứa hai số nguyên. Firmware ở Bài 04 sẽ kẹp từng giá trị vào khoảng \(-180..180\). Ngưỡng trần này được đặt có chủ đích: mở hết thang 255 trên pack 2S đầy làm xe vọt quá nhanh khi mới thử nghiệm (những vòng chạy đầu ở Bài 02 chỉ dùng mức 60–80). Mức 180 tương ứng hệ số duty:&lt;/p&gt;

\[D = \frac{180}{255} \approx 0{,}706\]

&lt;p&gt;đủ mạnh để thử trên sàn nhưng vẫn kiểm soát được an toàn. Lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; lập tức đặt cả hai mục tiêu về 0 để dừng khẩn cấp; lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 0 0&lt;/code&gt; cũng dừng xe nhưng mang ý nghĩa lệnh điều khiển bình thường.&lt;/p&gt;

&lt;p&gt;Trong lúc muốn xe tiếp tục chạy, máy chủ phải gửi lại lệnh ít nhất mỗi \(200~\mathrm{ms}\). Nếu quá \(250~\mathrm{ms}\) mà firmware không nhận được lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; hợp lệ, hệ thống coi kết nối máy chủ đã mất và tự ép mục tiêu về 0. Khoảng dự phòng \(50~\mathrm{ms}\) đủ dung sai cho độ trễ nhất thời của gói USB, nhưng không để xe chạy tự do khi laptop bị treo hoặc rơi vào chế độ ngủ. Nếu rút cáp hoặc ngừng gửi mà PWM vẫn giữ nguyên ở mức 80 thì cơ chế quá hạn đang bị lỗi.&lt;/p&gt;

&lt;p&gt;Mọi dòng không đúng cú pháp đều bị loại bỏ hoàn toàn: mục tiêu hợp lệ trước đó giữ nguyên cho đến khi hết hạn, và dữ liệu rác không bao giờ được gây ra chuyển động ngoài ý muốn. Cụ thể, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80&lt;/code&gt; thiếu tham số bánh phải, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 foo&lt;/code&gt; không được tự suy diễn thành 0 cho bánh phải, và ký tự lệnh phải là chữ hoa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; (không chấp nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;).&lt;/p&gt;

&lt;h2 id=&quot;năm-khung-một-quy-ước-dấu&quot;&gt;Năm khung, một quy ước dấu&lt;/h2&gt;

&lt;p&gt;Giá trị dương trên mỗi bánh tương ứng chiều tiến về phía mũi xe (Bài 02). Khi bánh phải nhanh hơn bánh trái, robot quay sang trái (yaw dương, khớp với Chương 06) vì:&lt;/p&gt;

\[\omega = \frac{v_r - v_l}{b}\]

&lt;p&gt;dương khi \(v_r &amp;gt; v_l\). Lưu ý rằng dấu chiều quay hoàn toàn khớp, nhưng các con số trong bảng dưới đây là nấc PWM chứ chưa phải vận tốc m/s:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Dòng&lt;/th&gt;
      &lt;th&gt;PWM trái&lt;/th&gt;
      &lt;th&gt;PWM phải&lt;/th&gt;
      &lt;th&gt;Bạn đang xin&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(+80\)&lt;/td&gt;
      &lt;td&gt;\(+80\)&lt;/td&gt;
      &lt;td&gt;Cả hai tiến, thẳng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -80 -80&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(-80\)&lt;/td&gt;
      &lt;td&gt;\(-80\)&lt;/td&gt;
      &lt;td&gt;Cả hai lùi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(-60\)&lt;/td&gt;
      &lt;td&gt;\(+60\)&lt;/td&gt;
      &lt;td&gt;Quay trái (phải tiến, trái lùi)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 0 0&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(0\)&lt;/td&gt;
      &lt;td&gt;\(0\)&lt;/td&gt;
      &lt;td&gt;Dừng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;target 0&lt;/td&gt;
      &lt;td&gt;target 0&lt;/td&gt;
      &lt;td&gt;Dừng khẩn&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Tương tự, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt; cho chuyển động quay phải khi bảng dấu đã chính xác. Nếu bỏ sót bước chỉnh dấu ở Bài 02, các khung lệnh trên sẽ quay sai hướng — hãy sửa đúng một lần ở Bài 02.&lt;/p&gt;

&lt;p&gt;Tính thử thời gian truyền cho khung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60\n&lt;/code&gt;: chuỗi gồm 9 byte (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;, khoảng trắng, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, khoảng trắng, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;). Ở tốc độ 115200 8N1 (10 bit cho mỗi byte tính cả bit start và stop), thời gian truyền trên đường dây là:&lt;/p&gt;

\[t \approx \frac{9 \times 10}{115200} \approx 0{,}78~\mathrm{ms}.\]

&lt;p&gt;Thời gian truyền chỉ chưa tới 1 miligiây, rất nhỏ so với nhịp gửi lại \(200~\mathrm{ms}\). Do đó, giao thức văn bản hoàn toàn không gây nghẽn đường truyền UART, trong khi đổi sang nhị phân sẽ làm mất đi sự tiện lợi khi quan sát và gõ trực tiếp lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Không nên tự gán giá trị tốc độ vật lý khi chưa đo đạc thực tế. Ví dụ, nếu tự giả định PWM 80 bằng \(0{,}20~\mathrm{m/s}\) trên cả hai bánh để tính:&lt;/p&gt;

\[v = \frac{0{,}20+0{,}20}{2} = 0{,}20~\mathrm{m/s}, \qquad \omega = 0,\]

&lt;p&gt;phép tính tuy gọn nhưng số liệu vật lý là tự suy diễn. Ở Chương 08, bạn sẽ đo thời gian chạy trên quãng đường 1 mét để hiệu chuẩn tốc độ thực; còn hiện tại, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; chỉ mang nghĩa chạy tiến ở mức 80 nấc PWM theo bảng dấu.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Bài này chỉ thiết kế và gõ thử văn bản, chưa chạy động cơ. Nếu trên ESP32 vẫn còn nạp chương trình cũ và dây động cơ đã gắn sẵn, hãy ngắt công tắc nguồn pin trước khi mở Serial Monitor. Sản phẩm của bài này là đặc tả năm khung lệnh; sang Bài 04 mới nạp firmware điều khiển và khi đó bánh xe vẫn phải kê trên không.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Việc&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; hoặc serial monitor&lt;/td&gt;
      &lt;td&gt;Nơi năm khung nằm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bảng dấu bài 07-02&lt;/td&gt;
      &lt;td&gt;Nghĩa của dương&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 tùy chọn, USB, không VM&lt;/td&gt;
      &lt;td&gt;Để thấy ký tự vọng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Máy tính&lt;/td&gt;
      &lt;td&gt;Kiểm tra thời gian 9 byte&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Ghi đầy đủ thông số giao thức vào sổ tay: 115200 8N1, UTF-8, kết thúc bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;, khoảng kẹp \(-180..180\), chu kỳ gửi lại của máy chủ \(200~\mathrm{ms}\), thời gian quá hạn của firmware \(250~\mathrm{ms}\).&lt;/li&gt;
  &lt;li&gt;Ngay dưới bảng dấu, viết năm khung lệnh chuẩn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -80 -80&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 0 0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;) kèm ý nghĩa chuyển động tương ứng: đi thẳng, lùi, quay trái, dừng, dừng khẩn.&lt;/li&gt;
  &lt;li&gt;Tạo hai dòng lệnh sai cú pháp (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 foo&lt;/code&gt;) và ghi chú bên cạnh: “bỏ qua hoàn toàn, không làm quay một bánh”.&lt;/li&gt;
  &lt;li&gt;Nếu có kết nối Serial Monitor (đã ngắt nguồn driver), gõ thử năm dòng hợp lệ để kiểm tra ký tự hiển thị.&lt;/li&gt;
  &lt;li&gt;Tính lại thời gian truyền khung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60\n&lt;/code&gt; như ví dụ và đặt cạnh chu kỳ \(200~\mathrm{ms}\) để phân biệt rõ hai thang thời gian.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Sổ tay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; ghi đầy đủ năm khung lệnh mẫu, câu khẳng định “dương là tiến trên cả hai bánh”, khoảng kẹp giá trị, hai mốc thời gian (\(200~\mathrm{ms}\) máy chủ và \(250~\mathrm{ms}\) firmware) và lưu ý rõ nấc PWM chưa phải đơn vị m/s. Ảnh chụp Serial Monitor là tùy chọn; bài này chưa yêu cầu robot chạy.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; bị mô tả là quay phải&lt;/td&gt;
      &lt;td&gt;Quy ước dấu bị lật; phải nhanh hơn là yaw trái&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Một số được nhận thành chỉ bánh trái&lt;/td&gt;
      &lt;td&gt;Ngữ pháp bị áp một nửa; bỏ cả dòng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hạn ghi 2 s “cho an toàn”&lt;/td&gt;
      &lt;td&gt;Máy ngủ sẽ còn kéo xe vài giây; dùng \(250~\mathrm{ms}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; dán nhãn \(0{,}2~\mathrm{m/s}\)&lt;/td&gt;
      &lt;td&gt;Hiệu chuẩn giả; chương 08 chưa tới&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chép struct nhị phân từ blog&lt;/td&gt;
      &lt;td&gt;Không gõ được, và không thấy nó hỏng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Bài này không phát sinh linh kiện bắt buộc mới. Nếu bạn chưa có ESP32 để thực hành, chỉ cần mua thêm một bo ESP32 devkit (giá tham khảo khoảng năm 2026: 70.000–150.000 đồng, bản dùng chip nạp CP2102 hoặc CH340 đều phù hợp). Chỉ nên đổi sang Raspberry Pi Pico nếu bạn tự viết lại phần cấu hình PWM LEDC ở Bài 04 vì mã nguồn mẫu viết cho Arduino-ESP32.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;L298N đã kiểm, khoảng 45.000 đồng, chỉ khi vẫn thiếu cầu: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=ESP32&quot;&gt;Hshop ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=ESP32%20devkit&quot;&gt;Shopee ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=ESP32&quot;&gt;Lazada ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=ESP32&quot;&gt;Thế Giới IC ESP32&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=sTHckUyxwp8&quot;&gt;Understanding UART&lt;/a&gt; — Rohde &amp;amp; Schwarz, tiếng Anh. Giải thích cấu trúc khung truyền UART gồm bit start, dữ liệu và bit stop — lớp truyền dẫn cho giao thức teleop của bài này.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60\n&lt;/code&gt; gồm bao nhiêu byte và mất bao lâu để truyền xong ở tốc độ 115200 8N1?&lt;/li&gt;
  &lt;li&gt;Dòng lệnh hợp lệ cuối cùng đến tại thời điểm \(t = 0\), sau đó máy tính chuyển sang chế độ ngủ. Khi nào firmware phải ép mục tiêu về 0, và vì sao máy chủ cần gửi lặp lại mỗi \(200~\mathrm{ms}\)?&lt;/li&gt;
  &lt;li&gt;Serial Monitor gửi lần lượt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80&lt;/code&gt; rồi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 200 -90&lt;/code&gt;. Dòng nào sai cú pháp, và firmware Bài 04 sẽ kẹp dòng hợp lệ thành cặp giá trị nào?&lt;/li&gt;
  &lt;li&gt;Vì sao &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; tương ứng với chuyển động quay trái? Với \(b = 0{,}15~\mathrm{m}\) (chỉ dùng để kiểm tra dấu), hiệu \((\mathrm{phải} - \mathrm{trái})\) dương hay âm, và tại sao phép chia cho \(b\) vẫn chưa cho ra đơn vị rad/s?&lt;/li&gt;
  &lt;li&gt;Một người đề xuất đóng gói hai giá trị thành hai biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int16&lt;/code&gt; nhị phân để “truyền nhanh hơn”. Cách làm đó làm mất lợi ích gì trên Serial Monitor, và thời gian truyền \(0{,}78~\mathrm{ms}\) có đủ lý do để chuyển sang nhị phân không?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Gồm 9 byte (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;, khoảng trắng, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, khoảng trắng, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;). Thời gian truyền xấp xỉ \(9 \times 10 / 115200 \approx 0{,}78~\mathrm{ms}\).&lt;/li&gt;
  &lt;li&gt;Firmware phải ép mục tiêu về 0 tại \(t = 250~\mathrm{ms}\). Chu kỳ gửi lại \(200~\mathrm{ms}\) chừa ra \(50~\mathrm{ms}\) dự phòng để độ trễ nhỏ của đường truyền không làm ngắt lệnh ngoài ý muốn.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80&lt;/code&gt; không hợp lệ và bị loại bỏ hoàn toàn. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 200 -90&lt;/code&gt; hợp lệ và sẽ được kẹp thành \(180\) và \(-90\).&lt;/li&gt;
  &lt;li&gt;Bánh trái \(-60\) (lùi), bánh phải \(+60\) (tiến) nên mũi xe quay sang trái. Hiệu \((\mathrm{phải} - \mathrm{trái}) = 60 - (-60) = +120 &amp;gt; 0\), khớp dấu dương của \(\omega\) ở Chương 06, nhưng đây là nấc PWM nên chia cho \(b\) không ra đơn vị rad/s.&lt;/li&gt;
  &lt;li&gt;Mất khả năng quan sát trực tiếp và tự gõ lệnh kiểm thử trên Serial Monitor. Thời gian truyền dạng chữ chưa tới \(1~\mathrm{ms}\), rất nhỏ so với chu kỳ \(200~\mathrm{ms}\), nên chuyển sang nhị phân không giải quyết được vấn đề thời gian thực hay kiểm tra kết nối máy chủ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/communication/serial/&quot;&gt;Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://pyserial.readthedocs.io/en/latest/shortintro.html&quot;&gt;pySerial, phần mở đầu&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/&quot;&gt;Mục lục gói geometry_msgs (Jazzy)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab: bring-up driver và test quay</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_02_driver_bringup/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_02_driver_bringup</id>
   <content type="html">&lt;p&gt;Mạch buck đã được chỉnh ổn định ở 5,0 V. Đây là lần đầu tiên bánh xe quay bằng nguồn điện: đặt xe lên giá kê sao cho bánh rời khỏi mặt bàn, chạy thử từng động cơ một cho tới khi lệnh PWM dương làm cả hai lốp quay tiến về phía mũi xe.&lt;/p&gt;

&lt;p&gt;Trong kỹ thuật nhúng và robotics, &lt;strong&gt;bring-up&lt;/strong&gt; là nghệ thuật đưa một phần cứng mới lắp vào hoạt động từng bước nhỏ có kiểm soát — không bao giờ nạp ngay cả bộ firmware 1.000 dòng rồi cầu nguyện mọi thứ chạy đúng. Ở bài lab bring-up driver, bạn kiểm tra tách biệt từng kênh động cơ, xác nhận cực tính tiến–lùi, đo đối xứng hai bánh và kiểm chứng dấu xung encoder ngay trên bàn kê bánh để mọi tầng điều khiển phía trên đứng trên nền móng 100% tin cậy.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-quy-trình-bring-up-từng-tầng-và-chẩn-đoán-cực-tính&quot;&gt;Kiến thức cần nhớ: Quy trình Bring-Up từng tầng và Chẩn đoán cực tính&lt;/h2&gt;

&lt;h3 id=&quot;1-tách-lớp-kiểm-thử-điện-áp-tĩnh--lệnh-đơn-kênh--phản-hồi-encoder&quot;&gt;1. Tách lớp kiểm thử: Điện áp tĩnh → Lệnh đơn kênh → Phản hồi encoder&lt;/h3&gt;
&lt;p&gt;Thay vì chạy cả hai bánh cùng lúc, hãy chạy từng bánh ở mức PWM thấp (25% → 50%) trong 1–2 giây. Nếu bánh trái quay tiến khi lệnh dương nhưng bánh phải lại quay lùi (rất thường gặp vì hai động cơ gắn đối xứng gương hai bên khung!), bạn sửa ngay bằng cách đảo cặp dây động cơ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OUT3/OUT4&lt;/code&gt; hoặc đảo hằng số cực tính trong phần mềm — đừng để nhầm lẫn dấu lan lên tầng động học!&lt;/p&gt;

&lt;h3 id=&quot;2-đồng-bộ-dấu-giữa-động-cơ-và-encoder&quot;&gt;2. Đồng bộ dấu giữa Động cơ và Encoder&lt;/h3&gt;
&lt;p&gt;Một lỗi siêu nguy hiểm khi sang điều khiển vòng kín PID: lệnh PWM dương làm bánh quay tiến, nhưng dây kênh A/B của encoder lại cắm ngược khiến bộ đếm chạy &lt;strong&gt;âm&lt;/strong&gt;! Lúc đó phản hồi âm biến thành &lt;strong&gt;phản hồi dương&lt;/strong&gt;, đẩy PWM vọt ngay lên 100% và làm xe phóng mất kiểm soát! Luôn xác nhận: &lt;em&gt;PWM dương \(\implies\) bánh tiến \(\implies\) encoder tăng dương&lt;/em&gt;.&lt;/p&gt;

&lt;h3 id=&quot;3-bất-đối-xứng-hở-vòng-tự-nhiên-giữa-hai-động-cơ&quot;&gt;3. Bất đối xứng hở vòng tự nhiên giữa hai động cơ&lt;/h3&gt;
&lt;p&gt;Hai động cơ cùng mã sản xuất vẫn thường chênh nhau \(2\%\text{–}8\%\) tốc độ ở cùng mức PWM do dung sai chổi than và hộp số. Bring-up ghi nhận mức chênh lệch này để xác nhận động cơ khỏe mạnh (nếu chênh trên 20% thì cần kiểm tra kẹt cơ khí hoặc dây tiếp xúc kém!).&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-phân-tích-log-bài-test-bring-up-2-giây-trên-bàn-kê-bánh&quot;&gt;Giải thích: Cầm tay phân tích log bài test Bring-Up 2 giây trên bàn kê bánh&lt;/h3&gt;

&lt;p&gt;Bạn kê bánh robot lên không, đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 128&lt;/code&gt; (50% của 8-bit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;255&lt;/code&gt;) cho cả hai kênh trong đúng &lt;strong&gt;\(2{,}0\text{ giây}\)&lt;/strong&gt; và đọc số xung encoder tích lũy được:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Bánh trái:&lt;/strong&gt; \(\Delta N_L = +2\,640\) xung (bánh quay thuận chiều tiến).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bánh phải:&lt;/strong&gt; \(\Delta N_R = -2\,580\) xung (bánh đang quay &lt;strong&gt;ngược&lt;/strong&gt; chiều lùi!).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Phân tích ngay 3 kết luận kỹ thuật từ 2 con số này:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Lỗi cực tính bánh phải:&lt;/strong&gt; Vì bánh phải quay ngược chiều lùi, bạn đảo hai dây nguồn vào động cơ phải (hoặc đảo cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RIGHT_MOTOR_DIR = -1&lt;/code&gt; trong code), rồi kiểm tra lại để đảm bảo \(\Delta N_R\) đổi thành \(+2\,580\) xung dương!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Độ bất đối xứng hai bánh ở vòng hở:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Trung bình hai bánh: \(\bar{N} = \frac{2\,640 + 2\,580}{2} = 2\,610\text{ xung}\).&lt;/li&gt;
      &lt;li&gt;Sai lệch tương đối: \(\frac{\vert 2\,640 - 2\,580 \vert}{2\,610} = \frac{60}{2\,610} \approx 2{,}30\%.\)
Mức chênh \(2{,}3\%\) là hoàn toàn bình thường ở vòng hở — đủ tốt để xác nhận cơ khí không kẹt và sẵn sàng bước sang bước thiết kế giao thức teleop!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Kê khung robot lên hộp sao cho cả hai lốp rời hoàn toàn mặt bàn, và chạy thử từng động cơ riêng lẻ với mức PWM giới hạn trong khoảng 60–80 trên thang 255.&lt;/li&gt;
  &lt;li&gt;Kéo chân STBY của TB6612 lên mức cao trước khi tìm lỗi phần mềm, sau đó ghi lại cặp mức logic IN làm bánh trái tiến và cặp làm bánh phải tiến.&lt;/li&gt;
  &lt;li&gt;Nếu một bánh quay ngược chiều, xử lý bằng cách đảo hai dây động cơ đó &lt;strong&gt;hoặc&lt;/strong&gt; ghi chú đảo dấu trong firmware — tuyệt đối không làm cả hai cùng lúc.&lt;/li&gt;
  &lt;li&gt;Đo dòng không tải, chỉ thử giữ kẹt (stall) tối đa 1 giây nếu cần đo, và ngắt nguồn ngay nếu mạch driver có mùi khét hoặc tản nhiệt quá nóng không chạm tay được.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bài 07-01 đã hoàn thiện mass chung, cầu chì trên cực dương và mạch buck 5,0 V đo trước khi gắn ESP32. Chương 05 đã trình bày bảng sự thật của cầu H: một cặp mức logic cho chiều tiến, cặp ngược lại cho chiều lùi, và không để ngắn mạch trên cùng một nhánh. Bạn cần phân biệt được các chân của TB6612 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMB&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN&lt;/code&gt;) hoặc L298N (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENB&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INx&lt;/code&gt;, tản nhiệt, jumper 5 V). Chương 06 cung cấp \(b\), \(r\) và quy ước chiều tiến là hướng mũi xe, quay trái (yaw dương) khi bánh phải nhanh hơn bánh trái. Quy ước dấu xác định hôm nay sẽ được dùng xuyên suốt cho bộ điều khiển PID ở Chương 08 và các topic ROS ở Chương 09. Bài này chưa dùng ROS.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Về sau, thông điệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; gửi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; (vận tốc tiến) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; (vận tốc quay yaw), rồi bộ điều khiển tách thành lệnh cho bánh trái và bánh phải. Nếu chiều “dương” của bánh phải thực tế lại làm xe chạy lùi, mọi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; dương sẽ biến thành cú xoay tại chỗ, và vòng PID ở Chương 08 sẽ không thể sửa nổi lỗi đấu dây này. Việc kiểm tra dấu chiều quay ngay khi bánh được kê trên không đảm bảo an toàn, tránh việc xe lao khỏi bàn. Ở Bài 03, chúng ta mới đặt tên cho hai con số lệnh này (dưới dạng nấc PWM, chưa phải đơn vị m/s).&lt;/p&gt;

&lt;h2 id=&quot;một-kênh-duty-nhỏ-stby-phải-thức&quot;&gt;Một kênh, duty nhỏ, STBY phải thức&lt;/h2&gt;

&lt;p&gt;Đặt khung xe lên một chiếc hộp hoặc chồng sách sao cho cả hai lốp treo lơ lửng, không chạm hộp lẫn mặt bàn. Cấp PWM lần đầu khi bánh đang chạm bàn rất dễ làm robot vọt rơi khỏi bàn.&lt;/p&gt;

&lt;p&gt;Chỉ cấp lệnh cho từng động cơ một trong khi động cơ kia giữ PWM bằng 0. Mức duty ban đầu chỉ nên nằm trong khoảng 60–80 trên thang 255, không mở tối đa 255. Với điện áp pack đang ở \(7{,}6~\mathrm{V}\):&lt;/p&gt;

\[D = \frac{70}{255} \approx 0{,}275, \qquad V_{avg} \approx 0{,}275 \times 7{,}6~\mathrm{V} \approx 2{,}09~\mathrm{V}\]

&lt;p&gt;(chưa tính sụt áp trên mạch cầu). TB6612 dùng cầu MOSFET có sụt áp rất nhỏ nên truyền gần trọn 2 V tới chổi than, giúp lốp quay chậm vừa đủ để quan sát chiều quay. Trong khi đó, L298N dùng cặp transistor Darlington bị sụt khoảng \(1{,}5\)–\(2~\mathrm{V}\) nên ở cùng mức duty, động cơ có thể quay rất chậm hoặc chưa thắng nổi ma sát. Đó là lý do TB6612 được ưu tiên hơn; nếu dùng L298N và phải tăng duty mới thấy bánh quay, hãy tăng từng nấc nhỏ và tuân thủ chặt chẽ quy tắc an toàn dòng kẹt bên dưới.&lt;/p&gt;

&lt;p&gt;Trên TB6612, chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; bắt buộc phải kéo lên mức cao; nếu để thấp hoặc bỏ nổi, cả hai kênh đều bị khóa ở trạng thái ngủ bất kể tín hiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM&lt;/code&gt;. Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; bỏ nổi là nguyên nhân phổ biến nhất của tình trạng “code đúng nhưng bánh đứng im”. Bạn có thể nối trực tiếp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; lên VCC trong bài thực hành này, hoặc điều khiển bằng một chân GPIO ở Bài 04. Trên L298N, lỗi tương tự xảy ra khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENA&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENB&lt;/code&gt; đang ở mức thấp hoặc đã rút jumper 5 V mà chưa cấp nguồn 5 V ngoài cho chân logic.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;Tiến là một đường chéo của chữ H; đường kia là lùi&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Dosmotorsl298n.jpg&quot; alt=&quot;Module L298N: vít motor, tản nhiệt trên cặp Darlington&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Quan sát chiều quay của lốp trái so với hướng mũi xe và ghi lại cặp mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt; giúp bánh quay tiến; sau đó lặp lại riêng cho bánh phải. Ví dụ mẫu (cần thay bằng kết quả thực tế trên xe của bạn): bánh trái tiến khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt; cao, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt; thấp, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA = 70&lt;/code&gt;; bánh phải tiến khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN1&lt;/code&gt; cao, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN2&lt;/code&gt; thấp, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMB = 70&lt;/code&gt;. Nếu bánh phải quay lùi với mẫu này, bạn có thể đảo hai đầu dây motor phải trên cầu đấu vít &lt;strong&gt;hoặc&lt;/strong&gt; giữ nguyên dây và đảo dấu lệnh bên phải trong firmware ở Bài 04 — chỉ chọn &lt;strong&gt;một&lt;/strong&gt; trong hai cách. Thực hiện cả hai sẽ làm hai lần đảo triệt tiêu nhau và khiến lệnh đi thẳng ở Bài 05 lại biến thành xoay tròn.&lt;/p&gt;

&lt;h2 id=&quot;dòng-không-tải-và-stall-không-được-giữ&quot;&gt;Dòng không tải, và stall không được giữ&lt;/h2&gt;

&lt;p&gt;Khi bánh treo trên không ở mức PWM 70, một động cơ TT vỏ vàng thường tiêu thụ dòng không tải khoảng \(0{,}10\)–\(0{,}20~\mathrm{A}\). Hãy mắc ampe kế nối tiếp vào nhánh động cơ đó (hoặc trên dây nguồn pin khi động cơ kia đang tắt) và ghi lại số đo. Khi chạy đồng thời hai động cơ, dòng tiêu thụ sẽ cộng lại.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/stall_current.png&quot; alt=&quot;Dòng không tải là đầu thấp của đường cong; stall là đầu cao&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Nếu muốn ghi nhận một điểm dòng kẹt (stall), chỉ giữ chặt bánh tối đa 1 giây để đọc đồng hồ rồi thả ngay. Thông số dòng kẹt \(1{,}2~\mathrm{A}\) tại \(6~\mathrm{V}\) mà người bán ghi chỉ là bậc độ lớn tham khảo, không phải mức dòng cho phép duy trì liên tục. Ngay cả ở PWM 70, dòng kẹt tuy thấp hơn mức toàn áp nhưng vẫn làm nóng nhanh cuộn dây động cơ và tản nhiệt driver. Nếu ngửi thấy mùi khét hoặc không thể chạm ngón tay vào lá tản nhiệt L298N (hay thân IC TB6612), hãy ngắt công tắc nguồn ngay.&lt;/p&gt;

&lt;p&gt;Kết thúc bước này, bạn chốt bảng dấu cho các bài sau: với cấu hình chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt; đã ghi, PWM dương làm cả hai lốp quay tiến, PWM âm làm lùi và PWM 0 là dừng.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Hai lốp phải rời hoàn toàn mặt bàn. Lần chạy thử đầu tiên chỉ cấp điện cho một động cơ với mức PWM giới hạn trong 60–80 trên 255 cho tới khi xác định rõ chiều quay. Công tắc nguồn pin đóng vai trò nút dừng khẩn cấp và phải luôn nằm trong tầm với. Nếu đo dòng kẹt, chỉ giữ tối đa 1 giây; ngừng thí nghiệm ngay nếu mạch quá nóng hoặc có mùi khét. Dòng động cơ bắt buộc lấy từ pin qua cầu chì (không lấy từ USB), và giữ ngón tay tránh xa bánh răng.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Việc&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot bài 07-01&lt;/td&gt;
      &lt;td&gt;Cây nguồn đã đo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TB6612 hoặc L298N&lt;/td&gt;
      &lt;td&gt;Cầu đang thử&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 hoặc ba sợi dây&lt;/td&gt;
      &lt;td&gt;STBY hoặc ENA cao, cặp IN, PWM&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ampe kế&lt;/td&gt;
      &lt;td&gt;Không tải, và stall một giây nếu đo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Băng dính trên mỗi lốp&lt;/td&gt;
      &lt;td&gt;Hướng nhìn từ mũi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Bảng dấu&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Đặt khung robot lên hộp kê sao cho cả hai bánh treo tự do, không chạm hộp lẫn mặt bàn.&lt;/li&gt;
  &lt;li&gt;Kéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; lên cao (trên TB6612) hoặc đảm bảo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENA&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENB&lt;/code&gt; sẵn sàng (trên L298N), đặt PWM về 0 rồi bật công tắc pin.&lt;/li&gt;
  &lt;li&gt;Chạy thử riêng động cơ trái ở PWM 70, thử lần lượt hai cặp mức logic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN&lt;/code&gt; khác nhau (không để cả hai cùng cao) và ghi lại cặp làm bánh lăn về phía mũi xe.&lt;/li&gt;
  &lt;li&gt;Lặp lại quy trình tương tự cho động cơ phải. Nếu bánh quay ngược, đảo hai dây động cơ đó trên cầu vít hoặc ghi chú “đảo dấu trong firmware” (chỉ chọn một cách).&lt;/li&gt;
  &lt;li&gt;Đo dòng không tải của từng động cơ ở PWM 70 (tùy chọn: giữ kẹt 1 giây để đọc dòng rồi thả ngay). Ghi rõ kết luận “PWM dương → cả hai bánh tiến” kèm tên chân thực tế vào sổ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Một bảng dấu ghi rõ mức logic từng chân, dòng không tải của từng động cơ (ví dụ: trái \(0{,}12~\mathrm{A}\), phải \(0{,}15~\mathrm{A}\) tại PWM 70 — giá trị thực tế có thể khác) và xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; (hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENA&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENB&lt;/code&gt;) đã ở mức cao. Khi nhìn từ đuôi xe và gửi lệnh dương, mảnh băng dính dán trên cả hai lốp đều quay hướng về mũi xe. Sổ tay xác nhận không xảy ra lỗi đảo kép (vừa đảo dây vừa đảo code).&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cả hai motor chết, code trông đúng&lt;/td&gt;
      &lt;td&gt;STBY thấp hoặc nổi; ENA/ENB thấp trên L298N&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chỉ một motor quay&lt;/td&gt;
      &lt;td&gt;Chân PWM, cặp IN, hoặc vít kênh đó hở&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lốp quay rồi driver có mùi&lt;/td&gt;
      &lt;td&gt;Duty quá cao hoặc giữ stall quá lâu; ngắt nguồn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trái tiến, phải lùi, cùng mẫu IN&lt;/td&gt;
      &lt;td&gt;Đảo dây motor phải hoặc dấu firmware, một lần&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dòng gần stall dù lốp “tự do”&lt;/td&gt;
      &lt;td&gt;Lốp cà tấm, hoặc hộp số kẹt&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Thông thường bạn đã có mạch driver từ Bài 07-01; nếu chưa có, hãy ưu tiên mua TB6612. Mạch L298N có thể dùng thay thế tạm thời nhưng tỏa nhiệt nhiều hơn (cần giữ nguyên lá tản nhiệt và vẫn cấp nguồn ESP32 qua mạch buck). Giá tham khảo khoảng năm 2026 (có thể thay đổi): module TB6612 25.000–70.000 đồng, động cơ TT 25.000–45.000 đồng/chiếc, L298N khoảng 45.000 đồng. Lưu ý module DRV8833 chỉ phù hợp với động cơ rất nhỏ, dễ bị quá tải nếu hai động cơ TT gặp tình huống kẹt bánh.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;L298N đã kiểm, khoảng 45.000 đồng: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;Hshop TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;Shopee TB6612&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=TB6612&quot;&gt;Lazada TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=TB6612&quot;&gt;Thế Giới IC TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=JPPTRj0KWbg&quot;&gt;TB6612FNG H-Bridge Motor Controller - Better than L298N?&lt;/a&gt; — DroneBot Workshop, tiếng Anh. Minh họa cách cấp PWM và tín hiệu chiều quay cho TB6612; thực hiện khi bánh kê lên khỏi bàn đúng quy trình bài này.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=fVgnUWIWzZ8&quot;&gt;Driving DC motors, part 3/3:  H-bridges (Kevin Lynch)&lt;/a&gt; — Northwestern Robotics, tiếng Anh. Giải thích nguyên lý đảo chiều trên cầu H để sửa đúng một chỗ khi động cơ quay ngược.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Với nguồn VM là \(8{,}0~\mathrm{V}\) và mức lệnh ban đầu là 80 trên thang 255, tính hệ số duty \(D\) và điện áp trung bình \(V_{avg}\) trước sụt áp cầu. Nếu L298N sụt thêm \(1{,}8~\mathrm{V}\), điện áp còn lại đặt lên chổi than là bao nhiêu?&lt;/li&gt;
  &lt;li&gt;Dòng không tải ở PWM 70 đo được \(0{,}14~\mathrm{A}\), còn khi giữ kẹt 1 giây lên tới \(0{,}95~\mathrm{A}\). Giải thích vì sao hai số đo này phù hợp với đặc tuyến động cơ và tại sao phải thả tay ngay.&lt;/li&gt;
  &lt;li&gt;Trên TB6612, chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; đang bỏ nổi, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt; mức cao, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt; mức thấp, PWM 80 nhưng bánh vẫn đứng yên. Cần bổ sung kết nối nào đầu tiên?&lt;/li&gt;
  &lt;li&gt;Bánh phải quay ngược chiều tiến. Bạn vừa đảo hai dây động cơ đó vừa nhân thêm \(-1\) vào lệnh bánh phải trong firmware. Khi gửi lệnh PWM dương trên sàn, robot sẽ chuyển động thế nào?&lt;/li&gt;
  &lt;li&gt;Bánh trái quay tiến khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1 = 1, AIN2 = 0&lt;/code&gt;, còn bánh phải quay tiến (trước khi chỉnh sửa) khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN1 = 0, BIN2 = 1&lt;/code&gt;. Hãy viết quy ước bảng dấu cho firmware Bài 04 mà không cần đảo thêm dây.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(D = 80/255 \approx 0{,}314\) và \(V_{avg} \approx 0{,}314 \times 8{,}0 \approx 2{,}51~\mathrm{V}\). Sau khi trừ sụt áp \(1{,}8~\mathrm{V}\) trên L298N, điện áp đặt lên chổi than chỉ còn khoảng \(0{,}7~\mathrm{V}\), thường chưa đủ thắng ma sát tĩnh. Đây là lý do nên ưu tiên TB6612 thay vì vội tăng PWM lên 255.&lt;/li&gt;
  &lt;li&gt;Khi chạy không tải, sức phản điện động (back-EMF) gần cân bằng nguồn nên dòng điện nhỏ. Khi kẹp giữ bánh, tốc độ về 0 làm sức phản điện động biến mất và dòng vọt về gần dòng kẹt; chỉ giữ tối đa 1 giây để tránh quá nhiệt cuộn dây và mạch cầu.&lt;/li&gt;
  &lt;li&gt;Kéo chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; lên mức cao (VCC hoặc GPIO mức cao), vì nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; thấp hoặc bỏ nổi thì TB6612 ở trạng thái ngủ.&lt;/li&gt;
  &lt;li&gt;Hai lần đảo chiều triệt tiêu lẫn nhau nên bánh phải vẫn quay lùi khi nhận lệnh dương, khiến robot xoay tròn thay vì chạy thẳng. Chỉ được thực hiện một trong hai cách sửa.&lt;/li&gt;
  &lt;li&gt;Lệnh trái dương: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1 = 1, AIN2 = 0&lt;/code&gt;; lệnh phải dương: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN1 = 0, BIN2 = 1&lt;/code&gt;. Khi lệnh âm, đảo ngược mức logic từng cặp tương ứng và giữ nguyên đấu dây trên cầu vít.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;Datasheet L298 của ST&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/communication/serial/&quot;&gt;Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lắp khung, motor và nguồn</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_01_assemble_power/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_01_assemble_power</id>
   <content type="html">&lt;p&gt;Bài này chuyển tấm khung rời thành một khung robot hoàn chỉnh, nhưng bánh xe vẫn chưa được cấp PWM để quay. Bạn sẽ lắp động cơ, gắn bánh caster, cố định khối pin và đo kiểm toàn bộ cây nguồn trước khi nạp bất kỳ chương trình điều khiển nào.&lt;/p&gt;

&lt;p&gt;Khoảnh khắc bạn nối động cơ, mạch nguồn và vi điều khiển lên cùng một bộ khung chính là lúc robot chuyển từ “tập hợp các linh kiện rời” thành một hệ thống cơ điện tử thống nhất. Nhưng đây cũng là giai đoạn dễ mắc lỗi nối dây nhất: nối nhầm mạch ổn áp tuyến tính cho dòng lớn khiến mạch nóng bỏng tay, đi dây mass (GND) nối tiếp vòng vèo tạo nhiễu đất làm reset ESP32 mỗi lần động cơ đề-pa, hay quên đo thông mạch trước khi cắm pin. Làm đúng &lt;strong&gt;kiến trúc phân phối nguồn&lt;/strong&gt; ngay từ bước lắp ráp sẽ tiết kiệm hàng ngày trời gỡ lỗi chập chờn về sau.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-mạch-hạ-áp-buck-vs-ldo-nối-đất-hình-sao-và-kiểm-tra-trước-khi-cấp-nguồn&quot;&gt;Kiến thức cần nhớ: Mạch hạ áp Buck vs. LDO, Nối đất hình sao và Kiểm tra trước khi cấp nguồn&lt;/h2&gt;

&lt;h3 id=&quot;1-mạch-ổn-áp-tuyến-tính-ldo--7805-vs-mạch-hạ-áp-xung-buck-converter&quot;&gt;1. Mạch ổn áp tuyến tính (LDO / 7805) vs. Mạch hạ áp xung (Buck Converter)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;LDO (như LM7805, AMS1117):&lt;/strong&gt; hoạt động như một điện trở tự điều chỉnh, &lt;strong&gt;đốt toàn bộ phần điện áp chênh lệch thành nhiệt&lt;/strong&gt;: \(P_{\text{nhiệt}} = (V_{\text{in}} - V_{\text{out}}) \cdot I\). Chỉ nên dùng cho dòng rất nhỏ (\(&amp;lt; 200\text{ mA}\)) hoặc chênh áp thấp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Buck Converter (hạ áp xung):&lt;/strong&gt; dùng cuộn cảm và MOSFET đóng ngắt tần số cao đạt hiệu suất &lt;strong&gt;85%–93%&lt;/strong&gt;, mát rượi ngay cả khi hạ từ 12 V xuống 5 V ở dòng 2–3 A cho Raspberry Pi và ESP32.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-nối-đất-hình-sao-star-grounding--cách-ly-nhiễu-động-cơ-khỏi-vi-điều-khiển&quot;&gt;2. Nối đất hình sao (Star Grounding) — cách ly nhiễu động cơ khỏi vi điều khiển&lt;/h3&gt;
&lt;p&gt;Dòng xung lớn của động cơ chạy qua dây mass sẽ tạo sụt áp nhiễu \(\Delta V = I \cdot R_{\text{dây}}\). Nếu vi điều khiển và cảm biến dùng chung đoạn dây mass chạy sau động cơ, điện thế chuẩn 0 V của vi điều khiển sẽ “nhảy múa” theo động cơ. &lt;strong&gt;Nối đất hình sao&lt;/strong&gt; gom dây mass công suất (driver) và dây mass tín hiệu (MCU/cảm biến) hội tụ riêng rẽ về đúng một điểm chung duy nhất tại cực âm nguồn tổng!&lt;/p&gt;

&lt;h3 id=&quot;3-quy-trình-đo-thông-mạch-trước--cấp-nguồn-không-tải-sau&quot;&gt;3. Quy trình “Đo thông mạch trước – Cấp nguồn không tải sau”&lt;/h3&gt;
&lt;p&gt;Trước khi cắm pin LiPo lần đầu, &lt;strong&gt;luôn dùng đồng hồ vạn năng ở thang đo thông mạch (continuity)&lt;/strong&gt; chạm hai cực nguồn tổng: nếu đồng hồ kêu “bíp” ngắn mạch thì dừng ngay lập tức! Sau khi xác nhận không chập, cấp nguồn cho mạch Buck &lt;em&gt;khi chưa cắm vi điều khiển&lt;/em&gt;, chỉnh chiết áp đo đúng \(5{,}00\text{ V}\) rồi mới kết nối tải logic.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-so-sánh-nhiệt-lượng-tỏa-ra-giữa-ldo-7805-và-buck-converter-khi-cấp-5-v--1-a-từ-pin-3s&quot;&gt;Giải thích: Cầm tay so sánh nhiệt lượng tỏa ra giữa LDO 7805 và Buck Converter khi cấp 5 V / 1 A từ pin 3S&lt;/h3&gt;

&lt;p&gt;Giả sử bạn cần hạ áp từ pin LiPo 3S danh định &lt;strong&gt;\(V_{\text{in}} = 11{,}1\text{ V}\)&lt;/strong&gt; xuống &lt;strong&gt;\(V_{\text{out}} = 5{,}0\text{ V}\)&lt;/strong&gt; để cấp dòng &lt;strong&gt;\(I_{\text{out}} = 1{,}0\text{ A}\)&lt;/strong&gt; (công suất hữu ích \(P_{\text{out}} = 5{,}0\text{ W}\)) cho mạch điều khiển và cảm biến:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Tiêu chí so sánh&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Ổn áp tuyến tính &lt;strong&gt;LDO 7805&lt;/strong&gt;&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Mạch hạ áp xung &lt;strong&gt;Buck (\(\eta = 90\%\))&lt;/strong&gt;&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Ý nghĩa thực tế trên robot&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Dòng hút từ pin 11,1 V&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(1{,}00\text{ A}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(\frac{5{,}0}{0{,}90 \times 11{,}1} \approx 0{,}50\text{ A}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Buck hút dòng từ pin chỉ bằng &lt;strong&gt;một nửa&lt;/strong&gt;!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Công suất đầu vào&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(11{,}1 \times 1{,}0 = 11{,}1\text{ W}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(5{,}0 / 0{,}90 = 5{,}56\text{ W}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tiết kiệm \(5{,}54\text{ W}\) liên tục&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Nhiệt tỏa ra trên mạch&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\((11{,}1 - 5{,}0) \times 1{,}0 = 6{,}1\text{ W}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(5{,}56 - 5{,}0 = 0{,}56\text{ W}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Buck mát hơn gần &lt;strong&gt;11 lần&lt;/strong&gt;!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Hiệu suất năng lượng&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(5{,}0 / 11{,}1 \approx 45{,}0\%\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(90{,}0\%\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tăng đáng kể thời lượng pin&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Con số \(6{,}1\text{ W}\) giải thích vì sao con chip ổn áp trên bo Arduino hay L298N nóng bỏng tay và tự ngắt chỉ sau 30 giây nếu bạn bắt nó cấp dòng 1 A từ pin 12 V!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Lắp hai động cơ TT sao cho lốp không cọ tấm mica, gắn bánh caster với vết tiếp xúc nằm sau chốt xoay, và cố định pin để trọng tâm rơi vào khoảng giữa trục bánh chủ động và bánh caster.&lt;/li&gt;
  &lt;li&gt;Đi dây cực dương pin qua công tắc nguồn, cầu chì 2–3 A rồi tách sang nhánh VM của mạch driver và đầu vào mạch hạ áp LM2596; chỉnh đầu ra buck về 5,0 V trước khi gắn ESP32.&lt;/li&gt;
  &lt;li&gt;Nối cực âm pin, GND driver, GND buck và GND vi điều khiển về một điểm mass chung, đồng thời giữ toàn bộ dây động cơ trên cầu đấu vít của driver (không qua breadboard).&lt;/li&gt;
  &lt;li&gt;Ghi điện áp pin không tải và điện áp đầu ra buck vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; kèm ảnh chụp đi dây; không sử dụng cổng USB làm nguồn nuôi động cơ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bài trước (&lt;a href=&quot;/contents/vi/chapter07/07_00_batteries_power_safety/&quot;&gt;pin và cây nguồn&lt;/a&gt;) đã xác định hóa học pin và quy trình chỉnh buck về 5,0 V trước khi gắn ESP32. Chương 04 đã nhấn mạnh cảm biến và vi điều khiển phải chung đất. Chương 05 trình bày mạch cầu H: TB6612 sẽ ở trạng thái ngủ khi chân STBY ở mức thấp, còn IC ổn áp 7805 trên mạch L298N không đủ ổn định để nuôi ESP32. Chương 06 đã cung cấp hai kích thước hình học là khổ vết \(b\) và bán kính lăn \(r\) (mẫu tham khảo trong khóa học có \(b = 0{,}15~\mathrm{m}\) và \(r = 31{,}2~\mathrm{mm}\)). Hãy chép sẵn hai số đo của riêng bạn vào ghi chú hôm nay. Bạn chỉ cần biết đo điện áp một chiều; bài này chưa dùng node ROS hay tín hiệu PWM.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Chương này chưa làm việc trực tiếp với ROS 2. Ở các chương sau, thông điệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; sẽ gửi vận tốc tiến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và vận tốc quay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; để một node tách thành lệnh cho hai bánh xe. Mọi thuật toán tách lệnh đều trở nên vô nghĩa nếu mũi xe bị bốc đầu khi chạy, mạch buck chưa được chỉnh điện áp hoặc vi điều khiển tự reset ngay khi động cơ khởi động. Các bài 03 và 04 sẽ dùng giao thức văn bản đơn giản thay cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;, nhưng phần cứng nguồn và cân bằng trọng tâm phải được hoàn thiện vững chắc ngay từ bước này.&lt;/p&gt;

&lt;h2 id=&quot;trọng-tâm-phải-nằm-giữa-trục-và-caster&quot;&gt;Trọng tâm phải nằm giữa trục và caster&lt;/h2&gt;

&lt;p&gt;Hai lốp xe tuyệt đối không được cọ vào tấm mica vì lực cản cọ sát sẽ đẩy động cơ về gần trạng thái kẹt (stall) và sinh nhiệt như đã phân tích ở Chương 05. Bánh caster lắp dưới phần mũi khung với trục xoay thẳng đứng và vết tiếp xúc nằm phía sau chốt xoay; nếu lắp ngược càng, bánh sẽ rung lắc khi di chuyển.&lt;/p&gt;

&lt;p&gt;Khối pin phải được buộc cố định chắc chắn để không xê dịch làm lệch trọng tâm hoặc gây chập dây. Nhìn từ mặt bên, trọng tâm chung của xe phải nằm trong khoảng giữa trục hai bánh chủ động và điểm chạm đất của bánh caster. Nếu trọng tâm lệch ra phía sau trục bánh, mũi xe sẽ nhấc lên khi tăng tốc và khiến lệnh đi thẳng bị lệch hướng.&lt;/p&gt;

&lt;p&gt;Chọn gốc tọa độ \(x = 0\) tại trục hai bánh chủ động, điểm chạm caster tại \(x = +90~\mathrm{mm}\) về phía mũi xe. Giả sử khung và hai động cơ nặng \(220~\mathrm{g}\) có trọng tâm riêng tại \(x = +15~\mathrm{mm}\), còn khối pin nặng \(90~\mathrm{g}\). Tọa độ trọng tâm chung là:&lt;/p&gt;

\[x_{cg} = \frac{220 \times 15 + 90 \times x_b}{310}.\]

&lt;p&gt;Nếu đặt pin phía sau trục tại \(x_b = -40~\mathrm{mm}\):&lt;/p&gt;

\[x_{cg} = \frac{3300 - 3600}{310} \approx -1{,}0~\mathrm{mm}.\]

&lt;p&gt;Trọng tâm rơi ra sau trục bánh nên bánh caster mất tải và mũi xe sẽ bốc đầu ngay khi tăng tốc. Ngược lại, nếu đặt cùng khối pin đó ở khoảng giữa trục và caster tại \(x_b = +35~\mathrm{mm}\):&lt;/p&gt;

\[x_{cg} = \frac{3300 + 3150}{310} \approx 20{,}8~\mathrm{mm},\]

&lt;p&gt;nằm gọn trong đoạn từ \(0\) đến \(90~\mathrm{mm}\). Khi đó bánh caster chịu một phần tải trọng khoảng \((20{,}8/90)\times 310~\mathrm{g} \approx 72~\mathrm{g}\), đủ để giữ mũi xe bám sàn ổn định trong khi hai bánh chủ động vẫn gánh phần lớn trọng lượng. Hãy ghi lại giá trị \(x_b\) thực tế của bạn bên cạnh \(b\) và \(r\).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;Bề rộng $$b$$ và caster phải còn tải&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;cây-nguồn-đo-trước-khi-gắn-mcu&quot;&gt;Cây nguồn, đo trước khi gắn MCU&lt;/h2&gt;

&lt;p&gt;Nhánh công suất động cơ và nhánh logic dùng chung khối pin và chung điểm mass, nhưng tuyệt đối không dùng chung một IC ổn áp tuyến tính và không đi dòng động cơ qua breadboard.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_tree.png&quot; alt=&quot;Pin, cầu chì, VM, và buck chỉnh 5,0 V trước khi cắm MCU&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Cực dương pin nối qua công tắc nguồn, qua cầu chì 2–3 A, rồi rẽ làm hai nhánh: nhánh VM cấp cho mạch driver và nhánh vào của mạch hạ áp LM2596. Cực âm pin, GND driver, GND buck và GND của vi điều khiển phải nối chung về một nút. Toàn bộ dây động cơ đi thẳng từ cầu đấu vít của driver tới hai cực động cơ, không cắm qua breadboard vì tiếp điểm breadboard chỉ chịu được vài trăm miliampe trong khi dòng kẹt động cơ lên tới cỡ ampe.&lt;/p&gt;

&lt;p&gt;Luôn chỉnh đầu ra mạch buck trước khi cắm vi điều khiển. Pack pin 2S đầy thường đo khoảng \(8{,}2~\mathrm{V}\) không tải (dải hoạt động \(7{,}4\)–\(8{,}4~\mathrm{V}\)). Dùng đồng hồ đo hai cực đầu ra buck và vặn chiết áp về đúng \(5{,}0~\mathrm{V}\) (chẳng hạn \(5{,}02~\mathrm{V}\) khi không tải). Chỉ sau khi xác nhận mức áp này, bạn mới cấp nguồn cho ESP32 (hoặc cấp nguồn ESP32 qua cáp USB lúc nạp code nhưng vẫn giữ nối chung GND). Không bao giờ đưa điện áp VM vào chân 5 V hay 3,3 V của ESP32, và không dùng cổng USB (giới hạn khoảng \(500~\mathrm{mA}\)) để nuôi động cơ khi hai động cơ TT có dòng kẹt khoảng \(2{,}4~\mathrm{A}\).&lt;/p&gt;

&lt;p&gt;Trên mạch L298N, jumper 5 V kích hoạt IC ổn áp tuyến tính 7805 lấy nguồn từ VM. Với pin 2S, đầu ra này tạm đủ nuôi phần logic nội bộ của chính chip L298N nhưng không đủ ổn định cho ESP32 khi phát Wi-Fi, nên vẫn bắt buộc dùng mạch buck rời cho ESP32 (nếu VM lớn hơn \(12~\mathrm{V}\), phải rút bỏ jumper 5 V). Kiểm tra công suất tỏa nhiệt trên 7805 tại điện áp đỉnh 2S với dòng \(0{,}25~\mathrm{A}\):&lt;/p&gt;

\[P \approx (8{,}4 - 5{,}0) \times 0{,}25 = 0{,}85~\mathrm{W}\]

&lt;p&gt;trên một vỏ IC nhỏ; ở mức \(12~\mathrm{V}\) với cùng dòng điện, nhiệt lượng tăng lên \(1{,}75~\mathrm{W}\). Do đó, cấp nguồn ESP32 qua mạch buck là lựa chọn bắt buộc trong cả hai trường hợp.&lt;/p&gt;

&lt;p&gt;Pin lithium phải luôn đặt trong đế pin hoặc pack có mạch BMS, không để đầu dây hàn trần dễ chạm chập. Sạc pin bằng bộ sạc đúng số cell và luôn có người giám sát. Mạch TP4056 chỉ dành cho pin 1S (đầy gần \(4{,}2~\mathrm{V}\)), không thể tạo ra \(8{,}4~\mathrm{V}\) và không cân bằng được hai cell nối tiếp trên pack 2S.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Bài thực hành này không phát PWM nên có thể đặt xe trực tiếp trên mặt bàn. Giữ công tắc pin ở trạng thái ngắt cho đến khi công tắc, cầu chì và toàn bộ dây mass đã đấu xong. Đảm bảo pin lithium nằm trong đế hoặc pack có BMS, không gắn TP4056 vào pack 2S. Nếu mạch buck có mùi khét hoặc nóng bất thường, ngắt công tắc nguồn ngay lập tức.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Việc&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung 2WD, hai motor TT, lốp, caster&lt;/td&gt;
      &lt;td&gt;Phần cơ chương 06&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pack 2S hoặc đế 18650 có BMS&lt;/td&gt;
      &lt;td&gt;Nguồn motor và logic&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Công tắc, đế cầu chì, chì 2–3 A&lt;/td&gt;
      &lt;td&gt;Nhánh dương&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TB6612 (nên dùng) hoặc L298N&lt;/td&gt;
      &lt;td&gt;Chỉ cấp VM; hôm nay chưa ra lệnh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Buck LM2596&lt;/td&gt;
      &lt;td&gt;Logic 5,0 V, chỉnh trước MCU&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 devkit&lt;/td&gt;
      &lt;td&gt;Gắn sau khi buck đã 5,0 V&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ, dây buộc, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Số đo&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Bắt cố định hai động cơ TT lên khung, sau đó dùng tay xoay thử từng lốp để chắc chắn không bị cọ vào tấm mica.&lt;/li&gt;
  &lt;li&gt;Lắp bánh caster sao cho vết tiếp xúc nằm phía sau chốt xoay. Chép hai thông số \(b\) và \(r\) từ Chương 06 vào sổ tay hôm nay.&lt;/li&gt;
  &lt;li&gt;Buộc cố định khối pin vào khoảng giữa trục bánh và bánh caster. Ước lượng tọa độ trọng tâm \(x_{cg}\) và ghi kết quả theo milimét.&lt;/li&gt;
  &lt;li&gt;Đấu dây cực dương pin qua công tắc, cầu chì rồi rẽ sang VM của driver và đầu vào buck. Nối chung cả bốn điểm mass; đảm bảo dây động cơ chỉ bắt trên cầu đấu vít của driver.&lt;/li&gt;
  &lt;li&gt;Khi chưa gắn ESP32, bật công tắc pin. Đo điện áp pack không tải và đầu ra mạch buck, vặn biến trở buck về đúng \(5{,}0~\mathrm{V}\), chụp ảnh hệ thống dây rồi mới gắn ESP32 vào đường nguồn 5 V (hoặc cấp USB nạp code nhưng giữ mass chung).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; có ghi ngày, điện áp pack không tải (ví dụ \(8{,}15~\mathrm{V}\) với pack 2S đầy), điện áp đầu ra buck sai lệch chỉ vài chục milivôn quanh \(5{,}0~\mathrm{V}\) đo trước khi gắn MCU, hai số đo \(b\), \(r\) và xác nhận dây động cơ không đi qua breadboard. Ảnh chụp thể hiện rõ công tắc, cầu chì, dây buộc pin và điểm mass chung. Chưa có lệnh quay bánh nào được thực hiện.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Mũi nhấc khi đặt xe xuống&lt;/td&gt;
      &lt;td&gt;Pin nằm sau trục; dịch về phía caster&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lốp cà mica&lt;/td&gt;
      &lt;td&gt;Sai đệm; sau này thành stall&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Buck ra 8 V hoặc 12 V&lt;/td&gt;
      &lt;td&gt;Chiết áp còn ở đầu; đừng cắm ESP32&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 reset khi chạm VM&lt;/td&gt;
      &lt;td&gt;Logic đang ăn 7805, hoặc đất chưa thành một nút&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Với tay lấy TP4056&lt;/td&gt;
      &lt;td&gt;Mạch ấy là 1S; pack 2S cần sạc 2S và BMS&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Chỉ đặt mua các linh kiện còn thiếu, không cần mua thêm bộ robot mới. Giá tham khảo khoảng năm 2026 (có thể thay đổi theo thời điểm): khung mica 2WD 60.000–150.000 đồng, động cơ TT 25.000–45.000 đồng/chiếc, module TB6612 25.000–70.000 đồng, mạch buck LM2596 10.000–25.000 đồng, pack 2S hoặc đế 18650 80.000–180.000 đồng, công tắc 8.000–20.000 đồng, đế cầu chì kèm chì 2–3 A 10.000–25.000 đồng, ESP32 devkit 70.000–150.000 đồng. Nếu hết TB6612, có thể dùng tạm L298N (đã kiểm tra giá khoảng 45.000 đồng), nhưng TB6612 mát hơn và trong mọi trường hợp vẫn dùng mạch buck để cấp nguồn ESP32. Lưu ý đế 1S không thay thế được pack 2S.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;L298N đã kiểm, khoảng 45.000 đồng: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=khung+xe+2wd&quot;&gt;Hshop khung&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=LM2596&quot;&gt;buck&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=ESP32&quot;&gt;ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=khung%20xe%202WD%20acrylic&quot;&gt;Shopee khung 2WD&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=LM2596&quot;&gt;LM2596&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=c%E1%BA%A7u%20ch%C3%AC%202A&quot;&gt;cầu chì&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=ESP32&quot;&gt;Lazada ESP32&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=pin%2018650%202S&quot;&gt;pin 18650&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=LM2596&quot;&gt;Thế Giới IC LM2596&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=ESP32&quot;&gt;ESP32&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=RsZBxfpcLu8&quot;&gt;PART1: Building Low-Cost Differential Wheeled Robot For Machine Learning, SLAM, and ROS&lt;/a&gt; — Aleksandar Haber PhD, tiếng Anh. Trình tự lắp đặt một robot vi sai cỡ nhỏ từ khung, động cơ tới hệ thống nguồn; giữ pin ngắt cho tới khi đo xong điện áp buck.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[BMS (Battery Management System)&lt;/td&gt;
          &lt;td&gt; &lt;/td&gt;
          &lt;td&gt;DIY or Buy&lt;/td&gt;
          &lt;td&gt; &lt;/td&gt;
          &lt;td&gt;Properly protecting Li-Ion/Li-Po Battery Packs](https://www.youtube.com/watch?v=rT-1gvkFj60) — GreatScott!, tiếng Anh. Minh họa bố trí pack pin và BMS thực tế trên xe; chú ý cực tính và đầu giắc trước khi đóng nắp.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Khung và hai động cơ nặng \(220~\mathrm{g}\) có trọng tâm tại \(x = +15~\mathrm{mm}\), pin nặng \(90~\mathrm{g}\) đặt tại \(x_b = -20~\mathrm{mm}\), bánh caster ở \(+90~\mathrm{mm}\). Tính \(x_{cg}\) và cho biết mũi xe có giữ được trên sàn không.&lt;/li&gt;
  &lt;li&gt;Mạch buck đo không tải được \(5{,}02~\mathrm{V}\), nhưng khi gắn ESP32 điện áp tụt xuống còn \(4{,}15~\mathrm{V}\). Cần kiểm tra những gì trước khi kết luận bo mạch hỏng?&lt;/li&gt;
  &lt;li&gt;Một sinh viên nối một mạch TP4056 vào hai đầu pack 2S để sạc qua cổng USB. Lỗi sai về số cell ở đây là gì và cần dùng loại bộ sạc nào?&lt;/li&gt;
  &lt;li&gt;Với VM là \(8{,}4~\mathrm{V}\) và jumper 5 V của L298N vẫn cắm, ước lượng công suất tỏa nhiệt trên IC 7805 ở dòng \(0{,}25~\mathrm{A}\). Vì sao vẫn phải cấp nguồn ESP32 bằng mạch buck?&lt;/li&gt;
  &lt;li&gt;Hai dây động cơ bị cắm xuyên qua breadboard trước khi vào driver, và cầu chì lại đặt sau mạch buck thay vì trên cực dương pin. Hãy viết lại đúng thứ tự cây nguồn (pin, công tắc, cầu chì, tải) và nêu rõ giới hạn của breadboard.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(x_{cg} = (3300 - 1800)/310 \approx 4{,}8~\mathrm{mm}\). Điểm này vẫn nằm giữa trục và caster nên mũi xe chưa nhấc, nhưng tải đè lên caster khá nhẹ; nếu caster rung hoặc kêu, nên dời pin về phía trước khoảng \(+35~\mathrm{mm}\).&lt;/li&gt;
  &lt;li&gt;Kiểm tra dây nối mass chung, kiểm tra xem cổng USB và đầu ra buck có đang cấp song song vào chân 5 V không, và kiểm tra dây quá mảnh hoặc mạch buck bị sụt áp khi chịu tải. Không vặn chiết áp buck trong lúc ESP32 đang cắm.&lt;/li&gt;
  &lt;li&gt;TP4056 là mạch sạc dành cho 1 cell (1S, đầy ở \(4{,}2~\mathrm{V}\)). Pack 2S có điện áp đầy gần \(8{,}4~\mathrm{V}\) nên bắt buộc dùng bộ sạc 2S có cân bằng và BMS.&lt;/li&gt;
  &lt;li&gt;\(P \approx (8{,}4 - 5{,}0)\times 0{,}25 = 0{,}85~\mathrm{W}\). Khi ESP32 phát Wi-Fi, dòng đỉnh vượt quá 0,25 A khiến IC 7805 nóng và sụt áp. Jumper 5 V trên 2S chỉ tạm nuôi logic của L298N (và phải rút bỏ nếu VM trên \(12~\mathrm{V}\)), còn ESP32 phải dùng mạch buck.&lt;/li&gt;
  &lt;li&gt;Thứ tự đúng: cực dương pin → công tắc → cầu chì 2–3 A → tách nhánh sang VM của driver và đầu vào buck. Dây động cơ nối trực tiếp trên cầu đấu vít vì tiếp điểm breadboard chỉ chịu được vài trăm miliampe, không chịu nổi dòng khởi động và dòng kẹt cỡ ampe.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;Datasheet L298 của ST&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Mạch Pololu TB6612FNG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/communication/serial/&quot;&gt;Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Pin, nguồn và an toàn trước khi lắp Capstone</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_00_batteries_power_safety/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter07/07_00_batteries_power_safety</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;~100 phút&lt;/strong&gt;. Trong bài này, bánh xe chưa được phép quay. Bạn sẽ xác định rõ loại hóa học pin, chọn dòng cầu chì phù hợp và chốt sơ đồ dây nguồn trước khi chuyển sang bài lắp khung.&lt;/p&gt;

&lt;p&gt;Nếu vi điều khiển là bộ não và động cơ là cơ bắp, thì &lt;strong&gt;khối pin LiPo / Li-Ion&lt;/strong&gt; chính là trái tim bơm năng lượng cho toàn bộ robot — đồng thời cũng là thành phần nguy hiểm nhất trong phòng lab nếu bị đối xử bất cẩn. Một viên pin LiPo 3S cỡ bằng nắm tay có thể xả tức thời hàng chục ampe, đủ làm nóng đỏ sợi dây đồng và bùng cháy trong vài giây nếu bị chập mạch hoặc xả quá sâu. Nắm vững hóa học pin, thông số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mAh&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-rating&lt;/code&gt;, cầu chì và mạch hạ áp là điều kiện tiên quyết &lt;em&gt;trước khi&lt;/em&gt; bạn chạm tay vào việc lắp Capstone.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-hóa-học-pin-lithium-ngưỡng-điện-áp-an-toàn-và-kiến-trúc-phân-phối-nguồn&quot;&gt;Kiến thức cần nhớ: Hóa học pin Lithium, Ngưỡng điện áp an toàn và Kiến trúc phân phối nguồn&lt;/h2&gt;

&lt;h3 id=&quot;1-giải-mã-thông-số-3s-2200mah-20c&quot;&gt;1. Giải mã thông số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3S 2200mAh 20C&lt;/code&gt;&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3S&lt;/code&gt; (3 Series):&lt;/strong&gt; 3 cell pin Lithium mắc nối tiếp. Mỗi cell có điện áp đầy &lt;strong&gt;\(4{,}2\text{ V}\)&lt;/strong&gt;, danh định &lt;strong&gt;\(3{,}7\text{ V}\)&lt;/strong&gt;, mức cảnh báo hạ cánh/dừng an toàn &lt;strong&gt;\(3{,}5\text{ V}\)&lt;/strong&gt;, và ngưỡng phá hủy hóa học tuyệt đối &lt;strong&gt;\(3{,}0\text{ V}\)&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2200 mAh&lt;/code&gt; (\(2{,}2\text{ Ah}\)):&lt;/strong&gt; dung lượng điện tích. Trong thực tế, chỉ nên dùng tối đa &lt;strong&gt;80%&lt;/strong&gt; dung lượng để giữ tuổi thọ pin (không bao giờ xả cạn 100%).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;20C&lt;/code&gt; (C-rating):&lt;/strong&gt; tốc độ xả liên tục tối đa an toàn tính bằng bội số của dung lượng (\(I_{\max} = C \times \text{Ah}\)).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-kiến-trúc-nguồn-chuẩn-cho-robot-cầu-chì--công-tắc--phân-nhánh&quot;&gt;2. Kiến trúc nguồn chuẩn cho robot: Cầu chì → Công tắc → Phân nhánh&lt;/h3&gt;
&lt;p&gt;Đường dây từ cực dương pin đi ra phải tuân theo đúng thứ tự: &lt;strong&gt;Giắc chống cắm ngược (XT60) → Cầu chì bảo vệ quá dòng → Công tắc tổng (E-Stop)&lt;/strong&gt; → chia làm 2 nhánh song song: (a) nhánh công suất trực tiếp cho driver động cơ, và (b) nhánh qua mạch hạ áp &lt;strong&gt;Buck converter (5 V / 3,3 V)&lt;/strong&gt; sạch sẽ cho vi điều khiển và cảm biến, với &lt;strong&gt;dây mass (GND) nối chung hình sao&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;3-quy-tắc-an-toàn-phòng-lab-bắt-buộc-với-pin-lithium&quot;&gt;3. Quy tắc an toàn phòng lab bắt buộc với pin Lithium&lt;/h3&gt;
&lt;p&gt;Luôn sạc bằng bộ sạc cân bằng (balance charger) đúng chế độ LiPo, lưu trữ pin ở điện áp bảo quản &lt;strong&gt;\(3{,}8\text{ V/cell}\)&lt;/strong&gt; trong túi chống cháy nếu không dùng quá vài ngày, và &lt;strong&gt;ngưng sử dụng ngay lập tức&lt;/strong&gt; nếu thấy vỏ pin phồng rộp hoặc có mùi ngọt hắc của dung môi điện phân.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-điện-áp-ngưỡng-dòng-xả-cực-đại-và-thời-gian-chạy-thực-tế-của-pin-lipo-3s&quot;&gt;Giải thích: Cầm tay tính điện áp ngưỡng, dòng xả cực đại và thời gian chạy thực tế của pin LiPo 3S&lt;/h3&gt;

&lt;p&gt;Xét viên pin LiPo &lt;strong&gt;3S – 2200 mAh – 20C&lt;/strong&gt; cấp nguồn cho robot Capstone tiêu thụ dòng trung bình &lt;strong&gt;\(I_{\text{tb}} = 2{,}75\text{ A}\)&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Ba mốc điện áp quan trọng của cả khối 3S:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Sạc đầy 100%: \(3 \times 4{,}2 = 12{,}6\text{ V}\).&lt;/li&gt;
      &lt;li&gt;Điện áp danh định: \(3 \times 3{,}7 = 11{,}1\text{ V}\).&lt;/li&gt;
      &lt;li&gt;Ngưỡng dừng chạy để sạc (\(3{,}5\text{ V/cell}\)): \(3 \times 3{,}5 = 10{,}5\text{ V}\) &lt;em&gt;(tuyệt đối không xả dưới \(9{,}0\text{ V}\)!)&lt;/em&gt;.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Dòng xả liên tục cực đại an toàn:&lt;/strong&gt;
\(I_{\max} = 20\text{C} \times 2{,}2\text{ Ah} = 44{,}0\text{ A}\)
&lt;em&gt;(Cực kỳ mạnh — đó là lý do nếu chập hai dây pin không có cầu chì, dây 18 AWG sẽ bốc cháy lập tức trước khi pin kịp hết điện!)&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thời gian chạy thực tế theo quy tắc giữ lại 20% dung lượng an toàn:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Dung lượng khả dụng (80%): \(Q_{\text{dùng}} = 0{,}80 \times 2{,}2\text{ Ah} = 1{,}76\text{ Ah}\).&lt;/li&gt;
      &lt;li&gt;Thời gian hoạt động liên tục:
\(t = \frac{Q_{\text{dùng}}}{I_{\text{tb}}} = \frac{1{,}76\text{ Ah}}{2{,}75\text{ A}} = 0{,}64\text{ giờ} \approx 38{,}4\text{ phút}.\)&lt;/li&gt;
      &lt;li&gt;Năng lượng dự trữ danh định của viên pin: \(E = 11{,}1\text{ V} \times 2{,}2\text{ Ah} = 24{,}42\text{ Wh}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn phân biệt được pin Li-ion 18650, LiPo dạng túi (pouch) và LiFePO4 dựa trên điện áp danh định và điện áp sạc đầy, từ đó không sử dụng nhầm bộ sạc khác hóa học. Bạn biết cách tính dòng xả liên tục theo hệ số C trên nhãn nhưng không dùng con số này để thay thế cho dòng đo thực tế khi chọn cầu chì. Bạn bố trí đúng vị trí cầu chì trên nhánh cực dương, chỉnh mạch hạ áp (buck) về 5,0 V trước khi kết nối ESP32, và vẽ hoàn chỉnh cây nguồn gồm: pin, công tắc, cầu chì, nhánh VM, mạch buck và điểm mass chung.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-trước&quot;&gt;Kiến thức cần trước&lt;/h2&gt;

&lt;p&gt;Bài an toàn ở Chương 00 đã nghiêm cấm việc gắn mạch sạc TP4056 lên đế pin 2S và quy định thứ tự đấu nối: mass, nguồn logic, tín hiệu, rồi mới tới VM. Định luật Ohm ở Chương 01 đủ để tính sụt áp trên dây dẫn, và bạn cần biết đo điện áp một chiều bằng đồng hồ vạn năng. Chưa cần firmware teleop; nếu chưa có cell pin trên bàn, bạn vẫn hoàn thành đầy đủ phần tính toán trên giấy và ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa có pin&lt;/code&gt; vào các ô đo thực tế.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-nằm-trên-lộ-trình&quot;&gt;Vì sao bài này nằm trên lộ trình&lt;/h2&gt;

&lt;p&gt;Các lỗi thường gặp nhất khiến Capstone A ngừng hoạt động đều bắt nguồn từ hệ thống nguồn: vi điều khiển tự reset ngay khi động cơ khởi động, cầu chì lắp sai nhánh nên không ngắt khi chập pin, hoặc dùng bộ sạc 4,2 V/cell cho pin LiFePO4. Bộ điều khiển PID hay lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; không thể cứu một cell pin bị phồng. Bài lắp khung ngay sau đây giả định rằng nhánh cực dương đã đi qua công tắc và cầu chì, đầu ra buck đã được chỉnh đúng 5,0 V và toàn bộ mass đã gom về một nút chung. ROS 2 chỉ gửi ý định chuyển động, còn năng lượng và dòng ngắn mạch nằm trực tiếp ở khối pin.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch07_battery_wiring.png&quot; alt=&quot;Cây nguồn: pin, công tắc, chì, rồi tách VM và buck&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;khái-niệm&quot;&gt;Khái niệm&lt;/h2&gt;

&lt;p&gt;Ba loại hóa học pin phổ biến trên robot cỡ nhỏ có mức điện áp sạc đầy khác nhau:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hóa học&lt;/th&gt;
      &lt;th&gt;Danh định một cell&lt;/th&gt;
      &lt;th&gt;Đầy (khoảng)&lt;/th&gt;
      &lt;th&gt;Pack 2S danh định&lt;/th&gt;
      &lt;th&gt;Pack 2S đầy&lt;/th&gt;
      &lt;th&gt;Sạc&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Li-ion 18650&lt;/td&gt;
      &lt;td&gt;3,6–3,7 V&lt;/td&gt;
      &lt;td&gt;4,20 V&lt;/td&gt;
      &lt;td&gt;7,2–7,4 V&lt;/td&gt;
      &lt;td&gt;8,40 V&lt;/td&gt;
      &lt;td&gt;Từng cell tới 4,20 V, hoặc sạc 2S có cân bằng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LiPo pouch&lt;/td&gt;
      &lt;td&gt;3,7 V&lt;/td&gt;
      &lt;td&gt;4,20 V&lt;/td&gt;
      &lt;td&gt;7,4 V&lt;/td&gt;
      &lt;td&gt;8,40 V&lt;/td&gt;
      &lt;td&gt;Sạc cân bằng đúng số cell, không phải sạc 18650 một ngăn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LiFePO4&lt;/td&gt;
      &lt;td&gt;3,2 V&lt;/td&gt;
      &lt;td&gt;khoảng 3,60–3,65 V&lt;/td&gt;
      &lt;td&gt;6,4 V&lt;/td&gt;
      &lt;td&gt;khoảng 7,3 V&lt;/td&gt;
      &lt;td&gt;Sạc LiFePO4. Cấm hồ sơ 4,20 V/cell&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Pin LiPo và Li-ion 18650 có cùng mức điện áp đầy 4,20 V mỗi cell nhưng khác nhau về vỏ bảo vệ: túi mềm của LiPo rất dễ bị thủng và bốc cháy, trong khi cell trụ kim loại khó bị đâm thủng hơn nhưng vẫn cháy nếu hư hại vỏ. Riêng LiFePO4 không thể sạc chung bằng bộ sạc LiPo/Li-ion dù an toàn nhiệt hơn, vì điện áp đầy của nó thấp hơn khoảng 0,55 V mỗi cell; dùng bộ sạc ngắt ở 4,20 V sẽ đẩy LiFePO4 vào vùng quá áp nguy hiểm.&lt;/p&gt;

&lt;p&gt;Hệ số C là thông số nhà sản xuất công bố: dòng xả liên tục danh định bằng C nhân với dung lượng tính theo ampe-giờ (Ah):&lt;/p&gt;

\[I_{\text{C}} = C \times \frac{Q_{\mathrm{mAh}}}{1000}\]

&lt;p&gt;Ví dụ, một cell 18650 ghi 2500 mAh và 10C có $I_{\text{C}} = 10 \times 2{,}5 = 25$ A. Đây là mức dòng xả khả dụng theo nhãn, tuyệt đối không phải lý do để bỏ cầu chì. Hai động cơ TT không bao giờ cần tới 25 A, nhưng nếu tua-vít vô tình làm chập cực pin, cell vẫn có thể phóng ra dòng lớn ngay tức thì. Cầu chì cần thiết chính vì pin có khả năng cấp dòng ngắn mạch cao.&lt;/p&gt;

&lt;p&gt;Dung lượng thực của cell 18650 hiếm khi vượt quá khoảng 3500 mAh; những nhãn in 9900 mAh là thông số ảo, như đã cảnh báo trong bài mua sắm ở Chương 00.&lt;/p&gt;

&lt;p&gt;Mạch BMS (quản lý và thường có chức năng cân bằng pin) nằm giữa các cell và hai cực đầu ra của pack, có nhiệm vụ ngắt khi xảy ra quá áp, dưới áp hoặc quá dòng theo ngưỡng thiết kế của mạch. Tuy nhiên, BMS không thay thế cho cầu chì chọn theo tiết diện dây và không thay thế công tắc ngắt nguồn đặt trong tầm tay. Nếu một pack pin giá rẻ quảng cáo “có mạch bảo vệ” nhưng bạn không quan sát thấy bảng mạch thực tế, hãy coi như pack đó chưa có bảo vệ.&lt;/p&gt;

&lt;p&gt;Mạch hạ áp (buck converter) dùng trong khóa học thường là loại dựa trên IC LM2596, yêu cầu điện áp đầu vào cao hơn điện áp đầu ra. Bạn phải chỉnh biến trở cho đến khi đồng hồ đo được đúng 5,0 V ở đầu ra &lt;strong&gt;khi chưa cắm ESP32&lt;/strong&gt;. Các module quảng cáo dòng 3 A thường nóng nhanh và sụt áp trước khi đạt 3 A liên tục; chúng phù hợp để cấp nguồn cho ESP32 và một vài cảm biến nhỏ, tuyệt đối không cấp nguồn cho hai động cơ qua mạch buck.&lt;/p&gt;

&lt;p&gt;Cầu chì phải lắp trên nhánh cực dương, ngay sau công tắc nguồn và trước điểm rẽ sang VM của driver và đầu vào mạch buck. Dòng định mức của cầu chì cần cao hơn dòng vận hành thực tế nhưng thấp hơn ngưỡng gây cháy đoạn dây nhỏ nhất trong mạch.&lt;/p&gt;

&lt;p&gt;Để ước lượng ban đầu, hãy đo dòng tiêu thụ thực tế của cả xe khi hai động cơ chạy trên sàn không bị kẹt, gọi là $I_{\mathrm{run}}$. Cầu chì tác động chậm có định mức khoảng $1{,}5$ đến $2$ lần $I_{\mathrm{run}}$ sẽ chịu được xung dòng khởi động. Với hai động cơ TT chạy nguồn 2S trong khóa học này, mức cầu chì thường chốt ở &lt;strong&gt;2 A hoặc 3 A&lt;/strong&gt; sau khi đã đo thực tế (khớp với checklist Chương 07). Nếu $I_{\mathrm{run}}$ đo được đã lên tới 2,5 A thì không nên gắn cầu chì 2 A rồi đổ lỗi cho động cơ hỏng khi đứt chì; hãy đo kiểm lại trước khi đổi cỡ cầu chì hoặc điều chỉnh chế độ chạy.&lt;/p&gt;

&lt;p&gt;Dây cắm Dupont chỉ dùng cho tín hiệu điều khiển. Dòng điện cấp cho động cơ phải đi qua dây đồng mềm đủ tiết diện, bắt trực tiếp vào cầu đấu vít trên mạch driver. Độ sụt áp trên dây dẫn tuân theo:&lt;/p&gt;

\[\Delta V = I R_{\mathrm{dây}}\]

&lt;p&gt;Một đoạn dây dài và mỏng có điện trở cỡ vài phần mười ôm khi mang dòng 2 A sẽ sụt vài phần mười vôn và nóng cục bộ. Nếu thấy dây dẫn ấm lên dù động cơ mới chạy không tải, hãy dừng ngay và thay dây tiết diện lớn hơn, không quấn thêm băng dính để che.&lt;/p&gt;

&lt;p&gt;Quy tắc sạc và bảo quản pin:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Luôn sạc pin ở nơi dễ quan sát, không sạc trên giường hoặc đặt dưới nắng trong ô tô.&lt;/li&gt;
  &lt;li&gt;Bảo quản và sạc pin LiPo bên trong túi chống cháy; không đâm thủng hoặc gấp gập túi cell.&lt;/li&gt;
  &lt;li&gt;Khi cất giữ lâu ngày, không để pin ở trạng thái đầy 100%: giữ Li-ion/LiPo ở khoảng 3,7–3,8 V mỗi cell và LiFePO4 ở khoảng 3,3 V mỗi cell (đây là điện áp bảo quản, không phải ngưỡng xả kiệt dưới tải).&lt;/li&gt;
  &lt;li&gt;Không bao giờ sạc lại cell bị phồng, thủng vỏ, sụt dưới khoảng 2,5 V (với Li-ion) hoặc tự nóng khi không tải; hãy mang đến điểm thu gom tái chế pin và không để chung với chìa khóa hay vật kim loại.&lt;/li&gt;
  &lt;li&gt;Luôn rút sạc trước khi rời khỏi nhà; BMS không thể thay thế sự giám sát của con người.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;ví-dụ-tính&quot;&gt;Ví dụ tính&lt;/h2&gt;

&lt;p&gt;Xét pack LiPo 2S có dung lượng 2200 mAh và nhãn 25C. Dòng xả liên tục theo nhãn là:&lt;/p&gt;

\[I_{\text{C}} = 25 \times 2{,}2 = 55~\mathrm{A}\]

&lt;p&gt;Xe đo được $I_{\mathrm{run}} = 0{,}8$ A khi chạy thẳng chậm và xung dòng kẹt ngắn khoảng 2 A. Khi đó, cầu chì 2 A hoặc 3 A là lựa chọn hợp lý: cao hơn dòng chạy 0,8 A, thấp hơn nhiều so với 55 A và bảo vệ tốt gần ngưỡng dòng kẹt. Thông số 25C không phải lý do để lắp cầu chì 50 A — nếu làm vậy, dây dẫn sẽ cháy trước khi cầu chì kịp ngắt.&lt;/p&gt;

&lt;p&gt;Với mạch buck: giả sử điện áp pin đo không tải là 8,1 V. Khi ESP32 vẫn để rời bên ngoài, bạn vặn biến trở cho đến khi đồng hồ trên hai cực ra báo 5,02 V rồi mới cấp vào phần logic. Nếu cắm ESP32 trước khi chỉnh, điện áp đầu ra ban đầu có thể đang ở mức 8 V và làm hỏng mạch ngay lập tức.&lt;/p&gt;

&lt;p&gt;Với pack LiFePO4 2S: điện áp đầy xấp xỉ $3{,}65 \times 2 = 7{,}3$ V, vẫn đủ nằm trong dải hoạt động 3–9 V của động cơ TT. Một bộ sạc dừng ở 8,4 V là bộ sạc sai hóa học; cần ghi rõ điều này vào sổ trước khi cắm giắc.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Không cấp tín hiệu PWM trong bài này và không đặt bánh xe chạm bàn nếu đã nối nguồn VM. Không sạc bất kỳ cell pin nào nếu chưa xác định chắc chắn hóa học pin. Đeo kính bảo hộ khi cắt dây và tháo nhẫn kim loại khi thao tác gần cực pin trần.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Việc&lt;/th&gt;
      &lt;th&gt;Giá&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ&lt;/td&gt;
      &lt;td&gt;Điện áp cell, đầu ra buck&lt;/td&gt;
      &lt;td&gt;Đã có từ chương 00, hoặc để trống đến khi bạn mua&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin bạn sẽ dùng (18650, LiPo hoặc LiFePO4)&lt;/td&gt;
      &lt;td&gt;Nguồn VM&lt;/td&gt;
      &lt;td&gt;Điền khi mở listing. Đừng chép tem dung lượng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Công tắc và cầu chì 2 A và 3 A&lt;/td&gt;
      &lt;td&gt;Ngắt được bằng tay, và ngắt khi chập&lt;/td&gt;
      &lt;td&gt;Điền khi mở listing&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Module buck hạ áp&lt;/td&gt;
      &lt;td&gt;5,0 V cho logic&lt;/td&gt;
      &lt;td&gt;Điền khi mở listing&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Túi LiPo nếu pack là túi mềm&lt;/td&gt;
      &lt;td&gt;Sạc và cất&lt;/td&gt;
      &lt;td&gt;Điền khi mở listing&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Các trang tìm kiếm tham khảo (giá thay đổi theo thời điểm): &lt;a href=&quot;https://hshop.vn/search?q=lipo&quot;&gt;Hshop, từ khóa lipo&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/&quot;&gt;Shopee&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/&quot;&gt;Lazada&lt;/a&gt;. Liên kết sản phẩm cell 18650 và bộ sạc khay đã có trong &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;bài BOM&lt;/a&gt; (ghi nhận ngày 23 tháng 9 năm 2026); hãy mở lại trang đó để kiểm tra trước khi đặt mua.&lt;/p&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Ghi rõ một dòng hóa học pin bạn dùng (18650, LiPo hoặc LiFePO4) kèm mức điện áp sạc đầy tối đa cho phép. Nếu chưa có pin, ghi loại dự định mua và hoàn thành phần giấy.&lt;/li&gt;
  &lt;li&gt;Đo điện áp hở mạch của từng cell hoặc từng dây cân bằng tiếp cận được. Cell 18650 hoặc LiPo đơn cần nằm trong khoảng 3,4 V đến 4,20 V trước khi lắp vào đế; nếu dưới khoảng 2,5 V, tuyệt đối không sạc.&lt;/li&gt;
  &lt;li&gt;Vẽ sơ đồ cây nguồn vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; với đủ bảy khối như hình minh họa: pin, công tắc, cầu chì, nhánh VM, mạch buck, ESP32 và mass chung. Đánh dấu rõ vùng cấm: cực dương pin không được nối trực tiếp vào chân 5 V hay 3,3 V của vi điều khiển.&lt;/li&gt;
  &lt;li&gt;Nếu đã có pin và mạch buck: đảm bảo bánh xe được nhấc khỏi bàn (hoặc chưa gắn động cơ). Cấp nguồn pin qua công tắc và cầu chì, vặn biến trở mạch buck đến 5,0 V ± 0,05 V và chụp ảnh màn hình đồng hồ trước khi nghĩ đến việc cắm ESP32.&lt;/li&gt;
  &lt;li&gt;Tính $I_{\text{C}}$ từ nhãn pin (nếu có) và ghi rõ một câu khẳng định không dùng con số này để chọn cầu chì. Ghi cỡ cầu chì sẽ lắp (2 A hoặc 3 A) cùng giá trị $I_{\mathrm{run}}$ đã đo (hoặc ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa đo&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Sổ tay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; ghi đầy đủ tên hóa học pin, ngưỡng điện áp đầy cho phép, số đo hoặc ảnh đồng hồ đầu ra buck 5,0 V (hoặc ghi chú chưa có buck), sơ đồ cây nguồn đủ bảy khối và câu xác nhận không chọn cầu chì theo $I_{\text{C}}$.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bạn thấy&lt;/th&gt;
      &lt;th&gt;Cần kiểm&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Buck ra hơn 6 V lúc ESP32 đã cắm&lt;/td&gt;
      &lt;td&gt;Bạn đã vặn khi có tải logic, hoặc biến trở chưa khóa. Rút ESP32, đặt lại 5,0 V&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 reset đúng lúc motor kéo&lt;/td&gt;
      &lt;td&gt;Motor và logic chung một nguồn yếu, hoặc mass không chung, hoặc dây motor đi qua breadboard&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TP4056 trên đế 2S&lt;/td&gt;
      &lt;td&gt;Tháo. Sạc từng cell 18650. LiPo 2S cần sạc cân bằng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LiFePO4 báo đầy ở 8,4 V&lt;/td&gt;
      &lt;td&gt;Sai hồ sơ sạc. Ngừng. Tìm sạc ghi LiFePO4&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chì 2 A nổ khi chưa kẹt bánh&lt;/td&gt;
      &lt;td&gt;$I_{\mathrm{run}}$ đã cao hơn mức chì, hoặc chập VM xuống mass phía sau chì. Đo dòng, đừng tăng chì trước khi tìm chập&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cell nóng trên bàn, không tải&lt;/td&gt;
      &lt;td&gt;Ngừng. Không sạc. Có thể chập trong hoặc BMS hỏng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [BMS (Battery Management System)&lt;/td&gt;
          &lt;td&gt; &lt;/td&gt;
          &lt;td&gt;DIY or Buy&lt;/td&gt;
          &lt;td&gt; &lt;/td&gt;
          &lt;td&gt;Properly protecting Li-Ion/Li-Po Battery Packs](https://www.youtube.com/watch?v=rT-1gvkFj60) — GreatScott!, tiếng Anh. Giải thích cách BMS bảo vệ pack pin lithium trước quá áp, dưới áp và quá dòng; lưu ý không bỏ BMS cũng không dùng BMS thay cầu chì.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=4qIYyaRKGVE&quot;&gt;LiPo C Rating Explained – No, a Higher C Rating Won’t Hurt Your ESC or Motor&lt;/a&gt; — RcHelpDotCom, tiếng Anh. Giải thích ý nghĩa hệ số C trên nhãn LiPo — thông số này không dùng làm cỡ cầu chì.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=rfChSvb8FX0&quot;&gt;Buck Converter&lt;/a&gt; — The Organic Chemistry Tutor, tiếng Anh. Nguyên lý mạch hạ áp buck xuống 5 V; luôn chỉnh điện áp ra trước khi nối ESP32.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Video ngắn (Facebook Reel) dưới đây của Hoàng Quân (Cơ điện tử ICTU, &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/931720623225817&quot;&gt;Test pin drone, ESC cháy&lt;/a&gt; — Chú thích: dùng pin xe nâng nặng 3 tạ để test drone, phần test cháy ESC khi pin chưa hết, cảm ơn PKG Battery. Minh họa thực tế việc pack pin công suất lớn có thể đốt cháy mạch công suất rất nhanh nếu không tính trước cầu chì và dòng liên tục.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nhãn một pack LiPo 3S ghi 1500 mAh, 30C. Tính $I_{\text{C}}$, và giải thích vì sao xe hai động cơ TT vẫn không lắp cầu chì 40 A.&lt;/li&gt;
  &lt;li&gt;Hai cell 18650 đo được 4,18 V và 3,71 V. Bạn có được lắp ngay hai cell này vào đế nối tiếp 2S để chạy không? Cần sạc như thế nào?&lt;/li&gt;
  &lt;li&gt;Mạch buck đo được 5,0 V khi không tải nhưng sụt xuống 4,6 V ngay khi ESP32 bật Wi-Fi. Việc đầu tiên cần kiểm tra hoặc xử lý là gì?&lt;/li&gt;
  &lt;li&gt;Vì sao mass của mạch buck, mạch driver và ESP32 phải nối thành một nút chung trước khi cấp tín hiệu PWM?&lt;/li&gt;
  &lt;li&gt;Một pack LiFePO4 4S đầy ở khoảng bao nhiêu vôn nếu mỗi cell đầy là 3,65 V? Có dùng được bộ sạc 4S Li-ion (16,8 V) không?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;$I_{\text{C}} = 30 \times 1{,}5 = 45$ A. Cầu chì dùng để bảo vệ dây dẫn và an toàn người dùng, không chọn theo dòng xả tối đa trên nhãn; dòng cầu chì phải bám theo $I_{\mathrm{run}}$ đo thực tế.&lt;/li&gt;
  &lt;li&gt;Không được lắp nối tiếp khi hai cell lệch nhau gần 0,5 V. Cần sạc riêng từng cell lên khoảng 4,20 V, đo lại để xác nhận cân bằng rồi mới lắp vào đế.&lt;/li&gt;
  &lt;li&gt;Mạch buck không đáp ứng nổi dòng đỉnh (hoặc dây nguồn quá mảnh gây sụt áp). Cần giảm tải, kiểm tra dây hoặc đổi module buck tốt hơn; tuyệt đối không vặn tăng lên 5,5 V lúc không tải để “bù”.&lt;/li&gt;
  &lt;li&gt;Dòng điện động cơ phải có đường hồi về nguồn ổn định; nếu không nối chung mass, dòng điện hoặc nhiễu có thể tìm đường hồi qua các chân GPIO làm hỏng vi điều khiển.&lt;/li&gt;
  &lt;li&gt;Điện áp đầy khoảng $4 \times 3{,}65 = 14{,}6$ V. Không được dùng bộ sạc 16,8 V vì đó là hồ sơ 4,20 V/cell dành cho Li-ion.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.batteryuniversity.com/article/bu-409-charging-lithium-ion&quot;&gt;Battery University, BU-409, sạc lithium-ion&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.batteryuniversity.com/article/bu-205-types-of-lithium-ion&quot;&gt;Battery University, BU-205, các họ lithium-ion&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.batteryuniversity.com/article/bu-409b-charging-lithium-iron-phosphate&quot;&gt;Battery University, BU-409b, sạc LiFePO4&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter00/00_03_safety_mindset_lab/&quot;&gt;Bài an toàn chương 00&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM chương 00&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 06</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_09_ch06_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_09_ch06_lab_checklist</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;50–70 phút&lt;/strong&gt; để tổng hợp các ghi chép đã làm và bổ sung những mục còn thiếu. Đây là bài kiểm tra tổng kết (cổng qua chương), không giới thiệu cơ cấu mới.&lt;/p&gt;

&lt;p&gt;Hoàn thiện xong phần cơ khí không có nghĩa là “nhìn xe đứng được trên bàn là xong”. Một khung xe đạt chuẩn sẵn sàng bước sang Capstone Chương 07 phải vượt qua được các bài kiểm tra định lượng về độ đồng tâm, lực cản lăn, độ vững khi rung lắc và khả năng tiếp cận bảo trì (có tháo thay pin trong 30 giây mà không cần tháo 12 con ốc không?). Checklist Chương 06 giúp bạn khóa chặt chất lượng cơ khí trước khi đóng dây điện tử lên khung.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-tiêu-chí-nghiệm-thu-cơ-khí-cho-robot-di-động&quot;&gt;Kiến thức cần nhớ: Tiêu chí nghiệm thu cơ khí cho robot di động&lt;/h2&gt;

&lt;h3 id=&quot;1-lực-cản-lăn-rolling-resistance-và-chẩn-đoán-kẹt-trục&quot;&gt;1. Lực cản lăn (Rolling Resistance) và chẩn đoán kẹt trục&lt;/h3&gt;
&lt;p&gt;Khi ngắt khớp nối động cơ (hoặc để bánh quay tự do), hệ số cản lăn \(C_{rr} = \frac{F_{\text{kéo}}}{M g}\) phản ánh trực tiếp sức khỏe của bạc đạn, bánh caster và độ đồng phẳng khung xe. Một khung lắp tốt trên sàn nhẵn có \(C_{rr} \approx 0{,}02\text{–}0{,}05\); nếu \(C_{rr} &amp;gt; 0{,}10\), năng lượng pin đang bị đốt oan để thắng ma sát kẹt cơ khí!&lt;/p&gt;

&lt;h3 id=&quot;2-bài-kiểm-tra-lắc-nhẹ-shake-test-và-kiểm-tra-giữ-dây&quot;&gt;2. Bài kiểm tra lắc nhẹ (Shake Test) và kiểm tra giữ dây&lt;/h3&gt;
&lt;p&gt;Nhấc robot lên khỏi mặt bàn và lắc nhẹ theo 3 trục: không được có tiếng ốc lỏng lạch cạch, cục pin không được xê dịch, và không sợi dây nào được lơ lửng gần bánh xe quay có nguy cơ bị cuốn vào trục.&lt;/p&gt;

&lt;h3 id=&quot;3-khả-năng-bảo-trì-design-for-maintainability&quot;&gt;3. Khả năng bảo trì (Design for Maintainability)&lt;/h3&gt;
&lt;p&gt;Trong giai đoạn tích hợp Chương 07, bạn sẽ cắm cáp USB nạp code, tháo sạc pin và đo áp bằng đồng hồ vạn năng hàng chục lần. Đảm bảo cổng USB, công tắc nguồn tổng, cầu chì và pin đều tiếp cận được trực tiếp mà không phải tháo rã cả khung xe.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-kiểm-tra-hệ-số-cản-lăn-c_rr-bằng-cân-lực-kéo-đơn-giản&quot;&gt;Giải thích: Cầm tay kiểm tra hệ số cản lăn \(C_{rr}\) bằng cân lực kéo đơn giản&lt;/h3&gt;

&lt;p&gt;Đặt robot đã lắp đủ khung, bánh và pin nặng tổng cộng &lt;strong&gt;\(M = 2{,}5\text{ kg}\)&lt;/strong&gt; (trọng lượng \(W = 2{,}5 \times 9{,}81 = 24{,}525\text{ N}\)) lên mặt sàn phẳng (bánh xe ở trạng thái lăn tự do không nối động cơ). Móc một cân lực lò xo nhỏ vào mũi xe và kéo từ từ theo phương ngang:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Trường hợp khung lắp chuẩn:&lt;/strong&gt; xe bắt đầu lăn đều ở lực kéo \(F_1 = 0{,}75\text{ N}\).
\(C_{rr, 1} = \frac{F_1}{M g} = \frac{0{,}75}{24{,}525} \approx 0{,}0306 \quad (3{,}1\%)\)
→ &lt;strong&gt;ĐẠT!&lt;/strong&gt; Bạc đạn êm, bánh caster không kẹt.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Trường hợp bánh caster bị vặn lệch hoặc lốp cọ nhẹ vào thành khung:&lt;/strong&gt; bạn phải kéo tới \(F_2 = 3{,}0\text{ N}\) xe mới lăn.
\(C_{rr, 2} = \frac{3{,}0}{24{,}525} \approx 0{,}122 \quad (12{,}2\%)\)
→ &lt;strong&gt;KHÔNG ĐẠT!&lt;/strong&gt; Ở tốc độ \(0{,}5\text{ m/s}\), riêng lực cản ma sát lỗi này đã ngốn mất \(P = F_2 \cdot v = 3{,}0 \times 0{,}5 = 1{,}5\text{ W}\) công suất cơ liên tục — bắt buộc căn chỉnh lại khung trước khi sang Chương 07!&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; của bạn có đầy đủ: khổ vết \(b\) và bán kính lăn \(r\), kết quả nhận dạng cỡ ốc, tỉ số truyền đã dự đoán và đo kiểm, một phép tính cơ cấu bốn thanh (Grashof), đai hoặc thanh răng, ảnh giảm lực kéo dây (hoặc kế hoạch có ngày thực hiện) và danh sách linh kiện còn thiếu cho Chương 07. Bạn cũng chốt được trong một câu rõ ràng rằng mình đã đạt cổng trên phần cứng thực tế hay trên bản vẽ có dán nhãn.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Nội dung từ Bài 01 đến Bài 08. Nếu có bài thực hành nào chưa xong, checklist này đóng vai trò mục lục giúp bạn mở lại đúng bài đó. Hãy sử dụng đúng sổ tay đã ghi các số đo milimét từ đầu chương.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Chương 07 sẽ bắt đầu cấp điện cho khung robot và Chương 08 sẽ lập trình các phương trình động học. Cả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; lẫn tệp URDF trong ROS đều cần lấy các tham số \(b\), \(r\) và \(i\) từ một nguồn thống nhất: trang tổng hợp có ghi ngày và phương pháp đo này. Việc thiếu bánh caster hay thiếu túi đai ốc nyloc nên nằm trong danh sách mua sắm từ bây giờ thay vì trở thành sự cố bất ngờ lúc lắp ráp. Điều kiện qua chương là bằng chứng ghi rõ trong sổ tay — không thể dựa vào trí nhớ ước chừng để sang Chương 07.&lt;/p&gt;

&lt;h2 id=&quot;cổng&quot;&gt;Cổng&lt;/h2&gt;

&lt;p&gt;Chép bảng dưới đây vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; và điền cột bằng chứng bằng số đo cụ thể, tên tệp ảnh, hoặc ghi chú “bản vẽ, chưa phải phần cứng”.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Cổng&lt;/th&gt;
      &lt;th&gt;“Xong” trông như thế nào&lt;/th&gt;
      &lt;th&gt;Bằng chứng&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Khổ vết \(b\)&lt;/td&gt;
      &lt;td&gt;Milimét giữa các tâm tiếp xúc của mặt lốp&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bán kính lăn \(r\)&lt;/td&gt;
      &lt;td&gt;Từ \(s/(2\pi)\), cả hai bánh&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nhận dạng ốc&lt;/td&gt;
      &lt;td&gt;M3 được gọi tên, nyloc được phân biệt với đai ốc thường&lt;/td&gt;
      &lt;td&gt;Ảnh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tỉ số truyền&lt;/td&gt;
      &lt;td&gt;Dự đoán và một phép kiểm trong khoảng 10 phần trăm, hoặc một dự đoán trên giấy đã dán nhãn&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cơ cấu thanh, đai, hoặc thanh răng&lt;/td&gt;
      &lt;td&gt;Một phép thử Grashof, hoặc một tỉ số răng GT2, hoặc một hành trình thanh răng&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Giảm lực kéo dây&lt;/td&gt;
      &lt;td&gt;Ảnh trước/sau, hoặc một kế hoạch viết ra nếu chưa có dây&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Linh kiện còn thiếu&lt;/td&gt;
      &lt;td&gt;Danh sách tường minh cho buổi lắp Chương 07&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Điều kiện đạt:&lt;/strong&gt; Mọi hàng đều có bằng chứng cụ thể; hàng tỉ số truyền nêu rõ cách kiểm chứng; hàng linh kiện còn thiếu ghi rõ “không” hoặc liệt kê danh sách đặt mua trong tuần. Nếu mới chỉ làm trên bản vẽ, bạn vẫn đạt cổng lập luận. Tuy nhiên, trạng thái “sẵn sàng phần cứng” là một xác nhận riêng trên cùng trang đó, chỉ được ghi khi tấm khung, hai bánh xe, bánh caster và hai động cơ đều đã có sẵn trên bàn và hoàn thành kiểm tra ở Bài 08. Chương 07 chỉ bắt đầu khi đạt trạng thái sẵn sàng phần cứng.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;Hai chiều dài mà cổng không nhận là số đoán&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/gear_train_ratio.png&quot; alt=&quot;Tỉ số mà cổng muốn đứng cạnh những chiều dài ấy&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Một sinh viên lăn thử bánh thứ nhất được quãng đường \(s = 188\) mm, suy ra:&lt;/p&gt;

\[r = \frac{188}{2\pi} \approx 29.9\ \text{mm}.\]

&lt;p&gt;Bánh còn lại cho kết quả 30,4 mm. Sinh viên ghi cả hai số đo, kiểm tra lại độ đồng tâm của lốp để xác nhận sai lệch nằm trong 1 mm rồi lấy giá trị trung bình làm việc là 30,2 mm. Khổ vết đo giữa hai tâm tiếp xúc là \(b = 152\) mm. Hộp số đóng dấu 1:48; sau 3 vòng đầu vào, góc đo được là \(23^\circ\) so với dự đoán:&lt;/p&gt;

\[\frac{3}{48}\times 360^\circ = 22.5^\circ,\]

&lt;p&gt;sai lệch khoảng 2%, đạt yêu cầu. Mục Grashof ghi đúng ví dụ Bài 04 (\(s + l = 120\) mm, \(p + q = 125\) mm). Ảnh giảm lực kéo dây thể hiện rõ dây rút và vòng dây dự phòng. Mục linh kiện còn thiếu ghi: một túi đai ốc nyloc. Như vậy, trang ghi chép này đạt cổng phần cứng (có thể thực hiện bước thử nhấc bánh nếu giá động cơ đã đủ hai đai ốc nyloc, dù chưa thể kết luận toàn bộ mối ốc đã hoàn tất cho tới khi mua đủ túi nyloc).&lt;/p&gt;

&lt;h2 id=&quot;lab-điền-cổng-từ-bằng-chứng-đang-có&quot;&gt;Lab: điền cổng từ bằng chứng đang có&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Buổi tổng hợp này không cấp nguồn điện. Nếu cần lặp lại phép xoay tay hoặc kiểm tra lực kéo dây, pin phải được ngắt hoàn toàn như hướng dẫn ở Bài 05 và Bài 07. Không “kích thử 5 V” để kiểm tra một động cơ bị nặng tay khi xoay.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; và các ảnh của chương này&lt;/td&gt;
      &lt;td&gt;Bằng chứng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung và dụng cụ, nếu các bạn đang đóng các lỗ hổng phần cứng&lt;/td&gt;
      &lt;td&gt;Chỉ những hàng không đạt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Danh sách mua&lt;/td&gt;
      &lt;td&gt;Hàng linh kiện còn thiếu&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Mở lại ghi chú của Bài 01, 02, 03 (hoặc 08), 04 và 07; chép chính xác số liệu đã đo thay vì nhập lại theo trí nhớ.&lt;/li&gt;
  &lt;li&gt;Điền đầy đủ bảng cổng: ghi tên tệp ảnh ở các mục yêu cầu ảnh, hoặc ghi “bản vẽ” nếu bộ kit chưa về.&lt;/li&gt;
  &lt;li&gt;Tính lại một phép kiểm tra theo mẫu ví dụ (\(r\) từ \(s\) hoặc \(\theta_{\text{out}}\) từ \(i\)) để loại bỏ sai sót do chép nhầm số.&lt;/li&gt;
  &lt;li&gt;Viết câu xác nhận đạt cổng và (nếu đủ điều kiện) câu xác nhận sẵn sàng phần cứng.&lt;/li&gt;
  &lt;li&gt;Chuyển mục linh kiện còn thiếu thành danh sách mua sắm cụ thể; chỉ đưa bộ đai GT2 vào nếu truyền động của bạn thực sự dùng đai.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Một bảng cổng điền đầy đủ kèm hai câu xác nhận rõ ràng, đủ để một đồng đội khác có thể bắt tay vào phần cơ khí Chương 07 dựa trên sổ tay của bạn mà không cần hỏi lại giá trị \(b\). Nếu dùng đường bản vẽ, câu xác nhận nêu rõ điều này và liệt kê phần cứng cần bổ sung.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Các bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường nghĩa là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;\(b\) trông như một số tròn 150 không có phương pháp&lt;/td&gt;
      &lt;td&gt;Nó được nhớ lại; đo lại giữa các tâm tiếp xúc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tỉ số không có góc dự đoán đứng cạnh&lt;/td&gt;
      &lt;td&gt;Phép kiểm bị bỏ; chạy Bài 08&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tuyên bố sẵn sàng phần cứng từ một bản vẽ&lt;/td&gt;
      &lt;td&gt;Tách thành hai câu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hàng linh kiện thiếu để trống&lt;/td&gt;
      &lt;td&gt;Nó vẫn là một hàng, kể cả khi danh sách là “không”&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thiếu \(r\) của một bánh&lt;/td&gt;
      &lt;td&gt;Đo nó; hai bán kính lệch làm lệnh đi thẳng bị quay ngang&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Chỉ đặt mua đúng những hạng mục còn thiếu trong bảng tổng hợp. Không cần mua dự phòng thêm khung xe thứ hai trước khi vào Chương 07, và chỉ mua đai GT2 khi thiết kế ở Bài 04 có dùng đai.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Lỗ hổng&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Khi nào&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khung xe robot 2 bánh&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;80.000–200.000&lt;/td&gt;
      &lt;td&gt;Chưa có tấm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ ốc M3 và nyloc&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ốc M3&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;25.000–70.000&lt;/td&gt;
      &lt;td&gt;Lần phân loại Bài 02 bị thiếu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh caster&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh caster robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;12.000–40.000&lt;/td&gt;
      &lt;td&gt;Mũi chưa có bánh có đuôi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đai và pulley GT2&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây đai GT2&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000 cho một đai&lt;/td&gt;
      &lt;td&gt;Chỉ khi truyền động của các bạn dùng đai&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Các trang tìm mua:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=khung+xe+robot&quot;&gt;Hshop: khung&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=%E1%BB%91c%20M3&quot;&gt;Shopee: ốc M3&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=b%C3%A1nh%20caster&quot;&gt;Lazada: bánh caster&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=%E1%BB%91c%20M3&quot;&gt;Thế Giới IC: ốc&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Giá bán có thể thay đổi theo thời điểm. Hãy đặt sớm các linh kiện còn thiếu trước buổi lab Chương 07 và giữ thông tin đơn hàng để đối chiếu đúng cỡ đã đo khi nhận hàng.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=aGHrhm-Rcp0&quot;&gt;Gear Train Design - How to calculate gear trains mechanical engineering&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Ôn lại cách tính và nhân tỉ số truyền trước khi đánh dấu hoàn thành checklist cơ khí.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Hai phép lăn một vòng cho kết quả 196 mm và 191 mm. Tính bán kính lăn của cả hai bánh; đã nên lấy trung bình ngay chưa?&lt;/li&gt;
  &lt;li&gt;Với hộp số đóng dấu 1:34, sau ba vòng đầu vào đo được góc đầu ra \(33^\circ\). Tính góc dự đoán, phần trăm sai số và cho biết kết quả này đạt hay cần đo lại.&lt;/li&gt;
  &lt;li&gt;Một thành viên ghi khổ vết \(b\) là “khoảng cách giữa hai động cơ, 140 mm”. Bạn cần yêu cầu họ đo lại kích thước nào, và việc đó giúp tránh triệu chứng gì trên ROS?&lt;/li&gt;
  &lt;li&gt;Ô giảm lực kéo dây ghi “để sau”. Cổng lập luận và trạng thái sẵn sàng phần cứng có đạt không?&lt;/li&gt;
  &lt;li&gt;Tự viết câu xác nhận đạt cổng bằng số liệu thực tế bạn có (ghi rõ “bản vẽ” nếu chưa có phần cứng).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Hai bán kính lần lượt là \(r \approx 31.2\) mm và \(30.4\) mm. Dù chênh lệch bán kính là 0,8 mm (gần sát ngưỡng 1 mm), chênh lệch quãng lăn tới 5 mm cho thấy lốp có thể lắp chưa đều; nên lắp chỉnh lại lốp và đo kiểm tra thêm một lần nữa trước khi lấy trung bình.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;Góc dự đoán: \(\frac{3}{34} \times 360^\circ \approx 31.8^\circ\). Sai số tương đối: $$&lt;/td&gt;
          &lt;td&gt;33 - 31.8&lt;/td&gt;
          &lt;td&gt;/31.8 \approx 4\%$$, nằm gọn trong ngưỡng 10% nên đạt.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;Yêu cầu đo lại khoảng cách giữa hai tâm tiếp xúc của mặt lốp. Việc này tránh hiện tượng gửi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; đi thẳng nhưng robot lại chạy cong thành vòng tròn.&lt;/li&gt;
  &lt;li&gt;Cổng lập luận chưa đạt cho tới khi có ảnh chụp hoặc một kế hoạch cụ thể có ngày thực hiện. Trạng thái sẵn sàng phần cứng cũng chỉ đạt sau khi hoàn thiện giảm lực kéo dây thực tế cùng toàn bộ checklist Bài 08.&lt;/li&gt;
  &lt;li&gt;Câu xác nhận cần nêu đầy đủ \(b\), \(r\), \(i\), kết quả mục cơ cấu thanh/đai/thanh răng và loại bằng chứng (phần cứng hay bản vẽ), tuyệt đối không dùng số ước đoán.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ros-controls/ros2_controllers/tree/master/diff_drive_controller&quot;&gt;diff_drive_controller&lt;/a&gt; — tệp cấu hình nơi các tham số \(b\), \(r\) và số xung mỗi vòng của bảng tổng hợp này sẽ được nhập vào.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://wiki.ros.org/diff_drive_controller&quot;&gt;Wiki diff_drive_controller ROS 1&lt;/a&gt; — tổng hợp tên tham số trên một trang (tài liệu ROS 1 nhưng giữ nguyên mô hình hình học).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://navigation.ros.org/setup_guides/odom/setup_odom.html&quot;&gt;Thiết lập odometry trên Nav2&lt;/a&gt; — hệ thống tiêu thụ trực tiếp dữ liệu odometry sinh ra từ các tham số trên.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab: đo tỉ số truyền và checklist lắp</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_08_mechanisms_lab/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_08_mechanisms_lab</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;80–110 phút&lt;/strong&gt;. Đây là bài thực hành trực tiếp trên cơ khí; nguồn điện phải tắt hoàn toàn trong mọi bước kiểm tra.&lt;/p&gt;

&lt;p&gt;Thông số in trên nhãn hộp số giá rẻ mua online không phải lúc nào cũng khớp 100% với thực tế: một động cơ quảng cáo “hộp số 30:1” đôi khi thực chất là 29,86:1 hoặc 34:1, và độ rơ thực tế sau khi lắp lên khung có thể lớn gấp đôi thông số lý thuyết. &lt;strong&gt;Bài lab đo tỉ số truyền và kiểm tra lắp ráp&lt;/strong&gt; rèn cho bạn thói quen quý giá của kỹ sư thực nghiệm: &lt;em&gt;đừng tin mù quáng vào nhãn bán hàng — hãy tự đo trực tiếp bằng thước, đánh dấu phấn và đồng hồ, rồi đưa con số đo thật vào cấu hình phần mềm!&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-phương-pháp-đo-thực-nghiệm-cơ-cấu-truyền-động&quot;&gt;Kiến thức cần nhớ: Phương pháp đo thực nghiệm cơ cấu truyền động&lt;/h2&gt;

&lt;h3 id=&quot;1-đo-tỉ-số-truyền-thực-nghiệm-phương-pháp-đếm-nhiều-vòng&quot;&gt;1. Đo tỉ số truyền thực nghiệm (Phương pháp đếm nhiều vòng)&lt;/h3&gt;
&lt;p&gt;Nếu chỉ quay trục vào 1 vòng để ước lượng góc trục ra bằng mắt, sai số đọc góc lên tới vài phần trăm. Kỹ thuật chuẩn là &lt;strong&gt;đánh dấu vạch tham chiếu trên cả hai trục&lt;/strong&gt;, quay trục đầu vào đúng \(N_{\text{in}}\) vòng lớn (ví dụ 10–20 vòng) cho đến khi trục ra quay đủ 1 hoặc nhiều vòng chẵn, từ đó tính \(G = N_{\text{in}} / N_{\text{out}}\) với sai số nhỏ hơn \(0{,}5\%\).&lt;/p&gt;

&lt;h3 id=&quot;2-đo-độ-rơ-góc-backlash-bằng-phương-pháp-tay-đòn-khuếch-đại&quot;&gt;2. Đo độ rơ góc (Backlash) bằng phương pháp tay đòn khuếch đại&lt;/h3&gt;
&lt;p&gt;Đo trực tiếp góc rơ \(1^\circ\) trên trục tròn đường kính \(6\text{ mm}\) bằng mắt thường là bất khả thi. Thay vào đó, kẹp một thanh thước mảnh dài \(L = 100\text{ mm}\) vào trục đầu ra, giữ cứng trục vào rồi lắc nhẹ đầu thanh thước qua lại đo khoảng dịch chuyển tuyến tính \(\Delta x\) bằng thước cặp, sau đó đổi sang độ: \(\Delta\theta = \frac{\Delta x}{L} \cdot \frac{180^\circ}{\pi}\).&lt;/p&gt;

&lt;h3 id=&quot;3-kiểm-tra-kẹt-cơ-khí-binding-theo-chu-kỳ-quay-360circ&quot;&gt;3. Kiểm tra kẹt cơ khí (Binding) theo chu kỳ quay \(360^\circ\)&lt;/h3&gt;
&lt;p&gt;Quay tay trục bánh xe trọn một vòng \(360^\circ\) (khi đã ngắt động cơ): nếu thấy nhẹ ở nửa vòng này nhưng đột nhiên nặng khựng ở nửa vòng kia, chắc chắn trục đang bị &lt;strong&gt;lệch tâm (eccentricity)&lt;/strong&gt; hoặc bánh răng bị đảo — bắt buộc căn chỉnh lại trước khi cấp điện!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-xử-lý-số-liệu-đo-tỉ-số-truyền-và-góc-rơ-backlash-trong-phòng-lab&quot;&gt;Giải thích: Cầm tay xử lý số liệu đo tỉ số truyền và góc rơ Backlash trong phòng lab&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Xác định tỉ số truyền thực của một hộp số không rõ nhãn:&lt;/strong&gt;
Bạn đánh dấu vạch sơn trắng lên trục động cơ và trục bánh xe. Quay trục động cơ đúng &lt;strong&gt;\(15\) vòng trọn vẹn&lt;/strong&gt;, thấy vạch trên bánh xe quay đúng &lt;strong&gt;\(1\) vòng&lt;/strong&gt; trở về vị trí cũ:
\(G_{\text{đo}} = \frac{N_{\text{in}}}{N_{\text{out}}} = \frac{15}{1} = 15{:}1.\)
&lt;em&gt;(Nếu quay \(30\) vòng trục vào mà trục ra quay được \(1\) vòng + \(18^\circ = 1{,}05\) vòng, thì \(G_{\text{đo}} = 30 / 1{,}05 \approx 28{,}57{:}1\) — sai khác tới 5% so với con số “30:1” in trên nhãn!)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Đo độ rơ góc Backlash bằng thanh thước dài \(L = 100\text{ mm}\):&lt;/strong&gt;
Giữ chặt trục đầu vào, lắc nhẹ đầu thanh thước dài \(100\text{ mm}\) gắn trên trục ra và đo được tổng biên độ lắc tự do &lt;strong&gt;\(\Delta x = 2{,}0\text{ mm}\)&lt;/strong&gt;:
\(\Delta\theta_{\text{rad}} = \frac{\Delta x}{L} = \frac{2{,}0}{100} = 0{,}020\text{ rad} \implies \Delta\theta_{\text{độ}} = 0{,}020 \times \frac{180^\circ}{\pi} \approx 1{,}15^\circ.\)
Ghi ngay con số \(G = 15{:}1\) và backlash \(1{,}15^\circ\) vào sổ lab: đó là cơ sở định lượng để bạn biết odometry có thể lệch tối đa bao nhiêu mỗi lần xe đảo chiều!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, các bạn có một giá trị tỉ số truyền đã được dự đoán và đo kiểm thực tế, một ước lượng độ rơ (backlash) tại bánh xe theo độ, và một danh sách kiểm tra lắp ráp (checklist) hoàn chỉnh để thực hiện trước khi cấp điện ở Chương 07. Các bạn phân biệt rõ tác dụng của bánh trung gian đơn với tầng đổi tỉ số, đồng thời biết nhân tỉ số hộp giảm tốc với tỉ số bộ truyền đai ngoài (nếu có).&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Kiến thức từ Bài 01 đến Bài 07: \(b\), \(r\), quy cách ốc M3, định nghĩa tỉ số truyền \(i = N_{\text{out}}/N_{\text{in}} = \omega_{\text{in}}/\omega_{\text{out}}\) và kỹ thuật kiểm tra giảm lực kéo dây. Dụng cụ chỉ cần kim chỉ cắt từ bìa cứng và thước đo góc (hoặc thước đo góc in trên giấy).&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Chương 07 sẽ là lần đầu tiên khung robot được cấp điện áp. Checklist trong bài này đóng vai trò chốt chặn an toàn: một bánh xe cọ khung, khối pin lỏng hay vít cố định trục đặt sai vị trí sẽ làm hỏng buổi thực hành cấp nguồn bằng đúng các dạng lỗi đã phân tích ở Bài 07.&lt;/p&gt;

&lt;p&gt;Tỉ số truyền bạn đo hôm nay chính là tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; dùng để quy đổi xung thành mét. Với bánh răng nhựa đúc, sai số đo nằm trong phạm vi 10% so với thông số nhãn là chấp nhận được. Nếu bộ kit chưa về kịp, phần tính dự đoán trên bản vẽ là sản phẩm nộp (ghi rõ nhãn “bản vẽ”); tuyệt đối không tự bịa số đo góc thực tế.&lt;/p&gt;

&lt;h2 id=&quot;các-bạn-đang-kiểm-gì&quot;&gt;Các bạn đang kiểm gì&lt;/h2&gt;

&lt;p&gt;Trước hết, đếm số răng nếu nhìn thấy hoặc đọc tỉ số đóng dấu trên vỏ hộp giảm tốc — đây là giá trị dự đoán. Bước kiểm chứng được thực hiện theo phương pháp động học: gắn một kim chỉ lên trục đầu vào và một kim chỉ lên trục đầu ra, xoay trục vào một số vòng đã biết rồi đo góc quay đầu ra. Với tỉ số \(i\), ba vòng quay của trục đầu vào phải tạo ra góc đầu ra:&lt;/p&gt;

\[\theta_{\text{out}} = \frac{3}{i} \times 360^\circ.\]

&lt;p&gt;Tiếp theo, giữ cố định trục đầu vào và lắc nhẹ bánh xe. Góc bánh dịch chuyển trước khi kim đầu vào bắt đầu nhúc nhích chính là độ rơ (backlash). Chỉ cần ước lượng bằng mắt theo độ: vài độ là bình thường ở hộp số nhựa đúc, nhưng nếu lên tới hàng chục độ thì nguyên nhân là vít cố định trục lỏng hoặc đai nhảy răng — đây là lỗi cơ khí cần sửa ngay.&lt;/p&gt;

&lt;p&gt;Lưu ý rằng một bánh trung gian đơn chỉ đảo chiều quay chứ không làm thay đổi tỉ số \(i\); chỉ tính nó là một cặp ăn khớp khi xác định dấu chiều quay (trừ khi đó là bánh răng kép gồm hai vòng răng khác số răng gắn cố định trên cùng trục). Nếu có thêm bộ truyền đai bên ngoài hộp số, tỉ số tổng tại bánh xe là tích của hai tầng — bỏ sót hệ số 2 ở bộ đai là lỗi phổ biến hơn cả việc đếm nhầm số răng.&lt;/p&gt;

&lt;p&gt;Sau khi đo tỉ số, hãy kiểm tra toàn bộ checklist lắp ráp khi pin vẫn đặt rời trên bàn (chưa cắm giắc): ốc vít đã siết chặt; vít cố định trục tỳ đúng mặt vát; bánh xe không cọ tấm khung; bánh caster xoay trơn và có khoảng đuôi đúng hướng; dây có vòng dự phòng và mối hàn không chịu lực uốn; pin không bị trượt khỏi vị trí; cả hai động cơ khi xoay tay có lực cản tương đương nhau. Nếu một động cơ có cảm giác sượng hoặc nặng tay hơn hẳn bên còn lại, hãy ghi nhận và xử lý trước Chương 07 chứ không cố bù bằng cách tăng PWM.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/gear_train_ratio.png&quot; alt=&quot;Gọi tên đầu vào trước khi chia: hình là dự đoán, hai kim là phép kiểm&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Xét động cơ TT đóng dấu tỉ số 1:48. Khi trục đầu vào quay 3 vòng, góc đầu ra dự đoán là:&lt;/p&gt;

\[\theta_{\text{out}} = \frac{3}{48} \times 360^\circ = 22.5^\circ.\]

&lt;p&gt;Giả sử bạn đo được \(24^\circ\) tại kim gắn trên bánh xe. Sai số tương đối là:&lt;/p&gt;

\[\frac{24 - 22.5}{22.5} \approx 0.067,\]

&lt;p&gt;tương đương khoảng 7%, nằm trong ngưỡng cho phép 10% đối với hộp số nhựa đúc và kim chỉ thủ công. Bạn ghi vào sổ: \(i = 48\) và “đã kiểm, 24° so với dự đoán 22,5°”. Nếu góc đo rơi vào khoảng \(45^\circ\), có thể bạn đã bỏ quên hoặc nhân nhầm một tầng tỉ số 2:1; nếu gần \(67.5^\circ\), có thể hộp số thực tế là \(i = 16\) hoặc bạn đã đếm nhầm số vòng trục vào. Độ rơ ước lượng khi lắc bánh (ví dụ \(5^\circ\)) được ghi bên cạnh tỉ số, không cố khử bằng cách ép chặt cặp bánh răng gây kẹt.&lt;/p&gt;

&lt;p&gt;Về chiều quay: một cặp ăn khớp ngoài (hoặc một số lẻ cặp ăn khớp ngoài) làm đầu ra quay ngược chiều đầu vào. Quy ước dấu firmware ở Bài 05 phải khớp với quan sát thực tế này bằng cách xoay trục bằng tay khi tắt nguồn.&lt;/p&gt;

&lt;h2 id=&quot;lab-hai-kim-rồi-danh-sách&quot;&gt;Lab: hai kim, rồi danh sách&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Nguồn điện luôn tắt; pin đã ngắt kết nối và đặt xa vị trí thao tác để tránh vô tình chạm cực. Cẩn thận tránh kẹp tay vào bánh răng; hãy xoay trục thông qua kim chỉ hoặc phần đầu trục thay vì thò ngón tay vào vùng ăn khớp. Nếu trục bị cứng không quay được, dừng lại kiểm tra chỗ kẹt thay vì cấp điện để “phá kẹt”.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ giảm tốc hoặc một bản vẽ bánh răng đúng tỉ lệ&lt;/td&gt;
      &lt;td&gt;Tỉ số&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hai kim: cờ băng dính hoặc mũi tên bìa&lt;/td&gt;
      &lt;td&gt;Đầu vào và đầu ra&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thước đo góc, hoặc một thước in&lt;/td&gt;
      &lt;td&gt;Góc đầu ra và độ rơ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lục giác cho vít cố định trục&lt;/td&gt;
      &lt;td&gt;Mặt vát&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dây rút&lt;/td&gt;
      &lt;td&gt;Vòng dự phòng, nếu dây đã có trên động cơ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Dự đoán, phép đo, checklist&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Ghi giá trị dự đoán (từ số răng đếm được hoặc dấu đóng trên vỏ hộp số) và tính \(\theta_{\text{out}}\) cho đúng ba vòng quay của trục đầu vào.&lt;/li&gt;
  &lt;li&gt;Gắn một kim chỉ lên trục đầu vào và một kim chỉ lên trục đầu ra, đồng thời đánh dấu mốc \(0^\circ\) trên mặt bàn.&lt;/li&gt;
  &lt;li&gt;Xoay trục đầu vào đúng 3 vòng, đo góc quay của kim đầu ra, so sánh với dự đoán và tính phần trăm sai lệch.&lt;/li&gt;
  &lt;li&gt;Giữ chặt trục đầu vào, lắc nhẹ trục đầu ra và ước lượng độ rơ theo độ tại bánh xe.&lt;/li&gt;
  &lt;li&gt;Kiểm tra và đánh dấu từng mục trong checklist lắp ráp vào sổ: ốc chặt; vít cố định trục trên mặt vát; bánh không cọ khung; caster xoay trơn và có khoảng đuôi; dây có vòng dự phòng và mối hàn không chịu uốn; pin không trượt; hai động cơ xoay tay có lực cản tương đương.&lt;/li&gt;
  &lt;li&gt;Nếu mới chỉ có bản vẽ, hoàn thành bước 1 cùng các phác thảo, ghi chú “bản vẽ, kết quả trên giấy là sản phẩm nộp” và chuẩn bị sẵn checklist để chạy ngay khi có phần cứng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Một tỉ số truyền có kết quả đo nằm trong ngưỡng 10% so với nhãn (khi có phần cứng) hoặc bản tính dự đoán trên giấy được dán nhãn rõ ràng (khi chưa có phần cứng), kèm giá trị độ rơ tính theo độ. Mỗi dòng trong checklist được đánh dấu “đạt”, “không đạt” hoặc “chưa có trên bàn”; mọi dòng “không đạt” phải ghi rõ biện pháp khắc phục trước khi cấp điện.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Các bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường nghĩa là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Góc là nghịch đảo của dự đoán&lt;/td&gt;
      &lt;td&gt;Tên đầu vào và đầu ra bị đảo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Góc bằng một nửa hoặc gấp đôi&lt;/td&gt;
      &lt;td&gt;Hộp giảm tốc và đai ngoài chưa được nhân, hoặc một trong hai bị áp hai lần&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chiều sai, tỉ số đúng&lt;/td&gt;
      &lt;td&gt;Một bánh trung gian bị tính như đổi tỉ số, hoặc một cặp ăn bị bỏ khỏi phép đếm dấu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đầu ra oặt hàng chục độ&lt;/td&gt;
      &lt;td&gt;Vít cố định trục, moay-ơ mòn răng, hoặc đai nhảy răng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Một động cơ cứng hơn nhiều khi xoay tay&lt;/td&gt;
      &lt;td&gt;Chỗ cà, hoặc hộp số hỏng; đừng cấp điện để xác nhận&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Chỉ đặt mua những linh kiện mà checklist chỉ ra còn thiếu. Bạn có thể dùng ứng dụng thước đo góc trên điện thoại nếu ngắm chuẩn dọc theo kim chỉ. Nên chọn mua động cơ TT có in rõ tỉ số trên vỏ để bước 1 không phải đoán.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mua gì&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ giảm tốc TT&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ giảm tốc TT encoder&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;45.000–120.000 mỗi cái&lt;/td&gt;
      &lt;td&gt;Dấu đóng là dự đoán&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ ốc M3, nếu lần phân loại ở Bài 02 bị thiếu&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ốc M3&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;25.000–70.000&lt;/td&gt;
      &lt;td&gt;Dòng ốc trên checklist&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh caster, nếu mũi vẫn cày&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh caster robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;12.000–40.000&lt;/td&gt;
      &lt;td&gt;Phải có đuôi&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Các trang tìm mua:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=%C4%91%E1%BB%99ng+c%C6%A1+gi%E1%BA%A3m+t%E1%BB%91c+TT&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=%C4%91%E1%BB%99ng%20c%C6%A1%20gi%E1%BA%A3m%20t%E1%BB%91c%20TT%20encoder&quot;&gt;Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=%E1%BB%91c%20M3&quot;&gt;Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=%E1%BB%91c%20M3&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Giá bán có thể thay đổi theo thời điểm.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=NmUq2TQ8m2M&quot;&gt;Compound Gears Explained:  Calculate Gear Ratio&lt;/a&gt; — INTEGRAL PHYSICS, tiếng Anh. Đếm số răng, tính tỉ số truyền rồi quay trục vào để đối chiếu — đúng quy trình thực hành của bài này.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=8fYwUV6QV8c&quot;&gt;How to measure Gear Backlash and Center Distance&lt;/a&gt; — Evolvent Design, tiếng Anh. Đo khe hở độ rơ trước khi vội kết luận tỉ số truyền bị sai.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Hộp giảm tốc đóng dấu tỉ số 1:90. Dự đoán góc quay đầu ra sau ba vòng quay của trục đầu vào.&lt;/li&gt;
  &lt;li&gt;Trên một động cơ đóng dấu 1:48, bạn đo được góc đầu ra \(12^\circ\) sau ba vòng trục vào. Kết quả này có nằm trong ngưỡng 10% so với \(22.5^\circ\) không? Bạn cần kiểm tra những gì tiếp theo?&lt;/li&gt;
  &lt;li&gt;Bánh dẫn 20 răng kéo bánh trung gian 20 răng, sau đó kéo bánh ra 40 răng. Tính tỉ số truyền \(i\) và cho biết chiều quay đầu ra so với bánh dẫn.&lt;/li&gt;
  &lt;li&gt;Một hộp giảm tốc 1:48 nối tiếp với bộ đai GT2 gồm pulley động cơ 20 răng và pulley bánh xe 40 răng. Tính tỉ số tổng \(i\) tại bánh xe và góc \(\theta_{\text{out}}\) sau ba vòng quay động cơ.&lt;/li&gt;
  &lt;li&gt;Liệt kê 7 mục trong checklist lắp ráp và đánh dấu sao hai mục mà nếu bỏ qua sẽ trực tiếp gây ra sai số odometry ảo rõ rệt nhất.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(\theta_{\text{out}} = \frac{3}{90} \times 360^\circ = 12^\circ\).&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;Sai số tương đối $$&lt;/td&gt;
          &lt;td&gt;12 - 22.5&lt;/td&gt;
          &lt;td&gt;/22.5 \approx 47\%$$, vượt xa ngưỡng 10%. Cần kiểm tra lại: đã xoay đúng 3 vòng trục vào chưa, nhãn trên vỏ có thực sự là 1:48 không, và có tầng đai hoặc tầng giảm tốc nào bị bỏ sót hoặc tính nhầm hay không.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;\(i = 40/20 = 2\) (bánh trung gian đơn không đổi tỉ số). Có hai cặp ăn khớp ngoài nên chiều quay đảo hai lần, đầu ra quay cùng chiều bánh dẫn.&lt;/li&gt;
  &lt;li&gt;\(i = 48 \times (40/20) = 96\). Sau 3 vòng động cơ, góc quay bánh xe là \(\theta_{\text{out}} = \frac{3}{96} \times 360^\circ = 11.25^\circ\).&lt;/li&gt;
  &lt;li&gt;Bảy mục: ốc chặt; vít cố định trục trên mặt vát; bánh không cọ khung; caster xoay trơn và có khoảng đuôi; dây có vòng dự phòng và mối hàn không chịu uốn; pin không trượt; lực cản xoay tay hai động cơ tương đương. Hai mục nên đánh dấu sao là vít cố định trục và bánh cọ khung, vì cả hai đều làm encoder đếm xung khác với chuyển động thực trên sàn.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Gear_ratio&quot;&gt;Gear ratio&lt;/a&gt; — nguyên lý chuỗi bánh răng kép, áp dụng tương tự cho trường hợp hộp giảm tốc nối tiếp bộ đai.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.sdp-si.com/resources/elements-of-metric-gear-technology/index.php&quot;&gt;Công nghệ bánh răng SDP-SI&lt;/a&gt; — giải thích khái niệm độ rơ như một khe hở thiết kế có chủ đích.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ros-controls/ros2_controllers/tree/master/diff_drive_controller&quot;&gt;diff_drive_controller&lt;/a&gt; — nơi nhập các tham số \(i\), \(b\) và \(r\) đo được vào hệ thống ROS.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Hỏng hóc cơ khí và chống kéo dây</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_07_mech_failure_modes/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_07_mech_failure_modes</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;60–80 phút&lt;/strong&gt;, bao gồm một lần kiểm tra lực kéo khi đã ngắt nguồn và thực hành làm điểm giảm lực kéo dây (strain relief) có chụp ảnh trước và sau.&lt;/p&gt;

&lt;p&gt;Trong các cuộc thi robotics sinh viên, tỷ lệ robot dừng cuộc chơi vì &lt;em&gt;lỗi thuật toán&lt;/em&gt; thực ra thấp hơn nhiều so với tỷ lệ dừng vì &lt;em&gt;lỗi cơ – điện cơ bản&lt;/em&gt;: một đầu dây nguồn bị đứt ngầm sát mối hàn sau 50 lần gập mở, một bánh xe tự văng ra vì thiếu keo khóa ren, hay một dây tín hiệu bị kéo căng làm tuột giắc cắm giữa chừng. Nhận diện sớm các &lt;strong&gt;chế độ hỏng hóc cơ khí (failure modes)&lt;/strong&gt; và thiết kế &lt;strong&gt;chống kéo dây (strain relief)&lt;/strong&gt; giúp robot của bạn sống sót qua hàng trăm giờ chạy thử thay vì hỏng đúng hôm chấm đồ án.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-mỏi-kim-loại-sụt-áp-dây-dẫn-và-quản-lý-bó-dây-harnessing&quot;&gt;Kiến thức cần nhớ: Mỏi kim loại, Sụt áp dây dẫn và Quản lý bó dây (Harnessing)&lt;/h2&gt;

&lt;h3 id=&quot;1-đứt-ngầm-do-mỏi-uốn-fatigue-fracture-và-chống-kéo-dây-strain-relief&quot;&gt;1. Đứt ngầm do mỏi uốn (Fatigue Fracture) và Chống kéo dây (Strain Relief)&lt;/h3&gt;
&lt;p&gt;Đồng mềm chịu kéo thẳng khá tốt, nhưng nếu bị uốn đi uốn lại ngay tại đúng ranh giới cứng của &lt;strong&gt;mối hàn thiếc&lt;/strong&gt; hoặc đầu chân giắc cắm, dây sẽ đứt gãy do mỏi chỉ sau vài chục chu kỳ. &lt;strong&gt;Strain relief&lt;/strong&gt; (dùng dây rút cố định bó dây vào khung &lt;em&gt;trước&lt;/em&gt; khi đi vào bo mạch, dùng ống co nhiệt bọc chân mối hàn, và giữ bán kính uốn tối thiểu \(R_{\min} \approx 8\text{–}10 \times D_{\text{dây}}\)) chuyển lực giật cơ học vào khung xe thay vì vào mối hàn mỏng manh.&lt;/p&gt;

&lt;h3 id=&quot;2-chống-lỏng-cơ-khí-và-tập-trung-ứng-suất-tại-góc-nhọn&quot;&gt;2. Chống lỏng cơ khí và tập trung ứng suất tại góc nhọn&lt;/h3&gt;
&lt;p&gt;Các vết nứt trên tấm mica hay chi tiết in 3D gần như luôn bắt đầu từ một &lt;strong&gt;góc vuông \(90^\circ\) sắc nhọn&lt;/strong&gt; nơi ứng suất tập trung gấp nhiều lần. Bo tròn góc trong (fillet bán kính \(2\text{–}3\text{ mm}\)) và không khoan lỗ ốc quá sát mép tấm (giữ khoảng cách mép \(\ge 1{,}5\times\) đường kính lỗ) ngăn nứt vỡ hiệu quả.&lt;/p&gt;

&lt;h3 id=&quot;3-chọn-sai-tiết-diện-dây-awg--nguyên-nhân-ngầm-gây-sụt-áp-và-reset-vi-điều-khiển&quot;&gt;3. Chọn sai tiết diện dây (AWG) — nguyên nhân ngầm gây sụt áp và reset vi điều khiển&lt;/h3&gt;
&lt;p&gt;Dây jumper breadboard (26–28 AWG) có điện trở rất lớn, chỉ chịu được vài trăm mA. Dùng dây jumper mảnh để cấp dòng \(3\text{ A}\) cho động cơ không chỉ gây nóng chảy vỏ nhựa mà còn làm sụt áp mạnh trên đường dây khiến mạch điều khiển reset bất thường!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-sụt-áp-dây-nguồn-24-awg-vs-18-awg-và-bán-kính-uốn-bó-dây&quot;&gt;Giải thích: Cầm tay tính sụt áp dây nguồn 24 AWG vs. 18 AWG và bán kính uốn bó dây&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — So sánh sụt áp trên dây nguồn tổng dài \(0{,}5\text{ m}\) (tổng khứ hồi dây dương + dây mass \(L = 1{,}0\text{ m}\)) khi robot kéo dòng \(I = 3{,}0\text{ A}\):&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Dùng dây mảnh 24 AWG&lt;/strong&gt; (điện trở \(r \approx 0{,}084\,\Omega/\text{m}\)):
\(\Delta V_{24} = I \cdot R = 3{,}0 \times 0{,}084 = 0{,}252\text{ V}, \qquad P_{\text{nóng}} = I^2 R = 9 \times 0{,}084 \approx 0{,}76\text{ W}.\)
&lt;em&gt;(Nếu là dây jumper 28 AWG với \(r \approx 0{,}213\,\Omega/\text{m}\), sụt áp vọt lên &lt;strong&gt;\(0{,}64\text{ V}\)&lt;/strong&gt; và dây nóng &lt;strong&gt;\(1{,}92\text{ W}\)&lt;/strong&gt;!)&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dùng dây nguồn chuẩn 18 AWG&lt;/strong&gt; (điện trở \(r \approx 0{,}021\,\Omega/\text{m}\)):
\(\Delta V_{18} = 3{,}0 \times 0{,}021 = 0{,}063\text{ V}, \qquad P_{\text{nóng}} = 9 \times 0{,}021 \approx 0{,}19\text{ W}\)
— giảm sụt áp và nhiệt tổn hao tới &lt;strong&gt;4 lần&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Kiểm tra bán kính uốn an toàn cho bó dây động (trên khớp quay/nắp mở):&lt;/strong&gt;
Với một bó dây bọc lưới đường kính ngoài \(D = 4\text{ mm}\), bán kính uốn động tối thiểu theo quy tắc \(8\text{–}10\times D\) là:
\(R_{\min} = 8 \times 4\text{ mm} = 32\text{ mm}\;(\text{đến } 40\text{ mm}).\)
Bẻ gập bó dây thành góc nhọn bán kính \(5\text{ mm}\) sẽ làm gãy lõi đồng bên trong sau vài chục lần mở nắp bảo trì!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, các bạn nhận diện được bốn dạng hỏng cơ khí phổ biến: bánh xe trượt kêu tách trong khi encoder vẫn đếm xung, càng hoặc moay-ơ nhựa mòn răng, mối hàn đứt ngầm do khung rung uốn và bánh xe cọ khung gây mùi khét do động cơ quá tải. Các bạn biết lắp điểm giảm lực kéo dây (strain relief) kết hợp vòng dây dự phòng để bảo vệ mối hàn, đồng thời soạn hai mục kiểm tra bắt buộc trước khi chạy Capstone A.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Kiến thức từ Bài 01 đến Bài 06: \(b\) và \(r\), cách siết vít cố định trục lên mặt vát chữ D, tỉ số truyền, khoảng đuôi bánh caster và tính chất vật liệu khung. Chương 05 đã cung cấp khái niệm dòng kẹt của động cơ; trong bài thực hành này, nguồn luôn tắt hoàn toàn.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;ROS tính toán quỹ đạo odometry mượt mà dựa vào xung encoder kết hợp các tham số \(i\), \(b\), \(r\). Tuy nhiên, nhiều hỏng hóc cơ khí vẫn cho ra tín hiệu xung hoàn hảo trong khi chuyển động thực trên sàn bị sai lệch: vít cố định trục trượt khỏi mặt vát, moay-ơ mòn răng, bánh xe cọ khung hay bánh caster mất tải. Nhìn trên đồ thị topic, chúng dễ bị nhầm là lỗi cấu hình tham số, nhưng bản chất lại là lỗi cơ khí phải xử lý bằng tay khi đã ngắt pin trước khi tinh chỉnh phần mềm.&lt;/p&gt;

&lt;p&gt;Tương tự, thiếu giảm lực kéo dây thường chỉ bộc lộ sau một thời gian chạy thử: mỗi cú xóc làm dây uốn ngay tại mối hàn, dẫn đến mỏi đồng và đứt ngầm khiến lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; vẫn gửi bình thường nhưng động cơ đã mất điện.&lt;/p&gt;

&lt;h2 id=&quot;những-hỏng-tìm-được-bằng-tay&quot;&gt;Những hỏng tìm được bằng tay&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Vít cố định trục trượt khỏi mặt vát chữ D:&lt;/strong&gt; Moay-ơ phát tiếng tách và bánh xe khựng lại, nhưng trục động cơ cùng encoder vẫn quay bình thường. Odometry vẫn cộng dồn quãng đường \(r \Delta\theta\) như thể bánh đang lăn, tạo ra sai số odometry ảo kinh điển. Dù đã lắp đúng ở Bài 02, bạn vẫn cần kiểm tra lại sau mỗi lần vận chuyển vì vít tỳ lệch sang phần tròn sẽ lỏng dần theo thời gian.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Càng servo hoặc moay-ơ nhựa bị mòn răng:&lt;/strong&gt; Các chi tiết nhựa mềm giá rẻ dễ bị trục kim loại làm mòn tròn rãnh răng. Khi răng đã mất, ốc giữ vẫn có cảm giác chặt nhưng trục quay tự do bên trong lỗ. Giải pháp duy nhất là thay càng hoặc moay-ơ mới; bôi keo lên răng đã mòn chỉ mang tính đối phó tạm thời và sẽ sớm hỏng lại.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mỏi dây tại mối hàn:&lt;/strong&gt; Khi khung xe rung hoặc uốn nhẹ, mối hàn là điểm cứng cố định nên sợi đồng bị gãy mỏi bên trong lớp vỏ cách điện ngay sát chân mối hàn (cách vài milimét), dù nhìn bề ngoài dây vẫn nguyên vẹn. Giảm lực kéo dây (strain relief) giúp chuyển vùng uốn ra khỏi mối hàn: dùng dây rút nhựa hoặc một giọt keo nến cố định dây vào tấm khung để mối hàn không còn chịu lực kéo. Phía trước điểm cố định, luôn chừa một vòng dây dự phòng (độ chùng có chủ đích) để dây có khoảng nhường khi cụm linh kiện rung lắc. Áp dụng cách này cho cả dây nguồn động cơ và dây tín hiệu encoder — dây encoder đứt ngầm sẽ trông giống hệt cảm biến hỏng trong firmware Chương 04.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bánh xe cọ khung:&lt;/strong&gt; Mặt lốp chạm vào tấm khung hoặc đầu ốc lồi ra khiến tốc độ giảm mạnh, dòng điện tăng tiến sát dòng kẹt (Chương 05) và động cơ nhanh chóng có mùi khét. Bạn dễ dàng phát hiện lỗi này khi ngắt pin và xoay bánh bằng tay: mỗi vòng quay sẽ nghe tiếng cọ sột soạt. Hãy dịch bánh ra ngoài hoặc xử lý phần vật cản trước khi nghi ngờ lỗi mạch cầu H.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bánh caster rung lắc ở tốc độ cao:&lt;/strong&gt; Đầu xe dao động trái phải do khoảng đuôi (trail) bị lắp ngược, quá ngắn, hoặc trọng tâm (Bài 01) phân bổ quá ít lực đè lên bánh caster khiến bánh không ổn định hướng. Lỗi này chỉ khắc phục được bằng cách chỉnh lại cơ cấu caster và trọng tâm, không thể bù bằng bộ lọc phần mềm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pin bị trượt:&lt;/strong&gt; Khối pin lỏng lẻo sẽ dịch chuyển trọng tâm ra ngoài tam giác đỡ, làm bánh caster nhấc bổng hoặc khiến một bánh chủ động mất độ bám và xe xoay ngang. Hãy dùng đai in 3D hoặc dây rút cố định chắc chắn pin đúng vị trí đã đo \(b\) ở Bài 01.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;Những vùng khung mà các dạng hỏng này sống: bánh, đường trục, caster, và dây chạy dọc tấm&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy dùng sơ đồ trên như bản đồ kiểm tra thực tế: chạm và kiểm tra lần lượt cả hai moay-ơ, khe hở hai bánh xe, càng caster, vị trí đặt pin và mọi đoạn dây đi ra từ mối hàn.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Giả sử encoder ghi nhận đủ một vòng quay tại trục bánh (sau khi chia tỉ số truyền), nhưng vít cố định trục bị trượt trọn vòng đó. Với bán kính lăn \(r = 31.2\) mm, quãng đường ảo sinh ra là:&lt;/p&gt;

\[s_{\text{phantom}} = 2\pi r = 2\pi \times 31.2 \approx 196\ \text{mm}.\]

&lt;p&gt;Bản đồ sẽ ghi thêm gần 20 cm mà xe thực tế chưa hề di chuyển. Chỉ cần hai lần trượt như vậy trên mỗi mét hành trình là tạo ra sai số 40 cm — mức sai lệch mà việc chỉnh hiệp phương sai (covariance) không thể che giấu trung thực. Giải pháp bắt buộc là kiểm tra cơ khí: đưa vít đúng mặt vát, vặn thử bánh và chụp ảnh xác nhận.&lt;/p&gt;

&lt;p&gt;Bánh cọ khung cũng gây ra rủi ro nhiệt tương tự: thay vì chạy ở dòng không tải nhỏ (Chương 05), ma sát kéo điểm làm việc lên gần dòng kẹt làm cuộn dây nóng nhanh. Không cần tính thêm thông số mới; yêu cầu tiên quyết là loại bỏ hoàn toàn tiếng cọ trước khi cấp lại điện áp.&lt;/p&gt;

&lt;h2 id=&quot;lab-giật-thử-và-một-điểm-neo&quot;&gt;Lab: giật thử và một điểm neo&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Ngắt pin, rút nguồn mạch cầu H và rút cáp USB nếu mạch điều khiển đang gắn trên khung. Mọi thao tác kéo kiểm tra đều thực hiện bằng tay khi không có điện. Keo nến là nguồn nhiệt duy nhất trong bài: giữ đầu súng bắn keo cách xa lớp cách điện dây và tay người thao tác. Không cấp điện cho động cơ rồi giữ bánh để thử độ bền mối dây.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung, hoặc một dây động cơ trên bàn nếu tấm chưa về&lt;/td&gt;
      &lt;td&gt;Các mối&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dây rút, hoặc một cây keo nến và súng&lt;/td&gt;
      &lt;td&gt;Một điểm giảm lực kéo dây&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điện thoại&lt;/td&gt;
      &lt;td&gt;Trước và sau&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Hai mục kiểm ngày Capstone&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Khi nguồn đã tắt, kéo nhẹ từng đầu giắc bằng cách cầm vào phần vỏ nhựa của giắc (không nắm lõi dây). Nếu giắc lỏng, cắm chặt lại và có thể chấm một giọt keo nến nhỏ cố định vỏ giắc nếu kết cấu cho phép.&lt;/li&gt;
  &lt;li&gt;Quan sát và thử uốn nhẹ từng dây gần mối hàn; nếu mối hàn bị dịch chuyển hoặc chịu lực uốn trực tiếp, vị trí đó đang thiếu giảm lực kéo dây.&lt;/li&gt;
  &lt;li&gt;Chụp ảnh vị trí mối dây dễ hỏng nhất. Gắn thêm dây rút hoặc điểm neo keo nến sao cho đoạn dây bắt đầu uốn nằm cách mối hàn ít nhất 1 cm, đồng thời chừa một vòng dây chùng dự phòng. Chụp ảnh sau khi hoàn thiện.&lt;/li&gt;
  &lt;li&gt;Dùng tay xoay và vặn thử từng bánh xe để xác nhận vít cố định trục bám chắc và lốp không cọ vào khung hay đầu ốc.&lt;/li&gt;
  &lt;li&gt;Thử đẩy nhẹ khối pin; nếu pin có thể xê dịch, ghi chú cần bổ sung đai giữ pin.&lt;/li&gt;
  &lt;li&gt;Viết hai mục kiểm tra cơ khí sẽ thực hiện vào ngày chạy Capstone theo đúng thứ tự trước khi bất kỳ ai gửi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Một cặp ảnh trước và sau của mối dây đã được làm giảm lực kéo dây, kèm hai mục kiểm tra cụ thể trong sổ tay (ví dụ: kiểm tra vít cố định trục trên mặt vát và kiểm tra không có tiếng cọ khi xoay bánh bằng tay). Khi lắc nhẹ vòng dây dự phòng, mối hàn phải đứng yên hoàn toàn.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Các bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường nghĩa là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh kêu tách, trục không theo&lt;/td&gt;
      &lt;td&gt;Vít cố định trục trượt khỏi mặt vát, hoặc moay-ơ mòn răng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sợi đứt ngay cạnh một mối nhìn vẫn đẹp&lt;/td&gt;
      &lt;td&gt;Mỏi; thêm giảm lực kéo dây trên mối thay thế&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Một tiếng cạo mỗi vòng, và trí nhớ về mùi động cơ ở Chương 05&lt;/td&gt;
      &lt;td&gt;Bánh cà&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Caster đập trái phải&lt;/td&gt;
      &lt;td&gt;Khoảng đuôi hoặc trọng tâm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin dịch khi nghiêng tấm&lt;/td&gt;
      &lt;td&gt;Thiếu đai; caster sẽ mất tải ở lần tăng tốc đầu&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Một túi dây rút nhựa và keo nến hữu ích hơn nhiều so với việc mua động cơ mới. Chỉ cần mua thêm bánh xe hoặc moay-ơ dự phòng nếu chi tiết hiện tại đã bị mòn tròn lỗ trục.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mua gì&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Dây rút&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây rút nhựa&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;10.000–30.000&lt;/td&gt;
      &lt;td&gt;Giảm lực kéo dây và đai pin tạm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Keo nến&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keo nến&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;Neo dây, giữ keo khỏi tiếp điểm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh TT dự phòng&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh xe cao su 65mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–45.000&lt;/td&gt;
      &lt;td&gt;Nếu moay-ơ đã mòn răng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Các trang tìm mua:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=d%C3%A2y%20r%C3%BAt%20nh%E1%BB%B1a&quot;&gt;Shopee: dây rút&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=keo%20n%E1%BA%BFn&quot;&gt;Lazada: keo nến&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=b%C3%A1nh+xe+cao+su&quot;&gt;Hshop: bánh xe&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=d%C3%A2y%20%C4%91i%E1%BB%87n&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Giá bán có thể thay đổi theo thời điểm. Một đầu giắc tuột ra là lỗi cơ khí – hàn mạch (Chương 01), không phải lỗi phần mềm.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=8fYwUV6QV8c&quot;&gt;How to measure Gear Backlash and Center Distance&lt;/a&gt; — Evolvent Design, tiếng Anh. Hướng dẫn đo độ rơ giữa hai bánh răng — khe hở gây ra vùng trễ khi encoder đã đếm nhưng bánh chưa dịch chuyển.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=8q25EUszBSI&quot;&gt;What is Bearing? Types of Bearings and How they Work?&lt;/a&gt; — The Engineers Post, tiếng Anh. Nhận diện hỏng hóc bạc đạn và lệch trục trước khi cố siết chặt thêm.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Robot di chuyển rất chậm trên sàn trong khi số xung encoder tăng rất nhanh. Nêu tên dạng hỏng và cách kiểm tra bằng tay.&lt;/li&gt;
  &lt;li&gt;Phần răng nhựa bên trong một càng servo đã bị mòn tròn dù ốc bắt vẫn chặt. Cần thay chi tiết nào, và vì sao bôi keo không phải giải pháp đáng tin cậy?&lt;/li&gt;
  &lt;li&gt;Dây động cơ được hàn và kéo căng thẳng tới mạch cầu H, không có độ chùng. Cần bổ sung những gì, và mối hàn phải có trạng thái ra sao khi lắc thử dây?&lt;/li&gt;
  &lt;li&gt;Động cơ kéo dòng cao, quay chậm và có mùi khét, dù vít cố định trục vẫn giữ chắc. Bạn cần tìm lỗi cản cơ khí nào?&lt;/li&gt;
  &lt;li&gt;Viết hai mục kiểm tra cho ngày chạy Capstone thành câu hoàn chỉnh sao cho thành viên khác có thể thực hiện khi pin vẫn đang ngắt.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Bánh xe bị trượt trên trục (vít cố định trục trượt khỏi mặt vát hoặc moay-ơ mòn răng). Kiểm tra bằng cách tắt nguồn rồi dùng tay vặn thử bánh; bánh không được trượt hay kêu tách.&lt;/li&gt;
  &lt;li&gt;Thay càng servo mới. Keo bôi lên phần răng đã tròn sẽ nhanh chóng bong ra ở lần chịu tải tiếp theo.&lt;/li&gt;
  &lt;li&gt;Bổ sung điểm giảm lực kéo dây (dây rút hoặc keo nến neo vào khung) cùng một vòng dây chùng dự phòng. Khi lắc dây, mối hàn phải đứng yên hoàn toàn.&lt;/li&gt;
  &lt;li&gt;Bánh xe cọ vào tấm khung hoặc đầu ốc. Xoay bánh bằng tay khi tắt nguồn để tìm và loại bỏ vị trí cọ sát.&lt;/li&gt;
  &lt;li&gt;Chọn bất kỳ hai mục kiểm tra nào trong bài thực hiện ở trạng thái tắt nguồn (ví dụ: vít cố định trục tỳ đúng mặt vát, xoay bánh không có tiếng cọ, caster có khoảng đuôi và chịu tải ổn định, pin cố định không trượt, mọi mối hàn đã được giảm lực kéo dây), kèm tiêu chí rõ ràng thế nào là đạt.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.adafruit.com/wires-and-connectors/wire&quot;&gt;Adafruit, dây và giắc&lt;/a&gt; — kiến thức về dây dẫn, mối nối và nguyên nhân dây đồng đứt mỏi sát mối hàn.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/connector-basics/all&quot;&gt;SparkFun, căn bản giắc nối&lt;/a&gt; — phân loại giắc nối và quy tắc cầm vỏ giắc khi tháo lắp.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://reprap.org/wiki/Print_Troubleshooting&quot;&gt;Gỡ lỗi in trên RepRap&lt;/a&gt; — phân tích lỗi mòn răng moay-ơ nhựa và nứt lớp in nếu chi tiết hỏng là chi tiết in 3D.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Vật liệu khung: in 3D vs cắt laser</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_06_materials_print_laser/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_06_materials_print_laser</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;70–90 phút&lt;/strong&gt;. Bạn có thể hoàn thành bài thực hành chỉ với một bức ảnh chi tiết in 3D bị gãy; việc có sẵn máy in hay không là tùy chọn.&lt;/p&gt;

&lt;p&gt;Khi bắt đầu chế tạo khung robot trong xưởng lab, hai công cụ chế mẫu nhanh phổ biến nhất nằm ngay trước mắt bạn: &lt;strong&gt;máy in 3D FDM (nhựa PLA/PETG)&lt;/strong&gt; và &lt;strong&gt;máy cắt laser (mica Acrylic / gỗ dán MDF/Plywood)&lt;/strong&gt;. Chọn sai công cụ không chỉ tốn hàng giờ chờ in mà còn tạo ra một bộ khung mềm oặt hoặc giòn gãy ngay cú va chạm đầu tiên. Bí quyết của người làm cơ khí robot là kết hợp cả hai: dùng cắt laser cho &lt;strong&gt;các tấm khung phẳng chịu lực lớn&lt;/strong&gt; và dùng in 3D cho &lt;strong&gt;các gá lắp hình học phức tạp&lt;/strong&gt; (giá đỡ động cơ, ngàm cảm biến, ống dẫn dây).&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-cơ-học-vật-liệu-tính-dị-hướng-của-in-3d-và-độ-cứng-uốn&quot;&gt;Kiến thức cần nhớ: Cơ học vật liệu, Tính dị hướng của in 3D và Độ cứng uốn&lt;/h2&gt;

&lt;h3 id=&quot;1-tính-dị-hướng-anisotropy-của-in-3d-fdm&quot;&gt;1. Tính dị hướng (Anisotropy) của in 3D FDM&lt;/h3&gt;
&lt;p&gt;Chi tiết in 3D được đắp từng lớp nhựa nóng chảy chồng lên nhau theo trục Z. Vì liên kết giữa các lớp yếu hơn liên kết dọc sợi nhựa, một giá đỡ động cơ chịu lực kéo &lt;strong&gt;vuông góc với mặt lớp in&lt;/strong&gt; rất dễ tách lớp (delamination) và gãy đôi! Định hướng chi tiết trên bàn in sao cho phương chịu kéo chính nằm dọc theo mặt phẳng XY là kỹ năng cần có của người thiết kế CAD cho in 3D.&lt;/p&gt;

&lt;h3 id=&quot;2-nhựa-pla-vs-petg-vs-tấm-cắt-laser-acrylic--plywood&quot;&gt;2. Nhựa PLA vs. PETG vs. Tấm cắt Laser (Acrylic / Plywood)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;PLA:&lt;/strong&gt; cứng, dễ in, độ chính xác cao, nhưng giòn và mềm oặt ngay ở \(55\text{–}60^\circ\text{C}\) (gắn trực tiếp lên động cơ nóng sẽ bị biến dạng!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;PETG / ABS:&lt;/strong&gt; dẻo dai hơn, chịu nhiệt tốt hơn (\(75\text{–}95^\circ\text{C}\)), rất hợp làm gá động cơ và chi tiết chịu va đập.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Acrylic (Mica) &amp;amp; Gỗ dán cắt laser:&lt;/strong&gt; cắt xong cả bộ khung 2 tầng chỉ trong &lt;strong&gt;5 phút&lt;/strong&gt; (so với 8 tiếng in 3D!), độ cứng phẳng cực tốt, nhưng Acrylic giòn khi khoan/va chạm mạnh ở góc vuông sắc (luôn bo góc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fillet&lt;/code&gt;!).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-quy-luật-lập-phương-của-độ-cứng-uốn-i--fracb-h312&quot;&gt;3. Quy luật lập phương của độ cứng uốn (\(I = \frac{b h^3}{12}\))&lt;/h3&gt;
&lt;p&gt;Độ chống võng của một tấm khung phẳng tỉ lệ với mô-men quán tính mặt cắt \(I = \frac{b h^3}{12}\) (với \(h\) là chiều dày tấm). Tăng độ dày tấm lên gấp đôi khiến khối lượng tăng &lt;strong&gt;2 lần&lt;/strong&gt;, nhưng độ cứng chống uốn tăng vọt lên tới &lt;strong&gt;\(2^3 = 8\) lần&lt;/strong&gt;!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-so-sánh-độ-cứng-uốn-và-khối-lượng-giữa-tấm-khung-in-3d-và-tấm-cắt-laser&quot;&gt;Giải thích: Cầm tay so sánh độ cứng uốn và khối lượng giữa tấm khung in 3D và tấm cắt laser&lt;/h3&gt;

&lt;p&gt;Xét một tấm sàn khung robot kích thước &lt;strong&gt;\(200\text{ mm} \times 150\text{ mm}\)&lt;/strong&gt; (diện tích \(A = 300\text{ cm}^2\)):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Hiệu ứng tăng độ dày từ \(h_1 = 3\text{ mm}\) lên \(h_2 = 6\text{ mm}\):&lt;/strong&gt;
\(\frac{I_2}{I_1} = \left(\frac{h_2}{h_1}\right)^3 = \left(\frac{6}{3}\right)^3 = 2^3 = 8\times\text{ cứng hơn!}\)
Cùng một tải trọng pin đặt ở giữa sàn, độ võng giảm xuống chỉ còn &lt;strong&gt;1/8&lt;/strong&gt;, trong khi trọng lượng tấm chỉ tăng \(2\times\) (hoặc thậm chí ít hơn nếu ta thêm các gân tăng cứng thay vì làm đặc toàn bộ!).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;So sánh khối lượng tấm dày \(h = 4\text{ mm}\) (thể tích \(V = 300 \times 0{,}4 = 120\text{ cm}^3\)):&lt;/strong&gt;&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Tấm in 3D PLA&lt;/strong&gt; (mật độ nhựa đặc \(1{,}24\text{ g/cm}^3\), in 20% infill + vỏ thành → mật độ hiệu dụng \(\approx 0{,}45\text{ g/cm}^3\)):
\(m_{\text{PLA}} \approx 120 \times 0{,}45 = 54\text{ g}\) (nhẹ, nhưng tốn ~3–4 giờ in).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Tấm Acrylic cắt laser đặc&lt;/strong&gt; (\(\rho = 1{,}18\text{ g/cm}^3\)):
\(m_{\text{Acrylic}} = 120 \times 1{,}18 = 141{,}6\text{ g}\) (nặng hơn ~88 g, nhưng cắt xong trong &lt;strong&gt;2 phút&lt;/strong&gt;, siêu phẳng và cứng!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, các bạn hiểu lý do nên chọn tấm khung mica 2WD mua sẵn cho Capstone A và chỉ dùng in 3D cho các giá đỡ, giá cảm biến và đai giữ pin. Các bạn tính được sự thay đổi kích thước lỗ khi cắt laser với khe cắt (kerf) 0,1–0,2 mm, biết ngưỡng nhiệt độ làm mềm nhựa PLA trong khoang ô tô kín, và biết xoay hướng in giá động cơ sao cho lực siết bu-lông không tách các lớp in ra khỏi nhau.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Các bạn đã nắm hình học khung xe ở Bài 01 và quy cách ốc M3 ở Bài 02. Không cần phần mềm CAD bản quyền; một bản phác thảo bằng bút chì có ghi rõ chiều dày theo milimét là đủ.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Capstone A cần lăn bánh được trong tháng này. In 3D toàn bộ khung xe sẽ tốn nhiều thời gian trên máy in (thậm chí bạn chưa có máy), và khung bằng nhựa PLA dễ bị cong vênh nếu để trong ô tô đỗ dưới nắng trước khi kịp hoàn thiện URDF. Phương án hợp lý nhất là mua sẵn khung mica 2WD, chỉ in 3D những chi tiết bộ kit không có như giá gắn cảm biến siêu âm, đai cố định pin (Bài 07) hoặc nắp che mối hàn.&lt;/p&gt;

&lt;p&gt;ROS không quan tâm vật liệu khung là loại nhựa nào; nó chỉ phụ thuộc vào vị trí chính xác của hai bánh xe. Nếu giá động cơ bị nứt hoặc biến dạng, vị trí bánh thay đổi và khổ vết \(b\) đã đo không còn đúng nữa. Chọn đúng vật liệu và hướng in chính là cách giữ giá trị \(b\) ổn định đúng như tham số trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;tấm-mica-giá-in&quot;&gt;Tấm mica, giá in&lt;/h2&gt;

&lt;p&gt;Mica đúc dày khoảng 3 mm là vật liệu phổ biến nhất cho tấm khung kit. Mica phẳng, cắt laser sắc nét nhưng khá giòn. Nếu siết ốc vào một lỗ quá chật, phần ren sẽ tạo ứng suất nêm và gây nứt lan giữa các lỗ. Do đó, với mỗi ốc M3, hãy dùng lỗ thông và bắt đai ốc nyloc (Bài 02) ở mặt sau. Mica cũng không phù hợp cho các ngàm cài đàn hồi; nếu cần ngàm cài, hãy dùng nhựa PETG hoặc bỏ thiết kế ngàm khỏi Capstone A.&lt;/p&gt;

&lt;p&gt;Khe cắt laser (kerf) là bề rộng phần vật liệu bị tia laser đốt mất, thường vào khoảng 0,1–0,2 mm trên máy cắt laser phổ thông khi cắt mica. Vì xưởng cắt đúng theo đường nét bản vẽ, đường kính lỗ cắt thực tế sẽ lớn hơn đường kính vẽ một lượng xấp xỉ khe cắt:&lt;/p&gt;

\[d_{\text{cut}} \approx d_{\text{drawn}} + k.\]

&lt;p&gt;Nếu vẽ lỗ thông 3,2 mm với khe cắt 0,2 mm, lỗ thực tế đạt khoảng 3,4 mm — ốc M3 đi qua dễ dàng. Ngược lại, nếu vẽ 3,0 mm với ý định để ốc tự tạo ren trong mica, lỗ cắt ra khoảng 3,2 mm khiến ốc bị lỏng; còn cố siết chặt quá tay sẽ làm nứt tấm. Khi gửi xưởng cắt, hãy xuất file DXF theo đơn vị milimét, đảm bảo mọi đường biên đều khép kín và ghi rõ chiều dày tấm vật liệu. Một đường biên bị hở có thể khiến máy cắt bỏ qua hoặc kết thúc đường cắt sai ý muốn.&lt;/p&gt;

&lt;p&gt;Nhựa PLA dễ in nhưng bắt đầu mềm ở khoảng 55–60 °C — mức nhiệt rất dễ đạt tới trong khoang ô tô đỗ ngoài trời vào buổi chiều ở Việt Nam. Một giá đỡ vuông vức buổi sáng có thể bị biến dạng trước giờ học. Nhựa PETG chịu nhiệt tốt hơn, dù khi in dễ tạo sợi mảnh (stringing) cần gọt nhẹ bằng dao sau khi in.&lt;/p&gt;

&lt;p&gt;Với các giá đỡ chịu lực như giá động cơ, yếu tố quyết định độ bền là hướng in. Chi tiết in 3D yếu nhất theo phương vuông góc với mặt phẳng lớp in (liên kết giữa các lớp yếu hơn sợi nhựa liên tục). Nếu lực siết bu-lông hoặc mô-men động cơ tác động theo hướng bóc tách hai lớp in, tai giá đỡ sẽ dễ dàng bị gãy. Hãy xoay mô hình trên bàn in sao cho mặt phẳng chịu lực kéo xé cắt ngang qua các đường lớp — khi đó vết nứt buộc phải bẻ đứt sợi nhựa liên tục thay vì tách dọc đường nối lớp. Một giá đỡ đặt in “trông gọn đẹp” nhưng để phần tai chịu lực xếp chồng song song với mặt nứt sẽ gãy ngay ở lần kẹt bánh đầu tiên.&lt;/p&gt;

&lt;p&gt;Bạn hoàn toàn có thể thực hiện việc này mà không cần sở hữu máy in 3D. Phòng lab hoặc dịch vụ in chỉ cần file STL (đơn vị milimét), tên vật liệu (PLA hoặc PETG) và ghi chú hướng in nếu cần. Tương tự, xưởng cắt laser cần file DXF (milimét), đường vẽ khép kín và chiều dày tấm cụ thể. Ghi đủ các thông số này ngay trong đơn đặt hàng sẽ giúp chi tiết gia công đúng kích thước ngay từ lần đầu.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;Tấm khung các bạn đang quyết định không in lại: đo nó, rồi chỉ in những giá nó còn thiếu&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hình trên là khung xe mua sẵn, nơi \(b\) và \(r\) được cố định bởi độ cứng của tấm mica. Chi tiết in 3D phù hợp để giữ cảm biến hoặc pin, nhưng sẽ tiềm ẩn rủi ro sai lệch trục bánh nếu bạn tự in lại toàn bộ khung ngay sát Chương 07.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Giả sử bạn thiết kế bản vẽ rãnh động cơ có lỗ thông M3 với \(d_{\text{drawn}} = 3.2\) mm và xưởng báo khe cắt \(k = 0.15\) mm:&lt;/p&gt;

\[d_{\text{cut}} \approx 3.2 + 0.15 = 3.35\ \text{mm}.\]

&lt;p&gt;Ốc M3 có đường kính đỉnh ren khoảng 3,0 mm, nên lỗ 3,35 mm đủ rộng để ốc đi qua thoải mái và lực kẹp do đai ốc phía sau đảm nhận. Nếu bạn vẽ 2,8 mm với hy vọng ren ốc sẽ bám trực tiếp vào mica:&lt;/p&gt;

\[d_{\text{cut}} \approx 2.8 + 0.15 = 2.95\ \text{mm},\]

&lt;p&gt;khiến ốc khó lọt qua hoặc tạo lực nêm làm nứt mica. Với đai giữ pin in 3D, mức nhiệt 58 °C trong cabin ô tô đã nằm trong vùng làm mềm của PLA; vì vậy nên chọn nhựa PETG nếu robot có thể phải để trong xe hơi, trong khi tấm khung chính vẫn giữ nguyên là mica mua sẵn.&lt;/p&gt;

&lt;h2 id=&quot;lab-đọc-một-lớp-gãy-phác-hướng-in&quot;&gt;Lab: đọc một lớp gãy, phác hướng in&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Chi tiết nhựa bị gãy thường có cạnh sắc dọc theo lớp tách; hãy cầm vào mặt cạnh nguyên vẹn, tránh chạm vào mảnh vỡ nhỏ. Nếu đến trực tiếp xưởng cắt laser, bàn máy và vật liệu vừa cắt còn nóng, hãy chờ người vận hành lấy chi tiết ra. Bài này không cần máy in hay dung môi; không dùng keo tùy tiện để “hàn” mica khi đang phân tích dạng hỏng.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Một chi tiết in đã gãy, hoặc một ảnh rõ của nó&lt;/td&gt;
      &lt;td&gt;Mổ lớp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bút chì, hình cạnh và hình trước&lt;/td&gt;
      &lt;td&gt;Hướng giá&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Phép khe cắt và câu vật liệu&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Quan sát kỹ mặt gãy của chi tiết in 3D: các đường lớp hiện lên như những sọc mảnh song song. Xác định xem vết gãy chạy dọc theo ranh giới giữa các sọc hay cắt ngang qua chúng. Nếu chạy dọc theo sọc, tải trọng đã tác động đúng vào mặt phẳng yếu nhất.&lt;/li&gt;
  &lt;li&gt;Phác thảo một giá động cơ TT gồm đế bắt vào khung và phần tai dựng lên để bắt động cơ. Đánh dấu trục siết bu-lông, sau đó vẽ hướng các đường lớp sao cho chúng cắt ngang mặt phẳng chịu lực xé. Ghi rõ “in úp mặt này xuống” lên mặt tiếp xúc với bàn in.&lt;/li&gt;
  &lt;li&gt;Tính một ví dụ kích thước lỗ sau cắt laser cho một lỗ trên bản vẽ của bạn (dùng \(k = 0.2\) mm nếu xưởng chưa cung cấp thông số). Ghi rõ đường kính vẽ và đường kính dự kiến sau cắt.&lt;/li&gt;
  &lt;li&gt;Viết một câu chốt phương án vật liệu cho Capstone A: dùng khung mica mua sẵn, ghi rõ tên các chi tiết sẽ in (giá đỡ, đai pin) và vật liệu tương ứng.&lt;/li&gt;
  &lt;li&gt;Nếu không có mẫu hỏng thực tế lẫn ảnh tự chụp, hãy dùng ảnh một chi tiết in bị gãy tìm được trong lab và ghi chú rõ nguồn ảnh.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Một ghi chú chỉ ra mặt phẳng yếu của chi tiết in, một bản phác thảo đánh dấu rõ mặt đặt lên bàn in cùng hướng lớp in, và một phép tính khe cắt (ví dụ vẽ 3,2 mm, cắt ra dự kiến 3,4 mm). Câu chốt vật liệu chỉ định mica cho khung chính và PLA hoặc PETG cho giá đỡ (nêu rõ PETG nếu có yêu cầu chịu nhiệt trong ô tô).&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Các bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường nghĩa là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Vết nứt đi theo các sọc&lt;/td&gt;
      &lt;td&gt;Các lớp chính là mặt xé; xoay chi tiết&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lỗ chật và mica nứt chân chim&lt;/td&gt;
      &lt;td&gt;Lỗ vẽ bỏ qua khe cắt, hoặc ốc bị dùng như một tarô&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Giá vênh sau một quãng nắng&lt;/td&gt;
      &lt;td&gt;PLA trên khoảng 55–60 °C&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Xưởng cắt viền ngoài và bỏ lỗ&lt;/td&gt;
      &lt;td&gt;Đường hở, hoặc lỗ nằm trên một lớp xưởng đã tắt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;STL trả về bé xíu&lt;/td&gt;
      &lt;td&gt;File theo inch, hoặc theo centimét, và xưởng đã theo các con số&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Ưu tiên ngân sách cho bộ khung mica 2WD. Chỉ cần mua cuộn nhựa khi bạn hoặc phòng lab tự in giá đỡ. Cắt laser là dịch vụ gia công tính phí theo file DXF; luôn ghi rõ chiều dày tấm và đơn vị milimét khi gửi file.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mua gì&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung mica 2WD&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khung xe robot 2 bánh&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;80.000–200.000&lt;/td&gt;
      &lt;td&gt;Tấm của Capstone&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nhựa PLA, 1 kg&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nhựa PLA&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;250.000–450.000&lt;/td&gt;
      &lt;td&gt;Dễ in; để xa ô tô kín&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nhựa PETG, 1 kg&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nhựa PETG&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;280.000–500.000&lt;/td&gt;
      &lt;td&gt;Chịu nhiệt hơn, tạo sợi nhiều hơn&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Các trang tìm mua:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=khung+xe+robot&quot;&gt;Hshop: khung&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=nh%E1%BB%B1a%20PLA&quot;&gt;Shopee: nhựa PLA&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=nh%E1%BB%B1a%20PLA&quot;&gt;Lazada: nhựa PLA&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=nh%E1%BB%B1a%20PLA&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Giá bán có thể thay đổi theo thời điểm. Dịch vụ cắt laser thường đặt trực tiếp tại xưởng địa phương hoặc makerspace; hãy chuẩn bị sẵn file DXF (milimét, đường kín) và xác nhận chiều dày mica họ có sẵn (thường là 3 mm).&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=6MA1Lv4YilM&quot;&gt;Laser, CNC, or 3D Printer Which One Is RIGHT For You?&lt;/a&gt; — Andy Bird Builds, tiếng Anh. So sánh mục đích sử dụng giữa in 3D, cắt laser và CNC để chọn công nghệ theo yêu cầu lực và độ phẳng thay vì chọn theo máy sẵn có.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một lỗ được vẽ với đường kính 3,4 mm, khe cắt laser là 0,2 mm. Đường kính dự kiến trên tấm mica là bao nhiêu và ốc M3 có đi qua được không?&lt;/li&gt;
  &lt;li&gt;Bạn cần bắt ốc chắc chắn vào chi tiết nhựa in 3D. Chi tiết phần cứng nào ở Bài 02 thay thế cho việc để ốc tự cắt ren trực tiếp vào nhựa PLA?&lt;/li&gt;
  &lt;li&gt;Tai giá động cơ bị nứt gãy phẳng dọc theo các đường sọc lớp in ngay lần đầu bánh bị kẹt. Mặt phẳng yếu nằm ở đâu và cần xoay bản in lại thế nào?&lt;/li&gt;
  &lt;li&gt;Robot đặt trong ô tô nóng khoảng 58 °C. Loại nhựa in nào có nguy cơ biến dạng đai giữ pin, và nên đổi sang loại nhựa nào?&lt;/li&gt;
  &lt;li&gt;Liệt kê định dạng file, đơn vị và thông tin bắt buộc cần gửi cho xưởng cắt laser, cũng như định dạng file và thông tin cần gửi cho xưởng in 3D.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Đường kính dự kiến khoảng 3,6 mm. Vì đỉnh ren M3 gần 3,0 mm nên ốc đi qua thoải mái với khe hở dư; bắt thêm đai ốc phía sau để kẹp chặt.&lt;/li&gt;
  &lt;li&gt;Đai đồng cấy ren bằng nhiệt (threaded insert), kết hợp ốc máy và đai ốc nyloc nếu mối ghép chịu rung.&lt;/li&gt;
  &lt;li&gt;Mặt nứt chính là mặt tiếp giáp giữa các lớp in. Cần xoay chi tiết khi in lại sao cho các đường lớp cắt ngang mặt phẳng chịu lực xé và đánh dấu rõ mặt tiếp xúc bàn in.&lt;/li&gt;
  &lt;li&gt;Nhựa PLA dễ mềm quanh 55–60 °C; nên chuyển sang nhựa PETG cho đai giữ pin.&lt;/li&gt;
  &lt;li&gt;Cắt laser: file DXF, đơn vị milimét, đường vẽ khép kín và chiều dày tấm. In 3D: file STL, đơn vị milimét, tên vật liệu (PLA/PETG) kèm yêu cầu hướng in nếu cần.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://reprap.org/wiki/Print_Troubleshooting&quot;&gt;Gỡ lỗi in trên RepRap&lt;/a&gt; — hướng dẫn xử lý lỗi tách lớp, tạo sợi và các lỗi in 3D thường gặp.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Polylactic_acid&quot;&gt;Polylactic acid&lt;/a&gt; — đặc tính nhiệt của nhựa PLA và lý do vật liệu mềm trong khoang xe nóng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Laser_cutting&quot;&gt;Laser cutting&lt;/a&gt; — giải thích khái niệm khe cắt (kerf) trong gia công laser.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Bánh xe, caster và sơ đồ cơ cấu vi sai</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_05_wheels_casters_diff/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_05_wheels_casters_diff</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;70–90 phút&lt;/strong&gt;. Nếu robot đã lắp lăn được, khoảng 10 phút trong đó dùng để xoay thử bằng tay quanh trung điểm trục; nếu chưa, bạn dùng 10 phút đó để vẽ sơ đồ mũi tên vận tốc.&lt;/p&gt;

&lt;p&gt;Tại sao gần như mọi robot hút bụi trong nhà lẫn xe giao hàng trong nhà kho đều chọn cấu hình &lt;strong&gt;vi sai hai bánh chủ động + một bánh đa hướng (caster)&lt;/strong&gt; thay vì hệ thống lái kiểu ô tô (Ackermann)? Bí mật nằm ở khả năng &lt;strong&gt;quay tại chỗ với bán kính bằng 0&lt;/strong&gt;: chỉ cần cho bánh trái lùi và bánh phải tiến cùng tốc độ, robot xoay gọn ngay giữa hành lang hẹp mà không cần lùi chuồng. Và tuyệt vời hơn nữa, toàn bộ động học của hệ vi sai nằm gọn trong hai phương trình cộng–trừ đơn giản đến bất ngờ!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-động-học-vi-sai-tâm-quay-tức-thời-icc-và-bánh-caster&quot;&gt;Kiến thức cần nhớ: Động học vi sai, Tâm quay tức thời (ICC) và Bánh Caster&lt;/h2&gt;

&lt;h3 id=&quot;1-hai-phương-trình-vàng-của-động-học-thuận-vi-sai-forward-kinematics&quot;&gt;1. Hai phương trình vàng của động học thuận vi sai (Forward Kinematics)&lt;/h3&gt;
&lt;p&gt;Gọi \(L\) là khoảng cách giữa tâm hai bánh chủ động (&lt;em&gt;track width&lt;/em&gt;), \(v_L\) và \(v_R\) là vận tốc tiếp tuyến của bánh trái và bánh phải (tính từ \(v = r\omega\)). Khi không trượt ngang, vận tốc tịnh tiến \(v\) và vận tốc góc \(\omega\) của tâm robot là:
\(v = \frac{v_R + v_L}{2}, \qquad \omega = \frac{v_R - v_L}{L}.\)
Từ đây suy ngay ra &lt;strong&gt;động học ngược (Inverse Kinematics)&lt;/strong&gt; để đổi lệnh ROS &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; \((v, \omega)\) thành tốc độ hai bánh:
\(v_R = v + \frac{\omega L}{2}, \qquad v_L = v - \frac{\omega L}{2}.\)&lt;/p&gt;

&lt;h3 id=&quot;2-tâm-quay-tức-thời-icc--instantaneous-center-of-curvature&quot;&gt;2. Tâm quay tức thời (ICC — Instantaneous Center of Curvature)&lt;/h3&gt;
&lt;p&gt;Mọi chuyển động của xe vi sai ở một thời điểm đều là một chuyển động tròn quanh điểm &lt;strong&gt;ICC&lt;/strong&gt; nằm trên đường kéo dài của trục hai bánh, cách tâm robot một bán kính \(R = \frac{v}{\omega} = \frac{L}{2}\frac{v_R + v_L}{v_R - v_L}\). Khi \(v_R = v_L\), \(R \to \infty\) (đi thẳng); khi \(v_R = -v_L\), \(R = 0\) (quay tại chỗ quanh tâm trục!).&lt;/p&gt;

&lt;h3 id=&quot;3-cái-bẫy-của-bánh-đa-hướng-swivel-caster-vs-ball-caster&quot;&gt;3. Cái bẫy của bánh đa hướng (Swivel Caster vs. Ball Caster)&lt;/h3&gt;
&lt;p&gt;Bánh bi thép (&lt;em&gt;ball caster&lt;/em&gt;) quay mọi hướng không bị giật nhưng hay kẹt tóc/bụi và ma sát cao trên thảm. Bánh xoay (&lt;em&gt;swivel caster&lt;/em&gt;) lăn rất êm nhưng có khoảng lệch trục xoay (&lt;em&gt;caster offset&lt;/em&gt;) — mỗi khi robot đột ngột đảo chiều từ tiến sang lùi, bánh xoay phải lật \(180^\circ\), tạo ra một cú hích ngang làm lệch hướng khởi động!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-giải-cả-động-học-thuận-và-động-học-ngược-cho-robot-vi-sai&quot;&gt;Giải thích: Cầm tay giải cả Động học thuận và Động học ngược cho robot vi sai&lt;/h3&gt;

&lt;p&gt;Xét robot vi sai có khoảng cách hai bánh &lt;strong&gt;\(L = 0{,}20\text{ m}\)&lt;/strong&gt; (20 cm) và bán kính bánh &lt;strong&gt;\(r = 0{,}035\text{ m}\)&lt;/strong&gt; (đường kính 7 cm):&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bài toán 1 — Động học thuận (từ encoder bánh xe suy ra chuyển động robot):&lt;/strong&gt;
Encoder đo được bánh trái chạy \(v_L = 0{,}30\text{ m/s}\), bánh phải chạy \(v_R = 0{,}50\text{ m/s}\).&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Vận tốc tịnh tiến của tâm robot:
\(v = \frac{0{,}50 + 0{,}30}{2} = 0{,}40\text{ m/s}.\)&lt;/li&gt;
  &lt;li&gt;Vận tốc góc quay (quy ước dương = rẽ trái / ngược chiều kim đồng hồ):
\(\omega = \frac{0{,}50 - 0{,}30}{0{,}20} = \frac{0{,}20}{0{,}20} = +1{,}00\text{ rad/s}\;(\approx 57{,}3^\circ/\text{s}).\)&lt;/li&gt;
  &lt;li&gt;Bán kính quỹ đạo cung tròn quanh tâm ICC:
\(R = \frac{v}{\omega} = \frac{0{,}40}{1{,}00} = 0{,}40\text{ m}\;(40\text{ cm về phía bên trái robot}).\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Bài toán 2 — Động học ngược (từ lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; tìm tốc độ góc từng bánh):&lt;/strong&gt;
Bộ điều hướng yêu cầu robot đi thẳng kết hợp rẽ phải: \(v = 0{,}50\text{ m/s},\; \omega = -2{,}0\text{ rad/s}\).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
\[v_R = 0{,}50 + \frac{(-2{,}0)(0{,}20)}{2} = 0{,}50 - 0{,}20 = 0{,}30\text{ m/s} \implies \omega_R = \frac{0{,}30}{0{,}035} \approx 8{,}57\text{ rad/s}.\]
  &lt;/li&gt;
  &lt;li&gt;\(v_L = 0{,}50 - \frac{(-2{,}0)(0{,}20)}{2} = 0{,}50 + 0{,}20 = 0{,}70\text{ m/s} \implies \omega_L = \frac{0{,}70}{0{,}035} = 20{,}0\text{ rad/s}.\)
Kiểm tra trực giác: bánh trái quay nhanh hơn bánh phải nên robot rẽ sang phải — khớp tuyệt đối!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, các bạn vẽ được sơ đồ vectơ vận tốc hai bánh cho ba trạng thái: đi thẳng, quẹo nhẹ và xoay tại chỗ, đồng thời tính được vận tốc góc xoay tại chỗ \(\omega = 2 v_r / b\). Các bạn giải thích được vai trò của khoảng đuôi (trail) trên bánh caster, lý do caster lắp ngược bị rung lắc và vì sao cơ cấu bốn bánh cố định tiêu tốn dòng điện gần mức dòng kẹt (Chương 05). Cuối cùng, các bạn xác định đúng trung điểm trục làm tâm quay và liên hệ trực tiếp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;) với hai động cơ.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Các bạn nắm vững định nghĩa từ Bài 01: khổ vết \(b\) giữa hai tâm tiếp xúc, bán kính lăn \(r\) từ phép lăn một vòng và cặp phương trình:&lt;/p&gt;

\[v = \frac{v_r + v_l}{2}, \qquad \omega = \frac{v_r - v_l}{b}.\]

&lt;p&gt;Các bạn đã hiểu sai số ở \(b\) sẽ khiến lệnh đi thẳng biến thành quỹ đạo cong, và tỉ số truyền bánh răng (Bài 03) quy đổi góc quay động cơ sang góc quay bánh xe.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Capstone A sử dụng cấu hình hai bánh chủ động kết hợp một bánh caster. Trong ROS, lệnh điều khiển gửi qua thông điệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; là \(v\) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; là \(\omega\). Bộ điều khiển &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; giải ngược hai phương trình trên để xuất lệnh tốc độ cho động cơ trái và động cơ phải.&lt;/p&gt;

&lt;p&gt;Nếu một động cơ bị đấu ngược chiều, bộ điều khiển phần mềm không thể tự biết: cả hai bánh cùng nhận lệnh “tiến” nhưng trên sàn một bánh lại chạy lùi, khiến robot xoay tròn thay vì đi thẳng. Ở Chương 07, bạn sẽ kiểm tra dấu chiều quay khi nhấc bánh khỏi sàn rồi mới xác nhận lại trên mặt đất — nơi bánh caster và độ bám lốp bắt đầu phát huy tác dụng. Sơ đồ mũi tên vận tốc hôm nay là chuẩn đối chiếu trực tiếp khi gỡ lỗi.&lt;/p&gt;

&lt;h2 id=&quot;mũi-tên-khoảng-đuôi-và-bánh-bị-cà&quot;&gt;Mũi tên, khoảng đuôi, và bánh bị cà&lt;/h2&gt;

&lt;p&gt;Khi đi thẳng, hai vết tiếp xúc chuyển động cùng vận tốc (\(v_r = v_l\)), do đó \(v\) bằng vận tốc bánh và \(\omega = 0\). Tâm quay tức thời nằm ở vô cực trên đường kéo dài của trục bánh.&lt;/p&gt;

&lt;p&gt;Khi quẹo trái nhẹ, cả hai bánh vẫn tiến về phía trước nhưng bánh phải nhanh hơn bánh trái. Trong hệ tọa độ robot chuẩn (trục \(x\) hướng về phía mũi, trục \(z\) hướng lên trên), \(\omega &amp;gt; 0\) tương ứng với chuyển động quay sang trái quanh một tâm nằm trên đường trục, phía ngoài bánh trái. Bánh caster lúc này tự xoay theo quỹ đạo.&lt;/p&gt;

&lt;p&gt;Xoay tại chỗ là trường hợp đặc biệt khi hai bánh quay ngược chiều với cùng độ lớn vận tốc: \(v_l = -v_r\). Khi đó:&lt;/p&gt;

\[v = \frac{v_r + (-v_r)}{2} = 0, \qquad \omega = \frac{v_r - (-v_r)}{b} = \frac{2 v_r}{b}.\]

&lt;p&gt;Vận tốc tịnh tiến bằng không và tâm quay trùng với trung điểm đoạn nối hai tâm tiếp xúc. Hãy đánh dấu điểm này lên tấm khung: khi xoay robot chậm bằng tay trên sàn nhẵn, dấu này phải đứng yên tại một điểm trên sàn trong lúc hai bánh lăn quanh nó. Nếu bạn đánh dấu sai vị trí (ví dụ đặt tại bánh caster hoặc giữa hai vỏ động cơ), điểm dấu sẽ vẽ thành một vòng tròn, cho thấy tâm hình học bạn chọn không trùng với tâm động học thực tế.&lt;/p&gt;

&lt;p&gt;Khoảng đuôi (trail) của bánh caster là yếu tố giúp điểm tựa thứ ba tự ổn định hướng. Trên bánh caster xoay, vết tiếp xúc nằm lùi phía sau trục xoay dọc theo chiều chuyển động; lực cản lăn tại vết tiếp xúc sẽ tự động kéo bánh thẳng hàng theo hướng đi (giống bánh xe đẩy siêu thị). Nếu lắp ngược caster khiến vết tiếp xúc nằm phía trước chốt xoay, bánh sẽ mất ổn định và rung lắc mạnh, đặc biệt khi tăng tốc. Bánh bi đa hướng hầu như không có khoảng đuôi vì điểm tiếp xúc nằm ngay trên trục xoay, nên dễ bị trượt cà làm tăng ma sát ngoài dự kiến dù vẫn đỡ được mũi xe. Capstone A ưu tiên dùng bánh caster xoay có khoảng đuôi rõ ràng và lắp sao cho vết tiếp xúc đi sau chốt xoay.&lt;/p&gt;

&lt;p&gt;Cơ cấu lái trượt (skid-steer) sử dụng bốn bánh cố định không xoay hướng. Mỗi khi chuyển hướng, lốp xe buộc phải trượt ngang trên sàn. Ma sát trượt ngang đòi hỏi động cơ kéo dòng điện rất lớn — tiệm cận mức dòng kẹt mà Chương 05 đã cảnh báo là rủi ro quá nhiệt. Capstone A tránh tổn hao này bằng cách dùng hai bánh chủ động kết hợp một bánh caster có đuôi để mọi cú quay đều dựa trên chuyển động lăn.&lt;/p&gt;

&lt;p&gt;Lưu ý rằng đường kính cao su ghi trên bao bì vẫn không phải là bán kính lăn thực tế. Hãy lặp lại phép lăn một vòng trên giấy (Bài 01) mỗi khi thay lốp hoặc thay đổi tải trọng. Nếu bán kính lăn của bánh phải lớn hơn bánh trái, một lệnh đi thẳng đều sẽ khiến robot tự quẹo nhẹ sang trái.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;Đặt thước ở đâu: tâm tiếp xúc và bán kính đã nén&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/diff_drive_kinematics.png&quot; alt=&quot;Mũi tên bằng nhau, mũi tên lệch nhau, và cú xoay đặt tâm quay đúng trung điểm trục&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy đối chiếu đồng thời cả hai hình: hình đầu tiên giúp đo đúng \(b\) và \(r\), còn hình thứ hai minh họa ngôn ngữ vectơ vận tốc cho ba trạng thái tiến, xoay tại chỗ và quẹo. Khi đấu dây động cơ ở Chương 07, hãy dán nhãn dây theo đúng quy ước mũi tên này trước khi mặc định tín hiệu PWM dương luôn làm xe chạy tiến.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Lấy khung xe ở Bài 01 với \(b = 0.15\) m, ra lệnh xoay tại chỗ ở \(v_r = 0.20\) m/s và \(v_l = -0.20\) m/s:&lt;/p&gt;

\[v = 0, \qquad \omega = \frac{2 \times 0.20}{0.15} = 2.667\ \text{rad/s}.\]

&lt;p&gt;Một vòng quay đầy đủ là \(2\pi\) radian, nên thời gian hoàn thành một vòng là:&lt;/p&gt;

\[T = \frac{2\pi}{2.667} \approx 2.36\ \text{s},\]

&lt;p&gt;giả sử hai bánh đạt đúng vận tốc 0,20 m/s và không bị trượt. Với bán kính lăn \(r = 31.2\) mm, độ lớn vận tốc góc của mỗi bánh xe là:&lt;/p&gt;

\[\dot{\theta} = \frac{0.20}{0.0312} \approx 6.41\ \text{rad/s},\]

&lt;p&gt;trong đó hai động cơ quay ngược dấu nhau. Nếu một dây động cơ bị đấu ngược, lệnh đi thẳng \(v = 0.20\) m/s, \(\omega = 0\) (phần mềm xuất hai giá trị dương) sẽ biến thành \(v_l = 0.20\) m/s và \(v_r = -0.20\) m/s trên sàn — tức là robot xoay tại chỗ thay vì đi thẳng. Vì vậy, bước thử nhấc bánh ở Chương 07 là bắt buộc để đảm bảo lệnh dương làm cả hai bánh cùng quay tiến.&lt;/p&gt;

&lt;h2 id=&quot;lab-tìm-tâm-quay-hoặc-vẽ-mũi-tên&quot;&gt;Lab: tìm tâm quay, hoặc vẽ mũi tên&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Khi xoay robot đã lắp bằng tay, luôn ngắt kết nối pin để tránh việc động cơ bị dẫn ngược phát điện áp ngược về mạch cầu H. Giữ ngón tay tránh xa bánh răng và khe dưới bánh caster khi xoay trên sàn cứng để không bị kẹp tay.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung từ Bài 01, hoặc bản vẽ nhìn từ trên đúng tỉ lệ&lt;/td&gt;
      &lt;td&gt;Cơ cấu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Băng dính và bút&lt;/td&gt;
      &lt;td&gt;Tâm quay dự đoán&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Phác mũi tên hoặc ghi chú thử bằng tay&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Đánh dấu trung điểm của đoạn nối hai tâm tiếp xúc mặt lốp — đây là tâm xoay dự đoán.&lt;/li&gt;
  &lt;li&gt;Nếu khung xe đã lăn được, ngắt pin rồi xoay chậm khung tại chỗ bằng tay. Quan sát điểm đánh dấu: nếu đo \(b\) chính xác và bánh không trượt, điểm dấu sẽ đứng yên tại một vị trí trên sàn.&lt;/li&gt;
  &lt;li&gt;Kiểm tra khoảng đuôi của bánh caster từ góc nhìn ngang: vết tiếp xúc phải nằm sau chốt xoay theo chiều đẩy tiến. Nếu càng bị lắp ngược, ghi chú để xoay lại trước Chương 07.&lt;/li&gt;
  &lt;li&gt;Nếu chưa có khung xe thực tế, vẽ ba sơ đồ nhìn từ trên xuống: đi thẳng (hai mũi tên tiến bằng nhau), xoay tại chỗ (hai mũi tên ngược chiều bằng nhau, có đánh dấu tâm xoay) và quẹo trái nhẹ (cả hai mũi tên tiến, mũi tên phải dài hơn). Ghi rõ \(v\) và \(\omega\) trên từng hình.&lt;/li&gt;
  &lt;li&gt;Ghi quy ước dấu sẽ áp dụng trong firmware vào sổ tay: giá trị dương cho bánh trái và bánh phải đều đẩy robot tiến về phía trước.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Điểm đánh dấu đứng yên khi xoay thử cẩn thận bằng tay, hoặc bộ ba sơ đồ mũi tên đã ghi chú đầy đủ. Ghi chú trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; nêu rõ hướng lắp caster (vết tiếp xúc đi sau chốt xoay) và quy ước dấu chiều quay để kiểm chứng khi nhấc bánh ở Chương 07.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Các bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường nghĩa là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;“Trung điểm” đi thành vòng tròn&lt;/td&gt;
      &lt;td&gt;Dấu nằm giữa hai vỏ động cơ, hoặc một bánh đang trượt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Caster kêu lạch cạch ngay khi đẩy&lt;/td&gt;
      &lt;td&gt;Càng lắp ngược, hoặc gần như không có tải trên caster&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh bi cà và làm tấm quay ngang&lt;/td&gt;
      &lt;td&gt;Không có khoảng đuôi; lắp bánh caster xoay cho Capstone A&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lệnh tiến vẽ thành vòng tròn sau khi có điện&lt;/td&gt;
      &lt;td&gt;Một dấu động cơ bị đảo; xác nhận khi nhấc bánh ở Chương 07&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đường thẳng cong nhẹ dù lệnh đều&lt;/td&gt;
      &lt;td&gt;Hai bán kính lăn không khớp nhau&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Khung xe đã được đề cập ở Bài 01; bài này bổ sung bánh xe và bánh caster nếu bộ kit của bạn chưa có sẵn. Bạn có thể mua thêm một bánh bi đa hướng (tùy chọn) để trực tiếp cảm nhận sự khác biệt khi không có khoảng đuôi, nhưng xe nộp cho Capstone A vẫn dùng bánh caster xoay.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mua gì&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh cao su&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh xe cao su 65mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–45.000 mỗi cái&lt;/td&gt;
      &lt;td&gt;Rồi làm phép lăn để lấy \(r\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh caster xoay&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh caster robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;12.000–40.000&lt;/td&gt;
      &lt;td&gt;Khoảng đuôi nhìn thấy phía sau chốt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh bi đa hướng&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh đa hướng bi&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;Để đối chiếu, tùy chọn; nó cà&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Các trang tìm mua:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=b%C3%A1nh+xe+cao+su&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=b%C3%A1nh%20xe%20cao%20su%2065mm&quot;&gt;Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=b%C3%A1nh%20caster%20robot&quot;&gt;Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=b%C3%A1nh%20xe%20robot&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Giá bán có thể thay đổi theo thời điểm. Hãy kiểm tra moay-ơ bánh khớp với trục chữ D của động cơ TT hoặc khớp với lỗ khớp nối đã đo ở Bài 02.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=2QE3LG5h2tw&quot;&gt;Differential Drive &amp;amp; Odometry Explained: How It Works&lt;/a&gt; — Netizen Robotics, tiếng Anh. Giải thích cơ cấu vi sai chuyển đổi vận tốc hai bánh thành tiến và quay; lưu ý bánh caster không phải bánh chủ động.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=eQ9E0Zvp9jw&quot;&gt;Modern Robotics, Chapter 13.4:  Odometry&lt;/a&gt; — Northwestern Robotics, tiếng Anh. Trình bày cách tính odometry từ bánh xe, nhấn mạnh sự tách biệt giữa bán kính bánh và khổ vết.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Cho \(b = 0.18\) m, \(v_r = 0.15\) m/s và \(v_l = -0.15\) m/s. Tính \(v\), \(\omega\) và thời gian xoay đủ một vòng.&lt;/li&gt;
  &lt;li&gt;Cả hai bánh cùng chạy ở \(+0.22\) m/s với \(b = 0.15\) m. Tính \(v\), \(\omega\) và cho biết tâm quay nằm ở đâu.&lt;/li&gt;
  &lt;li&gt;Khi đẩy robot tiến tới, bánh caster bị lật vòng rồi rung lắc liên tục. Khoảng đuôi đang gặp vấn đề gì và cách khắc phục cơ khí ra sao?&lt;/li&gt;
  &lt;li&gt;Một robot bốn bánh cố định xoay tại chỗ trên thảm khiến dòng điện tăng sát mức dòng kẹt. Giải thích nguyên nhân và cho biết Capstone A dùng cơ cấu nào để tránh tình trạng này.&lt;/li&gt;
  &lt;li&gt;Khi gửi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x = 0.2&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z = 0&lt;/code&gt; mà chưa thay đổi \(b\), robot lại chạy xoay tròn. Cần kiểm tra chi tiết đấu dây nào khi nhấc bánh khỏi sàn?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(v = 0\), \(\omega = 2\times 0.15/0.18 = 1.667\) rad/s, thời gian một vòng \(T = 2\pi/1.667 \approx 3.77\) s.&lt;/li&gt;
  &lt;li&gt;\(v = 0.22\) m/s, \(\omega = 0\). Tâm quay nằm ở vô cực trên đường kéo dài của trục bánh; hướng mũi xe không thay đổi.&lt;/li&gt;
  &lt;li&gt;Vết tiếp xúc đang đi trước chốt xoay (caster lắp ngược hoặc càng không có khoảng đuôi). Cần xoay và lắp lại để vết tiếp xúc nằm phía sau chốt xoay.&lt;/li&gt;
  &lt;li&gt;Bốn bánh cố định buộc lốp phải trượt cà ngang khi quay, tạo ma sát trượt lớn đòi hỏi mô-men và dòng điện gần mức kẹt. Capstone A dùng hai bánh chủ động kết hợp một bánh caster có đuôi để bánh tự xoay theo hướng lăn.&lt;/li&gt;
  &lt;li&gt;Một trong hai động cơ bị đấu ngược cực (đảo dấu). Cần nhấc bánh khỏi sàn và kiểm tra sao cho lệnh dương làm cả hai bánh cùng quay theo chiều tiến đã quy ước.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ros-controls/ros2_controllers/tree/master/diff_drive_controller&quot;&gt;diff_drive_controller (ROS 2)&lt;/a&gt; — cách chuyển đổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; thành vận tốc hai bánh theo các phương trình trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://navigation.ros.org/setup_guides/odom/setup_odom.html&quot;&gt;Thiết lập odometry trên Nav2&lt;/a&gt; — hướng dẫn xuất bản odometry sau khi các quy ước dấu và thông số bánh đã chính xác.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://wiki.ros.org/diff_drive_controller&quot;&gt;Wiki diff_drive_controller ROS 1&lt;/a&gt; — trình bày tương tự về việc tách khớp bánh trái và bánh phải (dù viết cho ROS 1 nhưng mô hình vẫn áp dụng đúng).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tài liệu tham khảo chuyên sâu hơn là cuốn &lt;em&gt;Introduction to Autonomous Mobile Robots&lt;/em&gt; của Siegwart, Nourbakhsh và Scaramuzza; với các tham số cụ thể cho phiên bản ROS đang dùng, hãy ưu tiên các liên kết tài liệu ở trên.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Cơ cấu thanh, cam, đai và bánh răng-thanh răng</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_04_linkages_belts_cams/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_04_linkages_belts_cams</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;80–100 phút&lt;/strong&gt;, bao gồm thực hành dựng cơ cấu bốn thanh bằng bìa cứng hoặc đếm răng pulley GT2 nếu bạn có sẵn linh kiện.&lt;/p&gt;

&lt;p&gt;Động cơ chỉ biết làm một việc duy nhất: &lt;strong&gt;quay tròn&lt;/strong&gt;. Nhưng robot trong đời thực lại cần gắp đồ song song, nâng hạ thẳng đứng, bước đi như chân nhện, hoặc truyền lực từ động cơ đặt gọn trong thân ra tận khớp cổ tay cách đó 30 centimet. Khoảng trống giữa &lt;em&gt;chuyển động quay tròn đơn điệu của động cơ&lt;/em&gt; và &lt;em&gt;mọi quỹ đạo phức tạp của robot&lt;/em&gt; được lấp đầy bởi bốn họ cơ cấu kinh điển: &lt;strong&gt;cơ cấu thanh (linkages), đai răng (timing belts), cam và bánh răng–thanh răng (rack-and-pinion)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-biến-đổi-chuyển-động-trong-thiết-kế-cơ-cấu-robot&quot;&gt;Kiến thức cần nhớ: Biến đổi chuyển động trong thiết kế cơ cấu robot&lt;/h2&gt;

&lt;h3 id=&quot;1-cơ-cấu-4-khâu-four-bar-linkage-và-định-lý-grashof&quot;&gt;1. Cơ cấu 4 khâu (Four-Bar Linkage) và định lý Grashof&lt;/h3&gt;
&lt;p&gt;Cơ cấu 4 khâu là “viên gạch Lego” linh hoạt nhất của cơ học máy: dùng để tạo tay kẹp song song, chân robot biết bước (cơ cấu Klann/Jansen) hoặc tay nâng không bị nghiêng góc. Gọi độ dài khâu ngắn nhất là \(s\), dài nhất là \(l\), hai khâu còn lại là \(p, q\). &lt;strong&gt;Điều kiện Grashof&lt;/strong&gt; phát biểu rằng ít nhất một khâu có thể quay tròn liên tục \(360^\circ\) khi và chỉ khi:
\(s + l \le p + q.\)&lt;/p&gt;

&lt;h3 id=&quot;2-đai-răng-gt2-timing-belt-và-truyền-động-bánh-răngthanh-răng&quot;&gt;2. Đai răng (GT2 Timing Belt) và Truyền động bánh răng–thanh răng&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Đai răng (như GT2, bước răng \(2\text{ mm}\)):&lt;/strong&gt; truyền lực êm, không trượt, gần như không có backlash và cho phép đặt động cơ nặng ở sát gốc (giảm quán tính cánh tay!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bánh răng – thanh răng (Rack &amp;amp; Pinion):&lt;/strong&gt; biến chuyển động quay trực tiếp thành tịnh tiến dọc thanh răng, chịu tải lớn và dễ kéo dài hành trình hơn trục vitme.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;3-cơ-cấu-cam--bộ-nhớ-cơ-khí-lập-trình-sẵn-quỹ-đạo&quot;&gt;3. Cơ cấu Cam — “bộ nhớ cơ khí” lập trình sẵn quỹ đạo&lt;/h3&gt;
&lt;p&gt;Một đĩa cam được cắt biên dạng phi tròn có thể biến tốc độ quay đều thành chuyển động nâng–dừng–hạ phức tạp mà không cần thêm cảm biến hay phần mềm — rất hữu ích cho các cơ cấu kẹp nhanh hoặc nạp đạn lặp đi lặp lại trong thi đấu robotics.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-hành-trình-đai-răng-gt2-và-kiểm-tra-điều-kiện-grashof&quot;&gt;Giải thích: Cầm tay tính hành trình đai răng GT2 và kiểm tra điều kiện Grashof&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Tính bước dịch chuyển tuyến tính của hệ truyền động đai GT2:&lt;/strong&gt;
Một trục trượt tuyến tính (giống trục X/Y máy in 3D) dùng đai răng &lt;strong&gt;GT2&lt;/strong&gt; (bước răng \(p = 2{,}0\text{ mm}\)) kéo bởi puly &lt;strong&gt;\(Z = 20\) răng&lt;/strong&gt;, gắn trên động cơ bước chạy vi bước &lt;strong&gt;1/16&lt;/strong&gt; (\(3\,200\text{ vi bước/vòng}\)):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Quãng đường tịnh tiến mỗi vòng quay động cơ:&lt;/strong&gt;
\(S_{\text{vòng}} = Z \times p = 20 \times 2{,}0 = 40{,}0\text{ mm/vòng}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Độ phân giải tuyến tính lý thuyết mỗi vi bước:&lt;/strong&gt;
\(\Delta x = \frac{40{,}0\text{ mm}}{3\,200} = 0{,}0125\text{ mm/vi bước}\;(12{,}5\,\mu\text{m}!).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Để trượt chính xác \(150\text{ mm}\):&lt;/strong&gt; vi điều khiển cần phát đúng \(\frac{150}{0{,}0125} = 12\,000\) xung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STEP&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Kiểm tra nhanh một cơ cấu 4 khâu có quay tròn liên tục được không?&lt;/strong&gt;
Giả sử bạn thiết kế cơ cấu 4 khâu với chiều dài các thanh (cm): \(s = 3,\; l = 10,\; p = 7,\; q = 8\):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Tổng ngắn nhất + dài nhất: \(s + l = 3 + 10 = 13\text{ cm}\).&lt;/li&gt;
  &lt;li&gt;Tổng hai khâu còn lại: \(p + q = 7 + 8 = 15\text{ cm}\).&lt;/li&gt;
  &lt;li&gt;Vì \(13 \le 15\), cơ cấu &lt;strong&gt;thỏa điều kiện Grashof&lt;/strong&gt; — cố định một khâu kề khâu ngắn nhất \(s = 3\) sẽ cho ta cơ cấu tay quay–con lắc (&lt;em&gt;crank-rocker&lt;/em&gt;) quay trọn \(360^\circ\) mượt mà không bị kẹt chết!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, các bạn kiểm tra được cơ cấu bốn thanh bằng điều kiện Grashof để nhận diện dạng maniven – cần lắc, tính được tỉ số truyền của bộ truyền đai răng từ số răng pulley, và quy đổi đường kính vòng chia của bánh răng dẫn thành hành trình thanh răng trên mỗi vòng quay. Các bạn cũng mô tả được đoạn nâng và đoạn dừng của cam, đồng thời biết nhân gộp tỉ số truyền của đai (nếu có) vào tỉ số tổng từ encoder tới bánh xe trước khi cấu hình ROS.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Các bạn đã biết tính tỉ số truyền \(i = N_{\text{out}}/N_{\text{in}}\) (Bài 03) và nắm rõ khổ vết \(b\) (Bài 01). Vật liệu thực hành chỉ cần bìa cứng, bốn đinh ghim và thước kẻ; pulley GT2 là linh kiện tùy chọn.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Hệ truyền động của Capstone A dùng hai bánh xe gắn qua hộp giảm tốc chứ không dùng cơ cấu thanh. Tuy nhiên, các cơ cấu trong bài này giúp bạn hiểu bản chất các loại khớp sẽ mô tả trong ROS về sau. Trong ROS, mỗi khớp được định nghĩa bằng tên, loại khớp và cặp giới hạn chuyển động. Một cơ cấu bốn thanh chỉ có thể lắc qua lại bắt buộc phải được khai báo giới hạn góc tương ứng; nếu không, bộ điều khiển sẽ ra lệnh tư thế vượt quá khả năng cơ khí mà mô hình bìa cứng đã chỉ ra.&lt;/p&gt;

&lt;p&gt;Nếu bạn đặt thêm bộ truyền đai GT2 giữa động cơ và bánh xe, tỉ số của đai sẽ nhân thêm vào tỉ số hộp giảm tốc ở Bài 03. Bỏ quên tỉ số đai sẽ khiến cùng một số xung encoder tương ứng với góc bánh sai lệch, làm tỉ lệ odometry sai giống hệt trường hợp đảo ngược \(i\). Cơ cấu cam thường xuất hiện ở công tắc hành trình hoặc một số tay kẹp; dù không cần cam để hoàn thành Capstone A, bạn cần hình thành thói quen ghi đầy đủ mọi tỉ số truyền nằm giữa encoder và mặt sàn.&lt;/p&gt;

&lt;h2 id=&quot;bốn-thanh-đai-răng-thanh-răng-và-cam&quot;&gt;Bốn thanh, đai răng, thanh răng, và cam&lt;/h2&gt;

&lt;p&gt;Cơ cấu bốn thanh gồm bốn khâu cứng liên kết bằng các khớp bản lề tạo thành một vòng kín. Gọi chiều dài khâu ngắn nhất là \(s\), khâu dài nhất là \(l\), hai khâu còn lại là \(p\) và \(q\). Điều kiện Grashof để ít nhất một khâu có thể quay tròn đủ một vòng tương đối so với các khâu khác (trường hợp vận hành ổn định) là:&lt;/p&gt;

\[s + l &amp;lt; p + q.\]

&lt;p&gt;Giáo trình thường viết dưới dạng \(\le\), nhưng trường hợp dấu bằng tạo ra cơ cấu điểm đổi (change-point): các khâu có thể duỗi thẳng hàng rồi kẹt hoặc đổi nhánh chuyển động, nên cần kiểm tra kích thước rất kỹ trước khi sử dụng. Khi bất đẳng thức \(s + l &amp;lt; p + q\) thỏa mãn và khâu ngắn nhất nằm liền kề khâu cố định (khâu nền), ta thu được cơ cấu maniven – cần lắc: khâu ngắn quay tròn liên tục, còn khâu đối diện lắc qua lại (tương tự cần gạt mưa ô tô). Ngược lại, nếu \(s + l &amp;gt; p + q\), không khâu nào quay hết vòng; cơ cấu chỉ lắc trong một góc giới hạn và động cơ sẽ bị kẹt tải nếu bị lệnh quay tròn.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/four_bar_linkage.png&quot; alt=&quot;Cơ cấu bốn thanh: maniven quay, cần lắc đung đưa, thanh nối ghép chúng&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy quan sát sơ đồ như một mạch kín: khâu nền gắn cố định vào khung, maniven nối với động cơ, cần lắc dao động qua lại và thanh nối liên kết hai khâu đó. Chỉ cần đổi khâu nào được cố định làm khâu nền, kiểu chuyển động của cả cơ cấu sẽ thay đổi dù chiều dài bốn thanh giữ nguyên.&lt;/p&gt;

&lt;p&gt;Bộ truyền đai răng (phổ biến nhất trên robot nhỏ là chuẩn GT2) có tỉ số truyền xác định trực tiếp từ số răng hai pulley:&lt;/p&gt;

\[i = \frac{N_{\text{driven}}}{N_{\text{driver}}}.\]

&lt;p&gt;Khi đai đủ căng và răng ăn khớp tốt, bộ truyền hầu như không bị trượt. Ở cấu hình đai hở thông thường (đai vòng quanh hai pulley mà không bắt chéo), hai pulley quay cùng chiều nhau; chiều quay chỉ đảo ngược khi đai được dẫn qua mặt lưng của một pulley trung gian. Chiều dài dây đai cố định khoảng cách tâm giữa hai trục, nên không thể thay đổi tỉ số truyền bằng cách kéo giãn hai động cơ ra xa nhau. Đai quá căng sẽ làm mòn nhanh bạc lót (vốn đã quyết định giữ nguyên ở Bài 02), còn đai quá lỏng sẽ bị nhảy răng khi chịu mô-men. Hiện tượng nhảy răng gây ra lỗi giống độ rơ — có khoảng trễ rồi nhảy bậc — mà encoder trên trục động cơ không hề phát hiện. Để kiểm tra độ căng, hãy búng nhẹ vào nhánh đai: âm trầm và căng vừa là đạt, âm ngân cao là quá căng, còn dây chùng nhão là quá lỏng.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/belt_pulley.png&quot; alt=&quot;Đai GT2 và pulley: số răng đặt tỉ số, độ căng quyết định răng có còn ăn&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Cơ cấu bánh răng – thanh răng biến chuyển động quay thành chuyển động tịnh tiến thẳng. Sau mỗi vòng quay của bánh răng dẫn, thanh răng dịch chuyển một đoạn đúng bằng chu vi vòng chia:&lt;/p&gt;

\[x = \pi d,\]

&lt;p&gt;trong đó \(d\) là đường kính vòng chia của bánh dẫn (đường kính tại vị trí răng thực sự ăn khớp lăn). Cơ cấu này thường dùng cho ray trượt thẳng trên tay máy nhỏ hoặc bệ cảm biến, không bắt buộc cho Capstone A, nhưng có cùng bản chất tính toán với bán kính lăn của bánh xe.&lt;/p&gt;

&lt;p&gt;Cam là một chi tiết quay có biên dạng tạo hình tỳ lên con đội. Đoạn nâng (rise) đẩy con đội dịch chuyển, còn đoạn dừng (dwell) giữ con đội đứng yên dù cam vẫn đang quay. Trong robot, cam đôi khi xuất hiện ở cơ cấu giới hạn hành trình hoặc tay kẹp cần giữ trạng thái đóng. Đoạn dừng đóng vai trò như một giới hạn chuyển động cứng; trong URDF, bạn thường mô tả nó bằng giới hạn khớp hoặc giản lược thành một cơ cấu đơn giản. Capstone A không yêu cầu thiết kế cam.&lt;/p&gt;

&lt;p&gt;Bất kỳ tầng truyền động nào đặt giữa encoder và lốp xe đều phải được ghi nhận vào tỉ số truyền tổng. Chẳng hạn, hộp số 1:48 kết hợp với bộ đai tỉ số 2 cho tỉ số tổng tại bánh xe là \(i = 96\). Đây chính là giá trị mà &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; cần nhận để tính odometry chính xác.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Kiểm tra điều kiện Grashof:&lt;/strong&gt; Cho bốn khâu có chiều dài \(s = 30\) mm, \(p = 55\) mm, \(q = 70\) mm và \(l = 90\) mm. Ta có:&lt;/p&gt;

\[s + l = 120\ \text{mm}, \qquad p + q = 125\ \text{mm}.\]

&lt;p&gt;Vì \(120 &amp;lt; 125\), ít nhất một khâu quay được đủ vòng. Nếu cố định khâu 70 mm và dẫn động khâu 30 mm (maniven), khâu đối diện sẽ lắc qua lại. Ngược lại, nếu tăng khâu dài nhất lên 110 mm thì \(s + l = 140 &amp;gt; 125\): điều kiện Grashof không thỏa mãn, mô hình bìa cứng sẽ bị kẹt trước khi quay hết vòng — đó chính là giới hạn khớp cảm nhận trực tiếp bằng tay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Hành trình thanh răng:&lt;/strong&gt; Một bánh răng dẫn có đường kính vòng chia 12 mm sẽ đẩy thanh răng đi được sau mỗi vòng quay là:&lt;/p&gt;

\[x = \pi \times 12 \approx 37.7\ \text{mm}.\]

&lt;p&gt;Nửa vòng quay tương ứng hành trình khoảng 18,8 mm. Lưu ý phải dùng đường kính vòng chia thay vì đường kính đỉnh răng ngoài (lớn hơn), và luôn ghi rõ đường kính nào đã được đo.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Bộ truyền đai:&lt;/strong&gt; Pulley gắn động cơ có 20 răng kéo pulley phía bánh xe 60 răng cho tỉ số \(i = 60/20 = 3\), tương tự cặp bánh răng 12 và 36 răng. Nếu có hộp giảm tốc phía trước pulley động cơ, cần nhân thêm tỉ số hộp số vào kết quả này.&lt;/p&gt;

&lt;h2 id=&quot;lab-bìa-hoặc-pulley-hoặc-phác-họa&quot;&gt;Lab: bìa, hoặc pulley, hoặc phác họa&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Đinh ghim có đầu nhọn; luôn cắm gọn trên bìa, không bỏ vào túi áo quần. Nếu thao tác căng dây đai GT2 thật, đảm bảo đã tắt nguồn và giữ ngón tay tránh xa vùng ăn khớp pulley vì đai quá căng có thể làm bật vít cố định trục bị lỏng.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Bìa, bốn đinh ghim hoặc chốt, thước&lt;/td&gt;
      &lt;td&gt;Cơ cấu bốn thanh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hoặc một cặp pulley GT2 và đai&lt;/td&gt;
      &lt;td&gt;Đếm răng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bút chì&lt;/td&gt;
      &lt;td&gt;Phác cơ cấu không thỏa Grashof và số thanh răng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Chiều dài, \(i\), hoặc hành trình thanh răng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Cắt bốn thanh bìa với khoảng cách tâm lỗ đúng như ví dụ: 30, 55, 70 và 90 mm. Ghim chúng thành vòng kín, cố định thanh 70 mm xuống mặt bàn rồi xoay maniven 30 mm để quan sát chuyển động lắc của khâu đối diện.&lt;/li&gt;
  &lt;li&gt;Thay khâu dài nhất thành 110 mm (giữ nguyên ba khâu kia) và thử xoay để cảm nhận vị trí kẹt. Phác họa cả hai cơ cấu vào sổ và ghi rõ trường hợp nào thỏa mãn \(s + l &amp;lt; p + q\).&lt;/li&gt;
  &lt;li&gt;Nếu có sẵn phần cứng GT2, đếm số răng pulley dẫn và pulley bị dẫn, tính tỉ số \(i\) và ghi nhận độ căng khi búng đai (chùng, ngân cao hay trầm vừa). Bạn có thể bỏ bước cắt bìa nếu thực hành trên pulley, nhưng vẫn cần phác họa một cơ cấu thỏa và một cơ cấu không thỏa Grashof kèm kích thước từng khâu.&lt;/li&gt;
  &lt;li&gt;Nếu không có cả pulley lẫn đinh ghim, hãy tính ví dụ thanh răng đường kính 12 mm và vẽ đúng tỉ lệ cả hai cơ cấu bốn thanh trên giấy, dán nhãn “bản vẽ”.&lt;/li&gt;
  &lt;li&gt;Viết một câu kết luận về tỉ số truyền tổng từ encoder tới bánh xe của Capstone A (chỉ nhân thêm tỉ số đai nếu robot của bạn thực sự dùng đai).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Hai hình phác họa có dán nhãn rõ ràng (một cơ cấu quay đủ vòng và một cơ cấu bị kẹt), hoặc tỉ số GT2 kèm hai hình phác họa đó. Số liệu hành trình thanh răng 37,7 mm/vòng được ghi chú nếu bạn thực hiện bước tính này. Tỉ số encoder-tới-bánh khớp với dấu đóng ở Bài 03 (hoặc nhân thêm tỉ số đai nếu có).&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Các bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường nghĩa là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;“Maniven” không quay hết&lt;/td&gt;
      &lt;td&gt;\(s + l\) lớn hơn \(p + q\), hoặc lỗ cách đầu thanh quá xa nên chiều dài thật khác nhãn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đai nhảy răng dưới mô-men ngón tay&lt;/td&gt;
      &lt;td&gt;Quá lỏng, hoặc răng không khớp bước đai&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bạc nóng và đai ngân&lt;/td&gt;
      &lt;td&gt;Quá căng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Góc bánh từ xung chỉ bằng một nửa những gì sàn cho thấy&lt;/td&gt;
      &lt;td&gt;Một đai 2:1 bị bỏ khỏi \(i\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Bìa cứng là đủ cho bài thực hành này. Chỉ nên mua đai và pulley GT2 nếu thiết kế truyền động của bạn thực sự dùng đai; hai bánh động cơ TT tiêu chuẩn của Capstone A thường không cần.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mua gì&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Đai GT2&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây đai GT2&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;Nêu chiều dài và bề rộng, thường 6 mm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pulley GT2&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pulley GT2&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;8.000–30.000&lt;/td&gt;
      &lt;td&gt;Số răng là tỉ số; khớp lỗ trục&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ thanh răng và bánh răng&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thanh răng bánh răng&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;30.000–90.000&lt;/td&gt;
      &lt;td&gt;Tùy chọn; đường kính vòng chia đi vào \(x = \pi d\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Các trang tìm mua:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=d%C3%A2y+%C4%91ai+GT2&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=d%C3%A2y%20%C4%91ai%20GT2&quot;&gt;Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=d%C3%A2y%20%C4%91ai%20GT2&quot;&gt;Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=GT2&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Giá bán có thể thay đổi theo thời điểm. Tránh mua các tin đăng pulley không ghi rõ số răng và đường kính lỗ trục.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=EBqrDj-Wkiw&quot;&gt;Four-Bar Linkage and Its Three Possible Motions&lt;/a&gt; — mecademie, tiếng Anh. Giới thiệu cơ cấu bốn thanh và ba kiểu chuyển động đặc trưng; hãy đếm số khâu trước khi dự đoán quỹ đạo đầu khâu.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=1Ty_1LF3Qv0&quot;&gt;Basic Mechanisms:  4 Bar Linkage&lt;/a&gt; — Mr. Dillman’s Tech Channel, tiếng Anh. Video ngắn minh họa cơ cấu bốn thanh hoạt động thực tế để dễ nhận diện khâu cố định.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Các video ngắn (Facebook Reel) dưới đây của Hoàng Quân (Cơ điện tử ICTU, &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1615249330240344&quot;&gt;Test thiết kế chân&lt;/a&gt; — Chú thích: test thiết kế chân. Minh họa việc kiểm tra cơ cấu chân trực tiếp trên bàn trước khi ghép thành chuỗi động học hoàn chỉnh.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/2974137376266883&quot;&gt;Một chân, ba bậc tự do&lt;/a&gt; — Chú thích: một chân có 3 bậc tự do. Ba khớp tương ứng với ba biến khớp; phần biến đổi sang quỹ đạo đầu chân sẽ được học ở các chương sau.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Cho cơ cấu bốn thanh có chiều dài các khâu lần lượt là 25 mm, 40 mm, 45 mm và 60 mm. Điều kiện \(s + l &amp;lt; p + q\) có thỏa mãn không? Nếu cố định khâu 45 mm và dẫn động khâu 25 mm, bạn dự đoán chuyển động gì?&lt;/li&gt;
  &lt;li&gt;Cho bốn khâu dài 40 mm, 50 mm, 55 mm và 120 mm. Cơ cấu này có khâu maniven nào quay đủ vòng không?&lt;/li&gt;
  &lt;li&gt;Một hệ truyền động gồm hộp giảm tốc đóng dấu 1:30 nối tiếp với pulley động cơ 16 răng kéo pulley bánh xe 48 răng. Tính tỉ số truyền tổng \(i\) từ trục động cơ tới bánh xe.&lt;/li&gt;
  &lt;li&gt;Đường kính vòng chia của bánh răng dẫn là 20 mm. Thanh răng dịch chuyển bao xa trong một vòng quay và trong một phần tư vòng quay?&lt;/li&gt;
  &lt;li&gt;Một cam có đoạn nâng kéo dài \(90^\circ\) và đoạn dừng kéo dài \(180^\circ\) tiếp theo. Con đội làm gì trong đoạn dừng, và Capstone A có cần cơ cấu này không?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Ta có \(s + l = 25 + 60 = 85\) mm và \(p + q = 40 + 45 = 85\) mm. Trường hợp dấu bằng là cơ cấu điểm đổi (sat biên Grashof), dễ bị duỗi phẳng và kẹt. Khâu ngắn vẫn có xu hướng làm maniven nhưng thiếu ổn định; nên tăng nhẹ chiều dài một khâu trung gian nếu cần cơ cấu quay tin cậy.&lt;/li&gt;
  &lt;li&gt;Ta có \(s + l = 160\) mm và \(p + q = 105\) mm (\(s + l &amp;gt; p + q\)), nên không khâu nào quay được đủ vòng.&lt;/li&gt;
  &lt;li&gt;Tỉ số bộ đai là \(i_{\text{belt}} = 48/16 = 3\); nhân với hộp giảm tốc 30 cho tỉ số tổng \(i = 90\).&lt;/li&gt;
  &lt;li&gt;Hành trình mỗi vòng là \(x = \pi \times 20 \approx 62.8\) mm; một phần tư vòng tương ứng khoảng 15,7 mm.&lt;/li&gt;
  &lt;li&gt;Trong đoạn dừng, con đội đứng yên dù cam vẫn đang quay. Capstone A không cần cơ cấu cam.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;thực-hành-thêm-teardown--mô-phỏng-cơ-cấu-3d&quot;&gt;Thực hành thêm: Teardown — mô phỏng cơ cấu 3D&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/elvarofficial/teardown&quot;&gt;Teardown&lt;/a&gt; (elvarofficial, giấy phép MIT) là skill mã nguồn mở cho Claude Code: mô tả một máy bằng lời, nhận về một file HTML chạy offline (khoảng 800 KB, three.js). Máy tự tháo rồi lắp lại; bấm vào bộ phận thì phần đang làm việc mở ra, kèm bảng số liệu chạy theo chuyển động. Tư thế được tính từ kích thước thật và một đầu vào duy nhất, ví dụ góc trục khuỷu, hành trình hoặc gió. Repo có sẵn động cơ V8, càng đáp Cessna 337 (thanh răng–bánh răng) và chân đi bộ Strandbeest 20 chân. Cài bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npx skills add elvarofficial/teardown -g -a claude-code&lt;/code&gt;, rồi trong thư mục đã cài chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm install &amp;amp;&amp;amp; npx playwright install chromium&lt;/code&gt; (cần Node.js 20 trở lên). Ba ví dụ có sẵn dựng được bằng Node, không cần Claude Code.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Dựng ví dụ càng đáp: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node sims/build.mjs cessna-live&lt;/code&gt;. Thanh răng 201 mm ăn bánh 32 răng, module 4 mm, quay đúng 180°. Kiểm tra tay: đường kính vòng chia \(32 \times 4 = 128\) mm, nửa chu vi \(\pi \times 128 / 2 \approx 201\) mm — cùng công thức hành trình \(x = \pi d\) ở trên.&lt;/li&gt;
  &lt;li&gt;Nhờ Teardown dựng cơ cấu bốn thanh với đúng chiều dài ví dụ (30, 55, 70, 90 mm), hoặc một chân Strandbeest. Đối chiếu góc và hành trình trên bảng số liệu với phép tính tay (điều kiện Grashof, quỹ đạo đầu khâu).&lt;/li&gt;
  &lt;li&gt;Số trên bảng do mô hình AI viết ra, nên hãy đối chiếu với sách hoặc phép tính của bạn trước khi dùng để dạy.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Four-bar_linkage&quot;&gt;Four-bar linkage&lt;/a&gt; — phân loại maniven – cần lắc và sự thay đổi chuyển động khi đổi khâu cố định.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Grashof%27s_law&quot;&gt;Grashof’s law&lt;/a&gt; — chi tiết phép thử điều kiện \(s + l\), bao gồm trường hợp dấu bằng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Timing_belt_(camshaft)&quot;&gt;Timing belt (đai răng)&lt;/a&gt; — nguyên lý ăn khớp răng và lý do đai chùng bị nhảy răng (bài viết dùng ví dụ động cơ đốt trong nhưng nguyên lý ăn khớp tương tự đai GT2).&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Bánh răng: trụ, worm, hành tinh, tỉ số, backlash</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_03_gear_trains_ratios/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_03_gear_trains_ratios</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;80–100 phút&lt;/strong&gt;, bao gồm thời gian đếm răng trực tiếp trên hộp giảm tốc bạn đang có hoặc trên một ảnh chụp rõ nét của hộp số đó.&lt;/p&gt;

&lt;p&gt;Một động cơ DC nhỏ thường quay tít mù ở tốc độ \(6\,000\text{ RPM}\) nhưng lại yếu đến mức chỉ cần hai ngón tay bóp nhẹ là đứng khựng lại. Làm sao biến sự “nhanh mà yếu” đó thành chuyển động “chậm mà khỏe” đủ đẩy cả con robot leo dốc? Câu trả lời cổ điển nhất của kỹ thuật cơ khí là &lt;strong&gt;hộp số bánh răng (gear train)&lt;/strong&gt; — bộ biến áp cơ học đánh đổi trực tiếp tốc độ góc lấy mô-men xoắn theo định luật bảo toàn công suất.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-các-họ-bánh-răng-tỉ-số-truyền-và-độ-rơ-backlash&quot;&gt;Kiến thức cần nhớ: Các họ bánh răng, Tỉ số truyền và Độ rơ (Backlash)&lt;/h2&gt;

&lt;h3 id=&quot;1-bánh-răng-trụ-spur-trục-vít-worm-và-hành-tinh-planetary&quot;&gt;1. Bánh răng trụ (Spur), Trục vít (Worm) và Hành tinh (Planetary)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Bánh răng trụ (Spur):&lt;/strong&gt; dễ chế tạo, hiệu suất cao (~90%–95% mỗi cấp), nhưng ồn ở tốc độ cao và tốn diện tích khi cần tỉ số lớn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trục vít – bánh vít (Worm):&lt;/strong&gt; đạt tỉ số truyền cực lớn (20:1 đến 60:1) trong 1 cấp và có tính &lt;strong&gt;tự hãm (self-locking)&lt;/strong&gt; khi mất điện (tay robot không bị rơi tự do), nhưng hiệu suất thấp (40%–70%) do ma sát trượt lớn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bánh răng hành tinh (Planetary):&lt;/strong&gt; đồng trục, chia tải đều lên 3–4 bánh răng vệ tinh nên cực gọn, chịu mô-men rất lớn và độ rơ thấp — tiêu chuẩn vàng cho khớp cánh tay robot và xe tự hành hiệu suất cao.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-tỉ-số-truyền-g-và-hiệu-suất-nối-tiếp-eta&quot;&gt;2. Tỉ số truyền \(G\) và hiệu suất nối tiếp \(\eta\)&lt;/h3&gt;
&lt;p&gt;Khi bánh chủ động có \(Z_{\text{in}}\) răng ăn khớp với bánh bị động \(Z_{\text{out}}\) răng, tỉ số truyền là \(G = Z_{\text{out}} / Z_{\text{in}}\). Tốc độ giảm \(G\) lần (\(\omega_{\text{out}} = \omega_{\text{in}} / G\)), còn mô-men tăng lên \(\tau_{\text{out}} = \tau_{\text{in}} \cdot G \cdot \eta\) (với \(\eta &amp;lt; 1\) là hiệu suất do ma sát). Nhiều cấp nối tiếp thì &lt;strong&gt;nhân&lt;/strong&gt; cả tỉ số truyền lẫn hiệu suất!&lt;/p&gt;

&lt;h3 id=&quot;3-độ-rơ-bánh-răng-backlash--kẻ-thù-thầm-lặng-của-độ-chính-xác&quot;&gt;3. Độ rơ bánh răng (Backlash) — kẻ thù thầm lặng của độ chính xác&lt;/h3&gt;
&lt;p&gt;Giữa hai răng ăn khớp luôn phải có một khe hở nhỏ để bôi trơn và tránh kẹt nhiệt. Khi động cơ đảo chiều, trục ra đứng yên một khoảnh khắc nhỏ trước khi răng bên kia chạm vào — góc rơ tự do đó gọi là &lt;strong&gt;backlash&lt;/strong&gt;. Trên cánh tay robot dài, chỉ \(1^\circ\) backlash ở khớp vai đã tạo ra sai số vài milimet ở đầu kẹp!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-hộp-số-2-cấp-và-sai-số-đầu-tay-kẹp-do-backlash&quot;&gt;Giải thích: Cầm tay tính hộp số 2 cấp và sai số đầu tay kẹp do Backlash&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Hộp số bánh răng trụ 2 cấp:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Cấp 1: bánh dẫn \(Z_1 = 12\) răng ăn khớp bánh bị dẫn \(Z_2 = 36\) răng \(\implies G_1 = 36/12 = 3{:}1\).&lt;/li&gt;
  &lt;li&gt;Cấp 2: bánh dẫn \(Z_3 = 12\) răng ăn khớp bánh bị dẫn \(Z_4 = 48\) răng \(\implies G_2 = 48/12 = 4{:}1\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tổng tỉ số truyền:&lt;/strong&gt; \(G_{\text{tổng}} = G_1 \times G_2 = 3 \times 4 = 12{:}1\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Giả sử động cơ đầu vào quay &lt;strong&gt;\(3\,600\text{ RPM}\)&lt;/strong&gt; với mô-men &lt;strong&gt;\(0{,}05\text{ N}\cdot\text{m}\)&lt;/strong&gt;, mỗi cấp ăn khớp đạt hiệu suất &lt;strong&gt;\(\eta = 90\% = 0{,}90\)&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tốc độ trục ra:&lt;/strong&gt; \(\omega_{\text{out}} = \frac{3\,600}{12} = 300\text{ RPM}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tổng hiệu suất 2 cấp:&lt;/strong&gt; \(\eta_{\text{tổng}} = 0{,}90 \times 0{,}90 = 0{,}81\;(81\%)\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mô-men trục ra thực tế:&lt;/strong&gt; \(\tau_{\text{out}} = 0{,}05 \times 12 \times 0{,}81 = 0{,}486\text{ N}\cdot\text{m}\) (mạnh hơn gần &lt;strong&gt;10 lần&lt;/strong&gt; động cơ gốc!).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Tác động của \(1{,}0^\circ\) backlash lên cánh tay dài \(L = 200\text{ mm}\):&lt;/strong&gt;
Đổi \(1{,}0^\circ\) sang radian: \(\Delta\theta = 1{,}0 \times \frac{\pi}{180} \approx 0{,}01745\text{ rad}\).
Độ rơ lắc tự do ngay tại đầu gắp:
\(\Delta x = L \cdot \Delta\theta = 200 \times 0{,}01745 \approx 3{,}49\text{ mm}!\)
Thấy rõ ngay: dù encoder trục động cơ chính xác tới \(0{,}01^\circ\), độ rơ cơ khí \(1^\circ\) trong hộp số vẫn làm đầu tay kẹp lệch gần \(3{,}5\text{ mm}\) nếu bạn không đặt encoder ở trục ra hoặc dùng hộp số độ rơ thấp!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, các bạn tính được tỉ số truyền từ số răng của từng cặp bánh răng, dự đoán góc quay đầu ra tương ứng với một góc quay đầu vào cho trước, và xác định đúng chiều quay qua mỗi cặp ăn khớp ngoài. Các bạn cũng phân biệt được công dụng của bộ truyền trục vít – bánh vít (worm), nắm được công thức tỉ số truyền hành tinh trong giáo trình đồng thời biết tin vào tỉ số đóng dấu trên vỏ động cơ TT, và nhận diện độ rơ (backlash) như phần chuyển động bị trễ xuất hiện dưới dạng xung encoder khi robot đảo chiều.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Các bạn đã biết đo khổ vết và bán kính lăn (Bài 01) và hiểu vít cố định trục phải tỳ đúng vào mặt vát trục chữ D (Bài 02). Số răng là số nguyên, nên chỉ cần các phép nhân chia cơ bản trên máy tính bỏ túi.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Encoder (Chương 04) thường đếm số vòng quay trên trục động cơ hoặc một trục trung gian trong hộp số, trong khi bánh xe chạm sàn đã được giảm tốc qua tỉ số truyền \(i\). Bộ điều khiển &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; chỉ quy đổi chính xác số xung thành mét khi cả tỉ số truyền \(i\) và bán kính lăn \(r\) đều đúng.&lt;/p&gt;

&lt;p&gt;Nếu nhầm lẫn giữa bánh đầu vào và bánh đầu ra, bạn sẽ đảo ngược giá trị \(i\). Khi đó, quãng đường odometry bị sai lệch theo hệ số \(i^2\): chẳng hạn tỉ số thật bằng 3 nhưng lưu nhầm thành \(1/3\) sẽ làm quãng đường ước lượng bị kéo giãn hoặc co ngắn tới 9 lần. Tương tự như lỗi khổ vết ở Bài 01 khiến xe chạy lệch thành đường cong, lỗi tỉ số truyền sẽ làm toàn bộ tỉ lệ bản đồ bị sai lệch. Hãy ghi \(i\) ngay cạnh \(b\) và \(r\) trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;cặp-ăn-worm-hành-tinh-và-vùng-chết&quot;&gt;Cặp ăn, worm, hành tinh, và vùng chết&lt;/h2&gt;

&lt;p&gt;Với một cặp bánh răng trụ ăn khớp, tỉ số truyền được định nghĩa là:&lt;/p&gt;

\[i = \frac{N_{\text{out}}}{N_{\text{in}}} = \frac{\omega_{\text{in}}}{\omega_{\text{out}}}.\]

&lt;p&gt;Trong đó \(N\) là số răng. Khi tốc độ góc giảm, mô-men xoắn ở đầu ra tăng lên tương ứng (sau khi trừ tổn hao ma sát tại bề mặt răng và bạc đạn). Ví dụ, bánh dẫn 12 răng ăn khớp với bánh bị dẫn 36 răng cho tỉ số \(i = 36/12 = 3\): động cơ quay ba vòng thì bánh xe quay một vòng, và mô-men tại bánh xe xấp xỉ gấp ba lần mô-men trục dẫn (thực tế nhỏ hơn một chút do ma sát).&lt;/p&gt;

&lt;p&gt;Mỗi cặp bánh răng ăn khớp ngoài sẽ làm đảo chiều quay, tức bánh bị dẫn quay ngược chiều bánh dẫn. Khi nối tiếp cặp ăn khớp thứ hai (bánh dẫn qua bánh trung gian rồi sang bánh ra), chiều quay bị đảo hai lần nên đầu ra quay cùng chiều với đầu vào. Một bánh trung gian đơn chỉ có tác dụng đổi chiều quay mà không làm thay đổi tỉ số truyền. Tuy nhiên, nếu hai bánh có số răng khác nhau được cố định trên cùng một trục (bánh răng kép), tỉ số truyền của các cấp sẽ nhân với nhau. Tương tự, nếu hệ truyền động có thêm bộ truyền đai (Bài 04) sau hộp giảm tốc, tỉ số truyền tổng tại bánh xe là:&lt;/p&gt;

\[i_{\text{wheel}} = i_{\text{gearbox}} \cdot i_{\text{belt}}.\]

&lt;p&gt;Bỏ sót một trong hai thừa số là lỗi sai tỉ lệ thường gặp nhất.&lt;/p&gt;

&lt;p&gt;Bộ truyền trục vít – bánh vít (worm) gồm một trục vít ăn khớp với bánh vít. Cơ cấu này đạt tỉ số truyền rất cao (thường hàng chục lần chỉ trong một cấp) và nhiều cặp có tính tự hãm (không thể xoay ngược từ phía bánh vít), giúp tay máy nhỏ giữ nguyên tư thế ngay cả khi mất nguồn động cơ. Nhược điểm là hiệu suất thấp: phần lớn năng lượng điện dự trù ở Chương 05 bị chuyển thành nhiệt do ma sát trượt. Vì vậy, bộ truyền worm thích hợp cho cơ cấu kẹp cần giữ chặt, nhưng không phù hợp cho bánh chủ động của Capstone A — nơi ta cần chuyển tối đa năng lượng pin thành chuyển động. Bạn có thể thử xoay trục đầu ra của một bộ worm bằng tay: thông thường bánh vít sẽ không quay được.&lt;/p&gt;

&lt;p&gt;Bộ truyền bánh răng hành tinh gom bánh răng mặt trời (sun), các bánh răng hành tinh trên cần mang (carrier) và vành răng ngoài (ring) vào gọn trong cùng một đường kính. Trong cấu hình cơ bản ở giáo trình (vành răng cố định, đầu vào là bánh mặt trời, đầu ra là cần mang), tỉ số truyền là:&lt;/p&gt;

\[i = 1 + \frac{N_{\text{ring}}}{N_{\text{sun}}}.\]

&lt;p&gt;Công thức trên áp dụng cho một cấp hành tinh đơn. Các hộp giảm tốc TT hoặc hộp số kim loại thực tế có thể gồm nhiều cấp bánh răng trụ hoặc nhiều tầng hành tinh xếp chồng, và con số đóng dấu trên vỏ (như 1:48, 1:90 hay 1:120) đã tính gộp tất cả các cấp bên trong. Hãy dùng công thức đếm răng khi bạn quan sát trực tiếp được bánh răng, và tin vào tỉ số đóng dấu khi hộp số đóng kín.&lt;/p&gt;

&lt;p&gt;Độ rơ (backlash) là khoảng chuyển động bị mất khi mô-men đảo chiều. Răng bánh răng luôn được chế tạo có khe hở nhỏ để tránh kẹt khi ăn khớp, và khe hở này tạo thành một khoảng trễ mỗi khi đổi chiều quay. Nếu giữ cố định trục động cơ rồi lắc nhẹ trục đầu ra, bạn sẽ thấy bánh xe dịch chuyển vài độ trước khi trục động cơ bắt đầu chịu lực. Các xung encoder sinh ra trong khoảng rơ này gây ra một độ lệch nhỏ không tương ứng với chuyển động thực trên sàn. Ta không thể loại bỏ hoàn toàn độ rơ ở hộp số TT nhựa đúc; thay vào đó, cần ước lượng góc rơ bằng mắt để ghi nhận và tránh các thuật toán điều khiển đảo chiều liên tục trong thời gian vài miligiây.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/gear_train_ratio.png&quot; alt=&quot;Chuỗi bánh răng kép: số răng đặt tỉ số, mỗi cặp ăn ngoài đảo chiều&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hình trên cho thấy tầm quan trọng của việc xác định đúng bánh đầu vào trước khi chia. Hãy lần theo đường truyền mô-men từ động cơ ra bánh xe, nhân lần lượt \(N_{\text{out}}/N_{\text{in}}\) qua từng cấp và đếm số cặp ăn khớp ngoài để biết chiều quay cuối cùng. Một bánh trung gian đơn nằm giữa hai bánh chỉ giúp đảo chiều, không thêm thừa số vào tỉ số truyền trừ khi nó là bánh răng kép có hai vòng răng khác số răng.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Xét cặp bánh răng có bánh dẫn \(N_{\text{in}} = 12\) và bánh bị dẫn \(N_{\text{out}} = 36\):&lt;/p&gt;

\[i = \frac{36}{12} = 3.\]

&lt;p&gt;Khi xoay bánh dẫn một góc \(90^\circ\), bánh đầu ra sẽ quay:&lt;/p&gt;

\[\theta_{\text{out}} = \frac{90^\circ}{3} = 30^\circ,\]

&lt;p&gt;theo chiều ngược lại vì đây là một cặp ăn khớp ngoài. Ba vòng đầy đủ của động cơ (\(1080^\circ\)) tạo ra đúng một vòng quay bánh xe. Nếu gọi nhầm tên đầu vào/đầu ra và lưu \(i = 12/36 = 1/3\), quãng đường tính toán sẽ lệch theo hệ số:&lt;/p&gt;

\[i_{\text{true}}^{2} = 9,\]

&lt;p&gt;tức dài gấp 9 lần hoặc ngắn chỉ còn \(1/9\) tùy bước nhân chia trong phần mềm. Cách tránh nhầm lẫn đơn giản nhất là luôn chỉ rõ bánh phía động cơ là “đầu vào” trước khi lập tỉ số.&lt;/p&gt;

&lt;p&gt;Với động cơ TT đóng dấu 1:48, hãy ghi trực tiếp giá trị 48 vào sổ tay dù bạn biết công thức hành tinh \(1 + N_{\text{ring}}/N_{\text{sun}}\), vì con số nhà sản xuất đã gộp toàn bộ các tầng giảm tốc bên trong.&lt;/p&gt;

&lt;h2 id=&quot;lab-đếm-dự-đoán-đối-chiếu&quot;&gt;Lab: đếm, dự đoán, đối chiếu&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Đảm bảo đã tắt nguồn. Răng bánh răng có thể kẹp ngón tay khi quay. Nếu mở hộp số, mỡ bôi trơn và các phe cài nhỏ rất dễ rơi bắn, nên đếm răng qua ảnh chụp sắc nét là đủ cho bài này. Tuyệt đối không cấp nguồn pin cho động cơ khi tay đang chạm vào cặp bánh răng.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Một hộp giảm tốc, một cặp bánh rời, hoặc một ảnh sắc của bánh các bạn có&lt;/td&gt;
      &lt;td&gt;Số răng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bút và giấy&lt;/td&gt;
      &lt;td&gt;Tỉ số và góc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Con số ROS sẽ dùng lại&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Xác định rõ bánh đầu vào (phía động cơ) và bánh đầu ra (phía bánh xe).&lt;/li&gt;
  &lt;li&gt;Đếm số răng của từng bánh. Với động cơ TT vỏ kín, đọc tỉ số đóng dấu (ví dụ 1:48) và ghi chú rõ là số liệu đọc từ nhãn vỏ.&lt;/li&gt;
  &lt;li&gt;Tính \(i = N_{\text{out}}/N_{\text{in}}\) hoặc chép lại tỉ số đóng dấu, đồng thời đếm số cặp ăn khớp ngoài để xác định chiều quay.&lt;/li&gt;
  &lt;li&gt;Dự đoán góc quay đầu ra khi trục vào quay \(90^\circ\) theo công thức \(\theta_{\text{out}} = 90^\circ / i\).&lt;/li&gt;
  &lt;li&gt;Nếu có bánh răng thực tế, xoay trục đầu vào khoảng một phần tư vòng (dựa theo vạch đánh dấu) để đối chiếu với dự đoán. Sau đó giữ trục vào, lắc nhẹ trục ra và ước lượng độ rơ theo độ.&lt;/li&gt;
  &lt;li&gt;Nếu chỉ làm việc trên bản vẽ hoặc ảnh chụp, bản tính dự đoán trên giấy là kết quả nộp; nhớ ghi chú nhãn “bản vẽ”.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Một giá trị \(i\) cụ thể, ghi chú chiều quay và góc dự đoán trong sổ tay. Với cặp 12 và 36 răng, kết quả là \(i = 3\) và \(\theta_{\text{out}} = 30^\circ\) ngược chiều đầu vào. Ở hộp số nhựa đúc thực tế, tỉ số đo thực nghiệm ở Bài 08 thường sai lệch không quá 10% so với dấu đóng, và độ rơ vài độ tại trục bánh là bình thường.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Các bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường nghĩa là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Tỉ số ra nhỏ hơn 1 trên hộp số lẽ ra phải làm chậm động cơ&lt;/td&gt;
      &lt;td&gt;Các bạn đã gọi bánh phía xe là đầu vào&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chiều bánh xe làm các bạn ngạc nhiên&lt;/td&gt;
      &lt;td&gt;Quên một cặp ăn ngoài, hoặc đếm bánh trung gian như một lần đổi tỉ số&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đầu ra dịch vài độ trước khi đầu vào buộc phải động&lt;/td&gt;
      &lt;td&gt;Độ rơ; ghi lại, đừng “sửa” bằng keo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dấu đóng nói 1:48 mà công thức một cấp của các bạn ra 4&lt;/td&gt;
      &lt;td&gt;Các bạn đang nhìn một cấp của một chồng; tin dấu đóng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Bộ truyền bánh răng của Capstone A đã tích hợp sẵn bên trong động cơ giảm tốc TT. Hãy chọn mua động cơ có ghi rõ tỉ số truyền trên vỏ và có sẵn encoder (nếu tìm được) để kết nối với mạch đếm xung của Chương 04. Bộ bánh răng nhựa rời chỉ cần thiết nếu bạn muốn thực hành đếm răng trực tiếp khi hộp số động cơ bị kín.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mua gì&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ giảm tốc TT có encoder&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ giảm tốc TT encoder&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;45.000–120.000 mỗi cái&lt;/td&gt;
      &lt;td&gt;Đọc dấu đóng: 1:48 là phổ biến&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ bánh răng nhựa&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh răng nhựa robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;20.000–60.000&lt;/td&gt;
      &lt;td&gt;Hữu ích khi hộp kín mà vẫn muốn đếm răng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Các trang tìm mua:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=%C4%91%E1%BB%99ng+c%C6%A1+gi%E1%BA%A3m+t%E1%BB%91c+TT&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=%C4%91%E1%BB%99ng%20c%C6%A1%20gi%E1%BA%A3m%20t%E1%BB%91c%20TT%20encoder&quot;&gt;Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=%C4%91%E1%BB%99ng%20c%C6%A1%20TT%20encoder&quot;&gt;Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=%C4%91%E1%BB%99ng+c%C6%A1+gi%E1%BA%A3m+t%E1%BB%91c&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Giá bán có thể thay đổi theo thời điểm. Hãy kiểm tra đường kính và kiểu trục khớp với bánh xe đã mua ở Bài 01 trước khi đặt hàng.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=aGHrhm-Rcp0&quot;&gt;Gear Train Design - How to calculate gear trains mechanical engineering&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Cách tính tỉ số truyền của một chuỗi bánh răng bằng cách nhân từng cặp, khớp với phương pháp trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=NmUq2TQ8m2M&quot;&gt;Compound Gears Explained:  Calculate Gear Ratio&lt;/a&gt; — INTEGRAL PHYSICS, tiếng Anh. Giải thích bánh răng kép và cách xác định hai bánh nào nằm chung một trục — vị trí rất dễ đếm nhầm.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=K8HQA1Uz1sE&quot;&gt;Worm Gears Explained, Calculated &amp;amp; Modeled&lt;/a&gt; — INTEGRAL PHYSICS, tiếng Anh. Nguyên lý truyền một chiều và tỉ số truyền lớn của trục vít – bánh vít.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một bánh dẫn 15 răng ăn khớp với bánh bị dẫn 60 răng. Tính tỉ số truyền \(i\), góc quay đầu ra khi động cơ quay đúng một vòng và chiều quay so với động cơ.&lt;/li&gt;
  &lt;li&gt;Nếu chèn thêm một bánh trung gian đơn 20 răng giữa bánh 15 răng và bánh 60 răng (bánh dẫn chỉ ăn khớp với bánh trung gian), tỉ số \(i\) và chiều quay đầu ra thay đổi thế nào?&lt;/li&gt;
  &lt;li&gt;Một cấp bánh răng hành tinh có vành cố định, \(N_{\text{sun}} = 12\) và \(N_{\text{ring}} = 36\). Tính \(i\) theo công thức giáo trình. Nếu động cơ TT của bạn đóng dấu 1:48, con số nào cần ghi vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;?&lt;/li&gt;
  &lt;li&gt;Nếu bạn lưu nhầm \(i = 1/4\) cho một hộp số có tỉ số thực bằng 4, quãng đường odometry sẽ sai lệch theo hệ số nào?&lt;/li&gt;
  &lt;li&gt;Khi giữ cố định trục động cơ, bánh xe vẫn lắc được khoảng \(8^\circ\). Hiện tượng này gọi là gì và nó ảnh hưởng ra sao đến số xung đo được khi robot bắt đầu quay?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(i = 60/15 = 4\). Một vòng động cơ tạo ra góc đầu ra \(360^\circ/4 = 90^\circ\), ngược chiều động cơ (do có một cặp ăn khớp ngoài).&lt;/li&gt;
  &lt;li&gt;Bánh trung gian đơn giữ nguyên \(i = 4\) nhưng thêm một lần đảo chiều, nên đầu ra quay cùng chiều với bánh dẫn.&lt;/li&gt;
  &lt;li&gt;Theo công thức một cấp hành tinh: \(i = 1 + 36/12 = 4\). Với động cơ TT đã đóng dấu, ghi giá trị 48 vì dấu đóng đã tính gộp toàn bộ các tầng bánh răng bên trong.&lt;/li&gt;
  &lt;li&gt;Sai lệch theo hệ số \(i^2 = 16\).&lt;/li&gt;
  &lt;li&gt;Đó là độ rơ (backlash). Góc rơ này có thể sinh xung ảo hoặc tạo khoảng trễ trước khi bánh thực sự dịch chuyển, làm sai lệch giai đoạn đầu của cú quay. Cần ghi nhận góc rơ, không đổ keo vào bánh răng để cố khử.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;thực-hành-thêm-berkeley-humanoid-lite--hộp-số-cycloid-in-3d&quot;&gt;Thực hành thêm: Berkeley Humanoid Lite — hộp số cycloid in 3D&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt; (Hybrid Robotics, UC Berkeley) là robot hình người mã nguồn mở, in 3D, cao 0,8 m, nặng 16 kg, chi phí phần cứng dưới 5.000 USD theo giá thị trường Mỹ. Khớp dùng hai cỡ actuator hộp số cycloid in 3D (6512 và 5010); linh kiện còn lại mua sẵn trên các sàn thương mại điện tử. Mã nguồn MIT, tài nguyên khác CC BY-SA 4.0.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Ở bộ truyền cycloid một cấp điển hình, đĩa có \(Z\) thùy (lobe) và vành cố định có \(Z+1\) chốt (pin/roller) thì tỉ số truyền là \(i = Z\): một vòng trục lệch tâm làm đĩa tiến \(1/Z\) vòng. &lt;a href=&quot;https://berkeley-humanoid-lite.gitbook.io/docs/in-depth-contents/motor-characterization&quot;&gt;Ghi chú đặc tính động cơ&lt;/a&gt; nhân quán tính phản hồi với \(15^2\), tức cả hai cỡ hộp dùng \(i = 15\). Hãy mở bản CAD hoặc file in 3D của 6512/5010 và kiểm tra xem \(Z\) có đúng 15 không.&lt;/li&gt;
  &lt;li&gt;Ước lượng bậc độ lớn cho khớp gối: nếu chân trụ phải chống gần như toàn bộ 16 kg và cánh tay đòn ngang chỉ khoảng 0,15–0,20 m, mô-men tĩnh đã vào khoảng \(16 \times 9{,}81 \times 0{,}15 \approx 24\) N·m đến khoảng 31 N·m — cùng tầm dải 0–20 N·m dùng khi đo độ cứng actuator 6512 trong bài RSS 2025. Đây là ước lượng trên giấy, không phải số đo trên robot.&lt;/li&gt;
  &lt;li&gt;Bài kiểm bền 60 giờ cho thấy độ rơ tăng dần do mòn. Ghi chú phát hành v1.1.0 (07/09/2025) cho biết hướng V2 đang xét actuator mua sẵn, vì hộp in 3D còn yếu cho các bài nặng. Phần sim-to-real nằm ở bài 10-05; đủ nguồn ở trang tham khảo của khóa học.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.sdp-si.com/resources/elements-of-metric-gear-technology/index.php&quot;&gt;SDP-SI, Elements of Metric Gear Technology&lt;/a&gt; — tài liệu chuyên sâu về hình học răng, tỉ số truyền và độ rơ từ nhà sản xuất bánh răng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Gear_ratio&quot;&gt;Gear ratio&lt;/a&gt; — định nghĩa tỉ số truyền \(N_{\text{out}}/N_{\text{in}}\) và cách tính chuỗi bánh răng kép.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Worm_drive&quot;&gt;Worm drive&lt;/a&gt; — đặc điểm tỉ số truyền cao, hiệu suất và cơ chế tự hãm của bộ truyền trục vít – bánh vít.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Ốc vít, bạc đạn, trục và khớp nối</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_02_fasteners_bearings_shafts/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_02_fasteners_bearings_shafts</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;70–90 phút&lt;/strong&gt;, bao gồm thời gian phân loại một bộ ốc nhỏ và chụp ảnh so sánh đai ốc nyloc với đai ốc thường.&lt;/p&gt;

&lt;p&gt;Phần mềm điều khiển của bạn có thể tinh vi đến đâu đi nữa, nhưng nếu một con vít chí (set screw) trên khớp nối bánh xe bị tuôn ren sau 10 phút rung lắc, cả con robot lập tức đứng im bất lực. Những chi tiết cơ khí tưởng như “lặt vặt” nhất — &lt;strong&gt;ốc vít hệ mét, vòng bi (bạc đạn), trục D-shaft và khớp nối mềm&lt;/strong&gt; — lại chính là nguyên nhân số một gây hỏng vặt trên các dự án robot sinh viên. Nắm vững ngôn ngữ tiêu chuẩn của chúng giúp bạn lắp ráp chắc chắn, đồng tâm và dễ bảo trì.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-tiêu-chuẩn-ren-hệ-mét-ổ-lăn-và-khớp-nối-trục&quot;&gt;Kiến thức cần nhớ: Tiêu chuẩn ren hệ mét, Ổ lăn và Khớp nối trục&lt;/h2&gt;

&lt;h3 id=&quot;1-quy-ước-ốc-vít-hệ-mét-m3-m4-và-chống-tự-tháo-do-rung&quot;&gt;1. Quy ước ốc vít hệ mét (M3, M4) và chống tự tháo do rung&lt;/h3&gt;
&lt;p&gt;Ký hiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M3 × 12&lt;/code&gt; nghĩa là đường kính danh nghĩa &lt;strong&gt;\(3\text{ mm}\)&lt;/strong&gt;, chiều dài thân ren &lt;strong&gt;\(12\text{ mm}\)&lt;/strong&gt;, bước ren thô tiêu chuẩn &lt;strong&gt;\(0{,}5\text{ mm/vòng}\)&lt;/strong&gt;. Trên robot có động cơ rung liên tục, đai ốc thường sẽ tự xoay lỏng rất nhanh — giải pháp chuẩn là dùng &lt;strong&gt;đai ốc chống trượt có vòng nylon (nyloc)&lt;/strong&gt;, long-đen vênh, hoặc keo khóa ren tháo được (Loctite xanh 243; &lt;em&gt;tránh dùng keo đỏ trên ốc nhỏ M3&lt;/em&gt;).&lt;/p&gt;

&lt;h3 id=&quot;2-bạc-đạn-vòng-bi--đỡ-tải-hướng-kính-và-giảm-mô-men-uốn-lên-trục-động-cơ&quot;&gt;2. Bạc đạn (Vòng bi) — đỡ tải hướng kính và giảm mô-men uốn lên trục động cơ&lt;/h3&gt;
&lt;p&gt;Trục hộp số động cơ nhỏ không được thiết kế để gánh tải trọng nặng treo lơ lửng quá xa vỏ hộp số. &lt;strong&gt;Vòng bi cầu rãnh sâu&lt;/strong&gt; (như 608ZZ, 688ZZ) thay ma sát trượt bằng ma sát lăn cực nhỏ, đỡ tải hướng kính (radial load) và giữ trục đồng tâm, bảo vệ bánh răng bên trong động cơ khỏi bị mòn lệch.&lt;/p&gt;

&lt;h3 id=&quot;3-trục-vát-cạnh-d-shaft-và-khớp-nối-mềm-flexible-coupling&quot;&gt;3. Trục vát cạnh (D-Shaft) và Khớp nối mềm (Flexible Coupling)&lt;/h3&gt;
&lt;p&gt;Bắt vít chí lên trục tròn trơn rất dễ trượt xoay khi gặp mô-men lớn; trục &lt;strong&gt;D-shaft&lt;/strong&gt; (vát một mặt phẳng) khóa chặt mô-men xoay cơ học. Khi nối hai trục độc lập (ví dụ trục động cơ nối trục vitme), luôn dùng &lt;strong&gt;khớp nối mềm&lt;/strong&gt; (khớp nối lò xo nhôm hoặc khớp nối cao su jaw coupling) để hấp thụ độ lệch tâm nhỏ \(0{,}1\text{–}0{,}3\text{ mm}\) thay vì ép cứng làm kẹt và nóng động cơ.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-kiểm-tra-chiều-dài-ăn-ren-m3-và-mô-men-uốn-trên-trục-bánh-xe&quot;&gt;Giải thích: Cầm tay kiểm tra chiều dài ăn ren M3 và mô-men uốn trên trục bánh xe&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Chọn đúng chiều dài ốc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M3&lt;/code&gt; (bước ren \(p = 0{,}5\text{ mm/vòng}\)):&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Quy tắc bền cơ khí: chiều dài ăn ren tối thiểu trong nhôm/nhựa cứng nên đạt &lt;strong&gt;\(1{,}5\text{–}2{,}0 \times d\)&lt;/strong&gt;, tức với ốc M3 cần tối thiểu \(4{,}5\text{–}6{,}0\text{ mm}\) (tương đương \(6{,}0 / 0{,}5 = 12\) vòng ren ăn khớp!).&lt;/li&gt;
  &lt;li&gt;Nếu bạn bắt một tấm khung dày \(4\text{ mm}\) kèm long-đen \(0{,}5\text{ mm}\) vào trụ đồng có lỗ ren sâu \(6\text{ mm}\), chiều dài ốc lý tưởng là \(4 + 0{,}5 + 5{,}5 = 10\text{ mm}\) (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M3 × 10&lt;/code&gt;). Dùng ốc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M3 × 6&lt;/code&gt; chỉ ăn được \(1{,}5\text{ mm}\) (3 vòng ren) sẽ tuôn ren ngay khi siết!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Tại sao phải lắp bánh xe sát vào bạc đạn/vỏ động cơ nhất có thể?&lt;/strong&gt;
Giả sử mỗi bánh xe gánh tải trọng đứng \(F = 15\text{ N}\):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Nếu tâm bánh xe cách ổ đỡ/vỏ động cơ &lt;strong&gt;\(d_1 = 1{,}0\text{ cm} = 0{,}01\text{ m}\)&lt;/strong&gt;:
Mô-men uốn lên trục: \(M_1 = F \cdot d_1 = 15 \times 0{,}01 = 0{,}15\text{ N}\cdot\text{m}\).&lt;/li&gt;
  &lt;li&gt;Nếu bạn dùng trục nối dài đẩy tâm bánh ra cách &lt;strong&gt;\(d_2 = 3{,}0\text{ cm} = 0{,}03\text{ m}\)&lt;/strong&gt; mà không thêm bạc đạn đỡ ngoài:
Mô-men uốn tăng vọt gấp &lt;strong&gt;3 lần&lt;/strong&gt;: \(M_2 = 15 \times 0{,}03 = 0{,}45\text{ N}\cdot\text{m}\) — dễ làm cong trục hoặc phá bạc đồng bên trong hộp số chỉ sau vài buổi chạy!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, các bạn phân biệt được ốc M2, M3 và M4 thông qua việc đo đường kính ren, biết chọn đai ốc nyloc cho các mối ghép dễ tự tháo khi rung, và siết vít cố định trục (set screw) đúng vào mặt vát của trục chữ D. Các bạn cũng xác định được khi nào nên dùng bạc đạn bi 608 (cho con lăn in 3D), khi nào nên giữ nguyên bạc lót sẵn có trong hộp giảm tốc TT, và biết chọn khớp nối theo đúng đường kính trục đo thực tế thay vì dựa vào trí nhớ.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Các bạn sử dụng được thước kẹp, hoặc dùng một đai ốc đã biết cỡ làm dưỡng kiểm tra khi không có thước (theo thói quen đo lường ở Chương 01). Các bạn đã biết ở Bài 01 rằng bánh xe gắn trực tiếp lên trục động cơ và các trục này cần truyền góc quay trung thực tới encoder.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Hệ thống odometry của Capstone A hoạt động dựa trên giả định rằng góc quay của bánh xe và góc quay của trục động cơ liên hệ chặt chẽ qua tỉ số truyền (sẽ ghi ở Bài 03). Nếu vít cố định trục (set screw) trượt khỏi mặt vát, bánh xe sẽ trượt kèm tiếng tách trong khi encoder vẫn đếm đủ vòng quay của động cơ. Khi đó, tư thế robot trong ROS bị trôi trên bản đồ dù xe thực tế đang khựng lại trên sàn. Các chi tiết ghép nối cơ khí chính là yếu tố quyết định sai số này có xảy ra hay không: một đai ốc nyloc không tự lỏng, một khớp nối đúng cỡ trục và một bạc đạn dùng đúng chỗ tạo nên nửa cơ khí của topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; đáng tin cậy. Nửa còn lại là tín hiệu xung encoder; nếu thiếu phần cơ khí vững chắc, mọi tinh chỉnh phần mềm chỉ tạo ra một quỹ đạo sai lệch.&lt;/p&gt;

&lt;h2 id=&quot;ốc-đai-ốc-và-một-mặt-vát-trên-trục&quot;&gt;Ốc, đai ốc, và một mặt vát trên trục&lt;/h2&gt;

&lt;p&gt;Ốc hệ mét được đặt tên theo đường kính ngoài của phần ren. Ốc M3 có đường kính đỉnh ren khoảng 3 mm và là cỡ tiêu chuẩn cho robot trong môn học này, dùng cho giá động cơ, giá cảm biến và tấm khung. Bước ren thô chuẩn của M3 là 0,5 mm, tức mỗi vòng vặn đủ sẽ đưa ốc tiến thêm 0,5 mm vào đai ốc. Ốc M2 có kích thước nhỏ, thường gặp trên một số mạch cảm biến và rất dễ tòe đầu; còn M4 cứng cáp hơn nhưng thường quá lớn so với rãnh lắp động cơ TT. Một lỗi mua hàng hay gặp là túi linh kiện ghi nhãn “M3” nhưng bên trong lại lẫn nhiều cỡ khác nhau.&lt;/p&gt;

&lt;p&gt;Đai ốc lục giác thông thường có ren trơn, nên rung động từ hộp giảm tốc dễ làm nó tự xoay và lỏng ra chỉ sau một buổi chạy thử. Đai ốc nyloc (đai ốc khóa nylon) có thêm vòng nhựa nylon ở một đầu để kẹp chặt ren: bạn vặn tay tới khi vòng nhựa bắt đầu bám, sau đó siết chặt bằng dụng cụ. Đây là lựa chọn tiêu chuẩn cho Capstone A. Keo khóa ren màu xanh (loại lực giữ trung bình) là phương án dự phòng dạng lỏng cho mối ghép kim loại–kim loại ít khi tháo lắp, nhưng không phù hợp trên nhựa PLA vì dung môi có thể làm giòn và nứt nhựa. Với các chi tiết in 3D, hãy dùng đai ốc nyloc hoặc ép nhiệt đai đồng cấy ren (threaded insert) rồi dùng nyloc trên phần ren kim loại đó.&lt;/p&gt;

&lt;p&gt;Trục của các động cơ giảm tốc phổ thông thường có dạng chữ D — hình trụ được vát phẳng một mặt. Moay-ơ bánh xe hoặc khớp nối có một vít cố định trục (set screw) siết từ bên hông vào trục. Vít này bắt buộc phải tỳ vuông góc lên mặt vát. Nếu tỳ lên phần mặt tròn, vít chỉ tạo một vết cấn nhỏ; khi có mô-men tải, moay-ơ sẽ trượt trên trục trong khi encoder gắn phía động cơ vẫn đếm xung bình thường, làm odometry báo quãng đường không hề xảy ra trên sàn. Sau khi siết vít, hãy dùng ngón tay vặn thử bánh xe: nếu vít nằm đúng mặt vát thì bánh giữ chắc, còn nếu nằm trên mặt tròn thì bánh sẽ trượt kèm tiếng tách.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Ball_bearing.jpg&quot; alt=&quot;Bạc đạn bi: vòng trong, bi, vòng ngoài&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Bạc đạn bi (như trong ảnh) gồm vòng trong, dãy viên bi và vòng ngoài. Cỡ 608 phổ biến có đường kính lỗ 8 mm, đường kính ngoài 22 mm và bề dày 7 mm — giống bạc đạn dùng trên ván trượt. Loại này phù hợp cho con lăn dẫn hướng đai in 3D lắp trên vai trục 8 mm, nhưng quá cỡ so với động cơ TT vỏ vàng, vốn không được thiết kế cho bạc 608. Hộp giảm tốc TT giá rẻ sử dụng bạc lót (ống trượt trơn); chúng chạy hơi ồn nhưng hoàn toàn bình thường. Việc tự ý tháo hộp số giữa chừng Capstone để “nâng cấp” bạc đạn dễ làm mất dấu tỉ số truyền in trên vỏ và tốn cả cuối tuần lắp lại. Hãy giữ nguyên hộp số cho tới khi robot đã vận hành ổn định.&lt;/p&gt;

&lt;p&gt;Khi động cơ và trục tải không liền khối, ta nối chúng bằng khớp nối. Khớp nối cứng yêu cầu hai trục phải đồng tâm tuyệt đối; chỉ một độ lệch góc nhỏ cũng tạo ra tải tuần hoàn làm nóng bạc đạn và khiến bánh xe đảo. Khớp nối hàm (jaw coupling) có miếng đệm đàn hồi hình sao ở giữa giúp bù sai lệch nhỏ và phù hợp hơn với khung xe do sinh viên gia công. Đường kính lỗ khắc trên khớp phải khớp chính xác với trục: các cỡ 3 mm, 4 mm và 6 mm đều phổ biến trên động cơ giảm tốc kim loại và không thể thay thế lẫn nhau. Lắp khớp 6 mm lên trục 4 mm sẽ làm lỗ kẹp bị méo thành hình ô-van và vẫn trượt khi chịu tải. Hãy đo trục bằng thước kẹp trước khi đặt mua. Riêng bánh xe TT thường đã đúc sẵn lỗ chữ D khớp trục động cơ nên bánh chủ động của Capstone A thường không cần khớp nối riêng.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Ốc M3 có bước ren 0,5 mm. Sau khi đai ốc đã vào ren, bốn vòng vặn đủ sẽ ăn sâu một đoạn:&lt;/p&gt;

\[4 \times 0.5 = 2.0\ \text{mm}.\]

&lt;p&gt;Khoảng cách 2 mm này đủ tạo ra sự khác biệt giữa một giá động cơ vừa khít và một con ốc bị siết kịch đáy làm nứt lỗ mica. Quy tắc thực hành là dừng tay ngay khi các chi tiết đã áp sát, rồi siết thêm khoảng một phần tư vòng.&lt;/p&gt;

&lt;p&gt;Xét ở phía trục bánh xe: giả sử vít cố định trục bị trượt khỏi mặt vát khiến bánh xe đứng yên mất đúng một vòng trong khi encoder vẫn đếm đủ vòng quay đó. Với bán kính lăn \(r = 31.2\) mm từ Bài 01, odometry sẽ ghi nhận một quãng đường ảo:&lt;/p&gt;

\[s = 2\pi r = 2\pi \times 31.2 \approx 196\ \text{mm}.\]

&lt;p&gt;Chỉ một lần trượt trục đã tạo ra sai số gần 20 cm, và sai số này sẽ lặp lại mỗi khi mô-men đảo chiều làm moay-ơ trượt.&lt;/p&gt;

&lt;h2 id=&quot;lab-phân-loại-ốc-và-tìm-mặt-vát&quot;&gt;Lab: phân loại ốc và tìm mặt vát&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Đảm bảo đã tắt nguồn. Vít cố định trục rất nhỏ và dễ văng khỏi đầu khóa lục giác, vì vậy luôn hướng moay-ơ ra xa mắt khi thao tác. Keo khóa ren là vật tư tùy chọn; luôn đậy nắp khi không dùng, tránh hít hơi keo và không nhỏ lên chi tiết nhựa.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Ốc và đai ốc lẫn M2/M3/M4, kể cả bộ rẻ&lt;/td&gt;
      &lt;td&gt;Đống cần phân loại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Một đai ốc nyloc và một đai ốc thường&lt;/td&gt;
      &lt;td&gt;Mẫu so sánh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thước kẹp, hoặc một đai ốc mỗi cỡ mà các bạn tin&lt;/td&gt;
      &lt;td&gt;Dưỡng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Một moay-ơ trục chữ D, hoặc ảnh moay-ơ các bạn đang có&lt;/td&gt;
      &lt;td&gt;Mặt vát&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điện thoại&lt;/td&gt;
      &lt;td&gt;Ảnh cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Đo đường kính đỉnh ren của từng con ốc bằng thước kẹp, hoặc thử vặn vào đai ốc mẫu đã biết cỡ. Ốc M2 sẽ lỏng trong đai ốc M3, ốc M4 không vặn vào được, còn ốc M3 sẽ ăn ren trơn tru.&lt;/li&gt;
  &lt;li&gt;Phân loại toàn bộ vào ba cốc riêng (M2, M3, M4) và ghi lại số lượng ốc M3 hiện có — đây là cỡ chính cho Capstone A.&lt;/li&gt;
  &lt;li&gt;Chụp ảnh một đai ốc nyloc đặt cạnh một đai ốc thường, sau đó chú thích rõ phần vòng nhựa nylon trên ảnh.&lt;/li&gt;
  &lt;li&gt;Nếu có moay-ơ bánh xe, nới vít cố định trục, xoay mặt vát của trục chữ D hướng thẳng vào đầu vít rồi siết chặt. Dùng tay vặn thử bánh xe và ghi lại xem mối ghép có giữ chắc không.&lt;/li&gt;
  &lt;li&gt;Nếu có bạc đạn 608, đo kiểm kích thước 8 × 22 × 7 mm và ghi ngắn gọn một câu phân biệt nơi nên dùng (con lăn in 3D) với nơi không nên dùng (bên trong hộp giảm tốc TT).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Ba nhóm ốc đã được phân loại và dán nhãn rõ ràng, một ảnh so sánh đai ốc nyloc với đai ốc thường, và một ghi chú trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; nêu chính xác đường kính trục cần mua linh kiện. Vít cố định trục sau khi lắp đúng mặt vát phải giữ chặt bánh xe khi vặn thử bằng tay.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Các bạn thấy&lt;/th&gt;
      &lt;th&gt;Thường nghĩa là&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Ốc gần như vào rồi kẹt&lt;/td&gt;
      &lt;td&gt;Sai cỡ, hoặc ren bắt chéo; tháo ra&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đai ốc quay mãi dưới rung&lt;/td&gt;
      &lt;td&gt;Đai ốc thường, và mối ấy chưa có nyloc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh kêu vo vo, encoder vẫn đếm&lt;/td&gt;
      &lt;td&gt;Vít cố định trục đang nằm trên phần tròn của trục chữ D&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Khớp nối kẹp rất chặt mà vẫn trượt&lt;/td&gt;
      &lt;td&gt;Lỗ lớn hơn trục một cỡ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Gờ nhựa nứt vào hôm sau khi bôi keo khóa ren&lt;/td&gt;
      &lt;td&gt;Keo khóa ren không phù hợp với nhựa PLA; chuyển sang nyloc hoặc đai đồng cấy ren&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Một bộ ốc M3 kèm một túi nhỏ đai ốc nyloc M3 là đủ dùng cho Capstone A. Chỉ mua bạc đạn 608 khi bạn có kế hoạch in con lăn, và chỉ mua khớp nối hàm khi cần nối hai trục sau khi đã đo chính xác đường kính bằng thước kẹp.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mua gì&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ ốc M3&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ốc M3&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;25.000–70.000&lt;/td&gt;
      &lt;td&gt;Nhiều chiều dài; kiểm tra có lục giác kèm không&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đai ốc nyloc&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đai ốc nyloc M3&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000 một túi&lt;/td&gt;
      &lt;td&gt;Vòng nylon nhìn thấy ở một đầu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bạc đạn 608&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bạc đạn 608&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;5.000–20.000 mỗi cái&lt;/td&gt;
      &lt;td&gt;8×22×7 mm, cho con lăn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Khớp nối hàm&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khớp nối trục 4mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–45.000&lt;/td&gt;
      &lt;td&gt;Khớp trục đã đo: 3 mm, 4 mm hoặc 6 mm&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nên tìm và so sánh ít nhất hai nhà bán vì giá linh kiện có thể thay đổi:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=%E1%BB%91c+M3&quot;&gt;Hshop: ốc M3&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=%C4%91ai%20%E1%BB%91c%20nyloc%20M3&quot;&gt;Shopee: đai ốc nyloc M3&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=b%E1%BA%A1c%20%C4%91%E1%BA%A1n%20608&quot;&gt;Lazada: bạc đạn 608&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=%E1%BB%91c%20v%C3%ADt&quot;&gt;Thế Giới IC: ốc vít&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [How to Measure Screws &amp;amp; Bolts - US &amp;amp; Metric Sizing&lt;/td&gt;
          &lt;td&gt;Fasteners 101](https://www.youtube.com/watch?v=78R1QXL9ijM) — Albany County Fasteners, tiếng Anh. Hướng dẫn đo đường kính ren và chiều dài ốc, đồng thời phân biệt hệ inch và hệ mét để không lắp nhầm vào cùng một lỗ ren.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=8q25EUszBSI&quot;&gt;What is Bearing? Types of Bearings and How they Work?&lt;/a&gt; — The Engineers Post, tiếng Anh. Giải thích nguyên lý hoạt động và các loại bạc đạn giúp giữ trục quay ổn định trước khi lắp khớp nối.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[How to Identify Thread Pitch and Size&lt;/td&gt;
          &lt;td&gt;Tech Tips&lt;/td&gt;
          &lt;td&gt;Swagelok (2020)](https://www.youtube.com/watch?v=Gdvtw0pTAOs) — Swagelok, tiếng Anh. Cách nhận diện bước ren khi hai con ốc có cùng đường kính nhưng không vặn vừa cùng một đai ốc.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một con ốc đo được 2,9 mm qua đỉnh ren và tiến thêm 0,5 mm sau mỗi vòng vặn. Đó là cỡ ốc nào và sau sáu vòng nó tiến sâu thêm bao nhiêu milimét?&lt;/li&gt;
  &lt;li&gt;Bạn đang dùng đai ốc thường cho giá động cơ chạy mỗi buổi lab. Nên thay bằng loại nào, và vì sao keo khóa ren xanh không phải lựa chọn đầu tiên nếu giá động cơ in bằng nhựa PLA?&lt;/li&gt;
  &lt;li&gt;Moay-ơ bánh xe phát ra tiếng tách khi vặn bằng tay, và khi cấp điện (nhấc bánh khỏi sàn), số xung encoder vẫn tăng dù bánh trượt. Vít cố định trục đang tỳ vào vị trí nào?&lt;/li&gt;
  &lt;li&gt;Thước kẹp đo được 4,0 mm trên trục động cơ giảm tốc kim loại. Bạn cần đặt khớp nối lỗ cỡ bao nhiêu, và điều gì xảy ra nếu dùng lỗ 6 mm?&lt;/li&gt;
  &lt;li&gt;Một người bạn định ép bạc đạn 608 vào hộp giảm tốc TT vỏ vàng để “quay êm hơn”. Trong phạm vi Capstone A, bạn nên khuyên họ xử lý bạc lót sẵn có thế nào?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Ốc M3, vì đường kính đỉnh ren xấp xỉ 3 mm và bước ren chuẩn là 0,5 mm. Sau sáu vòng, ốc tiến thêm \(6 \times 0.5 = 3.0\) mm.&lt;/li&gt;
  &lt;li&gt;Thay bằng đai ốc nyloc. Keo khóa ren xanh có thể làm nứt nhựa PLA; nếu muốn dùng khóa ren chắc trên nhựa in, hãy ép nhiệt đai đồng cấy ren rồi dùng nyloc trên phần ren kim loại.&lt;/li&gt;
  &lt;li&gt;Vít cố định trục đang tỳ lên phần tròn của trục chữ D thay vì mặt vát. Cần nới vít, xoay mặt vát về đúng vị trí dưới đầu vít, siết lại rồi vặn thử bằng tay.&lt;/li&gt;
  &lt;li&gt;Đặt khớp nối lỗ 4 mm. Nếu dùng lỗ 6 mm, khớp sẽ bị kẹp méo thành hình ô-van và trượt khi chịu mô-men.&lt;/li&gt;
  &lt;li&gt;Nên giữ nguyên bạc lót sẵn có. Bạc đạn 608 có kích thước 8×22×7 mm, chỉ phù hợp cho con lăn in 3D chứ không lắp vừa vào bên trong hộp giảm tốc TT.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.mcmaster.com/&quot;&gt;Danh mục McMaster-Carr&lt;/a&gt; — tra cứu ốc, đai ốc nyloc, vòng chặn trục và khớp nối hàm theo tên để học quy cách kích thước (chỉ dùng như tài liệu tra cứu, không cần đặt hàng từ nước ngoài).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://reprap.org/wiki/Threaded_insert&quot;&gt;Đai ren nhiệt trên RepRap&lt;/a&gt; — hướng dẫn sử dụng đai đồng cấy ren bằng nhiệt cho chi tiết in 3D, giúp tạo ren M3 bền vững trên nhựa PLA.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Ball_bearing&quot;&gt;Bạc đạn bi (tổng quan)&lt;/a&gt; — tổng quan về cấu tạo vòng bi và kích thước tiêu chuẩn 608.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Giải phẫu khung robot vi sai</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_01_chassis_anatomy/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter06/06_01_chassis_anatomy</id>
   <content type="html">&lt;p&gt;Thời lượng ước tính: &lt;strong&gt;70–90 phút&lt;/strong&gt;, bao gồm thời gian đo trực tiếp trên khung xe 2WD thực tế hoặc đo trên bản vẽ đúng tỉ lệ nếu bộ kit chưa về tới xưởng.&lt;/p&gt;

&lt;p&gt;Nhìn thoáng qua, khung xe robot vi sai hai bánh trông chỉ như một tấm nhựa phẳng gắn hai động cơ và một bánh đa hướng (caster). Nhưng chỉ cần bạn đặt cục pin nặng lệch về phía sau vài centimet, robot bỗng mất bám đường, bánh chủ động quay tít tại chỗ khi tăng tốc, hoặc chúi mũi xuống sàn mỗi khi phanh gấp! &lt;strong&gt;Giải phẫu khung robot&lt;/strong&gt; chính là bài toán cân bằng cơ học giữa vị trí trọng tâm (CoM), khoảng sáng gầm, độ cứng vững và cách bố trí không gian cho điện tử – cảm biến.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-trọng-tâm-phân-bổ-tải-trọng-và-hình-học-khung-vi-sai&quot;&gt;Kiến thức cần nhớ: Trọng tâm, Phân bổ tải trọng và Hình học khung vi sai&lt;/h2&gt;

&lt;h3 id=&quot;1-phân-bổ-trọng-lượng-giữa-trục-bánh-chủ-động-và-bánh-caster&quot;&gt;1. Phân bổ trọng lượng giữa trục bánh chủ động và bánh caster&lt;/h3&gt;
&lt;p&gt;Trên khung 3 điểm tựa (2 bánh chủ động + 1 bánh caster), tải trọng đè lên trục chủ động quyết định trực tiếp &lt;strong&gt;lực ma sát bám tối đa&lt;/strong&gt; \(F_{\max} = \mu N_{\text{drive}}\). Nếu đặt trọng tâm quá gần bánh caster, bánh chủ động bị nhẹ hẫng và trượt quay; ngược lại nếu đặt quá sát (hoặc vượt qua) trục bánh chủ động, robot dễ bị bốc đầu/chúi mũi khi tăng giảm tốc đột ngột. Vị trí lý tưởng thường đặt trọng tâm nằm trong vùng &lt;strong&gt;60%–75% tải trọng dồn lên trục chủ động&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;2-phân-tầng-cơ--điện-tử-multi-deck-architecture&quot;&gt;2. Phân tầng cơ – điện tử (Multi-Deck Architecture)&lt;/h3&gt;
&lt;p&gt;Khung robot tốt chia rõ các tầng chức năng: tầng đáy nặng nhất chứa &lt;strong&gt;pin và động cơ&lt;/strong&gt; để hạ thấp trọng tâm chống lật; tầng giữa chứa &lt;strong&gt;mạch nguồn, cầu chì và vi điều khiển&lt;/strong&gt; để dây công suất ngắn gọn; tầng trên cùng thoáng đãng dành cho &lt;strong&gt;LiDAR, camera và nút dừng khẩn cấp (E-Stop)&lt;/strong&gt; không bị che khuất tầm nhìn.&lt;/p&gt;

&lt;h3 id=&quot;3-trụ-đồng-standoff-độ-đồng-phẳng-và-khoảng-sáng-gầm&quot;&gt;3. Trụ đồng (Standoff), độ đồng phẳng và khoảng sáng gầm&lt;/h3&gt;
&lt;p&gt;Khung bị vặn xoắn dù chỉ \(2\text{ mm}\) sẽ làm một bánh chủ động nhấc nhẹ khỏi sàn khi qua gờ gạch, phá hỏng hoàn toàn odometry. Giữ trục hai bánh đồng trục, chọn khoảng sáng gầm phù hợp mặt sàn (thường \(10\text{–}20\text{ mm}\) trong nhà) và dùng trụ đồng liên kết cứng là tiêu chuẩn cơ khí cơ bản.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-phân-bổ-tải-trọng-bánh-chủ-động-và-gia-tốc-tối-đa-trước-khi-trượt-bánh&quot;&gt;Giải thích: Cầm tay tính phân bổ tải trọng bánh chủ động và gia tốc tối đa trước khi trượt bánh&lt;/h3&gt;

&lt;p&gt;Xét khung robot vi sai nặng &lt;strong&gt;\(M = 2{,}0\text{ kg}\)&lt;/strong&gt; (trọng lượng \(W = 2{,}0 \times 9{,}81 = 19{,}62\text{ N}\)), khoảng cách từ trục bánh chủ động phía trước tới bánh caster phía sau là &lt;strong&gt;\(L = 16\text{ cm}\)&lt;/strong&gt;. Bạn bố trí pin sao cho trọng tâm (CoM) nằm cách trục bánh chủ động &lt;strong&gt;\(a = 4\text{ cm}\)&lt;/strong&gt; về phía caster (cách caster \(b = 12\text{ cm}\)):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cân bằng mô-men để tìm lực pháp tuyến đè lên trục chủ động (\(N_{\text{drive}}\)) và bánh caster (\(N_{\text{caster}}\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Tỷ lệ tải dồn lên trục chủ động:
\(\frac{b}{L} = \frac{12}{16} = 75\% \implies N_{\text{drive}} = 0{,}75 \times 19{,}62 = 14{,}72\text{ N}\;(\text{tương đương } 1{,}5\text{ kg}).\)&lt;/li&gt;
      &lt;li&gt;Tỷ lệ tải dồn lên bánh caster:
\(\frac{a}{L} = \frac{4}{16} = 25\% \implies N_{\text{caster}} = 0{,}25 \times 19{,}62 = 4{,}91\text{ N}\;(\text{tương đương } 0{,}5\text{ kg}).\)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính lực kéo cực đại và gia tốc cực đại trước khi trượt bánh:&lt;/strong&gt;
Với hệ số ma sát lốp cao su trên sàn gạch \(\mu = 0{,}60\):
\(F_{\max} = \mu\,N_{\text{drive}} = 0{,}60 \times 14{,}72 = 8{,}83\text{ N} \implies a_{\max} = \frac{F_{\max}}{M} = \frac{8{,}83}{2{,}0} \approx 4{,}41\text{ m/s}^2.\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thử đảo ngược: nếu bạn đặt nhầm cục pin nặng tuốt ra đuôi xe sao cho \(a = 12\text{ cm}\) (chỉ còn 25% tải lên bánh chủ động), \(a_{\max}\) tụt thê thảm xuống chỉ còn \(1{,}47\text{ m/s}^2\) và robot liên tục trượt bánh mỗi khi đề-pa!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, các bạn xác định được hai kích thước hình học mà robot vi sai thực sự sử dụng khi di chuyển: khổ vết (track width) \(b\) đo giữa hai tâm vết tiếp xúc của lốp (không phải giữa hai vỏ động cơ) và bán kính lăn \(r\) tính từ phép lăn đúng một vòng khi lốp đã chịu tải. Từ một cặp vận tốc bánh xe, các bạn tính được vận tốc tịnh tiến và vận tốc góc quay của thân robot, biết cách bố trí pin sao cho bánh caster luôn bám sàn, và ghi lại \(b\), \(r\) theo đơn vị milimét để bài lắp ráp ở Chương 07 cũng như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; trong ROS sử dụng thống nhất.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Các bạn biết đọc thước milimét và không nhầm lẫn giữa milimét với centimét khi ghi chép. Các bạn đã hiểu động cơ giảm tốc một chiều làm quay bánh xe (Chương 05) và xung encoder về sau sẽ đại diện cho góc quay của trục (Chương 04). Chưa cần cài đặt ROS; nếu chưa có khung xe trên bàn, một bản vẽ đúng tỉ lệ vẫn đủ để thực hành, miễn là ghi chú rõ số liệu lấy từ bản vẽ.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Capstone A sử dụng đúng cấu hình khung này: hai bánh chủ động nằm trên cùng một trục và một bánh caster (hoặc đế trượt) giữ điểm tựa thứ ba để đầu khung không cày xuống sàn. Chương 08 sẽ trình bày đầy đủ mô hình động học. Trên ROS, bộ điều khiển &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; chuyển đổi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; thành vận tốc hai bánh, đồng thời tính ngược odometry từ xung encoder bằng chính hai tham số \(b\) và \(r\) đo hôm nay. Tệp URDF ở Chương 09–10 cũng định vị các khâu bánh xe theo các kích thước này.&lt;/p&gt;

&lt;p&gt;Nếu giá trị \(b\) bị sai, sai số sẽ lập tức biểu hiện trên quỹ đạo thực tế: khi bạn ra lệnh đi thẳng, robot lại chạy theo đường cong vì bộ điều khiển liên tục hiệu chỉnh theo một hình học không đúng với phần cứng. Vì vậy, hãy đo cẩn thận ngay từ đầu, ghi rõ đơn vị milimét vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; và tránh thói quen làm tròn ước lượng kiểu “khoảng 15 cm” khi chuyển sang các bài sau. Các con số ước lượng đại khái là nguyên nhân phổ biến nhất gây lệch bản đồ ở những nhóm mới lắp khung robot.&lt;/p&gt;

&lt;h2 id=&quot;hai-bánh-một-caster-hai-chiều-dài&quot;&gt;Hai bánh, một caster, hai chiều dài&lt;/h2&gt;

&lt;p&gt;Quan sát tấm khung từ phía trên: động cơ trái và động cơ phải hướng trục ra hai bên, hai bánh xe nằm trên cùng một đường trục chung. Robot không có cơ cấu lái vô-lăng mà chuyển hướng bằng cách tạo chênh lệch vận tốc giữa hai bánh. Điểm đỡ thứ ba là bánh caster xoay tự do hoặc đế trượt nhẵn, giúp mũi khung không chạm sàn. Capstone A dùng bánh caster xoay vì đế trượt tuy chấp nhận được trên xe nhẹ chạy trong nhà nhưng dễ gây ồn và để lại vết xước trên nền gạch men.&lt;/p&gt;

&lt;p&gt;Khổ vết (track width) \(b\) là khoảng cách giữa hai tâm vết tiếp xúc — tức giữa hai điểm chính giữa mặt lốp chạm đất. Khoảng cách giữa hai vỏ động cơ chỉ là kích thước lắp đặt trên bản vẽ tấm khung, không dùng trong phương trình động học. Nếu đo từ mép ngoài lốp trái sang mép ngoài lốp phải, bạn đã vô tình cộng thêm bề rộng một mặt lốp vào \(b\). Hãy đặt khung ở trạng thái chịu tải thực tế (có lắp pin), trượt đầu thước vào chính giữa mỗi mặt lốp rồi ghi kết quả bằng milimét. Dưới tải trọng, lốp hơi phình ngang nên khổ vết khi chưa lắp pin và khi đã lắp pin có thể lệch nhau vài milimét.&lt;/p&gt;

&lt;p&gt;Bán kính lăn \(r\) là bán kính hiệu dụng khi cao su bị nén dưới trọng lượng xe. Con số ghi trên túi linh kiện chỉ là đường kính tự do khi bánh chưa chịu tải. Khi lắp pin, lốp lún nhẹ làm bán kính giảm xuống. Để đo chính xác, hãy đánh dấu một điểm trên lốp, lăn đúng một vòng trên mặt giấy, đo chiều dài quãng đường \(s\) rồi tính:&lt;/p&gt;

\[r = \frac{s}{2\pi}.\]

&lt;p&gt;Chẳng hạn, một bánh ghi nhãn đường kính 65 mm nhưng lăn một vòng được 196 mm sẽ có bán kính lăn:&lt;/p&gt;

\[r = \frac{196}{2\pi} \approx 31.2\ \text{mm},\]

&lt;p&gt;nhỏ hơn đáng kể so với một nửa đường kính danh nghĩa (32,5 mm). Mức chênh lệch vài phần trăm này sẽ tích lũy trên mỗi mét di chuyển, khiến quãng đường odometry và bản đồ dài hơn hoặc ngắn hơn thực tế vài centimét. Khi đo, hãy đè lên bánh một lực tương đương tải trọng thực tế, hoặc lăn thẳng cả khung xe nếu hai bánh quay tự do. Thực hiện tương tự cho bánh còn lại; hai bán kính nên khớp nhau trong phạm vi 1 mm. Nếu lệch nhiều hơn, nhiều khả năng một lốp đang lắp lệch tâm trên trục, khiến robot tự lệch hướng khi chạy thẳng ngay cả khi firmware không có lỗi.&lt;/p&gt;

&lt;p&gt;Tam giác đỡ của khung xe được tạo bởi hai vết tiếp xúc của bánh chủ động và điểm chạm đất của bánh caster. Đường thẳng đứng đi qua trọng tâm của robot phải rơi vào bên trong tam giác này. Nếu đặt pin dựng đứng phía sau trục bánh hoặc nhô ra ngoài phạm vi tam giác đỡ, mũi xe dễ bị bốc đầu khi tăng tốc; ngược lại, nếu bánh caster gần như không chịu tải, nó sẽ rung lắc vì khoảng đuôi (trail) không ổn định. Hãy đặt khối pin thấp, nằm giữa hai bánh hoặc hơi dịch về phía trước trục, sau đó dùng tay ấn nhẹ vào giá đỡ caster để kiểm tra: bánh caster cần chịu một phần trọng lượng ổn định trong khi cả hai bánh chủ động vẫn bám chắc mặt sàn. Nếu ấn nhẹ vào caster mà mũi xe nhấc lên, pin đang đặt quá lùi về sau; nếu caster không chịu lực, pin đang đặt quá cao hoặc quá lệch về phía trước.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Xét khung xe có \(b = 0.15\) m, vận tốc thẳng tại vết tiếp xúc của bánh trái là \(v_l = 0.20\) m/s và bánh phải là \(v_r = 0.30\) m/s (chiều dương là chiều tiến). Vận tốc tịnh tiến \(v\) và vận tốc góc \(\omega\) của thân robot là:&lt;/p&gt;

\[v = \frac{v_r + v_l}{2} = \frac{0.30 + 0.20}{2} = 0.25\ \text{m/s},\]

\[\omega = \frac{v_r - v_l}{b} = \frac{0.30 - 0.20}{0.15} = 0.667\ \text{rad/s}.\]

&lt;p&gt;Trong một giây, trung điểm trục bánh đi được 25 cm theo hướng mũi xe, đồng thời hướng mũi xoay một góc \(0.667 \times 180/\pi \approx 38^\circ\). Theo quy ước này, \(\omega &amp;gt; 0\) khi bánh phải nhanh hơn bánh trái, tức robot quay sang trái.&lt;/p&gt;

&lt;p&gt;Nếu cùng cặp vận tốc bánh đó nhưng khổ vết bị nhập sai thành 0,18 m (do đo qua hai vỏ động cơ), vận tốc góc tính ra sẽ giảm xuống chỉ còn \(0.556\) rad/s. Bộ điều khiển tin theo giá trị 0,18 m sẽ tạo ra độ chênh vận tốc sai để bù cho góc quay thực tế (ứng với \(b = 0.15\) m), khiến robot chạy thành đường cong dù lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; yêu cầu đi thẳng. Vì vậy, từng milimét đo được trên thước quan trọng hơn nhiều so với ước lượng cảm tính “khung cỡ này chắc khoảng 15 cm”.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;Khổ vết giữa hai tâm tiếp xúc, và vòng lăn cho bán kính lăn&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/diff_drive_kinematics.png&quot; alt=&quot;Vận tốc thân và vận tốc góc vẽ từ hai vận tốc bánh&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hình thứ nhất minh họa cách đặt thước: \(b\) đo giữa hai tâm mặt lốp và \(r\) lấy từ bán kính lốp khi đã chịu nén. Hình thứ hai biểu diễn trực quan hai phương trình vận tốc: hai vectơ vận tốc tiến bằng nhau cho \(\omega = 0\), còn khi hai vectơ khác nhau, robot quay quanh một tâm nằm trên đường kéo dài của trục bánh. Chương 08 sẽ định nghĩa rõ tâm quay tức thời và bán kính quỹ đạo này. Trong bài hôm nay, các bạn chỉ cần ghi đầy đủ \(b\), \(r\), \(v\) và \(\omega\) kèm đơn vị vào sổ tay, đồng thời nêu rõ số liệu đo từ phần cứng hay bản vẽ.&lt;/p&gt;

&lt;h2 id=&quot;lab-đo-b-và-r&quot;&gt;Lab: đo b và r&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Đảm bảo đã tắt nguồn hoàn toàn. Nếu pin đang gắn sẵn trên khung, hãy tháo pin trước khi đưa thước kim loại lại gần tấm khung để tránh làm chập các đầu dây động cơ. Bài thực hành này không cần cấp điện và không có trục quay dưới điện áp; không cần cấp nguồn để kiểm tra độ tròn của bánh xe.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung 2WD, hoặc bản vẽ tỉ lệ của bộ kit đã đặt&lt;/td&gt;
      &lt;td&gt;Vật được đo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thước hoặc thước kẹp, đơn vị milimét&lt;/td&gt;
      &lt;td&gt;Khổ vết&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Giấy, băng dính, bút&lt;/td&gt;
      &lt;td&gt;Lăn một vòng để lấy \(r\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Các số Chương 07 và ROS sẽ chép&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Đặt khung xe lên mặt bàn phẳng đúng tư thế vận hành. Nếu đã có pin, đặt pin vào vị trí dự kiến trên khung.&lt;/li&gt;
  &lt;li&gt;Đánh dấu tâm mặt lốp ở cả hai bánh rồi đo khổ vết \(b\) giữa hai dấu này. Ghi kết quả theo milimét; đo riêng khoảng cách giữa hai vỏ động cơ để đối chiếu và ghi chú rõ là không đưa vào bộ điều khiển.&lt;/li&gt;
  &lt;li&gt;Đánh dấu một điểm trên lốp và điểm tương ứng trên tờ giấy. Lăn bánh đúng một vòng dưới tải trọng xấp xỉ tải thực tế của bánh đó, rồi đo chiều dài quãng đường \(s\).&lt;/li&gt;
  &lt;li&gt;Tính \(r = s / (2\pi)\), làm tròn đến một chữ số thập phân theo milimét. Lặp lại quy trình với bánh còn lại.&lt;/li&gt;
  &lt;li&gt;Dùng tay ấn nhẹ tại vị trí bánh caster để kiểm tra độ chịu tải. Nếu chưa có pin, phác thảo vị trí đặt pin sao cho trọng tâm nằm gọn trong tam giác đỡ.&lt;/li&gt;
  &lt;li&gt;Nếu bộ kit chưa về, thực hiện các bước 2–5 trên bản vẽ đúng tỉ lệ và ghi rõ tiêu đề: “bản vẽ, chưa phải phần cứng”.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Một cặp số liệu có ghi ngày trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; (ví dụ \(b = 148\) mm và \(r = 31.2\) mm), trong đó bán kính lăn \(r\) của hai bánh chênh lệch không quá 1 mm. Ghi chú nêu rõ nguồn đo (phần cứng hay bản vẽ), và khoảng cách hai vỏ động cơ được tách riêng, không nhầm lẫn với \(b\).&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Chuyện gì sai&lt;/th&gt;
      &lt;th&gt;Hệ quả về sau&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;\(b\) đo giữa hai thân động cơ&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; đi thẳng lại chạy thành vòng tròn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;\(r\) chép bằng nửa đường kính trên nhãn&lt;/td&gt;
      &lt;td&gt;Quãng đường odometry dài hoặc ngắn trên mỗi mét&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin kê phía sau trục&lt;/td&gt;
      &lt;td&gt;Bốc đầu, hoặc bánh caster lắc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bán kính trái và phải lệch vài milimét&lt;/td&gt;
      &lt;td&gt;Lệnh đi thẳng đã quay ngang&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Nên mua sẵn khung mica 2WD đã cắt gia công để kịp lắp ráp Capstone A trong tháng này; các giá đỡ nhỏ có thể in 3D sau ở Bài 06. Tấm khung có sẵn rãnh lắp động cơ TT và lỗ bắt bánh caster là bộ kit phổ biến trên các sàn thương mại điện tử. Lưu ý rằng bánh cao su và bánh caster thường được bán tách rời trong cùng tin đăng, nên cần kiểm tra kỹ thay vì mặc định chúng có sẵn trong hộp khung.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mua gì&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung 2WD&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khung xe robot 2 bánh&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;80.000–200.000&lt;/td&gt;
      &lt;td&gt;Tấm mica, rãnh động cơ, lỗ caster&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh cao su&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh xe cao su 65mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–45.000 mỗi cái&lt;/td&gt;
      &lt;td&gt;Đường kính trên nhãn chỉ là điểm bắt đầu của phép lăn, không phải \(r\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh caster xoay&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh caster robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;12.000–40.000&lt;/td&gt;
      &lt;td&gt;Bánh xoay có đuôi cho Capstone A&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Tìm trên các trang sau (vì đường dẫn cửa hàng và giá bán thay đổi theo thời điểm):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=khung+xe+robot&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=khung%20xe%20robot%202%20b%C3%A1nh&quot;&gt;Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=khung%20xe%20robot%202%20b%C3%A1nh&quot;&gt;Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=khung%20xe%20robot&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trước khi thanh toán, hãy xác nhận rãnh bắt động cơ khớp với hộp giảm tốc TT và bánh caster đã đi kèm trong bộ hoặc được đặt mua cùng đơn. Một tấm khung không có lỗ gắn caster sẽ khiến phần mũi của Capstone A cày trực tiếp xuống sàn.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=2QE3LG5h2tw&quot;&gt;Differential Drive &amp;amp; Odometry Explained: How It Works&lt;/a&gt; — Netizen Robotics, tiếng Anh. Trình bày cấu tạo hai bánh chủ động và khoảng cách giữa hai bánh của khung vi sai mà bài này yêu cầu đo.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=RsZBxfpcLu8&quot;&gt;PART1: Building Low-Cost Differential Wheeled Robot For Machine Learning, SLAM, and ROS&lt;/a&gt; — Aleksandar Haber PhD, tiếng Anh. Quá trình dựng một khung robot giá thấp; hãy đối chiếu vị trí đặt động cơ, bánh xe và pin với sơ đồ trong bài.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Phép lăn một vòng cho quãng đường \(s = 200\) mm. Tính bán kính lăn \(r\) theo milimét, làm tròn một chữ số thập phân.&lt;/li&gt;
  &lt;li&gt;Với \(b = 0.16\) m, \(v_l = 0.25\) m/s và \(v_r = 0.25\) m/s, hãy tính \(v\) và \(\omega\). Robot chuyển động như thế nào?&lt;/li&gt;
  &lt;li&gt;Giữ nguyên \(b = 0.16\) m, với \(v_l = 0.10\) m/s và \(v_r = 0.30\) m/s, hãy tính \(v\) và \(\omega\).&lt;/li&gt;
  &lt;li&gt;Một sinh viên đo từ mép ngoài lốp trái đến mép ngoài lốp phải được 180 mm, biết mỗi mặt lốp rộng 20 mm. Giá trị \(b\) nào cần nhập vào bộ điều khiển?&lt;/li&gt;
  &lt;li&gt;Khối pin được đặt trên một giá cao phía sau trục bánh, khiến bánh caster nhấc khỏi sàn khi đẩy robot tiến tới. Cần dời pin đến đâu, và nếu giữ nguyên vị trí đó thì lệnh đi thẳng trong ROS sẽ tạo ra quỹ đạo gì?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(r = 200/(2\pi) \approx 31.8\) mm.&lt;/li&gt;
  &lt;li&gt;\(v = 0.25\) m/s và \(\omega = 0\), robot đi thẳng.&lt;/li&gt;
  &lt;li&gt;\(v = 0.20\) m/s và \(\omega = (0.30-0.10)/0.16 = 1.25\) rad/s, robot quay sang trái vì bánh phải chạy nhanh hơn.&lt;/li&gt;
  &lt;li&gt;Mỗi tâm tiếp xúc nằm lùi vào trong bằng một nửa bề rộng mặt lốp so với mép ngoài, do đó cần trừ đúng một bề rộng mặt lốp: \(b = 180 - 20 = 160\) mm.&lt;/li&gt;
  &lt;li&gt;Cần dời pin xuống thấp và đưa vào bên trong tam giác đỡ, hơi dịch về phía trước trục nếu bánh caster đang quá nhẹ tải. Nếu giữ nguyên pin trên giá cao phía sau, caster sẽ mất tải, hướng mũi xe không ổn định và lệnh đi thẳng sẽ tạo ra quỹ đạo cong trên sàn.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ros-controls/ros2_controllers/tree/master/diff_drive_controller&quot;&gt;diff_drive_controller trong ros2_controllers&lt;/a&gt; — khoảng cách hai bánh và bán kính bánh là các tham số trực tiếp của gói này. Tài liệu bộ điều khiển bản Jazzy được sinh từ cây mã nguồn này; chỉ mục gói nằm tại &lt;a href=&quot;https://docs.ros.org/en/jazzy/index.html&quot;&gt;docs.ros.org/en/jazzy&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://wiki.ros.org/diff_drive_controller&quot;&gt;Wiki diff_drive_controller ROS 1&lt;/a&gt; — giải thích tên tham số bằng ngôn ngữ trực quan. Dù là trang của ROS 1, mô hình hình học vẫn giống hệt ROS 2.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://navigation.ros.org/setup_guides/odom/setup_odom.html&quot;&gt;Thiết lập odometry trên Nav2&lt;/a&gt; — minh họa cách sai số odometry ảnh hưởng khi chuyển từ điều khiển tay (teleop) sang điều hướng tự hành.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 05</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_07_ch05_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_07_ch05_lab_checklist</id>
   <content type="html">&lt;p&gt;Bài này là mốc kiểm tra hoàn thành Chương 05 trước khi chuyển sang phần cơ khí ở Chương 06 và tích hợp hai kênh PWM có dấu, pin cùng cầu chì ở Chương 07. Bạn vượt qua chương này khi điền đầy đủ bảng kiểm chứng bên dưới bằng số liệu và ảnh thực tế của chính mình: tên chip cầu H, bảng chân lý, nguồn động cơ tách khỏi USB, ghi chú dòng không tải/stall, phép tính xung servo (ngay cả khi không có servo), kiểm thử dừng bang-bang, ảnh đấu dây và hai lỗi đã gặp hoặc chủ động diễn tập.&lt;/p&gt;

&lt;p&gt;Trong phòng lab robotics, khoảnh khắc căng thẳng nhất luôn là lúc cấp nguồn lần đầu cho tầng cơ cấu chấp hành: chỉ một sợi dây nguồn cắm ngược cực hay một mức PWM khởi động sai cũng đủ làm robot phóng thẳng khỏi mặt bàn hoặc bốc khói mạch driver. Checklist Chương 05 đóng vai trò là quy trình &lt;strong&gt;nghiệm thu tầng truyền động&lt;/strong&gt; bài bản — từ kiểm tra ngân sách dòng đỉnh, đo vùng chết ma sát tĩnh (deadband), cho đến bài test “kê bánh trên không” (wheels-off-ground) trước khi đặt robot xuống sàn.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-nghiệm-thu-hệ-thống-truyền-động-an-toàn&quot;&gt;Kiến thức cần nhớ: Nghiệm thu hệ thống truyền động an toàn&lt;/h2&gt;

&lt;h3 id=&quot;1-ngân-sách-dòng-đỉnh-peak-current-budget-và-tách-nhánh-nguồn&quot;&gt;1. Ngân sách dòng đỉnh (Peak Current Budget) và tách nhánh nguồn&lt;/h3&gt;
&lt;p&gt;Động cơ và servo tiêu thụ dòng xung lớn hơn nhiều so với dòng trung bình khi khởi động hoặc đổi chiều đột ngột. Tính tổng dòng đỉnh của toàn hệ thống (động cơ + servo + vi điều khiển + cảm biến) và cộng thêm biên an toàn \(30\%\) là bước bắt buộc để chọn đúng pin, cầu chì và mạch hạ áp (buck converter).&lt;/p&gt;

&lt;h3 id=&quot;2-vùng-chết-pwm-pwm-deadband-do-ma-sát-tĩnh-stiction&quot;&gt;2. Vùng chết PWM (PWM Deadband) do ma sát tĩnh (Stiction)&lt;/h3&gt;
&lt;p&gt;Động cơ DC kèm hộp số không bao giờ quay ngay ở mức PWM 1% hay 5%: lực ma sát tĩnh trong bánh răng giữ trục đứng im cho tới khi PWM vượt qua một ngưỡng (thường 12%–20%). Nếu không đo và bù vùng chết này trong firmware, bộ điều khiển vòng kín sẽ bị “khựng” mỗi khi robot chạy chậm hoặc đảo chiều.&lt;/p&gt;

&lt;h3 id=&quot;3-quy-trình-kê-bánh-wheels-off-ground-và-kiểm-tra-chiều-quay-chuẩn&quot;&gt;3. Quy trình kê bánh (Wheels-Off-Ground) và kiểm tra chiều quay chuẩn&lt;/h3&gt;
&lt;p&gt;Luôn kê gầm robot lên khối gỗ hoặc hộp giấy sao cho bánh quay tự do trên không khi chạy thử code lần đầu. Xác nhận: lệnh tiến làm &lt;em&gt;cả hai bánh&lt;/em&gt; quay về phía trước và encoder &lt;em&gt;cả hai bên&lt;/em&gt; cùng tăng giá trị dương!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-lập-ngân-sách-dòng-đỉnh-và-bù-vùng-chết-pwm-cho-xe-lab-2-bánh&quot;&gt;Giải thích: Cầm tay lập ngân sách dòng đỉnh và bù vùng chết PWM cho xe lab 2 bánh&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Cộng ngân sách dòng đỉnh của robot trước khi chọn nguồn:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;2 động cơ DC truyền động (dòng tải nặng mỗi bên \(1{,}2\text{ A}\)): \(2 \times 1{,}2 = 2{,}4\text{ A}\).&lt;/li&gt;
  &lt;li&gt;1 servo gật gù camera (dòng đỉnh lúc khởi động): \(0{,}8\text{ A}\).&lt;/li&gt;
  &lt;li&gt;Vi điều khiển ESP32 + IMU + cảm biến siêu âm: \(0{,}4\text{ A}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tổng dòng đỉnh tức thời:&lt;/strong&gt; \(I_{\text{đỉnh}} = 2{,}4 + 0{,}8 + 0{,}4 = 3{,}6\text{ A}\).&lt;/li&gt;
  &lt;li&gt;Cộng biên an toàn \(30\%\): \(3{,}6 \times 1{,}3 = 4{,}68\text{ A} \implies\) cần pin và cầu chì tối thiểu &lt;strong&gt;\(5\text{ A}\)&lt;/strong&gt; (không bao giờ dùng cổng USB laptop \(0{,}5\text{ A}\) để thử tải động cơ!).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Bù vùng chết PWM 8-bit (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0..255&lt;/code&gt;):&lt;/strong&gt;
Khi tăng dần PWM từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; lên trên bàn test, bạn thấy bánh xe chỉ bắt đầu lăn ở mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM_min = 35&lt;/code&gt; (tức \(35 / 255 \approx 13{,}7\%\)). Để lệnh tốc độ chuẩn hóa \(u \in (0, 1]\) điều khiển mượt từ sát 0:
\(\mathrm{PWM}_{\text{out}} = 35 + \operatorname{round}\!\big(u \times (255 - 35)\big) = 35 + \operatorname{round}(220\,u).\)
Kiểm tra nhanh: khi bộ điều khiển ra lệnh nhẹ \(u = 0{,}10\), thay vì xuất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;26&lt;/code&gt; (bánh đứng im!), mạch xuất \(35 + 22 = 57\) giúp bánh lăn êm ngay lập tức!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn hoàn thiện bảng ký duyệt Chương 05 bằng các bằng chứng đo đạc và tính toán cụ thể thay vì đánh dấu chung chung. Bạn nộp được ảnh chụp đấu dây chứng minh nguồn động cơ tách biệt khỏi USB nhưng nối chung mass, trình bày rõ ràng tiêu chí hoàn thành trước khi bước vào Chương 06, và lập danh sách mua bổ sung ngắn gọn chỉ gồm những linh kiện thực sự còn thiếu (mạch cầu H, động cơ TT, cầu chì).&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Nội dung bảng tổng hợp trực tiếp từ bài 05-01 đến bài 05-06. Bạn cần mở sẵn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, đồng hồ vạn năng và ảnh chụp mạch thật. Với các mục chưa có phần cứng (như servo hoặc bước đo stall), bắt buộc hoàn thành phần tính toán tương ứng thay vì ghi “để sau”.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Khi ghép khung xe ở Chương 06 và nối hệ thống dây ở Chương 07, mọi tầng phần mềm phía trên (bao gồm lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; từ ROS 2 về sau) đều dựa vào sự chính xác của các ghi chép tại đây. Một bảng chân lý sai dấu, một mức dòng stall chưa đối chiếu với cầu chì hay một nhánh timeout bị bỏ sót đều sẽ dẫn tới robot quay sai chiều, tự reset do sụt áp hoặc không dừng trước vật cản. Bước rà soát này bảo đảm toàn bộ nền tảng chấp hành đã vững trước khi lắp ráp cơ khí.&lt;/p&gt;

&lt;h2 id=&quot;yêu-cầu-chi-tiết-của-từng-mục-kiểm-tra&quot;&gt;Yêu cầu chi tiết của từng mục kiểm tra&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Nhận diện mạch cầu H:&lt;/strong&gt; ghi đúng mã chip (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L298N&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt;) thay vì màu bo mạch, kèm một chân dễ gây lỗi đặc trưng: với TB6612 là “phải kéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; lên cao”; với L298N là “rút jumper 5 V khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM &amp;gt; 12 V&lt;/code&gt; và không nuôi ESP32 từ IC 7805 tích hợp”; với DRV8833 là “dải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; 2,7–10,6 V và cần chú ý giới hạn dòng stall”.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bảng chân lý:&lt;/strong&gt; ghi đủ các tổ hợp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(IN1, IN2, PWM)&lt;/code&gt; và hành vi thực tế của trục, khoanh rõ bộ ba quy ước chiều tiến cho Chương 07 và phân biệt trạng thái trôi (coast) với hãm (brake).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nguồn động cơ tách khỏi USB:&lt;/strong&gt; ảnh chụp phải thể hiện rõ pin/nguồn bàn cấp cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, cáp USB chỉ cắm vào vi điều khiển và có dây nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; chung giữa hai khối.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ghi chú dòng không tải và stall:&lt;/strong&gt; ghi dòng không tải đo được cho một động cơ, cộng dòng stall đo trong 1 giây (nếu có nguồn giới hạn dòng) hoặc công suất stall tính từ datasheet, kèm câu chọn cầu chì trên dây dương pin cao hơn dòng chạy bình thường.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Xung servo:&lt;/strong&gt; nếu đã thực hành với servo, ghi ba góc đo; nếu không có servo, ghi phép tính:&lt;/li&gt;
&lt;/ul&gt;

\[\frac{1.5~\mathrm{ms}}{20~\mathrm{ms}} = 7.5\%\]

&lt;p&gt;kèm giải thích rằng duty 7,5% ở 1 kHz chỉ tạo xung 75 µs nên phải dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeMicroseconds(1500)&lt;/code&gt; từ thư viện servo.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Kiểm thử vòng kín bang-bang:&lt;/strong&gt; nêu rõ ba trường hợp bằng số liệu cụ thể (xa hợp lệ $\rightarrow$ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 80&lt;/code&gt; tiến, dưới 20 cm $\rightarrow$ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt;, timeout hoặc 0 $\rightarrow$ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt;) và khẳng định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; về sau không được ghi đè cú dừng này.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ảnh đấu dây:&lt;/strong&gt; ảnh rõ nét để bạn cùng nhóm có thể nhận diện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, dây mass chung và dây tín hiệu PWM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hai chế độ lỗi:&lt;/strong&gt; ghi hai lỗi thực tế bạn đã gặp hoặc chủ động diễn tập kiểm thử (ví dụ rút dây Echo để kiểm tra dừng an toàn, hoặc tính đối chiếu dòng stall 2,4 A so với giới hạn 0,5 A của USB), không chép nguyên văn danh sách giáo trình.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;ví-dụ-một-sổ-ghi-chép-đạt-yêu-cầu&quot;&gt;Ví dụ một sổ ghi chép đạt yêu cầu&lt;/h2&gt;

&lt;p&gt;Một sổ mẫu ghi: dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;, chiều tiến là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1 = 1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN2 = 0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM &amp;gt; 0&lt;/code&gt;; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; cấp từ pin 2S, USB chỉ cắm ESP32; đo không tải 0,18 A ở 7,4 V; tài liệu nhà bán ghi stall 1,2 A ở 6 V. Kiểm tra tính toán:&lt;/p&gt;

\[R \approx \frac{6}{1.2} = 5~\Omega\]

\[P = I^2 R = (1.2)^2 \times 5 = 7.2~\mathrm{W}\]

&lt;p&gt;Ước lượng “khoảng 7 W khi stall ở 6 V” là hợp lý; hai động cơ kẹt cùng lúc khoảng 2,4 A, trong khi tổng dòng chạy bình thường ước tính $0{,}2 + 0{,}2 + 0{,}1 = 0{,}5~\mathrm{A}$ nên chọn cầu chì 2 A trên dây dương pin. Dòng servo ghi \(1500/20000 = 0.075\); dòng bang-bang ghi 180 mm dừng, 900 mm chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 80&lt;/code&gt;, timeout dừng; hai lỗi diễn tập gồm để nổi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; (bánh không quay cho tới khi nối dây) và lỗi ban đầu xử lý timeout nhầm sang nhánh tiến rồi đã sửa. Sổ này đạt đầy đủ tiêu chí.&lt;/p&gt;

&lt;h2 id=&quot;hình-đối-chiếu&quot;&gt;Hình đối chiếu&lt;/h2&gt;

&lt;p&gt;Sử dụng ba sơ đồ sau để rà soát kiến thức trong quá trình ký duyệt bảng:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;Đường cầu H bạn đã đi khi viết bảng chân lý&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/stall_current.png&quot; alt=&quot;Stall so với không tải, đường cong đứng sau ghi chú dòng của bạn&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sense_compute_act.png&quot; alt=&quot;Cảm nhận, tính, tác động, gồm nhánh dừng mà một lần đọc hỏng phải đi&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Buổi rà soát này không yêu cầu chạy lại thí nghiệm ở công suất lớn. Nếu không đủ điều kiện đo stall an toàn, chỉ cần trình bày phần tính toán. Tuyệt đối không cấp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; từ USB để chụp ảnh; luôn nhấc bánh khi kiểm tra lại bảng chân lý và giữ tay trên công tắc nguồn nếu chạy thử trên sàn.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; và ảnh đấu dây&lt;/td&gt;
      &lt;td&gt;Hồ sơ bằng chứng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ vạn năng&lt;/td&gt;
      &lt;td&gt;Bổ sung nếu còn thiếu số đo điện áp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạch cầu H, một động cơ, cảm biến khoảng cách&lt;/td&gt;
      &lt;td&gt;Bổ sung nếu còn thiếu mục thực hành&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cầu chì và đế &lt;em&gt;(nếu chưa có)&lt;/em&gt;&lt;/td&gt;
      &lt;td&gt;Chuẩn bị sẵn cho Chương 07&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Chép bảng ký duyệt bên dưới vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Điền đầy đủ từng dòng từ kết quả các bài trước; nếu thiếu mục nào thì bổ sung phép đo hoặc phép tính tương ứng.&lt;/li&gt;
  &lt;li&gt;Đính kèm ảnh đấu dây kèm chú thích tên chip và nguồn cấp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Rà soát lại toàn bộ bảng để bảo đảm giải thích trôi chảy từng mục, đặc biệt hành vi dừng khi cảm biến timeout.&lt;/li&gt;
  &lt;li&gt;Lập danh sách mua bổ sung ngắn gọn chỉ gồm các món còn thiếu (mạch cầu H, động cơ TT, cầu chì).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Bảng ký duyệt hoàn chỉnh kèm phần tóm tắt đạt yêu cầu bằng lời của bạn sao cho người khác nhìn vào ảnh và bảng có thể xác định ngay cấu hình chiều tiến và điều kiện ngắt PWM về 0.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Cần kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Các ô chỉ ghi chữ “có” mà thiếu số liệu&lt;/td&gt;
      &lt;td&gt;Thay bằng con số cụ thể (điện áp, dòng, độ rộng xung, khoảng cách)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bảng chân lý thiếu trạng thái trôi/hãm&lt;/td&gt;
      &lt;td&gt;Xem lại bài 05-01 và ghi rõ hành vi khi hai ngõ vào bằng nhau&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ảnh chỉ thấy cáp USB mà không thấy nguồn động cơ&lt;/td&gt;
      &lt;td&gt;Chưa tách nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;; chụp lại với pin/nguồn bàn nối vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bỏ trống dòng servo vì không có phần cứng&lt;/td&gt;
      &lt;td&gt;Bổ sung phép tính 7,5% và so sánh với 75 µs ở 1 kHz&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hai lỗi chỉ chép lại từ giáo trình&lt;/td&gt;
      &lt;td&gt;Thay bằng hai tình huống thực tế bạn đã gặp hoặc diễn tập kiểm thử&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;bảng-ký-duyệt&quot;&gt;Bảng ký duyệt&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Bằng chứng của bạn&lt;/th&gt;
      &lt;th&gt;Xong&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Chip cầu H và chân dễ gây lỗi (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;, jumper 7805, hoặc dải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;)&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bảng chân lý: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM&lt;/code&gt;, chiều quay, gồm trôi hoặc hãm&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nguồn động cơ tách khỏi USB, nối chung mass (ảnh)&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dòng không tải, và dòng stall đo được hoặc công suất stall tính tay&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Servo: ba góc đo, hoặc \(1.5/20 = 7.5\%\) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeMicroseconds(1500)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bang-bang: xa, dưới 20 cm, timeout/0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ảnh đấu dây đã lưu&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hai chế độ lỗi đã gặp hoặc diễn tập&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Điều kiện hoàn thành:&lt;/strong&gt; điền đầy đủ mọi dòng trong bảng, ảnh đấu dây khớp sơ đồ tách nguồn và trình bày rõ cơ chế ép động cơ dừng khi cảm biến timeout. Khi đạt mốc này, bạn đã sẵn sàng sang phần cơ khí Chương 06 và đấu dây hoàn chỉnh ở Chương 07.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Chỉ mua bổ sung linh kiện còn thiếu (thường là TB6612 hoặc L298N, cặp động cơ TT và cầu chì 2–3 A kèm đế). Giá tham khảo thay đổi theo thời điểm (L298N khoảng 35.000–70.000 VND, TB6612 khoảng 40.000–120.000 VND, động cơ TT khoảng 25.000–45.000 VND, cầu chì và đế khoảng 5.000–25.000 VND):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Trang L298N đã kiểm tra, khoảng 45.000 VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=motor%20TT&quot;&gt;motor TT&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=L298N&quot;&gt;L298N&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=JPPTRj0KWbg&quot;&gt;TB6612FNG H-Bridge Motor Controller - Better than L298N?&lt;/a&gt; — DroneBot Workshop, tiếng Anh. Đối chiếu lại sơ đồ đấu dây và hoạt động đảo chiều/tốc độ của đúng module bạn sử dụng trước khi chốt chương.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nếu một ô trong bảng ký duyệt chỉ ghi chữ “có”, điều gì xảy ra với kết quả kiểm tra dù các ô khác đều có số liệu?&lt;/li&gt;
  &lt;li&gt;Tài liệu ghi dòng stall 1,2 A ở 6 V. Tính \(R\) và \(P\), rồi đánh giá nhận định “khoảng 7 W” có đạt yêu cầu tính toán không.&lt;/li&gt;
  &lt;li&gt;Nếu không có servo vật lý, cần ghi phép tính và lời gọi hàm nào vào dòng servo?&lt;/li&gt;
  &lt;li&gt;Liệt kê đủ ba trường hợp bắt buộc phải có trong dòng bang-bang.&lt;/li&gt;
  &lt;li&gt;Cần thỏa mãn điều kiện gì (bao gồm hành vi timeout) để chính thức chuyển sang Chương 06?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Dòng đó không đạt; mỗi mục yêu cầu bằng chứng cụ thể (số liệu đo, tên chân, phép tính hoặc ảnh).&lt;/li&gt;
  &lt;li&gt;\(R \approx 5~\Omega\) và \(P = 7.2~\mathrm{W}\), nên nhận định “khoảng 7 W” đạt yêu cầu.&lt;/li&gt;
  &lt;li&gt;Ghi \(1.5/20 = 7.5\%\) và lời gọi hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeMicroseconds(1500)&lt;/code&gt; (kèm đối chiếu 75 µs ở 1 kHz).&lt;/li&gt;
  &lt;li&gt;Ba trường hợp: khoảng cách xa hợp lệ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 80&lt;/code&gt; tiến), khoảng cách dưới 20 cm (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt;), và timeout hoặc 0 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Hoàn thành đủ tất cả các dòng bảng ký duyệt bằng bằng chứng thực tế và xác nhận cảm biến timeout luôn ép động cơ dừng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Datasheet ST L298: &lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;https://www.st.com/resource/en/datasheet/l298.pdf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Pololu TB6612FNG: &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;https://www.pololu.com/product/713&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;SparkFun — chọn động cơ (stall so với không tải): &lt;a href=&quot;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&quot;&gt;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Topic ROS 2 (cơ chế cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; về sau): &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html&quot;&gt;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab: vòng kín cảm nhận → tính → tác động</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_06_closed_loop_intro/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_06_closed_loop_intro</id>
   <content type="html">&lt;p&gt;Mọi bộ điều khiển robot về sau đều tuân theo chung một vòng lặp ba khối: &lt;strong&gt;cảm nhận&lt;/strong&gt; (sense), &lt;strong&gt;tính toán&lt;/strong&gt; (compute) và &lt;strong&gt;tác động&lt;/strong&gt; (act). Bài thực hành này khép kín vòng lặp đó bằng luật điều khiển đơn giản nhất (điều khiển bật/tắt hay bang-bang): nếu khoảng cách nhỏ hơn 20 cm thì đưa PWM bánh xe về 0, ngược lại cho phép chạy tiến ở mức PWM vừa phải. Bộ điều khiển PID sẽ học ở Chương 08; trọng tâm của bài hôm nay là thiết lập nguyên tắc an toàn bắt buộc cho mọi luật điều khiển sau này: &lt;strong&gt;cảm biến lỗi hoặc mất tín hiệu thì phải dừng&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Nếu bạn đặt hai bánh xe robot cùng một mức PWM 60% và thả chạy thẳng trên hành lang, chuyện gì sẽ xảy ra? Chỉ sau 2 mét, robot sẽ lượn cong sang một bên! Lý do rất đời thường: không có hai động cơ nào giống hệt nhau 100% về ma sát, điện áp pin giảm dần mỗi phút, và mặt sàn chỗ trơn chỗ nhám. &lt;strong&gt;Điều khiển vòng kín (closed-loop control)&lt;/strong&gt; biến một cỗ máy “mù” chạy theo lệnh cố định thành một hệ thống biết tự sửa sai liên tục qua chu trình 3 bước kinh điển: &lt;strong&gt;Cảm nhận (Sense) → Tính sai số (Compute) → Tác động (Act)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-vòng-hở-vs-vòng-kín-và-tư-duy-phản-hồi-âm&quot;&gt;Kiến thức cần nhớ: Vòng hở vs. Vòng kín và Tư duy phản hồi âm&lt;/h2&gt;

&lt;h3 id=&quot;1-hạn-chế-cố-hữu-của-vòng-hở-open-loop&quot;&gt;1. Hạn chế cố hữu của vòng hở (Open-Loop)&lt;/h3&gt;
&lt;p&gt;Trong hệ vòng hở, lệnh điều khiển \(u\) chỉ phụ thuộc vào mục tiêu \(r\) mà không hề nhìn xem đầu ra thực tế \(y\) đang ở đâu. Bất kỳ &lt;strong&gt;nhiễu loạn ngoài (disturbance)&lt;/strong&gt; nào — như robot leo lên thảm dày, chở thêm tải, hay pin tụt từ 8,4 V xuống 7,2 V — đều truyền thẳng 100% thành sai số đầu ra.&lt;/p&gt;

&lt;h3 id=&quot;2-phản-hồi-âm-negative-feedback-và-sức-mạnh-khử-nhiễu&quot;&gt;2. Phản hồi âm (Negative Feedback) và sức mạnh khử nhiễu&lt;/h3&gt;
&lt;p&gt;Hệ vòng kín đo đầu ra thực tế \(y\) (ví dụ vận tốc bánh từ encoder), tính sai số \(e = r - y\), rồi điều chỉnh lệnh tác động \(u = K_p e\) ngược chiều sai số: nếu bánh chạy chậm hơn mục tiêu (\(e &amp;gt; 0\)), tăng PWM lên; nếu chạy lố (\(e &amp;lt; 0\)), giảm PWM xuống. Chính cơ chế phản hồi âm này tự động nén ảnh hưởng của nhiễu loạn và sai lệch phần cứng xuống \((1 + K_p)\) lần!&lt;/p&gt;

&lt;h3 id=&quot;3-độ-trễ-lấy-mẫu-và-ranh-giới-mất-ổn-định&quot;&gt;3. Độ trễ lấy mẫu và ranh giới mất ổn định&lt;/h3&gt;
&lt;p&gt;Phản hồi âm không thể vặn hệ số \(K_p\) lớn vô hạn. Vì mọi vòng điều khiển số đều có chu kỳ lấy mẫu \(\Delta t\) và quán tính cơ khí, tăng \(K_p\) quá cao sẽ khiến robot liên tục sửa “quá tay” sang trái rồi lại quá tay sang phải — sinh ra &lt;strong&gt;dao động (oscillation)&lt;/strong&gt; hoặc mất ổn định. Đây chính là tiền đề dẫn tới bộ điều khiển PID ở Chương 08.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-chứng-minh-phản-hồi-âm-nén-sai-số-nhiễu-loạn-giảm-đi-5-lần&quot;&gt;Giải thích: Cầm tay chứng minh phản hồi âm nén sai số nhiễu loạn giảm đi 5 lần&lt;/h3&gt;

&lt;p&gt;Xét một bánh xe robot có mô hình tĩnh đơn giản: ở trạng thái xác lập, tốc độ bánh bằng lệnh điều khiển cộng với nhiễu tải (ví dụ khi leo lên thảm dày làm hụt tốc độ \(d = -0{,}30\text{ m/s}\)):
\(v = u + d, \qquad \text{mục tiêu } v^* = 1{,}00\text{ m/s}.\)&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Trường hợp 1 — Điều khiển vòng hở (Open-Loop):&lt;/strong&gt;
Ta đặt cứng lệnh \(u = v^* = 1{,}00\). Khi gặp thảm dày (\(d = -0{,}30\)):
\(v_{\text{hở}} = 1{,}00 - 0{,}30 = 0{,}70\text{ m/s} \implies \text{Sai số } e_{\text{hở}} = 1{,}00 - 0{,}70 = 0{,}30\text{ m/s}\;(30\%!).\)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Trường hợp 2 — Điều khiển vòng kín tỷ lệ với \(u = u_{\text{ff}} + K_p(v^* - v)\) (đặt \(u_{\text{ff}} = 1{,}00\), độ lợi \(K_p = 4\)):&lt;/strong&gt;
Thay vào phương trình \(v = u_{\text{ff}} + K_p(v^* - v) + d\):
\(v = 1{,}00 + 4(1{,}00 - v) - 0{,}30 \implies v + 4v = 4{,}70 \implies 5v = 4{,}70 \implies v_{\text{kín}} = 0{,}94\text{ m/s}!\)
Sai số còn lại chỉ là:
\(e_{\text{kín}} = 1{,}00 - 0{,}94 = 0{,}06\text{ m/s} = \frac{0{,}30}{1 + K_p} = \frac{0{,}30}{5}!\)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Không cần biết trước tấm thảm dày bao nhiêu hay pin yếu đi mấy volt, chỉ riêng một vòng phản hồi tỷ lệ với \(K_p = 4\) đã tự động nén sai số tốc độ từ &lt;strong&gt;30% xuống còn 6%&lt;/strong&gt; (giảm đúng &lt;strong&gt;5 lần&lt;/strong&gt;) — và đó mới chỉ là khởi đầu trước khi ta thêm thành phần tích phân (I) để kéo sai số về đúng \(0\)!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn vẽ được sơ đồ vòng kín cảm nhận–tính–tác động nối cảm biến khoảng cách với lệnh điều khiển động cơ. Bạn triển khai được luật bang-bang tại ngưỡng 20 cm có xử lý timeout ép PWM về 0, đồng thời bổ sung vùng trễ (hysteresis) để động cơ không đóng ngắt liên tục quanh ngưỡng. Bạn cũng xác định được vị trí của subscriber ROS 2 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; trên sơ đồ và giải thích vì sao cơ chế dừng an toàn vẫn phải giữ lại ở tầng firmware.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn cần hàm đọc khoảng cách đã kiểm chứng từ Chương 04 (siêu âm hoặc ToF, quy đổi sang milimét hoặc centimét), một kênh động cơ có bảng chân lý rõ ràng từ bài 05-01 trên mạch cầu H đã nhận diện ở bài 05-02. Luôn thử nghiệm với bánh nhấc khỏi mặt bàn trước khi hạ xuống sàn; chuẩn bị sẵn một tấm bìa carton và công tắc cắt nguồn động cơ trong tầm tay.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Capstone A cần vừa tiến về phía trước vừa dừng lại kịp thời trước vật cản. Sang Chương 07 ta mở rộng ra hai kênh PWM có dấu, và sang Chương 08 ta thay khối tính bật/tắt bằng PID để giữ tốc độ hoặc hướng, nhưng cấu trúc cảm nhận và tác động giữ nguyên. Khi tích hợp ROS 2, một subscriber lắng nghe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; sẽ cung cấp vận tốc mong muốn cho khối tính, nhưng quyền ngắt khẩn cấp theo cảm biến vẫn phải thuộc về firmware: nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; yêu cầu đi tới trong khi cảm biến báo quá gần hoặc mất dữ liệu, bánh xe vẫn bắt buộc dừng.&lt;/p&gt;

&lt;h2 id=&quot;luật-điều-khiển-bang-bang-và-vùng-trễ&quot;&gt;Luật điều khiển bang-bang và vùng trễ&lt;/h2&gt;

&lt;p&gt;Ở mức cơ bản nhất, nếu khoảng cách nhỏ hơn 20 cm thì xuất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt;; nếu lớn hơn rõ rệt và số đọc hợp lệ thì xuất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 80&lt;/code&gt; (thang 8-bit, tốc độ chậm an toàn) theo chiều tiến đã ghi trong bảng chân lý:&lt;/p&gt;

\[\mathrm{PWM} =
\begin{cases}
0 &amp;amp; \text{nếu khoảng cách} &amp;lt; 20~\mathrm{cm} \\
80 &amp;amp; \text{còn lại, khi số đọc hợp lệ}
\end{cases}\]

&lt;p&gt;Điều kiện “khi số đọc hợp lệ” cực kỳ quan trọng: nếu cảm biến bị timeout, trả về giá trị lỗi hoặc trả về 0 do mất sóng phản hồi, hệ thống bắt buộc rẽ vào nhánh dừng. Tuyệt đối không để lỗi cảm biến bị hiểu nhầm là “đường trống”.&lt;/p&gt;

&lt;p&gt;Để tránh hiện tượng động cơ đóng ngắt lạch cạch khi vật cản đứng sát mốc 20 cm (nhiễu dao động quanh 19–21 cm), ta thêm một &lt;strong&gt;vùng trễ (hysteresis)&lt;/strong&gt;: dừng khi khoảng cách xuống dưới 20 cm (200 mm) và chỉ cho chạy lại khi khoảng cách vượt trên 25 cm (250 mm); trong khoảng từ 200 mm đến 250 mm, giữ nguyên trạng thái PWM hiện tại.&lt;/p&gt;

&lt;h2 id=&quot;cấu-trúc-ba-khối-trong-mã-nguồn&quot;&gt;Cấu trúc ba khối trong mã nguồn&lt;/h2&gt;

&lt;p&gt;Khối &lt;strong&gt;cảm nhận&lt;/strong&gt; là hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readRangeMm()&lt;/code&gt; từ Chương 04 được bọc lại để trả về số âm khi timeout hoặc lỗi; khối &lt;strong&gt;tính&lt;/strong&gt; là cấu trúc điều kiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;else if&lt;/code&gt;; khối &lt;strong&gt;tác động&lt;/strong&gt; điều khiển các chân hướng và PWM.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_IN1&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// sửa thành cặp tiến của bạn&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_IN2&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_PWM&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_TRIG&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// siêu âm; bỏ qua nếu dùng ToF&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_ECHO&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_FORWARD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STOP_MM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// 20 cm&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GO_MM&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// 25 cm, nhả trễ&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_IN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_IN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_IN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// tiến, theo bảng chân lý CỦA BẠN&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_IN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;analogWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;// tác động an toàn, trước mọi lần đọc cảm biến&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;readRangeMm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;// Chương 04; âm nghĩa là timeout / không hợp lệ&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STOP_MM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;analogWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GO_MM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;analogWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_FORWARD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// giữa 200 và 250 mm, giữ PWM trước đó&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readRangeMm()&lt;/code&gt; timeout, giá trị âm làm điều kiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mm &amp;lt; 0&lt;/code&gt; đúng và ép PWM về 0; giá trị 0 cũng thỏa \(0 \le 200\) nên lập tức dừng.&lt;/p&gt;

&lt;p&gt;Quy trình kiểm thử: đặt xe trên giá (bánh nhấc khỏi bàn), đưa tay ra xa để thấy bánh quay chậm, đưa tay vào trong 20 cm để thấy bánh dừng, sau đó che kín cảm biến hoặc rút tạm dây Echo để xác nhận bánh tự dừng khi mất tín hiệu. Chỉ sau khi vượt qua bài thử này mới đặt robot xuống sàn hướng về tấm bìa carton (chạy đoạn ngắn, tay sẵn sàng trên công tắc nguồn động cơ).&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-minh-họa-vùng-trễ&quot;&gt;Ví dụ minh họa vùng trễ&lt;/h2&gt;

&lt;p&gt;Với ngưỡng dừng 200 mm và ngưỡng chạy lại 250 mm:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Đọc được 180 mm (&amp;lt; 200 mm) $\rightarrow$ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Đọc được 900 mm ($\ge 250$ mm) $\rightarrow$ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 80&lt;/code&gt; (tiến).&lt;/li&gt;
  &lt;li&gt;Đọc được 0 hoặc timeout ($-1$) $\rightarrow$ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt; (dừng an toàn).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Theo dõi chuỗi tiến lại gần rồi lùi ra xa:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Tay tiến lại: 400 mm (tiến) $\rightarrow$ 260 mm (vẫn tiến) $\rightarrow$ 220 mm (trong vùng trễ, giữ tiến) $\rightarrow$ 180 mm (dừng).&lt;/li&gt;
  &lt;li&gt;Tay lùi ra: 220 mm (trong vùng trễ, vẫn dừng) $\rightarrow$ 260 mm (chạy tiến trở lại).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nhờ khoảng đệm 50 mm này, nhiễu đo quanh mốc 200 mm (chẳng hạn nhảy giữa 190 mm và 210 mm) không còn làm hộp số bật tắt liên tục.&lt;/p&gt;

&lt;h2 id=&quot;hình-minh-họa&quot;&gt;Hình minh họa&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sense_compute_act.png&quot; alt=&quot;Cảm nhận, tính, tác động: số đọc khoảng cách vào, một quyết định được đưa, một lệnh motor ra&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Đối chiếu ba khối với phần cứng của bạn: cảm biến siêu âm/ToF $\rightarrow$ khối điều kiện bang-bang $\rightarrow$ PWM cầu H. Khi bổ sung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; từ ROS vào khối tính về sau, nhánh dừng khẩn cấp theo cảm biến vẫn phải nằm chặn phía sau trong firmware.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Giai đoạn 1: kê khung xe để bánh quay tự do trên không, có giới hạn dòng hoặc cầu chì, USB chỉ cấp cho vi điều khiển. Giai đoạn 2 &lt;em&gt;(tùy chọn)&lt;/em&gt;: chạy thử đoạn ngắn trên sàn hướng về tấm bìa carton, giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 80&lt;/code&gt; (không đặt 255) và để tay sẵn trên công tắc nguồn động cơ. Không được bỏ qua nhánh kiểm tra timeout.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Vi điều khiển và cảm biến khoảng cách (Chương 04)&lt;/td&gt;
      &lt;td&gt;Khối cảm nhận&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Một động cơ và mạch cầu H đã nhận diện&lt;/td&gt;
      &lt;td&gt;Khối tác động&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin hoặc nguồn có giới hạn dòng cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, mass chung&lt;/td&gt;
      &lt;td&gt;Nguồn động cơ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tấm bìa carton&lt;/td&gt;
      &lt;td&gt;Vật cản thử nghiệm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Công tắc nguồn trong tầm tay&lt;/td&gt;
      &lt;td&gt;Ngắt khẩn cấp bằng tay&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sổ tay&lt;/td&gt;
      &lt;td&gt;Ghi ba trường hợp thử và kết quả timeout&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Kiểm tra lại hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readRangeMm()&lt;/code&gt; bằng thước tại mốc 20 cm và 50 cm trước khi bật động cơ.&lt;/li&gt;
  &lt;li&gt;Nạp mã nguồn trên với đúng chân hướng từ bảng chân lý của bạn, bảo đảm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt; lúc khởi động.&lt;/li&gt;
  &lt;li&gt;Với bánh nhấc khỏi bàn, thử lần lượt ba tình huống: đưa tay ở ~40 cm, đưa tay vào ~15 cm và che cảm biến/rút dây tín hiệu để kiểm tra timeout; ghi hành vi PWM tương ứng.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;(Tùy chọn)&lt;/em&gt; Đặt robot xuống sàn hướng về tấm bìa carton với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 80&lt;/code&gt; để xác nhận xe tự dừng gần mốc 20 cm; ngắt công tắc ngay nếu xe không dừng.&lt;/li&gt;
  &lt;li&gt;Ghi lại cơ chế vùng trễ trong hai dòng: khoảng cách dừng (200 mm) và khoảng cách chạy lại (250 mm).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Sổ tay ghi rõ ba trường hợp (xa $\rightarrow$ tiến, gần $\rightarrow$ dừng, timeout/0 $\rightarrow$ dừng), khoảng cách dừng thực tế (nếu có chạy sàn) và câu khẳng định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; về sau không được phép ghi đè lệnh dừng an toàn này.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Cần kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh vẫn quay khi rút cảm biến&lt;/td&gt;
      &lt;td&gt;Nhánh timeout bị rơi sang nhánh tiến; cần trả số âm và kiểm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mm &amp;lt; 0&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh đóng ngắt lạch cạch quanh 20 cm&lt;/td&gt;
      &lt;td&gt;Ngưỡng dừng và ngưỡng chạy lại trùng nhau; tách thành 200 mm và 250 mm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh quay lùi&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN2&lt;/code&gt; ngược với dòng tiến trong bảng chân lý&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh không bao giờ quay&lt;/td&gt;
      &lt;td&gt;Vật cản quá gần, mọi số đọc lỗi hoặc quên kéo cao chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enable&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Xe không dừng khi chạy trên sàn&lt;/td&gt;
      &lt;td&gt;Nhầm đơn vị (so sánh cm với 200 mm) hoặc cảm biến đặt lệch khỏi bìa; ngắt nguồn ngay&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài thực hành tận dụng lại cảm biến từ Chương 04 và mạch cầu H từ bài 05-02; chỉ mua bổ sung món còn thiếu (giá tham khảo: HC-SR04 khoảng 15.000–35.000 VND, động cơ TT khoảng 25.000–45.000 VND, L298N khoảng 35.000–70.000 VND):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Trang L298N đã kiểm tra, khoảng 45.000 VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=HC-SR04&quot;&gt;HC-SR04&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=HC-SR04&quot;&gt;HC-SR04&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=HC-SR04&quot;&gt;HC-SR04&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=VL53L0X&quot;&gt;VL53L0X&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=HC-SR04&quot;&gt;HC-SR04&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=O-OqgFE9SD4&quot;&gt;Control Systems Lectures - Closed Loop Control&lt;/a&gt; — Brian Douglas, tiếng Anh. Giới thiệu nguyên lý vòng kín (đo, so sánh với mục tiêu và hiệu chỉnh lệnh), khớp với cấu trúc bài thực hành.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=UR0hOmjaHp0&quot;&gt;PID Control - A brief introduction&lt;/a&gt; — Brian Douglas, tiếng Anh. Tổng quan về PID như bước phát triển tiếp theo của khối tính toán ở Chương 08.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Với ngưỡng dừng 200 mm và ngưỡng chạy lại 250 mm, hệ thống xuất mức PWM nào khi cảm biến lần lượt báo 180 mm, 900 mm và timeout?&lt;/li&gt;
  &lt;li&gt;Vì sao giá trị đo bằng 0 phải đi vào nhánh dừng?&lt;/li&gt;
  &lt;li&gt;Động cơ đóng ngắt liên tục khi giữ tay quanh mốc 20 cm. Cần tách hai hằng số nào để khắc phục?&lt;/li&gt;
  &lt;li&gt;Một bản tin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; yêu cầu đi tới đúng lúc cảm biến đo được 12 cm. Firmware phải xử lý ra sao?&lt;/li&gt;
  &lt;li&gt;Tóm tắt trong một câu vai trò của khối tính toán bang-bang triển khai trong bài hôm nay (trước khi học PID ở Chương 08).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;180 mm $\rightarrow$ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt; (dừng); 900 mm $\rightarrow$ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 80&lt;/code&gt; (tiến); timeout $\rightarrow$ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt; (dừng).&lt;/li&gt;
  &lt;li&gt;Giá trị 0 hoặc biểu thị lỗi timeout hoặc khoảng cách sát bằng 0 — cả hai đều không được coi là đường trống.&lt;/li&gt;
  &lt;li&gt;Tách ngưỡng dừng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STOP_MM = 200&lt;/code&gt;) và ngưỡng chạy lại (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GO_MM = 250&lt;/code&gt;), giữ nguyên trạng thái trong khoảng giữa.&lt;/li&gt;
  &lt;li&gt;Firmware bắt buộc dừng động cơ vì khoảng cách 12 cm vi phạm ngưỡng an toàn, không cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; ghi đè.&lt;/li&gt;
  &lt;li&gt;Khối tính bang-bang xuất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt; khi khoảng cách dưới 20 cm hoặc cảm biến lỗi/timeout, và xuất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 80&lt;/code&gt; tiến khi khoảng cách hợp lệ vượt ngưỡng nhả của vùng trễ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Control Guru — điều khiển bật/tắt (on/off) và đáp ứng ngõ ra quanh điểm đặt: &lt;a href=&quot;https://controlguru.com/the-on-off-controller-output-response-to-set-point-changes/&quot;&gt;https://controlguru.com/the-on-off-controller-output-response-to-set-point-changes/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Topic ROS 2 (cơ chế truyền tin mà subscriber &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; sẽ sử dụng ở Chương 09): &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html&quot;&gt;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Giới hạn dòng, stall và nhiệt</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_05_current_thermal/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_05_current_thermal</id>
   <content type="html">&lt;p&gt;Thông số động cơ có hai mức dòng điện rất khác nhau: dòng không tải đo khi bánh quay tự do trên không, và dòng kẹt trục (stall) khi rô-to đứng yên khiến suất điện động ngược bằng 0. Bài này giúp bạn quy đổi dòng stall thành công suất tỏa nhiệt trên động cơ và mạch cầu H, chọn cỡ cầu chì phù hợp trên dây dương pin và xử lý tình huống sụt áp (brownout) như một lỗi điều khiển.&lt;/p&gt;

&lt;p&gt;Rất nhiều mạch driver và động cơ robot chết không phải vì quá áp, mà vì một kịch bản quen thuộc: bánh xe robot kẹt vào chân bàn, phần mềm vẫn vô tư đẩy PWM 100%, dòng điện vọt lên mức &lt;strong&gt;dòng kẹt (stall current)&lt;/strong&gt; cao gấp 5–10 lần dòng chạy bình thường, và chỉ trong vài chục giây lớp sơn cách điện cuộn dây đồng nóng chảy bốc khói! Hiểu về &lt;strong&gt;giới hạn dòng, dòng kẹt và mô hình nhiệt&lt;/strong&gt; là tấm khiên bắt buộc để bảo vệ cả phần cứng lẫn an toàn cháy nổ trong phòng lab.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-định-luật-joule-mạch-nhiệt-tương-đương-và-cắt-dòng-bảo-vệ&quot;&gt;Kiến thức cần nhớ: Định luật Joule, Mạch nhiệt tương đương và Cắt dòng bảo vệ&lt;/h2&gt;

&lt;h3 id=&quot;1-vì-sao-dòng-kẹt-stall-current-lại-nguy-hiểm-theo-hàm-bình-phương&quot;&gt;1. Vì sao dòng kẹt (Stall Current) lại nguy hiểm theo hàm bình phương?&lt;/h3&gt;
&lt;p&gt;Khi bánh xe bị chặn cứng (\(\omega = 0\)), sức phản điện động \(E = K_e \omega\) tụt về \(0\), toàn bộ điện áp pin đặt thẳng lên điện trở cuộn dây nhỏ xíu \(R_a\): \(I_{\text{stall}} = V / R_a\). Tệ hơn nữa, công suất tỏa nhiệt tuân theo định luật Joule &lt;strong&gt;\(P_{\text{nhiệt}} = I^2 R_a\)&lt;/strong&gt;: dòng tăng lên \(4\) lần thì nhiệt lượng đốt nóng cuộn dây tăng vọt lên &lt;strong&gt;\(16\) lần&lt;/strong&gt;!&lt;/p&gt;

&lt;h3 id=&quot;2-mô-hình-điện-trở-nhiệt-thermal-resistance-r_theta&quot;&gt;2. Mô hình điện trở nhiệt (Thermal Resistance \(R_{\theta}\))&lt;/h3&gt;
&lt;p&gt;Trong kỹ thuật điện tử công suất, nhiệt chảy từ nhân bán dẫn (junction) ra môi trường y hệt dòng điện chảy qua điện trở:
\(T_J = T_{\text{amb}} + P_{\text{loss}} \cdot R_{\theta JA},\)
với \(T_J\) là nhiệt độ nhân chip (thường giới hạn tối đa \(125\text{–}150^\circ\text{C}\)), \(T_{\text{amb}}\) là nhiệt độ phòng, và \(R_{\theta JA}\) (\(^\circ\text{C/W}\)) là tổng trở nhiệt từ nhân ra không khí. Chỉ cần biết công suất tổn hao \(P_{\text{loss}}\) và \(R_{\theta JA}\) trên datasheet, bạn tính được ngay chip có cần gắn tản nhiệt hay không trước khi cấp nguồn!&lt;/p&gt;

&lt;h3 id=&quot;3-ba-lớp-phòng-thủ-cầu-chì-giới-hạn-dòng-phần-cứng-chopper-và-giám-sát-phần-mềm&quot;&gt;3. Ba lớp phòng thủ: Cầu chì, Giới hạn dòng phần cứng (Chopper) và Giám sát phần mềm&lt;/h3&gt;
&lt;p&gt;Một hệ thống robot chuẩn không bao giờ dựa vào một lớp bảo vệ duy nhất: (1) &lt;strong&gt;Cầu chì / polyfuse&lt;/strong&gt; cắt mạch khi ngắn mạch nặng; (2) &lt;strong&gt;Giới hạn dòng chủ động (current limiting / Vref)&lt;/strong&gt; trên driver tự băm xung khi dòng chạm ngưỡng; và (3) &lt;strong&gt;Watchdog phát hiện kẹt trong firmware&lt;/strong&gt; (thấy PWM cao mà xung encoder bằng 0 trong &amp;gt; 1 giây thì tự ngắt động cơ).&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-công-suất-đốt-nhiệt-khi-kẹt-trục-và-nhiệt-độ-nhân-chip-driver&quot;&gt;Giải thích: Cầm tay tính công suất đốt nhiệt khi kẹt trục và nhiệt độ nhân chip driver&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Sức nóng khủng khiếp khi kẹt trục (Stall) so với khi có giới hạn dòng:&lt;/strong&gt;
Xét động cơ DC \(12\text{ V}\) có điện trở cuộn dây \(R_a = 1{,}5\,\Omega\):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Khi kẹt cứng không có giới hạn dòng:&lt;/strong&gt;
\(I_{\text{stall}} = \frac{12\text{ V}}{1{,}5\,\Omega} = 8{,}0\text{ A} \implies P_{\text{nhiệt, stall}} = I_{\text{stall}}^2 R_a = 8{,}0^2 \times 1{,}5 = 96{,}0\text{ W}!\)
Động cơ nhỏ cầm tay đang biến thành một chiếc mỏ hàn \(96\text{ W}\) — cháy cuộn dây chỉ trong vài giây!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khi mạch driver đặt giới hạn dòng chủ động ở \(I_{\lim} = 2{,}0\text{ A}\):&lt;/strong&gt;
\(P_{\text{nhiệt, lim}} = 2{,}0^2 \times 1{,}5 = 6{,}0\text{ W}\)
— giảm dòng đi \(4\times\) nhưng cắt nhiệt lượng đốt nóng xuống tới &lt;strong&gt;\(16\times\)&lt;/strong&gt; (từ \(96\text{ W}\) còn \(6\text{ W}\))!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Kiểm tra nhiệt độ nhân chip driver bằng điện trở nhiệt:&lt;/strong&gt;
Giả sử một chip driver không gắn tản nhiệt có \(R_{\theta JA} = 45^\circ\text{C/W}\), đang tỏa công suất tổn hao \(P_{\text{loss}} = 2{,}5\text{ W}\) trong phòng lab nóng \(T_{\text{amb}} = 30^\circ\text{C}\):
\(T_J = 30 + 2{,}5 \times 45 = 30 + 112{,}5 = 142{,}5^\circ\text{C}!\)
Con số \(142{,}5^\circ\text{C}\) đã chạm sát ngưỡng ngắt nhiệt (\(150^\circ\text{C}\)) của hầu hết IC bán dẫn — chứng minh bằng tính tay rằng bạn &lt;em&gt;bắt buộc&lt;/em&gt; phải giảm dòng tải, đổi sang driver MOSFET có \(R_{\text{DS(on)}}\) nhỏ hơn, hoặc gắn tản nhiệt trước khi chạy liên tục!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn phân biệt rõ dòng không tải với dòng stall và dùng dòng stall làm cơ sở chọn mạch cầu H cũng như cầu chì. Từ điện áp định mức và dòng stall, bạn ước lượng được điện trở cuộn dây và công suất nhiệt tỏa ra trong động cơ khi bị kẹt. Bạn tính được nhiệt tỏa trên L298N từ mức sụt áp của chip và giải thích vì sao TB6612 mát hơn ở cùng dòng tải. Bạn cũng biết vị trí lắp cầu chì trên dây dương pin và lý do phải coi hiện tượng sụt nguồn reset vi điều khiển là một sự cố điều khiển cần chặn bằng phần mềm.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn chỉ cần vận dụng định luật Ohm, công thức công suất \(P = I^2 R\), khái niệm suất điện động ngược (bài 05-01) và đặc tính các mạch cầu H (bài 05-02). Để đo thực tế, cần nguồn bàn có giới hạn dòng hoặc ampe kế mắc nối tiếp; nếu không có thiết bị giới hạn dòng an toàn, bạn thực hiện nhánh tính toán từ thông số datasheet.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Chương 07 bắt buộc dùng pin kèm cầu chì vì cổng USB không đủ dòng cho hai động cơ TT khi kẹt trục. Ngay cả bộ pin 18650 yếu cũng có thể bị sụt áp mạnh khi hai bánh khởi động đồng thời: mạch giám sát sụt áp (brownout) của ESP32 sẽ reset chip, các chân GPIO trở về trạng thái khởi động và một mạch cầu H chưa được khóa chặt có thể làm robot giật chạy trước khi chương trình kịp đưa PWM về 0. Vì vậy, firmware phải luôn đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt; ngay những dòng đầu tiên của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt; và giữ trạng thái dừng an toàn cho đến khi nhận lệnh hợp lệ mới.&lt;/p&gt;

&lt;h2 id=&quot;dòng-không-tải-và-dòng-kẹt-trục-stall&quot;&gt;Dòng không tải và dòng kẹt trục (stall)&lt;/h2&gt;

&lt;p&gt;Khi bánh quay tự do (không tải), suất điện động ngược triệt tiêu phần lớn điện áp nguồn nên dòng rất nhỏ (thường vài chục đến vài trăm miliampe với động cơ TT vàng). Khi trục bị kẹt đứng yên, tốc độ bằng 0, suất điện động ngược bằng 0 và dòng vọt lên cực đại:&lt;/p&gt;

\[I_{stall} \approx \frac{V}{R}\]

&lt;p&gt;Khi chạy trên sàn phẳng, dòng tiêu thụ nằm giữa hai cực trị này nhưng sẽ tăng vọt ngay khi xe leo dốc, đẩy vật cản hoặc kẹt bánh vào chân bàn. Do đó, mọi tính toán chọn mạch cầu H và cầu chì đều phải xét đến dòng stall.&lt;/p&gt;

&lt;h2 id=&quot;công-suất-nhiệt-trong-động-cơ-bị-kẹt&quot;&gt;Công suất nhiệt trong động cơ bị kẹt&lt;/h2&gt;

&lt;p&gt;Khi động cơ bị kẹt, công cơ học sinh ra gần như bằng 0 nên toàn bộ công suất điện \(P = I \times V\) chuyển thành nhiệt đốt nóng cuộn dây và chổi than. Chỉ nên giữ thử stall khoảng 1–2 giây nếu có giới hạn dòng; giữ lâu hàng chục giây sẽ làm chảy lớp men cách điện. Hai động cơ kẹt đồng thời sẽ hút khoảng gấp đôi dòng stall của một động cơ, vượt xa giới hạn ~0,5 A của cổng USB máy tính và lập tức gây sụt nguồn reset bo mạch.&lt;/p&gt;

&lt;h2 id=&quot;nhiệt-trên-mạch-cầu-h-và-hiện-tượng-sụt-áp-pin&quot;&gt;Nhiệt trên mạch cầu H và hiện tượng sụt áp pin&lt;/h2&gt;

&lt;p&gt;Với mức sụt áp khoảng 2 V ở dòng 1 A, mỗi kênh của L298N tỏa nhiệt trực tiếp trên chip khoảng:&lt;/p&gt;

\[P \approx 2~\mathrm{V} \times 1~\mathrm{A} = 2~\mathrm{W}\]

&lt;p&gt;Chạy nặng cả hai kênh sẽ tăng gấp đôi lượng nhiệt đó, đòi hỏi tấm tản nhiệt kim loại thông thoáng. TB6612 dùng MOSFET nên sụt áp nhỏ hơn nhiều và chạy mát hơn hẳn, nhưng vẫn có giới hạn dòng liên tục khoảng 1,2 A mỗi kênh — nếu vỏ chip nóng rát tay thì động cơ đang bị quá tải hoặc kẹt trục.&lt;/p&gt;

&lt;p&gt;Khi bộ pin 18650 yếu hoặc có nội trở cao, dòng khởi động làm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; sụt mạnh; nếu mạch hạ áp nuôi vi điều khiển dùng chung pin, nguồn logic cũng sụt theo gây brownout reset. Để tránh robot lao đi sau reset, hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt; phải kéo PWM về 0 và đặt chân hướng an toàn trước mọi thao tác khởi tạo khác.&lt;/p&gt;

&lt;h2 id=&quot;vai-trò-của-cầu-chì-trên-dây-dương-pin&quot;&gt;Vai trò của cầu chì trên dây dương pin&lt;/h2&gt;

&lt;p&gt;Lắp cầu chì nối tiếp ngay trên dây dương sát bộ pin để mọi sự cố ngắn mạch phía sau đều được ngắt kịp thời. Với robot nhỏ dùng pin 2S và hai động cơ TT, cầu chì cỡ 2–3 A là điểm khởi đầu hợp lý: đủ cao hơn tổng dòng chạy bình thường (ví dụ mỗi động cơ 0,3 A + logic 0,2 A = 0,8 A) nhưng vẫn kịp đứt khi xảy ra ngắn mạch hoặc kẹt trục kéo dài. Có thể dùng cầu chì tự phục hồi (polyfuse/PTC): khi quá dòng nó nóng lên để ngắt mạch và tự nối lại sau khi nguội — cần để thoáng khí, không bọc kín băng keo cách nhiệt làm sai lệch dòng cắt.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Giả sử nhà sản xuất công bố động cơ 6 V có dòng stall 1,2 A. Điện trở cuộn dây xấp xỉ:&lt;/p&gt;

\[R \approx \frac{6}{1.2} = 5~\Omega\]

&lt;p&gt;Công suất tỏa nhiệt trong cuộn dây khi kẹt ở 6 V là:&lt;/p&gt;

\[P = I^2 R = (1.2)^2 \times 5 = 1.44 \times 5 = 7.2~\mathrm{W}\]

&lt;p&gt;Kiểm tra lại bằng \(P = I \times V = 1.2 \times 6 = 7.2~\mathrm{W}\). Mức 7,2 W trong một thân động cơ nhỏ giải thích vì sao chỉ được phép đo stall tối đa một giây. Hai động cơ như vậy kẹt cùng lúc sẽ đòi tới 2,4 A — lớn hơn nhiều so với giới hạn 0,5 A của cổng USB. Nếu đồng hồ đo dòng không tải ở PWM vừa chỉ khoảng 0,15 A, khoảng cách từ 0,15 A đến 1,2 A cho thấy độ dự phòng mà driver và cầu chì phải chịu được khi xe chạm tường.&lt;/p&gt;

&lt;h2 id=&quot;hình-minh-họa&quot;&gt;Hình minh họa&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/stall_current.png&quot; alt=&quot;Dòng stall theo tốc độ: dòng nhỏ khi đang quay, dòng lớn khi trục dừng&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Phía trái đồ thị ứng với tốc độ bằng 0 (dòng stall lớn nhất), phía phải ứng với tốc độ không tải (dòng nhỏ nhất).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Digital_Multimeter_Aka.jpg&quot; alt=&quot;Đồng hồ mắc nối tiếp hoặc màn hình dòng của nguồn, dùng cho một motor với bánh trên không&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Khi đo bằng đồng hồ vạn năng, phải cắm que vào lỗ đo dòng lớn (10 A), bắt đầu ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt; để tránh đứt cầu chì đồng hồ; sử dụng bộ nguồn bàn có đồng hồ dòng tích hợp sẽ an toàn và thuận tiện hơn.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Tháo động cơ khỏi khung (hoặc nhấc bánh khỏi bàn) và chỉ thử từng động cơ một. Chỉ thực hiện bước đo stall 1 giây khi có nguồn bàn đặt giới hạn dòng khoảng 2 A; nếu không có giới hạn dòng đáng tin cậy, hãy bỏ qua bước kẹt trục thật và tính công suất từ thông số nhà sản xuất. Không cấp nguồn động cơ từ USB và luôn nối chung mass nếu dùng vi điều khiển phát PWM.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Một động cơ DC chổi than&lt;/td&gt;
      &lt;td&gt;Đối tượng kiểm thử&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nguồn bàn có giới hạn dòng, hoặc pin kèm ampe kế nối tiếp&lt;/td&gt;
      &lt;td&gt;Cấp nguồn và đo dòng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạch cầu H hoặc dây nguồn trực tiếp&lt;/td&gt;
      &lt;td&gt;Đặt điện áp xác định&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ vạn năng&lt;/td&gt;
      &lt;td&gt;Đo điện áp và dòng điện&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sổ tay&lt;/td&gt;
      &lt;td&gt;Ghi dòng không tải, dòng stall hoặc công suất tính tay&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Đặt giới hạn dòng khoảng 2 A trên nguồn bàn (nếu có) và chỉnh điện áp bằng điện áp định mức (hoặc mức vừa phải, ghi rõ vào sổ).&lt;/li&gt;
  &lt;li&gt;Cho động cơ chạy không tải, ghi điện áp và dòng điện sau 1 giây.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;(Tùy chọn, chỉ khi có nguồn giới hạn dòng)&lt;/em&gt; Giữ kẹt trục tối đa 1 giây, đọc dòng rồi thả ngay; nếu nguồn lập tức cắt dòng thì ghi “nguồn đã giới hạn” và dừng, không lặp lại.&lt;/li&gt;
  &lt;li&gt;Nếu bỏ qua bước đo stall trực tiếp, lấy dòng stall từ datasheet để tính \(R \approx V/I_{stall}\) và \(P = I^2 R\), ghi rõ là số ước lượng từ tài liệu.&lt;/li&gt;
  &lt;li&gt;Viết một câu chọn giá trị cầu chì trên dây dương pin cho hai động cơ và giải thích vì sao giá trị đó cao hơn dòng chạy bình thường.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Sổ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; có đầy đủ dòng không tải đo được, dòng stall 1 giây (hoặc công suất stall tính từ datasheet) và câu chọn cầu chì (thường 2 A hoặc 3 A cho robot TT chạy 2S) kèm so sánh với tổng dòng chạy.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Cần kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ dòng báo gần 0 dù trục đang quay&lt;/td&gt;
      &lt;td&gt;Mắc đồng hồ song song thay vì nối tiếp, hoặc cắm nhầm lỗ đo áp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sụt nguồn và vi điều khiển reset&lt;/td&gt;
      &lt;td&gt;Đang lấy nguồn động cơ từ USB hoặc pin yếu; chuyển sang pin đủ dòng có bảo vệ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tản nhiệt L298N nóng rát sau thời gian ngắn&lt;/td&gt;
      &lt;td&gt;Công suất sụt áp lớn; giảm thời gian tải nặng, tăng thoáng khí hoặc đổi TB6612&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Polyfuse ngắt và giữ trạng thái hở nhiều phút&lt;/td&gt;
      &lt;td&gt;Cầu chì đang bảo vệ mạch; chờ nguội và xử lý điểm chập trước khi thử lại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot giật chạy sau khi reset&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt; chưa ép &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt; ngay từ đầu&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Chỉ cần chuẩn bị thêm cầu chì 2–3 A (cầu chì thủy tinh, cầu chì lưỡi hoặc polyfuse kèm đế) nếu bộ kit chưa có; giá tham khảo thay đổi theo thời điểm (cầu chì và đế khoảng 5.000–25.000 VND, động cơ TT khoảng 25.000–45.000 VND, L298N khoảng 35.000–70.000 VND):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Trang L298N đã kiểm tra, khoảng 45.000 VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=polyfuse&quot;&gt;polyfuse&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=7KbEVKZ2JhA&quot;&gt;Do you know how to Test Stall Current?&lt;/a&gt; — SoundTraxx, tiếng Anh. Minh họa kỹ thuật đo dòng khi giữ kẹt trục (áp dụng tương tự cho động cơ TT).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=W770Z1yvXs8&quot;&gt;What is a fuse? the basics explained&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Giải thích nguyên lý hoạt động và cách chọn cầu chì bảo vệ mạch.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một động cơ 6 V có dòng stall 1,2 A. Tính điện trở cuộn \(R\) và công suất tỏa nhiệt khi stall.&lt;/li&gt;
  &lt;li&gt;Hai động cơ trên kẹt trục cùng lúc sẽ hút tổng dòng bao nhiêu, và vì sao cổng USB 0,5 A không thể cấp nguồn cho chúng?&lt;/li&gt;
  &lt;li&gt;Một kênh L298N sụt 2 V ở dòng tải 0,8 A. Kênh này tỏa bao nhiêu watt nhiệt?&lt;/li&gt;
  &lt;li&gt;Khi chạy bình thường, mỗi bánh hút 0,35 A và mạch logic hút 0,15 A. Cầu chì 2 A trên dây dương pin có phù hợp (lớn hơn tổng dòng chạy) không?&lt;/li&gt;
  &lt;li&gt;ESP32 bị reset mỗi khi động cơ khởi động và khiến robot tự trôi đi. Cần thay đổi gì ở phần cứng và những dòng đầu tiên của hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt;?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(R \approx 6 / 1.2 = 5~\Omega\) và \(P = 1.2^2 \times 5 = 7.2~\mathrm{W}\).&lt;/li&gt;
  &lt;li&gt;Tổng dòng khoảng 2,4 A, vượt xa mức 0,5 A của cổng USB nên phải dùng pin và cầu chì riêng.&lt;/li&gt;
  &lt;li&gt;\(P = 2~\mathrm{V} \times 0.8~\mathrm{A} = 1.6~\mathrm{W}\).&lt;/li&gt;
  &lt;li&gt;Tổng dòng chạy là \(0.35 + 0.35 + 0.15 = 0.85~\mathrm{A}\), nhỏ hơn nhiều so với 2 A nên cầu chì 2 A phù hợp.&lt;/li&gt;
  &lt;li&gt;Phần cứng: tách nguồn động cơ sang pin khỏe hơn kèm cầu chì; phần mềm: đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt; và cấu hình chân hướng an toàn ngay đầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;SparkFun — động cơ và thông số dòng stall: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&quot;&gt;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Datasheet ST L298 (sụt áp và công suất tỏa nhiệt): &lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;https://www.st.com/resource/en/datasheet/l298.pdf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Mạch mang TB6612FNG của Pololu (hiệu suất cao hơn cho dòng động cơ TT): &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;https://www.pololu.com/product/713&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Stepper và microstep</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_04_steppers_intro/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_04_steppers_intro</id>
   <content type="html">&lt;p&gt;Động cơ bước (stepper) di chuyển theo từng nấc góc rời rạc theo số xung bạn cấp. Bài này trình bày về động cơ bước hybrid $1{,}8^\circ$, tác dụng thực sự của chế độ vi bước (microstep), cách tính và chỉnh điện áp tham chiếu giới hạn dòng trên mạch lái kiểu A4988 mà không vặn biến trở theo cảm tính, cũng như lý do hệ dẫn động của Capstone A vẫn dùng động cơ DC kết hợp encoder.&lt;/p&gt;

&lt;p&gt;Nếu bạn từng đứng ngắm một chiếc máy in 3D vẽ từng đường nhựa mảnh như sợi tóc lặp đi lặp lại chính xác đến phần trăm milimet mà &lt;em&gt;không hề có một cái encoder nào trên trục động cơ&lt;/em&gt;, bạn đã chứng kiến sức mạnh của &lt;strong&gt;động cơ bước (stepper motor)&lt;/strong&gt;. Thay vì quay liên tục như động cơ DC, động cơ bước chia một vòng quay thành hàng trăm nấc từ trường rời rạc; mỗi khi bộ điều khiển gửi một xung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STEP&lt;/code&gt;, roto nhảy chính xác thêm đúng một bước góc — biến chuyển động cơ khí thành một bài toán đếm xung số hóa thuần túy!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-từ-trường-bước-rời-rạc-vi-bước-microstepping-và-đường-cong-mô-men&quot;&gt;Kiến thức cần nhớ: Từ trường bước rời rạc, Vi bước (Microstepping) và Đường cong mô-men&lt;/h2&gt;

&lt;h3 id=&quot;1-cấu-trúc-cực-từ-và-bước-góc-cơ-bản-full-step&quot;&gt;1. Cấu trúc cực từ và bước góc cơ bản (Full-Step)&lt;/h3&gt;
&lt;p&gt;Động cơ bước lưỡng cực phổ biến nhất (NEMA 17) có góc bước cơ bản &lt;strong&gt;\(1{,}8^\circ\)&lt;/strong&gt;, tương đương &lt;strong&gt;\(360^\circ / 1{,}8^\circ = 200\text{ bước/vòng}\)&lt;/strong&gt;. Nhờ điều khiển hở vòng (open-loop) theo xung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STEP&lt;/code&gt; và chân hướng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DIR&lt;/code&gt;, ta biết chính xác vị trí góc mà không cần cảm biến phản hồi — với điều kiện tuyệt đối là tải không bao giờ vượt quá mô-men giữ làm &lt;strong&gt;trượt bước (missed steps)&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;2-vi-bước-microstepping--mượt-hơn-nhờ-điều-chế-dòng-hình-sincos&quot;&gt;2. Vi bước (Microstepping) — mượt hơn nhờ điều chế dòng hình sin–cos&lt;/h3&gt;
&lt;p&gt;Chạy bước đủ (full-step) gây rung và cộng hưởng cơ khí mạnh. Driver hiện đại (A4988, DRV8825, TMC2209) điều khiển dòng điện qua hai cuộn dây A và B theo hàm \(\cos\theta\) và \(\sin\theta\) để chia mỗi bước đủ thành \(4, 8, 16\) hoặc \(256\) &lt;strong&gt;vi bước (microsteps)&lt;/strong&gt;. Chuyển động trở nên êm ru, nhưng hãy nhớ một sự thật vật lý quan trọng: vi bước giúp &lt;strong&gt;tăng độ mượt&lt;/strong&gt;, chứ &lt;em&gt;không&lt;/em&gt; tăng độ chính xác tuyệt đối tương xứng vì mô-men gia tăng giữa hai vi bước kề nhau rất nhỏ!&lt;/p&gt;

&lt;h3 id=&quot;3-giới-hạn-tần-số-xung-và-sụt-giảm-mô-men-ở-tốc-độ-cao&quot;&gt;3. Giới hạn tần số xung và sụt giảm mô-men ở tốc độ cao&lt;/h3&gt;
&lt;p&gt;Khi động cơ bước quay nhanh, sức phản điện động và độ tự cảm cuộn dây \(L\) cản trở dòng điện tăng kịp trong thời gian một bước quá ngắn, khiến mô-men giảm mạnh ở RPM cao. Đó là lý do mọi máy CNC/máy in 3D đều cần &lt;strong&gt;hồ sơ tăng tốc hình thang hoặc đường cong chữ S (acceleration ramp)&lt;/strong&gt; thay vì nhảy vọt ngay từ 0 lên tốc độ tối đa.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-tần-số-xung-vi-bước-116-và-độ-sụt-mô-men-gia-tăng&quot;&gt;Giải thích: Cầm tay tính tần số xung vi bước 1/16 và độ sụt mô-men gia tăng&lt;/h3&gt;

&lt;p&gt;Xét động cơ NEMA 17 chuẩn (&lt;strong&gt;\(1{,}8^\circ/\text{bước}\)&lt;/strong&gt;, tức &lt;strong&gt;200 bước/vòng&lt;/strong&gt;) chạy ở chế độ vi bước &lt;strong&gt;1/16&lt;/strong&gt; với mục tiêu quay đều ở tốc độ &lt;strong&gt;\(120\text{ RPM}\)&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Độ phân giải góc và tổng số vi bước mỗi vòng:&lt;/strong&gt;
\(N_{\text{micro}} = 200 \times 16 = 3\,200\text{ vi bước/vòng}, \qquad \Delta\theta = \frac{1{,}8^\circ}{16} = 0{,}1125^\circ/\text{vi bước}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tần số xung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STEP&lt;/code&gt; vi điều khiển phải phát:&lt;/strong&gt;
Tốc độ \(120\text{ RPM} = \frac{120}{60} = 2\text{ vòng/giây}\).
\(f_{\text{step}} = 2\text{ vòng/s} \times 3\,200\text{ vi bước/vòng} = 6\,400\text{ Hz} \implies T_{\text{step}} = \frac{1}{6\,400} = 156{,}25\,\mu\text{s}.\)
Nếu vi điều khiển bận in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.println()&lt;/code&gt; chậm mất \(1\text{ ms}\), động cơ sẽ lập tức khựng và trượt bước!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kiểm chứng độ sụt mô-men gia tăng (Incremental Torque) ở vi bước 1/16:&lt;/strong&gt;
Mô-men kéo roto nhảy đúng 1 vi bước tỉ lệ với \(\sin\!\left(\frac{90^\circ}{M}\right)\) (với \(M = 16\) là hệ số vi bước):
\(\frac{\tau_{\text{inc}}}{\tau_{\text{hold}}} = \sin\!\left(\frac{90^\circ}{16}\right) = \sin(5{,}625^\circ) \approx 0{,}098 = 9{,}8\%!\)
Nghĩa là mỗi vi bước 1/16 chỉ tạo được chưa đầy &lt;strong&gt;10%&lt;/strong&gt; mô-men giữ toàn phần — nếu ma sát trục lớn hơn \(9{,}8\%\) mô-men giữ, roto sẽ đứng im vài vi bước rồi mới nhảy cục bộ!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn quy đổi được góc bước đầy $1{,}8^\circ$ thành 200 bước mỗi vòng và tính được số bước trên trục ra sau hộp giảm tốc. Bạn hiểu vi bước (1/2, 1/4, 1/16) giúp chuyển động mượt hơn nhưng không làm tăng mô-men giữ của động cơ. Bạn tính được điện áp tham chiếu \(V_{ref}\) cho mạch A4988 khi điện trở cảm biến dòng là \(0.068~\Omega\) và biết điều chỉnh công thức khi bo mạch clone dùng \(0.1~\Omega\). Bạn cũng nhận diện được kiểu lỗi đặc trưng của điều khiển vòng hở: mất bước dưới tải xảy ra âm thầm mà không báo lỗi.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn biết đo điện áp một chiều nhỏ (dưới 1 V) với que đen đặt trên mass, và nắm vững định luật Ohm cùng khái niệm dòng cuộn dây từ bài động cơ DC. Không bắt buộc phải có động cơ bước trên bàn; bạn có thể hoàn thành bài thực hành với ảnh chụp bo A4988/DRV8825 kết hợp phần tính toán (hoặc đếm thử một vòng nếu có sẵn NEMA17).&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Hệ bánh xe của Capstone A dùng động cơ DC kèm encoder để khép vòng phản hồi. Động cơ bước thường xuất hiện khi bạn mở rộng thêm cánh tay nhỏ, cơ cấu nâng hạ bút hoặc thanh trượt camera — những khớp cần giữ vị trí ổn định và quay góc xác định. Khi lập kế hoạch quỹ đạo khớp trên ROS, hệ thống cần biết chính xác tổng số bước trên một vòng &lt;strong&gt;trục ra sau hộp số&lt;/strong&gt; (nếu khai báo nhầm 200 bước cho trục gắn sau hộp số 50:1, khớp sẽ chỉ quay một góc rất nhỏ so với kế hoạch), đồng thời phải đặt đúng giới hạn dòng để tránh mất bước âm thầm.&lt;/p&gt;

&lt;h2 id=&quot;bước-đầy-vi-bước-và-hiện-tượng-mất-bước&quot;&gt;Bước đầy, vi bước và hiện tượng mất bước&lt;/h2&gt;

&lt;p&gt;Một động cơ bước hybrid phổ biến có góc bước đầy $1{,}8^\circ$, tương ứng:&lt;/p&gt;

\[\frac{360^\circ}{1.8^\circ} = 200\]

&lt;p&gt;bước đầy trên mỗi vòng trục động cơ. Các mạch lái kiểu băm dòng (chopper) như Allegro A4988 và TI DRV8825 điều tiết dòng trong cuộn dây quanh giới hạn đã cài đặt; chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STEP&lt;/code&gt; nhận xung dịch bước, chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DIR&lt;/code&gt; chọn chiều quay. Vì cuộn dây luôn duy trì dòng để tạo mô-men giữ ngay cả lúc trục đứng yên, động cơ bước đang cấp nguồn thường ấm lên khi nghỉ.&lt;/p&gt;

&lt;p&gt;Chế độ vi bước (1/2, 1/4, 1/8, 1/16 trên A4988 và mịn hơn trên DRV8825) chia mỗi bước đầy thành các nấc dòng trung gian nhỏ hơn, giúp giảm rung ồn và cộng hưởng cơ khí. Tuy nhiên, vi bước &lt;strong&gt;không nhân mô-men giữ&lt;/strong&gt; lên 16 lần — mô-men vẫn phụ thuộc vào động cơ, dòng cài đặt và nguồn cấp. Nếu tải ngoài vượt mô-men giữ, rô-to sẽ trượt tụt lại phía sau mà mạch lái không phát ra bất kỳ tín hiệu báo lỗi nào; bộ đếm bước của phần mềm vẫn nghĩ cơ cấu đã tới đích. Đó là bản chất của điều khiển vòng hở (chỉ khi gắn thêm encoder mới phát hiện được sai lệch vị trí này).&lt;/p&gt;

&lt;p&gt;Trên A4988, ba chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MS1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MS2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MS3&lt;/code&gt; chọn hệ số vi bước (thường từ tất cả mức thấp cho bước đầy đến tất cả mức cao cho 1/16 — luôn đối chiếu bảng tra của đúng bo mạch và không để chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MS&lt;/code&gt; bị nổi).&lt;/p&gt;

&lt;h2 id=&quot;chỉnh-biến-trở-giới-hạn-dòng&quot;&gt;Chỉnh biến trở giới hạn dòng&lt;/h2&gt;

&lt;p&gt;Trên bo A4988 chuẩn Pololu dùng điện trở cảm biến dòng \(R_{sense} = 0.068~\Omega\), mối liên hệ giữa dòng cực đại \(I_{max}\) và điện áp tham chiếu \(V_{ref}\) đo trên biến trở là:&lt;/p&gt;

\[I_{max} = \frac{V_{ref}}{8 \times 0.068} = \frac{V_{ref}}{0.544}\]

&lt;p&gt;Bạn đo \(V_{ref}\) giữa đầu vít/chân giữa (wiper) của biến trở và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; khi mạch đã cấp nguồn theo hướng dẫn nhà sản xuất. Lưu ý nhiều bo clone sử dụng điện trở \(R_{sense} = 0.1~\Omega\) (khi đó mẫu số là \(8 \times 0.1 = 0.8\) thay vì 0,544), nên bắt buộc kiểm tra trị số \(R_{sense}\) in trên bo mạch trước khi chỉnh. Không vặn biến trở hết cỡ vì dòng vượt định mức sẽ làm cháy cuộn dây hoặc quá nhiệt chip.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quy tắc an toàn bắt buộc:&lt;/strong&gt; không bao giờ rút hoặc cắm dây cuộn dây động cơ khi mạch lái đang cấp điện (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable&lt;/code&gt;). Ngắt mạch đột ngột cuộn cảm đang có dòng sẽ tạo xung điện áp ngược lớn làm hỏng chip lái ngay lập tức; luôn tắt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable&lt;/code&gt; hoặc ngắt nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; trước khi thay đổi dây.&lt;/p&gt;

&lt;h2 id=&quot;động-cơ-bước-đơn-cực-28byj-48&quot;&gt;Động cơ bước đơn cực 28BYJ-48&lt;/h2&gt;

&lt;p&gt;Bộ 28BYJ-48 kèm mạch ULN2003 là động cơ bước đơn cực (unipolar) 5 V có hộp giảm tốc nhựa. Đây là bộ thí nghiệm rẻ và dễ quan sát tốc độ chậm trên bàn, nhưng không phù hợp làm bánh dẫn động cho Capstone A vì kết cấu hộp số nhựa và mô-men/tốc độ đầu ra không đáp ứng tải khung xe.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Để đặt giới hạn dòng \(I_{max} = 1.0~\mathrm{A}\) trên bo A4988 có \(R_{sense} = 0.068~\Omega\):&lt;/p&gt;

\[V_{ref} = I_{max} \times 0.544 = 1.0 \times 0.544 = 0.544~\mathrm{V} \approx 0.54~\mathrm{V}\]

&lt;p&gt;Đặt que đen lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, que đỏ lên chân giữa/đầu vít biến trở và chỉnh nhẹ đến khoảng 0,54 V. Nếu bo mạch dùng \(R_{sense} = 0.1~\Omega\), tính lại:&lt;/p&gt;

\[V_{ref} = I_{max} \times (8 \times 0.1) = 0.80~\mathrm{V}\]

&lt;p&gt;Luôn ghi giá trị \(R_{sense}\) giả định bên cạnh mức \(V_{ref}\) đã đo.&lt;/p&gt;

&lt;p&gt;Đối với khớp nối dùng trên ROS: một động cơ 200 bước đầy chạy ở chế độ vi bước 1/16 cần \(200 \times 16 = 3200\) vi bước cho một vòng trục động cơ; nếu qua thêm hộp số 50:1 thì cần \(3200 \times 50 = 160000\) vi bước cho một vòng trục ra.&lt;/p&gt;

&lt;h2 id=&quot;hình-minh-họa&quot;&gt;Hình minh họa&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Digital_Multimeter_Aka.jpg&quot; alt=&quot;Đồng hồ số, dụng cụ bạn dùng trên chiết áp giới hạn dòng&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Que đen đặt tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; của driver, que đỏ đặt tại chân giữa (wiper) của biến trở; kết quả đo nằm ở mức dưới 1 V (nếu hiện điện áp nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; là đang đặt nhầm chân).&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Không rút dây cuộn động cơ khi driver đang có nguồn. Chỉnh xong giới hạn dòng trước khi cho chạy hành trình dài. Giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; trong dải cho phép của mạch lái (thường 8–12 V cho A4988; 5 V cho 28BYJ-48).&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ vạn năng&lt;/td&gt;
      &lt;td&gt;Đo điện áp \(V_{ref}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Module A4988 hoặc DRV8825 &lt;em&gt;(nếu có)&lt;/em&gt;&lt;/td&gt;
      &lt;td&gt;Xác định biến trở, \(R_{sense}\) và các chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MS&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ bước 4 dây &lt;em&gt;(tùy chọn)&lt;/em&gt;&lt;/td&gt;
      &lt;td&gt;Kiểm tra đếm 200 bước/vòng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hoặc ảnh chụp rõ nét của module&lt;/td&gt;
      &lt;td&gt;Dùng cho nhánh không có phần cứng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sổ tay&lt;/td&gt;
      &lt;td&gt;Ghi \(R_{sense}\), \(V_{ref}\) và số bước&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Nếu không có phần cứng: vẽ/đánh dấu vị trí biến trở và các chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MS&lt;/code&gt; từ ảnh sản phẩm, tính \(V_{ref}\) cho 1,0 A với \(0.068~\Omega\) (0,54 V) và bản clone \(0.1~\Omega\) (0,80 V). Hoàn thành đến đây là đủ.&lt;/li&gt;
  &lt;li&gt;Nếu có phần cứng: đọc trị số điện trở cảm biến dòng in trên bo mạch và ghi lại \(R_{sense}\).&lt;/li&gt;
  &lt;li&gt;Xác định đúng hai cặp dây cuộn bằng đồng hồ (cùng một cuộn có điện trở vài ohm, hai cuộn khác nhau hở mạch), đấu dây khi đã ngắt điện, sau đó cấp nguồn theo hướng dẫn và đo/chỉnh \(V_{ref}\).&lt;/li&gt;
  &lt;li&gt;Cấu hình chế độ bước đầy, dán cờ giấy lên trục, xuất chậm 200 xung và xác nhận trục NEMA17 $1{,}8^\circ$ quay đúng một vòng (lưu ý 28BYJ-48 có hộp số nên sẽ không ra 200 bước).&lt;/li&gt;
  &lt;li&gt;Giữ nhẹ trục trong lúc chạy chậm để quan sát hiện tượng mất bước (trục lệch vị trí khi chương trình kết thúc mà không báo lỗi), rồi thả tay ra trước khi mạch nóng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Sổ tay ghi rõ giá trị \(V_{ref}\) mục tiêu cho 1,0 A kèm \(R_{sense}\) tương ứng, ảnh/hình đánh dấu biến trở và chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MS&lt;/code&gt; (hoặc kết quả đếm 200 bước đầy trên NEMA17), cùng câu khẳng định rằng mất bước vòng hở không tự sinh thông báo lỗi.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Cần kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ chỉ rung/kêu mà không quay&lt;/td&gt;
      &lt;td&gt;Đấu sai cặp cuộn, giới hạn dòng quá thấp hoặc chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SLEEP&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENABLE&lt;/code&gt; chưa kích hoạt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ và chip nóng rát khi đứng yên&lt;/td&gt;
      &lt;td&gt;\(V_{ref}\) chỉnh quá cao so với dòng định mức động cơ; hạ ngay xuống&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chạy êm hơn nhưng yếu hơn khi chạm tay&lt;/td&gt;
      &lt;td&gt;Đang bật vi bước; mô-men không tăng theo hệ số chia bước&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Vị trí chỉ lệch khi mang tải&lt;/td&gt;
      &lt;td&gt;Mất bước; giảm tốc độ, chỉnh dòng tối đa đến định mức hoặc bổ sung encoder&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ hiện điện áp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; khi đo biến trở&lt;/td&gt;
      &lt;td&gt;Đặt nhầm que đỏ; chuyển đúng sang chân giữa (wiper) và đo so với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bạn có thể hoàn thành bài này mà không cần mua thêm linh kiện. Nếu muốn thực hành thêm trên bàn, bộ 28BYJ-48 + ULN2003 có giá rẻ (khoảng 25.000–50.000 VND), còn bộ NEMA17 (150.000–400.000 VND) + A4988 (20.000–55.000 VND) khớp đúng cấu hình 200 bước hybrid:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=A4988&quot;&gt;A4988&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=28BYJ-48&quot;&gt;28BYJ-48&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=NEMA17&quot;&gt;NEMA17&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=A4988&quot;&gt;A4988&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=28BYJ-48&quot;&gt;28BYJ-48&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=A4988&quot;&gt;A4988&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=DRV8825&quot;&gt;DRV8825&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=A4988&quot;&gt;A4988&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=ULN2003&quot;&gt;ULN2003&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=X4qkWxRBTEg&quot;&gt;Microstepping in bipolar stepper motors&lt;/a&gt; — Texas Instruments, tiếng Anh. Giải thích nguyên lý điều chế dòng lệch pha giữa hai cuộn dây trong vi bước.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Bs1zIZV1uSY&quot;&gt;Operating Principles of a Microstep Stepper Motor Drive&lt;/a&gt; — Oriental Motor USA, tiếng Anh. Minh họa trực quan cách chia nhỏ bước đầy để làm mượt chuyển động.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một động cơ bước $1{,}8^\circ$ cần bao nhiêu bước đầy cho một vòng quay? Ở chế độ vi bước 1/16, con số đó là bao nhiêu?&lt;/li&gt;
  &lt;li&gt;Với bo A4988 có \(R_{sense} = 0.068~\Omega\) và điện áp đo được \(V_{ref} = 0.30~\mathrm{V}\), giới hạn dòng đang đặt là bao nhiêu?&lt;/li&gt;
  &lt;li&gt;Với bo clone dùng \(R_{sense} = 0.1~\Omega\), cần đặt \(V_{ref}\) bao nhiêu để giới hạn dòng ở 0,8 A?&lt;/li&gt;
  &lt;li&gt;Một cánh tay chạy xong chương trình 400 bước nhưng đầu cánh vẫn thiếu so với vạch đích và phần mềm không báo lỗi. Hiện tượng vật lý nào đã xảy ra?&lt;/li&gt;
  &lt;li&gt;Một khớp ROS dùng động cơ 200 bước ở chế độ bước đầy qua hộp giảm tốc 30:1. Cần bao nhiêu bước cho một vòng trục ra?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;200 bước đầy và \(200 \times 16 = 3200\) vi bước 1/16.&lt;/li&gt;
  &lt;li&gt;\(I_{max} = 0.30 / 0.544 \approx 0.55~\mathrm{A}\).&lt;/li&gt;
  &lt;li&gt;\(V_{ref} = 0.8 \times (8 \times 0.1) = 0.64~\mathrm{V}\).&lt;/li&gt;
  &lt;li&gt;Mất bước (skipped steps) do điều khiển vòng hở dưới tải vượt mô-men giữ.&lt;/li&gt;
  &lt;li&gt;\(200 \times 30 = 6000\) bước trên một vòng trục ra.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Mạch mang A4988 của Pololu (hướng dẫn giới hạn dòng và công thức \(V_{ref}\)): &lt;a href=&quot;https://www.pololu.com/product/1182&quot;&gt;https://www.pololu.com/product/1182&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Datasheet TI DRV8825 (driver kiểu băm dòng và các chế độ vi bước): &lt;a href=&quot;https://www.ti.com/lit/ds/symlink/drv8825.pdf&quot;&gt;https://www.ti.com/lit/ds/symlink/drv8825.pdf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;SparkFun — chọn động cơ (đối chiếu khái niệm mô-men và dòng với động cơ DC): &lt;a href=&quot;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&quot;&gt;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Servo hobby, PWM và mô-men</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_03_servos_pwm/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_03_servos_pwm</id>
   <content type="html">&lt;p&gt;Bên trong vỏ nhựa của một servo hobby là một động cơ DC nhỏ, hộp giảm tốc, biến trở phản hồi vị trí và mạch điều khiển vòng kín tích hợp. Tín hiệu điều khiển servo dựa trên độ rộng xung tuyệt đối tính theo mili giây chứ không phải tỉ lệ phần trăm duty cycle như PWM điều tốc động cơ DC. Bài này làm rõ sự khác biệt giữa hai loại tín hiệu, giải thích ý nghĩa thông số mô-men &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kg·cm&lt;/code&gt; và cách cấp nguồn, quét góc an toàn mà không làm kẹt hay nóng động cơ.&lt;/p&gt;

&lt;p&gt;Khác với động cơ DC cứ cấp điện là quay tít mù không biết dừng ở đâu, một chiếc &lt;strong&gt;servo hobby&lt;/strong&gt; (như SG90 hay MG996R) là cả một &lt;em&gt;hệ thống điều khiển vòng kín thu nhỏ&lt;/em&gt; gói gọn trong hộp nhựa vài chục gam: bên trong nó đã có sẵn động cơ DC, hộp số giảm tốc tỉ số cao, một biến trở đo góc đầu ra và một mạch điều khiển phản hồi. Bạn không cần điều khiển tốc độ — bạn chỉ cần gửi một xung PWM mang thông điệp &lt;em&gt;“hãy quay tới đúng góc \(135^\circ\) và giữ cứng ở đó!”&lt;/em&gt;, và servo sẽ tự lo phần còn lại.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-giao-thức-xung-50-hz-hộp-số-và-đòn-bẩy-mô-men&quot;&gt;Kiến thức cần nhớ: Giao thức xung 50 Hz, Hộp số và Đòn bẩy mô-men&lt;/h2&gt;

&lt;h3 id=&quot;1-tín-hiệu-điều-khiển-vị-trí-50-hz-chu-kỳ-20-ms&quot;&gt;1. Tín hiệu điều khiển vị trí 50 Hz (Chu kỳ 20 ms)&lt;/h3&gt;
&lt;p&gt;Đừng nhầm lẫn PWM điều tốc động cơ DC (đổi hệ số chu kỳ từ 0% đến 100%) với &lt;strong&gt;PWM điều khiển servo RC&lt;/strong&gt;! Servo chuẩn đọc &lt;strong&gt;độ rộng xung mức cao&lt;/strong&gt; lặp lại mỗi \(20\text{ ms}\) (tần số \(50\text{ Hz}\)): xung rộng &lt;strong&gt;\(1{,}0\text{ ms}\)&lt;/strong&gt; ứng với \(0^\circ\), &lt;strong&gt;\(1{,}5\text{ ms}\)&lt;/strong&gt; ứng với góc giữa \(90^\circ\), và &lt;strong&gt;\(2{,}0\text{ ms}\)&lt;/strong&gt; ứng với \(180^\circ\) (nhiều servo thực tế mở rộng dải \(0{,}5\text{–}2{,}5\text{ ms}\) nên luôn cần đo thực nghiệm tránh ép kịch cơ khí làm vỡ bánh răng).&lt;/p&gt;

&lt;h3 id=&quot;2-mô-men-xoắn-torque-và-quy-tắc-cánh-tay-đòn&quot;&gt;2. Mô-men xoắn (Torque) và quy tắc cánh tay đòn&lt;/h3&gt;
&lt;p&gt;Nhà sản xuất thường ghi mô-men kẹt (&lt;em&gt;stall torque&lt;/em&gt;) theo đơn vị quen thuộc \(\text{kg}\cdot\text{cm}\) (ví dụ \(10\text{ kg}\cdot\text{cm} \approx 0{,}98\text{ N}\cdot\text{m}\)). Điều đó có nghĩa tại cánh tay đòn dài \(1\text{ cm}\), servo nhấc được tối đa \(10\text{ kg}\); nhưng nếu cánh tay robot dài \(20\text{ cm}\), sức nâng lập tức giảm \(20\) lần xuống chỉ còn \(0{,}5\text{ kg}\)!&lt;/p&gt;

&lt;h3 id=&quot;3-bẫy-sụt-áp-nguồn-brownout-khi-cấp-nguồn-servo-từ-chân-5-v-của-bo-mạch&quot;&gt;3. Bẫy sụt áp nguồn (Brownout) khi cấp nguồn servo từ chân 5 V của bo mạch&lt;/h3&gt;
&lt;p&gt;Một lỗi kinh điển trong phòng lab là cắm thẳng dây nguồn servo MG996R vào chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; của Arduino hoặc Raspberry Pi. Ngay khoảnh khắc servo khởi động hoặc bị giữ tải, nó kéo dòng đỉnh \(1\text{–}2{,}5\text{ A}\) làm điện áp nguồn tụt mạnh và &lt;strong&gt;khởi động lại (reset) toàn bộ vi điều khiển&lt;/strong&gt;! Luôn cấp nguồn riêng cho servo qua mạch hạ áp (BEC/Buck) và &lt;strong&gt;nối chung dây mass (GND)&lt;/strong&gt; với vi điều khiển.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-độ-rộng-xung-pwm-và-kiểm-tra-biên-an-toàn-mô-men-cánh-tay-robot&quot;&gt;Giải thích: Cầm tay tính độ rộng xung PWM và kiểm tra biên an toàn mô-men cánh tay robot&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Tính độ rộng xung và duty cycle cho góc mục tiêu \(\theta = 135^\circ\):&lt;/strong&gt;
Với dải chuẩn tuyến tính \(1{,}0\text{ ms} \leftrightarrow 0^\circ\) và \(2{,}0\text{ ms} \leftrightarrow 180^\circ\) trên chu kỳ \(T = 20\text{ ms}\):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Độ rộng xung cần phát:
\(t_{\text{pulse}} = 1{,}0 + \frac{135^\circ}{180^\circ} \times (2{,}0 - 1{,}0) = 1{,}0 + 0{,}75 = 1{,}75\text{ ms}.\)&lt;/li&gt;
  &lt;li&gt;Tỷ lệ chu kỳ (duty cycle):
\(D = \frac{1{,}75\text{ ms}}{20\text{ ms}} = 0{,}0875 = 8{,}75\%.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Kiểm tra xem servo \(10\text{ kg}\cdot\text{cm}\) có nhấc nổi tải trên cánh tay dài \(20\text{ cm}\) không?&lt;/strong&gt;
Giả sử cánh tay nhẹ (bỏ qua khối lượng thanh tay đòn) cần nâng một vật nặng &lt;strong&gt;\(m = 0{,}4\text{ kg}\)&lt;/strong&gt; ở đầu cánh tay dài &lt;strong&gt;\(L = 20\text{ cm}\)&lt;/strong&gt; nằm ngang (tư thế nặng nhất):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Mô-men tĩnh yêu cầu:
\(\tau_{\text{cần}} = m \times L = 0{,}4\text{ kg} \times 20\text{ cm} = 8{,}0\text{ kg}\cdot\text{cm} \quad (\approx 0{,}785\text{ N}\cdot\text{m}).\)&lt;/li&gt;
  &lt;li&gt;So với mô-men kẹt danh định \(\tau_{\text{stall}} = 10\text{ kg}\cdot\text{cm}\):
Tỷ lệ sử dụng tải = \(8{,}0 / 10{,}0 = 80\%\) — &lt;strong&gt;quá sát giới hạn!&lt;/strong&gt; Vì mô-men kẹt là mức tối đa ở trạng thái đứng yên (động cơ nóng rất nhanh và không còn lực dư để tạo gia tốc nâng lên), kỹ sư cơ khí luôn đòi hỏi &lt;strong&gt;hệ số an toàn tối thiểu \(2\times\)&lt;/strong&gt; (tức cần chọn servo \(\ge 16\text{ kg}\cdot\text{cm}\) hoặc rút ngắn cánh tay xuống \(10\text{ cm}\)).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn nắm rõ khung tín hiệu 50 Hz (chu kỳ 20 ms) cùng ý nghĩa các mức xung tiêu chuẩn khoảng 1,0 ms, 1,5 ms và 2,0 ms (cũng như dải mở rộng 0,5–2,5 ms trên một số mẫu). Bạn tính được tỉ lệ duty 7,5% của xung 1,5 ms trong khung 20 ms và giải thích vì sao &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; tần số cao kiểu động cơ không dùng được cho servo. Bạn hiểu thông số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kg·cm&lt;/code&gt; là mô-men stall tại một điện áp xác định, phân biệt được servo vị trí với servo quay liên tục, đồng thời cấp nguồn đúng từ đường 5 V ngoài có nối chung mass và dừng hành trình trước điểm chặn cơ khí.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn biết đo 5 V bằng đồng hồ vạn năng và đã hiểu PWM cầu H ở bài 05-01 để thấy rõ sự khác biệt về tần số và độ rộng xung. Bạn cần biết cách gọi thư viện servo (hoặc cấu hình timer xuất xung micro giây), kèm thước đo góc giấy, băng keo và nguồn 5 V chịu dòng khoảng 1 A. Nếu chưa có servo vật lý, bạn vẫn hoàn thành được phần tính toán và phác thảo của bài thực hành.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Hệ dẫn động hai bánh của Capstone A không dùng servo mà dùng động cơ DC chổi than và mạch cầu H. Tuy nhiên, một servo nhỏ quay ngang (pan) thường được lắp bổ sung về sau để quét cảm biến khoảng cách hoặc xoay camera. Trong ROS, tầng trên gửi vị trí góc khớp, còn firmware chuyển đổi góc đó thành độ rộng xung micro giây. Hiểu đúng tín hiệu, nguồn 5 V tách biệt và cách lắp cần servo ngay từ đầu giúp tránh hiện tượng khớp bị giật, sụt nguồn vi điều khiển hay vỡ bánh răng nhựa.&lt;/p&gt;

&lt;h2 id=&quot;xung-điều-khiển-trong-khung-20-ms&quot;&gt;Xung điều khiển trong khung 20 ms&lt;/h2&gt;

&lt;p&gt;Servo hobby nhận khung xung lặp lại mỗi 20 ms (tần số 50 Hz), trong đó độ rộng phần mức cao quyết định góc mục tiêu: khoảng 1,0 ms ứng với một đầu hành trình, 1,5 ms là vị trí giữa (tâm cơ khí) và 2,0 ms là đầu còn lại (một số mẫu hỗ trợ dải rộng hơn từ 0,5 ms đến 2,5 ms). Cách điều khiển rõ ràng nhất là dùng hàm xuất độ rộng xung theo micro giây như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1000&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1500&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Cần tránh nhầm lẫn với hệ số chu kỳ (duty cycle) của động cơ DC: xung 1,5 ms trong khung 20 ms có tỉ lệ là 7,5%, nhưng nếu đặt duty 7,5% trên một chân PWM tần số 1 kHz (chu kỳ 1 ms) thì độ rộng mức cao chỉ còn 75 µs — servo sẽ không nhận diện đây là vị trí tâm. Luôn dùng thư viện servo chuyên dụng hoặc timer cấu hình đúng chu kỳ 20 ms và độ rộng xung theo micro giây.&lt;/p&gt;

&lt;h2 id=&quot;mô-men-stall-bánh-răng-và-hai-loại-servo&quot;&gt;Mô-men stall, bánh răng và hai loại servo&lt;/h2&gt;

&lt;p&gt;Thông số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kg·cm&lt;/code&gt; ghi trên vỏ là &lt;strong&gt;mô-men stall&lt;/strong&gt; đo tại một mức điện áp cụ thể (thường là 4,8 V hoặc 6 V) — tức lực xoắn tối đa khi trục bị giữ đứng yên và động cơ bên trong hút dòng cực đại. Không nên để servo thường xuyên làm việc ở trạng thái stall. Mẫu SG90 (9 g) dùng bánh răng nhựa, phù hợp để xoay cảm biến nhẹ nhưng dễ mẻ răng nếu cần va vào điểm chặn cứng; mẫu MG90S dùng bánh răng kim loại bền cơ học hơn nhưng động cơ bên trong vẫn quá nhiệt nếu bị kẹt.&lt;/p&gt;

&lt;p&gt;Servo quay liên tục (continuous rotation servo) dùng chung giắc ba dây nhưng đã thay biến trở phản hồi bằng mạch chỉnh điểm dừng: xung 1500 µs giữ trục đứng yên, còn xung nhỏ hơn hoặc lớn hơn 1500 µs điều khiển tốc độ và chiều quay chứ không giữ một góc cố định.&lt;/p&gt;

&lt;h2 id=&quot;nguồn-5-v-nối-chung-mass-và-giới-hạn-nhiệt&quot;&gt;Nguồn 5 V, nối chung mass và giới hạn nhiệt&lt;/h2&gt;

&lt;p&gt;Dây nguồn (thường màu đỏ) phải cấp từ nguồn 5 V ngoài đủ dòng stall — ngay cả SG90 nhỏ đã có thể hút 0,5–0,7 A khi kẹt, vượt quá khả năng cấp dòng của chân 3,3 V trên ESP32 và dễ làm sụt cổng USB. Dây tín hiệu có thể nhận mức logic 3,3 V từ GPIO (đa số servo hobby nhận diện tốt mức cao 3,3 V). Dây mass (đen hoặc nâu) bắt buộc nối chung giữa nguồn 5 V và vi điều khiển; nếu thiếu mass chung, tín hiệu mất mốc tham chiếu khiến cần servo giật loạn hoặc chạy về một phía.&lt;/p&gt;

&lt;p&gt;Khi bị ép vào điểm chặn cơ khí, động cơ bên trong chạy gần dòng stall và nóng lên rất nhanh. Luôn giới hạn dải quét trong vùng di chuyển tự do; nếu vỏ servo nóng lên, hãy đưa về góc tự do gần nhất, ngắt nguồn và kiểm tra lại cơ khí.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Với lệnh tâm 1500 µs trong khung 20 ms:&lt;/p&gt;

\[D = \frac{1.5~\mathrm{ms}}{20~\mathrm{ms}} = 0.075 = 7.5\%\]

&lt;p&gt;Nếu nhầm lẫn và dùng duty 7,5% ở tần số PWM động cơ 1 kHz, thời gian mức cao thực tế chỉ là:&lt;/p&gt;

\[t_{high} = \frac{0.075}{1000~\mathrm{Hz}} = 75~\mu\mathrm{s}\]

&lt;p&gt;75 µs quá ngắn so với xung chuẩn 1500 µs; do đó cần dùng hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeMicroseconds(1500)&lt;/code&gt; của thư viện servo.&lt;/p&gt;

&lt;p&gt;Tình huống lắp lệch cơ khí: giả sử đã xuất đúng 1500 µs nhưng cánh tay servo vẫn lệch khoảng $40^\circ$ so với hướng mong muốn. Nguyên nhân là cần servo được ấn lên trục khía (spline) ở góc ngẫu nhiên. Cách sửa đúng: cấp nguồn đưa servo về 1500 µs để định tâm, sau đó &lt;strong&gt;tắt nguồn&lt;/strong&gt;, nhấc cần ra và lắp lại sao cho khớp với vạch $0^\circ$ trên thước. Tuyệt đối không dùng tay cưỡng bức xoay cần khi trục đang mang tải hoặc bị khóa bánh răng.&lt;/p&gt;

&lt;h2 id=&quot;hình-minh-họa&quot;&gt;Hình minh họa&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/servo_pwm.png&quot; alt=&quot;Xung servo hobby: một khoảng mức cao ngắn trong khung 20 ms&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Chu kỳ lặp là 20 ms và độ rộng mức cao (khoảng 1–2 ms) mang toàn bộ thông tin góc điều khiển.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Chỉ cấp nguồn servo từ nguồn 5 V ngoài, không lấy từ chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; của ESP32; nối chung mass. Giữ ngón tay và dây dẫn tránh khỏi vùng quét của cần; dừng ngay và ngắt nguồn nếu cần chạm điểm chặn hoặc vỏ servo nóng lên. Chỉ thử từng servo một.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Servo vị trí SG90 hoặc MG90S&lt;/td&gt;
      &lt;td&gt;Cơ cấu chấp hành cần kiểm thử&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nguồn 5 V chịu dòng khoảng 1 A&lt;/td&gt;
      &lt;td&gt;Cấp nguồn riêng cho servo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Vi điều khiển&lt;/td&gt;
      &lt;td&gt;Chỉ xuất chân tín hiệu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thước đo góc giấy và băng keo&lt;/td&gt;
      &lt;td&gt;Đo góc tương đối&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dây jumper&lt;/td&gt;
      &lt;td&gt;Nối tín hiệu, nguồn 5 V và mass chung&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Nối nguồn 5 V và mass từ nguồn ngoài cho servo, kéo dây mass chung sang vi điều khiển và nối chân tín hiệu vào một chân GPIO; tạm giữ nguồn tắt khi gá thử cần.&lt;/li&gt;
  &lt;li&gt;Nạp lệnh xuất 1500 µs, bật nguồn để xác định vị trí tâm, sau đó tắt nguồn và lắp lại cần khớp với mốc $0^\circ$ trên thước giấy.&lt;/li&gt;
  &lt;li&gt;Quét từ 1000 µs đến 2000 µs theo từng bước 50–100 µs (dừng khoảng 200 ms mỗi bước), ghi lại góc đo được tại ba mốc 1000, 1500 và 2000 µs.&lt;/li&gt;
  &lt;li&gt;Dừng ngay nếu nghe tiếng kẹt bánh răng hoặc thấy vỏ nóng, thu hẹp dải xung vào vùng di chuyển tự do.&lt;/li&gt;
  &lt;li&gt;Nếu chưa có servo trên bàn, hoàn thành bài bằng cách ghi phép tính 7,5%, phác họa khung 20 ms với xung 1,5 ms và viết một câu tóm tắt quy tắc cấp nguồn.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Ghi nhận đủ ba góc đo (hoặc phép tính duty và hình phác họa nếu không có phần cứng), dải xung tự do thực tế và xác nhận cấu hình nguồn (“5 V ngoài, mass chung, tín hiệu từ GPIO”).&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Cần kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cần giật hoặc kêu lạch cạch liên tục&lt;/td&gt;
      &lt;td&gt;Thiếu mass chung hoặc đang xuất PWM tần số cao kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cần ép vào điểm chặn rồi nóng lên&lt;/td&gt;
      &lt;td&gt;Độ rộng xung vượt hành trình cơ khí; thu hẹp dải xung&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 tự reset khi servo quay&lt;/td&gt;
      &lt;td&gt;Đang lấy nguồn servo từ USB hoặc chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;; chuyển sang nguồn 5 V ngoài&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Góc lệch một khoảng cố định&lt;/td&gt;
      &lt;td&gt;Lắp lệch trục khía; tắt nguồn, tháo cần và lắp lại sau khi định tâm 1500 µs&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cần quay tròn liên tục không dừng ở góc&lt;/td&gt;
      &lt;td&gt;Đang dùng servo quay liên tục (điều khiển tốc độ thay vì góc)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;SG90 đủ dùng cho giá xoay cảm biến nhẹ; chọn MG90S nếu cần bánh răng kim loại bền hơn. Giá tham khảo thay đổi theo thời điểm (SG90 khoảng 20.000–45.000 VND, MG90S khoảng 35.000–80.000 VND); nếu chưa có nguồn 5 V ngoài, có thể tìm mua thêm mạch hạ áp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buck 5V&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=SG90&quot;&gt;SG90&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=MG90S&quot;&gt;MG90S&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=SG90&quot;&gt;SG90&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=MG90S&quot;&gt;MG90S&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=SG90&quot;&gt;SG90&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=MG90S&quot;&gt;MG90S&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=SG90&quot;&gt;SG90&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=servo&quot;&gt;servo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=vbAZdQGTA0s&quot;&gt;Quick Guide to Servos&lt;/a&gt; — SparkFun Electronics, tiếng Anh. Minh họa xung điều khiển 1–2 ms lặp lại trong khung 20 ms, khác với PWM 0–100% của động cơ DC.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Độ rộng xung tâm tiêu chuẩn là bao nhiêu micro giây, tương ứng duty cycle bao nhiêu phần trăm trong khung 20 ms?&lt;/li&gt;
  &lt;li&gt;Một chân PWM 1 kHz chạy ở duty 10%. Thời gian mức cao là bao nhiêu micro giây, và có nằm trong dải 1,0–2,0 ms của servo không?&lt;/li&gt;
  &lt;li&gt;Thông số “1,8 kg·cm tại 4,8 V” trên nhãn servo có ý nghĩa gì, và tình trạng điện của động cơ bên trong tại mức tải đó ra sao?&lt;/li&gt;
  &lt;li&gt;Đã xuất đúng xung 1500 µs nhưng cánh tay servo vẫn lệch $40^\circ$. Cần điều chỉnh bộ phận nào và trong trạng thái nguồn nào?&lt;/li&gt;
  &lt;li&gt;Vì sao hệ dẫn động bánh xe của Capstone A không dùng xung servo của bài này, và loại xung này có thể được ứng dụng ở bộ phận nào về sau?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Xung tâm là 1500 µs, tương ứng \(1.5/20 = 7.5\%\).&lt;/li&gt;
  &lt;li&gt;Thời gian mức cao là \(0.10 / 1000~\mathrm{Hz} = 100~\mu\mathrm{s}\), nhỏ hơn nhiều so với dải 1000–2000 µs nên không điều khiển được servo.&lt;/li&gt;
  &lt;li&gt;Đó là mô-men stall tại 4,8 V; tại tải này động cơ DC bên trong hút dòng gần mức stall và nóng lên nhanh chóng.&lt;/li&gt;
  &lt;li&gt;Tắt nguồn, tháo cần khỏi trục khía rồi lắp lại đúng hướng sau khi đã định tâm ở 1500 µs.&lt;/li&gt;
  &lt;li&gt;Capstone A dẫn động bằng động cơ DC và cầu H; xung servo có thể dùng về sau cho khớp quay cảm biến hoặc camera trong firmware.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Tutorial servo hobby của SparkFun (xung, dây và nguồn): &lt;a href=&quot;https://learn.sparkfun.com/tutorials/hobby-servo-tutorial/all&quot;&gt;https://learn.sparkfun.com/tutorials/hobby-servo-tutorial/all&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Hướng dẫn chọn động cơ của SparkFun (giải thích dòng stall bên trong servo): &lt;a href=&quot;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&quot;&gt;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Nhận diện driver: L298N, TB6612, DRV8833</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_02_driver_board_id/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_02_driver_board_id</id>
   <content type="html">&lt;p&gt;Trước khi làm theo bất kỳ sơ đồ đấu nối nào trên mạng, bạn cần nhận diện chính xác con chip được hàn trên bo mạch cầu H, vì L298N, TB6612 và DRV8833 khác nhau hoàn toàn về mức sụt áp, nguồn logic, chân kích hoạt (standby/sleep) và khả năng chịu dòng stall của động cơ TT. Bài này hướng dẫn cách phân biệt ba dòng mạch phổ biến và đo kiểm các đường nguồn trước khi nối chân vi điều khiển.&lt;/p&gt;

&lt;p&gt;Nếu bạn từng mua bộ kit xe robot sinh viên và thấy trên mạch có một con chip to đùng gắn miếng tản nhiệt nhôm khổng lồ màu đen mang tên &lt;strong&gt;L298N&lt;/strong&gt;, hẳn bạn từng tự hỏi: &lt;em&gt;tại sao cùng kéo 2 động cơ bánh xe nhỏ xíu mà mạch L298N lại to nặng và nóng ran, còn mạch TB6612FNG hay DRV8833 lại nhỏ bằng đầu ngón tay cái và mát rượi?&lt;/em&gt; Câu trả lời nằm ngay ở công nghệ bán dẫn bên trong cầu H: thế hệ transistor &lt;strong&gt;BJT Darlington&lt;/strong&gt; từ thập niên 1980 so với &lt;strong&gt;MOSFET&lt;/strong&gt; hiện đại — một khác biệt nhỏ trong datasheet nhưng quyết định trực tiếp việc xe bạn chạy được 20 phút hay 1 giờ trên cùng một cục pin!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-so-sánh-công-nghệ-bán-dẫn-trong-driver-động-cơ&quot;&gt;Kiến thức cần nhớ: So sánh công nghệ bán dẫn trong Driver động cơ&lt;/h2&gt;

&lt;h3 id=&quot;1-bjt-darlington-l298n--sụt-áp-cố-định-lớn-và-tổn-hao-nhiệt-tuyến-tính&quot;&gt;1. BJT Darlington (L298N) — sụt áp cố định lớn và tổn hao nhiệt tuyến tính&lt;/h3&gt;
&lt;p&gt;Mạch L298N dùng các cặp transistor lưỡng cực (BJT) mắc Darlington. Nhược điểm cố hữu của BJT là dù dòng nhỏ hay lớn, mỗi cầu H luôn bị &lt;strong&gt;sụt áp bão hòa \(V_{\text{CE(sat)}} \approx 1{,}8\text{–}3{,}2\text{ V}\)&lt;/strong&gt;. Điện áp sụt này không biến mất mà biến thành nhiệt tỏa ra trên miếng nhôm tản nhiệt theo \(P_{\text{tổn hao}} = V_{\text{drop}} \cdot I\), đồng thời “ăn mất” phần lớn điện áp pin nếu bạn dùng pin 2S (7,4 V) hoặc pin AA (6 V).&lt;/p&gt;

&lt;h3 id=&quot;2-mosfet-tb6612fng-drv8833--điện-trở-kênh-r_textdson-cực-nhỏ&quot;&gt;2. MOSFET (TB6612FNG, DRV8833) — điện trở kênh \(R_{\text{DS(on)}}\) cực nhỏ&lt;/h3&gt;
&lt;p&gt;Các driver thế hệ mới dùng công tắc &lt;strong&gt;MOSFET&lt;/strong&gt; hoạt động như một điện trở cực nhỏ \(R_{\text{DS(on)}} \approx 0{,}2\text{–}0{,}5\,\Omega\) (tổng cả nhánh trên + nhánh dưới). Sụt áp chỉ là \(V_{\text{drop}} = I \cdot R_{\text{DS(on)}}\) và tổn hao nhiệt là \(P = I^2 R_{\text{DS(on)}}\) — nhỏ hơn L298N từ 4 đến 10 lần ở dòng 1 A, cho phép thu nhỏ mạch xuống cỡ con tem mà không cần tản nhiệt cồng kềnh.&lt;/p&gt;

&lt;h3 id=&quot;3-mức-logic-điều-khiển-33-v-vs-5-v-và-các-chân-bảo-vệ&quot;&gt;3. Mức logic điều khiển (3,3 V vs. 5 V) và các chân bảo vệ&lt;/h3&gt;
&lt;p&gt;Khi chọn driver cho ESP32/Raspberry Pi (logic 3,3 V) hay Arduino (5 V), cần kiểm tra 4 thông số vàng trên datasheet: dải điện áp động cơ (\(V_M\)), dòng liên tục/đỉnh (\(I_{\text{rms}} / I_{\text{peak}}\)), ngưỡng logic đầu vào (\(V_{\text{IH}}\)), và tính năng bảo vệ quá nhiệt/quá dòng (như chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; trên TB6612 hoặc giới hạn dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AISEN/BISEN&lt;/code&gt; trên DRV8833).&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-so-sánh-sụt-áp-và-nhiệt-tỏa-ra-giữa-l298n-và-tb6612fng-trên-pin-lipo-2s&quot;&gt;Giải thích: Cầm tay so sánh sụt áp và nhiệt tỏa ra giữa L298N và TB6612FNG trên pin LiPo 2S&lt;/h3&gt;

&lt;p&gt;Giả sử robot dùng pin LiPo 2S (&lt;strong&gt;\(V_{\text{bat}} = 7{,}4\text{ V}\)&lt;/strong&gt;) kéo &lt;strong&gt;2 động cơ&lt;/strong&gt;, mỗi động cơ ăn dòng liên tục &lt;strong&gt;\(I = 1{,}0\text{ A}\)&lt;/strong&gt; (tổng công suất cấp từ pin \(P_{\text{pin}} = 7{,}4 \times 2{,}0 = 14{,}8\text{ W}\)):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Thông số đánh giá (ở \(I = 1{,}0\text{ A/kênh}\))&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Driver &lt;strong&gt;L298N&lt;/strong&gt; (BJT, \(V_{\text{drop}} \approx 2{,}0\text{ V}\))&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Driver &lt;strong&gt;TB6612FNG&lt;/strong&gt; (MOSFET, \(R_{\text{DS(on)}} \approx 0{,}5\,\Omega\))&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Chênh lệch thực tế&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Sụt áp trên mỗi cầu H&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(2{,}0\text{ V}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(1{,}0 \times 0{,}5 = 0{,}5\text{ V}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;L298N mất áp gấp &lt;strong&gt;4×&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Điện áp thực đến động cơ&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(7{,}4 - 2{,}0 = 5{,}4\text{ V}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(7{,}4 - 0{,}5 = 6{,}9\text{ V}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;TB6612 giúp xe chạy nhanh hơn &lt;strong&gt;28%&lt;/strong&gt;!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Nhiệt lãng phí (cả 2 kênh)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(2 \times (2{,}0 \times 1{,}0) = 4{,}0\text{ W}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(2 \times (1{,}0^2 \times 0{,}5) = 1{,}0\text{ W}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;L298N đốt nhiệt gấp &lt;strong&gt;4×&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Hiệu suất truyền điện của driver&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(5{,}4 / 7{,}4 \approx 73{,}0\%\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(6{,}9 / 7{,}4 \approx 93{,}2\%\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tiết kiệm &lt;strong&gt;20,2%&lt;/strong&gt; dung lượng pin!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Bảng tính ngắn gọn này cho thấy rõ: L298N chỉ còn phù hợp làm mô hình học tập giá rẻ khi cấp nguồn 12 V; còn với mọi robot chạy pin 2S thực thụ, chuyển sang TB6612FNG hoặc DRV8833 là nâng cấp rẻ mà hiệu quả nhất bạn có thể làm!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn nhận diện được module L298N, TB6612 và DRV8833 dựa trên vỏ chip, tản nhiệt và nhãn chân. Bạn giải thích được chức năng jumper 5 V trên L298N, thời điểm bắt buộc phải tháo jumper và lý do không dùng mạch ổn áp 7805 tích hợp để cấp nguồn cho ESP32. Bạn hiểu rõ vì sao phải kéo chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; của TB6612 lên mức cao và giới hạn sử dụng của DRV8833 trên pin 1S–2S dưới 10 V. Bạn cũng thực hiện được phép đo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; và đường nguồn logic bằng đồng hồ vạn năng trước khi nối bất kỳ chân GPIO nào.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã quen với thang đo điện áp một chiều trên đồng hồ vạn năng, nắm các khái niệm cầu H từ bài 05-01 (chiều tiến/lùi, trùng dẫn, PWM trên chân enable) và tuân thủ nguyên tắc không lấy nguồn động cơ từ USB.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Capstone A sử dụng hai động cơ giảm tốc TT chạy nguồn pin 2S. Mạch cầu H quyết định bao nhiêu điện áp từ pin thực sự tới được động cơ và liệu mạch có hoạt động ổn định khi phát Wi-Fi hay không. Nếu nguồn logic bị sụt áp khiến ESP32 reset giữa chừng, các chân điều khiển có thể ở trạng thái không xác định và làm robot di chuyển ngoài ý muốn. Chọn TB6612 làm mặc định (và biết cách dùng an toàn nếu phải thay tạm bằng L298N) là bước chuẩn bị thiết yếu để lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; ở tầng ROS sau này vận hành tin cậy.&lt;/p&gt;

&lt;h2 id=&quot;nhận-diện-ba-dòng-module-phổ-biến&quot;&gt;Nhận diện ba dòng module phổ biến&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;L298N:&lt;/strong&gt; bo mạch kích thước lớn (thường màu đỏ hoặc xanh), nổi bật với tấm tản nhiệt kim loại bắt vít trên vỏ chip công suất lớn và một jumper 5 V nằm gần IC ổn áp. Hàng cầu đấu vít (domino) nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; và hai động cơ; hàng chân cắm logic gồm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN3&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN4&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENB&lt;/code&gt; cùng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;TB6612FNG:&lt;/strong&gt; module nhỏ gọn dùng cầu MOSFET của Toshiba, các chân chính gồm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMB&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;. Vì sụt áp rất nhỏ nên mạch không cần tản nhiệt kim loại lớn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DRV8833:&lt;/strong&gt; bo mạch breakout rất nhỏ dùng chip cầu kép của Texas Instruments, dải nguồn động cơ khoảng 2,7–10,6 V. Nhiều module chạy chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN/IN&lt;/code&gt; (hai chân điều khiển mã hóa cả chiều lẫn PWM) và không có tản nhiệt lớn. Mạch phù hợp với động cơ cỡ nhỏ chạy pin 1S–2S, nhưng dòng stall của cặp động cơ TT thường vượt quá khả năng chịu tải liên tục của chip này.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;l298n-và-jumper-5-v&quot;&gt;L298N và jumper 5 V&lt;/h2&gt;

&lt;p&gt;Tầng công suất của L298N là transistor Darlington lưỡng cực nên sụt khoảng 2 V khi có dòng chạy qua; toàn bộ phần điện áp sụt nhân với dòng điện chuyển thành nhiệt trên chip, đòi hỏi tấm tản nhiệt lớn. Phần logic của L298N yêu cầu nguồn 5 V.&lt;/p&gt;

&lt;p&gt;Khi cắm jumper 5 V, IC ổn áp tuyến tính 7805 tích hợp lấy điện từ nguồn động cơ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; để tạo ra 5 V nuôi khối logic. Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; vượt quá 12 V, 7805 sẽ tiêu tán công suất quá lớn và quá nhiệt — trường hợp này &lt;strong&gt;bắt buộc rút jumper&lt;/strong&gt; và cấp 5 V từ nguồn ngoài. Ngay cả khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; dưới 12 V, tuyệt đối không lấy chân 5 V của L298N để cấp nguồn cho ESP32: các xung dòng khi Wi-Fi phát sóng dễ làm sụt áp 7805, gây reset (brownout) cho ESP32. Hãy dùng một mạch hạ áp (buck) riêng cho vi điều khiển và nối chung mass với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Tín hiệu GPIO 3,3 V thường vẫn đạt ngưỡng nhận mức cao của L298N (khoảng 2,3 V) miễn nguồn logic 5 V ổn định và đã nối chung mass.&lt;/p&gt;

&lt;h2 id=&quot;tb6612fng--lựa-chọn-mặc-định-của-capstone-a&quot;&gt;TB6612FNG — lựa chọn mặc định của Capstone A&lt;/h2&gt;

&lt;p&gt;Do sử dụng MOSFET, TB6612FNG có sụt áp rất thấp nên truyền gần trọn điện áp pin tới động cơ. Chân nguồn logic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; chấp nhận dải rộng 2,7–5,5 V, có thể cấp trực tiếp từ đường 3,3 V của ESP32. Cực &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; chịu tới khoảng 15 V (rất thoải mái cho pin 2S 7,4–8,4 V) với dòng liên tục khoảng 1,2 A mỗi kênh — phù hợp với động cơ TT nếu tránh để kẹt trục kéo dài.&lt;/p&gt;

&lt;p&gt;Điểm dễ quên nhất trên TB6612 là chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; (Standby): chân này &lt;strong&gt;bắt buộc phải kéo lên mức cao&lt;/strong&gt; (nối vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; hoặc điều khiển bằng một chân GPIO kéo cao trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt;), nếu không cả hai cầu đều ở trạng thái ngủ và động cơ không quay dù mã nguồn hoàn toàn đúng.&lt;/p&gt;

&lt;h2 id=&quot;drv8833-và-các-mạch-cầu-quá-nhỏ&quot;&gt;DRV8833 và các mạch cầu quá nhỏ&lt;/h2&gt;

&lt;p&gt;DRV8833 phù hợp cho các cơ cấu chấp hành nhỏ chạy 1S hoặc 2S có dòng stall thấp; tương tự &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; trên TB6612, nhiều bo DRV8833 có chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SLEEP&lt;/code&gt; cần kéo cao để kích hoạt. Với cặp động cơ TT của Capstone A, nên chọn TB6612 thay vì DRV8833.&lt;/p&gt;

&lt;p&gt;Ngoài ra, các mạch giá rẻ như MX1508 hay L9110 thường xuất hiện chung trong kết quả tìm kiếm nhưng có khả năng chịu dòng và tản nhiệt quá nhỏ cho động cơ TT chạy pin 2S — một lần kẹt trục có thể làm hỏng chip ngay lập tức.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Giả sử nguồn động cơ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM = 12 V&lt;/code&gt; và vẫn cắm jumper 5 V trên L298N để nuôi ESP32 tiêu thụ trung bình \(0.20~\mathrm{A}\) (thực tế xung dòng Wi-Fi còn cao hơn). Công suất tỏa nhiệt trên IC 7805 là:&lt;/p&gt;

\[P_{7805} \approx (12 - 5) \times 0.20 = 1.4~\mathrm{W}\]

&lt;p&gt;Với vỏ IC nhỏ không có tản nhiệt đáng kể, 1,4 W dễ làm 7805 quá nhiệt, kéo sụt đường 5 V và khiến ESP32 tự reset. Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM &amp;gt; 12 V&lt;/code&gt;, công suất tỏa nhiệt càng lớn hơn nên bắt buộc phải rút jumper. Bên cạnh đó, với dòng tải 1 A qua một kênh L298N (sụt áp khoảng 2 V), bản thân chip L298N đã tỏa khoảng \(2~\mathrm{W}\) nhiệt trên mỗi kênh. Dùng TB6612 trên pin 2S giúp tránh cả hai nguồn sinh nhiệt này.&lt;/p&gt;

&lt;h2 id=&quot;hình-minh-họa&quot;&gt;Hình minh họa&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Dosmotorsl298n.jpg&quot; alt=&quot;Module cầu H kép L298N với tản nhiệt bắt vít&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Nhận diện nhanh L298N qua tấm tản nhiệt kim loại lớn, hàng cầu đấu vít cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;/động cơ và jumper 5 V gần IC ổn áp.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Digital_Multimeter_Aka.jpg&quot; alt=&quot;Đồng hồ số dùng để đo VM và ray logic trước khi nối bất kỳ GPIO nào&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Đặt que đen vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; của driver, que đỏ đo lần lượt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; và chân nguồn logic (5 V hoặc 3,3 V) trước khi nối dây sang vi điều khiển.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Ngắt nguồn động cơ khi đo thông mạch. Khi đo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, đặt đồng hồ ở thang điện áp một chiều và cẩn thận không để đầu que trượt chạm giữa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; với chân logic. Không nối chân ESP32 trước khi xác minh xong hai đường nguồn; luôn nhấc bánh khỏi bàn nếu đã gắn động cơ.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Module cầu H thực tế của bạn&lt;/td&gt;
      &lt;td&gt;Đối tượng nhận diện và đo kiểm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ vạn năng&lt;/td&gt;
      &lt;td&gt;Đo điện áp một chiều và thông mạch&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điện thoại&lt;/td&gt;
      &lt;td&gt;Chụp ảnh bo mạch để ghi chú nhãn chân&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ pin 2S hoặc nguồn bàn có giới hạn dòng &lt;em&gt;(tùy chọn)&lt;/em&gt;&lt;/td&gt;
      &lt;td&gt;Cấp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; để đo kiểm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sổ tay&lt;/td&gt;
      &lt;td&gt;Ghi danh sách chân và hai giá trị điện áp đo được&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Chụp rõ mặt trên và mặt dưới bo mạch, khoanh tròn mã chip in trên thân IC.&lt;/li&gt;
  &lt;li&gt;Ghi toàn bộ tên các cầu đấu vít và hàng chân cắm theo đúng nhãn in trên mạch (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ENA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;, …) vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Nếu mạch có jumper 5 V, ghi rõ chức năng của jumper và xác định nên giữ hay rút đối với mức điện áp pin của bạn.&lt;/li&gt;
  &lt;li&gt;Cấp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; (chưa nối vi điều khiển), đo và ghi lại điện áp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; cùng đường nguồn logic.&lt;/li&gt;
  &lt;li&gt;Xác nhận vai trò của chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; (trên TB6612), jumper 5 V (trên L298N) hoặc chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SLEEP&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; (trên DRV8833).&lt;/li&gt;
  &lt;li&gt;Chỉ sau khi hoàn tất các bước trên mới nối nguồn logic, dây &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; chung và các chân tín hiệu điều khiển.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Gọi đúng tên chip, có ảnh chụp gắn nhãn chân, có đủ hai giá trị điện áp đo được (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; và logic) và nêu đúng vai trò của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; hoặc jumper 5 V (với DRV8833, nêu rõ dải 2,7–10,6 V và đánh giá mức tương thích với pin đang dùng).&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Cần kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Chân logic 5 V đo ra 0 V dù đã cắm jumper&lt;/td&gt;
      &lt;td&gt;Thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, cắm nhầm chân jumper hoặc hỏng IC 7805&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 tự reset khi bật Wi-Fi&lt;/td&gt;
      &lt;td&gt;Đang lấy nguồn từ 7805 của L298N; chuyển sang mạch buck riêng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ngõ ra TB6612 không hoạt động&lt;/td&gt;
      &lt;td&gt;Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; để thấp hoặc để nổi; kéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; lên cao&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;DRV8833 tự ngắt khi chạy động cơ TT&lt;/td&gt;
      &lt;td&gt;Dòng stall vượt ngưỡng bảo vệ quá dòng của chip; đổi driver phù hợp hơn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MX1508 nóng rát tay&lt;/td&gt;
      &lt;td&gt;Động cơ quá tải so với mạch; ngắt nguồn ngay&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Chọn mua theo đúng mã chip đã xác định: ưu tiên TB6612 cho động cơ TT chạy 2S; L298N chấp nhận được cho bài thực hành cơ bản; DRV8833 chỉ dùng cho động cơ nhỏ. Giá tham khảo thay đổi theo thời điểm (L298N khoảng 35.000–70.000 VND, TB6612 khoảng 40.000–120.000 VND, DRV8833 khoảng 30.000–80.000 VND):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Trang L298N đã kiểm tra, khoảng 45.000 VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=DRV8833&quot;&gt;DRV8833&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=DRV8833&quot;&gt;DRV8833&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=DRV8833&quot;&gt;DRV8833&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=DRV8833&quot;&gt;DRV8833&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=JPPTRj0KWbg&quot;&gt;TB6612FNG H-Bridge Motor Controller - Better than L298N?&lt;/a&gt; — DroneBot Workshop, tiếng Anh. So sánh trực tiếp TB6612 và L298N về sụt áp, chân logic và chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=3N_4VpzmKY0&quot;&gt;What is an H-Bridge?&lt;/a&gt; — CircuitBread, tiếng Anh. Ôn lại nguyên lý cầu H bên trong cả hai dòng module.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một module có tấm tản nhiệt bắt vít và jumper 5 V, cấp nguồn động cơ từ pin 3S (12,6 V). Cần xử lý jumper thế nào và cấp nguồn cho ESP32 từ đâu?&lt;/li&gt;
  &lt;li&gt;Chương trình điều khiển TB6612 đã đặt đúng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt; nhưng trục động cơ vẫn đứng yên. Chân nào cần kiểm tra đầu tiên?&lt;/li&gt;
  &lt;li&gt;Nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; cấp vào bo DRV8833 đo được 11,1 V từ pin 3S. Mức áp này có nằm trong dải 2,7–10,6 V cho phép không?&lt;/li&gt;
  &lt;li&gt;Ở dòng 1 A, một kênh L298N sụt áp 2 V. Công suất chuyển thành nhiệt trên kênh đó là bao nhiêu watt?&lt;/li&gt;
  &lt;li&gt;Cửa hàng gợi ý dùng MX1508 cho robot hai động cơ TT chạy pin 2S. Bạn nên chọn mạch cầu H nào thay thế?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Rút jumper 5 V trên L298N (vì nguồn vượt 12 V) và cấp nguồn cho ESP32 từ mạch hạ áp (buck) riêng có nối chung mass.&lt;/li&gt;
  &lt;li&gt;Kiểm tra chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; và đảm bảo nó được kéo lên mức cao.&lt;/li&gt;
  &lt;li&gt;Không; 11,1 V vượt quá giới hạn trên 10,6 V của DRV8833 và có thể làm hỏng chip.&lt;/li&gt;
  &lt;li&gt;\(P = 2~\mathrm{V} \times 1~\mathrm{A} = 2~\mathrm{W}\).&lt;/li&gt;
  &lt;li&gt;Chọn TB6612 (không dùng MX1508 hay L9110 cho cặp động cơ TT trên 2S).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Datasheet TI DRV8833 (dải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; và giao diện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN/IN&lt;/code&gt;): &lt;a href=&quot;https://www.ti.com/lit/ds/symlink/drv8833.pdf&quot;&gt;https://www.ti.com/lit/ds/symlink/drv8833.pdf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Mạch mang TB6612FNG của Pololu (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; và dòng định mức): &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;https://www.pololu.com/product/713&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Mạch mang DRV8833 của Pololu: &lt;a href=&quot;https://www.pololu.com/product/2130&quot;&gt;https://www.pololu.com/product/2130&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Datasheet ST L298: &lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;https://www.st.com/resource/en/datasheet/l298.pdf&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Motor DC và cầu H</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_01_dc_hbridge/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter05/05_01_dc_hbridge</id>
   <content type="html">&lt;p&gt;Động cơ DC có chổi than là cơ cấu chấp hành chính của Capstone A. Bên trong vỏ kim loại, cổ góp liên tục đảo chiều dòng điện đúng thời điểm rô-to quay, còn cuộn dây nóng lên nhanh nhất khi trục bị kẹt (stall). Mạch cầu H gồm bốn khóa chuyển mạch cho phép một chân tín hiệu logic nhỏ điều khiển đảo chiều động cơ dùng nguồn pin. Cuối buổi thực hành (với bánh xe nhấc khỏi bàn), bạn sẽ hoàn thiện bảng chân lý thực tế cho đúng mạch cầu H đang sử dụng.&lt;/p&gt;

&lt;p&gt;Vi điều khiển của bạn chỉ xuất ra được dòng điện khoảng \(20\text{ mA}\) ở điện áp \(3{,}3\text{ V}\) — chỉ đủ thắp sáng một bóng LED bé xíu, không bao giờ quay nổi một bánh xe robot cần dòng \(1\text{–}3\text{ A}\) ở \(12\text{ V}\). Và ngay cả khi bạn nối pin trực tiếp vào động cơ, làm sao để đảo chiều tiến–lùi và điều tốc mượt mà chỉ bằng vài dòng code? Câu trả lời kinh điển là sự kết hợp giữa &lt;strong&gt;động cơ DC có chổi than&lt;/strong&gt;, &lt;strong&gt;mạch cầu H (H-Bridge)&lt;/strong&gt; và kỹ thuật điều chế độ rộng xung &lt;strong&gt;PWM&lt;/strong&gt; — bộ ba đã vận hành hàng triệu robot từ xe tự hành trong lớp học đến dây chuyền nhà máy.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-điện-cơ-động-cơ-dc-cầu-h-và-pwm&quot;&gt;Kiến thức cần nhớ: Điện cơ động cơ DC, Cầu H và PWM&lt;/h2&gt;

&lt;h3 id=&quot;1-phương-trình-điện-cơ-động-cơ-dc-và-sức-phản-điện-động-back-emf&quot;&gt;1. Phương trình điện cơ động cơ DC và sức phản điện động (Back-EMF)&lt;/h3&gt;
&lt;p&gt;Một động cơ DC được mô hình hóa như một điện trở cuộn dây \(R_a\) nối tiếp với nguồn &lt;strong&gt;sức phản điện động (Back-EMF)&lt;/strong&gt; \(E = K_e \omega\) tỷ lệ thuận với tốc độ góc \(\omega\):
\(V = I R_a + K_e \omega, \qquad \tau = K_t I.\)
Khi động cơ vừa khởi động (\(\omega = 0\)), \(E = 0\) nên dòng điện vọt lên cực đại (dòng kẹt/khởi động \(I_{\text{stall}} = V / R_a\)). Khi bánh quay nhanh dần, \(E\) tăng lên tự động “hãm” dòng \(I\) giảm xuống!&lt;/p&gt;

&lt;h3 id=&quot;2-mạch-cầu-h-h-bridge--4-công-tắc-điều-khiển-4-chế-độ&quot;&gt;2. Mạch Cầu H (H-Bridge) — 4 công tắc điều khiển 4 chế độ&lt;/h3&gt;
&lt;p&gt;Mạch cầu H gồm 4 transistor (MOSFET/BJT) xếp thành chữ H bao quanh động cơ: đóng chéo cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Q1, Q4)&lt;/code&gt; cho dòng chạy thuận; đóng chéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Q2, Q3)&lt;/code&gt; đảo chiều dòng chạy nghịch; mở cả 4 công tắc cho bánh &lt;strong&gt;trôi tự do (coast)&lt;/strong&gt;; đóng hai công tắc dưới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Q2, Q4)&lt;/code&gt; ngắn mạch cuộn dây qua chính sức phản điện động để &lt;strong&gt;phanh điện (brake)&lt;/strong&gt;. &lt;em&gt;Tuyệt đối cấm đóng cùng lúc một nhánh dọc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Q1, Q2)&lt;/code&gt; vì sẽ gây ngắn mạch nguồn (shoot-through) cháy driver ngay lập tức!&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;3-pwm-pulse-width-modulation-và-diode-dập-xung-cảm-ứng-flyback&quot;&gt;3. PWM (Pulse-Width Modulation) và Diode dập xung cảm ứng (Flyback)&lt;/h3&gt;
&lt;p&gt;Thay vì đốt điện bằng điện trở biến thiên để giảm áp, PWM đóng–ngắt công tắc cầu H cực nhanh (thường \(10\text{–}25\text{ kHz}\), trên ngưỡng nghe của tai người để tránh tiếng rít khó chịu) với hệ số chu kỳ \(D \in [0, 1]\), tạo điện áp trung bình \(V_{\text{avg}} = D \cdot V_{\text{bat}}\). Vì cuộn dây động cơ có độ tự cảm \(L\), mỗi lần ngắt dòng đột ngột sẽ sinh gai điện áp cao \(L \frac{di}{dt}\) — các &lt;strong&gt;diode hồi tiếp (flyback diodes)&lt;/strong&gt; có nhiệm vụ xả dòng cảm ứng này an toàn về nguồn.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-điện-áp-pwm-back-emf-dòng-điện-và-hiệu-suất-động-cơ-dc&quot;&gt;Giải thích: Cầm tay tính điện áp PWM, Back-EMF, dòng điện và hiệu suất động cơ DC&lt;/h3&gt;

&lt;p&gt;Xét một động cơ DC cấp nguồn pin &lt;strong&gt;\(V_{\text{bat}} = 12\text{ V}\)&lt;/strong&gt;, chạy PWM với độ rộng xung &lt;strong&gt;\(D = 75\%\)&lt;/strong&gt;. Thông số động cơ: điện trở phần ứng \(R_a = 2{,}0\,\Omega\), hằng số sức phản điện động \(K_e = 0{,}05\text{ V/(rad/s)}\) (bằng hằng số mô-men \(K_t = 0{,}05\text{ N}\cdot\text{m/A}\) trong hệ SI). Khi động cơ đang quay đều ở \(\omega = 100\text{ rad/s}\):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Điện áp trung bình đặt lên động cơ:&lt;/strong&gt;
\(V_{\text{avg}} = D \times V_{\text{bat}} = 0{,}75 \times 12 = 9{,}0\text{ V}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sức phản điện động (Back-EMF):&lt;/strong&gt;
\(E = K_e \omega = 0{,}05 \times 100 = 5{,}0\text{ V}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dòng điện phần ứng và mô-men tạo ra:&lt;/strong&gt;
\(I = \frac{V_{\text{avg}} - E}{R_a} = \frac{9{,}0 - 5{,}0}{2{,}0} = 2{,}0\text{ A}, \qquad \tau = K_t I = 0{,}05 \times 2{,}0 = 0{,}10\text{ N}\cdot\text{m}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cân bằng công suất điện – nhiệt – cơ:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Công suất điện cấp vào: \(P_{\text{in}} = V_{\text{avg}} I = 9{,}0 \times 2{,}0 = 18{,}0\text{ W}\).&lt;/li&gt;
      &lt;li&gt;Tổn hao nhiệt trên cuộn dây đồng: \(P_{\text{nhiệt}} = I^2 R_a = 2{,}0^2 \times 2{,}0 = 8{,}0\text{ W}\).&lt;/li&gt;
      &lt;li&gt;Công suất cơ học hữu ích: \(P_{\text{cơ}} = E \cdot I = \tau \cdot \omega = 0{,}10 \times 100 = 10{,}0\text{ W}\) (hiệu suất \(\eta = 10/18 \approx 55{,}6\%\)!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Kiểm tra nhanh: \(P_{\text{nhiệt}} + P_{\text{cơ}} = 8{,}0 + 10{,}0 = 18{,}0\text{ W} = P_{\text{in}}\) — định luật bảo toàn năng lượng khớp từng watt!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn giải thích được vì sao cổ góp giữ mô-men trên trục theo một chiều ổn định khi rô-to quay và vì sao dòng phần ứng đạt cực đại lúc kẹt trục (stall). Bạn gọi tên được cặp khóa quay tiến và cặp khóa quay lùi trong cầu H, đồng thời chỉ ra tổ hợp khóa gây ngắn mạch nguồn. Bạn tính được điện áp trung bình từ hệ số chu kỳ (duty cycle) PWM 8-bit trên bộ pin 7,4 V và ước lượng ảnh hưởng của sụt áp 1,5–2 V trên L298N. Bạn cũng lập được bảng chân lý cho một động cơ (với các cột &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM&lt;/code&gt; và chiều quay thực tế) và giải thích được hậu quả của việc cắm ngược dây động cơ đối với lệnh quay hướng (yaw).&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã biết đo điện áp một chiều bằng đồng hồ vạn năng, vận dụng định luật Ohm và điều khiển mức cao/thấp trên chân GPIO. Từ chương cảm biến, bạn đã nắm quy tắc nối chung mass giữa mọi module trên robot. Nguồn động cơ trong bài này là pin hoặc bộ nguồn bàn có giới hạn dòng; cáp USB chỉ cấp cho vi điều khiển.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Chương 07 sẽ xuất hai tín hiệu PWM có dấu cho bánh trái và bánh phải: khi quy ước chiều tiến là hướng mũi robot, giá trị dương phải làm bánh quay về phía trước. Về sau, một node ROS 2 sẽ nhận bản tin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; gồm vận tốc tịnh tiến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; và vận tốc quay góc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;, rồi quy đổi thành hai lệnh điều khiển động cơ. Nếu cắm ngược dây động cơ bên phải, một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; dương sẽ làm robot quay sai chiều, khiến toàn bộ tầng điều khiển phía trên hoạt động sai dù mã nguồn không có lỗi. Bảng chân lý lập hôm nay là cơ sở đảm bảo đúng dấu cho cả hệ thống.&lt;/p&gt;

&lt;h2 id=&quot;cổ-góp-và-suất-điện-động-ngược&quot;&gt;Cổ góp và suất điện động ngược&lt;/h2&gt;

&lt;p&gt;Trong động cơ DC có chổi than, cuộn dây phần ứng đặt trên rô-to. Hai chổi than cố định tì lên cổ góp bằng đồng chia thành nhiều phiến. Khi trục quay, các phiến đồng lần lượt đổi tiếp xúc giữa hai chổi, đảo chiều dòng điện trong cuộn dây đúng góc vị trí từ trường, nhờ đó mô-men trên trục luôn giữ cùng một chiều.&lt;/p&gt;

&lt;p&gt;Đồng thời, khi rô-to quay trong từ trường, động cơ hoạt động như một máy phát và sinh ra suất điện động ngược (back-EMF) ký hiệu là \(E\), tăng tỉ lệ với tốc độ quay và chống lại điện áp nguồn. Dòng điện qua cuộn dây xấp xỉ:&lt;/p&gt;

\[I \approx \frac{V_s - E}{R}\]

&lt;p&gt;trong đó \(V_s\) là điện áp thực đặt lên hai chổi than và \(R\) là điện trở cuộn dây. Khi trục bị kẹt đứng yên (stall), \(E = 0\) nên dòng chỉ còn bị giới hạn bởi điện trở đồng của cuộn dây:&lt;/p&gt;

\[I_{stall} \approx \frac{V}{R}\]

&lt;p&gt;Dòng stall này là thông số quyết định khi chọn mạch cầu H, tiết diện dây và cầu chì. Khi bánh quay tự do trên không (không tải), \(E\) tăng cao triệt tiêu phần lớn \(V_s\) nên dòng tiêu thụ nhỏ; nhưng ngay khi bánh kẹt vào vật cản, động cơ lập tức quay về trạng thái stall dù mức PWM không thay đổi.&lt;/p&gt;

&lt;h2 id=&quot;bốn-khóa-cầu-h-và-trạng-thái-ngắn-mạch-nguồn&quot;&gt;Bốn khóa cầu H và trạng thái ngắn mạch nguồn&lt;/h2&gt;

&lt;p&gt;Mạch cầu H gồm bốn khóa chuyển mạch xếp theo hình chữ H với động cơ nằm ở nhánh ngang: khóa phía cao bên trái, khóa phía thấp bên trái, khóa phía cao bên phải và khóa phía thấp bên phải. Để quay tiến, đóng khóa phía cao bên trái cùng khóa phía thấp bên phải để dòng đi từ cực trái sang cực phải của động cơ. Để quay lùi, đảo sang cặp khóa phía cao bên phải và khóa phía thấp bên trái để đảo chiều dòng điện và mô-men.&lt;/p&gt;

&lt;p&gt;Nếu đóng đồng thời hai khóa trên cùng một nhánh đứng (khóa cao và khóa thấp cùng một bên), dương nguồn pin sẽ nối thẳng xuống mass. Hiện tượng này gọi là trùng dẫn / dẫn xuyên (shoot-through) — một mạch ngắn trực tiếp có thể làm hỏng mạch cầu H chỉ trong vài mili giây. Không tự ý thử các tổ hợp điều khiển ngoài tài liệu; hãy tra bảng chân lý của đúng con chip đang dùng.&lt;/p&gt;

&lt;p&gt;Khi không cho động cơ quay, các mạch cầu H phân biệt hai trạng thái dừng là trôi (coast) và hãm điện (brake). Ở chế độ trôi (ví dụ hai ngõ vào cùng mức thấp trên một số chip), các khóa ngắt hở nên bánh quay tự do. Ở chế độ hãm điện (đóng đồng thời hai khóa phía thấp — hoặc đồng thời hai khóa phía cao — khi cặp đối diện đang mở), hai đầu cuộn dây được nối tắt với nhau qua các khóa mà không chập nguồn, tạo lực cản hãm trục. L298N, TB6612 và DRV8833 có bảng chân lý khác nhau cho hai chế độ này, nên bạn phải kiểm chứng trực tiếp bằng tay trên trục động cơ của mình.&lt;/p&gt;

&lt;h2 id=&quot;điều-chỉnh-tốc-độ-bằng-pwm&quot;&gt;Điều chỉnh tốc độ bằng PWM&lt;/h2&gt;

&lt;p&gt;Hai chân hướng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN2&lt;/code&gt;) chọn chiều dòng điện, còn tín hiệu PWM trên chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enable&lt;/code&gt; (hoặc chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM&lt;/code&gt; riêng) đóng cắt liên tục đường dòng đó. Bỏ qua sụt áp trên bán dẫn, điện áp trung bình đặt lên động cơ xấp xỉ:&lt;/p&gt;

\[V_{avg} \approx D \cdot V_s\]

&lt;p&gt;với hệ số chu kỳ \(D \in [0, 1]\). Ví dụ lệnh 8-bit bằng 180 tương ứng \(D = 180/255\). Độ tự cảm của cuộn dây làm phẳng dòng điện, nên giảm duty sẽ làm bánh chậm dần cho đến khi mô-men nhỏ hơn ma sát tĩnh và trục dừng hẳn.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Với bộ pin 2S có \(V_s = 7.4~\mathrm{V}\) và lệnh PWM 8-bit bằng 180/255:&lt;/p&gt;

\[D = \frac{180}{255} \approx 0.706\]

\[V_{avg} \approx 0.706 \times 7.4~\mathrm{V} \approx 5.22~\mathrm{V}\]

&lt;p&gt;5,22 V là điện áp trung bình trước khi trừ sụt áp trên mạch cầu H. L298N dùng tầng công suất Darlington lưỡng cực nên sụt khoảng 1,5–2 V khi dẫn, khiến hai đầu động cơ chỉ còn khoảng 3,2–3,7 V. Ngược lại, TB6612 dùng MOSFET có điện trở dẫn rất nhỏ nên giữ lại hầu hết mức 5,22 V cho động cơ — đó là lý do TB6612 là lựa chọn mặc định cho động cơ TT chạy pin 2S trong Capstone A.&lt;/p&gt;

&lt;h2 id=&quot;hình-minh-họa&quot;&gt;Hình minh họa&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Commutator_and_brushes_DC_motor.jpg&quot; alt=&quot;Cổ góp phân đoạn và chổi than của motor DC chổi than&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Các phiến đồng cổ góp đóng vai trò bộ đảo chiều cơ khí: chổi than đứng yên còn ống đồng quay bên dưới để đổi cực tính cấp cho cuộn dây.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;Ý tưởng cầu H: một đường qua motor, và chân đứng không được khép&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Chiều tiến tương ứng cặp khóa cao bên trái và thấp bên phải; tuyệt đối không để đồng thời khóa cao và khóa thấp trên cùng một nhánh đứng đóng cùng lúc.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Nhấc bánh xe khỏi mặt bàn hoặc kẹp cố định động cơ trần. Cấp nguồn động cơ từ pin hoặc bộ nguồn bàn có giới hạn dòng, không lấy từ cổng USB của vi điều khiển. Nối chung dây &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; giữa mạch cầu H và vi điều khiển. Giữ tay tránh xa bánh răng và luôn khởi đầu với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Bo mạch ESP32 hoặc họ Arduino&lt;/td&gt;
      &lt;td&gt;Xuất hai chân hướng và một chân PWM&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Một module cầu H&lt;/td&gt;
      &lt;td&gt;Mạch cần lập bảng chân lý&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Một động cơ DC chổi than (ưu tiên động cơ TT)&lt;/td&gt;
      &lt;td&gt;Quan sát chiều và chế độ dừng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ pin 2S hoặc nguồn bàn 6–9 V có giới hạn dòng&lt;/td&gt;
      &lt;td&gt;Cấp riêng cho cực &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ vạn năng&lt;/td&gt;
      &lt;td&gt;Đo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; và kiểm tra thông mạch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; chung&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Băng keo và sổ tay&lt;/td&gt;
      &lt;td&gt;Dán cờ quan sát chiều quay và ghi bảng chân lý&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Chụp ảnh sơ đồ đấu dây; đo kiểm điện áp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; và xác nhận vi điều khiển cấp nguồn riêng qua USB.&lt;/li&gt;
  &lt;li&gt;Đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt;, lần lượt thử bốn tổ hợp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(IN1, IN2)&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;00&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;01&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11&lt;/code&gt; (sau đó nâng nhẹ PWM khi cần kiểm chiều). Ghi nhận với mỗi tổ hợp xem trục đứng yên, quay chiều nào, hay đang ở chế độ hãm (khó xoay bằng tay) so với trôi (dễ xoay bằng tay).&lt;/li&gt;
  &lt;li&gt;Chọn tổ hợp quy ước cho chiều tiến, lần lượt đặt PWM ở các mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;64&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;128&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;180&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;255&lt;/code&gt; để xác nhận tốc độ tăng dần, rồi ghi lại bộ ba &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(IN1, IN2, PWM)&lt;/code&gt; quy ước chiều dương cho Chương 07.&lt;/li&gt;
  &lt;li&gt;Giữ PWM ở mức vừa phải, thử đảo hai dây động cơ một lần để thấy trục quay ngược chiều, sau đó cắm lại như cũ và ghi chú: đảo dây động cơ bên phải sẽ làm đảo dấu lệnh quay yaw.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Sổ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; có bảng chân lý bốn dòng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM&lt;/code&gt;, hành vi quan sát), bộ ba quy ước chiều tiến và phép tính 5,22 V cho mức 180/255 trên 7,4 V (hoặc tính lại \(D \cdot V_s\) theo đúng điện áp thực đo).&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Cần kiểm tra&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Trục không quay, mạch cầu H nóng nhanh&lt;/td&gt;
      &lt;td&gt;Chập dây hoặc trùng dẫn; đưa PWM về 0 và ngắt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; ngay&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trục không quay, mạch cầu H mát&lt;/td&gt;
      &lt;td&gt;Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enable&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; đang tắt, thiếu nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, hoặc sai chân PWM&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Vi điều khiển reset khi động cơ khởi động&lt;/td&gt;
      &lt;td&gt;Động cơ đang kéo dòng qua nguồn USB; chuyển &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; sang pin riêng và giữ mass chung&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chiều quay ngược với ghi chép&lt;/td&gt;
      &lt;td&gt;Cắm ngược hai dây động cơ hoặc đảo ngược &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN2&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ yếu hơn nhiều so với mức duty&lt;/td&gt;
      &lt;td&gt;Sụt áp lớn trên mạch cầu (nhất là L298N) hoặc pin yếu dưới tải&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bài này cần một động cơ giảm tốc DC có chổi than và một mạch cầu H (ưu tiên TB6612 cho Capstone A; L298N dùng được cho bài thực hành bảng chân lý). Giá tham khảo thay đổi theo thời điểm (năm 2026: động cơ TT vàng khoảng 25.000–45.000 VND, module L298N khoảng 35.000–70.000 VND):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=L298N&quot;&gt;L298N&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Trang L298N đã kiểm tra, khoảng 45.000 VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=L298N&quot;&gt;L298N&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=L298N&quot;&gt;L298N&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=fVgnUWIWzZ8&quot;&gt;Driving DC motors, part 3/3:  H-bridges (Kevin Lynch)&lt;/a&gt; — Northwestern Robotics, tiếng Anh. Phân tích bốn khóa cầu H và chiều dòng qua động cơ, khớp trực tiếp với sơ đồ trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=YYMsS50x1UY&quot;&gt;H Bridge Motor Control Basics Explained&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Giới thiệu ngắn gọn nguyên lý cầu H nếu muốn xem phiên bản cô đọng hơn.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một động cơ dùng pin 7,4 V có điện trở cuộn dây \(5~\Omega\). Ước lượng dòng stall.&lt;/li&gt;
  &lt;li&gt;Với PWM mức \(100/255\) và \(V_s = 6.0~\mathrm{V}\), tính \(V_{avg}\) trước sụt áp. Nếu mạch cầu H sụt 1,8 V, điện áp còn lại trên động cơ là bao nhiêu?&lt;/li&gt;
  &lt;li&gt;Nêu bằng lời: cặp khóa nào đóng khi quay tiến và cặp khóa nào đóng khi quay lùi?&lt;/li&gt;
  &lt;li&gt;Bảng chân lý quy ước &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1 = 1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN2 = 0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM &amp;gt; 0&lt;/code&gt; là tiến. Nếu cắm ngược dây động cơ bên phải trên robot hoàn chỉnh, lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; dương sẽ gây ra chuyển động gì?&lt;/li&gt;
  &lt;li&gt;Vì sao cần tự kiểm chứng chế độ trôi/hãm trên đúng mạch đang dùng thay vì chép bảng chân lý của L298N áp sang TB6612?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Dòng stall \(I_{stall} \approx 7.4 / 5 \approx 1.48~\mathrm{A}\).&lt;/li&gt;
  &lt;li&gt;\(D = 100/255 \approx 0.392\) cho \(V_{avg} \approx 2.35~\mathrm{V}\) trước sụt áp; sau khi trừ 1,8 V chỉ còn khoảng \(0.55~\mathrm{V}\), động cơ có thể không quay nổi.&lt;/li&gt;
  &lt;li&gt;Quay tiến: khóa cao bên trái + khóa thấp bên phải; quay lùi: khóa cao bên phải + khóa thấp bên trái.&lt;/li&gt;
  &lt;li&gt;Cắm ngược dây bánh phải làm lệnh yaw dương quay sai chiều.&lt;/li&gt;
  &lt;li&gt;Chế độ trôi (coast) và hãm (brake) phụ thuộc vào thiết kế nội bộ của từng chip driver; áp bảng chân lý của chip khác sẽ làm sai trạng thái dừng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Datasheet ST L298 (bảng chân lý, sụt áp và mức logic): &lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;https://www.st.com/resource/en/datasheet/l298.pdf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Mạch mang TB6612FNG của Pololu (cầu MOSFET mặc định cho Capstone A): &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;https://www.pololu.com/product/713&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;SparkFun — chọn động cơ và phân biệt dòng stall với dòng không tải: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&quot;&gt;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 04</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_07_ch04_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_07_ch04_lab_checklist</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;50–70 phút&lt;/strong&gt;. Đây là mốc kiểm tra hoàn thành chương, không bổ sung cảm biến mới. Bạn vượt qua chương 04 khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; chứa đầy đủ các bằng chứng đo kiểm bên dưới trước khi chuyển sang Chương 05.&lt;/p&gt;

&lt;p&gt;Trước khi để robot tự chạy vòng kín trong phòng lab, một kỹ sư robotics kỳ cựu không bao giờ vội viết thuật toán điều hướng ngay — họ dành 15 phút để &lt;strong&gt;kiểm định từng cảm biến lúc đứng yên và lúc chuyển động chậm&lt;/strong&gt;. Bởi nếu một dây encoder lỏng làm mất xung, một trục IMU bị đảo dấu, hay một cảm biến siêu âm nhiễu quá mức, thì bộ điều khiển thông minh nhất thế giới cũng sẽ lái robot đâm thẳng vào tường. Checklist Chương 04 đóng vai trò như “bảng kiểm trước giờ bay” giúp bạn tách biệt lỗi phần cứng/cảm biến khỏi lỗi thuật toán.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kiểm-định-cảm-biến-theo-quy-chuẩn-đo-lường&quot;&gt;Kiến thức cần nhớ: Kiểm định cảm biến theo quy chuẩn đo lường&lt;/h2&gt;

&lt;h3 id=&quot;1-độ-chính-xác-accuracy-vs-độ-chụm-precision--noise-floor&quot;&gt;1. Độ chính xác (Accuracy) vs. Độ chụm (Precision / Noise Floor)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Độ chính xác (Accuracy):&lt;/strong&gt; trung bình các phép đo có nằm sát giá trị thực tế hay bị lệch hằng số (&lt;strong&gt;bias&lt;/strong&gt;)? Bias sửa được bằng &lt;strong&gt;hiệu chuẩn (calibration)&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Độ chụm (Precision):&lt;/strong&gt; khi robot đứng yên tuyệt đối, các mẫu đo dao động quanh trung bình với độ lệch chuẩn \(\sigma\) lớn cỡ nào? Độ phân tán \(\sigma\) phản ánh &lt;strong&gt;sàn nhiễu (noise floor)&lt;/strong&gt; của cảm biến.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-quy-ước-dấu-và-hệ-tọa-độ-đồng-nhất-rep-103&quot;&gt;2. Quy ước dấu và hệ tọa độ đồng nhất (REP-103)&lt;/h3&gt;
&lt;p&gt;Trong robotics (đặc biệt khi tiến lên ROS 2 ở các chương sau), quy ước chuẩn là hệ tọa độ tay phải: trục &lt;strong&gt;X hướng về phía trước&lt;/strong&gt;, &lt;strong&gt;Y sang trái&lt;/strong&gt;, &lt;strong&gt;Z hướng lên trên&lt;/strong&gt;; quay ngược chiều kim đồng hồ nhìn từ trên xuống là &lt;strong&gt;yaw dương (+)&lt;/strong&gt;. Kiểm tra dấu encoder và gyro ngay từ lab Chương 04 giúp bạn tránh hàng giờ gỡ lỗi ngược hướng về sau.&lt;/p&gt;

&lt;h3 id=&quot;3-đánh-đổi-giữa-lọc-nhiễu-averaging-và-độ-trễ-pha&quot;&gt;3. Đánh đổi giữa lọc nhiễu (Averaging) và độ trễ pha&lt;/h3&gt;
&lt;p&gt;Lấy trung bình trượt \(N\) mẫu độc lập giúp giảm độ lệch chuẩn nhiễu đi \(\sqrt{N}\) lần, nhưng đồng thời cộng thêm độ trễ trung bình \(\frac{N-1}{2}\Delta t\). Trong vòng điều khiển nhanh, lọc quá tay sẽ làm robot phản ứng chậm và lắc qua lại.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-trung-bình-độ-lệch-chuẩn-sàn-nhiễu-và-hiệu-quả-lọc-trung-bình-4-mẫu&quot;&gt;Giải thích: Cầm tay tính trung bình, độ lệch chuẩn sàn nhiễu và hiệu quả lọc trung bình 4 mẫu&lt;/h3&gt;

&lt;p&gt;Khi robot đứng yên cách tường phẳng đúng &lt;strong&gt;\(20{,}0\text{ cm}\)&lt;/strong&gt;, bạn ghi lại 5 mẫu liên tiếp từ cảm biến khoảng cách (đơn vị cm):
\([20{,}0,\quad 20{,}4,\quad 19{,}6,\quad 20{,}2,\quad 19{,}8].\)&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tính giá trị trung bình mẫu (\(\bar{x}\)):&lt;/strong&gt;
\(\bar{x} = \frac{20{,}0 + 20{,}4 + 19{,}6 + 20{,}2 + 19{,}8}{5} = \frac{100{,}0}{5} = 20{,}0\text{ cm}\)
&lt;em&gt;(Bias \(\approx 0\) — cảm biến đã được hiệu chuẩn tốt!)&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Tính phương sai mẫu (\(s^2\)) và độ lệch chuẩn sàn nhiễu (\(s\)):&lt;/strong&gt;
Các độ lệch \((x_i - \bar{x})\): \([0,\; +0{,}4,\; -0{,}4,\; +0{,}2,\; -0{,}2]\).
\(s^2 = \frac{0^2 + 0{,}4^2 + (-0{,}4)^2 + 0{,}2^2 + (-0{,}2)^2}{5 - 1} = \frac{0 + 0{,}16 + 0{,}16 + 0{,}04 + 0{,}04}{4} = \frac{0{,}40}{4} = 0{,}10\text{ cm}^2.\)
\(s = \sqrt{0{,}10} \approx 0{,}316\text{ cm}.\)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Nếu gom trung bình \(N = 4\) mẫu ở tần số đo \(20\text{ Hz}\) (\(\Delta t = 50\text{ ms}\)):&lt;/strong&gt;&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;Sàn nhiễu giảm xuống còn: \(s_{\text{mới}} = \frac{0{,}316}{\sqrt{4}} = 0{,}158\text{ cm}\) (mịn gấp đôi!).&lt;/li&gt;
      &lt;li&gt;Cái giá phải trả: độ trễ trung bình tăng thêm \(\frac{4-1}{2} \times 50\text{ ms} = 75\text{ ms}\) — hoàn toàn chấp nhận được cho robot chạy chậm \(0{,}2\text{ m/s}\) (chỉ đi thêm \(1{,}5\text{ cm}\)), nhưng sẽ quá chậm cho drone bay nhanh!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Bạn tự đánh giá sổ thực hành Chương 04 theo tiêu chí đạt/trượt rõ ràng: có địa chỉ I2C kèm ảnh đấu dây, có giá trị \(N\) đo bằng tay cho encoder cùng mét trên mỗi xung, có số đo IMU lúc nằm yên (hoặc ghi chú hoãn có ngày kèm tính tay), có bảng sai số đo khoảng cách (hoặc bản dry-run 5,8 ms và 3,33 V), và có câu quyết định hoãn camera chứa con số 147 Mbit/s. Bạn trình bày gọn gàng trong một khối mẫu để người khác dễ kiểm tra, ghi nhận hai lỗi thực tế đã gặp (hoặc chủ động tái hiện để kiểm chứng) và phân biệt được một bản ghi chép kín chữ nhưng vẫn không đạt yêu cầu.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bài này tổng hợp kết quả từ Bài 04-01 đến 04-06 trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; cùng phiếu nhận diện Bài 04-02 và các con số kiểm chuẩn đã tính: \(N\) và \(r = 0{,}0325~\mathrm{m}\) (với bánh ví dụ), bước lọc bổ trợ $9{,}889^\circ$, xung Echo 5,8 ms tương ứng 99,5 cm, cầu chia áp 3,33 V và băng thông camera 147 Mbit/s. Chương 05 sẽ bắt đầu đấu động cơ và mạch cầu H nên mọi sai sót về điện áp hay hệ số đếm cần được xử lý dứt điểm tại đây.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Firmware teleop ở Chương 07 dựa trực tiếp vào cảm biến khoảng cách để dừng an toàn và dùng hằng số \(N\) để tính quãng đường bánh xe. Về sau, ROS 2 xuất đúng các đại lượng này thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Range&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Imu&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Image&lt;/code&gt;. Nếu \(N\) bị nhân đôi, odometry sẽ sai một nửa; nếu timeout của siêu âm bị lưu thành 0 cm, hệ thống sẽ báo nhầm là đang chạm tường; còn câu quyết định camera giúp tránh phát sinh tải USB 147 Mbit/s ngoài kế hoạch trước Chương 11.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sensor_package_id.png&quot; alt=&quot;Phiếu nhận diện mà cửa này kiểm: bus, điện áp, và module nào được hoãn&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;mẫu-khối-ghi-nhận-trong-lab-notesmd&quot;&gt;Mẫu khối ghi nhận trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Chép mẫu dưới đây vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, điền đầy đủ bằng chứng thực tế và xóa bỏ các dòng chưa có dữ liệu xác thực (giữ lại dòng để trống sẽ bị tính chưa đạt):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Ky duyet chuong 04
Ngay:
I2C: dia chi thay = ____ ; file anh = ____
Encoder: N = ____ xung moi vong truc ra (ba lan: __, __, __)
         r = ____ m ; met moi xung = ____
         kenh dan khi tien: ____  (hoac &quot;mot kenh, khong biet chieu&quot;)
IMU: |a| = ____ g ; truc luc de yen = ____ dau ____
     lech gyro deg/s = (__, __, __)
     HOAC giay hoan ngay ____ va da kiem buoc loc 9.889 deg
Do tam: 20 cm -&amp;gt; __ cm (sai __) ; 40 -&amp;gt; __ (sai __) ; 80 -&amp;gt; __ (sai __)
       HOAC dry-run: 5.8 ms -&amp;gt; 99.5 cm ; cau chia 5*(2/3) = 3.33 V
Camera: hoan den chuong 11 ; YUYV 640x480 30 fps xap xi 147 Mbit/s
Hong 1:
Hong 2:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;tiêu-chí-đạt-và-trượt&quot;&gt;Tiêu chí đạt và trượt&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;I2C:&lt;/strong&gt; ghi tối thiểu một địa chỉ quét được (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;) kèm ảnh chụp rõ module và dây nối. Ghi chung chung “quét ổn, không lỗi” mà thiếu địa chỉ và ảnh là trượt.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Encoder:&lt;/strong&gt; \(N\) phải đo từ ba lần quay tay trục ra (lệch nhau không quá 1 xung), kèm giá trị \(2\pi r / N\) tính theo mét (ví dụ với \(r = 0{,}0325~\mathrm{m}\), chu vi \(\approx 0{,}2042~\mathrm{m}\) và \(N = 20\) thì mỗi xung là \(0{,}01021~\mathrm{m}\)). Chép thông số PPR của người bán khi kết quả quay tay khác đi là trượt. Với encoder một kênh, chỉ đạt khi ghi rõ không xác định được chiều từ tín hiệu chân đó.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;IMU:&lt;/strong&gt; ghi độ lớn gia tốc khi nằm yên (gần 1 g), tên trục và dấu, cùng sai lệch tĩnh gyro tính bằng độ/giây sau khi đã chia thang datasheet — hoặc ghi chú hoãn có ngày kèm đủ hai phép tính tay: bước lọc bổ trợ \(\theta = 9{,}889~\mathrm{deg}\) và độ trôi \(0{,}5~\mathrm{deg/s} \times 60~\mathrm{s} = 30~\mathrm{deg}\). In số nguyên LSB thô như độ sẽ bị tính trượt.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đo khoảng cách:&lt;/strong&gt; có đủ bảng 20 / 40 / 80 cm kèm sai số có dấu (ở 40 cm trên bìa phẳng, sai số trong khoảng 5 cm là đạt; nếu lớn hơn phải giải thích được nguyên nhân). Thiếu bất kỳ mốc nào là trượt. Nếu chưa có cảm biến, bản dry-run chỉ đạt khi có đủ phép tính \(5{,}8~\mathrm{ms} \rightarrow 99{,}5~\mathrm{cm}\) và cầu chia \(5 \times 2/3 \approx 3{,}33~\mathrm{V}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Camera:&lt;/strong&gt; ghi rõ quyết định hoãn camera đến Chương 11 kèm số liệu luồng YUYV 640×480 ở 30 fps xấp xỉ 147 Mbit/s. Thiếu con số này là trượt.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hai lỗi thực tế:&lt;/strong&gt; mỗi lỗi nêu rõ hiện tượng quan sát được và cách đã sửa (hoặc sẽ sửa). Ghi “không có lỗi” sẽ không đạt; có thể ghi nhận trường hợp thử nghiệm đảo dây A/B hoặc đo tấm vải mất Echo nếu có số liệu kèm theo.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;ví-dụ-một-sổ-ghi-chép-chưa-đạt&quot;&gt;Ví dụ một sổ ghi chép chưa đạt&lt;/h2&gt;

&lt;p&gt;Mẫu dưới đây trông có vẻ đã điền kín các mục nhưng thực chất vi phạm nhiều tiêu chí quan trọng:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# lab-notes.md — vi du KHONG dat
Ngay: 12/9

I2C: quet on, khong loi. (khong dia chi, khong anh)

Encoder: nguoi ban ghi 40 PPR nen N = 40.
Quay tay mot vong thay khoang 20 nhay nhung minh giu 40.
r = 0.0325 m
met moi xung = 0.2042/40 = 0.0051 m

IMU: de phang, az = 16384 do. Gyro 70.

Do tam: ECHO vao GPIO18, khong dien tro, VCC 5 V.
So nhay. Minh se lay trung binh trong code.
Chua lam moc 20 cm. Moc 40 cm doi khi ra 80.

Camera: dang dat ESP32-CAM de Chuong 07 nhin thay hanh lang.

Loi: khong co.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Phân tích các lỗi: mục I2C thiếu cả địa chỉ lẫn ảnh; mục Encoder đo tay ra 20 nhưng vẫn giữ \(N = 40\) khiến mét trên mỗi xung chỉ còn một nửa (140 xung bị tính thành \(0{,}71~\mathrm{m}\) thay vì \(1{,}43~\mathrm{m}\)); mục IMU in giá trị LSB thô như độ (đúng ra ở \(\pm 2~\mathrm{g}\), 16384 là \(+1~\mathrm{g}\), và ở \(\pm 250~\mathrm{deg/s}\), 70 tương ứng \(70/131 \approx 0{,}53~\mathrm{deg/s}\)); mục đo tầm nối thẳng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; 5 V vào GPIO18 không qua cầu chia 1 kΩ / 2 kΩ (3,33 V) và bỏ sót mốc 20 cm; mục camera thiếu con số 147 Mbit/s và chọn sai ESP32-CAM cho Chương 07; cuối cùng dòng “không có lỗi” đã che lấp chính những sai sót nghiêm trọng ngay trên trang.&lt;/p&gt;

&lt;h2 id=&quot;cần-khắc-phục-gì-trước-khi-sang-chương-05&quot;&gt;Cần khắc phục gì trước khi sang Chương 05&lt;/h2&gt;

&lt;p&gt;Chương 05 sẽ bổ sung nguồn động cơ và mạch cầu H có dòng tải lớn. Trước khi chốt Chương 04, cần bảo đảm: lắp cầu chia áp 1 kΩ / 2 kΩ (~3,33 V) cho chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; hoặc tháo dây &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; ra; cập nhật \(N\) theo kết quả quay tay và tính lại mét trên mỗi xung; quy đổi dữ liệu IMU sang g và độ/giây (hoặc dùng ghi chú hoãn có ngày kèm $9{,}889^\circ$ và $30^\circ$); hoàn thiện bảng đo tầm hoặc bản dry-run; sửa lại câu hoãn camera (không dùng ESP32-CAM cho Chương 07); và ghi rõ hai lỗi thực tế đã xử lý.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Bài này tập trung rà soát sổ thực hành. Nếu cần cấp nguồn chụp lại ảnh, chỉ dùng USB, giữ bánh xe nhấc khỏi bàn và đảm bảo chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; đi qua cầu chia áp; không cố tình tái hiện lỗi nối thẳng 5 V lên GPIO thật.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Tài liệu rà soát và ký duyệt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ảnh phiếu Bài 04-02&lt;/td&gt;
      &lt;td&gt;Đối chiếu địa chỉ và sơ đồ chân&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sổ Bài 04-05&lt;/td&gt;
      &lt;td&gt;Bảng sai số hoặc bản dry-run&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Máy tính cầm tay&lt;/td&gt;
      &lt;td&gt;Kiểm tra lại \(2\pi r/N\) và phép tính Echo 5,8 ms&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Dán khối mẫu ký duyệt vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; và điền đầy đủ từ kết quả thực đo của các bài trước (dùng ghi chú hoãn hoặc dry-run có ngày đối với linh kiện chưa giao).&lt;/li&gt;
  &lt;li&gt;Kiểm tra lại trực tiếp phép tính mét trên mỗi xung và \(343 \times 0{,}0058 / 2 = 0{,}995~\mathrm{m}\).&lt;/li&gt;
  &lt;li&gt;Đối chiếu trang sổ của bạn với ví dụ chưa đạt ở trên để sửa mọi mục còn sơ sài hoặc sai đơn vị.&lt;/li&gt;
  &lt;li&gt;Ghi rõ hai lỗi đã gặp kèm số liệu cụ thể trước khi chuyển sang Chương 05.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Bất kỳ bạn cùng lớp nào đọc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; cũng thấy ngay: một địa chỉ I2C kèm ảnh, một giá trị \(N\) đo tay, số đo IMU theo g và độ/giây (hoặc ghi chú hoãn đủ số), bảng đo khoảng cách (hoặc 99,5 cm và 3,33 V), câu 147 Mbit/s và hai lỗi có số liệu.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Các mục chỉ ghi “có” hoặc “đã làm”&lt;/td&gt;
      &lt;td&gt;Thiếu con số đo, địa chỉ cụ thể hoặc tên tệp ảnh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chép \(N\) từ tiêu đề bán hàng&lt;/td&gt;
      &lt;td&gt;Bỏ qua kết quả tự quay tay để xác định \(N\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ghi chú hoãn IMU thiếu 9,889 và 30 độ&lt;/td&gt;
      &lt;td&gt;Chưa hoàn thành phần tính tay bắt buộc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bản dry-run đo tầm thiếu 3,33 V&lt;/td&gt;
      &lt;td&gt;Bỏ quên phép tính cầu chia áp trên giấy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Câu camera không ghi tốc độ bit&lt;/td&gt;
      &lt;td&gt;Thiếu số liệu 147 Mbit/s theo yêu cầu&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Chỉ mua bổ sung các cảm biến còn thiếu cho mốc hoàn thành chương; chưa mua camera.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Thay thế&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HC-SR04&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-sieu-am-srf04&quot;&gt;Trang Hshop&lt;/a&gt;, thường khoảng 20.000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;VL53L0X (tùy chọn)&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VL53L0X&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-tof-laser-radar-vl53l0x&quot;&gt;Hshop VL53L0X&lt;/a&gt;. Chỉ dùng siêu âm vẫn đủ đạt chương.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MPU-6050 GY-521&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;Hshop GY-521&lt;/a&gt;, thường khoảng 85.000. Có thể dùng MPU6500 nếu khớp thư viện.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ TT có encoder (một cặp)&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ TT encoder&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;40.000–90.000 một cái&lt;/td&gt;
      &lt;td&gt;Luôn tự đo \(N\) bằng tay thay vì tin tiêu đề&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=HC-SR04&quot;&gt;Hshop: HC-SR04&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=MPU6050%20GY-521&quot;&gt;Shopee: MPU6050&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=dong%20co%20TT%20encoder&quot;&gt;Lazada: động cơ TT encoder&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=VL53L0X&quot;&gt;Thế Giới IC: VL53L0X&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=IyGwvGzrqp8&quot;&gt;Serial Communications Explained: UART, I2C, and SPI&lt;/a&gt; — Electronoobs, tiếng Anh. Ôn nhanh ba chuẩn giao tiếp để kiểm tra lại toàn bộ đấu dây trước khi chốt Chương 04.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;p&gt;Dựa trên mẫu sổ chưa đạt và các con số trong bài để trả lời ngắn gọn:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Sổ mẫu đặt \(N = 40\) với \(r = 0{,}0325~\mathrm{m}\) dù quay tay thu được khoảng 20 xung. Họ đã tính ra bao nhiêu mét mỗi xung, giá trị đúng phải là bao nhiêu, và 140 xung tương ứng bao xa trong hai trường hợp?&lt;/li&gt;
  &lt;li&gt;Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; nối thẳng vào GPIO không qua cầu chia áp. Khi Echo ở mức cao, chân GPIO phải chịu bao nhiêu vôn? Cần mắc cặp điện trở nào và điện áp sau chia \(V_{\mathrm{pin}}\) bằng bao nhiêu?&lt;/li&gt;
  &lt;li&gt;Ghi chú hoãn IMU chỉ viết “module để sau” có đạt không? Hai kết quả tính tay nào bắt buộc phải có trên dòng đó?&lt;/li&gt;
  &lt;li&gt;Bản dry-run đo khoảng cách ghi “5,8 ms → 99,5 cm” nhưng không nhắc đến cầu chia áp. Đạt hay trượt, và còn thiếu con số điện áp nào?&lt;/li&gt;
  &lt;li&gt;Viết một câu quyết định camera đạt yêu cầu và chỉ ra câu nào trong sổ mẫu làm mục này bị trượt.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Họ tính ra \(0{,}0051~\mathrm{m}\) mỗi xung; với số đo tay \(N = 20\), giá trị đúng là \(0{,}01021~\mathrm{m}\). Sau 140 xung, sổ sai báo \(0{,}71~\mathrm{m}\) còn giá trị đúng là \(1{,}43~\mathrm{m}\).&lt;/li&gt;
  &lt;li&gt;Chân chịu 5 V; cần mắc 1 kΩ từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; tới GPIO và 2 kΩ từ GPIO xuống mass để hạ \(V_{\mathrm{pin}} \approx 3{,}33~\mathrm{V}\).&lt;/li&gt;
  &lt;li&gt;Trượt; ghi chú hoãn bắt buộc có ngày, kết quả lọc bổ trợ \(9{,}889~\mathrm{deg}\) và độ trôi \(0{,}5 \times 60 = 30~\mathrm{deg}\).&lt;/li&gt;
  &lt;li&gt;Trượt; thiếu kết quả điện áp cầu chia \(5 \times 2/3 \approx 3{,}33~\mathrm{V}\).&lt;/li&gt;
  &lt;li&gt;Câu đạt: hoãn camera đến Chương 11 kèm số liệu khoảng 147 Mbit/s; câu làm trượt trong sổ mẫu là “đang đặt ESP32-CAM để Chương 07 nhìn thấy hành lang”.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf&quot;&gt;Datasheet HC-SR04 (bản SparkFun)&lt;/a&gt; — xác nhận đặc tính xung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; 5 V.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf&quot;&gt;Datasheet MPU-6050&lt;/a&gt; — bảng hệ số quy đổi 16384 LSB/g thành 1 g.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/sensor_msgs/msg/Range.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Range&lt;/code&gt;&lt;/a&gt; và &lt;a href=&quot;https://docs.ros.org/en/jazzy/p/nav_msgs/msg/Odometry.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt;&lt;/a&gt; — hai loại bản tin ROS 2 tương ứng với các mục kiểm chứng trong chương này.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/i2c.html&quot;&gt;I2C của Espressif&lt;/a&gt; — tài liệu ngoại vi I2C dùng cho bước quét địa chỉ.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Camera cho robot: USB, CSI, băng thông</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_06_cameras_robots/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_06_cameras_robots</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;60–75 phút&lt;/strong&gt;. Yêu cầu bắt buộc của bài là bản dự toán băng thông; việc có sẵn camera trên bàn chỉ là tùy chọn và Chương 07 không yêu cầu bạn mua camera.&lt;/p&gt;

&lt;p&gt;Nếu encoder và siêu âm chỉ cho robot vài con số rời rạc mỗi giây, thì một chiếc &lt;strong&gt;camera&lt;/strong&gt; lại đổ vào bộ não robot một dòng thác hàng chục triệu byte dữ liệu mỗi giây. Chính vì giàu thông tin đến vậy, rất nhiều đội sinh viên mới làm robot rơi vào cùng một cái bẫy: cắm một chiếc webcam 1080p vào cổng USB 2.0 của Raspberry Pi, bật OpenCV ở 30 khung hình/giây rồi ngạc nhiên khi hệ thống giật cứng, CPU nhảy lên 100% và độ trễ điều khiển vọt lên nửa giây. Hiểu về camera cho robot không chỉ là gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cv2.VideoCapture(0)&lt;/code&gt;, mà là hiểu &lt;strong&gt;băng thông bus (USB vs. CSI), định dạng điểm ảnh và ngân sách độ trễ&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-giao-diện-truyền-ảnh-không-gian-màu-và-giới-hạn-băng-thông&quot;&gt;Kiến thức cần nhớ: Giao diện truyền ảnh, không gian màu và giới hạn băng thông&lt;/h2&gt;

&lt;h3 id=&quot;1-usb-uvc-vs-mipi-csi-2--đường-ống-dữ-liệu-khác-nhau-ra-sao&quot;&gt;1. USB (UVC) vs. MIPI CSI-2 — đường ống dữ liệu khác nhau ra sao?&lt;/h3&gt;
&lt;p&gt;Webcam &lt;strong&gt;USB&lt;/strong&gt; tiện lợi kiểu cắm-là-chạy, nhưng mọi khung hình phải đóng gói qua lớp giao thức USB, đi qua bộ điều khiển host và tiêu tốn chu kỳ CPU/DMA để giải mã. Ngược lại, &lt;strong&gt;MIPI CSI-2&lt;/strong&gt; (cáp dẹt trên Raspberry Pi, Jetson) đổ dữ liệu cảm biến trực tiếp vào bộ xử lý ảnh phần cứng (ISP) và RAM qua DMA với độ trễ cực thấp và gần như không tốn CPU.&lt;/p&gt;

&lt;h3 id=&quot;2-ảnh-thô-raw--rgb--yuyv-vs-ảnh-nén-mjpeg--h264&quot;&gt;2. Ảnh thô (RAW / RGB / YUYV) vs. Ảnh nén (MJPEG / H.264)&lt;/h3&gt;
&lt;p&gt;Mỗi điểm ảnh màu RGB888 tốn &lt;strong&gt;3 byte&lt;/strong&gt; (24 bit), YUYV (4:2:2) tốn &lt;strong&gt;2 byte&lt;/strong&gt; (16 bit), còn ảnh xám (Grayscale — quá đủ cho tìm vạch kẻ đường hay AprilTag!) chỉ tốn &lt;strong&gt;1 byte&lt;/strong&gt;. Khi băng thông USB 2.0 bị quá tải, camera tự chuyển sang nén &lt;strong&gt;MJPEG&lt;/strong&gt; để gửi vừa đường ống, đổi lại CPU robot phải giải nén lại từng khung hình.&lt;/p&gt;

&lt;h3 id=&quot;3-rolling-shutter-vs-global-shutter-và-hiện-tượng-méo-khi-robot-quay-nhanh&quot;&gt;3. Rolling Shutter vs. Global Shutter và hiện tượng méo khi robot quay nhanh&lt;/h3&gt;
&lt;p&gt;Camera giá rẻ dùng &lt;strong&gt;rolling shutter&lt;/strong&gt; (quét từng dòng từ trên xuống dưới trong 10–30 ms). Khi robot xoay nhanh hoặc rung mạnh, các đường thẳng đứng bị nghiêng méo (hiệu ứng “thạch rau câu”), làm sai lệch thuật toán Visual Odometry/SLAM — lý do các camera chuyên dụng cho robotics thường chọn &lt;strong&gt;global shutter&lt;/strong&gt; (chụp toàn bộ cảm biến cùng một khoảnh khắc).&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-băng-thông-luồng-ảnh-để-tránh-làm-nghẽn-cổ-chai-usb-20&quot;&gt;Giải thích: Cầm tay tính băng thông luồng ảnh để tránh làm nghẽn cổ chai USB 2.0&lt;/h3&gt;

&lt;p&gt;Biết rằng chuẩn &lt;strong&gt;USB 2.0&lt;/strong&gt; có tốc độ lý thuyết \(480\text{ Mbps}\), nhưng băng thông hữu dụng thực tế cho truyền ảnh liên tục chỉ khoảng &lt;strong&gt;\(280\text{–}320\text{ Mbps}\)&lt;/strong&gt;. Hãy tính lưu lượng ảnh thô &lt;strong&gt;RGB888 (3 byte/pixel = 24 bit/pixel)&lt;/strong&gt; ở &lt;strong&gt;30 FPS&lt;/strong&gt; cho 3 độ phân giải phổ biến:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Cấu hình ảnh (30 FPS)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Số byte mỗi khung&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Lưu lượng (MB/s)&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Băng thông (Mbps)&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Khả năng chạy trên USB 2.0 (thô)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;640×480 Grayscale (1 B/px)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(307\,200\text{ B}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(9{,}22\text{ MB/s}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(73{,}7\text{ Mbps}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Rất thoáng (dùng ~25% bus)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;640×480 RGB888 (3 B/px)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(921\,600\text{ B}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(27{,}65\text{ MB/s}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(221{,}2\text{ Mbps}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Vừa khít (chỉ cắm được 1 camera!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;1920×1080 RGB888 (3 B/px)&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(6\,220\,800\text{ B}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(186{,}6\text{ MB/s}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;\(1\,493\text{ Mbps}\)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;strong&gt;Quá tải gần 5×!&lt;/strong&gt; Bắt buộc nén MJPEG hoặc dùng USB 3.0 / CSI&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Kiểm tra nhanh dòng thứ ba: \(1\,920 \times 1\,080 \times 3 \times 30 \times 8 = 1\,492\,992\,000\text{ bit/s} \approx 1{,}493\text{ Gbps}\). Nhìn bảng này, bạn hiểu ngay vì sao robot dò đường hay bám tag chỉ cần &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;640×480&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;320×240&lt;/code&gt; ảnh xám là vừa đạt 30–60 FPS siêu mượt, vừa tiết kiệm gấp &lt;strong&gt;20 lần&lt;/strong&gt; băng thông và bộ nhớ!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn tính được băng thông video không nén và MJPEG theo độ phân giải và tốc độ khung hình, rồi đối chiếu với thông lượng thực tế của bus USB 2.0 khi chia sẻ cùng các thiết bị khác. Bạn phân biệt được ba phương án: webcam chuẩn UVC (USB, máy tính làm host), camera CSI (cáp dẹt không đi qua bộ điều khiển USB) và ESP32-CAM (xuất JPEG nhưng không phù hợp làm bộ điều khiển chính của Capstone A). Cuối bài, bạn viết câu quyết định hoãn camera đến phần nhận thức kèm con số 147 Mbit/s cho checklist chương 04.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã lập phiếu nhận diện linh kiện ở Bài 04-02 và nắm rõ giới hạn phần cứng của ESP32/Pico từ Chương 02. Bài này chỉ cần các phép tính nhân chia cơ bản; chưa cần OpenCV, GPU hay xử lý ảnh trên ROS (các nội dung đó dành cho Chương 11).&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Capstone A không cần camera: firmware Chương 07 điều khiển hai bánh qua lệnh serial và dừng an toàn bằng timeout hoặc cảm biến khoảng cách. Đưa thêm luồng video USB vào cùng thời điểm chỉ làm tranh chấp băng thông USB, tài nguyên CPU và thời gian triển khai, dễ làm chậm tiến độ phần động cơ.&lt;/p&gt;

&lt;p&gt;Đến phần nhận thức (Chương 11), hệ thống ROS 2 thường dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam&lt;/code&gt; hoặc node V4L2 để xuất ảnh thô qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Image&lt;/code&gt; hoặc ảnh nén JPEG qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/CompressedImage&lt;/code&gt;. Trường mã hóa (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yuyv&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rgb8&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jpeg&lt;/code&gt;) quyết định trực tiếp số byte trên mỗi điểm ảnh trong công thức tính băng thông bên dưới. Camera CSI trên Raspberry Pi không đi qua bộ điều khiển USB nhưng cũng không có cổng cắm trên ESP32. Do đó, việc tính trước băng thông và chốt kế hoạch hoãn camera giúp bạn tập trung ngân sách vào cảm biến khoảng cách và động cơ encoder.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sensor_package_id.png&quot; alt=&quot;Dòng nào là camera: phích USB hoặc cáp dẹt, không phải hai ống nhôm&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Đối chiếu lại phiếu nhận diện ở Bài 04-02: nhóm camera có đầu nối USB hoặc cáp dẹt bản rộng, khác hẳn hai ống nhôm của cảm biến siêu âm hay cửa sổ quang tí hon của cảm biến ToF.&lt;/p&gt;

&lt;h2 id=&quot;tính-băng-thông-từ-bytegiây-sang-mbits&quot;&gt;Tính băng thông: từ byte/giây sang Mbit/s&lt;/h2&gt;

&lt;p&gt;Băng thông của một luồng video không nén được ước lượng bởi:&lt;/p&gt;

\[B \approx w \times h \times \mathrm{fps} \times (\text{byte trên mỗi điểm ảnh}).\]

&lt;p&gt;Định dạng YUYV (4:2:2) dùng trung bình 2 byte cho mỗi điểm ảnh. Với độ phân giải 640×480 ở tốc độ 30 khung hình/giây:&lt;/p&gt;

\[B = 640 \times 480 \times 2 \times 30 = 18{,}432{,}000~\mathrm{byte/s}.\]

&lt;p&gt;Đổi sang bit/giây bằng cách nhân 8:&lt;/p&gt;

\[18{,}432{,}000 \times 8 = 147{,}456{,}000~\mathrm{bit/s} \approx 147~\mathrm{Mbit/s}.\]

&lt;p&gt;Tốc độ lý thuyết của USB 2.0 là 480 Mbit/s, nhưng khi trừ phần mào đầu giao thức và chia sẻ bus với hub hay bộ chuyển đổi USB-serial, thông lượng thực tế cho video chỉ vào khoảng 280–300 Mbit/s. Như vậy, chỉ một luồng YUYV 640×480 đã chiếm gần một nửa ngân sách thực tế, không còn đủ chỗ cho luồng thô thứ hai. Hơn nữa, video trên USB dùng chế độ truyền đẳng thời (isochronous): gói đến trễ sẽ bị bỏ qua thay vì truyền lại, gây rách hoặc mất khung hình.&lt;/p&gt;

&lt;p&gt;Nếu dùng nén MJPEG với tỉ lệ nén khoảng 10:1 trên cùng độ phân giải và tốc độ khung:&lt;/p&gt;

\[147 / 10 \approx 15~\mathrm{Mbit/s}.\]

&lt;p&gt;Mức 15 Mbit/s chạy thoải mái cùng cổng serial trên USB 2.0, đánh đổi bằng tải CPU giải mã trên máy tính chủ. Ngược lại, định dạng RGB (3 byte/điểm ảnh) còn nặng hơn YUYV: \(640 \times 480 \times 3 \times 30 = 27{,}648{,}000~\mathrm{byte/s}\), tương đương khoảng 221 Mbit/s, rất dễ tranh chấp băng thông với các thiết bị khác trên cùng bus.&lt;/p&gt;

&lt;h2 id=&quot;ba-phương-án-lắp-camera-cho-robot&quot;&gt;Ba phương án lắp camera cho robot&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Webcam USB (UVC):&lt;/strong&gt; giao tiếp theo chuẩn USB Video Class; trên Linux, lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsusb&lt;/code&gt; sẽ liệt kê thiết bị và node V4L2 mở cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video0&lt;/code&gt;. Máy tính chủ (laptop hoặc Raspberry Pi) đóng vai trò host, không nối vào GPIO. Đây là phương án hợp lý cho Chương 11 khi ESP32 chỉ đảm nhiệm điều khiển động cơ (và nên cấu hình xuất MJPEG để tiết kiệm băng thông).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Camera CSI:&lt;/strong&gt; nối trực tiếp bằng cáp ruy-băng vào cổng CSI của Raspberry Pi. Dữ liệu ảnh không đi qua bộ điều khiển USB nên không chiếm dụng băng thông 480 Mbit/s của USB 2.0. Tuy nhiên, ESP32 DevKit và Pico không có cổng CSI nên không thể sử dụng loại này.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;ESP32-CAM (OV2640):&lt;/strong&gt; nén sẵn JPEG giúp giảm tải đường truyền, nhưng bản thân bo mạch lại bị chiếm nhiều chân GPIO cho camera, mạch ổn áp dễ sụt nguồn khi chạy đồng thời Wi-Fi và cảm biến, đồng thời thiếu chân thuận tiện cho mạch cầu H động cơ. Có thể dùng làm mạch thí nghiệm phụ, nhưng không dùng thay ESP32 DevKit làm bộ điều khiển chính của Capstone A.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;lab-lập-bản-dự-toán-băng-thông&quot;&gt;Lab: lập bản dự toán băng thông&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Không dùng động cơ. Nếu có webcam USB để thử lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsusb&lt;/code&gt;, chỉ cắm vào cổng USB của laptop, không nối vào GPIO. Không cấp nguồn cho ESP32-CAM từ chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; của ESP32 DevKit (ESP32-CAM cần nguồn 5 V riêng ổn định).&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Giấy hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Ghi bản tính băng thông bắt buộc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Webcam USB (nếu có sẵn)&lt;/td&gt;
      &lt;td&gt;Thực hành kiểm tra tùy chọn bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsusb&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Laptop&lt;/td&gt;
      &lt;td&gt;Máy chủ cho webcam tùy chọn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32-CAM&lt;/td&gt;
      &lt;td&gt;Không bắt buộc và không dùng làm bộ điều khiển Capstone A&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Thực hiện phép tính \(640 \times 480 \times 2 \times 30\) để ra 18.432.000 byte/s và khoảng 147 Mbit/s, ghi đầy đủ vào sổ tay.&lt;/li&gt;
  &lt;li&gt;Chia cho 10 để ước lượng luồng MJPEG (khoảng 15 Mbit/s) và ghi chú ngân sách thực tế của USB 2.0 khoảng 280–300 Mbit/s khi dùng chung.&lt;/li&gt;
  &lt;li&gt;Viết một câu chốt quyết định hoãn camera đến Chương 11, bắt buộc có chứa con số 147 Mbit/s (ví dụ: “Hoãn camera đến Chương 11; một luồng YUYV 640×480 ở 30 fps đã chiếm khoảng 147 Mbit/s.”).&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;(Tùy chọn, nếu có sẵn camera)&lt;/em&gt; Cắm vào laptop, chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsusb&lt;/code&gt;, chép lại dòng thiết bị vừa xuất hiện và ghi loại đầu nối (USB-A, micro-USB hay CSI). Nếu chưa có camera, dừng ở bước 3 là đã hoàn thành bài lab.&lt;/li&gt;
  &lt;li&gt;Cập nhật dòng camera trên phiếu nhận diện Bài 04-02 với loại đầu nối dự kiến mua sau hoặc ghi “chưa mua”.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Sổ tay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; có đủ ba con số (18.432.000 byte/s, ~147 Mbit/s, MJPEG ~15 Mbit/s) kèm câu quyết định hoãn camera. Chưa có camera vật lý vẫn hoàn thành đầy đủ bài thực hành này.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Ghi băng thông là 18 triệu bit/s&lt;/td&gt;
      &lt;td&gt;Quên nhân 8 để đổi từ byte sang bit&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cho rằng USB 2.0 (480 Mbit/s) tải được hai luồng thô&lt;/td&gt;
      &lt;td&gt;Lấy trần lý thuyết thay vì thông lượng thực tế ~280–300 Mbit/s&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung hình bị rách khi cắm chung hub với USB-serial&lt;/td&gt;
      &lt;td&gt;Bus USB 2.0 đã quá tải thông lượng thực tế&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cắm camera CSI của Pi với ESP32 nhưng không thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;ESP32 DevKit không có giao diện CSI&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trì hoãn Chương 07 để chờ đơn hàng camera&lt;/td&gt;
      &lt;td&gt;Bỏ qua quyết định hoãn camera của bài này&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Không cần mua camera để hoàn thành Chương 04 hay bắt đầu Chương 07. Nếu dự trù cho Chương 11, một webcam chuẩn UVC phổ thông là lựa chọn ít rủi ro nhất.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Thay thế&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Webcam USB&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera usb robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;80.000–200.000&lt;/td&gt;
      &lt;td&gt;Bất kỳ webcam UVC nào laptop nhận diện trực tiếp.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32-CAM&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32-CAM&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;70.000–140.000&lt;/td&gt;
      &lt;td&gt;Chỉ dùng làm mạch phụ, không thay thế ESP32 DevKit điều khiển động cơ.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=camera%20usb&quot;&gt;Hshop: camera USB&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=camera%20usb%20robot&quot;&gt;Shopee: camera usb robot&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=ESP32-CAM&quot;&gt;Lazada: ESP32-CAM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=ESP32-CAM&quot;&gt;Thế Giới IC: ESP32-CAM&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=wQB1ffECTS0&quot;&gt;How to choose a camera for your Raspberry Pi&lt;/a&gt; — Yahboom Technology, tiếng Anh. So sánh cách chọn camera theo cổng kết nối và băng thông, đối chiếu USB với CSI.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=lfI-mcwCYF4&quot;&gt;Why choose a MIPI CSI-2 interface rather than USB?&lt;/a&gt; — AlliedVisionTV, tiếng Anh. Giải thích sự khác biệt về đường truyền dữ liệu giữa CSI và USB.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tính tốc độ theo byte/s và Mbit/s của luồng ảnh YUYV 640×480 ở 30 fps.&lt;/li&gt;
  &lt;li&gt;Nén luồng trên bằng MJPEG tỉ lệ 10:1 (khoảng 15 Mbit/s). Mức này có chạy thoải mái trên bus USB 2.0 thực tế 300 Mbit/s cùng một đường serial 12 Mbit/s không? Ngược lại, có thể chạy đồng thời hai luồng thô 147 Mbit/s trên cùng bus đó không?&lt;/li&gt;
  &lt;li&gt;Với chế độ nhỏ hơn là 320×240, định dạng RGB (3 byte/điểm ảnh) ở 15 fps, tính băng thông \(B\) theo Mbit/s và so sánh với luồng 147 Mbit/s.&lt;/li&gt;
  &lt;li&gt;Vì sao camera CSI của Raspberry Pi không tiêu tốn băng thông USB (nhưng cũng không dùng được trên ESP32), và vì sao ESP32-CAM vẫn không phù hợp làm bộ điều khiển Chương 07 dù luồng JPEG khá nhỏ?&lt;/li&gt;
  &lt;li&gt;Viết một câu quyết định hoãn camera đạt yêu cầu checklist Chương 04 (có chứa con số 147 Mbit/s).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(18{,}432{,}000~\mathrm{byte/s}\), tương đương \(147{,}5~\mathrm{Mbit/s}\) (xấp xỉ 147 Mbit/s).&lt;/li&gt;
  &lt;li&gt;\(15 + 12 = 27~\mathrm{Mbit/s}\), nằm rất thoải mái trong ngưỡng 300 Mbit/s. Hai luồng thô chiếm tới khoảng 295 Mbit/s (chưa tính đường serial), vượt quá ngân sách thực tế 280–300 Mbit/s.&lt;/li&gt;
  &lt;li&gt;\(320 \times 240 \times 3 \times 15 = 3{,}456{,}000~\mathrm{byte/s} \approx 27{,}6~\mathrm{Mbit/s}\), bằng khoảng một phần năm luồng YUYV 640×480.&lt;/li&gt;
  &lt;li&gt;Camera CSI đi trực tiếp vào bộ xử lý hình ảnh qua cáp ruy-băng chứ không qua bộ điều khiển USB, nhưng ESP32 không có cổng CSI. Với ESP32-CAM, dù dữ liệu JPEG nhỏ, bo mạch lại bị chiếm nhiều chân GPIO và dễ sụt nguồn ổn áp khi gánh thêm động cơ.&lt;/li&gt;
  &lt;li&gt;Bất kỳ câu nào nêu rõ hoãn camera đến Chương 11 kèm số liệu YUYV 640×480 ở 30 fps chiếm khoảng 147 Mbit/s.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/pixfmt-yuyv.html&quot;&gt;Định dạng điểm ảnh YUYV của V4L2&lt;/a&gt; — tài liệu giải thích quy cách 2 byte mỗi điểm ảnh trong công thức.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://index.ros.org/p/usb_cam/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam&lt;/code&gt; trên ROS 2&lt;/a&gt; — gói phần mềm phổ biến đọc webcam UVC trong ROS 2.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/sensor_msgs/msg/Image.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Image&lt;/code&gt;&lt;/a&gt; và &lt;a href=&quot;https://docs.ros.org/en/jazzy/p/sensor_msgs/msg/CompressedImage.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/CompressedImage&lt;/code&gt;&lt;/a&gt; — hai định dạng bản tin tương ứng ảnh thô (147 Mbit/s) và ảnh nén JPEG (~15 Mbit/s).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/accessories/camera.html&quot;&gt;Tài liệu camera Raspberry Pi&lt;/a&gt; — chi tiết giao diện CSI và lý do không dùng trên ESP32.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab: siêu âm và đo khoảng cách ToF</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_05_ultrasonic_tof/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_05_ultrasonic_tof</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;80–100 phút&lt;/strong&gt;. Đây là bài thực hành đo khoảng cách với thước mét, tấm bìa phẳng và bảng đánh giá sai số; không gắn bánh xe trong bài này.&lt;/p&gt;

&lt;p&gt;Dơi bay trong hang tối bằng sóng siêu âm, còn những chiếc robot tự hành hiện đại lại chiếu những xung tia hồng ngoại siêu ngắn rồi bấm đồng hồ đo thời gian ánh sáng dội ngược về — đó là hai gương mặt kinh điển của nguyên lý &lt;strong&gt;Time-of-Flight (ToF)&lt;/strong&gt;: đo khoảng cách bằng cách đo &lt;em&gt;thời gian bay đi và về&lt;/em&gt; của một làn sóng. Dù cùng dùng đúng một công thức \(d = \frac{v \cdot t}{2}\), sự chênh lệch gần một triệu lần giữa tốc độ âm thanh (\(343\text{ m/s}\)) và tốc độ ánh sáng (\(3 \times 10^8\text{ m/s}\)) tạo ra hai thế giới cảm biến hoàn toàn khác nhau về độ rộng chùm tia, tần số lấy mẫu và dạng lỗi vật lý.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-vật-lý-truyền-sóng-trong-đo-khoảng-cách-robot&quot;&gt;Kiến thức cần nhớ: Vật lý truyền sóng trong đo khoảng cách robot&lt;/h2&gt;

&lt;h3 id=&quot;1-siêu-âm-hc-sr04--nón-sóng-rộng-phản-xạ-gương-và-ảnh-hưởng-nhiệt-độ&quot;&gt;1. Siêu âm (HC-SR04) — nón sóng rộng, phản xạ gương và ảnh hưởng nhiệt độ&lt;/h3&gt;
&lt;p&gt;Cảm biến siêu âm phát chùm sóng \(40\text{ kHz}\) mở rộng theo hình nón góc khoảng \(15^\circ\text{–}30^\circ\). Ưu điểm là rẻ và phát hiện tốt cả kính trong suốt; nhưng nhược điểm chí mạng là &lt;strong&gt;phản xạ gương (specular reflection)&lt;/strong&gt;: nếu sóng đập xiên góc vào tường phẳng nhẵn, sóng nảy đi nơi khác không quay về đầu thu, khiến robot tưởng phía trước trống không! Ngoài ra, tốc độ âm thanh phụ thuộc nhiệt độ không khí theo \(v(T) \approx 331{,}3 + 0{,}606\,T\;(^\circ\text{C})\).&lt;/p&gt;

&lt;h3 id=&quot;2-quang-học-tof-vl53l0x--vl53l1x--tia-laser-hẹp-và-bộ-đếm-nano-giây&quot;&gt;2. Quang học ToF (VL53L0X / VL53L1X) — tia laser hẹp và bộ đếm nano-giây&lt;/h3&gt;
&lt;p&gt;Cảm biến ToF hồng ngoại dùng diode phát VCSEL (940 nm) chiếu một chùm sáng hẹp kết hợp mảng diode đơn photon (SPAD) đo thời gian bay ở mức &lt;strong&gt;pico-giây đến nano-giây&lt;/strong&gt;. Nhờ tia hẹp, nó đo cực sắc nét qua khe cửa hẹp và không bị nhiễu chéo (crosstalk) giữa các cảm biến cạnh nhau như siêu âm, nhưng lại bị suy yếu dưới ánh nắng mặt trời gắt hoặc bề mặt đen hấp thụ hồng ngoại.&lt;/p&gt;

&lt;h3 id=&quot;3-chu-kỳ-đo-tối-đa-và-lọc-nhiễu-ngoại-lai-median-filter&quot;&gt;3. Chu kỳ đo tối đa và lọc nhiễu ngoại lai (Median Filter)&lt;/h3&gt;
&lt;p&gt;Với siêu âm, phải chờ tiếng vang dội xong (thường tối thiểu \(30\text{–}60\text{ ms}\) mỗi lần bắn) để tránh thu nhầm tiếng vang của xung trước đó (&lt;em&gt;ghost echo&lt;/em&gt;). Trong cả hai loại cảm biến, một &lt;strong&gt;bộ lọc trung vị (median filter)&lt;/strong&gt; 3–5 mẫu luôn đánh bại trung bình cộng vì nó vứt sạch các cú đo lỗi đột ngột về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_range&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-khoảng-cách-siêu-âm-vs-quang-học-tof-và-sai-số-nhiệt-độ&quot;&gt;Giải thích: Cầm tay tính khoảng cách Siêu âm vs. Quang học ToF và sai số nhiệt độ&lt;/h3&gt;

&lt;p&gt;Xét một vật cản đặt cách robot đúng &lt;strong&gt;\(d = 20{,}0\text{ cm} = 0{,}200\text{ m}\)&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Cảm biến siêu âm HC-SR04 ở nhiệt độ phòng \(20^\circ\text{C}\) (\(v = 343\text{ m/s}\)):&lt;/strong&gt;
Thời gian xung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; đo được (đi và về tổng \(0{,}400\text{ m}\)):
\(t_{\text{echo}} = \frac{2d}{v} = \frac{0{,}400}{343} \approx 1{,}166 \times 10^{-3}\text{ s} = 1\,166\,\mu\text{s}.\)
Kiểm tra ngược: \(d = \frac{343 \times 1\,166 \times 10^{-6}}{2} \approx 0{,}200\text{ m}\).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Điều gì xảy ra nếu robot chạy ngoài sân nắng nóng \(40^\circ\text{C}\) nhưng code vẫn gán cứng \(v = 343\text{ m/s}\)?&lt;/strong&gt;
Ở \(40^\circ\text{C}\), tốc độ âm thanh thực là \(v(40) \approx 331{,}3 + 0{,}606 \times 40 = 355{,}5\text{ m/s}\). Sóng bay nhanh hơn nên xung về sớm hơn:
\(t_{\text{thực}} = \frac{0{,}400}{355{,}5} \approx 1\,125\,\mu\text{s}.\)
Code gán cứng \(343\text{ m/s}\) sẽ tính nhầm khoảng cách thành \(\frac{343 \times 1\,125 \times 10^{-6}}{2} \approx 19{,}3\text{ cm}\) (lệch \(-3{,}5\%\)!).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;So sánh với cảm biến laser ToF (VL53L0X) ở cùng khoảng cách \(20{,}0\text{ cm}\) (\(c = 3 \times 10^8\text{ m/s}\)):&lt;/strong&gt;
\(t_{\text{ánh sáng}} = \frac{0{,}400}{3 \times 10^8} \approx 1{,}33 \times 10^{-9}\text{ s} = 1{,}33\text{ nano-giây!}\)
Con số \(1{,}33\text{ ns}\) giải thích vì sao bạn không bao giờ đo ToF quang học bằng hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pulseIn()&lt;/code&gt; trên vi điều khiển mà phải dùng chip chuyên dụng có bộ đếm thời gian lượng tử bên trong!&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn tính được khoảng cách từ độ rộng xung Echo theo công thức \(d = vt/2\) với vận tốc âm \(343~\mathrm{m/s}\), cũng như công thức rút gọn theo micro giây \(d_{\mathrm{cm}} \approx t_{\mu\mathrm{s}} \times 0{,}01715\). Bạn biết hạ mức Echo 5 V của HC-SR04 xuống khoảng 3,33 V trước khi đưa vào ESP32 và hiểu rõ việc mất tín hiệu Echo (timeout) nghĩa là không xác định được khoảng cách chứ không phải 0 cm. Bạn giải thích được vì sao hai module VL53L0X mặc định (cùng địa chỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;) không thể hoạt động song song nếu chưa đổi địa chỉ, đồng thời hoàn thành bảng sai số tại ba mốc 20 / 40 / 80 cm so với thước đo (hoặc bản chuẩn bị dry-run kèm phép tính 5,8 ms và cầu chia áp).&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã nhận diện được HC-SR04 và VL53L0X ở Bài 04-02, biết quét địa chỉ I2C từ Bài 04-01 (nếu có VL53L0X), biết đo độ rộng xung hoặc đọc thư viện từ Chương 03 và có đồng hồ vạn năng để kiểm tra điện áp chân Echo. Chưa cần động cơ hay mạch cầu H.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Firmware teleop ở Chương 07 chỉ an toàn khi có điều kiện tự dừng: trong cùng vòng lặp điều khiển động cơ, nếu khoảng cách quá gần hoặc không đọc được tín hiệu hợp lệ, hệ thống phải đưa PWM về 0. Ngưỡng dừng đó được kiểm chứng trực tiếp bằng thước đo trong bài này.&lt;/p&gt;

&lt;p&gt;Ở các chương ROS 2 sau này, kết quả đo sẽ được xuất thành bản tin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Range&lt;/code&gt; với loại bức xạ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;radiation_type&lt;/code&gt;: siêu âm hoặc hồng ngoại), giới hạn tin cậy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;min_range&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_range&lt;/code&gt; và giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;range&lt;/code&gt; tính bằng mét. Không được để giá trị 0 của trạng thái timeout bị hiểu nhầm thành “khoảng cách 0 cm”; cần quy ước ký hiệu lỗi rõ ràng ngay từ bây giờ. Nếu dùng hai cảm biến VL53L0X, phải dùng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt; tách địa chỉ trước khi xuất thành hai topic riêng biệt.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/range_echo_tof.png&quot; alt=&quot;Thời gian echo đi và về, và số ToF đã là milimét ngay trong chip&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;thời-gian-bay-của-sóng-âm&quot;&gt;Thời gian bay của sóng âm&lt;/h2&gt;

&lt;p&gt;Chùm sóng siêu âm của HC-SR04 đi từ đầu phát đến vật cản rồi phản xạ ngược về đầu thu, nên khoảng cách bằng một nửa tổng quãng đường truyền:&lt;/p&gt;

\[d = \frac{v t}{2}.\]

&lt;p&gt;Ở nhiệt độ phòng ($20\,^\circ\mathrm{C}$), lấy \(v \approx 343~\mathrm{m/s}\). Nếu xung Echo ở mức cao trong \(5{,}8~\mathrm{ms} = 0{,}0058~\mathrm{s}\), khoảng cách là:&lt;/p&gt;

\[d = \frac{343 \times 0{,}0058}{2} = 343 \times 0{,}0029 = 0{,}995~\mathrm{m} \approx 99{,}5~\mathrm{cm}.\]

&lt;p&gt;Quy đổi sang đơn vị centimét và micro giây (chia \(343 / 2\) cho \(10^{4}\) hay chia mét cho \(10^{6}\) rồi đổi ra cm):&lt;/p&gt;

\[d_{\mathrm{cm}} \approx t_{\mu\mathrm{s}} \times 0{,}01715.\]

&lt;p&gt;Kiểm tra lại với cùng xung trên: \(5800 \times 0{,}01715 = 99{,}5~\mathrm{cm}\). Mỗi 1 cm tương ứng khoảng \(58~\mu\mathrm{s}\) độ rộng xung; do đó sai lệch định thời (jitter) cỡ \(100~\mu\mathrm{s}\) đã làm kết quả lệch khoảng \(1{,}7~\mathrm{cm}\).&lt;/p&gt;

&lt;p&gt;Vận tốc âm trong không khí tăng khoảng \(0{,}6~\mathrm{m/s}\) cho mỗi độ C. Ở $30\,^\circ\mathrm{C}$, \(v \approx 349~\mathrm{m/s}\), nên nếu firmware vẫn cố định hệ số 0,01715 thì số đo sẽ ngắn hơn thực tế một chút. Trong bài lab này cứ dùng 343 m/s và ghi chú thêm nhiệt độ phòng cạnh bảng nếu biết.&lt;/p&gt;

&lt;h2 id=&quot;hc-sr04-xung-kích-góc-mở-và-hạ-áp-echo&quot;&gt;HC-SR04: xung kích, góc mở và hạ áp Echo&lt;/h2&gt;

&lt;p&gt;Module cấp nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC = 5 V&lt;/code&gt;. Khi tạo xung kích mức cao khoảng \(10~\mu\mathrm{s}\) ở chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRIG&lt;/code&gt;, module phát chùm sóng 40 kHz và giữ chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; ở mức cao cho tới khi nhận sóng phản hồi. Trên bản phổ biến, mức cao của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; là 5 V, cần hạ áp qua cầu chia \(1~\mathrm{k}\Omega\) (từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; tới GPIO) và \(2~\mathrm{k}\Omega\) (từ GPIO xuống mass):&lt;/p&gt;

\[V_{\mathrm{pin}} = 5 \times \frac{2}{3} \approx 3{,}33~\mathrm{V}.\]

&lt;p&gt;Chùm sóng âm có góc mở khoảng $15^\circ$: tại khoảng cách 80 cm, bán kính vùng quét đã đạt \(80 \tan 7{,}5^\circ \approx 10~\mathrm{cm}\), nên các vật gần bên như chân ghế hay khung cửa có thể phản xạ trước tấm bìa mục tiêu. Bề mặt vải mềm hấp thụ sóng 40 kHz khiến không có tín hiệu phản hồi, còn góc tường có thể gây phản xạ nhiều lần. Khoảng mù tối thiểu khoảng 2 cm. Dù thông số quảng cáo ghi tới 4 m (trong điều kiện tường cứng, phòng yên tĩnh và nguồn 5 V ổn định), trên robot thực tế chỉ nên tin cậy vùng đo dưới khoảng 1 m.&lt;/p&gt;

&lt;p&gt;Khi quá thời gian chờ (timeout) mà không nhận được xung phản hồi, nhiều thư viện trả về giá trị 0. Giá trị 0 này mang nghĩa “không xác định được khoảng cách” — logic an toàn phải coi đó là trạng thái dừng động cơ chứ không gán bừa thành 400 cm.&lt;/p&gt;

&lt;h2 id=&quot;vl53l0x-đo-trực-tiếp-theo-milimét-qua-i2c&quot;&gt;VL53L0X: đo trực tiếp theo milimét qua I2C&lt;/h2&gt;

&lt;p&gt;VL53L0X đo thời gian bay của tia laser hồng ngoại 940 nm ngay bên trong cảm biến và trả kết quả theo milimét qua bus I2C tại địa chỉ mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. Góc nhìn khoảng $25^\circ$; trên bề mặt trắng có thể đo được khoảng 1,2–2 m, nhưng hiệu năng giảm rõ trên vật liệu màu đen, mặt kính hoặc dưới ánh nắng mạnh (vì ánh sáng mặt trời chứa bức xạ hồng ngoại gây nhiễu).&lt;/p&gt;

&lt;p&gt;Hai module VL53L0X mặc định đều dùng địa chỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt; nên sẽ xung đột nếu nối song song trực tiếp. Cần dành riêng một chân GPIO nối vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt; để giữ một cảm biến ở trạng thái reset trong lúc đổi địa chỉ I2C cho cảm biến còn lại, sau đó mới bật lại cảm biến thứ nhất.&lt;/p&gt;

&lt;h2 id=&quot;lab-đo-kiểm-tại-ba-mốc-khoảng-cách&quot;&gt;Lab: đo kiểm tại ba mốc khoảng cách&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Chỉ dùng tấm bìa cứng làm vật cản, không hướng vào người hay cửa kính. Dù VL53L0X dùng laser 940 nm công suất thấp, vẫn tuân thủ nguyên tắc an toàn: không nhìn trực tiếp vào lỗ phát và không đặt ống kính hay kính lúp chắn sát phía trước. Cấp nguồn 5 V đúng chuẩn cho HC-SR04, tuyệt đối không để 5 V chạm vào đường 3,3 V của ESP32. Không cấp nguồn cho động cơ trong bài thực hành này.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td&gt;Cảm biến siêu âm đo thời gian Echo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điện trở 1 kΩ và 2 kΩ&lt;/td&gt;
      &lt;td&gt;Cầu chia áp bảo vệ chân GPIO&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 hoặc Pico&lt;/td&gt;
      &lt;td&gt;Xuất &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRIG&lt;/code&gt;, đọc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; qua cầu chia&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thước mét và tấm bìa cứng cỡ A4 trở lên&lt;/td&gt;
      &lt;td&gt;Mốc chuẩn và bia phản xạ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Module VL53L0X&lt;/td&gt;
      &lt;td&gt;Cột đo đối chiếu (nếu có)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Bảng ghi sai số&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Lắp hoàn chỉnh cầu chia áp trước khi nối chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; vào GPIO; kiểm tra chắc chắn điện trở 2 kΩ nối đúng về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Xuất xung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRIG&lt;/code&gt; 10 µs và in đồng thời cả độ rộng xung theo micro giây lẫn khoảng cách tính bằng hệ số 0,01715 để dễ phát hiện lỗi quy đổi.&lt;/li&gt;
  &lt;li&gt;Cố định thước mét trên bàn, lần lượt đặt tấm bìa vuông góc tại 20 cm, 40 cm và 80 cm (đo tính từ mặt đầu thu phát siêu âm). Tại mỗi mốc, ghi 5 lần đọc liên tiếp và lấy giá trị trung vị.&lt;/li&gt;
  &lt;li&gt;Tính cột sai số có dấu (số cảm biến trừ số thước). Ghi số cụ thể, không mô tả chung chung.&lt;/li&gt;
  &lt;li&gt;Nếu có VL53L0X, xác nhận địa chỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt; và bổ sung cột đo ToF tại cùng ba mốc. Nếu linh kiện chưa giao tới, thực hiện bản dry-run gồm bảng để trống kèm hai phép tính tay: Echo 5,8 ms (99,5 cm) và cầu chia áp (3,33 V).&lt;/li&gt;
  &lt;li&gt;Đặt thêm một tấm vải gấp ở mốc 40 cm và ghi nhận kết quả siêu âm (có thể ghi “không có echo” nếu bị hấp thụ).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Điền đầy đủ bảng theo mẫu sau vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Thước (cm)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Siêu âm (cm)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Sai số (cm)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;ToF (cm), nếu có&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;20&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;40&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;80&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Trên tấm bìa phẳng, HC-SR04 hoạt động tốt thường sai lệch vài centimét ở mốc 40 cm và tăng nhẹ ở mốc 80 cm. Các trường hợp timeout trả về 0 phải ghi rõ là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;timeout&lt;/code&gt;, không ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,0 cm&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Chân GPIO nóng, số đọc treo cứng&lt;/td&gt;
      &lt;td&gt;Đưa thẳng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; 5 V không qua cầu chia áp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mốc 20 cm đọc ra khoảng 40 cm&lt;/td&gt;
      &lt;td&gt;Quên chia đôi thời gian đi và về&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Số đo nhảy bất thường gần đồ vật&lt;/td&gt;
      &lt;td&gt;Chùm sóng $15^\circ$ bắt phải vật cản gần hơn ở bên cạnh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đo vải không ra số hoặc sai gấp đôi&lt;/td&gt;
      &lt;td&gt;Sóng bị hấp thụ hoặc phản xạ đa đường yếu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hai cảm biến VL53L0X chỉ hiện một địa chỉ&lt;/td&gt;
      &lt;td&gt;Trùng địa chỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;, chưa dùng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Luôn báo 0 khi đo trong phòng&lt;/td&gt;
      &lt;td&gt;Trạng thái timeout, không phải vật cản chạm sát&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Cảm biến siêu âm HC-SR04 là lựa chọn tối thiểu cần có trước Capstone A; VL53L0X là lựa chọn bổ sung hữu ích nếu ngân sách cho phép.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Thay thế&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cảm biến siêu âm HC-SR04&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-sieu-am-srf04&quot;&gt;Hshop HC-SR04&lt;/a&gt; từng niêm yết khoảng 20.000. Chỉ thay bằng US-100 khi đọc rõ chế độ GPIO/UART.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;VL53L0X&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VL53L0X ToF&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-tof-laser-radar-vl53l0x&quot;&gt;Hshop VL53L0X&lt;/a&gt;. Lưu ý VL53L1X dùng thư viện khác.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1 kΩ, 2 kΩ&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;điện trở 1k 2k&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;10.000–20.000 một vỉ&lt;/td&gt;
      &lt;td&gt;Bắt buộc dùng cho đường Echo 5 V.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=HC-SR04&quot;&gt;Hshop: HC-SR04&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=VL53L0X&quot;&gt;Shopee: VL53L0X&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=cam%20bien%20sieu%20am%20HC-SR04&quot;&gt;Lazada: cảm biến siêu âm&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=HC-SR04&quot;&gt;Thế Giới IC: HC-SR04&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=6F1B_N6LuKw&quot;&gt;Using the HC-SR04 Ultrasonic Distance Sensor with Arduino - Everything you need to know!&lt;/a&gt; — DroneBot Workshop, tiếng Anh. Giải thích chi tiết cách đổi thời gian Echo sang khoảng cách (tập trung vào phần tính toán thời gian bay của âm thanh).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=SWBI6_rxmT8&quot;&gt;VL53l0x: Time-of-Flight Distance Sensor (ESP32 + Arduino series)&lt;/a&gt; — Simply Explained, tiếng Anh. Minh họa cảm biến VL53L0X đo thời gian bay của ánh sáng và đọc kết quả trực tiếp qua I2C.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; giữ mức cao trong 5,8 ms. Tính khoảng cách \(d\) theo mét và centimét với \(v = 343~\mathrm{m/s}\).&lt;/li&gt;
  &lt;li&gt;Tại mốc 40 cm, cảm biến trả về xung Echo dài \(2320~\mu\mathrm{s}\). Công thức \(t \times 0{,}01715\) cho kết quả bao nhiêu centimét?&lt;/li&gt;
  &lt;li&gt;Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; ở mức cao 5 V nối về GPIO của ESP32. Khi không có cầu chia áp thì chân chịu bao nhiêu vôn, và khi mắc cầu chia 1 kΩ (phía trên) / 2 kΩ (xuống mass) thì điện áp tại chân là bao nhiêu?&lt;/li&gt;
  &lt;li&gt;Hai cảm biến VL53L0X nối song song mà không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt;. Vì sao quét I2C không thấy đủ hai cảm biến và xử lý bằng chân nào?&lt;/li&gt;
  &lt;li&gt;Ở nhiệt độ phòng $30\,^\circ\mathrm{C}$, vận tốc âm \(v \approx 349~\mathrm{m/s}\). Với khoảng cách thật 80 cm, xung Echo kéo dài bao lâu, và firmware dùng hằng số cố định 0,01715 cm/µs sẽ báo kết quả bao nhiêu?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(d = 0{,}995~\mathrm{m} \approx 99{,}5~\mathrm{cm}\) (kiểm tra lại: \(5800 \times 0{,}01715 = 99{,}5~\mathrm{cm}\)).&lt;/li&gt;
  &lt;li&gt;\(2320 \times 0{,}01715 \approx 39{,}8~\mathrm{cm}\).&lt;/li&gt;
  &lt;li&gt;Không cầu chia: 5 V; có cầu chia 1 kΩ / 2 kΩ: \(5 \times 2/3 \approx 3{,}33~\mathrm{V}\).&lt;/li&gt;
  &lt;li&gt;Cả hai đều có địa chỉ mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt; nên trả lời đè lên nhau; dùng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt; để giữ một chip ở trạng thái reset, đổi địa chỉ chip còn lại rồi bật lại chip đầu.&lt;/li&gt;
  &lt;li&gt;\(t = 2 \times 0{,}80 / 349 \approx 4{,}58~\mathrm{ms} = 4585~\mu\mathrm{s}\); nhân với 0,01715 cho \(78{,}6~\mathrm{cm}\), ngắn hơn thực tế khoảng 1,4 cm.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;thực-hành-thêm-roverkit--lắp-xe-và-mô-phỏng-tránh-vật-cản&quot;&gt;Thực hành thêm: RoverKit — lắp xe và mô phỏng tránh vật cản&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://roverkit.vercel.app/#parts&quot;&gt;RoverKit&lt;/a&gt; (Dilum Sanjaya) là ứng dụng chạy trên trình duyệt: hướng dẫn lắp một xe robot 4 bánh qua mười bước dựng 3D, rồi cho chạy thử trong sân mô phỏng 3×3 m. Giao diện có tiếng Việt. Đây là bài tập bổ sung, không thay bộ kit Capstone A.&lt;/p&gt;

&lt;h3 id=&quot;linh-kiện-trên-xe-đối-chiếu-với-khóa-học&quot;&gt;Linh kiện trên xe, đối chiếu với khóa học&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Bộ não:&lt;/strong&gt; Arduino UNO R3 hoặc ESP32-WROOM-32 kèm board mở rộng. Bộ não mặc định của khóa học là ESP32 (bài 02-01). Trên bàn lab, mạch 5 V cầm tay là Arduino Mega 2560; mã viết cho UNO gần như chạy được vì cùng API Arduino, chỉ khác số chân và bộ nhớ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Driver:&lt;/strong&gt; L9110S. Khóa học ưu tiên TB6612 (Chương 05): L9110S yếu hơn, dễ nóng và sụt áp khi bốn động cơ TT cùng kéo.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Động cơ và bánh:&lt;/strong&gt; bốn động cơ giảm tốc TT, bánh 65 mm, điều khiển theo cặp trái/phải (skid-steer). Capstone A dùng hai bánh chủ động và một bánh caster, không phải bốn bánh dẫn động.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cảm biến:&lt;/strong&gt; HC-SR04 gắn trên servo SG90 để quay sang phải rồi sang trái. Đúng bài này: module nguồn 5 V, và với ESP32 bắt buộc có cầu chia áp Echo \(1~\mathrm{k}\Omega\) / \(2~\mathrm{k}\Omega\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nguồn và khung:&lt;/strong&gt; hai pin 18650, LED với điện trở 220 Ω, còi 5 V qua transistor NPN, ba tầng đế với trụ đồng M3 (kiến trúc phân tầng ở bài 06-01).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;chế-độ-auto-hoạt-động-như-thế-nào&quot;&gt;Chế độ Auto hoạt động như thế nào&lt;/h3&gt;

&lt;p&gt;Trong sân mô phỏng, lái thủ công bằng WASD, rồi bật Auto. Xe đi thẳng và đo khoảng cách phía trước. Khi vật cản gần hơn khoảng 22 cm, hoặc khi số đo gần như không đổi trong khoảng 1,5 s (xe bị kẹt), xe dừng, bật LED và kêu một tiếng. Servo quay sang phải (khoảng 20°), đo, rồi quay sang trái (khoảng 160°), đo, rồi về giữa. Firmware so hai số đo và rẽ về phía có nhiều chỗ hơn. Nếu cả hai phía đều chật (dưới khoảng 18 cm), xe lùi rồi quét lại. Đây là vòng cảm nhận–quyết định–hành động hở (open-loop): không có encoder hay IMU, nên xe không biết mình đã quay đủ góc hay đã trượt bánh.&lt;/p&gt;

&lt;p&gt;Trên trang có sơ đồ luồng chương trình; bấm từng ô để xem đoạn mã tương ứng.&lt;/p&gt;

&lt;h3 id=&quot;cách-dùng-kèm-bài-này&quot;&gt;Cách dùng kèm bài này&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Mở &lt;a href=&quot;https://roverkit.vercel.app/#parts&quot;&gt;RoverKit&lt;/a&gt;, chọn tiếng Việt nếu muốn.&lt;/li&gt;
  &lt;li&gt;Đi hết mười bước lắp 3D để xem HC-SR04, servo, cầu chia áp và driver nằm ở đâu.&lt;/li&gt;
  &lt;li&gt;Vào phần mô phỏng, đặt vài vật cản, lái bằng tay rồi bật Auto. Đối chiếu ngưỡng dừng khoảng 22 cm với số đo bạn vừa làm trên bàn lab.&lt;/li&gt;
  &lt;li&gt;Chú ý góc mở của chùm siêu âm: tia trong mô phỏng cũng có bề rộng, nên một vật lệch bên có thể bị “thấy” sớm — đúng như góc mở khoảng \(15^\circ\) đã nói ở trên.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Firmware cho xe thật bản ESP32 điều khiển qua Wi-Fi WebSocket (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ws://192.168.4.1:81/&lt;/code&gt;). Trang HTTPS công khai chặn kết nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ws://&lt;/code&gt;, nên muốn lái xe thật phải chạy app trên máy (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm run dev&lt;/code&gt;). Tại thời điểm 26/11/2026, dự án chưa công bố repo công khai.&lt;/p&gt;

&lt;h3 id=&quot;giới-hạn-cần-nhớ&quot;&gt;Giới hạn cần nhớ&lt;/h3&gt;

&lt;p&gt;RoverKit là công cụ nhập môn, không phải mô hình điều khiển của Capstone A. L9110S yếu hơn driver khóa học dùng. Bốn bánh skid-steer không có encoder nên không làm được odometry vòng kín ở bài 04-03 và 05-06. Cũng không có IMU. Dùng để hiểu mạch, thứ tự lắp và logic tránh vật cản; khi chuyển sang xe thật của khóa học, hãy đo encoder, kiểm tra chiều bánh và giữ mạch nguồn như Chương 07.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf&quot;&gt;Datasheet HC-SR04 (bản SparkFun)&lt;/a&gt; — thông số xung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRIG&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; và nguồn 5 V.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/vl53l0x.pdf&quot;&gt;Datasheet ST VL53L0X&lt;/a&gt; — chi tiết phép đo milimét, góc nhìn và địa chỉ I2C.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/2490&quot;&gt;Mạch Pololu VL53L0X&lt;/a&gt; — tài liệu về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt; và chuyển mức logic.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/sensor_msgs/msg/Range.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Range&lt;/code&gt;&lt;/a&gt; — định dạng bản tin ROS 2 cho dữ liệu khoảng cách ở Chương 09.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>IMU: gia tốc, gyro, fusion</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_04_imu_basics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_04_imu_basics</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;75–90 phút&lt;/strong&gt;, gồm một lần đo khi để yên và một lần nghiêng $90^\circ$. Bài này không dùng đến động cơ.&lt;/p&gt;

&lt;p&gt;Trong tai trong của con người có một cơ quan kỳ diệu gọi là hệ tiền đình, giúp bạn biết đầu mình đang nghiêng hay xoay ngay cả khi nhắm chặt mắt. Trên robot, cơ quan tương đương chính là &lt;strong&gt;IMU (Inertial Measurement Unit)&lt;/strong&gt; — một con chip MEMS nhỏ bằng hạt gạo chứa gia tốc kế (accelerometer) đo vector trọng lực cộng gia tốc tịnh tiến, và con quay hồi chuyển (gyroscope) đo vận tốc góc. Bí mật thú vị nhất của IMU nằm ở chỗ: &lt;em&gt;cả hai cảm biến bên trong nó đều có khuyết điểm chí mạng khi đứng một mình, nhưng khi ghép lại bằng hợp nhất cảm biến (sensor fusion), khuyết điểm của cái này lại được cái kia triệt tiêu gọn gàng&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-vật-lý-quán-tính-mems-và-nghệ-thuật-hợp-nhất-cảm-biến&quot;&gt;Kiến thức cần nhớ: Vật lý quán tính MEMS và nghệ thuật hợp nhất cảm biến&lt;/h2&gt;

&lt;h3 id=&quot;1-gia-tốc-kế-accelerometer--đúng-về-dài-hạn-nhưng-ồn-ở-ngắn-hạn&quot;&gt;1. Gia tốc kế (Accelerometer) — đúng về dài hạn nhưng ồn ở ngắn hạn&lt;/h3&gt;
&lt;p&gt;Khi robot đứng yên, gia tốc kế đo chính xác vector trọng lực \(\mathbf{g} \approx 9{,}81\text{ m/s}^2\), cho phép suy ra góc nghiêng (roll, pitch) tuyệt đối bằng hàm \(\mathrm{atan2}\) mà &lt;strong&gt;không bao giờ trôi theo thời gian&lt;/strong&gt;. Tuy nhiên, ngay khi động cơ rung hoặc robot tăng tốc đột ngột, gia tốc cơ học cộng lẫn vào \(\mathbf{g}\) khiến góc tính từ gia tốc kế nhảy loạn xạ ở tần số cao.&lt;/p&gt;

&lt;h3 id=&quot;2-con-quay-hồi-chuyển-gyroscope--mượt-ở-ngắn-hạn-nhưng-trôi-ở-dài-hạn&quot;&gt;2. Con quay hồi chuyển (Gyroscope) — mượt ở ngắn hạn nhưng trôi ở dài hạn&lt;/h3&gt;
&lt;p&gt;Gyroscope đo vận tốc góc \(\omega\) (rad/s hoặc độ/s) cực mượt và miễn nhiễm với gia tốc tịnh tiến. Nhưng muốn có góc \(\theta\), ta phải &lt;strong&gt;tích phân theo thời gian&lt;/strong&gt;: \(\theta(t) = \theta_0 + \int \omega(\tau)\,d\tau\). Mọi chip MEMS đều có một độ lệch tĩnh nhỏ (&lt;strong&gt;bias&lt;/strong&gt; \(b\)); tích phân hằng số \(b\) theo thời gian tạo ra sai số góc tăng tuyến tính \(b \cdot t\) khiến hướng robot trôi dần không phanh!&lt;/p&gt;

&lt;h3 id=&quot;3-bộ-lọc-bù-complementary-filter-và-cầu-nối-tới-kalmanmadgwick&quot;&gt;3. Bộ lọc bù (Complementary Filter) và cầu nối tới Kalman/Madgwick&lt;/h3&gt;
&lt;p&gt;Vì gia tốc kế đáng tin ở &lt;strong&gt;tần số thấp&lt;/strong&gt; (dài hạn) còn gyroscope đáng tin ở &lt;strong&gt;tần số cao&lt;/strong&gt; (ngắn hạn), &lt;strong&gt;bộ lọc bù&lt;/strong&gt; chỉ cần một phép tổ hợp tuyến tính nhẹ hều: lấy \(98\%\) góc tích phân từ gyro cộng \(2\%\) góc từ gia tốc kế ở mỗi chu kỳ. Các thuật toán cao cấp hơn như Madgwick, Mahony hay EKF thực chất mở rộng đúng ý tưởng này sang không gian 3D bằng quaternion.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-sai-số-trôi-của-gyro-và-chạy-1-bước-bộ-lọc-bù&quot;&gt;Giải thích: Cầm tay tính sai số trôi của Gyro và chạy 1 bước Bộ lọc bù&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Sức tàn phá của bias gyro khi đứng một mình:&lt;/strong&gt;
Giả sử một chip IMU giá rẻ chưa hiệu chuẩn kỹ còn dư bias nhỏ \(b = 0{,}5^\circ/\text{s}\) trên trục yaw. Sau đúng &lt;strong&gt;1 phút (\(60\text{ s}\))&lt;/strong&gt; robot đứng yên hoàn toàn, góc tích phân từ gyro đã tự trôi đi:
\(\Delta\theta_{\text{trôi}} = 0{,}5^\circ/\text{s} \times 60\text{ s} = 30^\circ!\)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Chạy tay 1 bước Bộ lọc bù (\(\alpha = 0{,}98\), chu kỳ \(\Delta t = 0{,}01\text{ s}\)):&lt;/strong&gt;
Tại bước \(k-1\), góc nghiêng ước lượng của robot là \(\theta_{k-1} = 10{,}00^\circ\). Ở bước \(k\):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Gyro đo vận tốc góc \(\omega_k = 50{,}0^\circ/\text{s}\).&lt;/li&gt;
  &lt;li&gt;Gia tốc kế (đang bị rung động cơ nhẹ) báo góc tức thời \(\theta_{\text{acc}, k} = 12{,}00^\circ\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tính từng bước theo công thức \(\theta_k = \alpha(\theta_{k-1} + \omega_k \Delta t) + (1 - \alpha)\,\theta_{\text{acc}, k}\):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Góc dự đoán từ gyro:
\(\theta_{\text{gyro}, k} = 10{,}00^\circ + 50{,}0^\circ/\text{s} \times 0{,}01\text{ s} = 10{,}50^\circ.\)&lt;/li&gt;
  &lt;li&gt;Trộn bằng bộ lọc bù:
\(\theta_k = 0{,}98 \times 10{,}50^\circ + 0{,}02 \times 12{,}00^\circ = 10{,}29^\circ + 0{,}24^\circ = 10{,}53^\circ.\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Kết quả \(10{,}53^\circ\) vừa chặn đứng cú giật nhiễu \(12{,}00^\circ\) của gia tốc kế (chỉ kéo nhẹ \(0{,}03^\circ\)), vừa âm thầm kéo góc trở về phương trọng lực thực trong dài hạn để triệt tiêu trôi gyro!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn hiểu rõ gia tốc kế đo lực riêng (specific force) chứ không đo trực tiếp góc nghiêng, và biết kiểm tra độ lớn gia tốc khi bo mạch nằm yên đạt xấp xỉ 1 g. Bạn nắm được con quay hồi chuyển (gyro) đo vận tốc góc (độ/giây) và tính được độ trôi góc tích lũy sau một phút khi có sai lệch tĩnh (bias). Bạn thực hiện được một bước tính tay của bộ lọc bổ trợ (complementary filter) và biết quy đổi số nguyên thô của MPU-6050 sang đơn vị vật lý theo hệ số trong datasheet, đồng thời luôn ghi nhận dấu của từng trục khi nằm yên trước khi áp dụng công thức tính góc.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã biết quét địa chỉ I2C từ Bài 04-01 (thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt; trước khi gọi thư viện), nhận diện được GY-521 ở Bài 04-02 và biết in dữ liệu theo chu kỳ từ Chương 03. Bài có sử dụng hàm lượng giác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atan2&lt;/code&gt;; chưa cần ROS.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Capstone A ở Chương 07 có thể chạy teleop hoàn chỉnh mà chưa cần IMU, vì yêu cầu cốt lõi ở đó là nhận lệnh và dừng an toàn khi quá thời gian hoặc gặp vật cản. Việc lắp thêm IMU không thay thế cơ chế timeout 300 ms.&lt;/p&gt;

&lt;p&gt;Khi chuyển sang ROS 2, dữ liệu cảm biến quán tính được xuất dưới dạng bản tin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Imu&lt;/code&gt; gồm vận tốc góc (rad/s), gia tốc tuyến tính (m/s²) và quaternion hướng (nên đánh dấu chưa có hướng bằng giá trị hiệp phương sai $-1$ trên đường chéo cho tới khi bộ lọc hướng đủ tin cậy). Bộ lọc bổ trợ trong bài chỉ nhằm ước lượng góc ngẩng (pitch) cơ bản để ghi sổ; các bộ lọc phức tạp hơn như Madgwick hay Kalman sẽ học sau khi đã chuẩn hóa trục, khử sai lệch tĩnh và quy đổi đúng đơn vị vật lý thay vì xuất nhầm giá trị LSB thô lên đồ thị.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/imu_axes.png&quot; alt=&quot;Trục IMU: lực riêng khi để yên, và tốc độ góc quanh từng trục&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;lực-riêng-không-phải-là-góc-nghiêng&quot;&gt;Lực riêng không phải là góc nghiêng&lt;/h2&gt;

&lt;p&gt;Gia tốc kế đo lực riêng — tức gia tốc tương đối so với trạng thái rơi tự do. Khi đặt yên trên bàn, bo mạch không chuyển động nhưng phản lực của mặt bàn đẩy nó hướng lên, nên trục hướng lên trời sẽ đọc xấp xỉ $+1~\mathrm{g}$. Phép kiểm tra độ lớn tổng hợp:&lt;/p&gt;

\[\sqrt{a_x^2 + a_y^2 + a_z^2} \approx 1~\mathrm{g}\]

&lt;p&gt;là bước kiểm chứng nhanh cho thấy hệ số quy đổi đơn vị đang đúng. Gia tốc kế không trực tiếp xuất góc nghiêng; ta chỉ suy ra được góc ngẩng khi giả định gia tốc trung bình dài hạn là trọng lực và biết chính xác chiều các trục của chip.&lt;/p&gt;

&lt;p&gt;Quy ước hệ trục trước khi đo: trục \(x\) hướng về mũi, trục \(y\) sang trái và trục \(z\) hướng lên trên. Khi bo mạch nằm phẳng và đọc \(a_z \approx +g\), góc ngẩng mũi lên được ước lượng bởi:&lt;/p&gt;

\[\theta_{\mathrm{acc}} \approx \operatorname{atan2}(-a_x, a_z).\]

&lt;p&gt;Khi mũi ngẩng lên, thành phần phản lực dọc theo \(-x\) tăng nên \(-a_x\) dương. Nếu bo mạch đặt phẳng trên bàn mà đọc \(a_z \approx -g\), tức chip đang gắn lộn ngược so với quy ước trên — nếu áp dụng thẳng công thức mà không đổi dấu trục, kết quả sẽ báo góc sai lệch rất lớn (tới $180^\circ$). Luôn ghi lại dấu từng trục ở trạng thái nằm yên trước khi viết công thức lọc.&lt;/p&gt;

&lt;h2 id=&quot;con-quay-hồi-chuyển-đo-tốc-độ-góc-không-đo-góc&quot;&gt;Con quay hồi chuyển đo tốc độ góc, không đo góc&lt;/h2&gt;

&lt;p&gt;Con quay hồi chuyển (gyro) xuất vận tốc góc (độ/giây hoặc rad/s) quanh từng trục. Để có góc quay, ta phải tích phân vận tốc góc theo thời gian, và mọi sai lệch tĩnh (bias) nhỏ sẽ tích lũy thành độ trôi góc tăng dần theo thời gian. Ví dụ, một sai lệch không đổi \(0{,}5~\mathrm{deg/s}\) sau 60 giây tích phân sẽ tạo ra góc quay giả:&lt;/p&gt;

\[0{,}5 \times 60 = 30~\mathrm{deg}\]

&lt;p&gt;dù robot hoàn toàn đứng yên. Vì thế, bài thực hành yêu cầu đo trung bình giá trị gyro khi để yên (sau khi đã quy đổi sang độ/giây) rồi trừ đi trước khi tích phân. Lưu ý sai lệch này vẫn có thể thay đổi theo nhiệt độ, nên việc trừ giá trị ban đầu chỉ khắc phục được phần lệch tại thời điểm khởi động.&lt;/p&gt;

&lt;h2 id=&quot;một-bước-tính-của-bộ-lọc-bổ-trợ&quot;&gt;Một bước tính của bộ lọc bổ trợ&lt;/h2&gt;

&lt;p&gt;Trong khoảng thời gian ngắn, góc tích phân từ gyro mượt và ít bị nhiễu rung động hơn; nhưng về dài hạn, góc suy từ gia tốc kế lại ổn định hơn vì không bị trôi tích lũy. Bộ lọc bổ trợ (complementary filter) kết hợp ưu điểm của cả hai:&lt;/p&gt;

\[\theta \leftarrow \alpha\,(\theta + \omega \Delta t) + (1 - \alpha)\,\theta_{\mathrm{acc}}.\]

&lt;p&gt;Với \(\alpha = 0{,}98\) và \(\Delta t = 0{,}01~\mathrm{s}\), hằng số thời gian hiệu chỉnh từ gia tốc kế vào khoảng \(\Delta t / (1 - \alpha) = 0{,}5~\mathrm{s}\): các biến động nhanh bám theo gyro, còn góc nghiêng dài hạn được kéo dần về \(\theta_{\mathrm{acc}}\).&lt;/p&gt;

&lt;p&gt;Xét một bước tính cụ thể khi \(\theta = 10~\mathrm{deg}\), \(\omega = 5~\mathrm{deg/s}\) và \(\theta_{\mathrm{acc}} = 2~\mathrm{deg}\):&lt;/p&gt;

\[\theta_{\mathrm{gyro}} = 10 + 5 \times 0{,}01 = 10{,}05~\mathrm{deg},\]

\[\theta = 0{,}98 \times 10{,}05 + 0{,}02 \times 2 = 9{,}849 + 0{,}04 = 9{,}889~\mathrm{deg}.\]

&lt;p&gt;Giá trị mới vừa phản ánh xu hướng của gyro vừa được kéo nhẹ về phía $2^\circ$ của gia tốc kế. Cần bảo đảm đồng nhất đơn vị giữa \(\omega\) và \(\theta\) (cùng theo độ hoặc cùng theo radian) để tránh sai lệch hệ số \(\pi/180\).&lt;/p&gt;

&lt;h2 id=&quot;giá-trị-thô-lsb-của-mpu-6050-không-phải-là-độ&quot;&gt;Giá trị thô (LSB) của MPU-6050 không phải là độ&lt;/h2&gt;

&lt;p&gt;Module phổ biến GY-521 (MPU-6050) có địa chỉ I2C &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt; nối mass) hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt; mức cao), hỗ trợ các tầm đo \(\pm 2, 4, 8, 16~\mathrm{g}\) cho gia tốc kế và \(\pm 250\) đến \(\pm 2000~\mathrm{deg/s}\) cho gyro, xuất số nguyên 16-bit. Ở tầm \(\pm 2~\mathrm{g}\), độ nhạy là \(16384~\mathrm{LSB/g}\) — nghĩa là giá trị thô \(a_z \approx +16384\) khi nằm yên tương ứng \(+1~\mathrm{g}\), không phải “16384 độ”. Ở tầm \(\pm 250~\mathrm{deg/s}\), độ nhạy là \(131~\mathrm{LSB/(deg/s)}\), nên giá trị thô 70 tương ứng:&lt;/p&gt;

\[\omega = \frac{70}{131} \approx 0{,}53~\mathrm{deg/s}.\]

&lt;p&gt;Hệ số chia phải khớp tuyệt đối với tầm đo đã cấu hình trong thanh ghi. Ngay cả khi dùng thư viện tự động quy đổi, bạn vẫn nên in đồng thời cả số nguyên thô lẫn giá trị đã chia ít nhất một lần để tự kiểm chứng hệ số.&lt;/p&gt;

&lt;h2 id=&quot;lab-đo-khi-nằm-yên-và-khi-nghiêng-90circ&quot;&gt;Lab: đo khi nằm yên và khi nghiêng $90^\circ$&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Chỉ cấp nguồn qua cổng USB, không chạy động cơ và đặt module cố định trên mặt bàn. Nếu chip nóng bất thường, rút cáp ngay và kiểm tra lại dây &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;GY-521 hoặc module MPU-6050 khác&lt;/td&gt;
      &lt;td&gt;Cảm biến IMU&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 hoặc Pico&lt;/td&gt;
      &lt;td&gt;Master I2C&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Breadboard, dây ngắn&lt;/td&gt;
      &lt;td&gt;Đấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt;, 3,3 V, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Một cuốn sách hoặc mép bàn vuông góc&lt;/td&gt;
      &lt;td&gt;Điểm tựa để nghiêng lặp lại đúng $90^\circ$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Ghi dấu trục, kiểm chứng 1 g và độ lệch gyro theo độ/giây&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Chạy quét I2C xác nhận địa chỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt; hiện ổn định qua hai lần quét liên tiếp.&lt;/li&gt;
  &lt;li&gt;Cấu hình tầm đo \(\pm 2~\mathrm{g}\) và \(\pm 250~\mathrm{deg/s}\) (hoặc đọc lại tầm mặc định của thư viện), ghi kèm hệ số LSB tương ứng.&lt;/li&gt;
  &lt;li&gt;Đặt bo mạch nằm phẳng, đứng yên vài giây. Ghi \(a_x, a_y, a_z\) (đơn vị g) và ba trục gyro (đơn vị độ/giây). Kiểm tra độ lớn gia tốc xấp xỉ 1 g; tính trung bình dữ liệu gyro trong 1 giây làm sai lệch tĩnh ban đầu.&lt;/li&gt;
  &lt;li&gt;Dựng nghiêng bo mạch $90^\circ$ sao cho trục tiến hướng thẳng lên trên. Quan sát giá trị ~1 g chuyển từ trục \(z\) sang trục \(x\) (hoặc \(-x\)) và ghi lại sự thay đổi trục.&lt;/li&gt;
  &lt;li&gt;Nếu chưa có module trên bàn, không tự điền số liệu giả; hãy lập ghi chú hoãn có ngày kèm đủ hai phép tính tay: bước lọc bổ trợ ra $9{,}889^\circ$ và độ trôi $30^\circ$ từ $0{,}5~\mathrm{deg/s}$ trong 60 s.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Một dòng ghi chép rõ ràng theo mẫu: “nằm phẳng, \(a_z = +0{,}98~\mathrm{g}\), $$&lt;/td&gt;
      &lt;td&gt;a&lt;/td&gt;
      &lt;td&gt;= 1{,}02~\mathrm{g}\(, độ lệch gyro\)(0{,}4;\ -0{,}2;\ 0{,}6)~\mathrm{deg/s}\(; nghiêng $90^\circ$ chuyển giá trị ~1 g từ\)z\(sang\)x$$.” Có thể ghi thêm số LSB thô bên cạnh nhưng không được thay thế giá trị đã quy đổi.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Độ lớn khi nằm yên ra 2 g hoặc 0,5 g&lt;/td&gt;
      &lt;td&gt;Chia sai hệ số so với tầm đo thực tế&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Độ lệch gyro lên tới hàng chục độ/giây&lt;/td&gt;
      &lt;td&gt;Quên chia cho 131, hoặc vẫn đang cầm bo mạch trên tay&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Công thức góc ngẩng ra $180^\circ$ khi đặt phẳng&lt;/td&gt;
      &lt;td&gt;Giá trị \(a_z\) âm nhưng vẫn dùng nguyên \(\operatorname{atan2}(-a_x, a_z)\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Quét I2C không thấy địa chỉ&lt;/td&gt;
      &lt;td&gt;Cấp sai áp, đảo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt; hoặc chưa nối chung mass&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hai module trên cùng bus không ACK&lt;/td&gt;
      &lt;td&gt;Trùng địa chỉ; cần kéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt; của một module lên mức cao&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Một module IMU 6 trục là đủ cho toàn bộ môn học, chưa cần mua cảm biến 9 trục có từ kế.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Thay thế&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;GY-521 MPU-6050&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;Hshop GY-521&lt;/a&gt; từng niêm yết khoảng 85.000. Có thể dùng MPU6500 nếu mã nguồn tương thích thanh ghi.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dây Dupont&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây dupont cái cái&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–30.000&lt;/td&gt;
      &lt;td&gt;Nên dùng dây ngắn để tránh nhiễu trên bus I2C.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=MPU6050&quot;&gt;Hshop: MPU6050&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=GY-521%20MPU6050&quot;&gt;Shopee: GY-521&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=MPU6050&quot;&gt;Lazada: MPU6050&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=MPU6050&quot;&gt;Thế Giới IC: MPU6050&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=qS9GwaekLW4&quot;&gt;What is an Inertial Measurement Unit (IMU)?&lt;/a&gt; — Phidgets Inc., tiếng Anh. Phân biệt vai trò và đơn vị của gia tốc kế và con quay hồi chuyển trong cùng một IMU.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[How does an Accelerometer work?&lt;/td&gt;
          &lt;td&gt;3D Animation](https://www.youtube.com/watch?v=KuekQ-m9xpw) — CircuitBread, tiếng Anh. Minh họa vì sao gia tốc kế đo gia tốc riêng chứ không đo vận tốc hay góc trực tiếp.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Thực hiện một bước lọc bổ trợ với \(\alpha = 0{,}98\), \(\Delta t = 0{,}01~\mathrm{s}\), \(\theta = 10~\mathrm{deg}\), \(\omega = 5~\mathrm{deg/s}\) và \(\theta_{\mathrm{acc}} = 2~\mathrm{deg}\). Tính giá trị \(\theta\) tiếp theo.&lt;/li&gt;
  &lt;li&gt;Sai lệch gyro \(0{,}5~\mathrm{deg/s}\) tích phân trong 60 s gây trôi bao nhiêu độ? Nếu sai lệch là \(0{,}8~\mathrm{deg/s}\) trong 2 phút thì trôi bao nhiêu độ?&lt;/li&gt;
  &lt;li&gt;Ở tầm \(\pm 2~\mathrm{g}\) (\(16384~\mathrm{LSB/g}\)) và \(\pm 250~\mathrm{deg/s}\) (\(131~\mathrm{LSB/(deg/s)}\)), đổi hai giá trị thô \(a_z = 16384\) và \(\omega_z = 70\) sang đơn vị vật lý. Vì sao ghi “16384 độ” là sai ở cả hai khía cạnh?&lt;/li&gt;
  &lt;li&gt;Bo mạch nằm phẳng đọc \(a_z = +1~\mathrm{g}\). Khi ngẩng mũi lên, cảm biến đọc \(a_x = -0{,}50~\mathrm{g}\) và \(a_z = 0{,}866~\mathrm{g}\). Tính góc ngẩng bằng \(\operatorname{atan2}(-a_x, a_z)\).&lt;/li&gt;
  &lt;li&gt;Nếu gắn ngược bo mạch đó sao cho khi nằm phẳng đọc \(a_z = -1~\mathrm{g}\) và \(a_x = 0\), công thức chưa điều chỉnh trả về góc bao nhiêu, và bạn cần ghi chú gì trước khi lọc?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(\theta_{\mathrm{gyro}} = 10{,}05~\mathrm{deg}\), suy ra \(\theta = 0{,}98 \times 10{,}05 + 0{,}02 \times 2 = 9{,}889~\mathrm{deg}\).&lt;/li&gt;
  &lt;li&gt;\(0{,}5 \times 60 = 30~\mathrm{deg}\); với \(0{,}8~\mathrm{deg/s}\) trong 120 s là \(0{,}8 \times 120 = 96~\mathrm{deg}\).&lt;/li&gt;
  &lt;li&gt;\(a_z = +1{,}00~\mathrm{g}\) và \(\omega_z = 70/131 \approx 0{,}53~\mathrm{deg/s}\). Ghi “16384 độ” sai cả về đơn vị (đang là giá trị LSB thô chưa chia thang) lẫn đại lượng vật lý (đây là kênh gia tốc, không phải góc).&lt;/li&gt;
  &lt;li&gt;\(\operatorname{atan2}(0{,}50,\ 0{,}866) = 30^\circ\).&lt;/li&gt;
  &lt;li&gt;\(\operatorname{atan2}(0,\ -1) = 180^\circ\) dù bo mạch đang nằm phẳng. Cần ghi lại dấu đo được khi để yên và đổi dấu ánh xạ trục trước khi áp dụng công thức.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf&quot;&gt;Datasheet MPU-6000/MPU-6050 (TDK InvenSense)&lt;/a&gt; — bảng độ nhạy chính thức (16384 LSB/g và 131 LSB/(deg/s)).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/mpu-6050-hookup-guide/all&quot;&gt;SparkFun: nối MPU-6050&lt;/a&gt; — hướng dẫn chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt;, địa chỉ và đọc dữ liệu thô ban đầu.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/sensor_msgs/msg/Imu.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Imu&lt;/code&gt;&lt;/a&gt; — quy ước đơn vị rad/s, m/s² và ý nghĩa hiệp phương sai $-1$ khi chưa ước lượng hướng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/i2c.html&quot;&gt;I2C master của Espressif&lt;/a&gt; — tài liệu ngoại vi I2C giao tiếp với GY-521.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Encoder và đếm odometry thô</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_03_encoders_counts/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_03_encoders_counts</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;75–90 phút&lt;/strong&gt;. Phần lớn thời gian dành cho việc tự quay bánh bằng tay đúng một vòng để đo số xung thực tế, rồi quy đổi sang quãng đường theo mét.&lt;/p&gt;

&lt;p&gt;Nếu bạn nhắm mắt bước đi trong phòng tối, làm sao bạn biết mình đã đi được bao xa? Bạn đếm số bước chân của chính mình! Với robot di động, &lt;strong&gt;encoder&lt;/strong&gt; gắn sau đuôi trục động cơ chính là “bộ đếm bước chân” cơ điện tử đó: mỗi khi bánh xe quay thêm một góc nhỏ li ti, encoder phát ra một xung điện để vi điều khiển cộng dồn thành quãng đường và vận tốc (&lt;strong&gt;odometry&lt;/strong&gt;). Tuy nhiên, giống như việc đi trên sàn trơn khiến số bước chân không còn khớp với quãng đường thực, hiểu sâu về encoder nghĩa là nắm cả độ phân giải xung lẫn nguyên nhân khiến sai số odometry tích lũy dần theo thời gian.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-từ-xung-vuông-cầu-phương-đến-odometry-bánh-xe&quot;&gt;Kiến thức cần nhớ: Từ xung vuông cầu phương đến odometry bánh xe&lt;/h2&gt;

&lt;h3 id=&quot;1-giải-mã-cầu-phương-quadrature-decoding-4-và-chiều-quay&quot;&gt;1. Giải mã cầu phương (Quadrature Decoding 4×) và chiều quay&lt;/h3&gt;
&lt;p&gt;Một encoder quang học hoặc từ tính (Hall) chuẩn có hai kênh &lt;strong&gt;A&lt;/strong&gt; và &lt;strong&gt;B&lt;/strong&gt; lệch pha nhau đúng \(90^\circ\) (một phần tư chu kỳ). Nếu chỉ đếm cạnh lên của kênh A, bạn không biết bánh đang tiến hay lùi. Khi kết hợp cả hai kênh A và B: kênh A đi trước B báo hiệu quay thuận, B đi trước A báo hiệu quay nghịch. Đếm cả cạnh lên lẫn cạnh xuống trên cả A và B (&lt;strong&gt;chế độ 4×&lt;/strong&gt;) giúp tăng độ phân giải góc lên gấp &lt;strong&gt;4 lần&lt;/strong&gt; so với chỉ số PPR gốc trên trục động cơ.&lt;/p&gt;

&lt;h3 id=&quot;2-nhân-độ-phân-giải-qua-hộp-số-và-bẫy-tràn-số-nguyên-integer-overflow&quot;&gt;2. Nhân độ phân giải qua hộp số và bẫy tràn số nguyên (Integer Overflow)&lt;/h3&gt;
&lt;p&gt;Đặt encoder ở &lt;em&gt;trục động cơ&lt;/em&gt; (trước hộp số giảm tốc tỉ số \(G\)) thay vì trục bánh xe giúp tăng mật độ xung lên \(G\) lần, cho phép đo vận tốc cực mượt ở tốc độ thấp. Nhưng mật độ xung cao cũng kéo theo hai rủi ro phần mềm: phải dùng &lt;strong&gt;ngắt phần cứng (hardware interrupt / timer encoder mode)&lt;/strong&gt; để không mất xung ở tốc độ cao, và phải tính hiệu \(\Delta N\) theo số học bù hai an toàn khi bộ đếm 16-bit hoặc 32-bit quay vòng.&lt;/p&gt;

&lt;h3 id=&quot;3-bản-chất-tích-phân-của-odometry-và-sai-số-trôi-tích-lũy&quot;&gt;3. Bản chất tích phân của Odometry và sai số trôi tích lũy&lt;/h3&gt;
&lt;p&gt;Từ số xung \(\Delta N\), ta tính quãng đường bánh xe \(\Delta s = \frac{\Delta N}{N_{\text{rev}}} \pi D\). Vì vị trí \((x, y, \theta)\) là tổng tích phân của hàng nghìn bước nhỏ liên tiếp, chỉ cần sai số đường kính bánh xe \(1\%\) hay một cú trượt bánh thoáng qua cũng khiến sai số vị trí lệch hàng chục centimet sau vài mét — lý do odometry luôn cần kết hợp thêm IMU hoặc LiDAR/camera.&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-cầm-tay-tính-độ-phân-giải-odometry-và-vận-tốc-bánh-xe-từ-số-xung-thực&quot;&gt;Giải thích: Cầm tay tính độ phân giải odometry và vận tốc bánh xe từ số xung thực&lt;/h3&gt;

&lt;p&gt;Giả sử robot dùng động cơ DC có encoder &lt;strong&gt;11 PPR&lt;/strong&gt; gắn ở trục sau động cơ, qua hộp số giảm tốc tỉ số &lt;strong&gt;\(G = 30{:}1\)&lt;/strong&gt;, chạy giải mã cầu phương &lt;strong&gt;4×&lt;/strong&gt;, lắp bánh xe đường kính &lt;strong&gt;\(D = 65\text{ mm}\)&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tổng số xung trên 1 vòng quay hoàn chỉnh của bánh xe:&lt;/strong&gt;
\(N_{\text{rev}} = 11\text{ PPR} \times 4 \times 30 = 1\,320\text{ xung/vòng bánh}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chu vi bánh xe và độ phân giải tuyến tính mỗi xung:&lt;/strong&gt;
\(C = \pi D = \pi \times 65 \approx 204{,}2\text{ mm} \implies \Delta s_{\text{1 xung}} = \frac{204{,}2}{1\,320} \approx 0{,}155\text{ mm/xung}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính quãng đường và vận tốc khi đọc được \(\Delta N = 660\) xung trong \(\Delta t = 0{,}5\text{ s}\):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Số vòng quay: \(660 / 1\,320 = 0{,}5\text{ vòng}\).&lt;/li&gt;
      &lt;li&gt;Quãng đường bánh đi được: \(\Delta s = 0{,}5 \times 204{,}2 = 102{,}1\text{ mm} = 0{,}1021\text{ m}\).&lt;/li&gt;
      &lt;li&gt;Vận tốc tuyến tính: \(v = \frac{0{,}1021}{0{,}5} = 0{,}2042\text{ m/s}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nếu lốp cao su bị mòn hoặc non hơi làm đường kính thực giảm từ \(65\text{ mm}\) xuống \(63{,}7\text{ mm}\) (chỉ lệch \(2\%\)), sau \(10\text{ m}\) chạy thẳng, odometry sẽ báo nhầm tới \(20\text{ cm}\) — đó là lý do bước hiệu chuẩn đường kính bánh thực tế luôn bắt buộc trước khi chạy điều hướng!&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn đọc được hai sóng vuông lệch pha (quadrature) của encoder hai kênh A và B để xác định kênh nào dẫn trước, từ đó phân biệt chiều tiến và chiều lùi. Bạn không tin mù quáng vào thông số “PPR” trên trang bán hàng mà tự đo số xung thực tế trên một vòng trục ra của bánh theo đúng chế độ giải mã trong firmware. Bạn quy đổi được số đếm thành cung tròn với bánh đường kính 65 mm, rồi từ quãng đường hai bánh tính được một bước ước lượng vị trí (dead reckoning): quãng đường tịnh tiến của tâm trục và góc quay theo radian — nền tảng trực tiếp của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã biết đếm cạnh xung trên GPIO và in ra màn hình serial (Chương 03), đồng thời phân biệt được đĩa từ Hall trên hộp số TT vàng với đĩa quang có rãnh (Bài 04-02). Encoder tín hiệu số không dùng I2C như Bài 04-01; UART chỉ dùng để gửi số đếm lên máy tính. Luôn dùng đồng hồ vạn năng đo mức cao của tín hiệu encoder trước khi nối vào GPIO 3,3 V. Bài này chưa cần ROS và không cần cấp điện cho động cơ ở bước đo chính.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Capstone A ở Chương 07 chạy teleop xe vi sai qua cổng serial và tự dừng khi hết hạn lệnh. Bản chạy thử đó vẫn hoạt động được khi chưa có encoder, nhưng nhật ký kiểm thử sẽ đáng tin cậy hơn nhiều khi có thêm số đếm bánh để đối chiếu trực tiếp với thước đo thực tế.&lt;/p&gt;

&lt;p&gt;Trong ROS 2, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; áp dụng đúng các công thức hình học này: firmware hoặc lớp giao tiếp phần cứng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2_control&lt;/code&gt;) đổi số xung thành góc quay bánh nhờ hằng số \(N\) (số xung trên một vòng trục ra sau giải mã); bộ điều khiển đổi góc quay thành mét qua tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt; (\(r\)) và thành góc quay hướng (yaw) qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_separation&lt;/code&gt; (khẩu độ \(b\)), rồi xuất bản tin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt; (còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/JointState&lt;/code&gt; mang chính vị trí góc bánh theo radian). Nếu khai báo \(N\) lớn gấp đôi thực tế, odometry sẽ báo quãng đường chỉ bằng một nửa thực tế dù bán kính bánh hoàn toàn đúng. Vì vậy, luôn đo \(N\) trực tiếp trên bánh thật thay vì chép từ tiêu đề quảng cáo.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/encoder_quadrature.png&quot; alt=&quot;Hai kênh quadrature A và B: cạnh nào lên trước thì đó là chiều&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;hai-sóng-vuông-lệch-pha&quot;&gt;Hai sóng vuông lệch pha&lt;/h2&gt;

&lt;p&gt;Encoder quadrature xuất hai chuỗi sóng vuông A và B lệch nhau khoảng một phần tư chu kỳ ($90^\circ$). Chiều quay được xác định bởi kênh nào chuyển mức trước: theo một quy ước thường dùng (cần kiểm chứng lại trên mạch thực tế), nếu cạnh lên của A xuất hiện khi B đang ở mức thấp thì tính là chiều tiến, còn nếu cạnh lên của B xuất hiện khi A đang ở mức thấp thì tính là chiều lùi. Đảo hai dây A và B sẽ làm đảo dấu bộ đếm.&lt;/p&gt;

&lt;p&gt;Ngược lại, encoder một kênh chỉ đếm được số xung mà không tự biết chiều quay; việc suy đoán dấu theo lệnh điều khiển động cơ sẽ sai lệch khi bánh bị trôi quán tính hoặc giật ngược lúc phanh.&lt;/p&gt;

&lt;h2 id=&quot;tránh-hiểu-nhầm-thông-số-ppr&quot;&gt;Tránh hiểu nhầm thông số “PPR”&lt;/h2&gt;

&lt;p&gt;Trên các trang bán linh kiện, “PPR” (Pulses Per Revolution) có thể chỉ số chu kỳ mỗi vòng của một kênh, nhưng đôi khi lại chỉ tổng số cạnh sau khi giải mã. Khi dùng chế độ giải mã 4× (đếm cả cạnh lên và cạnh xuống của cả hai kênh A và B):&lt;/p&gt;

\[N \approx 4 \times (\text{số chu kỳ trên một vòng của một kênh}).\]

&lt;p&gt;Ngoài ra, nhiều động cơ TT vàng gắn đĩa nam châm trực tiếp lên trục động cơ (phía trước hộp số). Nếu đĩa tạo 11 cạnh lên mỗi vòng trục động cơ (đếm 1×) đi qua hộp số tỉ số 1:48, thì số xung trên một vòng trục ra gắn bánh là:&lt;/p&gt;

\[N = 11 \times 48 = 528.\]

&lt;p&gt;Nếu lấy nhầm số 11 thay vào công thức quãng đường, mọi khoảng cách tính ra sẽ bị phóng đại gấp 48 lần. Cách chắc chắn nhất để xác định \(N\) là vạch dấu trên lốp, quay tay đúng một vòng và đọc trực tiếp từ bộ đếm của chính firmware đang chạy.&lt;/p&gt;

&lt;h2 id=&quot;quãng-đường-ứng-với-mỗi-xung&quot;&gt;Quãng đường ứng với mỗi xung&lt;/h2&gt;

&lt;p&gt;Giả sử bánh không trượt, mỗi vòng quay đưa tâm bánh đi một đoạn đúng bằng chu vi \(2\pi r\), nên với \(\Delta c\) xung đếm được:&lt;/p&gt;

\[\Delta s = \frac{2\pi r}{N}\Delta c.\]

&lt;p&gt;Với bánh đường kính 65 mm, bán kính \(r = 0{,}0325~\mathrm{m}\) và chu vi là:&lt;/p&gt;

\[2\pi r \approx 2 \times 3{,}1416 \times 0{,}0325 = 0{,}2042~\mathrm{m}.\]

&lt;p&gt;Nếu dùng đĩa quang đơn giản với firmware đếm \(N = 20\) xung trên một vòng trục ra, mỗi xung tương ứng:&lt;/p&gt;

\[\frac{0{,}2042}{20} = 0{,}01021~\mathrm{m} \approx 1{,}021~\mathrm{cm}.\]

&lt;p&gt;Khi đếm được 140 xung, quãng đường là:&lt;/p&gt;

\[\Delta s = 140 \times 0{,}01021 \approx 1{,}43~\mathrm{m}.\]

&lt;p&gt;Lưu ý \(N = 20\) chỉ là ví dụ cho đĩa rãnh học tập; nếu lần quay tay thực tế trên động cơ của bạn cho ra 360, 528 hay 1440, hãy thay đúng con số đó vào công thức.&lt;/p&gt;

&lt;h2 id=&quot;một-bước-ước-lượng-vị-trí-dead-reckoning&quot;&gt;Một bước ước lượng vị trí (dead reckoning)&lt;/h2&gt;

&lt;p&gt;Cặp công thức động học vi sai sau sẽ được dùng lại ở Chương 06 và trong bộ điều khiển:&lt;/p&gt;

\[\Delta s = \frac{\Delta s_r + \Delta s_l}{2}, \qquad \Delta \theta = \frac{\Delta s_r - \Delta s_l}{b}.\]

&lt;p&gt;Trong đó \(b\) là khoảng cách giữa tâm hai vết bánh (wheel separation), không phải đường kính bánh. Ví dụ với \(\Delta s_r = 0{,}10~\mathrm{m}\), \(\Delta s_l = 0{,}06~\mathrm{m}\) và \(b = 0{,}15~\mathrm{m}\):&lt;/p&gt;

\[\Delta s = \frac{0{,}10 + 0{,}06}{2} = 0{,}08~\mathrm{m}, \qquad \Delta \theta = \frac{0{,}04}{0{,}15} = 0{,}267~\mathrm{rad} \approx 15{,}3^\circ.\]

&lt;p&gt;Tâm trục xe dịch chuyển 8 cm và quay khoảng $15{,}3^\circ$ về phía bánh chậm hơn (bánh trái). Công thức chưa tính đến độ trượt của bánh. Nếu chỉ đảo nhầm dây A và B ở encoder bánh phải, sổ ghi sẽ nhận \(\Delta s_r = -0{,}10~\mathrm{m}\) dù bánh thực tế đi tới, làm sai lệch hoàn toàn cả \(\Delta s\) lẫn \(\Delta \theta\).&lt;/p&gt;

&lt;h2 id=&quot;lab-quay-tay-một-vòng-để-đo-n&quot;&gt;Lab: quay tay một vòng để đo \(N\)&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Thực hiện phép đếm khi &lt;strong&gt;không cấp điện&lt;/strong&gt; cho động cơ. Nếu đã có mạch cầu H và muốn thực hiện bước tùy chọn kiểm tra chiều khi cấp điện, bắt buộc kê bánh xe nhấc khỏi bàn, chỉ cấp xung ngắn và giữ tay tránh xa nan hoa. Nếu đo thấy mức logic cao của cảm biến Hall là 5 V, phải lắp cầu chia áp 1 kΩ / 2 kΩ (tương tự chân Echo ở Bài 04-02) trước khi nối vào GPIO của ESP32.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Một động cơ giảm tốc có encoder, hoặc đĩa quang rãnh rời&lt;/td&gt;
      &lt;td&gt;Trục cần đo số xung&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 hoặc Pico&lt;/td&gt;
      &lt;td&gt;Đọc và đếm cạnh xung&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bút đánh dấu&lt;/td&gt;
      &lt;td&gt;Kẻ vạch mốc trên lốp và vỏ hộp số&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sổ tay&lt;/td&gt;
      &lt;td&gt;Ghi \(N\), bán kính và mét trên mỗi xung&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạch cầu H và pin&lt;/td&gt;
      &lt;td&gt;Chỉ dùng cho bước kiểm tra chiều tùy chọn (bánh nhấc khỏi bàn)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Kẻ một vạch mốc trên lốp và một vạch tương ứng trên vỏ hộp số để xác định chính xác một vòng quay.&lt;/li&gt;
  &lt;li&gt;Không cấp nguồn động cơ; đưa bộ đếm về 0.&lt;/li&gt;
  &lt;li&gt;Quay tay trục ra đúng một vòng trở lại vạch mốc rồi ghi số đếm. Lặp lại ba lần; ba kết quả nên lệch nhau không quá 1 xung.&lt;/li&gt;
  &lt;li&gt;Giá trị thu được chính là \(N\) cho chế độ giải mã đang chạy. Tính \(2\pi r / N\) theo đúng bán kính chiếc lốp đã đo.&lt;/li&gt;
  &lt;li&gt;Vẫn không cấp điện động cơ, quay bánh theo chiều tiến của robot và xác định kênh nào lên trước (dùng máy phân tích logic hoặc in sự kiện “A lên”, “B lên” ra serial). Nếu chỉ có encoder một kênh, ghi rõ: “một kênh, chiều phụ thuộc lệnh động cơ”.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;(Tùy chọn, bánh nhấc khỏi bàn)&lt;/em&gt; Cấp một xung điện ngắn theo chiều tiến và xác nhận cùng kênh đó dẫn trước; nếu ngược dấu, đảo hai dây A và B rồi kiểm tra lại.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Ba lần quay tay cho kết quả \(N\) thống nhất, giá trị quãng đường trên mỗi xung tính theo mét và ghi chú kênh dẫn trước khi đi tiến được lưu đầy đủ vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Số đếm đột ngột tăng gấp đôi&lt;/td&gt;
      &lt;td&gt;Đổi chế độ giải mã từ 1× sang 2× hoặc 4× nhưng quên cập nhật \(N\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trang bán ghi 40 PPR nhưng quay tay ra 20&lt;/td&gt;
      &lt;td&gt;Trang bán tính tổng số cạnh nhiều hơn số cạnh mã nguồn đang đếm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ra lệnh tiến nhưng số đếm giảm&lt;/td&gt;
      &lt;td&gt;Đảo hai dây A/B hoặc đảo ngược cực dây động cơ (chỉ sửa từng thứ một)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh đứng yên nhưng số đếm vẫn tăng&lt;/td&gt;
      &lt;td&gt;Nhiễu/dội tín hiệu, chân để nổi hoặc chưa nối chung mass&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Quãng đường tính ra dài gấp vài chục lần thực tế&lt;/td&gt;
      &lt;td&gt;Dùng nhầm số xung của trục động cơ mà quên nhân tỉ số hộp số&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;cùng-mức-pwm-vì-sao-xe-vẫn-chạy-lệch&quot;&gt;Cùng mức PWM, vì sao xe vẫn chạy lệch?&lt;/h2&gt;

&lt;p&gt;Ngay cả khi gửi cùng một mức PWM cho cả hai bánh, xe vi sai vẫn thường bị lệch sang một bên. Hai động cơ giảm tốc TT vàng không bao giờ đồng nhất tuyệt đối: độ rơ bánh răng, ma sát và sai số chế tạo khiến một trục quay nhanh hơn trục kia vài phần trăm. Điều khiển vòng hở không có phản hồi nên không thể tự bù sai lệch này.&lt;/p&gt;

&lt;p&gt;Encoder cung cấp tín hiệu phản hồi tốc độ để thấy ngay độ lệch đó. Đĩa quang 20 rãnh (ví dụ \(N = 20\)) đủ để học nguyên lý đếm, còn trên robot Capstone A nên dùng cặp động cơ TT có sẵn encoder Hall đọc bằng ESP32/Pico và lái bằng TB6612 (lưu ý các video minh họa dùng Arduino và L298N không phải bộ kit mặc định của khóa học).&lt;/p&gt;

&lt;p&gt;Ví dụ khi đếm đồng thời hai bánh trong cùng một khoảng chạy: bánh trái đạt 519 xung, bánh phải đạt 552 xung. Bánh phải nhanh hơn bánh trái \(552 - 519 = 33\) xung, tương đương:&lt;/p&gt;

\[\frac{33}{519} \approx 6{,}4\%.\]

&lt;p&gt;Với độ lệch này, lệnh “đi thẳng” ở vòng hở sẽ tạo thành quỹ đạo cong. Bạn có thể thử nhanh (nhấc bánh khỏi bàn): xuất cùng một mức duty vừa phải cho hai kênh trong 3 giây, so sánh hai bộ đếm và ghi cặp số vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Giải pháp căn cơ không phải chỉnh tay một hệ số bù cố định rồi bỏ đó, mà là điều khiển vòng kín tốc độ từng bánh bằng PID ở &lt;a href=&quot;/contents/vi/chapter08/08_03_pid_tuning/&quot;&gt;bài 08-03&lt;/a&gt; và phân tích động học vi sai ở &lt;a href=&quot;/contents/vi/chapter08/08_04_diff_kinematics/&quot;&gt;bài 08-04&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Xem thêm: video ngắn của LPPS - 1 Robotics Team, &lt;a href=&quot;https://www.facebook.com/reel/1851960802892543&quot;&gt;cùng PWM, sao xe không đi thẳng&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Nên mua theo cặp động cơ TT có sẵn encoder Hall để hai bánh đồng bộ về tỉ số truyền hộp số.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Thay thế&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ TT có encoder Hall&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ TT encoder Hall&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;40.000–90.000 một cái&lt;/td&gt;
      &lt;td&gt;Động cơ N20 có encoder nếu khớp lỗ gá và trục (3 mm hoặc 4 mm)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đĩa rãnh và cặp quang&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đĩa encoder quang&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;Phù hợp để học cách đo \(N\), ít chắc chắn hơn cho khung Capstone&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1 kΩ và 2 kΩ&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;điện trở 1k 2k&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;10.000–20.000 một vỉ&lt;/td&gt;
      &lt;td&gt;Chỉ cần khi mức cao của cảm biến Hall đo được 5 V&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=dong%20co%20encoder&quot;&gt;Hshop: động cơ encoder&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=dong%20co%20TT%20encoder&quot;&gt;Shopee: động cơ TT encoder&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=encoder%20Hall%20TT&quot;&gt;Lazada: encoder Hall TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=encoder&quot;&gt;Thế Giới IC: encoder&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cần đọc kỹ thông số xung là tính trên trục động cơ hay trục ra, sau đó luôn kiểm chứng lại bằng cách tự quay tay một vòng.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=v4BbSzJ-hz4&quot;&gt;How Rotary Encoder Works and How To Use It with Arduino&lt;/a&gt; — How To Mechatronics, tiếng Anh. Giải thích hai kênh A, B và cách thứ tự cạnh xác định chiều quay.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=eQ9E0Zvp9jw&quot;&gt;Modern Robotics, Chapter 13.4:  Odometry&lt;/a&gt; — Northwestern Robotics, tiếng Anh. Tổng quan cách tích hợp các bước đếm encoder thành tư thế robot (sẽ triển khai chi tiết ở Chương 08).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;p&gt;Lấy \(r = 0{,}0325~\mathrm{m}\) và chu vi \(\approx 0{,}2042~\mathrm{m}\) nếu đề không nêu khác.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Với \(N = 20\) xung trên một vòng trục ra, bánh xe lăn được bao xa sau 140 xung?&lt;/li&gt;
  &lt;li&gt;Cùng firmware đó, quay tay một vòng thu được 20 xung nhưng bạn lại nhập thông số “40 PPR” của người bán vào công thức. Chương trình sẽ báo quãng đường bao nhiêu cho 140 xung, so với quãng đường đúng?&lt;/li&gt;
  &lt;li&gt;Bánh xe thực tế đi được \(\Delta s_r = 0{,}10~\mathrm{m}\) và \(\Delta s_l = 0{,}06~\mathrm{m}\) với khẩu độ \(b = 0{,}15~\mathrm{m}\). Do đảo dây A và B ở encoder bánh phải, sổ ghi nhận \(-0{,}10~\mathrm{m}\) và \(+0{,}06~\mathrm{m}\). Tính \(\Delta s\) và \(\Delta \theta\) sai lệch so với giá trị đúng (\(0{,}08~\mathrm{m}\) và \(0{,}267~\mathrm{rad}\)).&lt;/li&gt;
  &lt;li&gt;Một kênh đếm 80 cạnh lên khi đi tới và 80 cạnh lên nữa khi lăn lùi về đúng vạch xuất phát (với \(N = 20\)). Encoder quadrature báo quãng đường thực bằng bao nhiêu, còn nếu cộng dồn 160 cạnh như chỉ đi tới thì kết quả là bao nhiêu?&lt;/li&gt;
  &lt;li&gt;Một động cơ TT phát 11 cạnh lên trên mỗi vòng trục động cơ, qua hộp số 1:48, mã nguồn đếm 1× ở trục ra. Hỏi \(N\) bằng bao nhiêu, và nếu dùng nhầm 11 thì quãng đường tính ra sai lệch gấp mấy lần?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Mỗi xung tương ứng \(0{,}01021~\mathrm{m}\); sau 140 xung được \(1{,}43~\mathrm{m}\).&lt;/li&gt;
  &lt;li&gt;Với \(N = 40\), chương trình báo \(0{,}715~\mathrm{m}\), chỉ bằng một nửa giá trị đúng \(1{,}43~\mathrm{m}\).&lt;/li&gt;
  &lt;li&gt;Sổ báo \(\Delta s = -0{,}02~\mathrm{m}\) và \(\Delta \theta = -1{,}067~\mathrm{rad}\) (xấp xỉ \(-61^\circ\)), thay vì \(+0{,}08~\mathrm{m}\) và \(+15{,}3^\circ\).&lt;/li&gt;
  &lt;li&gt;Encoder quadrature có tổng đại số bằng 0 nên quãng đường thực bằng 0; nếu cộng dồn cả 160 cạnh sẽ ra \(1{,}63~\mathrm{m}\).&lt;/li&gt;
  &lt;li&gt;\(N = 11 \times 48 = 528\); dùng nhầm 11 sẽ làm mọi quãng đường phóng đại gấp 48 lần.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/rotary-encoder-breakout-hookup-guide/all&quot;&gt;SparkFun: nối rotary encoder&lt;/a&gt; — giải thích thứ tự cạnh và giới hạn của encoder một kênh.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/3081&quot;&gt;Pololu: cặp encoder từ&lt;/a&gt; — ví dụ thực tế về việc datasheet ghi số xung theo trục động cơ và cần nhân tỉ số hộp số.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/nav_msgs/msg/Odometry.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt;&lt;/a&gt; — chuẩn bản tin ROS 2 tổng hợp từ các bước ước lượng dead reckoning.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://control.ros.org/jazzy/doc/ros2_controllers/diff_drive_controller/doc/userdoc.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt;&lt;/a&gt; — cách &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt; (\(r\)) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_separation&lt;/code&gt; (\(b\)) kết hợp với số đếm đã quy đổi từ \(N\).&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Nhận diện module: HC-SR04, ToF, IMU, IR, encoder, camera</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_02_sensor_package_id/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_02_sensor_package_id</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;60–80 phút&lt;/strong&gt;. Đây là bài thực hành nhận diện linh kiện trực tiếp trên bàn; bạn có thể kết hợp ảnh chụp các module đang có với tài liệu datasheet của những món chưa mua.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn nhận diện được đúng loại cảm biến dựa trên hình dáng vật lý và nhãn chân trước khi cấp điện, xác định được điện áp nuôi và chuẩn giao tiếp (trig/echo, I2C, analog hay USB), đồng thời tuyệt đối không đưa tín hiệu Echo 5 V trực tiếp vào chân ESP32. Bạn lập được một phiếu nhận diện dùng lại cho Capstone A để chọn đúng cảm biến đo khoảng cách và encoder bánh xe, cũng như phân biệt rõ linh kiện cần chuẩn bị trước Chương 07 với các thiết bị thị giác để dành cho Chương 11.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã hoàn thành bài 04-01 (I2C, SPI, UART), nắm thói quen đọc sơ đồ chân từ Chương 01 và có sẵn đồng hồ vạn năng. Không bắt buộc phải có đầy đủ mọi loại cảm biến ngay trên bàn.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Trên bàn linh kiện robot, năm loại cảm biến phổ biến nhất — &lt;strong&gt;siêu âm HC-SR04&lt;/strong&gt;, &lt;strong&gt;laser ToF (VL53L0X)&lt;/strong&gt;, &lt;strong&gt;IMU (MPU6050)&lt;/strong&gt;, &lt;strong&gt;cảm biến hồng ngoại dò line (IR)&lt;/strong&gt; và &lt;strong&gt;encoder bánh xe&lt;/strong&gt; — đều có kích thước chỉ bằng đồng xu, nhưng nguyên lý vật lý, điện áp làm việc và điểm mù của chúng lại khác nhau một trời một vực! Nhận diện đúng từng module bằng mắt thường và hiểu rõ giới hạn vật lý của nó giúp bạn chọn đúng “đôi mắt” cho từng nhiệm vụ.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-năm-giác-quan-cốt-lõi-của-robot-di-động&quot;&gt;Kiến thức cần nhớ: Năm Giác quan Cốt lõi của Robot Di động&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Đo Khoảng cách (Siêu âm HC-SR04 vs. Laser ToF VL53L0X):&lt;/strong&gt; HC-SR04 phát sóng âm 40 kHz hình nón rộng (~\(15^\circ\)), hoạt động tốt với kính trong suốt nhưng dễ bị hấp thụ bởi rèm vải mềm và dùng logic &lt;strong&gt;5 V&lt;/strong&gt;; VL53L0X đo thời gian bay của tia laser hồng ngoại chùm hẹp (\(t \sim\) nano-giây), dùng &lt;strong&gt;3,3 V I2C&lt;/strong&gt; và cực chính xác ở cự ly gần (&amp;lt; 1,2 m) nhưng kém hơn dưới ánh nắng gắt.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đo Chuyển động Nội tại (IMU 6 bậc tự do &amp;amp; Encoder):&lt;/strong&gt; IMU đo gia tốc tuyến tính (\(a_x, a_y, a_z\)) và vận tốc góc (\(\omega_x, \omega_y, \omega_z\)) trực tiếp trên thân xe; Encoder đếm số vòng quay từng bánh xe để tính quãng đường (odometry) nhưng bị sai khi bánh xe trượt trên sàn trơn!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cảm biến Gần / Dò vạch (Hồng ngoại phản xạ IR):&lt;/strong&gt; Phát tia hồng ngoại và đo cường độ phản xạ từ mặt sàn (màu trắng phản xạ mạnh, vạch đen hấp thụ) — rất nhạy với khoảng cách gầm xe và ánh sáng mặt trời.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-khoảng-cách-từ-xung-echo-của-hc-sr04-và-kiểm-tra-mạch-hạ-áp-5-v--33-v&quot;&gt;Ví dụ làm từng bước: Tính khoảng cách từ xung Echo của HC-SR04 và kiểm tra mạch hạ áp 5 V → 3,3 V&lt;/h3&gt;

&lt;p&gt;Cảm biến siêu âm HC-SR04 phát xung âm thanh với tốc độ trong không khí ở \(20^\circ\text{C}\) là \(c = 343\text{ m/s} = \mathbf{0.0343\text{ cm}/\mu\text{s}}\). Giả sử vi điều khiển đo được độ rộng xung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; trên chân Echo là &lt;strong&gt;\(t = 1{,}166\ \mu\text{s}\)&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Vì sóng âm phải bay từ cảm biến đến vật cản rồi &lt;strong&gt;phản xạ quay ngược lại&lt;/strong&gt; (tổng quãng đường \(2d\)):
\(d = \frac{c \cdot t}{2} = \frac{0.0343\text{ cm}/\mu\text{s} \times 1{,}166\ \mu\text{s}}{2} \approx \mathbf{20.0\text{ cm}}!\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kiểm tra an toàn điện áp chân Echo cho ESP32/Pico (3,3 V):&lt;/strong&gt; HC-SR04 bản chuẩn cấp nguồn 5 V sẽ đẩy chân Echo lên &lt;strong&gt;5,0 V&lt;/strong&gt;. Dùng cặp điện trở phân áp \(R_1 = 1\text{ k}\Omega\) (nối từ Echo) và \(R_2 = 2\text{ k}\Omega\) (nối xuống GND):
\(V_{\text{GPIO}} = 5.0 \times \frac{2}{1 + 2} = \mathbf{3.33\text{ V}} \quad (\text{an toàn tuyệt đối!}).\)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-4-bước-nhận-diện-nhanh-một-module-cảm-biến-mới-trước-khi-cắm-dây&quot;&gt;Giải thích: 4 bước nhận diện nhanh một module cảm biến mới trước khi cắm dây&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Đọc kỹ chữ in lụa (silkscreen) quanh hàng chân:&lt;/strong&gt; Kiểm tra ngay ghi chú điện áp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIN 3-5V&lt;/code&gt;), tên bus (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA/SCL&lt;/code&gt; = I2C, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRIG/ECHO&lt;/code&gt; = siêu âm, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A0/D0&lt;/code&gt; = cảm biến IR/analog).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Soi chiều trục tọa độ in trên bo IMU:&lt;/strong&gt; Các module MPU6050/ICM-20948 đều in mũi tên trục \(+X, +Y\) ngay trên mặt PCB — gắn lệch \(90^\circ\) mà không đổi dấu trong code sẽ làm xe quay ngược chiều!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Kiểm tra xem trên module đã có sẵn IC ổn áp và điện trở kéo lên chưa:&lt;/strong&gt; Nhiều module GY-521 (MPU6050) đã hàn sẵn LDO 3,3 V và pull-up I2C, trong khi cảm biến trần cần cấp đúng 3,3 V sạch.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Kết hợp đa cảm biến (Sensor Fusion) để bù điểm mù cho nhau:&lt;/strong&gt; Siêu âm + ToF bù nhau trước kính và vải; Encoder + IMU (con quay hồi chuyển) bù nhau chống trượt bánh và trôi góc!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu đặt vật cản cách cảm biến HC-SR04 đúng &lt;strong&gt;1,00 mét&lt;/strong&gt; (\(100\text{ cm}\)), xung Echo sẽ kéo dài khoảng bao nhiêu mili-giây? Ta có \(t = \dfrac{2 \times 100}{0.0343} \approx \mathbf{5{,}831\ \mu\text{s} \approx 5.83\text{ ms}}\) — và nếu vật cản ở quá xa (4 m), chờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pulseIn()&lt;/code&gt; không đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;timeout&lt;/code&gt; có thể làm kẹt vòng lặp chính tới hàng chục mili-giây!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Capstone A cần một cảm biến phát hiện vật cản phía trước và (nếu muốn kiểm chứng định lượng tốt hơn) encoder đếm vòng quay bánh xe. Cảm biến siêu âm HC-SR04 hoặc laser ToF VL53L0X đảm nhiệm việc đo khoảng cách; động cơ giảm tốc có encoder từ tính đảm nhiệm việc đếm xung. IMU chưa bắt buộc cho lần chạy teleop đầu tiên và sẽ được dùng sau cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Imu&lt;/code&gt;. Riêng camera thuộc phạm vi Chương 11 vì đòi hỏi băng thông USB và năng lực xử lý vượt quá khả năng của ESP32 thuần; mua camera quá sớm khi robot chưa dừng an toàn được chỉ gây lãng phí ngân sách.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sensor_package_id.png&quot; alt=&quot;Phiếu nhận diện module cảm biến&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;cách-nhận-diện-bằng-mắt-thường&quot;&gt;Cách nhận diện bằng mắt thường&lt;/h2&gt;

&lt;p&gt;Hãy quan sát theo đúng trình tự: hình dáng vật lý (vỏ kim loại, cửa sổ quang học, số chân), sau đó đọc chữ in cạnh hàng chân, cuối cùng tra điện áp và giao thức trong datasheet. Một module có hình dáng tương tự HC-SR04 nhưng có 5 chân có thể là RCWL-1601 hoặc US-100 hỗ trợ UART — việc sao chép mã nguồn mẫu mà không kiểm tra nhãn chân sẽ không hoạt động.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;HC-SR04:&lt;/strong&gt; có hai đầu thu phát siêu âm vỏ nhôm hình trụ gắn trên bo mạch nhỏ với bốn chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRIG&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;. Module dùng nguồn 5 V; chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRIG&lt;/code&gt; nhận xung kích khoảng 10 µs, còn chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; xuất xung có độ rộng tỉ lệ với thời gian sóng âm đi và về. Trên bản phổ biến, mức cao của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; là 5 V, vượt giới hạn chịu áp của ESP32 nên phải hạ áp qua cầu chia (1 kΩ từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; tới GPIO, 2 kΩ từ GPIO xuống mass để đưa về khoảng 3,3 V). Chùm sóng âm có góc mở khá rộng (khoảng 15°), nên bề mặt cứng như khung cửa và bề mặt mềm như rèm vải sẽ cho kết quả rất khác nhau.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;VL53L0X và họ cảm biến ToF:&lt;/strong&gt; đặc trưng bởi cửa sổ quang màu đen rất nhỏ (thường đặt trên bo mạch breakout màu tím), gồm các chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIN&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt;, đôi khi có thêm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO1&lt;/code&gt;. Giao tiếp bằng I2C với địa chỉ mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;, trả về trực tiếp khoảng cách tính bằng milimét do chip tự tính toán. Nhiều bo breakout có sẵn ổn áp nên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIN&lt;/code&gt; nhận 3,3–5 V, nhưng mức logic I2C vẫn phải khớp với vi điều khiển. Khi dùng hai module VL53L0X trên cùng bus, chúng sẽ trùng địa chỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt; trừ khi dùng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt; để bật từng con và gán địa chỉ mới.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;MPU-6050 / GY-521:&lt;/strong&gt; bo mạch phẳng nhỏ (thường màu xanh dương) với hàng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XDA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XCL&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INT&lt;/code&gt;, giao tiếp I2C ở địa chỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt; tùy mức chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt;. Đây là cảm biến quán tính gồm gia tốc kế và con quay hồi chuyển, không dùng để đo khoảng cách.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cảm biến hồng ngoại phản xạ / vật cản:&lt;/strong&gt; gồm một LED phát trong suốt và một phototransistor thu màu đen đặt cạnh nhau, thường có ba chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OUT&lt;/code&gt; (hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO&lt;/code&gt;). Loại ngõ ra số chuyển mức khi ánh sáng phản xạ vượt ngưỡng chỉnh bằng biến trở; loại ngõ ra tương tự (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO&lt;/code&gt;) cần nối vào chân ADC. Cảm biến dò line TCRT5000 cũng thuộc nhóm này — chúng không phải cảm biến ToF laser và dễ bị bão hòa dưới ánh nắng mạnh.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Encoder bánh xe:&lt;/strong&gt; thường gặp hai dạng là đĩa có rãnh đi qua cặp thu phát quang, hoặc đĩa nam châm nhỏ gắn phía sau trục động cơ TT vàng đi kèm cảm biến Hall. Loại có hai kênh tín hiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; (quadrature) cho biết cả tốc độ lẫn chiều quay; loại chỉ có một kênh tín hiệu chỉ đo được tốc độ và không tự phân biệt được chiều tiến hay lùi.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Camera:&lt;/strong&gt; nhận diện dễ dàng qua cáp dẹt mềm bản rộng (FPC) cắm trên bo mạch như ESP32-CAM (OV2640), hoặc cáp USB của webcam chuẩn UVC. Webcam USB kết nối với máy tính chủ (laptop/Pi), không nối vào GPIO. ESP32-CAM không phù hợp làm bộ điều khiển chính cho Capstone A vì camera chiếm phần lớn chân GPIO, mạch ổn áp dễ sụt nguồn và không thuận tiện ghép thêm mạch cầu H động cơ.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay-hạ-áp-chân-echo-5-v&quot;&gt;Ví dụ tính tay: hạ áp chân Echo 5 V&lt;/h2&gt;

&lt;p&gt;Khi phát xung phản hồi, chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; của HC-SR04 ở mức 5 V. Mắc cầu chia áp gồm \(R_1 = 1~\mathrm{k}\Omega\) từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; tới chân ESP32 và \(R_2 = 2~\mathrm{k}\Omega\) từ chân đó xuống mass, điện áp tại chân vi điều khiển là:&lt;/p&gt;

\[V_{pin} = 5 \times \frac{R_2}{R_1 + R_2} = 5 \times \frac{2}{3} \approx 3{,}33~\mathrm{V}.\]

&lt;p&gt;Mức 3,33 V nằm trong giới hạn an toàn của ESP32. Bỏ qua cầu chia áp sẽ khiến chân GPIO chịu quá áp, nóng lên và hỏng. Ngược lại, VL53L0X dùng bus I2C 3,3 V không cần cầu chia này nếu bo mạch breakout đã hỗ trợ mức logic 3,3 V. Mỗi loại module có yêu cầu điện riêng và cần ghi chú rõ trên phiếu nhận diện.&lt;/p&gt;

&lt;h2 id=&quot;lab-lập-phiếu-nhận-diện-một-trang&quot;&gt;Lab: lập phiếu nhận diện một trang&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Thực hiện bước nhận diện khi chưa cấp điện. Khi cấp nguồn kiểm tra, chỉ cấp qua cổng USB cho vi điều khiển; không nối đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; 5 V của cảm biến vào chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; của ESP32 và không nối chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; vào GPIO khi chưa có cầu chia áp.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Bất kỳ hai loại nào trong: HC-SR04, VL53L0X, GY-521, module IR, động cơ encoder, webcam&lt;/td&gt;
      &lt;td&gt;Linh kiện thực tế cần nhận diện&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điện thoại chụp ảnh&lt;/td&gt;
      &lt;td&gt;Lưu hình vào phiếu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ vạn năng&lt;/td&gt;
      &lt;td&gt;Đo kiểm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; sau khi cấp nguồn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Datasheet tương ứng&lt;/td&gt;
      &lt;td&gt;Xác minh điện áp và chuẩn giao tiếp&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Khi chưa cấp nguồn, chụp cận cảnh từng module sao cho đọc rõ nhãn in tại hàng chân.&lt;/li&gt;
  &lt;li&gt;Ghi vào bảng nháp cho mỗi module: tên gọi, các nhãn chân nhìn thấy, chuẩn giao tiếp dự đoán, điện áp dự đoán và phân loại (“dùng ngay cho Capstone A” hoặc “để sau”).&lt;/li&gt;
  &lt;li&gt;Mở datasheet hoặc sơ đồ của nhà sản xuất để kiểm tra lại, sửa và đánh dấu rõ các điểm ban đầu dự đoán sai.&lt;/li&gt;
  &lt;li&gt;Nếu đã có module I2C 3,3 V, cấp nguồn và kiểm tra lại như bài 04-01; nếu linh kiện đang vận chuyển, dừng ở bước hoàn thiện phiếu giấy.&lt;/li&gt;
  &lt;li&gt;Lưu phiếu vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; để đối chiếu lại ở Chương 07.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Tối thiểu hai module được xác minh chính xác điện áp và chuẩn bus; có câu ghi chú rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; của HC-SR04 ở mức 5 V và VL53L0X dùng I2C tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;; nêu rõ lựa chọn cảm biến khoảng cách và quyết định hoãn camera sang chương sau.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Nhầm lẫn&lt;/th&gt;
      &lt;th&gt;Hệ quả&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Thấy cửa sổ đen nhỏ đều tưởng là IMU&lt;/td&gt;
      &lt;td&gt;Quét I2C nhầm cảm biến khoảng cách, hoặc cố đọc mét từ con quay hồi chuyển&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đưa thẳng 5 V vào chân ESP32&lt;/td&gt;
      &lt;td&gt;Chân GPIO quá nhiệt, treo trạng thái hoặc hỏng vĩnh viễn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nối hai bo VL53L0X mà không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Quét chỉ thấy một địa chỉ hoặc xung đột bus&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dùng encoder một kênh&lt;/td&gt;
      &lt;td&gt;Không tự phân biệt được chiều tiến và lùi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chọn ESP32-CAM làm bộ điều khiển Capstone A&lt;/td&gt;
      &lt;td&gt;Thiếu chân điều khiển động cơ và dễ sụt nguồn&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Ưu tiên mua cảm biến khoảng cách và (nếu ngân sách cho phép) cặp động cơ TT có gắn sẵn encoder; để dành camera cho Chương 11.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Thay thế&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cảm biến siêu âm HC-SR04&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-sieu-am-srf04&quot;&gt;Hshop HC-SR04&lt;/a&gt; từng niêm yết khoảng 20.000. Chỉ thay bằng US-100 khi đã kiểm tra rõ chế độ UART/GPIO.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Module VL53L0X&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VL53L0X ToF&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-tof-laser-radar-vl53l0x&quot;&gt;Hshop VL53L0X&lt;/a&gt;. Lưu ý VL53L1X đo xa hơn nhưng dùng thư viện khác.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GY-521&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;MPU6500 nếu mã mẫu tương thích thanh ghi.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;IR vật cản hoặc TCRT5000&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cảm biến hồng ngoại vật cản&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;8.000–25.000&lt;/td&gt;
      &lt;td&gt;Phù hợp dò line, không đủ tin cậy làm cảm biến tránh va chạm duy nhất.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Motor TT + encoder&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ TT encoder Hall&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;40.000–90.000 một cái&lt;/td&gt;
      &lt;td&gt;Có thể dùng N20 hộp số kim loại có encoder nếu khớp lỗ gá khung (kiểm tra đường kính trục 3 mm hay 4 mm).&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trở cầu chia&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;điện trở 1k 2k&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;10.000–20.000 một thanh&lt;/td&gt;
      &lt;td&gt;Thế Giới IC phù hợp mua điện trở rời.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Liên kết tìm kiếm nhanh:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=HC-SR04&quot;&gt;Hshop, từ khóa HC-SR04&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=dong%20co%20TT%20encoder&quot;&gt;Shopee, motor encoder&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=VL53L0X&quot;&gt;Lazada, VL53L0X&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=HC-SR04&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=n-gJ00GTsNg&quot;&gt;HC-SR04 Ultrasonic Distance Sensor and Arduino (Lesson #9)&lt;/a&gt; — Science Buddies, tiếng Anh. Giải thích nguyên lý hai chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRIG&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; của HC-SR04 (không phải I2C).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=qS9GwaekLW4&quot;&gt;What is an Inertial Measurement Unit (IMU)?&lt;/a&gt; — Phidgets Inc., tiếng Anh. Tổng quan cấu tạo IMU gồm gia tốc kế và con quay hồi chuyển.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=v4BbSzJ-hz4&quot;&gt;How Rotary Encoder Works and How To Use It with Arduino&lt;/a&gt; — How To Mechatronics, tiếng Anh. Minh họa cấu tạo hai kênh của encoder quay, phân biệt với biến trở.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một bo mạch có hai đầu hình trụ bằng nhôm cùng bốn chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRIG&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;. Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; xuất mức cao bao nhiêu vôn và cần thêm cặp điện trở nào khi nối với ESP32?&lt;/li&gt;
  &lt;li&gt;Một bo mạch màu tím có cửa sổ quang nhỏ màu đen cùng hai chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt;. Đây thuộc dòng cảm biến nào và địa chỉ I2C mặc định là gì?&lt;/li&gt;
  &lt;li&gt;Bạn cần biết bánh trái đang quay tiến hay quay lùi. Một cảm biến Hall đơn kênh có đủ không? Bạn cần chọn loại nào?&lt;/li&gt;
  &lt;li&gt;Phân loại các linh kiện sau vào hai nhóm “cần trước Capstone A” và “để sau khi robot đã chạy teleop”: HC-SR04, camera USB, động cơ có encoder, ESP32-CAM, VL53L0X.&lt;/li&gt;
  &lt;li&gt;Hai module VL53L0X đều có địa chỉ mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. Chân nào dùng để xử lý xung đột địa chỉ, và kết quả quét sẽ ra sao nếu chưa sử dụng chân đó?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Khoảng 5 V; cần cầu chia áp 1 kΩ (từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; tới GPIO) và 2 kΩ (từ GPIO xuống mass) để hạ xuống xấp xỉ 3,33 V.&lt;/li&gt;
  &lt;li&gt;Cảm biến khoảng cách ToF (như VL53L0X), địa chỉ mặc định thường là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Không đủ; cần encoder hai kênh lệch pha (quadrature A và B), nếu không thì chiều quay chỉ suy đoán từ lệnh điều khiển động cơ và không phát hiện được lúc bánh tự trôi.&lt;/li&gt;
  &lt;li&gt;Trước Capstone A: HC-SR04 hoặc VL53L0X, và động cơ có encoder (nếu muốn đo quãng đường). Để sau: camera USB và ESP32-CAM.&lt;/li&gt;
  &lt;li&gt;Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt;. Nếu không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt; để tắt/bật riêng từng chip và đổi địa chỉ, quá trình quét sẽ bị xung đột (chỉ hiện một địa chỉ hoặc lỗi bus).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf&quot;&gt;Datasheet HC-SR04 (bản SparkFun lưu)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/vl53l0x.pdf&quot;&gt;Datasheet ST VL53L0X&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf&quot;&gt;Datasheet TDK MPU-6000/6050&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/2490&quot;&gt;Ghi chú board Pololu VL53L0X&lt;/a&gt; — thông tin về địa chỉ và mức logic có thể tham khảo cho cả các bo breakout khác.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Giao diện cảm biến: I2C, SPI, UART</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_01_bus_interfaces/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter04/04_01_bus_interfaces</id>
   <content type="html">&lt;p&gt;Thời lượng: &lt;strong&gt;70–90 phút&lt;/strong&gt;, bao gồm một lần quét địa chỉ I2C trên mạch thật. Nếu module chưa giao tới, bạn vẫn có thể hoàn thành bài bằng cách thực hành chuẩn bị (dry-run) đúng trình tự kiểm tra điện.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn có thể chọn đúng chuẩn giao tiếp I2C, SPI hay UART dựa trên datasheet của cảm biến thay vì chọn theo thói quen. Bạn hiểu khi nào bus cực thu hở (open-drain) cần điện trở kéo lên và luôn chạy quét địa chỉ (chẳng hạn thu được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt;) trước khi tin vào thư viện. Bạn cũng giải thích được vì sao điều khiển từ xa (teleop) của Capstone A sử dụng UART, trong khi IMU và cảm biến ToF trên cùng robot có thể dùng chung một bus I2C, và vì sao một topic ROS 2 không thể thay thế cho việc đấu nối phần cứng chính xác.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã biết đo thông mạch và điện áp một chiều bằng đồng hồ vạn năng (Chương 01), nắm được các chân GPIO 3,3 V trên ESP32 hoặc Pico (Chương 02), và đã nạp thành công chương trình nháy LED (Chương 03). Bài này chưa cần cài ROS.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Một con robot hiện đại mang trên mình hàng loạt “giác quan”: gia tốc kế–con quay hồi chuyển (IMU), cảm biến khoảng cách laser (ToF), màn hình OLED, bộ thu GPS, LiDAR… Nếu mỗi cảm biến đòi riêng 8 sợi dây song song thì bo vi điều khiển sẽ hết sạch chân ngay từ cảm biến thứ hai! Ba chuẩn truyền thông nối tiếp kinh điển — &lt;strong&gt;I2C&lt;/strong&gt;, &lt;strong&gt;SPI&lt;/strong&gt; và &lt;strong&gt;UART&lt;/strong&gt; — ra đời để chia sẻ hàng chục thiết bị trên chỉ 2 đến 4 sợi dây mỏng, mỗi chuẩn tối ưu cho một bài toán tốc độ, khoảng cách và số lượng thiết bị khác nhau.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-chuẩn-bus-cảm-biến-cốt-lõi-trên-robot&quot;&gt;Kiến thức cần nhớ: Ba Chuẩn Bus Cảm biến Cốt lõi trên Robot&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;I2C (Inter-Integrated Circuit — 2 dây &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA, SCL&lt;/code&gt;):&lt;/strong&gt; Bus đồng bộ dùng chung cho nhiều cảm biến bằng &lt;strong&gt;địa chỉ 7-bit&lt;/strong&gt; (ví dụ IMU &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt;, OLED &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x3C&lt;/code&gt;), tốc độ chuẩn 100 kHz hoặc 400 kHz. Vì là chuẩn cực máng hở (&lt;em&gt;open-drain&lt;/em&gt;), bus I2C &lt;strong&gt;bắt buộc phải có điện trở kéo lên&lt;/strong&gt; (thường \(4.7\text{ k}\Omega\) lên 3,3 V).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SPI (Serial Peripheral Interface — 4 dây &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MOSI, MISO, SCLK, CS&lt;/code&gt;):&lt;/strong&gt; Bus đồng bộ song công toàn phần tốc độ cực cao (1–40 MHz!), không cần địa chỉ mà chọn từng thiết bị bằng chân riêng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CS&lt;/code&gt; (Chip Select) — lý tưởng cho màn hình màu, thẻ nhớ SD và IMU tốc độ lấy mẫu kHz.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;UART (Universal Asynchronous Receiver/Transmitter — 2 dây chéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX ↔ RX&lt;/code&gt;):&lt;/strong&gt; Truyền bất đồng bộ điểm–điểm không cần dây xung nhịp (hai bên tự thống nhất tốc độ baud như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200 bps&lt;/code&gt;) — dùng cho GPS, LiDAR 2D và giao tiếp giữa ESP32 với máy tính Raspberry Pi!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-thời-gian-đọc-14-byte-từ-imu-mpu6050-qua-i2c-400-khz-vs-spi-4-mhz&quot;&gt;Ví dụ làm từng bước: Tính thời gian đọc 14 byte từ IMU MPU6050 qua I2C (400 kHz) vs. SPI (4 MHz)&lt;/h3&gt;

&lt;p&gt;Một lần đọc đầy đủ gia tốc (6 byte) + nhiệt độ (2 byte) + vận tốc góc (6 byte) từ IMU cần truyền &lt;strong&gt;14 byte&lt;/strong&gt; dữ liệu:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Qua I2C Fast Mode (\(400\text{ kHz}\)):&lt;/strong&gt; mỗi byte trên I2C cần &lt;strong&gt;9 xung clock&lt;/strong&gt; (8 bit dữ liệu + 1 bit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ACK&lt;/code&gt;), cộng khoảng 3 khung mở đầu (địa chỉ ghi + thanh ghi bắt đầu + địa chỉ đọc) = \(17 \times 9 = \mathbf{153\text{ xung clock}}\):
\(t_{\text{I2C}} \approx \frac{153}{400{,}000\text{ Hz}} \approx \mathbf{0.383\text{ ms}} \quad (\text{dư sức cho vòng lặp 100–200 Hz!}).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Qua SPI (\(4\text{ MHz}\)):&lt;/strong&gt; truyền 1 byte địa chỉ + 14 byte dữ liệu = \(15 \times 8 = \mathbf{120\text{ xung clock}}\):
\(t_{\text{SPI}} = \frac{120}{4{,}000{,}000\text{ Hz}} = \mathbf{0.030\text{ ms}} \quad (\mathbf{12.7\times\text{ nhanh hơn I2C!}}).\)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-bước-đấu-nối-và-gỡ-lỗi-bus-cảm-biến-không-bao-giờ-đoán-mò&quot;&gt;Giải thích: 4 bước đấu nối và gỡ lỗi Bus Cảm biến không bao giờ đoán mò&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I2C Scanner&lt;/code&gt; trước khi viết bất kỳ dòng code giải mã nào:&lt;/strong&gt; Nếu chương trình quét địa chỉ báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Found 0x68&lt;/code&gt; (IMU) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Found 0x29&lt;/code&gt; (ToF VL53L0X), bạn chắc chắn 100% dây &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA/SCL&lt;/code&gt;, nguồn và điện trở kéo lên đều đúng!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Đổi địa chỉ khi hai cảm biến I2C trùng nhau:&lt;/strong&gt; Ví dụ muốn gắn 2 cảm biến MPU6050 trên cùng 1 bus I2C, kéo chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt; của con thứ hai lên 3,3 V để đổi địa chỉ từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Nhớ quy tắc đấu chéo của UART (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX → RX&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RX → TX&lt;/code&gt;):&lt;/strong&gt; Khác với I2C (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA → SDA&lt;/code&gt;) và SPI (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MOSI → MOSI&lt;/code&gt;), UART phải nối chân phát của bên này vào chân thu của bên kia — và &lt;em&gt;luôn&lt;/em&gt; nối chung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Giữ dây I2C/SPI ngắn (&amp;lt; 20 cm) và tránh đi song song sát dây động cơ:&lt;/strong&gt; Dây I2C dài quá làm tăng điện dung ký sinh làm méo cạnh xung, dễ gây treo bus khi động cơ phát nhiễu điện từ!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu bạn mắc &lt;strong&gt;hai&lt;/strong&gt; cảm biến giống hệt nhau có cùng địa chỉ I2C cố định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt; (như hai cảm biến laser VL53L0X) lên cùng một bus &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA/SCL&lt;/code&gt; mà không tách chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt; để đổi địa chỉ phần mềm hoặc dùng bộ chia kênh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TCA9548A&lt;/code&gt;, chuyện gì sẽ xảy ra? Cả hai cảm biến cùng kéo đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt; trả lời đồng thời gây &lt;strong&gt;xung đột bus (bus contention)&lt;/strong&gt; và trả về dữ liệu sai hoàn toàn!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng-với-capstone-a-và-ros&quot;&gt;Vì sao bài này quan trọng với Capstone A và ROS&lt;/h2&gt;

&lt;p&gt;Capstone A ở Chương 07 điều khiển robot vi sai từ laptop qua cổng serial — đó chính là giao tiếp UART với tốc độ baud cố định, cặp chân TX/RX đã được bắt chéo bên trong chip USB-serial và quy tắc tự dừng khi mất lệnh. Khi cơ chế dừng an toàn đã chạy ổn định, ta mới gắn thêm IMU và cảm biến ToF (thường là VL53L0X) dùng chung bus I2C. Đến Chương 09, các số đo này sẽ được xuất thành bản tin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Imu&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Range&lt;/code&gt; (hoặc đưa qua micro-ROS ở Chương 10). Nếu bus thiếu điện trở kéo lên hoặc sai địa chỉ, đồ thị ROS sẽ không có dữ liệu — nguyên nhân nằm ở phần đấu dây breadboard chứ không phải tệp launch.&lt;/p&gt;

&lt;h2 id=&quot;ba-chuẩn-bus-và-nguyên-tắc-giao-tiếp&quot;&gt;Ba chuẩn bus và nguyên tắc giao tiếp&lt;/h2&gt;

&lt;p&gt;Mỗi cảm biến số thực chất là một vi mạch giao tiếp theo giao thức được quy định sẵn trong datasheet. Nhiệm vụ của bạn là khớp đúng điện áp, sơ đồ dây và tốc độ, rồi xác nhận cảm biến có phản hồi trước khi đọc số đo khoảng cách hay góc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I2C&lt;/strong&gt; sử dụng hai đường tín hiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt; (dữ liệu) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt; (xung nhịp), cùng dây nguồn và mass. Cả hai đường đều thuộc kiểu cực thu hở (open-drain): thiết bị chỉ kéo dây xuống mức thấp, còn điện trở kéo lên (pull-up) sẽ đưa dây về mức cao khi không có thiết bị nào truyền. Nhiều bo mạch breakout đã hàn sẵn điện trở kéo lên 4,7 kΩ hoặc 10 kΩ lên mức logic của mạch đó; khi ghép nhiều module song song, điện trở kéo tương đương giảm xuống (kéo mạnh hơn). Điện trở kéo lên nội của ESP32 khá yếu (vài chục kΩ), không đủ tin cậy ở tốc độ 400 kHz. Trên bus 3,3 V dây ngắn, giá trị khởi đầu thực tế là điện trở ngoài 4,7 kΩ kéo lên 3,3 V nếu module chưa tích hợp sẵn.&lt;/p&gt;

&lt;p&gt;Mỗi thiết bị I2C có một địa chỉ 7-bit. Ví dụ, MPU-6050 dùng địa chỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; khi chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt; nối mass và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt; khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt; kéo lên mức cao; VL53L0X mặc định ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. Master gọi địa chỉ, slave phản hồi xác nhận (ACK) bằng cách kéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt; xuống thấp, sau đó việc đọc thanh ghi mới hợp lệ. Tốc độ chế độ chuẩn (Standard Mode) là 100 kHz và chế độ nhanh (Fast Mode) là 400 kHz; không nên bắt đầu ở 1 MHz ngay lập tức.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SPI&lt;/strong&gt; sử dụng bốn đường tín hiệu gồm xung nhịp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCLK&lt;/code&gt;, dữ liệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MOSI&lt;/code&gt; (Master Out Slave In), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MISO&lt;/code&gt; (Master In Slave Out) và chân chọn chip &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CS&lt;/code&gt; riêng cho từng thiết bị, cùng nguồn và mass. SPI truyền song công (full-duplex) và tốc độ cao hơn nhiều nên một số IMU hỗ trợ cả SPI lẫn I2C. Chế độ SPI (Mode 0–3) xác định bởi cặp bit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CPOL&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CPHA&lt;/code&gt; (Mode 0 có xung nhịp nghỉ ở mức thấp và lấy mẫu ở cạnh lên); chọn sai mode sẽ khiến cảm biến trả về dữ liệu vô nghĩa dù đấu dây đúng. SPI không dùng địa chỉ dạng byte — chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CS&lt;/code&gt; chính là tín hiệu chọn thiết bị, và phải giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CS&lt;/code&gt; ở mức cao đối với các thiết bị không được truy cập.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UART&lt;/strong&gt; truyền không đồng bộ (không có dây xung nhịp). Hai bên phải thống nhất tốc độ baud (Capstone A dùng 115200), cấu hình khung truyền (thường là 8N1: 8 bit dữ liệu, không parity, 1 bit dừng) và bắt chéo dây: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; bên này nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RX&lt;/code&gt; bên kia. Trên bo mạch có chip USB-serial, việc bắt chéo này đã làm sẵn bên trong mạch nên Serial Monitor hoạt động ngay; nhưng khi nối trực tiếp hai bo mạch với nhau, nối nhầm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; sẽ làm mất giao tiếp. Hai thiết bị vẫn bắt buộc nối chung mass và mỗi đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; chỉ nên có một thiết bị phát.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/bus_i2c_spi_uart.png&quot; alt=&quot;Ba hợp đồng nối dây I2C, SPI và UART&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-và-đấu-nối-một-bus-hai-cảm-biến&quot;&gt;Ví dụ tính và đấu nối: một bus, hai cảm biến&lt;/h2&gt;

&lt;p&gt;Giả sử bạn cần gắn cả GY-521 (MPU-6050) và VL53L0X lên ESP32 DevKit. Cả hai đều hỗ trợ I2C và hoạt động với logic 3,3 V. Bạn nối chung chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt; của cả hai vào GPIO 21, chung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt; vào GPIO 22 (đối chiếu lại nhãn mạch vì một số biến thể có thể khác), nối chung &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; với ESP32 và cấp nguồn 3,3 V. Không tự ý cấp 5 V cho module VL53L0X nếu chưa kiểm tra rõ bo mạch có mạch ổn áp và chuyển mức trên board hay không.&lt;/p&gt;

&lt;p&gt;Khi chạy chương trình quét I2C, kết quả phải hiện đủ hai địa chỉ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;). Nếu chỉ thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt;, cần kiểm tra nguồn của bo mạch ToF, xem dây &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt; có bị đảo hoặc địa chỉ có bị thay đổi hay không. Nếu không thấy địa chỉ nào, hãy kiểm tra dây mass chung trước khi nghi ngờ thư viện.&lt;/p&gt;

&lt;p&gt;Cổng USB của ESP32 sử dụng một bộ UART riêng (dùng cho teleop về sau), hoàn toàn tách biệt với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt;. Do đó bạn có thể vừa quét bus I2C vừa in kết quả lên Serial Monitor — mô hình tách biệt “I2C cho cảm biến trên xe, UART cho giao tiếp với máy tính” này sẽ được giữ nguyên trong Capstone A.&lt;/p&gt;

&lt;h2 id=&quot;lab-quét-địa-chỉ-trước-khi-giải-mã&quot;&gt;Lab: quét địa chỉ trước khi giải mã&lt;/h2&gt;

&lt;h3 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h3&gt;

&lt;p&gt;Các module trong bài chỉ tiêu thụ vài chục miliampe. Không gắn mạch cầu H động cơ chung trên cùng thanh nguồn breadboard. Nếu cảm biến nóng lên sau vài giây cấp điện, rút cáp USB ngay vì khả năng cao đã đấu ngược &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Vai trò&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 devkit hoặc Raspberry Pi Pico&lt;/td&gt;
      &lt;td&gt;Master I2C&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GY-521 MPU-6050, hoặc bất kỳ module I2C bạn đang có&lt;/td&gt;
      &lt;td&gt;Thiết bị có địa chỉ xác định&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điện trở 4,7 kΩ ×2&lt;/td&gt;
      &lt;td&gt;Điện trở kéo lên (nếu module chưa hàn sẵn)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Breadboard và dây dupont&lt;/td&gt;
      &lt;td&gt;Tạo bus dây ngắn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ vạn năng&lt;/td&gt;
      &lt;td&gt;Đo thông mạch GND và kiểm tra điện áp VCC&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;các-bước&quot;&gt;Các bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Đo thông mạch giữa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; của module và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; của vi điều khiển trước khi cấp nguồn.&lt;/li&gt;
  &lt;li&gt;Cắm USB và đo điện áp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; ngay tại chân module; bảo đảm đạt khoảng 3,3 V (không phải 5 V đối với breakout chỉ chịu 3,3 V).&lt;/li&gt;
  &lt;li&gt;Nạp chương trình quét I2C. Với ESP32 trên nền tảng Arduino:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;Wire.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Wire&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// SDA, SCL — khớp board của bạn&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;scan&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint8_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;127&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Wire&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;beginTransmission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Wire&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endTransmission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ACK 0x%02X&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trên Pico dùng MicroPython, thao tác tương đương là khởi tạo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I2C(0, sda=Pin(0), scl=Pin(1), freq=100_000)&lt;/code&gt; rồi gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i2c.scan()&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Ghi lại các địa chỉ quét được vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; kèm ảnh chụp cách đấu dây.&lt;/li&gt;
  &lt;li&gt;Nếu có module I2C thứ hai, gắn thêm vào bus và xác nhận cả hai địa chỉ đều phản hồi ACK. Hai thiết bị trùng địa chỉ sẽ gây xung đột trên bus — cần xử lý theo datasheet trước khi nghĩ đến phần mềm.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;kết-quả-mong-đợi&quot;&gt;Kết quả mong đợi&lt;/h3&gt;

&lt;p&gt;Với một module: quét ra một địa chỉ ổn định (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; cho GY-521 khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt; nối mass). Với hai module khác địa chỉ: quét ra đủ hai địa chỉ. Sau khi rút và cắm lại USB, kết quả quét vẫn giữ nguyên.&lt;/p&gt;

&lt;h3 id=&quot;lỗi-thường-gặp&quot;&gt;Lỗi thường gặp&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Không nhận được ACK&lt;/td&gt;
      &lt;td&gt;Chưa nối chung mass, đảo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt;, hoặc mất nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Địa chỉ lúc hiện lúc mất&lt;/td&gt;
      &lt;td&gt;Dây dupont tiếp xúc lỏng, hoặc kéo lên sai mức điện áp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mọi địa chỉ đều báo ACK&lt;/td&gt;
      &lt;td&gt;Đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt; bị chập xuống mass&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;100 kHz chạy được, 400 kHz lỗi&lt;/td&gt;
      &lt;td&gt;Dây quá dài hoặc thiếu điện trở kéo lên phù hợp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Quét thấy địa chỉ nhưng thư viện báo không tìm thấy chip&lt;/td&gt;
      &lt;td&gt;Thư viện cấu hình sai địa chỉ hoặc sai cặp chân I2C&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bạn cần ít nhất một module I2C để hoàn thành bài lab và vài điện trở 4,7 kΩ nếu chưa có sẵn. Mạch chuyển mức logic (loại dùng MOSFET BSS138, 4 hoặc 8 kênh) nên mua sẵn cho các bài sau khi ghép ngoại vi 5 V với ESP32 3,3 V, nhưng chưa bắt buộc cho GY-521 chạy 3,3 V.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Món&lt;/th&gt;
      &lt;th&gt;Từ khóa&lt;/th&gt;
      &lt;th&gt;Khoảng giá (VND)&lt;/th&gt;
      &lt;th&gt;Ghi chú&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;GY-521 MPU-6050&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;Trang Hshop&lt;/a&gt; từng niêm yết khoảng 85.000. Có thể thay bằng MPU6500 nếu thư viện tương thích.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trở 4,7 kΩ&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;điện trở 4.7k 1/4W&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;10.000–25.000 một thanh&lt;/td&gt;
      &lt;td&gt;Thế Giới IC phù hợp để mua điện trở rời.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạch dịch mức&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;logic level shifter 3.3 5&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;8.000–25.000&lt;/td&gt;
      &lt;td&gt;Chỉ dùng cho đường tín hiệu, không dùng làm nguồn công suất.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dây dupont&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây dupont đực cái&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nên tìm trực tiếp bằng từ khóa trên trang bán hàng uy tín:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=MPU6050&quot;&gt;Tìm trên Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=MPU6050%20GY-521&quot;&gt;Tìm trên Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=MPU6050&quot;&gt;Tìm trên Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=tr%E1%BB%9F%20k%C3%A9o&quot;&gt;Tìm trên Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Giá thị trường thay đổi theo thời điểm; hãy so sánh vài nhà bán và ưu tiên module in rõ ký hiệu chân trên bo mạch.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=IyGwvGzrqp8&quot;&gt;Serial Communications Explained: UART, I2C, and SPI&lt;/a&gt; — Electronoobs, tiếng Anh. So sánh trực quan UART, I2C và SPI theo số dây và cơ chế xung nhịp, khớp với ba chuẩn giao tiếp trong bài.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một module in nhãn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; kèm dòng chữ “I2C 0x29”. Đây là chuẩn bus nào, có thể cần thêm linh kiện gì nếu mạch chưa hàn sẵn, và chương trình quét phải in ra kết quả gì?&lt;/li&gt;
  &lt;li&gt;Chương trình quét I2C không trả về địa chỉ nào. Nêu ba bước kiểm tra phần cứng cần làm theo đúng thứ tự trước khi sửa mã nguồn.&lt;/li&gt;
  &lt;li&gt;Hai bo mạch GY-521 đều nối chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt; xuống mass và gắn chung trên một bus I2C. Điều gì sẽ xảy ra, và cách khắc phục đơn giản nhất là gì?&lt;/li&gt;
  &lt;li&gt;Hệ thống teleop Capstone A dùng chuẩn 115200, 8N1. Nếu nối trực tiếp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; của bộ chuyển đổi USB-serial vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; của ESP32, ESP32 sẽ nhận được gì? Khi nối UART rời giữa hai bo mạch cần đấu dây thế nào?&lt;/li&gt;
  &lt;li&gt;Vì sao kiểm tra lỗi “topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/imu/data&lt;/code&gt; không có dữ liệu” trên ROS không phải là bước gỡ lỗi đầu tiên đối với một robot chưa từng quét bus I2C?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gợi-ý-đáp-án&quot;&gt;Gợi ý đáp án&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Chuẩn I2C; có thể cần điện trở kéo lên (4,7 kΩ lên 3,3 V) nếu module chưa tích hợp sẵn; chương trình quét phải in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Kiểm tra lần lượt: nối chung mass (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;), điện áp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; có mặt và đúng định mức, dây &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt; không bị đấu ngược.&lt;/li&gt;
  &lt;li&gt;Cả hai xung đột tại cùng địa chỉ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt;. Cách khắc phục đơn giản nhất là kéo chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AD0&lt;/code&gt; của một module lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; để đổi địa chỉ sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt;, rồi cấu hình lại địa chỉ tương ứng trong mã nguồn.&lt;/li&gt;
  &lt;li&gt;ESP32 không nhận được dữ liệu hợp lệ vì hai đầu phát nối vào nhau. Khi nối UART rời giữa hai bo mạch, phải bắt chéo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RX&lt;/code&gt; và nối chung mass (riêng cáp USB tích hợp trên devkit đã bắt chéo sẵn).&lt;/li&gt;
  &lt;li&gt;Vì có khả năng cảm biến chưa hề giao tiếp được ở tầng phần cứng (chưa ACK). Cần quét xác nhận địa chỉ I2C thành công trước khi kiểm tra tầng xuất dữ liệu ROS.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.nxp.com/docs/en/user-guide/UM10204.pdf&quot;&gt;NXP UM10204, đặc tả bus I2C&lt;/a&gt; — tài liệu chuẩn về cực thu hở, địa chỉ và định thời I2C.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/i2c/all&quot;&gt;Bài I2C của SparkFun&lt;/a&gt; — minh họa chi tiết điện trở kéo lên, địa chỉ và sơ đồ đấu dây.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/all&quot;&gt;Bài SPI của SparkFun&lt;/a&gt; — giải thích các chế độ SPI (CPOL/CPHA) và chân chọn chip &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CS&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/serial-communication/all&quot;&gt;Truyền serial của SparkFun&lt;/a&gt; — tổng quan khung truyền UART và tốc độ baud.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/i2c.html&quot;&gt;Ghi chú driver I2C của Espressif&lt;/a&gt; — đặc tính phần cứng của ngoại vi I2C trên ESP32.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 03</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_06_ch03_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_06_ch03_lab_checklist</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Bài kiểm tra này chốt lại chương 03 bằng các bằng chứng thực tế thay vì đánh dấu hoàn thành theo cảm tính. Bạn cần xuất trình được log serial của nút nhấn, các dòng duty PWM (hoặc ảnh LED ở hai mức độ sáng rõ rệt), kết quả đếm chống dội khớp số lần nhấn với sai số bằng 0, và sơ đồ máy trạng thái có mũi tên quá thời gian ghi rõ 300 ms. Bạn chỉ được ghi câu “PWM bằng không trong FAULT” khi mã nguồn thực sự ép ngõ ra về 0, và biết cách bác bỏ một bản báo cáo đánh dấu đạt PWM trong khi lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; duy nhất chỉ nằm trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Danh sách này kiểm chứng kết quả từ bài 1 đến bài 5: vòng lặp không chặn (thay thế &lt;a href=&quot;/contents/vi/chapter02/02_04_blink_serial_hello/&quot;&gt;nhịp tim chương 02&lt;/a&gt;), nút nhấn kéo lên, PWM bằng LEDC hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;, ISR ngắn gọn và máy trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;. Nếu chưa có phần cứng, bạn chưa được tính đạt chương; chỉ được kiểm tra sơ bộ trên mã nguồn đã viết hoàn chỉnh khi trích dẫn được đúng dòng kiểm tra timeout và dòng ép duty về 0.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Chương 03 là bước ngoặt biến bo mạch thụ động thành một “bộ não nhúng” thực thụ biết đọc nút nhấn chống dội, phát xung PWM không rít, đếm xung ngắt tốc độ cao và chuyển trạng thái có kỷ luật. &lt;strong&gt;Checklist Chương 03&lt;/strong&gt; giúp bạn kiểm chứng độ mượt thời gian thực (&lt;em&gt;jitter&lt;/em&gt;, tốc độ đáp ứng) của toàn bộ tầng firmware trước khi nối các cảm biến bus số I2C/SPI/UART ở Chương 04.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kiểm-chứng-tính-thời-gian-thực-của-firmware-firmware-verification&quot;&gt;Kiến thức cần nhớ: Kiểm chứng Tính Thời gian thực của Firmware (Firmware Verification)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Đo Độ giật Chu kỳ (Loop Jitter):&lt;/strong&gt; Một vòng lặp thiết kế ở 50 Hz (\(20\text{ ms}\)) chỉ đạt chuẩn khi khoảng cách giữa các lần thực thi thực tế tập trung chặt quanh \(20\text{ ms}\) (sai lệch \(&amp;lt; 5\text{–}10\%\)) mà không có gai vọt bất thường.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kiểm chứng Xung &amp;amp; Ngắt Bằng Con số:&lt;/strong&gt; Kiểm tra tỷ lệ mất xung encoder khi quay tay đúng 1 vòng bánh xe (so sánh số xung đếm được với số khe đĩa lý thuyết).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kiểm thử Chuyển Trạng thái &amp;amp; Dừng Khẩn cấp (E-Stop):&lt;/strong&gt; Xác minh mọi mũi tên trong bảng FSM hoạt động đúng như thiết kế và trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESTOP&lt;/code&gt; luôn thắng mọi trạng thái khác.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-soi-nhật-ký-serial-để-phát-hiện-lỗi-độ-giật-jitter-trong-vòng-lặp-50-hz&quot;&gt;Ví dụ làm từng bước: Soi nhật ký Serial để phát hiện lỗi độ giật (Jitter) trong vòng lặp 50 Hz&lt;/h3&gt;

&lt;p&gt;Xét hai đoạn nhật ký dấu thời gian &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms&lt;/code&gt; in ra từ hai bản firmware cùng đặt chu kỳ mục tiêu \(20\text{ ms}\):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Firmware ĐẠT:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms = [1000, 1020, 1041, 1060, 1080]&lt;/code&gt; → các khoảng chu kỳ \(\Delta t = [20,\ 21,\ 19,\ 20]\text{ ms}\). Trung bình \(20.0\text{ ms}\), độ giật cực đại chỉ &lt;strong&gt;\(\pm 1\text{ ms}\) (5%)&lt;/strong&gt;!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Firmware CHƯA ĐẠT:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms = [1000, 1020, 1185, 1205, 1225]&lt;/code&gt; → xuất hiện một gai &lt;strong&gt;\(\Delta t = 165\text{ ms}\)&lt;/strong&gt; (vọt gấp &lt;strong&gt;8,25 lần&lt;/strong&gt; chu kỳ chuẩn!). Nguyên nhân thường là một hàm đọc cảm biến bị treo chờ timeout, một lần in Serial dài làm đầy bộ đệm, hoặc một câu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; bị bỏ quên trong nhánh phụ!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-bước-nghiệm-thu-sạch-tầng-firmware-chương-03&quot;&gt;Giải thích: 4 bước nghiệm thu sạch tầng Firmware Chương 03&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Chạy kiểm thử xoay bánh xe 1 vòng thủ công:&lt;/strong&gt; Đánh dấu 1 vạch bút trắng trên bánh xe, xoay tay đúng 1 vòng \(360^\circ\) và kiểm tra biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks&lt;/code&gt; khớp số xung lý thuyết (ví dụ 20 hoặc 80 xung) với sai số \(\le 1\) xung.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Kiểm tra chống dội bằng 10 lần bấm nhanh liên tiếp:&lt;/strong&gt; Đếm trên Serial phải tăng đúng từ 0 lên 10, không nhảy cóc thành 13 hay 17.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Kiểm tra PWM bằng LED hoặc động cơ ở 3 mức (0%, 50%, 100%):&lt;/strong&gt; Xác minh tần số 20 kHz không rít và mức 0% tắt hẳn hoàn toàn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Thử bấm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESTOP&lt;/code&gt; ngay giữa trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUN&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVOID&lt;/code&gt;:&lt;/strong&gt; Đảm bảo log in chuyển về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESTOP&lt;/code&gt; tức thì và bánh xe cắt điện ngay lập tức.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Khi xoay tay bánh xe đúng &lt;strong&gt;5 vòng&lt;/strong&gt;, encoder có 20 khe (đọc 1 cạnh) lý thuyết phải đếm được \(5 \times 20 = 100\) xung, nhưng firmware chỉ đếm được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;92&lt;/code&gt; xung (mất 8%). Bạn nên kiểm tra gì đầu tiên? Kiểm tra xem có vô tình đặt hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;debounce&lt;/code&gt; quá lớn lên chân encoder, hoặc có đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print()&lt;/code&gt; trong ISR làm trễ mất xung hay không!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Các chương tiếp theo mặc định rằng firmware vi sai của bạn đã nhận lệnh teleop và tự động ngắt cả hai kênh PWM khi mất lệnh 300 ms. Hệ thống ROS ở tầng trên sẽ gửi lệnh vận tốc dựa trên giả định đó và không thể phát hiện ra việc nút nhấn bị nhiễu, duty chỉ ghi một lần khi khởi động, hay trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; quên ghi 0 ra chân PWM. Quy tắc nhấc bánh khỏi bàn vẫn được duy trì cho đến khi toàn bộ các mục kiểm chứng này được ghi nhận đầy đủ trong sổ thực hành.&lt;/p&gt;

&lt;h2 id=&quot;nội-dung&quot;&gt;Nội dung&lt;/h2&gt;

&lt;p&gt;Mỗi mục kiểm tra yêu cầu một kết quả quan sát cụ thể kèm bằng chứng (log hoặc ảnh) trong sổ lab. Bạn chỉ cần thực hiện bằng ngôn ngữ mình chọn cho Capstone A (C++ hoặc MicroPython), nhưng mã nguồn bắt buộc phải tuân theo cấu trúc vòng lặp không chặn ở bài 1; bất kỳ bài nộp nào còn dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; trong vòng điều khiển đều không đạt dù đèn LED có sáng tắt.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Mục nút nhấn:&lt;/strong&gt; dán đoạn log từ bài GPIO thể hiện đúng một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt; và một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt; cho mỗi lần thao tác (không in liên tục mỗi mili giây), ghi kèm số chân GPIO và cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.PULL_UP&lt;/code&gt;. Nếu có thử nghiệm chân nổi trước đó, cần ghi rõ log nộp là kết quả sau khi đã bật kéo lên.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mục PWM:&lt;/strong&gt; ảnh chụp cùng một đèn LED ở hai mức tối và sáng rõ rệt, hoặc ba dòng log serial thể hiện duty khoảng 25%, 50% và 90% theo đúng độ phân giải đã cấu hình (ghi rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach&lt;/code&gt; kèm số bit, hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;). Tải trong ảnh phải là LED nối tiếp điện trở; gắn động cơ trực tiếp vào GPIO là không đạt ngay cả khi động cơ quay.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mục chống dội:&lt;/strong&gt; ghi rõ số lần nhấn thực tế $N$ và số lần bộ đếm in ra khớp nhau với sai số bằng 0:&lt;/li&gt;
&lt;/ul&gt;

\[e = N_{\mathrm{in}} - N_{\mathrm{nhấn}} = 0.\]

&lt;p&gt;Có thể đính kèm log thử nghiệm khi bỏ bộ lọc (nhảy 5–30 nhịp mỗi lần nhấn) ở bên dưới để đối chiếu, nhưng không thay thế được kết quả $e = 0$. Nếu vẫn bị đếm thừa, cần quay lại chỉnh bộ lọc 20 ms.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Mục sơ đồ máy trạng thái:&lt;/strong&gt; tự vẽ lại sơ đồ trong sổ (không chỉ chụp lại hình bài giảng) gồm đủ ba trạng thái, mũi tên từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt; sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; ghi đúng 300 ms, và cả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; lẫn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; đều đánh dấu duty 0. Ghi câu “PWM bằng không trong FAULT” chỉ khi hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply()&lt;/code&gt; thực sự khóa ngõ ra về 0 ngoài trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Thứ tự cấp nguồn: logic trước, nguồn motor sau, bánh nhấc&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Sơ đồ thứ tự cấp nguồn nhắc lại giới hạn của chương 03: USB nuôi vi điều khiển, đường 3,3 V nuôi nút kéo lên và LED, còn nguồn động cơ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; trên TB6612) vẫn để trống. Hoàn thành checklist chương 03 không đồng nghĩa với việc đã được đấu dây động cơ.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay-phát-hiện-một-checklist-chưa-đạt&quot;&gt;Ví dụ tính tay: phát hiện một checklist chưa đạt&lt;/h2&gt;

&lt;p&gt;Xét một bài nộp đánh dấu hoàn thành cả bốn mục (nút nhấn, PWM, chống dội, máy trạng thái) nhưng mã nguồn đính kèm chỉ gồm:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcAttach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PWM done&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;still on&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Khi đối chiếu với các tiêu chí: không có nút nhấn nên không thể có log nhấn hay kết quả chống dội; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; chỉ chạy một lần duy nhất trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt; ở mức cố định 180 (khoảng 70%) và không bao giờ đưa về 0; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt; không đọc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; và bị chặn bởi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt;, lớn hơn nhiều so với ngưỡng cắt:&lt;/p&gt;

\[1000\,\mathrm{ms} &amp;gt; 300\,\mathrm{ms}.\]

&lt;p&gt;Dòng in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM done&lt;/code&gt; chỉ là chuỗi văn bản cố định, không phải bằng chứng đo kiểm. Nếu chấp nhận bài nộp này, khi lắp động cơ vào robot sẽ chạy ngay lập tức lúc khởi động và không thể dừng khi mất lệnh. Bài chỉ đạt sau khi mã nguồn cập nhật duty từ máy trạng thái mỗi vòng lặp, không chặn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt;, có mức duty 0 và tắt LED khi ngừng gõ lệnh.&lt;/p&gt;

&lt;p&gt;Một lỗi tinh vi hơn cũng cần kiểm tra: chương trình có máy trạng thái đầy đủ, nhưng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt; đã gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite(PIN, 180)&lt;/code&gt; và nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; chỉ gán biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty = 0&lt;/code&gt; mà không gọi lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite(PIN, 0)&lt;/code&gt;. Ngoại vi PWM phần cứng vẫn giữ nguyên xung cũ cho đến khi được ghi giá trị mới, nên hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply()&lt;/code&gt; phải gọi tường minh lệnh ghi 0 cho cả hai kênh PWM mỗi khi ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;nội-dung-cần-nộp-trong-sổ-lab&quot;&gt;Nội dung cần nộp trong sổ lab&lt;/h2&gt;

&lt;p&gt;Kiểm tra lần lượt sáu mục sau trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;; mục nào thiếu bằng chứng cụ thể thì giữ ở trạng thái chưa hoàn thành:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Log nút nhấn:&lt;/strong&gt; dán một cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt; sạch cho mỗi thao tác, ghi rõ chân GPIO và cấu hình kéo lên.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bằng chứng PWM:&lt;/strong&gt; ảnh LED ở hai mức sáng hoặc log các mức duty quanh 25%, 50%, 90% kèm một dòng duty bằng 0; nêu rõ API (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach&lt;/code&gt; + độ phân giải, hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kết quả chống dội:&lt;/strong&gt; ghi số lần nhấn $N$ và số lần in $N$ ($e = 0$); có thể ghi thêm số nhịp khi bỏ lọc để đối chiếu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sơ đồ máy trạng thái:&lt;/strong&gt; đủ ba trạng thái, mũi tên 300 ms sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;, đánh dấu duty 0 ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;, kèm câu “PWM bằng không trong FAULT” khớp với hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ngôn ngữ và cấu trúc vòng lặp:&lt;/strong&gt; nêu rõ C++ hay MicroPython, xác nhận vòng điều khiển không gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleep&lt;/code&gt; dài hơn vài mili giây và trích hằng số timeout từ mã nguồn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Xác nhận giới hạn động cơ:&lt;/strong&gt; ghi rõ “không nối động cơ vào GPIO, chưa nối VM, luôn nhấc bánh khi lắp driver về sau.” (Tham khảo trước cho chương sau: module TB6612 giá 45000₫, dải VM 4,5 V – 10 V, dòng liên tục khoảng 1,2 A tại &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-tb6612fng-dc-motor-driver&quot;&gt;Hshop&lt;/a&gt;, nhưng chưa đấu dây trong chương này.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nếu bo mạch chưa giao tới, mục 1–3 để trạng thái mở; mục 4 và 5 có thể chuẩn bị trước trên mã nguồn đã biên dịch hoặc kiểm tra cú pháp và ghi nhãn “chỉ mã nguồn” cho đến khi kiểm chứng trên LED thật.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=-Yicg2TTMPs&quot;&gt;Understanding State Machines, Part 1: What Are They?&lt;/a&gt; — MATLAB, tiếng Anh. Ôn lại cấu trúc trạng thái và chuyển đổi để đối chiếu với sơ đồ nộp trong checklist.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Đóng vai người chấm bài cho đoạn mã ví dụ ở trên: liệt kê các mục checklist bị trượt và chỉ ra dòng mã khiến câu “PWM bằng không trong FAULT” không đúng sự thật.&lt;/li&gt;
  &lt;li&gt;Sơ đồ của một sinh viên có đủ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;, nhưng mũi tên quá thời gian lại vẽ từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; với nhãn “1 s”. Sơ đồ này sai ở hai điểm nào?&lt;/li&gt;
  &lt;li&gt;Log chống dội ghi nhận 10 lần nhấn nhưng dòng cuối in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;presses=12&lt;/code&gt;. Mục chống dội đã đạt chưa, và cần kiểm tra điểm nào trong mã nguồn của bài 4 trước tiên?&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt; xuất duty 200, còn nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; chỉ gán &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty = 0&lt;/code&gt; mà không gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt;. Sau 300 ms im lặng, tín hiệu thực tế trên chân GPIO là gì và cần sửa gì trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply()&lt;/code&gt;?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý&lt;/summary&gt;

1. Trượt các mục: log nút nhấn, bằng chứng PWM nhiều mức và mức 0, kết quả chống dội, sơ đồ máy trạng thái và vòng lặp không chặn (`delay(1000)`). Dòng `ledcWrite(PIN, 180)` trong `setup()` mà không có lệnh ghi 0 nào về sau làm câu &quot;PWM bằng không trong FAULT&quot; sai.
2. Sai thứ nhất: kiểm tra mất lệnh chỉ áp dụng khi đang ở `TELEOP` (vì `IDLE` vốn đã có duty 0). Sai thứ hai: ngưỡng timeout quy định là 300 ms, không phải 1 s.
3. Chưa đạt vì sai số $e = 2 \neq 0$. Cần kiểm tra bộ lọc ổn định 20 ms bao quanh lệnh tăng bộ đếm và xem cấu hình ngắt `CHANGE` có đang đếm nhầm cạnh nhả hay không.
4. Chân GPIO vẫn tiếp tục phát xung PWM ở mức duty 200. Hàm `apply()` phải gọi tường minh `ledcWrite` với giá trị 0 mỗi khi trạng thái là `FAULT` hoặc `IDLE`.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/time/millis/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;&lt;/a&gt; — hàm định thời dùng để kiểm chứng mũi tên timeout 300 ms trong mã nộp.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html&quot;&gt;LEDC trên Arduino ESP32&lt;/a&gt; — tài liệu đối chiếu giá trị ghi duty với độ phân giải đã cấu hình.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; — tầng giao tiếp sẽ tích hợp lên firmware đã qua checklist này, nhưng vẫn giữ nguyên yêu cầu ép PWM về 0 tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Máy trạng thái trong firmware robot</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_05_state_machines_fw/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_05_state_machines_fw</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn xây dựng được máy trạng thái gồm ba trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;, trong đó mỗi sự kiện (nhận lệnh mới, mất lệnh 300 ms, nhấn nút xác nhận) thực hiện đúng một chuyển trạng thái xác định. Tại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;, cả hai kênh PWM đều bị ép về 0. Bạn triển khai được máy trạng thái này bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;switch&lt;/code&gt; (hoặc điều kiện theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt;) trong C++, cũng như bằng chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elif&lt;/code&gt; hoặc từ điển hàm trong MicroPython trên nền vòng lặp không chặn của bài 1, đồng thời loại bỏ hoàn toàn kiểu viết dùng nhiều biến cờ boolean rời rạc có thể cùng bật một lúc.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã biết tính thời gian trôi qua bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff()&lt;/code&gt; từ bài 1 (thay thế nhịp tim chặn ở &lt;a href=&quot;/contents/vi/chapter02/02_04_blink_serial_hello/&quot;&gt;bài serial chương 02&lt;/a&gt;), biết đọc cạnh nút nhấn và xuất PWM bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt; mà không dựa vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; mặc định. Trong bài này, đèn LED từ bài GPIO đóng vai trò chỉ báo “động cơ đang được phép chạy”; mạch TB6612 vẫn chưa được nối.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Hãy hình dung đoạn code robot của một người mới sau ba tuần thêm tính năng: 7 biến cờ kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is_running&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;obstacle_seen&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;turning_left&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;button_held&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;low_battery&lt;/code&gt; đan chéo nhau trong một rừng câu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if / else if&lt;/code&gt; dài 200 dòng! Chỉ cần vừa gặp vật cản &lt;em&gt;đồng thời&lt;/em&gt; pin yếu, robot rơi vào một tổ hợp trạng thái bất hợp lệ khiến nó vừa tiến vừa báo lỗi! &lt;strong&gt;Máy trạng thái hữu hạn (Finite State Machine — FSM)&lt;/strong&gt; quét sạch mớ hỗn độn đó bằng một mô hình toán học trong sáng: tại mọi thời điểm, robot chỉ ở &lt;strong&gt;đúng một trạng thái&lt;/strong&gt; duy nhất với các luật chuyển trạng thái tường minh.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-lý-thuyết-máy-trạng-thái-hữu-hạn-fsm-trong-firmware-robot&quot;&gt;Kiến thức cần nhớ: Lý thuyết Máy Trạng thái Hữu hạn (FSM) trong Firmware Robot&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bộ Tứ Định nghĩa FSM (States, Events, Transitions, Actions):&lt;/strong&gt; Gồm tập hữu hạn các &lt;em&gt;trạng thái&lt;/em&gt; (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUN&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVOID&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESTOP&lt;/code&gt;), các &lt;em&gt;sự kiện&lt;/em&gt; đầu vào (nhấn nút, cảm biến \(&amp;lt; 25\text{ cm}\), hết thời gian quay), &lt;em&gt;hàm chuyển trạng thái&lt;/em&gt; và &lt;em&gt;hành động&lt;/em&gt; khi vào/đang ở/thoát trạng thái.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đảm bảo Bất biến An toàn (Safety Invariants):&lt;/strong&gt; Thay vì kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if (!estop)&lt;/code&gt; ở 15 chỗ khác nhau, ta biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESTOP&lt;/code&gt; thành một trạng thái ưu tiên cao nhất có hành động &lt;em&gt;vào trạng thái&lt;/em&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;on_enter&lt;/code&gt;) cưỡng bức cắt PWM về 0 ngay lập tức — và chỉ thoát ra được khi người vận hành chủ động nhấn reset!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kết hợp FSM với Vòng lặp Không chặn:&lt;/strong&gt; Mỗi lần chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt;, hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;switch (state)&lt;/code&gt; chỉ thực thi nhanh nhánh của trạng thái hiện tại (vài micro-giây) và kiểm tra điều kiện chuyển tiếp bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; — tạo ra kiến trúc firmware sạch, dễ vẽ sơ đồ và dễ kiểm thử.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-bảng-chuyển-trạng-thái-4-trạng-thái-cho-xe-robot-tránh-vật-cản&quot;&gt;Ví dụ làm từng bước: Bảng chuyển trạng thái 4 trạng thái cho xe robot tránh vật cản&lt;/h3&gt;

&lt;p&gt;Thay vì 4 biến cờ Boolean (tạo ra tới \(2^4 = 16\) tổ hợp, trong đó 12 tổ hợp vô nghĩa!), ta khóa chặt hành vi xe vào &lt;strong&gt;đúng 4 trạng thái&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Trạng thái hiện tại&lt;/th&gt;
      &lt;th&gt;Hành động liên tục&lt;/th&gt;
      &lt;th&gt;Sự kiện kích hoạt&lt;/th&gt;
      &lt;th&gt;Trạng thái kế tiếp&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;&lt;/strong&gt; (Đứng chờ)&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM_L = 0, PWM_R = 0&lt;/code&gt;, LED nhấp 1 Hz&lt;/td&gt;
      &lt;td&gt;Nhấn nút Start (đã chống dội)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUN&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUN&lt;/code&gt;&lt;/strong&gt; (Chạy thẳng)&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM_L = 50%, PWM_R = 50%&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Khoảng cách \(d &amp;lt; 25\text{ cm}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVOID&lt;/code&gt;&lt;/strong&gt; (ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_enter = now&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVOID&lt;/code&gt;&lt;/strong&gt; (Quay né)&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM_L = -40%, PWM_R = +40%&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - t_enter &amp;gt;= 400 ms&lt;/code&gt; và \(d \ge 35\text{ cm}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUN&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Bất kỳ trạng thái nào&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;—&lt;/td&gt;
      &lt;td&gt;Nhấn nút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESTOP&lt;/code&gt; hoặc \(V_{\text{pin}} &amp;lt; 6.4\text{ V}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESTOP&lt;/code&gt;&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt;, LED nhấp 5 Hz)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Chú ý chi tiết &lt;strong&gt;độ trễ trễ ngưỡng (hysteresis)&lt;/strong&gt; trong bảng: vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVOID&lt;/code&gt; khi \(d &amp;lt; 25\text{ cm}\) nhưng chỉ quay lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUN&lt;/code&gt; khi \(d \ge 35\text{ cm}\) (khoảng đệm &lt;strong&gt;10 cm&lt;/strong&gt;) — chặn đứng hoàn toàn hiện tượng xe giật lắc qua lại liên tục ngay tại mép 25 cm!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-4-bước-chuyển-hóa-yêu-cầu-robot-thành-fsm-không-lỗi&quot;&gt;Giải thích: 4 bước chuyển hóa yêu cầu robot thành FSM không lỗi&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Vẽ sơ đồ trạng thái trên giấy &lt;em&gt;trước&lt;/em&gt; khi mở trình soạn thảo:&lt;/strong&gt; Liệt kê mọi trạng thái có thể, mũi tên chuyển và đảm bảo mọi trạng thái đều có lối ra hợp lệ (không bị kẹt chết).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Thiết lập thứ tự ưu tiên sự kiện (An toàn luôn đứng đầu):&lt;/strong&gt; Trong mỗi chu kỳ, luôn kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESTOP&lt;/code&gt; và sụt áp pin &lt;em&gt;trước&lt;/em&gt; mọi logic điều hướng khác.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Dùng hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enter_state(next)&lt;/code&gt; tập trung để ghi log chuyển trạng thái:&lt;/strong&gt; Mỗi khi đổi trạng thái, in đúng 1 dòng log &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t=12450ms: RUN -&amp;gt; AVOID (d=22cm)&lt;/code&gt; — đọc log bạn biết ngay robot đang nghĩ gì!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Thêm ngưỡng trễ (Hysteresis) và Timeout cho trạng thái né tránh:&lt;/strong&gt; Nếu xe kẹt trong góc tường quá 3 giây không thoát được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVOID&lt;/code&gt;, timeout sẽ chuyển xe về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESTOP&lt;/code&gt; thay vì quay tròn vô tận đến hết pin!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu robot đang chạy vòng lặp FSM ở 50 Hz (\(T = 20\text{ ms}\)) với vận tốc \(0.5\text{ m/s}\) và bạn nhấn nút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESTOP&lt;/code&gt;, quãng đường tối đa xe đi thêm trước khi lệnh cắt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM = 0&lt;/code&gt; được thực thi là bao nhiêu? Tối đa 1 chu kỳ vòng lặp: \(0.5\text{ m/s} \times 0.02\text{ s} = \mathbf{1\text{ cm}}\) (chưa kể quán tính cơ học trượt nhẹ)!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Firmware điều khiển từ xa (teleop) của Capstone A không nên là tập hợp các câu lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; rời rạc. Tại mọi thời điểm, robot chỉ ở một trong ba tình huống: đang chờ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;), đang chạy theo lệnh còn hạn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt;), hoặc đã dừng an toàn do mất lệnh (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;). Tín hiệu PWM là ngõ ra phụ thuộc trực tiếp vào trạng thái hiện tại, tránh bị khối lệnh khác ghi đè ngoài ý muốn. Khi tích hợp với ROS ở các chương sau, cơ chế này bảo đảm cả hai kênh TB6612 luôn về duty 0 trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;. Ngược lại, cách viết dùng hai biến boolean độc lập (như cờ nháy đèn và cờ chạy động cơ) có thể để cả hai cùng bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;, khiến robot vẫn chạy trong khi đèn báo trạng thái lỗi đang nháy.&lt;/p&gt;

&lt;h2 id=&quot;nội-dung&quot;&gt;Nội dung&lt;/h2&gt;

&lt;p&gt;Máy trạng thái (state machine) gồm một biến duy nhất biểu diễn trạng thái hiện tại cùng bộ quy tắc chuyển trạng thái theo từng sự kiện. Firmware trong bài có ba trạng thái:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;IDLE:&lt;/strong&gt; chưa cho phép chạy. Cả hai kênh PWM bằng 0 và LED chỉ báo tắt. Khi nhận được lệnh hợp lệ, máy chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;TELEOP:&lt;/strong&gt; đang nhận lệnh còn hiệu lực. Các kênh PWM cập nhật theo lệnh và mỗi lệnh mới làm mới mốc thời gian &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lastCmd&lt;/code&gt;. Nếu khoảng thời gian im lặng đạt 300 ms, máy chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;FAULT:&lt;/strong&gt; đường truyền lệnh bị gián đoạn hoặc hệ thống đã ngắt an toàn. Cả hai kênh PWM bị ép về 0 và LED tắt. Để thoát khỏi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;, người vận hành bắt buộc phải nhấn nút xác nhận để quay về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; — không nhảy thẳng về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt;. Mọi lệnh gửi tới khi đang ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; đều bị bỏ qua để tránh việc kết nối tự phục hồi làm robot bất ngờ chạy tiếp theo lệnh cũ.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/firmware_state_machine.png&quot; alt=&quot;IDLE, TELEOP và FAULT, với mũi tên 300 ms&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Cấu trúc &lt;strong&gt;cần tránh&lt;/strong&gt; dùng các cờ boolean độc lập:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blinkMode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;motorsOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blinkMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* đảo LED */&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;motorsOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;blinkMode&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;motorsOn&lt;/code&gt; là hai biến tách biệt, không có cơ chế nào ngăn chúng cùng mang giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; trong cùng một vòng lặp, dẫn đến đèn báo nháy trong khi chân PWM vẫn xuất 200. Khi thêm cờ thứ ba cho timeout, số tổ hợp trạng thái tăng lên 8 và rất khó kiểm thử hết. Dùng một &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt; duy nhất giới hạn hệ thống trong đúng 3 trạng thái và gom toàn bộ việc xuất PWM về một chỗ duy nhất.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C++ cho lõi Arduino ESP32:&lt;/strong&gt; LED trên GPIO 18 chỉ sáng khi ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty &amp;gt; 0&lt;/code&gt; (kênh thứ hai được mô phỏng bằng chú thích để giữ đúng nguyên tắc “cả hai kênh”). Lệnh qua serial rút gọn thành một ký tự: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; là tiến ở mức 40% thang (102/255), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; là lệnh dừng chủ động từ người vận hành để về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; với duty 0. Sự im lặng (không có ký tự mới sau 300 ms) khác hoàn toàn với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;300&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint8_t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Fault&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastCmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 0..255, vai cả hai kênh TB6612&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INPUT_PULLUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;state IDLE duty 0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Sau này: ledcWrite(PWMA, enabled ? duty : 0); ledcWrite(PWMB, enabled ? duty : 0);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prevBtn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digitalRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btnPress&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prevBtn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;prevBtn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;available&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;F&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;S&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lastCmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;F&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;102&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// khoảng 40% của 255, hoặc lệnh dừng&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;S&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// Trong FAULT, ký tự bị bỏ qua có chủ đích.&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Teleop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastCmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Fault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Fault&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btnPress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trong hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply()&lt;/code&gt;, điều kiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enabled&lt;/code&gt; yêu cầu trạng thái phải là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mode::Teleop&lt;/code&gt;, nên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; không bao giờ bật LED (hoặc xuất PWM khác 0). Khi hết hạn 300 ms, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty&lt;/code&gt; cũng được gán về 0 để không lưu giá trị cũ. Cạnh nút ở đoạn mã trên viết gọn để tập trung vào máy trạng thái; trong thực tế bạn nên kết hợp với bộ lọc chống dội 20 ms của bài GPIO và tuyệt đối không thêm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; vào nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; có thể ánh xạ mỗi trạng thái tới một hàm xử lý thông qua từ điển &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HANDLERS&lt;/code&gt;. Mỗi vòng lặp chỉ gọi đúng một hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HANDLERS[state]&lt;/code&gt; nên hai trạng thái không thể chạy đồng thời.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PULL_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;300&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;IDLE&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;prev_btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;TELEOP&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;TELEOP&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;IDLE&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;FAULT&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_fault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;IDLE&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;HANDLERS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;IDLE&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;TELEOP&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;FAULT&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_fault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Cổng serial bài dạy có thể là sys.stdin; phần lab nêu một kiểu.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;level&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;level&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prev_btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;prev_btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;HANDLERS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ngoài cách dùng từ điển hàm, một chuỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elif&lt;/code&gt; theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; cũng hợp lệ. Điều cần tránh là tạo một trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SLEEP&lt;/code&gt; rồi gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; bên trong: ngủ chặn không phải là trạng thái vận hành mà là khoảng mù khiến đồng hồ không được kiểm tra.&lt;/p&gt;

&lt;p&gt;Phân biệt rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; là lệnh dừng chủ động (về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;, không cần nhấn nút xác nhận), còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; dành cho trường hợp mất kết nối hoặc mất luồng lệnh (bắt buộc nhấn nút mới được phép nhận lệnh lại). Trong ví dụ tính tay bên dưới, ký hiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F 40&lt;/code&gt; thể hiện rõ mức 40%; khi thực hành trên Serial Monitor, gõ gọn một chữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; đã gắn sẵn mức 40% đó.&lt;/p&gt;

&lt;p&gt;Về sau, một client &lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; có thể gửi trực tiếp các lệnh vận tốc vào chính máy trạng thái này: mỗi bản tin mới sẽ làm tươi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lastCmd&lt;/code&gt;, còn logic chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; và ép PWM về 0 vẫn giữ nguyên trên vi điều khiển.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Hệ thống khởi động ở trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;: LED tắt, hai kênh PWM bằng 0. Tại $t = 0$, cổng serial nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F 40&lt;/code&gt;: máy chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt;, cập nhật &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lastCmd = 0&lt;/code&gt; và đặt duty 40% (tương đương $0{,}40 \times 255 = 102$ trên thang LEDC 8-bit). LED bật sáng; nếu đã nối driver thì cả hai kênh A và B đều nhận mức 102.&lt;/p&gt;

&lt;p&gt;Sau đó không có ký tự nào gửi thêm. Tuổi lệnh là $t - 0$, và điều kiện quá thời gian là:&lt;/p&gt;

\[t - t_{\text{cmd}} \ge 300\,\mathrm{ms}.\]

&lt;p&gt;Ngay ở lượt lặp đầu tiên thỏa điều kiện này, máy chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;, gán &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty = 0&lt;/code&gt; và tắt LED. Nếu gõ thêm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; trong lúc đang ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;, lệnh bị bỏ qua và duty vẫn bằng 0. Khi người vận hành nhấn nút xác nhận, máy chuyển về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; với duty vẫn bằng 0; chỉ khi nhận một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; mới ở trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;, LED mới sáng trở lại.&lt;/p&gt;

&lt;p&gt;Nếu trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt; có chèn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(200)&lt;/code&gt;, kết quả tính tay từ bài 1 lặp lại y nguyên: lần đầu vòng lặp phát hiện quá hạn là tại 400 ms, khiến LED (và về sau là TB6612) duy trì mức 102 thêm 100 ms ngoài giới hạn cho phép.&lt;/p&gt;

&lt;h2 id=&quot;bài-lab&quot;&gt;Bài lab&lt;/h2&gt;

&lt;p&gt;Không nối động cơ, không nối VM và không nối pin. LED đại diện cho trạng thái “động cơ được phép chạy”, còn Serial Monitor đóng vai trò tay điều khiển.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Nạp mã C++ (hoặc nối các hàm MicroPython với luồng đọc serial để ký tự gõ vào gán cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd&lt;/code&gt;). Xác nhận lúc mới khởi động màn hình báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt; và LED tắt.&lt;/li&gt;
  &lt;li&gt;Gõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt;: LED sáng và log (nếu in thêm) báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt; với duty 102 (hoặc 40 ở bản Python). Gõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;: LED tắt và trạng thái quay về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Gõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; lần nữa rồi ngừng gõ. Trong khoảng 300 ms, LED phải tự tắt và máy chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;. Nếu LED vẫn sáng, kiểm tra xem đồng hồ có bị bỏ qua hoặc có lệnh ngủ dài hơn 300 ms hay không. Thử gõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; khi đang ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; và xác nhận LED vẫn tắt.&lt;/li&gt;
  &lt;li&gt;Nhấn nút xác nhận: trạng thái chuyển về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;, duty vẫn bằng 0. Chỉ khi gõ thêm một chữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; mới thì LED mới sáng lại.&lt;/li&gt;
  &lt;li&gt;Vẽ lại sơ đồ ba trạng thái cùng mũi tên timeout 300 ms vào sổ tay và ghi rõ câu “PWM bằng không trong FAULT” để phục vụ checklist cuối chương.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nếu LED sáng ngay khi vừa khởi động, hãy kiểm tra xem &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply()&lt;/code&gt; có bị bỏ sót điều kiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TELEOP&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt; đã xuất sẵn một mức PWM cố định hay không.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=-Yicg2TTMPs&quot;&gt;Understanding State Machines, Part 1: What Are They?&lt;/a&gt; — MATLAB, tiếng Anh. Trình bày khái niệm trạng thái, sự kiện và chuyển trạng thái — khớp trực tiếp với ba trạng thái chờ, chạy và lỗi của firmware teleop.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=CbNXEyYL_f8&quot;&gt;Finite State Machines: The Embedded Trick That Changes Everything&lt;/a&gt; — AsabreTech, tiếng Anh. Ứng dụng máy trạng thái hữu hạn từ góc nhìn lập trình firmware nhúng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Lần theo chuỗi sự kiện sau và ghi lại trạng thái cùng giá trị duty sau mỗi bước: vừa khởi động; nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt;; im lặng 300 ms; nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; khi chưa nhấn nút; nhấn nút; nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Một sinh viên thêm trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mode::Sleep&lt;/code&gt; và gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; bên trong “để robot nghỉ”. Sự kiện nào bị bỏ lỡ trong khoảng thời gian đó, và TB6612 sẽ phản ứng ra sao nếu duty trước khi ngủ khác 0?&lt;/li&gt;
  &lt;li&gt;Vì sao mọi lệnh nhận được khi đang ở trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; phải bị bỏ qua thay vì coi là tín hiệu cho thấy kết nối đã khôi phục?&lt;/li&gt;
  &lt;li&gt;Hãy đề xuất cách viết lại đoạn mã hai biến boolean (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;blinkMode&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;motorsOn&lt;/code&gt;) thành một biến duy nhất sao cho tình huống “vừa nháy đèn lỗi vừa chạy động cơ” không thể xảy ra.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý&lt;/summary&gt;

1. `IDLE` (duty 0) $\rightarrow$ `TELEOP` (duty 102 hoặc 40%) $\rightarrow$ `FAULT` (duty 0) $\rightarrow$ vẫn `FAULT` (duty 0) sau lệnh `F` thứ hai $\rightarrow$ `IDLE` (duty 0) sau khi nhấn nút $\rightarrow$ `TELEOP` (duty 102) sau lệnh `F` cuối.
2. Trong suốt `delay(1000)`, điều kiện im lặng 300 ms không được kiểm tra. Nếu không ép duty về 0 trước khi ngủ, cả hai kênh PWM vẫn giữ mức cũ suốt 1 giây.
3. `FAULT` đòi hỏi xác nhận chủ động từ con người để bảo đảm an toàn; nếu tự động chạy lại ngay khi gói tin đến trở lại sau sự cố mất sóng, robot có thể di chuyển bất ngờ khi không ai kiểm soát.
4. Xóa bỏ `blinkMode` và `motorsOn`, thay bằng một biến `Mode` duy nhất (`Idle`, `Teleop`, `Fault`) và chỉ cho phép bật ngõ ra trong nhánh `Teleop` của hàm `apply()`.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/time/millis/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;&lt;/a&gt; — bộ định thời cho phép so sánh timeout trong bản C++, đảm bảo phép trừ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - lastCmd&lt;/code&gt; luôn đúng khi tràn bộ đếm.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; — nền tảng giao tiếp ROS cho vi điều khiển sẽ cung cấp các sự kiện lệnh cho máy trạng thái này ở các chương sau.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Ngắt, chống dội tiếp điểm và ISR ngắn</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_04_interrupts_debounce/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_04_interrupts_debounce</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn phân biệt được hiện tượng dội cơ khí (bounce) của tiếp điểm với một lần nhấn thật, đồng thời thiết kế hàm phục vụ ngắt (ISR) thật ngắn chỉ làm nhiệm vụ đặt cờ. Bạn cấu hình ngắt nút nhấn theo chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt;, để vòng lặp chính xử lý cửa sổ chống dội 20 ms và xác nhận mỗi lần nhấn vật lý chỉ tăng bộ đếm đúng 1 đơn vị. Bạn cũng giải thích được vì sao gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; bên trong ISR sẽ làm đóng băng các ngắt khác, và nắm rõ giới hạn của một ISR đọc encoder về sau: chỉ tăng một biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile uint32_t&lt;/code&gt; rồi thoát ngay.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Ở bài GPIO, bạn đã đấu nút nhấn từ chân GPIO xuống mass với kéo lên nội và lọc tín hiệu ổn định 20 ms bằng cách đọc liên tục trong vòng lặp. Bài này chuyển việc phát hiện sự kiện cạnh sang ngắt ngoài, còn bước kiểm tra thời gian ổn định vẫn nằm trong vòng lặp không chặn của bài 1 (thay thế kiểu &lt;a href=&quot;/contents/vi/chapter02/02_04_blink_serial_hello/&quot;&gt;nhịp tim &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; của chương 02&lt;/a&gt;). Bạn dùng lại nút nhấn sẵn có, chưa cần thêm linh kiện hay nối động cơ.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Khi bạn nhấn một chiếc nút cơ khí, mắt thường thấy nó đóng “tách” một cái dứt khoát — nhưng dưới con mắt tốc độ MHz của vi điều khiển, hai lá đồng bên trong bật nảy lên xuống tới &lt;strong&gt;5–15 lần trong vòng 10 mili-giây&lt;/strong&gt; (&lt;strong&gt;hiện tượng dội tiếp điểm — switch bounce&lt;/strong&gt;)! Cùng lúc đó, hai đĩa mã hóa vòng quay (encoder) trên trục bánh xe phát ra hàng trăm xung mỗi giây mà vòng lặp chính không thể lúc nào cũng đứng chờ để kiểm tra. &lt;strong&gt;Ngắt phần cứng (Interrupt / ISR)&lt;/strong&gt; kết hợp &lt;strong&gt;chống dội (debounce)&lt;/strong&gt; chính là đôi tai nhạy bén giúp robot không bao giờ bỏ lỡ sự kiện tốc độ cao.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ngắt-phần-cứng-isr-chống-dội-và-đồng-bộ-dữ-liệu&quot;&gt;Kiến thức cần nhớ: Ngắt Phần cứng (ISR), Chống dội và Đồng bộ Dữ liệu&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Thăm dò (Polling) vs. Ngắt Phần cứng (Interrupt):&lt;/strong&gt; &lt;em&gt;Polling&lt;/em&gt; liên tục hỏi trong vòng lặp chính (dễ bỏ lỡ xung ngắn khi CPU đang bận); &lt;em&gt;Interrupt&lt;/em&gt; buộc phần cứng tạm dừng chương trình chính ngay khi có cạnh lên/xuống trên chân GPIO, nhảy vào &lt;strong&gt;Hàm phục vụ ngắt (ISR)&lt;/strong&gt; rồi quay lại đúng chỗ cũ trong vài micro-giây!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quy tắc Vàng cho ISR (“Ngắn — Nhanh — Không chặn”):&lt;/strong&gt; Trong ISR tuyệt đối &lt;em&gt;không&lt;/em&gt; gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt;, I2C hay cấp phát bộ nhớ! ISR chỉ nên tăng một biến đếm hoặc bật một cờ báo khai báo với từ khóa &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt;&lt;/strong&gt; (và gắn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IRAM_ATTR&lt;/code&gt; trên ESP32) để vòng lặp chính xử lý sau.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chống dội Tiếp điểm (Debouncing):&lt;/strong&gt; Với nút nhấn cơ khí (dội trong 5–20 ms), ta lọc nhiễu bằng cách chỉ chấp nhận thay đổi trạng thái sau khi tín hiệu giữ ổn định qua một khoảng thời gian &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;debounce_ms&lt;/code&gt; (thường 25–50 ms).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-tần-số-ngắt-encoder-và-chứng-minh-vì-sao-cấm-serialprint-trong-isr&quot;&gt;Ví dụ làm từng bước: Tính tần số ngắt Encoder và chứng minh vì sao cấm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print()&lt;/code&gt; trong ISR&lt;/h3&gt;

&lt;p&gt;Giả sử bánh xe robot quay ở tốc độ &lt;strong&gt;300 vòng/phút&lt;/strong&gt; (\(5\text{ vòng/giây}\)) với đĩa encoder &lt;strong&gt;20 khe&lt;/strong&gt; đọc đầy đủ 4 cạnh pha A/B (quadrature 4× → &lt;strong&gt;80 xung/vòng&lt;/strong&gt;):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tần số xung ngắt trên 1 bánh xe:&lt;/strong&gt;
\(f_{\text{xung}} = 5\text{ vòng/s} \times 80\text{ xung/vòng} = \mathbf{400\text{ xung/giây}} \quad (\text{mỗi xung cách nhau }\mathbf{2.5\text{ ms}}).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tải CPU nếu viết ISR chuẩn (chỉ tăng biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile int32_t ticks++&lt;/code&gt;, tốn \(1\ \mu\text{s}\)):&lt;/strong&gt;
\(\text{Tải CPU} = 400 \times 1\ \mu\text{s} = 400\ \mu\text{s/s} = \mathbf{0.04\%\text{ CPU}} \quad (\text{nhẹ như lông hồng!}).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thảm họa nếu lỡ viết &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.println(ticks)&lt;/code&gt; (tốn ~\(5\text{ ms}\)) bên trong ISR:&lt;/strong&gt; mỗi lần gọi ISR dài gấp đôi khoảng cách giữa 2 xung (\(5\text{ ms} &amp;gt; 2.5\text{ ms}\)), làm chồng chéo ngắt, mất sạch xung đếm và khiến bộ giám sát &lt;strong&gt;Watchdog Timer&lt;/strong&gt; của ESP32 lập tức reset bo mạch!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-4-bước-thiết-kế-ngắt-và-chống-dội-an-toàn-tuyệt-đối&quot;&gt;Giải thích: 4 bước thiết kế Ngắt và Chống dội an toàn tuyệt đối&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Khai báo mọi biến chia sẻ giữa ISR và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt;:&lt;/strong&gt; Nếu thiếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt;, trình biên dịch C++ có thể tối ưu hóa bằng cách lưu biến trong thanh ghi CPU và không bao giờ thấy giá trị mới mà ISR vừa cập nhật!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Đọc biến đa byte (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int32_t&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int64_t&lt;/code&gt;) trong vùng cấm ngắt ngắn (Critical Section):&lt;/strong&gt; Tắt ngắt 1 micro-giây để sao chép &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks&lt;/code&gt; ra biến cục bộ rồi bật ngắt lại ngay, tránh đọc phải giá trị bị xé đôi (&lt;em&gt;torn read&lt;/em&gt;) khi ISR nhảy vào giữa chừng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Phân biệt nút nhấn cơ khí (cần debounce 30 ms) với cảm biến quang/Hall encoder (tín hiệu sạch, KHÔNG debounce bằng 30 ms!):&lt;/strong&gt; Nếu bạn áp dụng bộ lọc 30 ms lên encoder 400 Hz, bạn sẽ lọc sạch 90% xung quay bánh xe!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Dùng bộ đếm phần cứng (PCNT trên ESP32 / PIO trên Pico) khi tốc độ xung rất cao:&lt;/strong&gt; Khi encoder gắn trước hộp số (vài chục nghìn xung/giây), phần cứng đếm tự động giúp giải phóng hoàn toàn CPU.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Một encoder gắn trực tiếp lên trục động cơ (trước hộp số tỉ số truyền 1:48) có 11 xung/vòng, quay ở 6.000 vòng/phút (100 vòng/giây). Tần số xung tạo ra là bao nhiêu? \(100 \times 11 = \mathbf{1{,}100\text{ xung/giây}}\) (đọc 4 cạnh là &lt;strong&gt;4.400 xung/giây&lt;/strong&gt; — cách nhau chỉ \(227\ \mu\text{s}\)!) — lúc này giữ ISR dưới \(2\ \mu\text{s}\) hoặc dùng bộ đếm phần cứng là bắt buộc!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Trên Capstone A, nút nhấn dùng để xóa trạng thái lỗi (FAULT), còn ngắt phần cứng về sau dùng để đếm xung encoder bánh xe trong lúc firmware vẫn đang cập nhật PWM. Nếu một ISR in dữ liệu serial, cấp phát bộ nhớ hoặc gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt;, nó sẽ chiếm dụng CPU khiến vòng lặp chính không kịp kiểm tra ngưỡng mất lệnh 300 ms để đưa hai kênh TB6612 về 0. Ở chiều ngược lại, nếu không chống dội, một lần nhấn nút có thể tạo ra 5 đến 30 cạnh tín hiệu khiến thao tác xóa lỗi kích hoạt lặp lại nhiều lần. Do đó, ISR phải thật ngắn gọn để vòng giám sát luôn có CPU, còn mọi thao tác chờ theo thời gian phải nằm trong vòng lặp chính.&lt;/p&gt;

&lt;h2 id=&quot;nội-dung&quot;&gt;Nội dung&lt;/h2&gt;

&lt;p&gt;Tiếp điểm cơ khí không đóng dứt khoát một lần duy nhất mà thường chạm, nảy ra rồi đóng lại nhiều lần. Với các nút nhấn thông dụng trong môn học, thời gian dội thường kéo dài khoảng 10 ms đến 50 ms — đó là lý do ta chọn ngưỡng lọc mặc định 20 ms. Nếu gắn ngắt trực tiếp lên chân nút mà không lọc, phần cứng sẽ gọi ISR cho từng cạnh dội dù người dùng chỉ nhấn một lần.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/debounce_timeline.png&quot; alt=&quot;Tiếp điểm nảy, rồi một mức ổn&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hàm phục vụ ngắt (ISR — Interrupt Service Routine) được phần cứng tự động gọi ngay khi chân GPIO xuất hiện cạnh khớp với cấu hình, tạm ngắt ngang vòng lặp chính. Nhờ vậy, các xung ngắn không bị bỏ sót giữa hai lượt kiểm tra của vòng lặp. Tuy nhiên, trong lúc ISR chạy, các ngắt khác phải chờ và vòng lặp chính bị tạm dừng (trên lõi đơn). Vì thế, ISR phải thật ngắn: chỉ đặt một cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; hoặc tăng một biến đếm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; rồi thoát ngay. Từ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; báo cho trình biên dịch biết biến có thể thay đổi bất ngờ bên ngoài luồng chính nên phải luôn đọc lại từ bộ nhớ. Tuyệt đối không gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt;, không cấp phát bộ nhớ hay tạo đối tượng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; trong ISR. Với MicroPython, &lt;a href=&quot;https://docs.micropython.org/en/latest/reference/isr_rules.html&quot;&gt;quy tắc ISR của MicroPython&lt;/a&gt; nghiêm cấm cấp phát bộ nhớ heap bên trong hàm xử lý ngắt; nguyên tắc tương tự trong Arduino giúp tránh xung đột với ngắt UART của &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Vòng lặp chính là nơi xử lý thời gian: xóa cờ ngắt rồi áp dụng quy tắc ổn định 20 ms giống bài GPIO để xác nhận lần nhấn. Với nút kéo lên nối xuống mass, chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt; bắt cạnh xuống khi nhấn, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt; bắt cả cạnh xuống khi nhấn lẫn cạnh lên khi nhả (khi đó bộ lọc ổn định sẽ loại bỏ cả xung dội lúc nhả).&lt;/p&gt;

&lt;p&gt;Khi làm việc với encoder về sau, ISR chỉ nên thực hiện đúng một thao tác tăng biến đếm:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;volatile&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IRAM_ATTR&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onEnc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;encCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Không tính toán tốc độ bằng số thực, không in serial và không chống dội bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; trong ISR; vòng lặp chính sẽ sao chép &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;encCount&lt;/code&gt; sang biến cục bộ để tính tốc độ và cập nhật PWM. Trên lõi Arduino ESP32, thuộc tính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IRAM_ATTR&lt;/code&gt; đặt hàm ISR vào bộ nhớ RAM nội để tránh bị gián đoạn khi hệ thống truy cập bộ nhớ flash.&lt;/p&gt;

&lt;p&gt;Dưới đây là kiểu viết &lt;strong&gt;sai cần tránh&lt;/strong&gt; — chống dội bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; ngay trong ISR:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IRAM_ATTR&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;badIsr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;// chặn lõi; các ISR khác phải chờ&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digitalRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pressFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; vốn dựa vào ngắt định thời hệ thống; gọi nó bên trong một ISR sẽ làm treo hoặc trì hoãn toàn bộ các ngắt khác (như UART hay kênh encoder còn lại). Ngay cả một vòng lặp chờ bận 20 ms trong ISR cũng làm vòng lặp chính không kiểm tra được timeout 300 ms và khiến encoder mất xung. Tách thành cờ trong ISR và lọc thời gian trong vòng lặp chính giúp ISR thoát chỉ trong vài micro giây mà vẫn không chặn hệ thống.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arduino C++ (ESP32):&lt;/strong&gt; bộ đếm chỉ tăng sau khi tín hiệu giữ ổn định ở mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; đủ 20 ms sau cạnh xuống trên GPIO 4.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;volatile&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IRAM_ATTR&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onFall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// không gì khác&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INPUT_PULLUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;attachInterrupt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digitalPinToInterrupt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;onFall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FALLING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;presses=0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digitalRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// đã lấy; phép thử đứng yên vẫn là người quyết&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;presses=%lu&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ngắt chỉ ghi nhận đã có cạnh xuống, còn việc lọc và in kết quả nằm trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt;. Để thấy rõ tác dụng của bộ lọc, bạn có thể tạm bỏ kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DEBOUNCE_MS&lt;/code&gt; và tăng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;presses&lt;/code&gt; trực tiếp mỗi khi cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fell&lt;/code&gt; bật: một lần nhấn trên nút cơ khí thông thường sẽ làm bộ đếm nhảy vọt khoảng 5 đến 30 đơn vị.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; cấu trúc tương tự — callback chỉ gán một biến cờ toàn cục đã khởi tạo sẵn, không tạo danh sách hay chuỗi định dạng bên trong handler.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_fall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PULL_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;irq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trigger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IRQ_FALLING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;on_fall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;presses=0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;presses&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trong MicroPython, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.IRQ_FALLING&lt;/code&gt; tương ứng với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt;, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.IRQ_RISING | Pin.IRQ_FALLING&lt;/code&gt; tương ứng với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt;. Nếu đặt lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;on_fall&lt;/code&gt;, chương trình rất dễ báo lỗi cấp phát bộ nhớ hoặc hoạt động thiếu ổn định.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Giả sử một lần nhấn tạo ra các cạnh xuống tại $t = 0, 3, 7, 12$ ms rồi giữ ổn định ở mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; cho đến khi nhả. Nếu không chống dội, ISR &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt; chạy 4 lần (và với nút dội mạnh hơn có thể lên 5–30 lần trong cửa sổ 10–50 ms), khiến bộ đếm tăng sai nhiều lần cho một thao tác bấm duy nhất. Với bộ lọc 20 ms, ba cạnh đầu chỉ đặt lại mốc thời gian; cạnh cuối xảy ra lúc 12 ms nên mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; được xác nhận lúc $12 + 20 = 32$ ms và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;presses&lt;/code&gt; chỉ tăng đúng 1.&lt;/p&gt;

&lt;p&gt;Xung dội lúc nhả không làm tăng bộ đếm vì mã chỉ đếm khi trạng thái ổn định chuyển sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt;. Ngược lại, nếu đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; trong ISR ngay tại $t = 0$, CPU bị giữ suốt 20 ms, bỏ lỡ mọi ngắt khác và làm trễ vòng kiểm tra timeout 300 ms. Với encoder có các cạnh xung chỉ cách nhau khoảng 200 µs, một khoảng chặn 20 ms sẽ làm mất hàng trăm xung — đó là lý do ISR encoder chỉ được phép tăng biến đếm rồi thoát.&lt;/p&gt;

&lt;h2 id=&quot;bài-lab&quot;&gt;Bài lab&lt;/h2&gt;

&lt;p&gt;Giữ nguyên mạch nút nhấn từ bài GPIO, chỉ cấp nguồn USB và không nối động cơ.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Nạp chương trình đã có chống dội. Nhấn nút đúng 10 lần (khoảng 1 lần mỗi giây). Serial Monitor phải in lần lượt từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;presses=1&lt;/code&gt; đến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;presses=10&lt;/code&gt;, sai số bằng 0. Nếu lệch, kiểm tra lại logic lọc hoặc xem có đếm nhầm cạnh nhả hay không.&lt;/li&gt;
  &lt;li&gt;Tạm bỏ bộ lọc 20 ms để mỗi lần cờ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fell&lt;/code&gt; bật đều tăng bộ đếm. Nhấn dứt khoát một lần và quan sát bộ đếm nhảy vọt (thường từ 5 đến 30 nhịp). Dán cả hai đoạn log (có lọc và không lọc) vào sổ tay để đối chiếu.&lt;/li&gt;
  &lt;li&gt;Không sửa hiện tượng dội bằng cách đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onFall&lt;/code&gt;. Ghi rõ vào sổ: phần chờ chống dội phải đặt cạnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_diff()&lt;/code&gt; trong vòng lặp chính, không đặt trong ISR.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;(Tùy chọn)&lt;/em&gt; Đổi chế độ ngắt sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt; và xác nhận rằng dù có dội lúc nhả, bộ đếm vẫn chỉ tăng 1 mỗi lần nhấn nếu chỉ đếm khi mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; đã ổn định.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [What is Switch Bounce and How to Debounce – ATM&lt;/td&gt;
          &lt;td&gt;Digi-Key Electronics](https://www.youtube.com/watch?v=IvU8m_30iK0) — DigiKey, tiếng Anh. Quan sát dạng sóng dội tiếp điểm trước khi viết ISR.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[ESP32 LED + Push Button Tutorial for Beginners&lt;/td&gt;
          &lt;td&gt;ESP32 GPIO Input &amp;amp; Output](https://www.youtube.com/watch?v=zCzchrkpHzk) — Alvin Pribadi, tiếng Anh. Ví dụ nút nhấn trên GPIO ESP32; lưu ý hàm ISR trong bài này phải ngắn gọn hơn các ví dụ xử lý trực tiếp.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một lần nhấn tạo ra các cạnh xuống tại 0, 2, 5, 9, 14 và 18 ms rồi giữ ổn định ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt;. Nếu không lọc, ISR đếm thành bao nhiêu lần? Với bộ lọc 20 ms, lần nhấn được xác nhận tại thời điểm nào và số đếm sau lọc là bao nhiêu?&lt;/li&gt;
  &lt;li&gt;Vì sao gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.println&lt;/code&gt; bên trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onFall&lt;/code&gt; vi phạm nguyên tắc “ISR ngắn” dù chương trình có thể vẫn chạy được vài lần nhấn đầu?&lt;/li&gt;
  &lt;li&gt;Với một encoder cầu phương (quadrature) ở các chương sau, thao tác nào sau đây được phép đặt trong ISR: tăng biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile uint32_t&lt;/code&gt;, tính tốc độ vòng/phút bằng số thực, hay gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt;?&lt;/li&gt;
  &lt;li&gt;Hai nút A và B đều gắn ngắt, trong đó ISR của nút A gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt;. Một cạnh xuống xuất hiện trên nút B sau nút A 4 ms. ISR của nút B bị trễ bao lâu, và điều này ảnh hưởng thế nào đến phép kiểm tra timeout 300 ms trong vòng lặp chính?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý&lt;/summary&gt;

1. Không có bộ lọc, 6 cạnh xuống tạo thành 6 lần đếm. Với bộ lọc 20 ms, cạnh cuối ở 18 ms nên trạng thái `LOW` được xác nhận lúc $18 + 20 = 38$ ms và số đếm là 1.
2. `Serial` sử dụng bộ đệm và thường phụ thuộc vào ngắt UART; gọi trong ISR kéo dài thời gian xử lý và có nguy cơ gây khóa chết (deadlock) với chính ngắt UART. Chỉ đặt cờ trong ISR và in ở `loop()`.
3. Chỉ thao tác tăng biến `volatile uint32_t` thuộc về ISR; mọi phép tính tốc độ và lệnh xuất PWM phải thực hiện trong vòng lặp chính.
4. ISR của nút B phải đợi `delay(20)` của nút A kết thúc nên chạy trễ khoảng 16 ms. Toàn bộ vòng lặp chính — bao gồm phép kiểm tra mất lệnh 300 ms — cũng bị chặn suốt 20 ms đó.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;attachInterrupt&lt;/code&gt;&lt;/a&gt; — tài liệu Arduino về các chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RISING&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt; và yêu cầu giữ ISR ngắn gọn.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.micropython.org/en/latest/reference/isr_rules.html&quot;&gt;Quy tắc ISR của MicroPython&lt;/a&gt; — hướng dẫn tránh cấp phát heap và đưa phần xử lý chính về vòng lặp.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>PWM, timer và tín hiệu tốc độ động cơ</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_03_pwm_timers/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_03_pwm_timers</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn giải thích được tác động của hệ số chu kỳ (duty cycle) lên điện áp trung bình của tải thuần trở và lý do mối quan hệ đó chỉ mang tính &lt;em&gt;xấp xỉ&lt;/em&gt; đối với động cơ DC cỡ nhỏ. Bạn lập trình được hiệu ứng LED sáng mờ dần (breathing) trên chân GPIO an toàn bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; trong lõi ESP32 Arduino 3.x, hiểu vì sao không nên mặc định &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; luôn có độ phân giải 8-bit (0–255) trên nền tảng này, đồng thời viết được chương trình tương đương trong MicroPython với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;. Bạn cũng nắm được vì sao mức duty 20% trên nguồn động cơ 7,4 V thường chưa đủ để khởi động động cơ giảm tốc TT và vì sao bài thực hành này vẫn dùng LED làm tải thay vì nối động cơ thật.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã quen với giao tiếp serial từ bài &lt;a href=&quot;/contents/vi/chapter02/02_04_blink_serial_hello/&quot;&gt;nhịp tim chương 02&lt;/a&gt; và vòng lặp không chặn từ bài 1 chương này, nhờ đó quá trình tăng giảm độ sáng sẽ không dùng các lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; chặn toàn bộ chương trình. Tải thí nghiệm là LED kèm điện trở nối tiếp từ bài GPIO; mạch cầu H TB6612 chỉ được nhắc đến như thiết bị nhận tín hiệu về sau và chưa đấu dây trong bài này.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Vi điều khiển là cỗ máy số thuần túy — chân GPIO của nó chỉ biết đúng hai trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0 V&lt;/code&gt; (Tắt) hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3,3 V&lt;/code&gt; (Bật), hoàn toàn không có núm vặn biến trở để xuất ra điện áp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1,65 V&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2,1 V&lt;/code&gt;! Vậy làm sao nó điều khiển được động cơ quay êm ở 30%, 60% hay 85% tốc độ? Bí mật nằm ở &lt;strong&gt;điều chế độ rộng xung (PWM — Pulse Width Modulation)&lt;/strong&gt;: bật tắt cực nhanh hàng chục nghìn lần mỗi giây bằng &lt;strong&gt;bộ định thời phần cứng (Hardware Timer)&lt;/strong&gt; để quán tính cơ học của động cơ tự lấy trung bình thành một điện áp mượt mà!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bộ-định-thời-phần-cứng-timer-và-điều-chế-độ-rộng-xung-pwm&quot;&gt;Kiến thức cần nhớ: Bộ định thời Phần cứng (Timer) và Điều chế Độ rộng Xung (PWM)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chu kỳ (\(T\)), Tần số (\(f = 1/T\)) và Hệ số Xung (Duty Cycle \(D\)):&lt;/strong&gt; Trong mỗi chu kỳ \(T\), tín hiệu giữ mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; trong thời gian \(t_{\text{on}}\) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; trong \(T - t_{\text{on}}\). Tỉ lệ \(D = \dfrac{t_{\text{on}}}{T} \in [0, 1]\) quyết định điện áp trung bình cấp vào tải: \(V_{\text{avg}} = D \cdot V_{\text{nguồn}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Độ phân giải (Resolution — 8-bit, 10-bit, 16-bit):&lt;/strong&gt; PWM 8-bit chia mức tốc độ thành \(2^8 = 256\) nấc (0–255); PWM 10-bit chia thành \(2^{10} = 1{,}024\) nấc (0–1023) — độ phân giải càng cao, bộ điều khiển PID chỉnh tốc độ càng mịn!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chọn Tần số PWM cho Động cơ DC (Ngưỡng Âm thanh 20 kHz):&lt;/strong&gt; Nếu chạy PWM ở 500 Hz – 1 kHz (mặc định của Arduino Uno), cuộn dây động cơ rung theo tần số đó và phát ra tiếng rít chói tai! Tăng tần số PWM lên &lt;strong&gt;20–25 kHz&lt;/strong&gt; (vượt ngưỡng nghe của tai người) làm xe chạy im phăng phắc trong khi vẫn giữ tổn hao chuyển mạch MOSFET thấp.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-thông-số-pwm-10-bit-ở-tần-số-20-khz-cho-động-cơ-dc&quot;&gt;Ví dụ làm từng bước: Tính thông số PWM 10-bit ở tần số 20 kHz cho động cơ DC&lt;/h3&gt;

&lt;p&gt;Giả sử bạn cấu hình kênh LEDC trên ESP32 ở tần số &lt;strong&gt;\(f_{\text{PWM}} = 20\text{ kHz}\)&lt;/strong&gt;, độ phân giải &lt;strong&gt;10-bit&lt;/strong&gt; (\(0\text{–}1023\)), lái động cơ qua TB6612FNG với điện áp hiệu dụng sau sụt áp driver là &lt;strong&gt;\(V_m = 7.0\text{ V}\)&lt;/strong&gt;. Bạn ghi giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty = 614&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chu kỳ một xung PWM:&lt;/strong&gt;
\(T = \frac{1}{20{,}000\text{ Hz}} = \mathbf{50\ \mu\text{s}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hệ số xung (Duty Cycle):&lt;/strong&gt;
\(D = \frac{614}{1{,}023} \approx \mathbf{60.0\%}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thời gian bật (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;) và tắt (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt;) trong mỗi chu kỳ:&lt;/strong&gt;
\(t_{\text{on}} = 0.60 \times 50\ \mu\text{s} = \mathbf{30\ \mu\text{s}}, \qquad t_{\text{off}} = 50 - 30 = \mathbf{20\ \mu\text{s}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Điện áp trung bình động cơ cảm nhận được:&lt;/strong&gt;
\(V_{\text{avg}} = 0.60 \times 7.0\text{ V} = \mathbf{4.20\text{ V}}!\)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-4-lưu-ý-thực-chiến-khi-dùng-pwm-và-timer-trên-robot&quot;&gt;Giải thích: 4 lưu ý thực chiến khi dùng PWM và Timer trên robot&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Dùng Timer phần cứng (LEDC trên ESP32 / PWM slice trên Pico), không tự tạo PWM bằng vòng lặp phần mềm:&lt;/strong&gt; Timer phần cứng tự phát xung chính xác tới nano-giây mà không tốn 1% CPU nào!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Biết giới hạn “vùng chết” (Deadzone) của động cơ DC:&lt;/strong&gt; Ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty = 10%&lt;/code&gt; (\(V_{\text{avg}} = 0.7\text{ V}\)), ma sát tĩnh của hộp số lớn hơn mô-men động cơ nên bánh xe đứng im mà cuộn dây vẫn ăn dòng nóng lên — luôn đo ngưỡng khởi động tối thiểu (thường 20–30%) của từng động cơ!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Kiểm tra trần tần số PWM của IC Driver:&lt;/strong&gt; TB6612FNG hỗ trợ tối đa &lt;strong&gt;100 kHz&lt;/strong&gt; (chạy 20 kHz cực đẹp), còn các driver rơ-le hay mạch quang cách ly chậm không chịu được tần số cao.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Không nhầm PWM động cơ (20 kHz) với PWM điều khiển Servo RC (50 Hz):&lt;/strong&gt; Servo RC tiêu chuẩn yêu cầu chính xác chu kỳ &lt;strong&gt;20 ms (50 Hz)&lt;/strong&gt; với độ rộng xung từ &lt;strong&gt;1,0 ms đến 2,0 ms&lt;/strong&gt; (duty chỉ 5%–10%) để mã hóa góc quay \(0^\circ\text{–}180^\circ\)!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Để điều khiển góc giữa \(90^\circ\) của một động cơ servo RC chuẩn (chu kỳ \(20\text{ ms}\), xung mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; dài \(1.5\text{ ms}\)), hệ số xung (duty cycle) bằng bao nhiêu phần trăm? Đúng \(\dfrac{1.5\text{ ms}}{20\text{ ms}} = \mathbf{7.5\%}\)!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Firmware điều khiển từ xa (teleop) của Capstone A không gửi trực tiếp giá trị mô-men xoắn mà gửi hai tín hiệu PWM tới chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMB&lt;/code&gt; của TB6612 để mạch cầu H đóng cắt nguồn động cơ tương ứng. Khi mất lệnh sau 300 ms, cả hai mức duty phải đưa về 0 để dừng bánh xe. Thực hành trực tiếp các mức 0%, 25%, 50% và 90% trên LED giúp bạn kiểm chứng tín hiệu điều khiển trước khi ghép vào hệ cơ khí. Không nối động cơ thẳng vào GPIO vì chân vi điều khiển không chịu được dòng cuộn dây; ngay cả khi đã nối mạch cầu H ở chương sau, vẫn phải nhấc bánh khỏi bàn trong lần thử duty thấp đầu tiên.&lt;/p&gt;

&lt;h2 id=&quot;nội-dung&quot;&gt;Nội dung&lt;/h2&gt;

&lt;p&gt;Bộ định thời (timer) tạo tín hiệu PWM bằng cách giữ chân ở mức cao trong một phần cố định của chu kỳ và mức thấp ở phần còn lại. Hệ số chu kỳ (duty cycle) $D$ là tỉ lệ thời gian ở mức cao trong một chu kỳ ($0 \le D \le 1$). Với tải thuần trở nối vào nguồn ổn định, điện áp trung bình xấp xỉ:&lt;/p&gt;

\[V_{\mathrm{tb}} \approx D \cdot V_{\mathrm{nguồn}}.\]

&lt;p&gt;LED mắc nối tiếp điện trở đáp ứng khá sát mô hình này: độ sáng thay đổi theo $D$ (khi điện áp đỉnh vượt điện áp thuận của LED), còn mắt người sẽ cảm nhận độ sáng trung bình thay vì thấy từng xung đóng ngắt riêng lẻ.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/pwm_duty_cycle.png&quot; alt=&quot;Duty 25%, 50% và 90%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Động cơ DC có đặc tính khác tải thuần trở: cuộn dây vừa có điện trở vừa có điện cảm, đồng thời rô-to sinh sức phản điện động khi quay. Duty cycle vẫn quyết định điện áp trung bình đặt lên cuộn dây, nhưng tốc độ quay chỉ tỉ lệ thô với điện áp này và tồn tại vùng chết ở mức duty thấp do ma sát hộp số và sụt áp chổi than. Tăng $D$ từ 0,05 lên 0,15 có thể chưa làm trục quay; khi vượt qua ngưỡng khởi động, động cơ sẽ giật quay đột ngột thay vì tăng tuyến tính từ 0.&lt;/p&gt;

&lt;p&gt;Tần số PWM cũng quan trọng không kém duty. Ở tần số thấp quanh 100 Hz, nhiều động cơ giảm tốc nhỏ phát tiếng kêu lạch cạch hoặc rít vì mô-men biến thiên theo từng chu kỳ 10 ms. Với LED, tần số khoảng 1 kHz đã đủ để mắt không thấy nhấp nháy. Với động cơ DC, tần số PWM thường chọn trong khoảng 1 kHz đến 20 kHz để giảm gợn dòng và đẩy tiếng rít ra ngoài ngưỡng nghe — mức 20 kHz là lựa chọn tiêu chuẩn cho ngõ vào TB6612 về sau. Trong bài thực hành LED này, giữ tần số ở 1 kHz là phù hợp.&lt;/p&gt;

&lt;p&gt;Trên các bo mạch Arduino AVR cổ điển, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite(pin, 128)&lt;/code&gt; mặc định tương ứng khoảng nửa thang 8-bit (0–255). Quy ước này không tự động giữ nguyên trên lõi Arduino ESP32. Ở ESP32 Arduino 3.x, API chuẩn là ngoại vi LEDC: bạn gắn chân với tần số và độ phân giải tường minh qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach&lt;/code&gt;, rồi ghi giá trị qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt;. Khi cấu hình 8-bit, thang giá trị là 0–255; nhưng nếu cấu hình 10-bit (0–1023), giá trị 255 chỉ tương đương khoảng 1/4 thang. Hãy luôn dùng lời gọi LEDC tường minh thay vì chép nguyên đoạn mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; từ AVR.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_PIN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;// an toàn trên DevKit ESP32 thông thường; không phải chân strap&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_HZ&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// lab LED. Kênh motor sau này muốn 20000.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint8_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_BITS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// duty 0..255 chỉ vì độ phân giải là 8&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcAttach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PWM_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_HZ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_BITS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;constrain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PWM_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;duty=%d frac=%.2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;255.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Chu kỳ 15 ms được quản lý bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; (không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(15)&lt;/code&gt;), bảo đảm vòng lặp vẫn sẵn sàng kiểm tra ngưỡng timeout 300 ms khi tích hợp sau này.&lt;/p&gt;

&lt;p&gt;Trong MicroPython, độ rộng xung được truyền qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt; (thang 16-bit từ 0 đến 65535):&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pwm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PWM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty_u16&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# LED. freq=20000 khi sau này là ngõ vào motor.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1200&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;65535&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;65535&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pwm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;duty_u16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;duty_u16&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Một hệ số chu kỳ $D$ tương ứng với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int(D * 65535)&lt;/code&gt;, còn tham số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;freq&lt;/code&gt; tính bằng hertz. Giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;freq=1000&lt;/code&gt; cho bài LED và chỉ chuyển lên 20000 Hz khi điều khiển mạch cầu H TB6612.&lt;/p&gt;

&lt;p&gt;Cần đặc biệt tránh các chân strapping của ESP32 (thường là GPIO 0, GPIO 2, GPIO 12 và GPIO 15), vì chip đọc trạng thái của chúng tại thời điểm reset để chọn chế độ khởi động và điện áp flash. Nếu nối mạch ngoài làm kéo mức các chân này, chip có thể không khởi động hoặc nạp chương trình thất bại. Nên chọn GPIO 18 hoặc GPIO 19 cho bài thực hành PWM trên các bản DevKit phổ biến, đồng thời luôn đối chiếu lại sơ đồ chân thực tế của bo mạch đang dùng.&lt;/p&gt;

&lt;p&gt;Khi chuyển sang phần động cơ ở chương sau, mạch cầu H TB6612 nhận tín hiệu PWM mức logic ở các chân điều khiển và đóng cắt nguồn động cơ riêng qua cực &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; (dải khoảng 4,5 V đến 10 V, dòng liên tục khoảng 1,2 A trên module thông dụng). Nhờ tách biệt này, pin 7,4 V chỉ nối vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; chứ không chạm GPIO. Tham khảo sản phẩm cho tuần lắp mạch sau: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-tb6612fng-dc-motor-driver&quot;&gt;TB6612FNG tại Hshop&lt;/a&gt; giá niêm yết 45000₫. Trong chương 03, cực &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; vẫn để hở cho đến khi máy trạng thái ở bài 5 đảm bảo đưa được duty về 0.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Với bộ pin 2S có điện áp danh định 7,4 V, mức duty 20% tạo ra điện áp trung bình xấp xỉ:&lt;/p&gt;

\[V_{\mathrm{tb}} \approx 0{,}20 \times 7{,}4 = 1{,}48\,\mathrm{V}.\]

&lt;p&gt;Ở mức khoảng 1,5 V này, động cơ giảm tốc TT vàng thường chưa thắng được ma sát chổi than và hộp số để bắt đầu quay. Do đó, các thử nghiệm chuyển động đầu tiên (với bánh nhấc khỏi bàn) thường bắt đầu từ khoảng 30% đến 40%:&lt;/p&gt;

\[0{,}30 \times 7{,}4 \approx 2{,}2\,\mathrm{V}, \qquad 0{,}40 \times 7{,}4 \approx 3{,}0\,\mathrm{V}.\]

&lt;p&gt;Các con số trên áp dụng khi nguồn 7,4 V đi qua mạch cầu H; tuyệt đối không đặt 7,4 V lên GPIO vì vượt quá giới hạn chịu áp của chân.&lt;/p&gt;

&lt;p&gt;Trên mạch LED thí nghiệm:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;20% tương ứng giá trị 8-bit là $0{,}20 \times 255 \approx 51$, hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt; là $0{,}20 \times 65535 \approx 13107$.&lt;/li&gt;
  &lt;li&gt;50% tương ứng 128 (8-bit) và 32768 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;90% tương ứng khoảng 230 (8-bit) và khoảng 58982 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hãy ghi lại các giá trị này trong log để làm bằng chứng cho checklist cuối chương.&lt;/p&gt;

&lt;p&gt;Về tần số: ở 100 Hz, mỗi chu kỳ dài $1/100 = 10$ ms nên mô-men động cơ bị ngắt quãng rõ rệt và gây tiếng kêu; nâng lên 20 kHz rút ngắn chu kỳ còn $50$ µs, giúp độ tự cảm cuộn dây làm phẳng dòng điện. Với LED, mắt không phân biệt được 1 kHz với 20 kHz nên không cần tăng lên 20 kHz trong bài thực hành này.&lt;/p&gt;

&lt;h2 id=&quot;bài-lab&quot;&gt;Bài lab&lt;/h2&gt;

&lt;p&gt;Dùng LED và điện trở 180 Ω từ bài GPIO nối vào GPIO 18 (trừ khi nhãn mạch cho biết chân đó đã dùng cho chức năng khác); chỉ cấp nguồn qua USB.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Cấu hình PWM ở 1 kHz, độ phân giải 8-bit (hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt; trong MicroPython) và chạy hiệu ứng sáng mờ dần. Quan sát LED sáng/tối mượt mà và giá trị trên serial tăng giảm đều. Chụp ảnh LED ở một mức tối và một mức sáng, hoặc dán ba dòng log có duty quanh 25%, 50% và 90%.&lt;/li&gt;
  &lt;li&gt;Đặt cố định duty bằng 0 và xác nhận LED tắt hẳn; đặt duty bằng 255 (hoặc 65535) và xác nhận độ sáng tương đương khi xuất mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; liên tục.&lt;/li&gt;
  &lt;li&gt;Hạ thử tần số xuống 100 Hz trong vài giây để quan sát hiện tượng nhấp nháy trên LED (tương tự hiện tượng giật/lạch cạch trên động cơ), sau đó đưa lại về 1 kHz.&lt;/li&gt;
  &lt;li&gt;Không nối chân LED sang pin hay động cơ. Nếu bo mạch không khởi động lại được sau khi cắm dây, kiểm tra xem có dây nào cắm nhầm vào chân strapping (GPIO 0, 2, 12 hoặc 15) hay không và dời sang chân khác.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [ESP32 + PWM + Android&lt;/td&gt;
          &lt;td&gt;What is PWM?&lt;/td&gt;
          &lt;td&gt;Pulse Width Modulation](https://www.youtube.com/watch?v=vzdAOaAk5JQ) — Slavko’s Hobby Channel, tiếng Anh. Giải thích nguyên lý điều rộng xung và cách duty thay đổi điện áp trung bình (có thể bỏ qua phần điều khiển bằng điện thoại).&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vbAZdQGTA0s&quot;&gt;Quick Guide to Servos&lt;/a&gt; — SparkFun Electronics, tiếng Anh. Giới thiệu tín hiệu servo như một trường hợp riêng của PWM; bài này chỉ tập trung vào PWM điều chỉnh tốc độ động cơ DC.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Với pin 7,4 V và mức duty thử nghiệm ban đầu là 0,35 (bánh nhấc khỏi bàn), tính điện áp trung bình đặt lên động cơ, đồng thời quy đổi 0,35 sang giá trị 8-bit cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; và giá trị 16-bit cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Một sinh viên gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach(18, 1000, 10)&lt;/code&gt; rồi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite(18, 255)&lt;/code&gt; với kỳ vọng đèn sáng một nửa. Tỉ lệ duty thực tế đang xuất là bao nhiêu, và cần ghi số nào để đạt nửa thang 10-bit?&lt;/li&gt;
  &lt;li&gt;Vì sao tần số 100 Hz không phù hợp cho động cơ DC nhưng vẫn có thể dùng tạm ở bước 3 của bài thực hành với LED?&lt;/li&gt;
  &lt;li&gt;Nêu hai chân GPIO trên ESP32 cần tránh khi chạy thí nghiệm PWM và chân đang được dùng trong mã mẫu. Các chân cần tránh đó ảnh hưởng đến quá trình gì khi khởi động chip?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý&lt;/summary&gt;

1. Điện áp trung bình: $0{,}35 \times 7{,}4 \approx 2{,}6$ V. Duty 8-bit: `round(0.35 * 255) = 89`. Giá trị `duty_u16`: `round(0.35 * 65535) = 22937`.
2. Thang 10-bit chạy từ 0 đến 1023, nên 255 chỉ khoảng 1/4 thang (~25%), không phải một nửa. Nửa thang 10-bit là 512.
3. Ở 100 Hz, động cơ nhận xung mô-men mỗi 10 ms nên rung và kêu lạch cạch; với LED, 100 Hz chỉ gây nhấp nháy nhẹ để minh họa rồi chỉnh lại về 1 kHz.
4. Tránh các chân strapping như GPIO 0, 2, 12 và 15 (được chip đọc lúc reset để xác định chế độ boot và điện áp flash). Mã mẫu dùng GPIO 18.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html&quot;&gt;LEDC trên Arduino ESP32&lt;/a&gt; — tài liệu chính thức về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach(pin, freq, resolution)&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; trên lõi 3.x.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.micropython.org/en/latest/library/machine.PWM.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;machine.PWM&lt;/code&gt; của MicroPython&lt;/a&gt; — hướng dẫn sử dụng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;freq&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt; (thang 0–65535).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu mã 713&lt;/a&gt; — tài liệu tham khảo cho mạch cầu H TB6612FNG trước khi lắp ráp ở các chương sau.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab — mẫu GPIO cho nút nhấn và LED</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_02_gpio_patterns/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_02_gpio_patterns</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài thực hành này, bạn đấu nối được nút nhấn từ chân GPIO xuống mass (GND) sao cho trạng thái nhả đọc mức cao và trạng thái nhấn đọc mức thấp nhờ cấu hình kéo lên nội (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; hoặc tương đương trong MicroPython). Bạn biết tính điện trở nối tiếp cho LED trên đường nguồn 3,3 V, viết được chương trình chỉ in cạnh lên và cạnh xuống bằng Arduino C++ và MicroPython, đồng thời chỉ xác nhận thay đổi trạng thái sau khi tín hiệu thô ổn định trong 20 ms. Bạn cũng nhận diện được ba lỗi phổ biến: chân để nổi khi quên kéo lên, LED thiếu điện trở hạn dòng và nút mắc vào 3,3 V mà không có kéo xuống khiến chân luôn đọc mức cao. Bài này chưa cấp điện cho động cơ.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Từ bài &lt;a href=&quot;/contents/vi/chapter02/02_04_blink_serial_hello/&quot;&gt;nhịp tim chương 02&lt;/a&gt;, bạn đã thiết lập được Serial Monitor ở 115200 baud và biết đổi chân LED nếu nhãn mạch không dùng GPIO 2. Bài 1 chương này đã thay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; bằng đồng hồ không chặn, và các chương trình dưới đây tiếp tục giữ nguyên tắc đó. Linh kiện cần chuẩn bị gồm breadboard, một nút nhấn nhả (tức thời), một LED, điện trở quanh mức 180 Ω và dây nối; chưa cần TB6612, pin hay khung xe.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Nghe tưởng chừng quá đơn giản: nối một chiếc nút nhấn vào chân GPIO rồi đọc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;digitalRead()&lt;/code&gt; xem là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;! Thế nhưng ngay lần đầu thử trên breadboard, rất nhiều người sửng sốt thấy màn hình Serial tự động nhảy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0, 1, 1, 0, 1&lt;/code&gt; điên cuồng dù ngón tay &lt;strong&gt;chưa hề chạm vào nút nhấn&lt;/strong&gt;! Hiện tượng ma quái đó gọi là &lt;strong&gt;chân thả nổi (floating input)&lt;/strong&gt; — và cách khắc phục nó chỉ cần đúng một điện trở kéo lên (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt;) và tư duy &lt;em&gt;logic tích cực mức thấp (active-low)&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-trạng-thái-điện-của-chân-gpio-push-pull-floating-và-pull-up&quot;&gt;Kiến thức cần nhớ: Trạng thái Điện của Chân GPIO (Push-Pull, Floating và Pull-Up)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chân Thả nổi (Floating Input):&lt;/strong&gt; Một chân GPIO ở chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT&lt;/code&gt; có trở kháng đầu vào cực lớn (~hàng chục mega-ohm); khi nút nhấn nhả ra, sợi dây cắm hoạt động y hệt một chiếc &lt;strong&gt;ăng-ten&lt;/strong&gt; thu nhiễu điện từ 50 Hz từ lưới điện phòng học, làm điện áp dao động lung tung giữa 0 V và 3,3 V!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Điện trở Kéo lên Nội bộ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt;) &amp;amp; Logic Active-Low:&lt;/strong&gt; Kéo chân GPIO lên 3,3 V qua một điện trở nội bộ (\(R_{\text{pull}} \approx 45\text{ k}\Omega\) trên ESP32, ~\(50\text{ k}\Omega\) trên Pico) và nối nút nhấn xuống &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;. Khi &lt;strong&gt;nhả nút&lt;/strong&gt;, chân ổn định ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH (1)&lt;/code&gt;; khi &lt;strong&gt;nhấn nút&lt;/strong&gt;, chân nối thẳng xuống &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; đọc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW (0)&lt;/code&gt; (&lt;em&gt;active-low&lt;/em&gt;) — loại bỏ hoàn toàn nhiễu thả nổi mà không cần thêm điện trở ngoài!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ngõ ra Đẩy–Kéo (Push-Pull) &amp;amp; Ranh giới Dòng Cấp/Hút (Source/Sink):&lt;/strong&gt; Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OUTPUT&lt;/code&gt; dùng cặp MOSFET đẩy lên 3,3 V (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;) hoặc kéo xuống 0 V (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt;); tổng dòng qua tất cả các chân GPIO cùng lúc không được vượt quá giới hạn của cả chip.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-dòng-rò-qua-điện-trở-kéo-lên-nội-bộ-input_pullup&quot;&gt;Ví dụ làm từng bước: Tính dòng rò qua điện trở kéo lên nội bộ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt;)&lt;/h3&gt;

&lt;p&gt;Giả sử bạn dùng chân GPIO 3,3 V của ESP32 với điện trở kéo lên nội bộ \(R_{\text{pull}} = 45\text{ k}\Omega\) nối tới một nút nhấn khẩn cấp (đầu kia nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Khi nhả nút:&lt;/strong&gt; mạch hở, dòng tiêu thụ \(I = \mathbf{0\ \mu\text{A}}\), điện áp tại chân GPIO là \(3.3\text{ V}\) (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Khi giữ nút nhấn:&lt;/strong&gt; dòng chạy từ đường 3,3 V qua \(45\text{ k}\Omega\) xuống &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; là:
\(I = \frac{3.3\text{ V}}{45{,}000\ \Omega} \approx \mathbf{73.3\ \mu\text{A}} \quad (0.073\text{ mA}!),\)
công suất tiêu tán chỉ \(3.3 \times 73.3\ \mu\text{A} \approx \mathbf{0.24\text{ mW}}\) — vừa chống nhiễu tuyệt đối, vừa gần như không tốn pin!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bẫy sai lầm phổ biến:&lt;/strong&gt; nếu bạn nối nút nhấn trực tiếp giữa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; mà quên đặt qua điện trở, bấm nút là gây &lt;strong&gt;ngắn mạch trực tiếp&lt;/strong&gt; làm sập nguồn toàn bộ bo mạch!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-mẫu-thiết-kế-gpio-sạch-cho-firmware-robot&quot;&gt;Giải thích: 4 mẫu thiết kế GPIO sạch cho firmware robot&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Luôn dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; cho nút nhấn và công tắc hành trình:&lt;/strong&gt; Vừa gọn dây (chỉ cần kéo dây nút nhấn về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, không cần kéo đường nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; ra vỏ khung kim loại dễ chập), vừa chống nhiễu thả nổi triệt để.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Đọc trạng thái bằng hàm bọc có tên rõ nghĩa:&lt;/strong&gt; Viết &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bool is_button_pressed() { return digitalRead(PIN_BTN) == LOW; }&lt;/code&gt; để không bao giờ bị loạn đầu giữa mức điện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; và ý nghĩa logic “đang nhấn”.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Tách rõ phát hiện &lt;em&gt;mức&lt;/em&gt; (level) với phát hiện &lt;em&gt;cạnh&lt;/em&gt; (edge):&lt;/strong&gt; Muốn chuyển chế độ mỗi lần bấm 1 cái, phải so sánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pressed &amp;amp;&amp;amp; !prev_pressed&lt;/code&gt; (bắt cạnh nhấn) thay vì chỉ kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if (pressed)&lt;/code&gt; (sẽ đảo chế độ 500 lần mỗi giây!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Luôn định nghĩa trạng thái ban đầu an toàn trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt;:&lt;/strong&gt; Ghi mức tắt động cơ/LED ngay khi cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pinMode(..., OUTPUT)&lt;/code&gt; để tránh giật bánh xe lúc mới bật nguồn.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Một công tắc hành trình cản trước của xe robot dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; (active-low). Nếu sợi dây nối công tắc bị đứt giữa chừng khi xe đang chạy, chân GPIO sẽ đọc mức gì? Nó sẽ kéo lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; (không nhấn) — đó là lý do trong hệ thống an toàn công nghiệp, &lt;strong&gt;nút dừng khẩn cấp (E-Stop)&lt;/strong&gt; luôn dùng tiếp điểm &lt;em&gt;thường đóng (NC)&lt;/em&gt;: dây đứt lập tức báo dừng xe an toàn!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Trên Capstone A, nút nhấn đại diện cho thao tác xác nhận của người vận hành — chẳng hạn xóa trạng thái lỗi (FAULT) hoặc cho phép robot thoát trạng thái dừng. Nếu chân nút bị để nổi, nhiễu điện sẽ tạo ra các lần nhấn giả và có thể tự ý xóa lỗi mà bạn chưa kiểm tra. Tương tự, khi LED đóng vai trò đèn báo trạng thái, thiếu điện trở hạn dòng sẽ gây quá dòng cho chân GPIO. Mọi thử nghiệm trong bài chỉ làm việc với tín hiệu vào và LED chỉ thị; động cơ tuyệt đối không nối trực tiếp vào GPIO mà phải đi qua mạch cầu H riêng ở các chương sau.&lt;/p&gt;

&lt;h2 id=&quot;nội-dung&quot;&gt;Nội dung&lt;/h2&gt;

&lt;p&gt;GPIO là chân tín hiệu số có thể cấu hình làm ngõ vào hoặc ngõ ra. Khi bật điện trở kéo lên nội (internal pull-up) cho ngõ vào, chân được kéo yếu lên mức 3,3 V; đầu còn lại của nút nhấn nối xuống mass (GND), không nối lên 3,3 V. Khi nhả nút, mạch hở nên điện trở kéo lên giữ chân ở mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;. Khi nhấn nút, tiếp điểm đóng thẳng xuống mass (mạnh hơn điện trở kéo lên nội) nên chân đọc mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt;. Với người mới, logic đảo này (“nhấn = LOW”) đôi khi gây ngược trực giác, vì vậy hãy ghi chú rõ ngay trong mã nguồn để tránh nhầm lẫn về sau.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/gpio_pullup_button.png&quot; alt=&quot;Nút xuống mass, GPIO kéo lên&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Nếu vi điều khiển không có kéo lên nội, bạn dùng điện trở ngoài 10 kΩ nối từ GPIO lên 3,3 V và vẫn nối nút từ GPIO xuống mass. Giá trị 10 kΩ đủ nhỏ để chống nhiễu khi chạm tay vào dây, nhưng đủ lớn để khi giữ nút chỉ tiêu thụ dòng $3{,}3 / 10000 = 0{,}33$ mA.&lt;/p&gt;

&lt;p&gt;Mỗi đèn LED chỉ thị cần một điện trở nối tiếp riêng. Một LED thông dụng có điện áp thuận khoảng 2,0 V và dòng làm việc an toàn, đủ sáng từ chân GPIO 3,3 V là 8 mA. Áp dụng định luật Ohm cho phần điện áp rơi trên điện trở:&lt;/p&gt;

\[R = \frac{V_{CC} - V_F}{I_F} = \frac{3{,}3 - 2{,}0}{0{,}008} = 162{,}5\,\Omega.\]

&lt;p&gt;Giá trị tiêu chuẩn gần nhất phổ biến trong bộ điện trở 1/4 W là 180 Ω, cho dòng thực tế thấp hơn mục tiêu một chút:&lt;/p&gt;

\[I = \frac{3{,}3 - 2{,}0}{180} \approx 7{,}2\,\mathrm{mA}, \qquad P = I^2 R \approx 9\,\mathrm{mW}.\]

&lt;p&gt;Công suất 9 mW rất nhỏ so với định mức 250 mW của điện trở 1/4 W nên linh kiện hoàn toàn không nóng; giá trị 220 Ω cũng dùng tốt, chỉ tối hơn một chút. Nếu bỏ qua điện trở nối tiếp, dòng không được giới hạn sẽ dễ làm cháy LED hoặc hỏng tầng đệm chân GPIO. Khi đấu dây, cực âm (cathode) của LED nối về mass, cực dương (anode) nối qua điện trở về chân GPIO (hoạt động trên đường nguồn 3,3 V).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/led_series_resistor.png&quot; alt=&quot;LED, điện trở nối tiếp, và mass&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Tiếp điểm cơ khí không đóng ngắt dứt khoát trong một lần mà bị dội (bounce) trong thời gian ngắn; bài sau sẽ phân tích kỹ hiện tượng này. Trong bài này, ta áp dụng quy tắc lọc đơn giản trong vòng lặp: chỉ công nhận trạng thái mới khi tín hiệu thô giữ nguyên không đổi suốt 20 ms. Lưu mẫu thô gần nhất và mốc thời gian thay đổi; mỗi khi tín hiệu thô đổi mức thì đặt lại đồng hồ. Khi tín hiệu thô giữ ổn định đủ 20 ms và khác với trạng thái đã xác nhận trước đó, ta cập nhật trạng thái và in ra đúng một dòng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt; ở cạnh xuống, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt; ở cạnh lên). Không in ở mọi vòng lặp để tránh làm ngập màn hình serial.&lt;/p&gt;

&lt;p&gt;Tuyệt đối không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; để chống dội, vì hàm chặn sẽ làm đứng nhịp tim và làm giảm khả năng đáp ứng của vòng lặp điều khiển; kiểm tra theo mốc thời gian giúp vòng lặp luôn rảnh để xử lý tác vụ khác.&lt;/p&gt;

&lt;p&gt;Ba lỗi đấu nối thường gặp nhất:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Chân để nổi (floating pin):&lt;/strong&gt; đấu nút xuống mass nhưng quên cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.PULL_UP&lt;/code&gt;. Khi nhả nút, sợi dây hoạt động như một anten thu nhiễu khiến serial tự in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt; liên tục dù không ai chạm. Hãy kiểm tra lại chế độ chân trước khi sửa phần chống dội.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;LED thiếu điện trở nối tiếp:&lt;/strong&gt; LED có thể lóe sáng gắt rồi hỏng, hoặc buộc chân GPIO cấp dòng vượt định mức. Luôn lắp điện trở khoảng 180 Ω nối tiếp dù LED trông có vẻ vẫn sáng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nút luôn đọc mức cao:&lt;/strong&gt; đấu nút giữa 3,3 V và GPIO trong khi vẫn bật kéo lên nội (và không có kéo xuống). Khi nhả, kéo lên đưa chân về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;; khi nhấn, nút nối thẳng lên 3,3 V cũng là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;, nên không bao giờ xuất hiện cạnh tín hiệu. Hãy chuyển đầu nút sang GND theo chuẩn của môn học.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Không cấp nguồn động cơ, cuộn dây rơ-le hay chân VM của TB6612 từ GPIO. GPIO chỉ xuất tín hiệu logic điều khiển, không cấp dòng công suất.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arduino C++ cho ESP32:&lt;/strong&gt; nút nhấn tại GPIO 4, LED tại GPIO 18 (cả hai đều tránh nhóm chân strapping sẽ đề cập ở bài PWM).&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INPUT_PULLUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// nhả thì cao, nhấn thì thấp&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;gpio edges&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digitalRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// LED sáng khi đang giữ&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;press&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;release&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trong đoạn mã trên, đèn LED sáng trong suốt thời gian giữ nút để dễ quan sát, còn cổng serial chỉ in ra đúng tại các cạnh đã xác nhận.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MicroPython:&lt;/strong&gt; giữ cùng số chân nếu dùng ESP32; trên Pico, đổi sang các chân GP trống tương ứng và giữ nguyên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.PULL_UP&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PULL_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;gpio edges&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;press&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;release&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Giả sử bạn nhấn nút tại $t = 1000$ ms và nhả tại $t = 1600$ ms; tiếp điểm dội trong 8 ms sau mỗi thao tác rồi ổn định. Theo luật 20 ms (tính từ cạnh cuối của đợt dội ở $t = 1008$ ms và $t = 1608$ ms, hoặc tại $t = 1020$ ms và $t = 1620$ ms nếu không có dội thêm sau mốc nhấn/nhả), chương trình xác nhận mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt;, in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt; và bật LED, sau đó xác nhận mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;, in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt; và tắt LED. Màn hình chỉ hiển thị:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpio edges
press
release
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu một lần nhấn tạo ra hàng chục dòng liên tiếp, điều kiện lọc ổn định chưa bọc đúng lệnh in. Nếu màn hình không in gì, hãy dùng đồng hồ đo điện áp tại chân nút: nhả gần 3,3 V và nhấn gần 0 V; nếu trạng thái nhả dao động bất định quanh 0,8 V đến 2 V thì chân đang bị nổi.&lt;/p&gt;

&lt;p&gt;Xét thêm một LED có điện áp thuận $V_F = 2{,}1$ V cần dòng 5 mA từ nguồn 3,3 V:&lt;/p&gt;

\[R = \frac{3{,}3 - 2{,}1}{0{,}005} = 240\,\Omega.\]

&lt;p&gt;Trong bộ linh kiện thông dụng, bạn chọn 220 Ω hoặc 270 Ω. Nếu dùng sẵn điện trở 180 Ω, dòng qua LED khoảng $(3{,}3 - 2{,}1) / 180 \approx 6{,}7$ mA — vẫn nằm trong ngưỡng an toàn và sáng hơn một chút.&lt;/p&gt;

&lt;h2 id=&quot;bài-lab&quot;&gt;Bài lab&lt;/h2&gt;

&lt;p&gt;Chỉ cấp nguồn cho bo mạch qua cổng USB; không nối pin và không để dây chạm vào chân 5 V.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Rút cáp USB trước khi cắm dây. Nối một chân nút nhấn vào GPIO 4 và chân đối diện vào GND. Mắc nối tiếp LED và điện trở 180 Ω giữa GPIO 18 và GND (anode phía GPIO, cathode phía GND). Kiểm tra lại sơ đồ trước khi cắm USB.&lt;/li&gt;
  &lt;li&gt;Nạp mã C++ hoặc chạy kịch bản MicroPython, mở Serial Monitor ở 115200 baud. Màn hình chỉ nên hiện một dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gpio edges&lt;/code&gt; rồi đứng yên.&lt;/li&gt;
  &lt;li&gt;Nhấn và nhả nút chậm một lần: log phải hiện đúng một cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt;, đồng thời LED chỉ sáng khi đang giữ nút. Thử nhấn 5 lần và xác nhận thu được đúng 5 cặp.&lt;/li&gt;
  &lt;li&gt;Thử tạm bỏ kéo lên nội (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT&lt;/code&gt; thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt;), chạy lại để quan sát hiện tượng chân nổi tạo ra các cạnh giả, ghi nhận vào sổ rồi khôi phục lại kéo lên.&lt;/li&gt;
  &lt;li&gt;Ghi vào sổ số chân đã dùng, giá trị điện trở và dán một cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt; sạch (kèm ảnh chụp breadboard nếu có) để dùng cho checklist cuối chương.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nếu serial in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt; bình thường nhưng LED không sáng, kiểm tra lại cực tính LED (có thể bị cắm ngược) hoặc số chân. Nếu serial không in gì và đồng hồ đo thấy 3,3 V ở cả hai trạng thái nhấn/nhả, nút đang bị mắc nhầm lên đường 3,3 V thay vì GND.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [ESP32 LED + Push Button Tutorial for Beginners&lt;/td&gt;
          &lt;td&gt;ESP32 GPIO Input &amp;amp; Output](https://www.youtube.com/watch?v=zCzchrkpHzk) — Alvin Pribadi, tiếng Anh. Minh họa thao tác đọc nút nhấn và điều khiển LED trên GPIO, đối chiếu với mẫu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OUTPUT&lt;/code&gt; trong bài.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[What is Switch Bounce and How to Debounce – ATM&lt;/td&gt;
          &lt;td&gt;Digi-Key Electronics](https://www.youtube.com/watch?v=IvU8m_30iK0) — DigiKey, tiếng Anh. Giải thích hiện tượng dội cơ học của nút nhấn gây ra nhiều cạnh tín hiệu nếu chưa chống dội.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một LED đỏ có $V_F = 1{,}8$ V và bạn muốn dòng $I_F = 6$ mA từ nguồn 3,3 V. Tính giá trị điện trở $R$, rồi chọn giá trị tiêu chuẩn gần nhất sao cho dòng thực tế không vượt quá 6 mA.&lt;/li&gt;
  &lt;li&gt;Trạng thái ổn định chuyển từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; tại $t = 250$ ms và trở lại &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; tại $t = 900$ ms (đã qua bộ lọc 20 ms). Hãy liệt kê các dòng serial in ra và trạng thái LED trong từng khoảng thời gian. Những gì không được phép xuất hiện giữa hai mốc này?&lt;/li&gt;
  &lt;li&gt;Một sinh viên nối nút giữa 3,3 V và GPIO 4, khai báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pinMode(4, INPUT_PULLUP)&lt;/code&gt; rồi thắc mắc vì sao LED không đổi trạng thái. Cho biết điện áp tại chân GPIO 4 ở cả hai trạng thái nút và dây cần thay đổi.&lt;/li&gt;
  &lt;li&gt;Nếu vi điều khiển không có kéo lên nội và bạn chỉ có một điện trở 10 kΩ cùng nút nhấn, điện trở này nối giữa hai điểm nào và dòng qua nó khi đang giữ nút là bao nhiêu?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý&lt;/summary&gt;

1. $R = (3{,}3 - 1{,}8) / 0{,}006 = 250\,\Omega$. Chọn giá trị tiêu chuẩn 270 Ω để dòng nhỏ hơn 6 mA (220 Ω sẽ vượt nhẹ mức 6 mA).
2. Log in `press` tại cạnh xác nhận đầu (250 ms) và `release` tại cạnh sau (900 ms). LED chỉ sáng trong khoảng trạng thái ổn định là `LOW`. Không được in thêm các mẫu lặp lại giữa 250 ms và 900 ms.
3. Cả khi nhả lẫn khi nhấn, chân GPIO 4 đều ở gần 3,3 V nên không có cạnh tín hiệu. Cần chuyển đầu nút đang nối 3,3 V sang GND và giữ nguyên `INPUT_PULLUP`.
4. Điện trở 10 kΩ nối giữa GPIO và nguồn 3,3 V; đầu còn lại của nút nối xuống GND. Khi giữ nút, dòng qua điện trở là $3{,}3 / 10\,\mathrm{k}\Omega = 0{,}33$ mA.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pinMode&lt;/code&gt; của Arduino&lt;/a&gt; — giải thích chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; dùng cho nút nhấn kéo lên trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.micropython.org/en/latest/library/machine.Pin.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;machine.Pin&lt;/code&gt; của MicroPython&lt;/a&gt; — tài liệu về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.PULL_UP&lt;/code&gt; và phương thức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;value()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Nếu chưa chuẩn bị bo mạch chính, xem danh mục đầy đủ tại &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;bài mua sắm chương 00&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;linh-kiện-cho-bài-lab&quot;&gt;Linh kiện cho bài lab&lt;/h2&gt;

&lt;p&gt;Mức giá dưới đây tham khảo từ Hshop (tính bằng đồng Việt Nam và có thể thay đổi theo thời điểm): nút nhấn tròn 12 mm PBS-11B kèm dây giá 10000₫ tại &lt;a href=&quot;https://hshop.vn/nut-nhan-nha-tron-pbs-11b-12mm-kem-cap&quot;&gt;hshop.vn/nut-nhan-nha-tron-pbs-11b-12mm-kem-cap&lt;/a&gt; (bất kỳ nút nhấn nhả thường hở hai chân nào đều tương đương về mặt điện); bộ LED giá 20000₫ tại &lt;a href=&quot;https://hshop.vn/bo-5-loai-led-sieu-sang-3mm-thong-dung-5-kind-3mm-transparent-color-led&quot;&gt;hshop.vn/bo-5-loai-led-sieu-sang-3mm-thong-dung-5-kind-3mm-transparent-color-led&lt;/a&gt; (LED 3 mm hay 5 mm đều áp dụng cùng công thức tính điện trở nối tiếp theo $V_F$).&lt;/p&gt;

&lt;p&gt;Breadboard: &lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;hshop.vn/test-board-cammb-102&lt;/a&gt;. Dây cắm đực–đực 20 cm (40 sợi): &lt;a href=&quot;https://hshop.vn/day-cam-breadboard-duc-duc-20cm-cap-det-40-soi-m-m-jumper-wire&quot;&gt;hshop.vn/day-cam-breadboard-duc-duc-20cm-cap-det-40-soi-m-m-jumper-wire&lt;/a&gt;. Bộ điện trở 1/4 W (có sẵn giá trị 180 Ω và 220 Ω) có thể tìm trên Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=b%E1%BB%99%20%C4%91i%E1%BB%87n%20tr%E1%BB%9F%201%2F4W&quot;&gt;bộ điện trở 1/4W&lt;/a&gt;. Chưa cần mua động cơ cho chương này và tuyệt đối không nối động cơ vào GPIO.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>C++ và MicroPython trên robot</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_01_cpp_vs_micropython/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter03/03_01_cpp_vs_micropython</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn có thể viết cùng một chương trình nhịp tim không chặn (non-blocking) bằng cả C++ và MicroPython, đồng thời chỉ ra chính xác dòng lệnh nào làm trễ ngưỡng dừng an toàn 300 ms. Bạn sử dụng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; trên lõi Arduino và cặp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; cùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff()&lt;/code&gt; trong MicroPython để thay thế cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; của bài &lt;a href=&quot;/contents/vi/chapter02/02_04_blink_serial_hello/&quot;&gt;nhịp tim chương 02&lt;/a&gt;. Bạn cũng tính được vì sao một lệnh chờ 200 ms làm lỡ mốc 300 ms trên xe vi sai, từ đó biết cách chọn ngôn ngữ phù hợp khi thử nghiệm cảm biến mới và khi vận hành robot thực tế.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn cần đã có bo mạch in ra mốc thời gian mili giây tăng đều và nháy được một đèn LED từ bài nhịp tim chương 02 (nhánh ESP32 dùng PlatformIO, nhánh Pico dùng REPL MicroPython). Bạn biết đổi số chân LED khi mã mẫu không khớp với nhãn in trên bo mạch. Bài này chưa dùng PWM, ngắt hay mạch cầu H; thói quen cần bỏ là dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleep&lt;/code&gt; kéo dài nửa giây, vốn chỉ phù hợp khi chương trình không phải giám sát thêm tác vụ nào khác.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Một câu hỏi kinh điển khiến nhiều kỹ sư robot trẻ tranh luận sôi nổi: &lt;em&gt;nên viết firmware robot bằng &lt;strong&gt;C++&lt;/strong&gt; (Arduino/ESP-IDF) hay &lt;strong&gt;MicroPython&lt;/strong&gt;?&lt;/em&gt; Nếu chỉ nói chung chung “C++ nhanh hơn còn Python dễ viết hơn” thì vẫn quá mơ hồ — dưới góc nhìn điều khiển thời gian thực, sự khác biệt sinh tử nằm ở &lt;strong&gt;tính tất định về thời gian (timing determinism)&lt;/strong&gt; và &lt;strong&gt;độ trễ dọn rác bộ nhớ (garbage collection pause)&lt;/strong&gt;!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-biên-dịch-tĩnh-c-vs-thông-dịch-quản-lý-bộ-nhớ-tự-động-micropython&quot;&gt;Kiến thức cần nhớ: Biên dịch Tĩnh (C++) vs. Thông dịch Quản lý Bộ nhớ Tự động (MicroPython)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tốc độ Thực thi &amp;amp; Chi phí Runtime:&lt;/strong&gt; C++ biên dịch trực tiếp ra mã máy gốc (native machine code), chạy phép tính số thực dấu phẩy động trên FPU của ESP32 chỉ mất vài nano-giây; MicroPython chạy qua máy ảo bytecode nên chậm hơn khoảng \(10\text{–}50\times\) trên các vòng lặp tính toán dày đặc.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tính Tất định &amp;amp; Garbage Collector (GC):&lt;/strong&gt; C++ cấp phát biến tĩnh trên stack/static RAM nên thời gian chạy vòng lặp PID 100 Hz gần như &lt;strong&gt;bất biến&lt;/strong&gt;; MicroPython định kỳ kích hoạt trình dọn rác GC (có thể đóng băng CPU từ &lt;strong&gt;2 đến 10 ms&lt;/strong&gt; bất ngờ) nếu bạn cấp phát chuỗi/danh sách liên tục trong vòng lặp!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tốc độ Tạo mẫu &amp;amp; Chiến lược Lai:&lt;/strong&gt; MicroPython cực mạnh để thử nhanh cảm biến qua REPL và viết kịch bản hành vi cấp cao; C++ là chuẩn vàng cho vòng lặp động cơ tốc độ cao, đọc encoder bằng ngắt và tích hợp micro-ROS.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-so-sánh-ngân-sách-thời-gian-vòng-lặp-pid-100-hz-t--10text-ms-giữa-c-và-micropython&quot;&gt;Ví dụ làm từng bước: So sánh ngân sách thời gian vòng lặp PID 100 Hz (\(T = 10\text{ ms}\)) giữa C++ và MicroPython&lt;/h3&gt;

&lt;p&gt;Giả sử bạn chạy vòng lặp điều khiển động cơ ở &lt;strong&gt;\(100\text{ Hz}\)&lt;/strong&gt; (mỗi chu kỳ tối đa &lt;strong&gt;\(10.0\text{ ms}\)&lt;/strong&gt;):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Giai đoạn trong 1 chu kỳ (10 ms)&lt;/th&gt;
      &lt;th&gt;Firmware C++ (ESP32, 240 MHz)&lt;/th&gt;
      &lt;th&gt;Firmware MicroPython (RP2040, 133 MHz)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Đọc 2 encoder + tính PID + xuất PWM&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(0.08\text{ ms}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(0.65\text{ ms}\)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Độ trễ bất ngờ từ Garbage Collector&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(0.00\text{ ms}\)&lt;/strong&gt; (không dùng heap)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(3.50\text{ ms}\)&lt;/strong&gt; (nếu cấp phát chuỗi mới mỗi vòng!)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Thời gian xấu nhất (Worst-Case)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(0.08\text{ ms}\)&lt;/strong&gt; (chiếm \(0.8\%\) chu kỳ)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(4.15\text{ ms}\)&lt;/strong&gt; (chiếm \(41.5\%\) — giật mạnh nếu lên 500 Hz!)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Kết luận bằng con số: ở &lt;strong&gt;50–100 Hz&lt;/strong&gt; cho xe vi sai cơ bản, &lt;em&gt;cả hai&lt;/em&gt; ngôn ngữ đều chạy mượt (miễn là trong MicroPython bạn gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gc.collect()&lt;/code&gt; có kiểm soát và tránh tạo list/string mới trong vòng lặp nhanh!). Nhưng nếu làm drone tự cân bằng chạy &lt;strong&gt;1.000 Hz&lt;/strong&gt; (\(T = 1.0\text{ ms}\)), chỉ &lt;strong&gt;C++&lt;/strong&gt; mới giữ được độ trễ dưới 1 ms không bị GC phá vỡ!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-4-tiêu-chí-chọn-đúng-ngôn-ngữ-cho-từng-tầng-robot&quot;&gt;Giải thích: 4 tiêu chí chọn đúng ngôn ngữ cho từng tầng robot&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Đo chu kỳ thực tế thay vì đoán:&lt;/strong&gt; Dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;micros()&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_us()&lt;/code&gt; đo trực tiếp thời gian chạy vòng lặp lớn nhất (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_loop_us&lt;/code&gt;) để biết bạn còn bao nhiêu phần trăm dự phòng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Không cấp phát bộ nhớ động (heap) trong vòng lặp điều khiển nhanh:&lt;/strong&gt; Dù viết C++ (tránh lớp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; gây phân mảnh heap!) hay MicroPython (tránh tạo mảng mới mỗi vòng), hãy dùng mảng tĩnh cấp phát sẵn từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Dùng MicroPython để khám phá, C++ để sản xuất tốc độ cao:&lt;/strong&gt; Tận dụng điểm mạnh của mỗi ngôn ngữ thay vì cực đoan chọn một bỏ một.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Tách rõ lớp toán điều khiển khỏi lớp truy cập thanh ghi:&lt;/strong&gt; Khi viết hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pid_step(setpoint, measured, dt)&lt;/code&gt; thuần túy không phụ thuộc phần cứng, bạn có thể dịch chuyển giữa C++ và Python chỉ trong 10 phút!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Trong MicroPython, nếu một vòng lặp chạy ở 50 Hz (\(T = 20\text{ ms}\)) mất \(1\text{ ms}\) tính toán nhưng thỉnh thoảng bị GC pause \(4\text{ ms}\), tổng thời gian xấu nhất là \(5\text{ ms} &amp;lt; 20\text{ ms}\) — vòng lặp có bị trễ nhịp không? &lt;strong&gt;Không bị trễ nhịp&lt;/strong&gt; (vẫn hoàn thành trước mốc 20 ms), nhưng &lt;em&gt;độ giật thời điểm lấy mẫu&lt;/em&gt; (jitter) tăng thêm 4 ms, làm thành phần đạo hàm \(D\) trong PID hơi nhiễu!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Capstone A sử dụng khung xe hai bánh vi sai: các lệnh điều khiển từ xa (teleop) được chuyển thành hai kênh PWM đưa tới mạch cầu H TB6612, và chính firmware phải chủ động đưa cả hai kênh về 0 khi ngừng nhận lệnh. Quy tắc an toàn rất rõ ràng: sau 300 ms không có lệnh mới, cả hai duty phải về 0 (và bánh xe luôn được nhấc khỏi bàn trong lần kiểm chứng đầu tiên). Bất kỳ chương trình nào không thể kiểm tra đồng hồ trong khoảng thời gian đó sẽ giữ nguyên mức PWM cũ sau khi người điều khiển đã thả nút. Cả MicroPython và C++ đều đáp ứng được Capstone A; điều bài học này loại bỏ là những lệnh chờ chặn như 200 ms, đủ dài để vượt quá mốc 300 ms.&lt;/p&gt;

&lt;h2 id=&quot;nội-dung&quot;&gt;Nội dung&lt;/h2&gt;

&lt;p&gt;Cả hai ngôn ngữ đều chạy trên cùng vi điều khiển bạn đã chuẩn bị ở chương 02, dùng chung sơ đồ chân GPIO, đường nguồn logic 3,3 V và cổng serial USB. Yêu cầu cốt lõi của Capstone A không nằm ở sở thích ngôn ngữ mà ở khả năng liên tục kiểm tra xem lệnh gần nhất đã quá hạn hay chưa.&lt;/p&gt;

&lt;p&gt;MicroPython duy trì môi trường tương tác đọc–thực thi–in (REPL) trực tiếp trên chip: bạn có thể dán một hàm, gọi thử, xem kết quả ngay rồi mới lưu vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt;. Chu kỳ thử nghiệm ngắn này rất tiện khi đang kiểm tra đấu dây cảm biến mới. Đánh đổi nằm ở trình thực thi: bộ thu gom rác (garbage collector) đôi lúc tạm dừng chương trình trong vài mili giây — thỉnh thoảng vài chục mili giây — ngoài lịch bạn chủ động thiết lập. Tần số gửi lệnh teleop thủ công trong môn học nằm quanh 20 đến 50 Hz, tương ứng chu kỳ:&lt;/p&gt;

\[T_{20} = \frac{1}{20} = 50\,\mathrm{ms}, \qquad T_{50} = \frac{1}{50} = 20\,\mathrm{ms}.\]

&lt;p&gt;Một lần thu gom rác ngắn có thể làm giật nhẹ một chu kỳ điều khiển, nhưng không tự nó kích hoạt nhầm ngưỡng 300 ms miễn vòng lặp kiểm tra lại mốc thời gian ngay khi chạy tiếp. Tuy nhiên, độ trễ bất ngờ này không phù hợp cho các giao thức tự tạo xung bằng phần mềm (bit-bang) tốc độ cao như UART mềm hay đọc chuỗi xung cảm biến dày đặc vì sẽ làm mất cạnh tín hiệu. Không dùng MicroPython cho các tác vụ đó; ngược lại, nó hoàn toàn dùng tốt cho teleop ở tốc độ thao tác của con người nếu không chèn lệnh ngủ dài.&lt;/p&gt;

&lt;p&gt;C++ trên lõi Arduino (biên dịch bằng PlatformIO như chương 02) tạo tệp nhị phân trên máy tính rồi nạp xuống chip. Chương trình không có bộ thu gom rác, còn hàm phục vụ ngắt (ISR) gắn trực tiếp với các cờ kích phần cứng cụ thể nên rất dễ kiểm soát thời gian thực khi đọc encoder về sau. Đánh đổi là mỗi lần sửa mã đều phải chờ biên dịch và nạp lại. Mức chờ này hoàn toàn chấp nhận được khi hoàn thiện firmware chạy robot, và không phải lý do để giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; trong vòng điều khiển.&lt;/p&gt;

&lt;p&gt;Hai đoạn mã dưới đây thực hiện cùng một hành vi nhịp tim: đảo LED mỗi 500 ms, còn các lượt lặp chưa đến hạn sẽ đi thẳng qua để các kiểm tra lệnh phía sau luôn được thực thi. Trong Arduino C++, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; trả về số nguyên 32-bit không dấu (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint32_t&lt;/code&gt;), quay vòng sau khoảng 49,7 ngày; phép trừ hai biến &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint32_t&lt;/code&gt; luôn cho hiệu đúng kể cả lúc tràn bộ đếm (không lưu mốc thời gian trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; có dấu). Trong MicroPython, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; cũng quay vòng theo chu kỳ nội bộ nên bắt buộc dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff(now, last)&lt;/code&gt; để tính khoảng thời gian.&lt;/p&gt;

&lt;p&gt;Trên nhiều bản MicroPython cho ESP32, vòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while True&lt;/code&gt; chạy kín liên tục có thể làm nghẽn tác vụ nền và kích hoạt bộ định thời giám sát (watchdog). Đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep_ms(1)&lt;/code&gt; ở cuối vòng lặp là cách nhường CPU hợp lý, khác hoàn toàn với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(0.2)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(1)&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(200)&lt;/code&gt;. Khi đã điều khiển động cơ, vòng lặp không được bị chặn quá vài mili giây: nhường 1 ms vẫn cho phép kiểm tra ngưỡng 300 ms hàng trăm lần, còn ngủ 200 ms sẽ bỏ lỡ hạn dừng.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LED_PIN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// chân LED bạn đã nhấp ở chương 02&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BLINK_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastBlink&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastBlink&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;heartbeat, non-blocking&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastBlink&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BLINK_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastBlink&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;t=%lu led=%d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Chỗ kiểm tuổi lệnh nằm ở vòng này, kể cả vòng không đảo LED.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# đổi thành LED chương 02 bạn đã tin
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BLINK_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;heartbeat, non-blocking&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BLINK_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# chỉ nhường; đừng sleep(0.2) khi đã có timeout
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Cấu trúc của hai đoạn mã hoàn toàn tương ứng: lưu mốc thời gian gần nhất, trừ an toàn khi tràn bộ đếm, chỉ đảo LED khi đủ hạn và tiếp tục vòng lặp ở mọi lượt còn lại. Phần chạy tiếp đó chính là nơi đặt phép kiểm tra 300 ms. Một cách phân công hiệu quả là dùng MicroPython khi cần thử nhanh cảm biến mới, và chuyển sang C++ khi hệ thống vận hành chính thức hoặc đòi hỏi tính định thời chặt chẽ hơn. Cả hai ngôn ngữ đều hợp lệ cho Capstone A miễn phép kiểm tra quá thời gian thực sự hoạt động trong vòng lặp.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-tay&quot;&gt;Ví dụ tính tay&lt;/h2&gt;

&lt;p&gt;Điều kiện ngắt an toàn của Capstone A là:&lt;/p&gt;

\[\text{cắt cả hai kênh PWM nếu } t - t_{\text{cmd}} \ge 300\,\mathrm{ms}.\]

&lt;p&gt;Nếu đặt một lệnh ngủ 200 ms ở cuối vòng lặp, điều kiện trên chỉ được kiểm tra tại các mốc bội số của 200 ms. Giả sử lệnh cuối tới lúc $t = 0$ (cập nhật $t_{\text{cmd}} = 0$), sau đó vòng lặp ngủ. Các thời điểm kiểm tra kế tiếp là 200 ms, 400 ms, 600 ms. Tại 200 ms, thời gian im lặng vẫn nhỏ hơn 300 ms nên TB6612 giữ nguyên mức PWM cũ. Lần kiểm tra tiếp theo rơi vào 400 ms:&lt;/p&gt;

\[t_{\text{dừng}} = \left\lceil \frac{300}{200} \right\rceil \times 200 = 400\,\mathrm{ms}.\]

&lt;p&gt;Chương trình đã bỏ qua mốc 300 ms và chỉ dừng khi đã trễ:&lt;/p&gt;

\[400 - 300 = 100\,\mathrm{ms}.\]

&lt;p&gt;Trong 100 ms thừa đó, cả hai bánh vẫn chạy theo tốc độ cũ — đúng tình huống cơ chế timeout cần ngăn chặn. So sánh 200 ms với 300 ms rồi cho rằng “ngủ ngắn hơn timeout nên an toàn” là sai lầm. Độ trễ mỗi vòng lặp chỉ nên ở mức vài mili giây, nhỏ hơn nhiều so với chu kỳ 20 ms của luồng lệnh 50 Hz; riêng một giấc ngủ 200 ms đã làm lỡ tới:&lt;/p&gt;

\[\frac{200\,\mathrm{ms}}{20\,\mathrm{ms}} = 10\]

&lt;p&gt;chu kỳ lệnh liên tiếp.&lt;/p&gt;

&lt;p&gt;Ngược lại, một lần thu gom rác kéo dài khoảng 15 ms trên vòng lặp MicroPython có nhường CPU chỉ làm chậm một chu kỳ teleop mà không che lấp mốc 300 ms, vì điều kiện timeout được kiểm tra lại ngay khi trình thực thi chạy tiếp. Do đó, khoảng dừng ngắn của bộ thu gom rác có thể chấp nhận được ở tần số 20–50 Hz, còn lệnh ngủ 200 ms thì không.&lt;/p&gt;

&lt;h2 id=&quot;bài-lab&quot;&gt;Bài lab&lt;/h2&gt;

&lt;p&gt;Tiếp tục sử dụng đèn LED từ chương 02; không gắn động cơ và không nối TB6612. Mục tiêu là đo khả năng phản hồi theo đồng hồ của vòng lặp.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Nạp đoạn mã phù hợp với bo mạch của bạn (trên MicroPython, chạy thử qua REPL trước khi lưu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt;). Kiểm tra màn hình chỉ hiện một dòng khởi động duy nhất, tiếp theo là các dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t=...&lt;/code&gt; cách nhau khoảng 500 ms đồng bộ với đèn LED. Nếu dòng khởi động xuất hiện lại, đó là lỗi reset cần xử lý như ở chương 02.&lt;/li&gt;
  &lt;li&gt;Thêm một biến đếm số lượt lặp và in ra mỗi giây một lần bằng cùng kỹ thuật không chặn, ghi con số vào sổ rồi xóa phần đếm này đi. (Với MicroPython có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep_ms(1)&lt;/code&gt;, số lượt mỗi giây sẽ quanh mức 1000.)&lt;/li&gt;
  &lt;li&gt;Chèn thử &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(200)&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(0.2)&lt;/code&gt; vào cuối vòng lặp rồi quan sát lại tốc độ vòng lặp và đèn LED. Tần suất vòng lặp giảm mạnh, và ngưỡng 300 ms giờ chỉ còn được kiểm tra trên lưới 200 ms như ví dụ tính tay.&lt;/li&gt;
  &lt;li&gt;Xóa lệnh ngủ 200 ms trước khi kết thúc buổi thực hành để giữ lại bản nhịp tim không chặn. Ghi vào sổ ngôn ngữ đã dùng, số chân LED, số lượt lặp mỗi giây và câu ghi nhớ: “ngủ 200 ms thì robot dừng ở 400 ms.”&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=3ATDCi4ApY4&quot;&gt;Get started with PlatformIO and the ESP 32&lt;/a&gt; — John Green Dev, tiếng Anh. Minh họa quy trình biên dịch và nạp C++ qua PlatformIO.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[Raspberry Pi PICO&lt;/td&gt;
          &lt;td&gt;Starting With MicroPython + Examples&lt;/td&gt;
          &lt;td&gt;I2C OLED, ADC, PWM](https://www.youtube.com/watch?v=zlKJ5hvfs6s) — Electronoobs, tiếng Anh. Minh họa chạy trực tiếp tệp MicroPython trên Pico không qua bước biên dịch.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Giả sử vòng lặp không chặn của bạn thực hiện được 25000 lượt mỗi giây. Một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(200)&lt;/code&gt; làm mất bao nhiêu lượt lặp, và nếu phép kiểm tra mất lệnh 300 ms chỉ chạy sau mỗi lần ngủ như vậy thì thời điểm sớm nhất nó kích hoạt là khi nào?&lt;/li&gt;
  &lt;li&gt;Một chương trình MicroPython dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(0.5)&lt;/code&gt; rồi mới đọc cổng serial. Hãy nêu tên hai hàm đồng hồ cần dùng để LED vẫn đảo mỗi 500 ms trong khi dữ liệu serial được kiểm tra ở mọi lượt lặp.&lt;/li&gt;
  &lt;li&gt;Lệnh điều khiển gửi ở tần số 40 Hz và xảy ra một lần thu gom rác đơn lẻ kéo dài 12 ms. Khoảng dừng đó tự nó có buộc robot phải vào trạng thái dừng an toàn không? Nếu đổi sang bài toán bit-bang một giao thức có độ rộng mỗi bit 8 µs, liệu khoảng dừng 12 ms có chấp nhận được không?&lt;/li&gt;
  &lt;li&gt;Chiều nay bạn cần thử nghiệm nhanh một cảm biến khoảng cách mới; tuần sau robot vận hành thực tế bắt đầu có dấu hiệu trễ mốc 300 ms khi tải nặng. Bạn chọn ngôn ngữ nào cho mỗi tình huống, và điều kiện bắt buộc chung cho cả hai là gì?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý&lt;/summary&gt;

1. Mỗi lượt kéo dài $1/25000$ giây, nên 0,2 s làm mất $0{,}2 \times 25000 = 5000$ lượt lặp. Khi kiểm tra trên lưới 200 ms, mốc đầu tiên thỏa điều kiện $\ge 300$ ms là 400 ms.
2. Lưu `last = time.ticks_ms()`, đảo LED khi `time.ticks_diff(time.ticks_ms(), last) &amp;gt;= 500`, và kiểm tra dữ liệu serial ở mọi vòng lặp (có thể dùng `sleep_ms(1)` nhưng phải xóa `sleep(0.5)`).
3. 12 ms nhỏ hơn nhiều so với 300 ms nên một lần dừng đơn lẻ không làm kích hoạt timeout. Ngược lại, một bit rộng 8 µs chắc chắn bị hỏng khi chương trình dừng 12 ms — đây là trường hợp không dùng MicroPython cho bit-bang.
4. Dùng MicroPython để thử nhanh cảm biến mới và chuyển vòng điều khiển chính sang C++ khi cần định thời ổn định dưới tải. Ở cả hai trường hợp, timeout bắt buộc phải được viết thành phép so sánh thời gian thực sự trong vòng lặp.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.micropython.org/en/latest/reference/isr_rules.html&quot;&gt;Quy tắc ISR của MicroPython&lt;/a&gt; — các ràng buộc cần nắm khi dùng callback ngắt, đồng thời giải thích lý do bài ngắt sắp tới cấm cấp phát bộ nhớ trong ISR.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/time/millis/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;&lt;/a&gt; — tài liệu về bộ đếm mili giây không dấu trong Arduino C++ và chu kỳ tràn khoảng 49,7 ngày.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.platformio.org/&quot;&gt;PlatformIO&lt;/a&gt; — công cụ biên dịch đã thiết lập từ chương 02 cho ESP32.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 02</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_06_ch02_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_06_ch02_lab_checklist</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Bài kiểm tra này dùng để chốt lại toàn bộ chương 02 dựa trên các bằng chứng thực tế có thể kiểm chứng trực tiếp. Bạn cần ghi rõ bộ công cụ (toolchain) cùng số phiên bản, xác nhận LED tích hợp nháy đúng chu kỳ đã chọn, dán đoạn log nhịp tim serial thay vì chỉ mô tả bằng lời, hoàn thiện thẻ sơ đồ chân cho đúng bo mạch trên bàn (gồm chân LED và chân ngõ ra an toàn), ghi chính xác hai giá trị đo đường nguồn (3,3 V và 5 V/VBUS), và ghi rõ câu xác nhận chưa dùng các chân nguồn động cơ (VM và pin). Nếu có mục nào chưa đạt, hãy quay lại bài học tương ứng để xử lý thay vì vội mua thêm bo mạch mới.&lt;/p&gt;

&lt;h2 id=&quot;cần-gì-trước-khi-học&quot;&gt;Cần gì trước khi học&lt;/h2&gt;

&lt;p&gt;Bài này tổng hợp kết quả từ bài 1 đến bài 5: câu quyết định chọn vi điều khiển (ESP32 hoặc đúng phiên bản Pico), bộ công cụ tương ứng, log nhịp tim (hoặc ghi rõ đang chờ nếu chưa có mạch) và thẻ sơ đồ chân. Bạn cần có đồng hồ vạn năng cho mục đo nguồn. Danh sách linh kiện đầy đủ nằm ở chương 00 (&lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;Hóa đơn linh kiện và cách mua&lt;/a&gt;). Cần đọc lại quy tắc nhấc bánh khỏi bàn từ bài nhịp tim để bảo đảm không đánh dấu đạt khi robot đang có nguy cơ tự lăn.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Trước khi viết các thuật toán điều khiển phức tạp như PWM động cơ, ngắt encoder hay máy trạng thái ở Chương 03, bạn cần chắc chắn 100% rằng nền móng vi điều khiển ở Chương 02 đã vững như bàn thạch: nạp code ổn định, không rớt cổng Serial, nhịp tim không chặn chạy đều tăm tắp và đường nguồn không bao giờ sụt áp. &lt;strong&gt;Checklist Chương 02&lt;/strong&gt; là bài kiểm tra nghiệm thu nhanh giúp bạn bước sang Chương 03 không mang theo bất kỳ “quả bom nổ chậm” nào.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-nghiệm-thu-tầng-vi-điều-khiển-mcu-bring-up-verification&quot;&gt;Kiến thức cần nhớ: Nghiệm thu Tầng Vi điều khiển (MCU Bring-Up Verification)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tính ổn định Nạp &amp;amp; Giao tiếp:&lt;/strong&gt; Cắm rút USB 3 lần liên tiếp đều nhận đúng cổng, nạp firmware thành công ở lần đầu và log Serial hiển thị rõ ràng không rác ký tự.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Định thời Chính xác &amp;amp; Không chặn:&lt;/strong&gt; Bộ đếm thời gian (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_ms()&lt;/code&gt;) giữ chu kỳ nhịp tim ổn định, không dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; chặn CPU.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;An toàn Đường nguồn &amp;amp; Mức logic:&lt;/strong&gt; Có bảng Pinmap rõ ràng, tách biệt nguồn động lực với nguồn logic 3,3 V và xác minh không xảy ra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Brownout Reset&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-bốn-con-số-nghiệm-thu-đầu-ra-chương-02&quot;&gt;Ví dụ làm từng bước: Bốn con số nghiệm thu đầu ra Chương 02&lt;/h3&gt;

&lt;p&gt;Hãy đối chiếu 4 chỉ số thực đo từ bo mạch của bạn với ngưỡng chuẩn sau:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục kiểm tra&lt;/th&gt;
      &lt;th&gt;Cách đo / Bằng chứng&lt;/th&gt;
      &lt;th&gt;Ngưỡng đạt chuẩn&lt;/th&gt;
      &lt;th&gt;Ví dụ thực đo ĐẠT&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;1. Chu kỳ log nhịp tim (500 ms)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Hiệu 2 dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms&lt;/code&gt; liên tiếp trên Serial&lt;/td&gt;
      &lt;td&gt;\([495\text{ ms},\ 505\text{ ms}]\)&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms = 1000, 1500, 2001&lt;/code&gt; (\(\Delta = 500\text{–}501\text{ ms}\))&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;2. Tỉ lệ lỗi khung Serial&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Quan sát 60 dòng log ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200 bps&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;0%&lt;/strong&gt; ký tự rác&lt;/td&gt;
      &lt;td&gt;60/60 dòng sạch CSV&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;3. Điện áp đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; chạy tải logic&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Đo DC V tại chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3–GND&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\([3.20\text{ V},\ 3.40\text{ V}]\)&lt;/td&gt;
      &lt;td&gt;\(3.31\text{ V}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;4. Số lần tự reset trong 5 phút&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Theo dõi dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BOOT&lt;/code&gt; trên Serial&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;0 lần&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;0 lần reset bất thường&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;giải-thích-4-bước-xử-lý-khi-một-mục-trong-checklist-chương-02-chưa-đạt&quot;&gt;Giải thích: 4 bước xử lý khi một mục trong Checklist Chương 02 chưa đạt&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Cô lập phần cứng về cấu hình tối thiểu:&lt;/strong&gt; Tháo toàn bộ cảm biến/dây nối ngoài, chỉ giữ lại bo MCU trần cắm USB để xác định lỗi thuộc về bo mạch hay thuộc về ngoại vi.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Kiểm tra lại tốc độ baud và cáp USB:&lt;/strong&gt; 70% lỗi Serial rác hoặc không nạp được đến từ lệch baud rate hoặc cáp USB kém tiếp xúc.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Dùng đồng hồ đo lại nguồn trước khi nghi chip hỏng:&lt;/strong&gt; Vi điều khiển hiếm khi tự chết nếu chưa bị quá áp; thường do nguồn tụt áp hoặc chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EN&lt;/code&gt; bị chạm lỏng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Lưu bản chụp log Serial và ảnh Pinmap vào sổ lab:&lt;/strong&gt; Đây là mốc tham chiếu cực quý giá nếu sau này lắp thêm động cơ mà bo đột ngột mất ổn định.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu nhật ký Serial in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms&lt;/code&gt; nhảy từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5000&lt;/code&gt; thẳng về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;120&lt;/code&gt; giữa chừng mà bạn không hề bấm nút Reset, hiện tượng gì vừa xảy ra? Bo mạch vừa bị &lt;strong&gt;tự khởi động lại (Watchdog reset hoặc Brownout reset)&lt;/strong&gt; — cần kiểm tra lại nguồn cấp hoặc vòng lặp bị treo!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-với-capstone-a&quot;&gt;Vì sao bài này gắn với Capstone A&lt;/h2&gt;

&lt;p&gt;Chương 07 sử dụng vi điều khiển này để chạy chương trình điều khiển từ xa (teleop) cho xe vi sai với hai kênh PWM nối tới TB6612, nguồn pin cấp vào VM, cơ chế tự ngắt sau khoảng 300 ms mất lệnh và quy tắc nhấc bánh khi thử nghiệm. Chương 07 giả định mọi mục trong danh sách kiểm tra này đã hoàn tất. Nếu bước sang chương sau mà vẫn chưa xác định chắc chắn chân mass hay độ ổn định nguồn, bạn sẽ mất thời gian sửa mã nguồn timeout trong khi lỗi thật sự là chip đang bị reset. Đến chương 08, kết nối Wi-Fi trên ESP32 càng dễ lộ lỗi nếu nguồn sụt áp (brownout), biểu hiện như tín hiệu mạng chập chờn. Danh sách kiểm tra này giúp loại bỏ các lỗi phần cứng cơ bản trước khi đi sâu vào phần mềm.&lt;/p&gt;

&lt;h2 id=&quot;sáu-mục-kiểm-tra-và-bằng-chứng-tương-ứng&quot;&gt;Sáu mục kiểm tra và bằng chứng tương ứng&lt;/h2&gt;

&lt;p&gt;Mỗi mục trong danh sách đòi hỏi bằng chứng cụ thể ghi trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, không phải việc bạn nhớ mình đã đọc qua lý thuyết. Chương 02 gồm sáu mục; mỗi mục chỉ có hai trạng thái: &lt;strong&gt;ĐẠT&lt;/strong&gt; (kèm bằng chứng rõ ràng) hoặc &lt;strong&gt;MỞ&lt;/strong&gt; (chưa hoàn thành). Không có trạng thái trung gian như “gần đạt”.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bộ công cụ (Toolchain):&lt;/strong&gt; ghi rõ số phiên bản cụ thể. Với Arduino IDE: phiên bản trong hộp thoại About kèm phiên bản gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32&lt;/code&gt; trong Boards Manager. Với PlatformIO: kết quả lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pio --version&lt;/code&gt; cùng xác nhận dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board = esp32dev&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt;. Với Pico: phiên bản Thonny hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt; kèm tên tệp UF2 (nêu rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI_PICO&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI_PICO2&lt;/code&gt; hay bản W). Ghi chung chung “đã cài rồi” là không đạt.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nháy LED tích hợp:&lt;/strong&gt; xác nhận trực quan LED tích hợp nháy đúng chu kỳ đã chọn (bài nhịp tim là 500 ms, đảo mỗi 250 ms, tức 2 Hz; nếu chọn chu kỳ khác thì ghi rõ con số đó). Đèn báo nguồn không được tính, và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; chỉ hợp lệ sau khi đã đối chiếu với nhãn trên mạch. Sổ tay cần ghi rõ số chân GPIO hoặc bí danh Pico (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;25&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Log nhịp tim serial:&lt;/strong&gt; dán trực tiếp 10 dòng log từ lần chạy thật, có duy nhất một dòng tiêu đề &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt;, mốc mili giây tăng đều xấp xỉ chu kỳ khai báo, không bị chèn dòng khởi động lại ở giữa. Mỗi dòng thể hiện thời gian và trạng thái. Ghi “đếm ổn” sẽ không đạt vì sau này cần đúng dãy số đó để đối chiếu khi kiểm tra sụt áp. Với ESP32, tốc độ baud phải ghi 115200; để monitor ở 9600 là chưa đạt dù đèn vẫn nháy.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thẻ sơ đồ chân:&lt;/strong&gt; bản vẽ hoặc bảng Markdown lập cho chính bo mạch đang có (không chép từ ảnh của biến thể khác), ghi rõ chân LED, các chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; và ít nhất một chân ngõ ra an toàn (không phải chân strapping đang cần kéo mức đặc biệt, không phải chân UART của chip USB-serial và không phải chân nguồn). Trên Pico W, không được ghi GPIO 25 là LED tích hợp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đo nguồn bằng đồng hồ (chỉ cắm USB, không nối pin):&lt;/strong&gt; ghi chính xác hai con số thực đo — khoảng 4,7 V đến 5,1 V trên đường 5 V hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt;, và khoảng 3,25 V đến 3,35 V trên đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; (ví dụ 4,82 V sau diode là đạt nếu ghi rõ số liệu; viết “5 V ổn” là không đạt). Nếu chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; đo được 4,9 V, phải rút cáp ngay để kiểm tra vì mạch đang bị lỗi nguồn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Xác nhận chân động cơ chưa sử dụng:&lt;/strong&gt; ghi rõ câu xác nhận: “Chân VM trên TB6612 và pin 2S vẫn để hở. Pin không được chạm vào GPIO, 3V3, 5V/VIN, VSYS hay VBUS”, kèm quy tắc nhấc bánh khỏi bàn. Ghi chung chung “động cơ để sau” mà không gọi tên cụ thể VM và pin là chưa đạt.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Checklist dừng trước nguồn motor&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Sơ đồ nhắc lại giới hạn của chương 02: nguồn USB, nháy LED và nhịp tim serial xác nhận phần logic hoạt động ổn định; toàn bộ phần driver và pin nằm ở phía bên phải vạch ngăn cách và chưa được nối ở chương này.&lt;/p&gt;

&lt;p&gt;Trình tự khởi chạy ở bài 1 gồm: nhận USB, nháy LED tích hợp, serial 115200 baud, đọc nút nhấn, xuất PWM không tải, sau đó mới tới driver và pin. Sáu mục trên dừng lại trước bước đấu driver; ngay cả khi hoàn thành danh sách này, vẫn không được nối trực tiếp động cơ vào chân GPIO.&lt;/p&gt;

&lt;p&gt;Nếu linh kiện vẫn đang trên đường giao, bạn có thể chuẩn bị trước phiên bản bộ công cụ và lập trước thẻ sơ đồ chân dựa trên đúng mẫu sản phẩm đã đặt, còn các mục nháy LED, log serial và đo điện áp ghi rõ trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đang chờ&lt;/code&gt;. Không được điền số liệu giả định. Đây là bước chuẩn bị trước khi có phần cứng, chưa tính là đã hoàn thành chương.&lt;/p&gt;

&lt;h2 id=&quot;cách-xử-lý-khi-có-mục-chưa-đạt&quot;&gt;Cách xử lý khi có mục chưa đạt&lt;/h2&gt;

&lt;p&gt;Khi một mục chưa đạt, hãy mở lại đúng bài học tương ứng để khắc phục:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Thiếu phiên bản bộ công cụ:&lt;/strong&gt; mở hộp thoại About, chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pio --version&lt;/code&gt; hoặc kiểm tra thông tin phiên bản MicroPython trong REPL rồi ghi lại (xem lại bài cài đặt ESP32 hoặc Pico).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đèn không nháy hoặc sai chân:&lt;/strong&gt; xem lại bài nạp ESP32 hoặc bài nạp UF2 cho Pico, sau đó xem bài nhịp tim. Đối chiếu nhãn in trên mạch vì GPIO 2 không áp dụng cho mọi bản mạch, còn Pico W phải dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mất serial, ký tự rác hoặc lặp dòng tiêu đề:&lt;/strong&gt; xem bài nhịp tim. Đặt đúng 115200 baud; nếu tiêu đề lặp lại khi không có động cơ, đổi cáp USB và tháo các dây nối ngoài trước khi sửa mã nguồn. Nếu còn nối dây VM, rút ngay để tránh sụt áp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thiếu thẻ sơ đồ chân hoặc sai biến thể:&lt;/strong&gt; xem bài sơ đồ chân, vẽ lại đủ đường 5 V/VBUS, 3V3, GND, chân LED và một chân ngõ ra an toàn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thiếu số đo điện áp hoặc chỉ ghi “ổn”:&lt;/strong&gt; thực hiện phép đo khi chỉ cắm USB theo bài sơ đồ chân. Nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; lệch xa 3,3 V, dừng lại kiểm tra và tuyệt đối không gắn pin.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thiếu câu xác nhận về động cơ:&lt;/strong&gt; bổ sung ngay theo ví dụ mẫu ở bài sơ đồ chân, nhấn mạnh pin 2S chỉ nối vào VM, không chạm hàng chân vi điều khiển.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lưu ý: việc mua thêm một bo mạch ESP32 thứ hai khi có mục chưa đạt thường chỉ lặp lại đúng lỗi cáp ban đầu; hãy ưu tiên thay cáp chỉ sạc trước.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đã-làm&quot;&gt;Ví dụ đã làm&lt;/h2&gt;

&lt;p&gt;Dưới đây là hai ví dụ ghi chép trung thực:&lt;/p&gt;

&lt;p&gt;Bo mạch của Hà đã giao từ tuần trước. Sổ ghi: Arduino IDE 2.3.2 kèm gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32&lt;/code&gt; 3.0.7; NodeMCU-32S dùng CH340 ở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COM5&lt;/code&gt;; LED tích hợp tại GPIO 2 (đã đối chiếu nhãn mạch), đảo trạng thái mỗi 250 ms; 10 dòng nhịp tim bước nhảy từ 498 ms đến 505 ms dưới một dòng tiêu đề duy nhất ở 115200 baud; thẻ sơ đồ chân gồm 5V, 3V3, GND, LED GPIO 2 và ngõ ra an toàn GPIO 4; đo bằng đồng hồ khi chỉ cắm USB được 4,91 V và 3,31 V; kèm câu xác nhận “VM và pin 2S để hở, dương pin không chạm 5V, 3V3 hay bất kỳ GPIO nào.” Cả sáu mục đều đạt, động cơ TT vẫn để trong hộp và quy tắc nhấc bánh đã được ghi nhận.&lt;/p&gt;

&lt;p&gt;Pico 2 của Khoa vẫn đang vận chuyển. Sổ ghi: đã cài Thonny 4.1.4, ghi sẵn tên tệp UF2 từ trang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI_PICO2&lt;/code&gt; để tránh nạp nhầm bản RP2040, lập trước thẻ sơ đồ chân theo ảnh sản phẩm gồm VBUS, VSYS, 3V3, GND, LED GP25 và ngõ ra an toàn GP16, kèm ghi chú “không đưa 5 V vào chân GP; VM và pin không có trong kiện này.” Các mục nháy LED, serial và đo điện áp ghi rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đang chờ&lt;/code&gt;, và Khoa chưa đánh dấu hoàn thành chương.&lt;/p&gt;

&lt;p&gt;Một mẫu sổ thứ ba (chưa đạt) được dùng làm đề phân tích trong phần bài tập bên dưới.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, tạo một mục mới ghi ngày thực hiện và tiêu đề “kiểm tra hoàn thành chương 02”. Với từng mục trong sáu mục, ghi &lt;strong&gt;ĐẠT&lt;/strong&gt; kèm bằng chứng cụ thể, hoặc &lt;strong&gt;MỞ&lt;/strong&gt; kèm tên bài cần làm lại. Nếu chưa nhận được bo mạch, ghi rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đang chờ&lt;/code&gt; cho các mục nháy LED, serial và đo nguồn. Kiểm tra lại toàn bộ một lượt: bất kỳ mục nào thiếu bằng chứng xác thực (số phiên bản, dòng log serial, số đo điện áp, số chân cụ thể, xác nhận về VM và pin) đều phải chuyển về trạng thái &lt;strong&gt;MỞ&lt;/strong&gt; trước khi lưu.&lt;/p&gt;

&lt;p&gt;Chương 02 chỉ hoàn thành khi cả sáu mục đều &lt;strong&gt;ĐẠT&lt;/strong&gt;, có ghi quy tắc nhấc bánh và chưa nối động cơ. Bản chuẩn bị trước khi có phần cứng phải ghi rõ ba mục đo kiểm ở trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đang chờ&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=sJtJJU3c3xE&quot;&gt;Lập trình ESP32 - Bài 1 - Cách nạp chương trình cho ESP32 bằng Arduino IDE - Tự học lập trình IOT&lt;/a&gt; — Điện thông minh E-smart, tiếng Việt. Minh họa quy trình nạp để bảo đảm LED nháy từ chính chương trình bạn viết chứ không phải từ đèn báo nguồn có sẵn.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;p&gt;Xét mẫu ghi chép chưa đạt dưới đây:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ngày: 23 Sep 2026
Board: ESP32 NodeMCU
Toolchain: đã cài Arduino IDE
LED: GPIO 2, hình như có nhấp
Baud monitor: 9600
Serial: thấy vài ký tự, không chép
Ray: 5V ổn, 3.3 ổn
Tiếp: PWM motor từ GPIO 2, sắp gắn pin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;Liệt kê tất cả các mục chưa đạt trong mẫu sổ trên và chỉ ra bằng chứng còn thiếu của từng mục.&lt;/li&gt;
  &lt;li&gt;Chương trình nhịp tim đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.begin(115200)&lt;/code&gt;, còn monitor đang để 9600. Màn hình sẽ hiển thị gì, và cần thay đổi thông số nào để khắc phục?&lt;/li&gt;
  &lt;li&gt;Ghi chú “GPIO 2, hình như có nhấp” chưa đạt yêu cầu kiểm chứng. Cần xác nhận điều gì trên bo mạch để công nhận GPIO 2, và phải ghi thế nào nếu đèn thực tế nằm ở chân khác?&lt;/li&gt;
  &lt;li&gt;Viết lại hai dòng về đường nguồn và động cơ cho đúng định dạng yêu cầu mà không tự bịa số liệu chưa đo (dùng trạng thái đang chờ nếu chưa đo bằng đồng hồ).&lt;/li&gt;
  &lt;li&gt;Với mẫu sổ trên, cần mở lại những bài học nào trước tiên, và vì sao mua thêm một bo mạch mới là cách xử lý sai?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Đáp án&lt;/summary&gt;

1. Toàn bộ sáu mục đều chưa đạt: **Toolchain** thiếu số phiên bản IDE và gói `esp32`; **Nháy LED** ghi &quot;hình như&quot;, thiếu chu kỳ và chưa đối chiếu nhãn mạch; **Serial** thiếu 10 dòng log dán kèm và sai tốc độ baud (9600); **Đo nguồn** chỉ ghi &quot;ổn&quot; thay vì hai số đo thực tế; **Chân động cơ** dự định xuất PWM động cơ thẳng từ GPIO 2 và gắn pin, trái ngược yêu cầu để hở VM và pin; **Thẻ sơ đồ chân** bị bỏ sót hoàn toàn nên mục ngõ ra an toàn cũng chưa đạt.
2. Màn hình sẽ hiện toàn ký tự lỗi (rác) trong khi đèn LED vẫn có thể nháy bình thường. Chỉ cần chỉnh tốc độ Serial Monitor lên 115200 baud cho khớp chương trình, không hạ tốc độ trong mã nguồn xuống 9600.
3. Nhãn in trên mạch hoặc sơ đồ hãng phải xác nhận LED tích hợp nối với GPIO 2 (phổ biến trên DevKit V1 nhưng không phải mọi mạch). Nếu đèn nằm ở chân khác, ghi đúng số chân GPIO đó, xác nhận đèn nháy theo chu kỳ đã chọn và không liệt kê GPIO 2 là chân LED; `LED_BUILTIN` cũng cần kiểm chứng tương tự.
4. Dòng đo nguồn đúng định dạng: &quot;chỉ USB, chân 5V ____ V, chân 3V3 ____ V, pin chưa nối (đang chờ đo đồng hồ)&quot;. Dòng động cơ đúng định dạng: &quot;VM và pin 2S để hở; pin không chạm GPIO, 3V3 hay 5V/VIN; không điều khiển động cơ trực tiếp từ GPIO 2.&quot;
5. Mở lại bài nhịp tim trước để sửa tốc độ baud và thu thập log, sau đó mở bài sơ đồ chân để đo đường nguồn và lập thẻ chân kèm ghi chú về VM. Mua bo mạch thứ hai không giải quyết được lỗi cấu hình và thiếu ghi chép; nếu không thấy cổng kết nối thì cần kiểm tra và thay cáp dữ liệu trước.

&lt;/details&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Chỉ mua bổ sung những món còn thiếu cho mục chưa đạt. Thiếu cáp dữ liệu: &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;. Thiếu bo mạch chính: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;ESP32 NodeMCU-32S CH340, 190 000 ₫&lt;/a&gt; hoặc &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32, 265 000 ₫&lt;/a&gt; — không mua module trần &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;ESP32-S3-WROOM-1 giá 135 000 ₫&lt;/a&gt; vì không dùng làm bo mạch khởi đầu. Hướng Pico (giá ghi nhận ngày 23 tháng 9 năm 2026 từ &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;trang tìm Pico&lt;/a&gt;): Pico 2 giá 195 000 ₫ (&lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-rp2350&quot;&gt;trang sản phẩm&lt;/a&gt;) và Pico 2 W giá 275 000 ₫ (&lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-w-rp2350&quot;&gt;trang sản phẩm&lt;/a&gt;). Tham khảo thêm trên Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=esp32%20devkit%20ch340&quot;&gt;ESP32 DevKit CH340&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=raspberry%20pi%20pico%202&quot;&gt;Pico 2&lt;/a&gt;. &lt;a href=&quot;https://www.thegioiic.com/esp32-devkitc-32u-module-wifi-bluetooth-2-4ghz&quot;&gt;DevKitC-32U ở Thế Giới IC&lt;/a&gt; là mẫu bo mạch phát triển (kiểm tra kỹ cổng USB); &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; là cửa hàng linh kiện rời. Chưa cần mua động cơ hay TB6612 để hoàn thành chương 02; các món này nằm trong &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;giỏ hàng chương 00&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.platformio.org/en/latest/boards/espressif32/esp32dev.html&quot;&gt;PlatformIO &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;&lt;/a&gt; — mã bo mạch mặc định cho mục toolchain ESP32.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html&quot;&gt;Cài arduino-esp32&lt;/a&gt; — nguồn cung cấp phiên bản gói phần mềm cần ghi vào sổ tay.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micropython.org/download/&quot;&gt;Trang tải MicroPython&lt;/a&gt; — nơi tra cứu chính xác tên tệp UF2 riêng cho RP2040 và RP2350.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;Datasheet ESP32&lt;/a&gt; — tài liệu đối chiếu bảng đặc tính điện để chọn chân ngõ ra an toàn thay vì chỉ tin sơ đồ trên blog.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Sơ đồ chân, mức logic và đường nguồn</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_05_pinouts_power_rails/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_05_pinouts_power_rails</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn xác định được đúng vị trí chân nguồn USB 5 V và chân nguồn 3,3 V đã ổn áp trên bo mạch của mình, rồi đo kiểm cả hai bằng đồng hồ vạn năng khi chỉ cấp nguồn qua USB. Bạn nắm vững nguyên tắc mọi chân GPIO của ESP32 và Pico đều là 3,3 V và có thể hỏng nếu đưa 5 V vào. Bạn vẽ được sơ đồ đấu nguồn pin 2S vào chân VM của TB6612 trên giấy, đồng thời liệt kê đầy đủ các chân header mà nguồn pin tuyệt đối không được chạm tới. Bạn cũng thiết kế được mạch chuyển mức tín hiệu Echo của HC-SR04 kèm nối chung mass trước khi đấu dây cảm biến thật. Trong suốt bài thực hành này, không kết nối pin.&lt;/p&gt;

&lt;h2 id=&quot;cần-gì-trước-khi-học&quot;&gt;Cần gì trước khi học&lt;/h2&gt;

&lt;p&gt;Bạn cần đã hoàn thành bài nhịp tim để xác nhận bo mạch kết nối tốt với máy tính và biết rõ chân LED của mình. Bạn cần biết đặt đồng hồ vạn năng ở thang đo điện áp một chiều và đặt que đen lên chân mass (GND) xác định. Kiến thức về mạch chia áp ở chương 01 là phần tính toán duy nhất cần dùng. Nguồn động cơ chưa được đấu nối; danh sách đầy đủ gồm driver và pin nằm ở bài mua sắm chương 00 (&lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;Hóa đơn linh kiện và cách mua&lt;/a&gt;).&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Nhìn vào hai hàng chân kim loại chạy dọc bo mạch ESP32 hay Raspberry Pi Pico, người mới thường thấy tất cả các chân đều giống hệt nhau — nhưng chỉ một sợi dây cắm lệch từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; hoặc cấp tín hiệu 5 V vào chân GPIO là đủ khiến con chip im lặng vĩnh viễn! Hiểu rõ &lt;strong&gt;kiến trúc các đường nguồn (power rails)&lt;/strong&gt; và &lt;strong&gt;phân loại chân GPIO&lt;/strong&gt; là bước chuyển từ “cắm theo hình vẽ” sang tự thiết kế sơ đồ đấu dây chính xác.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-đường-nguồn-power-rails-và-phân-loại-chân-vi-điều-khiển&quot;&gt;Kiến thức cần nhớ: Đường nguồn (Power Rails) và Phân loại Chân Vi điều khiển&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Ba Đường Nguồn Độc lập trên Robot:&lt;/strong&gt; Đường &lt;strong&gt;Pin động lực&lt;/strong&gt; (7,4–8,4 V cấp thẳng cho Driver động cơ), đường &lt;strong&gt;5 V Buck&lt;/strong&gt; (cấp vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIN&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; và cảm biến 5 V), và đường &lt;strong&gt;3,3 V Logic&lt;/strong&gt; (do LDO trên bo MCU tạo ra, giới hạn dòng thường ~\(300\text{–}500\text{ mA}\), &lt;em&gt;chỉ&lt;/em&gt; nuôi chip và cảm biến nhỏ!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Phân nhóm Chân GPIO trên ESP32 &amp;amp; Pico:&lt;/strong&gt; Không phải chân nào cũng đa năng — cần tách riêng nhóm chân &lt;em&gt;an toàn đa dụng&lt;/em&gt;, nhóm &lt;em&gt;chỉ đọc vào&lt;/em&gt; (GPIO 34–39 trên ESP32 không có điện trở kéo lên nội bộ!), nhóm &lt;em&gt;khởi động (Strapping pins)&lt;/em&gt; và nhóm &lt;em&gt;bus chuyên dụng&lt;/em&gt; (I2C/SPI/UART).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nối Chung Mass (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;) nhưng Tách Dòng Lớn:&lt;/strong&gt; Mọi đường nguồn khác mức áp đều bắt buộc nối chung một điểm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; để đồng nhất mốc 0 V cho tín hiệu logic, nhưng dây mass động cơ phải chạy thẳng về cực âm pin để dòng nhiễu không chạy xuyên qua bo MCU.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-tổng-dòng-trên-đường-3v3-của-bo-esp32-giới-hạn-ldo-500-ma&quot;&gt;Ví dụ làm từng bước: Tính tổng dòng trên đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; của bo ESP32 (giới hạn LDO 500 mA)&lt;/h3&gt;

&lt;p&gt;Giả sử bạn dùng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; xuất ra từ bo ESP32 để cấp nguồn cho các module nhỏ trên xe:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Bản thân chip ESP32 (Wi-Fi bật nhẹ): &lt;strong&gt;\(160\text{ mA}\)&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Cảm biến IMU (MPU6050 / ICM-20948): &lt;strong&gt;\(5\text{ mA}\)&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Màn hình OLED 0,96” + 2 encoder quang: \(25 + 20 = \mathbf{45\text{ mA}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tổng dòng đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;:&lt;/strong&gt; \(160 + 5 + 45 = \mathbf{210\text{ mA}} \ll 500\text{ mA}\) → &lt;strong&gt;an toàn, LDO mát!&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Nhưng nếu bạn cắm thêm &lt;strong&gt;1 động cơ servo SG90&lt;/strong&gt; (dòng đỉnh &lt;strong&gt;\(600\text{ mA}\)&lt;/strong&gt;) vào đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; từ USB: tổng dòng vọt lên &lt;strong&gt;\(810\text{ mA}\)&lt;/strong&gt; → vượt trần LDO/USB, gây sụt áp tức thì và reset ESP32! Servo &lt;em&gt;bắt buộc&lt;/em&gt; phải ăn nguồn từ nhánh Buck 5 V riêng!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-nguyên-tắc-khóa-bản-đồ-chân-pinmap-trước-khi-cắm-dây&quot;&gt;Giải thích: 4 nguyên tắc khóa bản đồ chân (Pinmap) trước khi cắm dây&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Lập bảng Pinmap ra giấy/file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pinmap.md&lt;/code&gt; trước khi viết code:&lt;/strong&gt; Ghi rõ số chân, chức năng, mức điện áp (3,3 V hay 5 V) và màu dây cho từng kết nối.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Tránh xa các chân Strapping khi còn chân rảnh:&lt;/strong&gt; Trên ESP32, kéo nhầm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO 12&lt;/code&gt; lên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; lúc bật nguồn làm sai điện áp Flash khiến bo không khởi động được!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Luôn hạ áp mọi tín hiệu 5 V (như chân Echo của HC-SR04) trước khi đưa vào GPIO 3,3 V:&lt;/strong&gt; Dùng cặp điện trở \(1\text{ k}\Omega / 2\text{ k}\Omega\) như đã tính ở Chương 01.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Kiểm tra sơ đồ chân theo đúng phiên bản bo thực tế trên tay:&lt;/strong&gt; Bo ESP32 30 chân và 38 chân có thứ tự chân khác nhau ở hai đầu mép!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Trên ESP32, vì sao khi mắc một nút nhấn vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO 34&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pinMode(34, INPUT_PULLUP)&lt;/code&gt; thì nút nhấn lại bị nhiễu loạn xạ? Vì các chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO 34–39&lt;/code&gt; là chân &lt;em&gt;input-only&lt;/em&gt; và &lt;strong&gt;không có điện trở pull-up/pull-down nội bộ&lt;/strong&gt; trong silicon — muốn dùng phải gắn điện trở kéo lên \(10\text{ k}\Omega\) bên ngoài!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-với-capstone-a&quot;&gt;Vì sao bài này gắn với Capstone A&lt;/h2&gt;

&lt;p&gt;Nhiều lỗi nghiêm trọng khi chạy firmware teleop ở chương 07 không xuất phát từ lỗi lập trình mà từ lỗi đấu nối phần cứng: đưa thẳng xung Echo 5 V vào GPIO 3,3 V; quên nối chung mass giữa mạch động cơ và vi điều khiển khiến tín hiệu PWM bị trôi mức tham chiếu và TB6612 không đọc được; hoặc cấp nhầm pin 7,4 V vào chân 5V hay 3V3 trên bo mạch. Hậu quả là chip bị reset liên tục hoặc hỏng chân ngay lần cấp điện đầu tiên. Cơ chế tự ngắt 300 ms và quy tắc nhấc bánh không thể bảo vệ được một chân GPIO đã bị quá áp. Thẻ sơ đồ chân bạn lập trong bài này sẽ là tài liệu đối chiếu trực tiếp khi bắt đầu lắp driver ở chương 05 và xuất PWM ở chương 07.&lt;/p&gt;

&lt;h2 id=&quot;hai-đường-nguồn-trên-bo-mạch-và-đường-nguồn-riêng-cho-động-cơ&quot;&gt;Hai đường nguồn trên bo mạch và đường nguồn riêng cho động cơ&lt;/h2&gt;

&lt;p&gt;Chân GPIO của ESP32 và Pico hoạt động ở mức logic 3,3 V; cấp 5 V vào chân GPIO có thể làm hỏng chân chip. Trên ESP32, điện áp cực đại tuyệt đối chỉ cao hơn nguồn nuôi khoảng một mức sụt áp diode:&lt;/p&gt;

\[V_{DD} + 0{,}3\,\mathrm{V} = 3{,}3\,\mathrm{V} + 0{,}3\,\mathrm{V} = 3{,}6\,\mathrm{V}.\]

&lt;p&gt;Giới hạn chịu áp trên Pico cũng tương tự, chỉ nhỉnh hơn 3,3 V vài phần mười vôn. Điện áp 5 V vượt xa cả hai giới hạn này.&lt;/p&gt;

&lt;p&gt;Tách biệt với giới hạn hư hỏng trên, quy tắc ước lượng trong học tập cho mức logic cao là điện áp nên đạt ít nhất khoảng \(0{,}7 \times 3{,}3\,\mathrm{V} \approx 2{,}3\,\mathrm{V}\) để được đọc ổn định là mức cao (giá trị \(V_{IL}\) và \(V_{IH}\) chính xác tra trong datasheet). Quy tắc vận hành đơn giản và an toàn nhất là không bao giờ nối trực tiếp ngõ ra 5 V vào chân GPIO 3,3 V.&lt;/p&gt;

&lt;p&gt;Trên bo mạch ESP32 cổ điển, chân ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIN&lt;/code&gt; lấy nguồn 5 V từ cổng USB (thường đi qua một diode Schottky nên điện áp thực đo thấp hơn 5 V vài phần mười vôn). Đường này có thể cấp cho một cảm biến tiêu thụ dòng nhỏ khi thử trên bàn, nhưng tuyệt đối không dùng để cấp nguồn cho động cơ TT. Dòng không tải của một động cơ TT theo thông số bán hàng đã vào khoảng 110 mA đến 150 mA, và tăng lên gấp nhiều lần khi bị kẹt trục. Hai động cơ cộng thêm xung dòng phát Wi-Fi sẽ làm sụt nguồn cổng USB, gây mất kết nối hoặc reset do sụt áp (brownout) — biểu hiện giống hiện tượng lặp lại dòng tiêu đề đã gặp ở bài nhịp tim. Trong khi đó, chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; là ngõ ra của mạch ổn áp dùng để nuôi khối logic, hoàn toàn không dành cho động cơ.&lt;/p&gt;

&lt;p&gt;Trên Raspberry Pi Pico, tên các chân nguồn thể hiện rõ chức năng hơn: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt; là nguồn 5 V trực tiếp từ USB khi cắm cáp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; là ngõ vào nguồn hệ thống nhận từ USB qua diode và chấp nhận nguồn ngoài trong khoảng 1,8 V đến 5,5 V; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; là ngõ ra đã ổn áp. Không bao giờ đưa pin 7,4 V vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt;, vì cả hai đều vượt quá giới hạn thiết kế, đặc biệt đưa 7,4 V vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; sẽ đánh thẳng vào ngõ ra mạch ổn áp.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_rails_3v3.png&quot; alt=&quot;USB 5 V, 3,3 V đã ổn áp, và ray motor nằm ngoài header&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy hiểu sơ đồ nguồn theo cả mức điện áp lẫn khả năng cấp dòng: đường 5 V từ USB chỉ đủ cho tải nhỏ, đường 3,3 V dành riêng cho mạch logic, mass (GND) phải nối chung, còn đường nguồn động cơ phải tách riêng hoàn toàn khỏi hàng chân vi điều khiển.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/pcb_mcu_anatomy.png&quot; alt=&quot;Giải phẫu board: que đo đặt lên header, không đặt lên module&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Cổng USB cấp cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt; (hoặc chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; trên ESP32), còn mạch ổn áp tạo ra đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;. Khi đo kiểm, đặt đầu que đo trực tiếp vào các chân header, không đặt lên vỏ kim loại của module.&lt;/p&gt;

&lt;p&gt;Việc nối chung mass giữa vi điều khiển và mạch cầu H là bắt buộc. TB6612 đọc mức điện áp PWM dựa trên tham chiếu mass logic của chính nó. Nếu chân GND của driver không nối với GND của ESP32 hoặc Pico, driver sẽ không nhận diện được mức cao/thấp một cách ổn định. Khi lắp driver ở các chương sau, phải nối chung chân GND logic với vi điều khiển, còn đường nguồn động cơ VM đi riêng từ pin.&lt;/p&gt;

&lt;h2 id=&quot;xử-lý-chân-echo-5-v&quot;&gt;Xử lý chân Echo 5 V&lt;/h2&gt;

&lt;p&gt;Ngõ ra Echo của cảm biến siêu âm HC-SR04 xuất xung 5 V, do đó bắt buộc phải hạ mức điện áp trước khi đưa vào chân GPIO của ESP32 hoặc Pico. Cách chắc chắn nhất là dùng mạch chuyển mức logic (level shifter): nối phía điện áp thấp (LV) vào chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; của bo mạch, phía điện áp cao (HV) vào nguồn 5 V của cảm biến và nối chung mass. Tín hiệu Echo đi qua chiều từ HV sang LV rồi vào GPIO. Nhiều cảm biến HC-SR04 có thể nhận tín hiệu kích (Trigger) ở mức 3,3 V, nhưng đó là đặc tính ngõ vào của cảm biến chứ không đồng nghĩa với việc được bỏ qua mạch chuyển mức ở chân Echo.&lt;/p&gt;

&lt;p&gt;Trong trường hợp cần tính toán bằng mạch chia điện trở, nếu mắc điện trở \(1\,\mathrm{k}\Omega\) từ chân Echo đến nút nối GPIO và điện trở \(2\,\mathrm{k}\Omega\) từ nút đó xuống GND, điện áp thu được là:&lt;/p&gt;

\[V_{\mathrm{GPIO}} = 5\,\mathrm{V} \times \frac{2}{1 + 2} \approx 3{,}33\,\mathrm{V}.\]

&lt;p&gt;Giá trị 3,33 V nằm khá sát giới hạn trên cho phép, và sai số điện trở hoặc chiều dài dây dẫn có thể làm điện áp tại nút này dịch chuyển, vì vậy dùng module chuyển mức vẫn ổn định hơn khi lắp trên robot thật. Bài này chưa đấu cảm biến; bạn chỉ cần ghi công thức chia áp lên thẻ sơ đồ chân để chuẩn bị cho các chương sau.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đã-làm&quot;&gt;Ví dụ đã làm&lt;/h2&gt;

&lt;p&gt;Một bộ pin 2S ghép từ hai cell 18650 có điện áp danh định 7,4 V (sạc đầy cao hơn và khi yếu thấp hơn). Hà vẽ đường đi hợp lệ duy nhất cho dây dương (màu đỏ) của pin: nối thẳng vào cực &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; của mạch cầu H TB6612 (loại module bán tại Hshop trong ghi chú chương 00 hỗ trợ dải nguồn động cơ bao gồm 7,4 V). Dây âm (màu đen) nối vào cực &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; của driver, đồng thời kéo một dây riêng nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; này sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; của vi điều khiển.&lt;/p&gt;

&lt;p&gt;Bên cạnh đó, Hà lập danh sách các chân tuyệt đối không được chạm dây dương pin:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Trên ESP32: mọi chân GPIO, chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; và chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIN&lt;/code&gt; (vì nối với nguồn USB 5 V, cấp pin 2S vào có thể đẩy ngược điện áp về laptop).&lt;/li&gt;
  &lt;li&gt;Trên Pico: mọi chân GP, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; (giới hạn tối đa 5,5 V) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; (nguồn logic) của mạch cầu H nhận 3,3 V hoặc 5 V chứ không phải nguồn pin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;. Hà ghi rõ trên bản vẽ: “pin 2S chỉ nối vào VM” và “VCC logic lấy từ 3V3, không lấy từ pin”. Ở bước này chưa nối dây thật; phần thực hành đo điện áp chỉ tiến hành với nguồn USB.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Chỉ cắm cáp USB; đảm bảo đã tháo hoàn toàn pin, mạch cầu H và cảm biến.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Cắm que đỏ vào lỗ đo điện áp (V), đặt que đen lên một chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; xác định, chỉnh đồng hồ về thang đo điện áp một chiều (DCV).&lt;/li&gt;
  &lt;li&gt;Đo đường nguồn 5 V (chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIN&lt;/code&gt; trên ESP32 hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt; trên Pico) và ghi chính xác giá trị đo được.&lt;/li&gt;
  &lt;li&gt;Đo đường nguồn 3,3 V (chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3(OUT)&lt;/code&gt; trên Pico) và ghi chính xác giá trị đo được.&lt;/li&gt;
  &lt;li&gt;Vẽ thẻ sơ đồ chân cho bo mạch của bạn gồm: hai đường nguồn vừa đo, các chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, chân LED của bài nhịp tim, một chân GPIO trống đã đối chiếu nhãn mạch và mục cấm (ghi rõ “không cấp 5 V vào GPIO” và “pin 2S chỉ nối vào VM và GND của driver”).&lt;/li&gt;
  &lt;li&gt;Phác thảo mạch chuyển mức (hoặc mạch chia áp 1 kΩ / 2 kΩ) cho chân Echo của HC-SR04 ngay trên thẻ; chưa đấu dây cảm biến thật.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Kết quả đo chuẩn khi chỉ dùng USB nằm trong khoảng 4,7 V đến 5,1 V ở đường 5 V (chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; của ESP32 qua diode thường đọc khoảng 4,7 V đến 4,9 V) và khoảng 3,25 V đến 3,35 V ở đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;. Ghi chính xác các chữ số đo được, không ghi chung chung là “5 V”. Nếu đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; nằm ngoài khoảng cho phép — đặc biệt nếu vượt quá 3,6 V chứng tỏ có rò áp 5 V — phải rút cáp ngay để kiểm tra. Tuyệt đối không nối pin trong bài thực hành này.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Thứ tự nối: mass chung, logic, tín hiệu, nguồn motor sau cùng, bánh nhấc&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Sơ đồ nhắc lại trình tự đấu nối an toàn: hiện tại bạn vẫn đang ở bước kiểm tra nguồn USB; nguồn động cơ là bước cuối cùng và chỉ được cấp khi bánh xe đã nhấc khỏi bàn.&lt;/p&gt;

&lt;p&gt;Các lỗi đo thường gặp: đặt ngược que đo (que đen vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt;, que đỏ vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;) khiến đồng hồ hiện số âm; đặt que lên vỏ kim loại hoặc thân tụ thay vì chân header; hoặc đo chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; ra 0 V khi USB đang cắm (do đặt nhầm chân, thiếu tiếp xúc mass hoặc hỏng mạch ổn áp — cần dừng lại kiểm tra). Cấp nguồn động cơ TT từ chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; sẽ gây sụt áp ngay khi động cơ khởi động hoặc kẹt trục.&lt;/p&gt;

&lt;p&gt;An toàn: giữ pin ở ngoài khu vực mạch. Không đặt đồng hồ ở thang đo dòng điện (A/mA) rồi chạm trực tiếp giữa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; vì sẽ gây ngắn mạch qua đồng hồ.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=RBDVBafNF44&quot;&gt;HOW TO USE LOGIC LEVEL SHIFTER ON ARDUINO TO CONVERT CIRCUITS AND SENSORS FROM 5V TO 3.3V CIRCUITS&lt;/a&gt; — Circuit Desolator, tiếng Anh. Giải thích lý do không nối thẳng 5 V vào 3,3 V và cách dùng mạch chuyển mức trước khi ghép cảm biến với ESP32.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=f7aySy_0URE&quot;&gt;Logic Level Converters - Learn &amp;amp; Example Project - TXS0108E&lt;/a&gt; — DIY Machines, tiếng Anh. Giới thiệu module chuyển mức 8 kênh; chú ý phân biệt phía điện áp cao (HV) và phía điện áp thấp (LV) cho khớp sơ đồ chân trong bài.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Tính điện áp ngõ ra của mạch chia áp cho nguồn 5 V với điện trở phía trên \(1\,\mathrm{k}\Omega\) và phía dưới \(2\,\mathrm{k}\Omega\). Nếu cả hai điện trở cùng tăng thêm 5%, tỉ số chia áp có thay đổi không?&lt;/li&gt;
  &lt;li&gt;Các cực của pin 2S 7,4 V được phép nối vào chân nào của TB6612, và tuyệt đối không được chạm vào những chân header nào của ESP32?&lt;/li&gt;
  &lt;li&gt;Trên Raspberry Pi Pico, chân nào là USB 5 V, chân nào nhận nguồn ngoài trong dải 1,8 V đến 5,5 V, và chân nào là ngõ ra đã ổn áp? Nếu cấp nhầm 7,4 V thì chân nào chịu rủi ro hư hỏng trực tiếp nhất?&lt;/li&gt;
  &lt;li&gt;Khi chỉ cắm USB, đồng hồ đo được 4,86 V và 3,29 V. Hai giá trị này có nằm trong ngưỡng bình thường không? Thẻ sơ đồ chân vẫn cần bổ sung câu ghi chú gì về cực VM?&lt;/li&gt;
  &lt;li&gt;Một sinh viên nối thẳng chân Echo của HC-SR04 vào GPIO 18 “chỉ để thử nhanh”. Mức điện áp nào tác động lên chân GPIO và cần bổ sung mạch gì trên đường dây đó?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Đáp án&lt;/summary&gt;

1. $$5 \times 2 / 3 \approx 3{,}33\,\mathrm{V}$$. Nếu cả hai điện trở cùng nhân với 1,05 thì hệ số triệt tiêu ở cả tử và mẫu nên tỉ số chia áp không đổi, điện áp lý tưởng vẫn xấp xỉ 3,33 V. Tuy nhiên, nếu sai số giữa hai điện trở lệch nhau thì điện áp sẽ thay đổi — đó là lý do dùng module chuyển mức ổn định hơn mạch chia áp ghép từ điện trở rời.
2. Cực dương pin nối vào `VM`, cực âm nối vào `GND` của mạch cầu H (và `GND` này phải nối chung với `GND` của vi điều khiển). Pin tuyệt đối không được chạm vào bất kỳ chân GPIO nào của ESP32, chân `3V3` hay chân `5V`/`VIN`.
3. `VBUS` là USB 5 V; `VSYS` là ngõ vào chấp nhận khoảng 1,8 V đến 5,5 V (nhận từ USB qua diode); `3V3` là ngõ ra đã ổn áp. Đưa 7,4 V vào `3V3` sẽ đánh thẳng vào ngõ ra ổn áp, còn đưa vào `VSYS` cũng vượt xa ngưỡng cho phép tối đa 5,5 V.
4. Cả hai đều đạt: 4,86 V nằm trong khoảng 4,7 V – 5,1 V và 3,29 V nằm trong khoảng 3,25 V – 3,35 V. Thẻ vẫn cần ghi rõ: pin 2S chỉ nối vào cực `VM` và `GND` của driver, đồng thời xác nhận chưa nối pin trong lần đo này.
5. Chân Echo đẩy xung 5 V vào GPIO 3,3 V, vượt quá giới hạn cực đại tuyệt đối khoảng 3,6 V. Cần tháo dây Echo ngay và lắp qua mạch chuyển mức logic (hoặc mạch chia áp đã tính toán) kèm nối chung mass trước khi thử tiếp.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;Logic levels của SparkFun&lt;/a&gt; — giải thích chi tiết lý do cần chuyển mức giữa tín hiệu 5 V và ngõ vào 3,3 V.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://datasheets.raspberrypi.com/pico/pico-datasheet.pdf&quot;&gt;Datasheet Pico&lt;/a&gt; — sơ đồ và thông số các đường nguồn VBUS, VSYS, 3V3 cùng dải điện áp cho phép của VSYS.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;Datasheet ESP32 (PDF)&lt;/a&gt; — bảng đặc tính điện của chân GPIO và các mức giới hạn cực đại tuyệt đối.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab — nhấp LED, serial và nhịp tim robot</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_04_blink_serial_hello/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_04_blink_serial_hello</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Bài thực hành này xác nhận bộ công cụ (toolchain) đã cài ở bài trước hoạt động ổn định, đồng thời thay thế đoạn mã nháy LED kiểu chặn (blocking) bằng một vòng lặp không chặn có thể xử lý nhiều tác vụ song song. Bạn sẽ đảo trạng thái LED mỗi 250 ms và in một dòng nhịp tim (heartbeat) mỗi 500 ms gồm mốc thời gian tính bằng mili giây và trạng thái LED. Vòng lặp được viết bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; trên ESP32 hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; trên Pico, và bạn chỉ chạy đoạn mã dành cho bo mạch của mình. Qua bài này, bạn giải thích được vì sao không thể dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep&lt;/code&gt; khi cần giám sát ngưỡng dừng an toàn 300 ms, đồng thời nhận diện được vòng lặp reset, lỗi chọn sai chân LED và lỗi đặt tốc độ monitor ở 9600 baud.&lt;/p&gt;

&lt;h2 id=&quot;cần-gì-trước-khi-học&quot;&gt;Cần gì trước khi học&lt;/h2&gt;

&lt;p&gt;Nếu dùng ESP32, bạn cần đã nạp thành công chương trình từ bài PlatformIO hoặc Arduino IDE, xác định được tên cổng và đặt tốc độ 115200 baud ở cả hai phía. Nếu dùng Pico, bo mạch cần đã cài MicroPython, Thonny hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt; kết nối đúng cổng và đèn LED đã hoạt động với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; hoặc GPIO 25. Chỉ cần chuẩn bị một trong hai hệ thống; chưa dùng mạch cầu H, pin hay khung xe. Nếu cáp USB có chất lượng kém, bài thực hành này sẽ làm lộ rõ vấn đề.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Chương trình đầu tiên của mọi kỹ sư nhúng luôn là nhấp nháy đèn LED — nhưng ở cấp độ kỹ thuật robot, đèn LED không phải để “cho vui”, mà là &lt;strong&gt;nhịp tim chẩn đoán (heartbeat)&lt;/strong&gt; báo cho bạn biết vi điều khiển vẫn đang sống và vòng lặp chính không bị treo! Bí mật tách biệt code đồ chơi với firmware robot thật nằm ở đúng một khái niệm: &lt;strong&gt;xóa sổ hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; chặn CPU&lt;/strong&gt; và thay bằng &lt;strong&gt;bộ định thời không chặn (non-blocking scheduler)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-lập-lịch-không-chặn-và-truyền-thông-serial-chẩn-đoán&quot;&gt;Kiến thức cần nhớ: Lập lịch Không chặn và Truyền thông Serial Chẩn đoán&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Blocking (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt;) vs. Non-Blocking (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_ms()&lt;/code&gt;):&lt;/strong&gt; Khi gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt;, CPU đứng yên hoàn toàn suốt 500 ms — bỏ lỡ mọi nút nhấn khẩn cấp, xung encoder và gói tin cảm biến! Lập lịch không chặn kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - last_time &amp;gt;= interval&lt;/code&gt; trong vòng lặp chính, cho phép chạy song song nhiều tác vụ ở nhiều tần số khác nhau trên cùng 1 lõi CPU.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nhịp tim (Heartbeat LED) &amp;amp; Log có Cấu trúc (Structured Serial Log):&lt;/strong&gt; Thay vì in chữ rời rạc kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ok&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;123&lt;/code&gt;, hãy in định dạng bảng CSV/key-value kèm dấu thời gian (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms,state,v_batt,rpm_l,rpm_r&lt;/code&gt;) để có thể vẽ đồ thị và phân tích ngay bằng Python!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ngân sách Băng thông Serial (Đừng để &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt; bóp nghẹt vòng lặp):&lt;/strong&gt; In một dòng log dài 100 byte ở tốc độ 9.600 bps tốn tới &lt;strong&gt;104 ms&lt;/strong&gt; — đủ làm sập hoàn toàn vòng lặp điều khiển 50 Hz (20 ms)!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-so-sánh-delay500-với-millis-và-tính-chi-phí-thời-gian-của-một-dòng-serialprint&quot;&gt;Ví dụ làm từng bước: So sánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; và Tính chi phí thời gian của một dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Mất mẫu cảm biến khi dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt;:&lt;/strong&gt; Giả sử robot cần nhấp LED nhịp tim mỗi &lt;strong&gt;500 ms&lt;/strong&gt; (2 Hz) &lt;em&gt;đồng thời&lt;/em&gt; đọc cảm biến khoảng cách mỗi &lt;strong&gt;20 ms&lt;/strong&gt; (50 Hz):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Nếu viết &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; sau lệnh đảo LED: trong 500 ms đó lẽ ra phải đọc \(500 / 20 = 25\) mẫu cảm biến, nhưng CPU ngủ cứng suốt 500 ms → &lt;strong&gt;bỏ lỡ 24 trên 25 mẫu (mất 96% dữ liệu!)&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Dùng hai bộ định thời độc lập &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last_led&lt;/code&gt; (500 ms) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last_sensor&lt;/code&gt; (20 ms): cả hai chạy mượt mà với &lt;strong&gt;0% mẫu bị mất&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Thời gian truyền 1 dòng log 80 byte qua UART (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8N1&lt;/code&gt; = 10 bit/byte):&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Ở baud rate &lt;strong&gt;9.600 bps&lt;/strong&gt; (\(960\text{ byte/s}\)): \(\dfrac{80}{960} = \mathbf{83.3\text{ ms}}\) (lớn gấp &lt;strong&gt;4 lần&lt;/strong&gt; chu kỳ điều khiển 20 ms → làm treo vòng lặp ngay khi đầy buffer TX!).&lt;/li&gt;
  &lt;li&gt;Ở baud rate &lt;strong&gt;115.200 bps&lt;/strong&gt; (\(11{,}520\text{ byte/s}\)): \(\dfrac{80}{11{,}520} \approx \mathbf{6.94\text{ ms}}\) — và nếu chỉ in log ở tần số 10 Hz (mỗi 100 ms), băng thông chiếm chưa tới \(7\%\)!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-quy-tắc-viết-vòng-lặp-nền-tảng-cho-mọi-firmware-robot&quot;&gt;Giải thích: 4 quy tắc viết vòng lặp nền tảng cho mọi firmware robot&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Tuyệt đối cấm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; bên trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt; chính:&lt;/strong&gt; Mọi chu kỳ thời gian đều dùng phép so sánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - last &amp;gt;= period&lt;/code&gt; (kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint32_t&lt;/code&gt; trên C++ để tự an toàn khi tràn sau 49,7 ngày!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Tách tần số điều khiển khỏi tần số in log:&lt;/strong&gt; Chạy PID/đọc cảm biến ở 50–100 Hz, nhưng chỉ in log ra Serial ở 5–10 Hz để không làm nghẽn bộ đệm UART.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Luôn in dấu thời gian &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms&lt;/code&gt; ở cột đầu tiên:&lt;/strong&gt; Nhìn vào hiệu hai dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms&lt;/code&gt; liên tiếp, bạn lập tức đo được chu kỳ thực tế và phát hiện ngay hiện tượng giật (jitter).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Dùng LED nhịp tim để chẩn đoán không cần dây USB:&lt;/strong&gt; Nhấp đều 1 Hz = chạy bình thường; nhấp nhanh 5 Hz = cảnh báo pin yếu; sáng đứng hoặc tắt hẳn = firmware bị treo!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu bạn chạy vòng lặp điều khiển 100 Hz (chu kỳ 10 ms) và in một dòng log dài 60 byte ở &lt;em&gt;mọi&lt;/em&gt; chu kỳ (tức 100 dòng/giây = 6.000 byte/giây), tốc độ baud tối thiểu cần chọn (với khung 10 bit/byte) là bao nhiêu? \(6{,}000 \times 10 = \mathbf{60{,}000\text{ bps}}\) — nên chọn chuẩn &lt;strong&gt;115.200 bps&lt;/strong&gt; để có biên an toàn gần gấp đôi!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-với-capstone-a&quot;&gt;Vì sao bài này gắn với Capstone A&lt;/h2&gt;

&lt;p&gt;Vòng lặp điều khiển từ xa (teleop) ở chương 07 có cấu trúc tương tự vòng nhịp tim này, chỉ thay việc bật tắt LED bằng việc cập nhật hệ số chu kỳ (duty) cho bánh trái và bánh phải qua TB6612. Mốc thời gian giúp bạn kiểm chứng vòng lặp đang chạy đúng chu kỳ thiết kế. Đồng thời, vòng lặp đó phải phát hiện được khoảng 300 ms không có lệnh mới để đưa cả hai duty về 0, và ở lần đầu cho phép duty khác 0, bánh xe phải được nhấc khỏi bàn. Nếu chương trình bị chặn bên trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt;, vi điều khiển không thể kiểm tra tình trạng mất lệnh cho đến khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; kết thúc, khiến bánh xe tiếp tục quay theo mức PWM cũ thêm tới một giây sau khi người điều khiển đã thả nút. Đến chương 08, khi truyền lệnh qua Wi-Fi, cơ chế kiểm tra quá thời gian (timeout) vẫn dựa trên phép so sánh thời gian không chặn như bài này.&lt;/p&gt;

&lt;h2 id=&quot;hai-bộ-định-thời-không-chặn&quot;&gt;Hai bộ định thời không chặn&lt;/h2&gt;

&lt;p&gt;Nhịp tim (heartbeat) là dòng thông điệp mà firmware tự động gửi ra theo chu kỳ cố định, bất kể bạn có nhập lệnh hay không. Trong bài này, LED đảo trạng thái mỗi 250 ms nên một chu kỳ sáng–tắt trọn vẹn là 500 ms, tương ứng tần số 2 Hz:&lt;/p&gt;

\[T_{\text{nhấp}} = 2 \times 250\,\mathrm{ms} = 500\,\mathrm{ms}, \qquad f = \frac{1}{0{,}5\,\mathrm{s}} = 2\,\mathrm{Hz}.\]

&lt;p&gt;Dòng serial được in mỗi 500 ms với hai trường dữ liệu: thời gian mili giây hiện tại và trạng thái LED (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;). Trong 5 giây quan sát, bạn sẽ thấy khoảng 10 lần nháy trọn vẹn và khoảng 10 dòng dữ liệu in ra, vì chu kỳ in cũng bằng 500 ms.&lt;/p&gt;

&lt;p&gt;Cần lưu ý một hiện tượng lấy mẫu dễ gặp: vì chu kỳ in (500 ms) đúng bằng hai lần chu kỳ đảo đèn (250 ms), nếu hai bộ đếm khởi tạo cùng thời điểm và trạng thái được in ngay sau bước đảo đèn, giá trị ở cột trạng thái có thể lặp lại cùng một số (luôn là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; hoặc luôn là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;) dù ngoài thực tế đèn vẫn nháy đều. Hãy quan sát trực tiếp đèn LED để kiểm tra nhịp nháy, và kiểm tra cột mili giây để đánh giá bộ định thời: hiệu giữa hai dòng liên tiếp phải xấp xỉ 500 ms. Nếu cột trạng thái đứng yên nhưng đèn vẫn nháy đúng 2 Hz, chương trình vẫn hoạt động bình thường; không nên chèn thêm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; để cố thay đổi cột này.&lt;/p&gt;

&lt;p&gt;Hàm lấy thời gian khác nhau tùy nền tảng nhưng cùng một nguyên lý. Trên core Arduino của ESP32, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; đếm số mili giây kể từ lúc khởi động và sẽ tràn bộ đếm (quay vòng về 0) sau:&lt;/p&gt;

\[2^{32}\,\mathrm{ms} \approx 49{,}7\,\text{ngày}.\]

&lt;p&gt;Phép trừ số nguyên không dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - last&lt;/code&gt; vẫn cho kết quả chính xác ngay cả khi bộ đếm tràn, miễn cả hai biến đều thuộc kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint32_t&lt;/code&gt;. Trên Pico, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; cũng đếm mili giây, và cần dùng hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff(now, last)&lt;/code&gt; để tính hiệu thời gian thay vì phép trừ thông thường của Python (vốn sẽ sai khi bộ đếm quay vòng). Ở cả hai nền tảng, mốc thời gian tăng đều khoảng 500 ms mỗi dòng là bằng chứng vòng lặp chạy ổn định; ngược lại, nếu con số đột ngột nhảy về giá trị nhỏ — đặc biệt kèm theo dòng tiêu đề khởi động lặp lại — chip đã bị reset.&lt;/p&gt;

&lt;p&gt;Bài này loại bỏ hoàn toàn kiểu viết dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(1)&lt;/code&gt;. Trong suốt thời gian CPU dừng ở hàm chặn, phép kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - lastCmdMs &amp;gt; 300&lt;/code&gt; không được thực thi: nếu ngưỡng timeout xảy ra sau 50 ms kể từ khi vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt;, lệnh dừng bánh sẽ bị trễ tới 950 ms. Với vòng lặp không chặn, điều kiện được kiểm tra ở mỗi vòng lặp, nên độ trễ sau timeout chỉ bằng thời gian của một chu kỳ vòng lặp — rất nhỏ so với 300 ms. Trong mã mẫu dưới đây, dòng điều khiển động cơ chỉ để dạng chú thích vì trên bàn chưa nối driver.&lt;/p&gt;

&lt;p&gt;Chỉ chạy đoạn mã dành cho bo mạch của bạn; đoạn mã còn lại chỉ dùng để đọc tham khảo.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ESP32 (core Arduino):&lt;/strong&gt; đổi hằng số chân nếu nhãn trên mạch không dùng GPIO 2 (hoặc dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; nếu đã đối chiếu khớp với bo mạch).&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kLedPin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// DevKit V1 thường dùng GPIO 2; đối chiếu silk&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TOGGLE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BEAT_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastToggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastBeat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kLedPin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastToggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastBeat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;heartbeat&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastToggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TOGGLE_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastToggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kLedPin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastBeat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BEAT_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastBeat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// ms, state. Sau này cả hai duty bánh đi theo đồng hồ này.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Timeout lệnh 300 ms là một phép so ở đây, không phải delay.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%lu,%d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Capstone A, không phải hôm nay:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// if (now - lastCmdMs &amp;gt; 300) { cả hai duty TB6612 = 0; }&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Raspberry Pi Pico (MicroPython):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt; dùng cho Pico W và Pico 2 W; nhánh dự phòng GPIO 25 dùng cho Pico bản gốc và Pico 2 không W khi firmware chưa hỗ trợ bí danh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# Pico W, Pico 2 W
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# Pico gốc, Pico 2
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;TOGGLE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;BEAT_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_toggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_beat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;heartbeat&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_toggle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TOGGLE_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_toggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_beat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BEAT_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_beat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# ms, state. Timeout 300 ms đứng cạnh phép thử này.
&lt;/span&gt;        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# sau: if time.ticks_diff(now, last_cmd) &amp;gt; 300: cả hai duty = 0
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hãy ghi sẵn quy tắc “nhấc bánh khỏi bàn” vào sổ tay ngay từ bây giờ dù chưa lắp khung xe: bất cứ khi nào nạp hoặc thử nghiệm lệnh có thể làm động cơ quay, lốp xe không được chạm mặt bàn để tránh robot tự chạy rơi khỏi bàn. Hiện tại, mạch TB6612 và pin vẫn để nguyên trong hộp linh kiện.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Thứ tự nguồn: logic trước, nguồn motor sau, bánh nhấc lên&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Sơ đồ minh họa đúng trình tự đang thực hành: nguồn USB và đường 3,3 V ổn định đi trước, được kiểm chứng bằng bài nháy LED và dòng nhịp tim serial; phần driver và pin nằm ở giai đoạn sau.&lt;/p&gt;

&lt;h2 id=&quot;đọc-và-kiểm-tra-log&quot;&gt;Đọc và kiểm tra log&lt;/h2&gt;

&lt;p&gt;Sau một dòng tiêu đề &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; duy nhất, hãy chép lại 10 dòng kế tiếp. Dưới đây là mẫu log hoạt động bình thường trên ESP32 (thời gian tính bằng mili giây):&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;heartbeat
1840,0
2342,0
2843,0
3344,0
3846,0
4347,0
4848,0
5349,0
5851,0
6352,0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Giá trị tuyệt đối của dòng đầu tiên phụ thuộc vào thời gian khởi động &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt; và độ trễ mở cổng USB. Điều quan trọng là hiệu giữa các dòng liên tiếp phải xấp xỉ 500 ms (sai lệch vài mili giây là bình thường). Do hai bộ đếm khởi phát cùng lúc và 500 ms đúng bằng hai lần 250 ms, lần in luôn rơi vào cùng một trạng thái nên cột thứ hai lặp lại giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;; miễn đèn LED vẫn nháy 2 Hz thì đây không phải lỗi kẹt chân. Ngược lại, nếu mốc thời gian quay về số nhỏ hoặc xuất hiện thêm dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; thứ hai giữa 10 dòng, chip đã bị reset và bài thực hành chưa đạt cho đến khi thu được chuỗi 10 dòng tăng đơn điệu.&lt;/p&gt;

&lt;p&gt;Khi không nối động cơ mà bo mạch vẫn reset, hãy kiểm tra cáp USB hoặc các điểm chạm chập (như dây nối vô tình chập từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; xuống &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;). Cáp sạc mỏng, kém chất lượng có thể khiến cổng kết nối chập chờn rồi sụt áp. Hãy tháo hết dây nối ngoài, đổi cáp tốt và ghi lại một khối log sạch. Nếu monitor hiển thị toàn ký tự rác trong khi LED vẫn nháy 2 Hz, nguyên nhân là lệch tốc độ baud (chẳng hạn monitor để 9600 còn chương trình là 115200). Nếu cột mili giây tăng đều nhưng đèn không sáng, hãy kiểm tra lại hằng số chân LED theo nhãn mạch (và trên Pico W thì dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; thay vì GPIO 25).&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đã-làm&quot;&gt;Ví dụ đã làm&lt;/h2&gt;

&lt;p&gt;Dũng sử dụng bo mạch ESP32 DevKit cổ điển (LED ở GPIO 2, chip CH340), nạp đoạn mã C++ và mở monitor ở 115200 baud. Lần chạy đầu in hai dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; trong vòng 6 giây kèm mốc thời gian tụt về gần 200 ms dù không nối động cơ. Nguyên nhân là sợi cáp chỉ sạc hoạt động chập chờn. Sau khi đổi sang cáp dữ liệu, log chỉ hiện một dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; duy nhất và mốc thời gian tăng đều khoảng 501 ms mỗi dòng, đèn nháy 2 Hz. Dũng dán 10 dòng log vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; kèm ghi chú: “Nhấc bánh khỏi bàn trước mọi lệnh điều khiển TB6612; chưa nối VM và pin.”&lt;/p&gt;

&lt;p&gt;Ở bàn Pico, sinh viên chạy đoạn mã MicroPython với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; trên Pico W. Giá trị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_ms&lt;/code&gt; tăng đều khoảng 500 ms mỗi dòng, sổ ghi rõ: “Pico W, dùng bí danh LED, không dùng GPIO 25, nhấc bánh khi thử động cơ, chưa nối động cơ.”&lt;/p&gt;

&lt;p&gt;Dũng cũng ghi thêm phép đối chiếu bên lề giải thích lý do bỏ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt;: nếu mất lệnh từ thời điểm \(t = 0\), hệ thống phải đưa PWM về 0 ngay khi \(t &amp;gt; 300\,\mathrm{ms}\). Trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt;, điều kiện không được kiểm tra nên mức duty cũ có thể duy trì tới \(t = 1000\,\mathrm{ms}\), trong khi vòng lặp không chặn sẽ ngắt ngay ở vòng kế tiếp sau mốc 300 ms.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Mở dự án đã nạp thành công (hoặc phiên MicroPython đang chạy) từ bài trước. Thay chương trình nháy LED kiểu chặn bằng đoạn mã không chặn tương ứng với bo mạch của bạn.&lt;/li&gt;
  &lt;li&gt;Kiểm tra lại chân LED: dùng GPIO 2 hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; nếu khớp nhãn mạch trên ESP32; dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; hoặc GPIO 25 tùy phiên bản Pico.&lt;/li&gt;
  &lt;li&gt;Mở Serial Monitor hoặc REPL ở tốc độ 115200 baud (nếu nền tảng yêu cầu chọn baud). Xác nhận xuất hiện một dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt;, tiếp theo là các dòng cách nhau khoảng 500 ms và đèn nháy 2 Hz.&lt;/li&gt;
  &lt;li&gt;Chép 10 dòng log vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, ghi kèm tên bo mạch, chân LED, quy tắc nhấc bánh và xác nhận chưa nối chân VM hay pin.&lt;/li&gt;
  &lt;li&gt;Nếu dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; lặp lại, dừng lại kiểm tra cáp hoặc chạm chập phần cứng trước khi chỉnh sửa mã nguồn.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hoàn thành đến đây là đủ; không gắn thêm Wi-Fi, mạch cầu H hay pin trong bài thực hành này.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=sJtJJU3c3xE&quot;&gt;Lập trình ESP32 - Bài 1 - Cách nạp chương trình cho ESP32 bằng Arduino IDE - Tự học lập trình IOT&lt;/a&gt; — Điện thông minh E-smart, tiếng Việt. Hướng dẫn nạp chương trình nháy LED trên ESP32; bước in dòng serial nhịp tim trong bài này là bước tiếp nối ngay sau đó.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[ESP32 LED + Push Button Tutorial for Beginners&lt;/td&gt;
          &lt;td&gt;ESP32 GPIO Input &amp;amp; Output](https://www.youtube.com/watch?v=zCzchrkpHzk) — Alvin Pribadi, tiếng Anh. Ví dụ kết hợp nút nhấn và LED trên GPIO, tương ứng với các thao tác vào/ra cơ bản tiếp theo.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Với chu kỳ nháy 500 ms (đảo LED mỗi 250 ms) và chu kỳ in nhịp tim 500 ms, trong 4 giây bạn sẽ quan sát được bao nhiêu chu kỳ nháy trọn vẹn và bao nhiêu dòng nhịp tim?&lt;/li&gt;
  &lt;li&gt;Vì sao cột trạng thái trong log luôn hiển thị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; nhưng bài thực hành vẫn có thể đạt yêu cầu? Bạn cần kiểm tra những yếu tố nào để xác nhận?&lt;/li&gt;
  &lt;li&gt;Giả sử lệnh dừng an toàn cần kích hoạt sau 300 ms kể từ gói lệnh cuối, nhưng vòng lặp lại dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt;. Tính từ lúc điều kiện quá thời gian (timeout) trở thành đúng, lệnh đưa duty về 0 có thể bị trễ tối đa bao lâu?&lt;/li&gt;
  &lt;li&gt;Một đoạn log có dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; thứ hai chen vào giữa dòng 4 và dòng 5, đồng thời cột mili giây tụt từ 2600 xuống 180 dù không nối động cơ. Nêu hai nguyên nhân hợp lý và một nguyên nhân chưa phù hợp để kết luận.&lt;/li&gt;
  &lt;li&gt;Monitor đang đặt ở 9600 baud, màn hình toàn ký tự rác nhưng đèn LED vẫn nháy đều 2 Hz. Bạn cần chỉnh thông số nào, và vì sao không nên vội cài lại core?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Đáp án&lt;/summary&gt;

1. Trong 4 giây có $$4 / 0{,}5 = 8$$ chu kỳ nháy trọn vẹn và $$4 / 0{,}5 = 8$$ dòng nhịp tim. Dòng đầu tiên có thể trễ nhẹ do thời gian khởi tạo `setup` hoặc REPL, nhưng khoảng cách giữa các dòng liên tiếp phải xấp xỉ 500 ms.
2. Vì chu kỳ in (500 ms) là bội số nguyên của chu kỳ đảo đèn (250 ms), thời điểm lấy mẫu luôn rơi vào cùng một pha nên trạng thái in ra bị lặp lại. Để xác nhận đạt, hãy nhìn trực tiếp đèn LED xem có nháy 2 Hz không và kiểm tra cột mili giây có tăng đều khoảng 500 ms hay không.
3. Trong lúc thực thi `delay(1000)`, phép kiểm tra không chạy nên thời điểm đưa duty về 0 có thể bị trễ tối đa tới 1000 ms sau khi điều kiện timeout đã đúng. Ngược lại, vòng lặp không chặn sẽ xử lý ngay ở lượt lặp đầu tiên sau mốc 300 ms.
4. Chip đang bị reset. Khi không có tải động cơ, hai nguyên nhân hợp lý là cáp USB kém/chập chờn, dây nối ngoài gây chạm chập nguồn (hoặc nút EN bị chạm/nhiễu). Không thể kết luận lỗi do động cơ kéo sụt chân VM vì hiện tại chưa nối dây VM hay động cơ.
5. Chỉnh tốc độ Serial Monitor lên 115200 baud cho khớp với `Serial.begin(115200)`. Đèn LED nháy đều chứng tỏ chương trình đã nạp và đang chạy bình thường; ký tự rác chỉ do lệch tốc độ baud, không phải lỗi hỏng chip hay hỏng core.

&lt;/details&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bạn không cần mua thêm linh kiện nếu bo mạch và cáp dữ liệu đã hoạt động ổn định. Nếu mạch liên tục reset do dùng cáp chưa rõ nguồn gốc, hãy thay bằng cáp dữ liệu &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;. Nếu vẫn chưa có bo mạch: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;NodeMCU-32S giá 190 000 ₫&lt;/a&gt; hoặc &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32 giá 265 000 ₫&lt;/a&gt;, tránh chọn module trần &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;ESP32-S3-WROOM-1 giá 135 000 ₫&lt;/a&gt;. Với hướng Pico, kết quả tìm kiếm &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;raspberry pi pico&lt;/a&gt; ngày 23 tháng 9 năm 2026 hiển thị Pico 2 giá 195 000 ₫ và Pico 2 W giá 275 000 ₫ (&lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-rp2350&quot;&gt;Pico 2&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-w-rp2350&quot;&gt;Pico 2 W&lt;/a&gt;). Tham khảo thêm trên Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=esp32%20devkit%20ch340&quot;&gt;ESP32 DevKit CH340&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=raspberry%20pi%20pico%202&quot;&gt;Pico 2&lt;/a&gt;. Danh sách linh kiện đầy đủ nằm ở &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;chương 00&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.platformio.org/en/latest/boards/espressif32/esp32dev.html&quot;&gt;PlatformIO &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;&lt;/a&gt; — cấu hình bo mạch dùng cho đoạn mã C++ trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html&quot;&gt;Cài arduino-esp32&lt;/a&gt; — gói thư viện lõi cung cấp hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/micropython.html&quot;&gt;MicroPython trên Pico&lt;/a&gt; — tài liệu về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;machine.Pin&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://datasheets.raspberrypi.com/pico/pico-datasheet.pdf&quot;&gt;Datasheet Pico&lt;/a&gt; — đối chiếu thông tin GPIO 25 trước khi sử dụng nhánh dự phòng trên các bản không có W.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Raspberry Pi Pico và MicroPython</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_03_pico_micropython/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_03_pico_micropython</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn biết cách đưa Raspberry Pi Pico hoặc Pico 2 vào chế độ BOOTSEL để ổ bootloader (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt; trên Pico/RP2040 hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RP2350&lt;/code&gt; trên Pico 2/RP2350) xuất hiện, rồi chép đúng tệp firmware UF2 MicroPython cho từng phiên bản. Bạn cấu hình được Thonny hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt; kết nối tới bo mạch và kiểm tra REPL trả lời phép tính &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 + 1&lt;/code&gt;. Bạn lập trình nháy LED tích hợp ở tần số 2 Hz kèm in bộ đếm, phân biệt rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; trên Pico W / Pico 2 W với GPIO 25 trên Pico bản gốc / Pico 2 không W, đồng thời tránh nạp nhầm tệp UF2 của RP2040 lên Pico 2. Toàn bộ GPIO giữ ở mức 3,3 V và chưa đấu nối động cơ.&lt;/p&gt;

&lt;h2 id=&quot;cần-gì-trước-khi-học&quot;&gt;Cần gì trước khi học&lt;/h2&gt;

&lt;p&gt;Bạn cần đã chọn dòng bo mạch Pico ở bài trước, hoặc muốn tìm hiểu nhanh quy trình làm việc của dòng mạch này. Bạn chỉ cần biết thao tác chép tệp vào ổ đĩa USB và chuẩn bị một cáp micro-USB có truyền dữ liệu. Bài này không sử dụng PlatformIO (công cụ dành cho hướng ESP32). Danh sách linh kiện đầy đủ của robot vẫn nằm ở chương 00 (&lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;Hóa đơn linh kiện và cách mua&lt;/a&gt;).&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Nếu C++ giống như tự tay điều khiển hộp số sàn để vắt từng mili-giây hiệu năng, thì &lt;strong&gt;Raspberry Pi Pico + MicroPython&lt;/strong&gt; giống như việc mở thẳng cửa sổ trò chuyện trực tiếp với phần cứng! Cắm Pico vào máy tính qua chế độ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BOOTSEL&lt;/code&gt;, kéo thả đúng một tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.uf2&lt;/code&gt;, và chỉ 10 giây sau bạn đã có dấu nhắc &lt;strong&gt;REPL (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt;)&lt;/strong&gt; để bật tắt chân GPIO, đọc cảm biến I2C hay thử nghiệm công thức điều khiển ngay lập tức mà không cần chờ biên dịch lại!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kiến-trúc-rp2040-và-thực-thi-micropython-trên-pico&quot;&gt;Kiến thức cần nhớ: Kiến trúc RP2040 và Thực thi MicroPython trên Pico&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bootloader USB Mass Storage (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BOOTSEL&lt;/code&gt;):&lt;/strong&gt; Chip RP2040 tích hợp sẵn bootloader trong ROM không bao giờ hỏng: giữ nút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BOOTSEL&lt;/code&gt; khi cắm USB, Pico hiện ra như một ổ USB &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt; để kéo thả tệp firmware &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.uf2&lt;/code&gt; (phân biệt rõ bản &lt;strong&gt;Pico thường&lt;/strong&gt; vs &lt;strong&gt;Pico W&lt;/strong&gt; có chip Wi-Fi!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thông dịch tương tác (REPL) &amp;amp; Hệ thống tệp nội bộ:&lt;/strong&gt; MicroPython chạy trực tiếp trình thông dịch trên RAM/Flash của RP2040; tệp lưu với tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt; trên Pico sẽ tự động chạy ngay khi cấp nguồn độc lập bằng pin (không cần cắm máy tính).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Định thời Không tràn số (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_ms&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_diff&lt;/code&gt;) &amp;amp; Đường nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt;:&lt;/strong&gt; Khác với Python trên PC dùng số nguyên độ chính xác vô hạn, MicroPython dùng bộ đếm 30-bit cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_ms()&lt;/code&gt; để chạy nhanh không cấp phát bộ nhớ; về nguồn điện, chân &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt;&lt;/strong&gt; (1,8 V – 5,5 V) là đầu vào chuẩn cho nguồn pin ngoài qua diode chống ngược tích hợp!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tránh-lỗi-tràn-bộ-đếm-30-bit-trên-micropython-bằng-ticks_diff&quot;&gt;Ví dụ làm từng bước: Tránh lỗi tràn bộ đếm 30-bit trên MicroPython bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_diff()&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Trên Raspberry Pi Pico, hàm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; đếm từ \(0\) đến \(2^{30} - 1 = 1{,}073{,}741{,}823\text{ ms}\) (\(\approx \mathbf{12.43\text{ ngày}}\)) rồi quay về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; (&lt;em&gt;wrap-around&lt;/em&gt;).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Giả sử ngay trước khi tràn, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_start = 1_073_741_800&lt;/code&gt;, và \(50\text{ ms}\) sau bộ đếm quay vòng về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_now = 26&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Nếu bạn viết phép trừ ngây thơ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_now - t_start&lt;/code&gt;:
\(26 - 1{,}073{,}741{,}800 = \mathbf{-1{,}073{,}741{,}774\text{ ms}} \quad (\text{số âm khổng lồ → vòng lặp treo cứng!}).\)&lt;/li&gt;
  &lt;li&gt;Dùng chuẩn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff(t_now, t_start)&lt;/code&gt;: hàm thực hiện phép trừ modulo \(2^{30}\) có dấu và trả về chính xác &lt;strong&gt;\(+50\text{ ms}\)&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-bước-làm-chủ-raspberry-pi-pico-và-micropython-trên-robot&quot;&gt;Giải thích: 4 bước làm chủ Raspberry Pi Pico và MicroPython trên robot&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Chọn đúng tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.uf2&lt;/code&gt; theo phần cứng (Pico vs. Pico W):&lt;/strong&gt; Trên Pico thường, LED trên bo nối vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GP25&lt;/code&gt;; trên &lt;strong&gt;Pico W&lt;/strong&gt;, LED nối qua chip Wi-Fi nên phải gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;, Pin.OUT)&lt;/code&gt; — nạp nhầm bản &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.uf2&lt;/code&gt; sẽ không điều khiển được LED!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Tận dụng REPL để kiểm thử từng linh kiện rời:&lt;/strong&gt; Trước khi viết chương trình dài, gõ 3 dòng trực tiếp trong REPL (Thonny) để quét địa chỉ I2C hoặc thử quay từng bánh xe giúp tiết kiệm hàng giờ gỡ lỗi.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Luôn cấp nguồn pin ngoài vào chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt;, không cấp vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;:&lt;/strong&gt; Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; đi qua mạch Buck-Boost nội bộ của Pico tạo ra nguồn 3,3 V sạch và có diode bảo vệ khi cắm đồng thời cáp USB (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Chừa đường thoát an toàn trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt;:&lt;/strong&gt; Chèn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(1)&lt;/code&gt; ở đầu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt; để nếu code lỡ kẹt vòng lặp vô hạn, bạn vẫn kịp bấm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl+C&lt;/code&gt; trong Thonny giành lại quyền điều khiển REPL!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Một bộ nguồn hạ áp Buck ngoài cấp \(5.0\text{ V}\) vào chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; của Pico, đồng thời bạn cắm cáp USB vào máy tính để xem log. Điều gì bảo vệ dòng điện không chảy ngược từ pin vào cổng USB máy tính? Diode Schottky tích hợp giữa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; trên bo Pico sẽ chặn dòng chảy ngược (nhưng nếu cấp nguồn vào chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt; thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt;, bạn sẽ bỏ qua diode bảo vệ này!).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-với-capstone-a&quot;&gt;Vì sao bài này gắn với Capstone A&lt;/h2&gt;

&lt;p&gt;Nếu chọn Pico làm bộ điều khiển chính, tệp UF2 MicroPython và cửa sổ REPL này sẽ là môi trường phát triển cho firmware điều khiển từ xa (teleop) ở chương 07: nhận lệnh hai bánh, xuất PWM tới mạch cầu H TB6612 và chạy vòng lặp giám sát để đưa cả hai duty về 0 sau khoảng 300 ms không có lệnh mới (với quy tắc nhấc bánh khỏi bàn ở lần thử đầu tiên). MicroPython rút ngắn thời gian kiểm thử vì bạn có thể tương tác trực tiếp với vi điều khiển trước khi chạy tệp hoàn chỉnh. Lưu ý rằng các bản Pico không có chữ W không tích hợp Wi-Fi, nên đến bài kết nối không dây ở chương 08, bạn sẽ cần Pico W, Pico 2 W hoặc chuyển sang ESP32.&lt;/p&gt;

&lt;h2 id=&quot;chế-độ-bootsel-và-chọn-đúng-tệp-uf2&quot;&gt;Chế độ BOOTSEL và chọn đúng tệp UF2&lt;/h2&gt;

&lt;p&gt;Cả RP2040 lẫn RP2350 đều có thể khởi động từ bộ nhớ ROM nạp sẵn (bootloader) thông qua thao tác vật lý: rút hẳn cáp khỏi bo mạch, nhấn giữ nút BOOTSEL, cắm cáp USB vào máy tính trong khi vẫn giữ nút, sau đó thả nút BOOTSEL. Máy tính sẽ nhận diện một ổ đĩa gắn ngoài tương tự USB nhỏ: trên Pico bản gốc và Pico W (chip RP2040), ổ có tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt;; trên Pico 2 và Pico 2 W (chip RP2350), ổ có tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RP2350&lt;/code&gt;. Dù tên ổ phân biệt được thế hệ chip RP2040 với RP2350, nó vẫn không phân biệt bản không W với bản có W trong cùng thế hệ, nên bạn luôn phải đối chiếu tên tệp UF2 với đúng nhãn in trên bo mạch.&lt;/p&gt;

&lt;p&gt;Tải tệp firmware cho Pico bản gốc (RP2040) tại trang chính thức &lt;a href=&quot;https://micropython.org/download/RPI_PICO/&quot;&gt;MicroPython cho RPI_PICO&lt;/a&gt;, còn Pico 2 (RP2350) dùng tệp riêng tại &lt;a href=&quot;https://micropython.org/download/RPI_PICO2/&quot;&gt;MicroPython cho RPI_PICO2&lt;/a&gt;. Trang tổng hợp &lt;a href=&quot;https://micropython.org/download/&quot;&gt;micropython.org/download&lt;/a&gt; cũng liệt kê riêng bản dành cho Pico W và Pico 2 W. Hãy chép tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.uf2&lt;/code&gt; vào đúng ổ bootloader đó. Ngay sau khi chép xong, ổ đĩa sẽ tự ngắt kết nối và chip khởi động lại vào môi trường MicroPython. Ổ đĩa sẽ không xuất hiện lại trừ khi bạn lặp lại thao tác giữ nút BOOTSEL; việc ổ tự biến mất là dấu hiệu nạp thành công.&lt;/p&gt;

&lt;p&gt;Tuyệt đối không nạp tệp của RP2040 lên Pico 2. Bootloader vẫn có thể cho phép chép tệp, nhưng sau đó bo mạch sẽ đứng im và không tạo được cổng REPL. Nếu lỡ nạp nhầm, hãy thực hiện lại thao tác rút cáp, giữ BOOTSEL, cắm cáp rồi chép đúng tệp UF2 của RP2350. Tương tự, nạp nhầm tệp của bản Pico W lên bản không W (hoặc ngược lại) cũng gây lỗi; hãy luôn đối chiếu với dòng chữ in trực tiếp trên bo mạch của bạn.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Raspberry_Pi_Pico.jpg&quot; alt=&quot;Pico gốc: BOOTSEL, USB, LED GPIO 25&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Trước khi chép firmware, hãy xác định ba vị trí mốc trên ảnh và trên mạch thực tế: cổng micro-USB, nút BOOTSEL và đèn LED tích hợp nằm gần cổng USB. Ảnh minh họa là Pico bản gốc với đèn LED nối ở GPIO 25; trên Pico W, đèn không nằm ở chân này.&lt;/p&gt;

&lt;h2 id=&quot;kết-nối-repl-và-điều-khiển-led&quot;&gt;Kết nối REPL và điều khiển LED&lt;/h2&gt;

&lt;p&gt;Thonny là trình soạn thảo thuận tiện vì hiển thị rõ trình thông dịch đang dùng. Sau khi cài đặt và mở Thonny, chọn trình thông dịch &lt;strong&gt;MicroPython (Raspberry Pi Pico)&lt;/strong&gt; cùng cổng kết nối tương ứng với bo mạch vừa khởi động lại. Lỗi phổ biến nhất là trình thông dịch vẫn đang trỏ vào Python cục bộ trên laptop: khi đó lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print&lt;/code&gt; vẫn chạy nhưng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import machine&lt;/code&gt; sẽ báo lỗi, dễ gây nhầm lẫn là Pico bị hỏng.&lt;/p&gt;

&lt;p&gt;Nếu quen làm việc trên dòng lệnh, bạn có thể dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt; (cài qua &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; trong môi trường cục bộ) để mở cùng phiên REPL đó. Chỉ dùng một trong hai công cụ tại một thời điểm để tránh tranh chấp cổng serial.&lt;/p&gt;

&lt;p&gt;Nhấp vào cửa sổ Shell và gõ:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu kết quả trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; kèm dấu nhắc mới, kết nối đã sẵn sàng. Trong chế độ Pico của Thonny không cần gọi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.begin&lt;/code&gt;; nếu sau này bạn dùng phần mềm terminal serial thông thường, cổng USB REPL của MicroPython vẫn hoạt động như một thiết bị serial với tốc độ chuẩn 115200 baud.&lt;/p&gt;

&lt;p&gt;Chân LED tích hợp là điểm dễ gây nhầm lẫn nhất giữa các phiên bản Pico:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Pico bản gốc (RP2040):&lt;/strong&gt; LED nối với GPIO 25. Các bản MicroPython gần đây cũng hỗ trợ bí danh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt; cho chân này; nếu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; báo lỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ValueError&lt;/code&gt;, hãy dùng trực tiếp GPIO 25.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pico W:&lt;/strong&gt; LED được nối vào chip không dây CYW43439 chứ không nối vào GPIO 25 (lúc này GPIO 25 là chân I/O thông thường). Bắt buộc dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; để bật tắt đèn tích hợp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pico 2 (RP2350, không W):&lt;/strong&gt; giữ cách mắc như bản gốc, đèn người dùng nằm ở GPIO 25.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pico 2 W:&lt;/strong&gt; giống Pico W, phải dùng bí danh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt;, không dùng GPIO 25.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Quy tắc thực hành là đọc kỹ nhãn in trên bo mạch trước khi chọn tham số khởi tạo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Bài thực hành yêu cầu tần số nháy 2 Hz: bật 250 ms và tắt 250 ms, tổng chu kỳ là 500 ms:&lt;/p&gt;

\[f = \frac{1}{0{,}250\,\mathrm{s} + 0{,}250\,\mathrm{s}} = 2\,\mathrm{Hz}.\]

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Pico W / Pico 2 W: đèn là Pin(&quot;LED&quot;), không phải GPIO 25.
# Pico gốc và Pico 2 (không W): đèn là GPIO 25.
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hãy chạy thử trực tiếp từ trình soạn thảo và quan sát đèn trước khi lưu thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt;. Một tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt; bị lỗi hoặc vòng lặp khóa cứng sẽ tự chạy mỗi lần cấp nguồn, khiến bo mạch có vẻ như bị treo; nếu gặp tình huống này, bạn phải đưa mạch vào lại chế độ BOOTSEL hoặc ngắt chương trình để sửa. Ở bài nháy LED đầu tiên này, dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep&lt;/code&gt; là chấp nhận được vì chỉ cần quan sát nhịp 2 Hz và bộ đếm; sang bài sau, chúng ta sẽ bỏ hàm chặn này để vòng lặp theo dõi được ngưỡng mất lệnh 300 ms.&lt;/p&gt;

&lt;h2 id=&quot;tóm-tắt-nhanh-về-đường-nguồn&quot;&gt;Tóm tắt nhanh về đường nguồn&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt; là đường nguồn 5 V trực tiếp từ cổng USB khi đang cắm cáp. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; là ngõ vào nguồn hệ thống của bo mạch, chấp nhận dải điện áp khoảng 1,8 V đến 5,5 V (nguồn USB đi tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; qua một diode). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; là đường nguồn đã ổn áp (ngõ ra). Mọi chân GP đều hoạt động ở mức logic 3,3 V. Đưa tín hiệu 5 V vào chân GP có thể làm hỏng chip, và tuyệt đối không nối pin 2S (7,4 V) vào chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt;. Bạn sẽ đo kiểm các đường nguồn này ở bài sơ đồ chân; hôm nay chỉ cấp nguồn qua USB.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đã-làm&quot;&gt;Ví dụ đã làm&lt;/h2&gt;

&lt;p&gt;Chi sử dụng Pico bản gốc cùng một sợi cáp điện thoại cũ chưa từng dùng để chép dữ liệu. Lần cắm đầu tiên, dù giữ nút BOOTSEL đúng cách, máy tính vẫn không hiện ổ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt;. Sau khi đổi sang cáp dữ liệu Ugreen, ổ đĩa lập tức xuất hiện. Chi suýt chép nhầm tệp UF2 của Pico 2 có sẵn trong thư mục tải về, nhưng kịp kiểm tra lại nhãn in trên mạch ghi “Pico” nên đã tải đúng tệp từ trang RPI_PICO. Sau khi chép, ổ tự biến mất. Mở Thonny, ban đầu trình thông dịch vẫn trỏ vào Python trên laptop nên lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import machine&lt;/code&gt; báo lỗi. Chuyển sang &lt;strong&gt;MicroPython (Raspberry Pi Pico)&lt;/strong&gt; thì dấu nhắc REPL hiện ra, gõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 + 1&lt;/code&gt; trả về &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; hoạt động trên bản firmware này, đèn xanh nháy hai lần mỗi giây và bộ đếm tăng đều trong cửa sổ Shell. Chi ghi vào sổ: “Pico gốc, UF2 RP2040, bí danh LED chạy được, GPIO 25 cùng chân đèn, không Wi-Fi, không nối động cơ.”&lt;/p&gt;

&lt;p&gt;Ở bàn bên cạnh là một bo mạch Pico 2 W (ổ bootloader hiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RP2350&lt;/code&gt;). Nạp tệp RP2040 sẽ sai họ chip; sau khi nạp đúng UF2 cho Pico 2 W, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; bật được đèn và GPIO 25 được ghi chú là chân trống, không phải chân LED.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Rút hẳn cáp khỏi bo mạch, nhấn giữ nút BOOTSEL, cắm cáp dữ liệu rồi thả nút. Xác nhận ổ bootloader xuất hiện (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt; trên Pico/Pico W hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RP2350&lt;/code&gt; trên Pico 2/Pico 2 W).&lt;/li&gt;
  &lt;li&gt;Tải đúng tệp UF2 khớp với bo mạch: &lt;a href=&quot;https://micropython.org/download/RPI_PICO/&quot;&gt;RPI_PICO&lt;/a&gt; cho Pico gốc, &lt;a href=&quot;https://micropython.org/download/RPI_PICO2/&quot;&gt;RPI_PICO2&lt;/a&gt; cho Pico 2, hoặc bản có W tương ứng từ &lt;a href=&quot;https://micropython.org/download/&quot;&gt;trang tải chính thức&lt;/a&gt;. Chép tệp vào ổ bootloader và đợi ổ tự biến mất.&lt;/li&gt;
  &lt;li&gt;Mở Thonny chọn &lt;strong&gt;MicroPython (Raspberry Pi Pico)&lt;/strong&gt; (hoặc chạy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt;), gõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 + 1&lt;/code&gt; và xác nhận kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Chạy đoạn mã nháy LED ở trên. Kiểm tra đèn nháy hai lần mỗi giây và bộ đếm tăng liên tục 1, 2, 3 mà không bị khởi động lại.&lt;/li&gt;
  &lt;li&gt;Ghi vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; đúng tên in trên mạch (Pico, Pico W, Pico 2 hoặc Pico 2 W), nguồn tải tệp UF2 đã dùng và tham số đã bật sáng đèn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;25&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Bài đạt khi cửa sổ REPL kết nối đúng vào Pico (không phải Python của laptop) và bạn quan sát trực tiếp nhịp nháy 2 Hz.&lt;/p&gt;

&lt;p&gt;Các lỗi thường gặp: thả nút BOOTSEL trước khi cắm cáp nên mạch chạy thẳng vào firmware cũ; dùng cáp chỉ sạc nên không hiện cả ổ đĩa lẫn cổng serial; chọn sai họ UF2 (nạp ảnh RP2040 lên Pico 2 hoặc nạp nhầm bản W/không W); hoặc quên chuyển trình thông dịch trong Thonny nên báo không tìm thấy mô-đun &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;machine&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An toàn: mọi chân GP chỉ chịu tối đa 3,3 V. Không nối pin, động cơ hay cảm biến 5 V trong bài này; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; là ngõ ra ổn áp và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; không chịu được điện áp pin 2S.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [Raspberry Pi PICO&lt;/td&gt;
          &lt;td&gt;Starting With MicroPython + Examples&lt;/td&gt;
          &lt;td&gt;I2C OLED, ADC, PWM](https://www.youtube.com/watch?v=zlKJ5hvfs6s) — Electronoobs, tiếng Anh. Hướng dẫn dùng MicroPython trên Pico qua REPL kèm ví dụ PWM và ADC (phần I2C để dành cho chương 04).&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=L03jT5slWnw&quot;&gt;Raspberry Pi Pico - A Beginners Guide&lt;/a&gt; — Tinkernut, tiếng Anh. Minh họa thao tác nạp firmware và lần cắm USB đầu tiên trước khi lập trình.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nêu bốn thao tác để vào chế độ BOOTSEL và tên ổ đĩa bootloader xuất hiện trên Pico (RP2040) cũng như Pico 2 (RP2350). Vì sao chỉ dựa vào tên ổ đĩa vẫn chưa đủ để chọn đúng tệp UF2 cho mọi phiên bản?&lt;/li&gt;
  &lt;li&gt;Đoạn mã dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(25)&lt;/code&gt; trên Pico W nhưng đèn tích hợp không sáng. Hãy viết lại dòng khởi tạo đúng và cho biết GPIO 25 trên Pico W thực chất là chân gì.&lt;/li&gt;
  &lt;li&gt;Bạn lỡ chép tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI_PICO-....uf2&lt;/code&gt; lên Pico 2, ổ đĩa biến mất nhưng không thấy cổng REPL. Cần thay bằng tệp nào và tệp cũ thuộc dòng chip nào?&lt;/li&gt;
  &lt;li&gt;Thonny báo không tìm thấy mô-đun &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;machine&lt;/code&gt;. Nguyên nhân cấu hình nằm ở đâu?&lt;/li&gt;
  &lt;li&gt;Vì sao bài này cấm đưa tín hiệu 5 V vào chân GP, kể cả chỉ là một đường dây Echo từ cảm biến?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Đáp án&lt;/summary&gt;

1. Bốn bước: rút hẳn cáp, nhấn giữ BOOTSEL, cắm cáp USB khi vẫn đang giữ nút, sau đó thả BOOTSEL. Ổ đĩa hiện tên `RPI-RP2` trên Pico/Pico W (RP2040) và `RP2350` trên Pico 2/Pico 2 W (RP2350). Dù phân biệt được hai thế hệ chip, tên ổ vẫn giống nhau giữa bản không W và bản có W trong cùng thế hệ, nên vẫn phải đối chiếu tên tệp UF2 với đúng nhãn trên mạch.
2. Đổi thành `led = Pin(&quot;LED&quot;, Pin.OUT)`. Trên Pico W và Pico 2 W, LED tích hợp nối với chip không dây, còn GPIO 25 là một chân GPIO thông thường.
3. Đưa bo mạch vào lại chế độ BOOTSEL và chép đúng tệp UF2 tải từ trang `RPI_PICO2` (hoặc `RPI_PICO2_W` nếu là bản W). Tệp `RPI_PICO-...` là firmware dành cho chip RP2040 của Pico bản gốc, không chạy được trên RP2350 của Pico 2.
4. Trình thông dịch của Thonny đang trỏ vào Python cục bộ trên laptop hoặc chọn sai cổng kết nối. Cần chuyển sang **MicroPython (Raspberry Pi Pico)** hoặc dùng `mpremote` trỏ đúng cổng của Pico.
5. Chân GP của Pico dùng mức logic 3,3 V; tín hiệu 5 V vượt ngưỡng chịu đựng và có thể làm hỏng chân chip. Nguồn động cơ và pin 2S cũng phải đi qua mạch cầu H ở bài sau, không nối trực tiếp vào bo mạch.

&lt;/details&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Chỉ cần mua một bo mạch họ Pico và một cáp micro-USB truyền dữ liệu nếu bạn chưa có. Giá dưới đây được ghi nhận công khai trên Hshop ngày 23 tháng 9 năm 2026: trang tìm kiếm &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;raspberry pi pico&lt;/a&gt; hiển thị Pico 2 giá 195 000 ₫ và Pico 2 W giá 275 000 ₫. Trang sản phẩm chi tiết: &lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-rp2350&quot;&gt;Pico 2, RP2350&lt;/a&gt; và &lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-w-rp2350&quot;&gt;Pico 2 W&lt;/a&gt; (Pico 2 không tích hợp Wi-Fi, còn Pico 2 W có sẵn). Cáp dữ liệu tương thích: &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Nếu tham khảo thêm trên Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=raspberry%20pi%20pico%202&quot;&gt;Raspberry Pi Pico 2&lt;/a&gt;. Nếu vẫn đang cân nhắc phương án ESP32 mặc định, bo mạch hoàn chỉnh gồm &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;NodeMCU-32S giá 190 000 ₫&lt;/a&gt; và &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32 giá 265 000 ₫&lt;/a&gt;, tránh mua module trần &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;ESP32-S3-WROOM-1 giá 135 000 ₫&lt;/a&gt;. Có thể tham khảo thêm &lt;a href=&quot;https://www.thegioiic.com/esp32-devkitc-32u-module-wifi-bluetooth-2-4ghz&quot;&gt;DevKitC-32U của Thế Giới IC&lt;/a&gt;; &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; là cửa hàng linh kiện rời. Danh sách linh kiện đầy đủ nằm ở &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;chương 00&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/micropython.html&quot;&gt;MicroPython trên Raspberry Pi Pico&lt;/a&gt; — hướng dẫn cài đặt chính thức từ Raspberry Pi.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://datasheets.raspberrypi.com/pico/pico-datasheet.pdf&quot;&gt;Datasheet Pico&lt;/a&gt; — thông tin về GPIO 25, các đường nguồn VBUS, VSYS, 3V3 và điện áp cực đại cho phép.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micropython.org/download/&quot;&gt;Trang tải MicroPython&lt;/a&gt; — danh mục phân tách rõ tệp UF2 cho RP2040 và RP2350; nên tải trực tiếp từ trang chính thức theo đúng bo mạch thay vì lấy tệp chia sẻ lại trên diễn đàn.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Arduino IDE và PlatformIO với ESP32</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_02_esp32_platformio/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_02_esp32_platformio</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Trong bài này, bạn sẽ cài đặt một bộ công cụ (toolchain) duy nhất cho ESP32 và tạm bỏ qua lựa chọn còn lại cho đến khi nạp thành công chương trình nháy LED ổn định. Với Arduino IDE 2, bạn thêm đường dẫn quản lý bo mạch của Espressif, cài gói &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32&lt;/code&gt; và chọn đúng mục ESP32 Dev Module hoặc NodeMCU-32S khớp với nhãn in trên mạch. Với PlatformIO, bạn cấu hình tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; với bo mạch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt; và tốc độ monitor 115200 baud. Bạn dùng macro &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; nếu gói phần mềm có định nghĩa sẵn, và chỉ dùng GPIO 2 sau khi đã xác minh bo mạch thực tế mắc đèn ở chân này. Bạn cũng nắm được cách nhận diện các lỗi phổ biến: không hiện cổng kết nối, cáp chỉ sạc, thiếu driver CH340 và sụt áp (brownout) do còn nối nguồn động cơ trong lúc nạp.&lt;/p&gt;

&lt;h2 id=&quot;cần-gì-trước-khi-học&quot;&gt;Cần gì trước khi học&lt;/h2&gt;

&lt;p&gt;Bạn cần đã chốt bo mạch ESP32 làm vi điều khiển chính từ bài trước. Nếu bạn chọn dòng Pico, hãy đọc nhanh bài này để nắm tổng quan rồi chuyển sang bài về Pico. Cài đặt cả hai bộ công cụ trong cùng một buổi rất dễ dẫn đến nhầm lẫn cấu hình bo mạch trong dự án. Về phần cứng, bạn cần một laptop và một sợi cáp USB có hỗ trợ truyền dữ liệu. Danh mục linh kiện đầy đủ nằm ở bài mua sắm chương 00 (&lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;Hóa đơn linh kiện và cách mua&lt;/a&gt;).&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Arduino IDE rất tuyệt vời để nhấp nháy đèn LED đầu tiên trong 5 phút, nhưng khi dự án robot của bạn lớn lên thành 8 file mã nguồn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;motors.cpp&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;encoders.cpp&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pid.cpp&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imu.cpp&lt;/code&gt;…) với 4 thư viện ngoài, việc quản lý thư viện toàn cục trong Arduino IDE rất dễ biến thành cơn ác mộng “hôm qua chạy, hôm nay báo lỗi biên dịch”! &lt;strong&gt;PlatformIO trên VS Code&lt;/strong&gt; giải quyết dứt điểm điều đó bằng cấu hình khai báo theo từng dự án (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt;), tự động gợi ý mã thông minh và quản lý toolchain ESP32 chuẩn công nghiệp.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kiến-trúc-biên-dịch--nạp-firmware-esp32-với-platformio&quot;&gt;Kiến thức cần nhớ: Kiến trúc Biên dịch &amp;amp; Nạp Firmware ESP32 với PlatformIO&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cấu hình Khai báo (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt;):&lt;/strong&gt; Một tệp duy nhất khóa chặt bo mạch (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board = esp32dev&lt;/code&gt;), framework (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arduino&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;espidf&lt;/code&gt;), tốc độ nạp (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;upload_speed&lt;/code&gt;), tốc độ cổng Serial (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;monitor_speed = 115200&lt;/code&gt;) và danh sách thư viện kèm đúng số phiên bản (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lib_deps&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bản đồ Bộ nhớ ESP32 (Flash vs. SRAM):&lt;/strong&gt; Mã chương trình nằm trong bộ nhớ &lt;strong&gt;Flash&lt;/strong&gt; (thường 4 MB — mất điện không mất dữ liệu); biến toàn cục, ngăn xếp (stack) và bộ đệm chạy nằm trong &lt;strong&gt;SRAM&lt;/strong&gt; (520 KB — cực nhanh nhưng giới hạn!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kênh Nạp &amp;amp; Chẩn đoán USB-UART (CP2102 / CH340):&lt;/strong&gt; Cầu nối USB-UART biến cổng USB máy tính thành cổng COM ảo; lỗi nạp thường đến từ thiếu driver cầu nối, cáp USB chỉ có dây sạc (không có dây dữ liệu D+/D-), hoặc quên nhấn giữ nút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BOOT&lt;/code&gt; trên một số bo devkit đời cũ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-đọc-báo-cáo-bộ-nhớ-biên-dịch-platformio-của-một-firmware-robot-esp32&quot;&gt;Ví dụ làm từng bước: Đọc báo cáo bộ nhớ biên dịch PlatformIO của một firmware robot ESP32&lt;/h3&gt;

&lt;p&gt;Sau khi bấm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Build&lt;/code&gt;, PlatformIO in ra bảng thống kê bộ nhớ tương tự:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;RAM (DRAM khả dụng cho ứng dụng = 327.680 byte):&lt;/strong&gt; dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;48.200 byte&lt;/code&gt; → \(\dfrac{48{,}200}{327{,}680} = \mathbf{14.7\%}\) (còn dư &lt;strong&gt;279 KB&lt;/strong&gt; RAM!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Flash (phân vùng app mặc định = 1.310.720 byte trong 4 MB):&lt;/strong&gt; dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;820.400 byte&lt;/code&gt; → \(\dfrac{820{,}400}{1{,}310{,}720} = \mathbf{62.6\%}\) (khi bật Wi-Fi + BLE, dung lượng thư viện có thể vượt 1,3 MB → cần đổi bảng phân vùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board_build.partitions = huge_app.csv&lt;/code&gt; để mở rộng lên &lt;strong&gt;3 MB&lt;/strong&gt;!).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chỉ bằng 2 phép chia phần trăm này, bạn biết ngay khi nào cần mở rộng phân vùng Flash và đảm bảo không bao giờ để RAM chạm mức nguy hiểm (\(&amp;gt; 80\%\)) gây tràn stack ngẫu nhiên!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-4-bước-gỡ-lỗi-nạp-esp32-nhanh-trong-60-giây&quot;&gt;Giải thích: 4 bước gỡ lỗi nạp ESP32 nhanh trong 60 giây&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Kiểm tra cáp USB truyền dữ liệu:&lt;/strong&gt; Cáp sạc điện thoại giá rẻ thường cắt bỏ 2 dây dữ liệu — nếu cắm vào mà không xuất hiện cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COM&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; mới, hãy đổi ngay sang cáp có truyền dữ liệu!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Đóng các cửa sổ Serial Monitor khác đang chiếm cổng:&lt;/strong&gt; Một cổng COM chỉ cho phép &lt;strong&gt;1 chương trình&lt;/strong&gt; mở tại một thời điểm; nếu để mở màn hình Serial ở cửa sổ khác, lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Upload&lt;/code&gt; sẽ báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Permission denied&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Port busy&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Xử lý lỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Connecting........_____.....&lt;/code&gt;:&lt;/strong&gt; Giữ nút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BOOT&lt;/code&gt; trên ESP32, bấm nhả nút &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EN&lt;/code&gt; (Reset), rồi thả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BOOT&lt;/code&gt; ngay khi thấy dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Writing at 0x00010000...&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Đồng bộ tốc độ baud Serial (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200&lt;/code&gt;):&lt;/strong&gt; Nếu màn hình Serial in toàn ký tự rác kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;⸮⸮x⸮&lt;/code&gt;, hãy kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.begin(115200)&lt;/code&gt; trong code có khớp với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;monitor_speed = 115200&lt;/code&gt; trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; hay không!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Tại sao một dự án ESP32 dung lượng biên dịch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.45 MB&lt;/code&gt; lại báo lỗi &lt;em&gt;“Sketch too big”&lt;/em&gt; dù trên vỏ bo ghi rõ Flash &lt;strong&gt;4 MB&lt;/strong&gt;? Vì bảng phân vùng mặc định của ESP32 chia 4 MB thành 2 phân vùng OTA (mỗi phân vùng app chỉ &lt;strong&gt;1,25 MB&lt;/strong&gt;) cộng phân vùng lưu tệp SPIFFS — đổi sang phân vùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;huge_app.csv&lt;/code&gt; sẽ gộp lại thành &lt;strong&gt;3 MB&lt;/strong&gt; cho ứng dụng!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-với-capstone-a&quot;&gt;Vì sao bài này gắn với Capstone A&lt;/h2&gt;

&lt;p&gt;Chương trình điều khiển từ xa (teleop) cho xe vi sai ở chương 07 chạy trực tiếp trên chính vi điều khiển này, thực hiện xuất PWM cho hai bánh qua TB6612, tự ngắt sau khoảng 300 ms không có lệnh và yêu cầu nhấc bánh khi thử nghiệm lần đầu. Firmware đó chỉ đáng tin cậy khi bo mạch của bạn đã nhận được tệp nạp (binary), điều khiển chính xác một chân GPIO đã chọn và gửi được dữ liệu lên máy tính ở đúng tốc độ baud. Phần teleop qua Wi-Fi ở chương 08 cũng sử dụng chung quy trình nạp này. Trước khi bật kết nối không dây, Serial Monitor ở tốc độ 115200 baud là công cụ quan sát chính để kiểm tra hoạt động của hệ thống.&lt;/p&gt;

&lt;h2 id=&quot;chọn-một-bộ-công-cụ&quot;&gt;Chọn một bộ công cụ&lt;/h2&gt;

&lt;p&gt;Cả Arduino IDE 2 và PlatformIO đều sử dụng chung lõi (core) Arduino của Espressif. Hãy chọn công cụ đã được cài sẵn trên máy thực hành; tuần này chưa yêu cầu dùng ESP-IDF.&lt;/p&gt;

&lt;p&gt;Arduino IDE 2 phù hợp nếu bạn ưu tiên thao tác qua giao diện đồ họa. Sau khi cài IDE, mở Preferences (hoặc Settings) và dán đường dẫn sau vào mục Additional Boards Manager URLs:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;https://espressif.github.io/arduino-esp32/package_esp32_index.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tiếp theo, mở Boards Manager, tìm từ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32&lt;/code&gt; và cài gói &lt;strong&gt;esp32 by Espressif Systems&lt;/strong&gt;. Sau khi cài xong, mở danh sách bo mạch và chọn mục khớp với nhãn in trên mạch: các bản DevKit 30 chân hoặc 38 chân thông dụng chọn &lt;strong&gt;ESP32 Dev Module&lt;/strong&gt;, còn bo mạch có nhãn NodeMCU-32S chọn &lt;strong&gt;NodeMCU-32S&lt;/strong&gt; nếu có trong danh sách. Chọn đúng cổng serial vừa xuất hiện. Nếu lần nạp đầu báo lỗi đồng bộ (sync) hoặc quá thời gian chờ (timeout), hãy hạ Upload Speed xuống 115200 vì mức mặc định 921600 thường dễ rơi dữ liệu trên cáp dài hoặc một số bản mạch dùng chip CH340.&lt;/p&gt;

&lt;p&gt;PlatformIO phù hợp khi bạn muốn quản lý cấu hình bằng tệp văn bản để dễ theo dõi thay đổi (diff). Cài Visual Studio Code, thêm tiện ích PlatformIO IDE và chờ quá trình tải các gói khởi đầu hoàn tất. Tạo dự án mới và cấu hình tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; như sau:&lt;/p&gt;

&lt;div class=&quot;language-ini highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;[env:esp32dev]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;platform&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;espressif32&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;board&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;esp32dev&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;framework&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;arduino&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;monitor_speed&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;115200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Đích &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt; áp dụng cho các bo mạch ESP32 cổ điển gồm module họ WROOM và một chip USB-serial rời, tương thích với NodeMCU-32S và hầu hết DevKit V1 dùng trong môn học. Cấu hình này không đúng cho các bo mạch ESP32-S3 sử dụng USB tích hợp sẵn trong chip. Đây cũng là lý do bài trước khuyến nghị mua bo mạch ESP32 cổ điển, trừ khi bạn chủ động điều chỉnh lại cả tệp cấu hình lẫn sơ đồ chân.&lt;/p&gt;

&lt;h2 id=&quot;đèn-led-và-cổng-giao-tiếp&quot;&gt;Đèn LED và cổng giao tiếp&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; là macro được định nghĩa sẵn cho một số biến thể bo mạch. Nếu bo mạch bạn chọn có macro này, hãy dùng nó cho bài thử nháy LED đầu tiên; nếu không, dùng GPIO 2 — chân LED tích hợp phổ biến trên nhiều bản DevKit V1 và NodeMCU-32S. Tuy nhiên, đây không phải quy chuẩn bắt buộc cho mọi nhà sản xuất: một số mạch gắn đèn ở chân khác, thậm chí chỉ có đèn báo nguồn không thể điều khiển bằng phần mềm. Hãy đọc nhãn in trên mạch hoặc tra sơ đồ của hãng, ghi số chân vào sổ tay và đổi hằng số nếu GPIO 2 không sáng, thay vì vội cài lại IDE.&lt;/p&gt;

&lt;p&gt;Đoạn mã ngắn dưới đây dùng cho bài thực hành này:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#ifndef LED_BUILTIN
#define LED_BUILTIN 2  // hay gặp trên DevKit V1; đối chiếu pinout của bạn
#endif
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;high&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;low&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tốc độ trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.begin(115200)&lt;/code&gt; phải khớp với tốc độ trên Serial Monitor. Khi lệch tốc độ baud, màn hình sẽ không im lặng mà hiển thị các ký tự lỗi (rác). Chương trình này tạm dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; để dễ quan sát ở lần chạy đầu tiên; sang bài nhịp tim, chúng ta sẽ bỏ cấu trúc này vì lệnh dừng an toàn không thể bị chặn bên trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; khi cần kiểm tra ngưỡng 300 ms.&lt;/p&gt;

&lt;p&gt;Con chip nằm sát cổng USB không phải ESP32 mà là chip chuyển đổi USB-serial, thường là CH340 hoặc CP2102 trên các bo mạch cổ điển. CP2102 thường được hệ điều hành tự nhận diện. Với CH340 trên Windows, bạn thường phải cài thêm driver CH340 của hãng WCH để cổng COM mới xuất hiện trong Device Manager. Trên Linux, các chip này xuất hiện dưới dạng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; (hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ttyUSB1&lt;/code&gt; nếu cắm thêm thiết bị); trên Windows là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COM3&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COM4&lt;/code&gt; hoặc một tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COMx&lt;/code&gt; mới. Trên Linux, tài khoản người dùng cần thuộc nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;, và thay đổi này chỉ có hiệu lực sau khi đăng xuất rồi đăng nhập lại.&lt;/p&gt;

&lt;p&gt;Cáp USB chỉ sạc không thể dùng để kết nối máy tính: đèn báo nguồn trên mạch vẫn có thể sáng vì dây nguồn còn, nhưng không có dây dữ liệu. Hãy thay bằng cáp từng dùng để chép tệp dữ liệu thành công.&lt;/p&gt;

&lt;p&gt;Để nạp chương trình, ESP32 cổ điển cần vào chế độ bootloader serial bằng cách kéo chân GPIO 0 xuống mức thấp tại thời điểm reset. Nút BOOT (đôi khi ghi IO0) điều khiển chân này, còn nút EN, RST hoặc RESET dùng để khởi động lại chip. Phần lớn bo mạch phát triển có mạch tự động kích hoạt chế độ nạp qua tín hiệu DTR và RTS của chip USB-serial. Nếu bo mạch không tự vào chế độ nạp và quá trình nạp bị timeout, hãy giữ nút BOOT, bấm nạp và thả BOOT khi màn hình log báo đang kết nối (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;connecting&lt;/code&gt;). Nếu thao tác chưa ổn định, hãy giữ BOOT, nhấn nhả EN, thả BOOT rồi tiến hành nạp ngay sau đó.&lt;/p&gt;

&lt;h2 id=&quot;các-nguyên-nhân-thường-gặp-khi-nạp-lỗi&quot;&gt;Các nguyên nhân thường gặp khi nạp lỗi&lt;/h2&gt;

&lt;p&gt;Ba nhóm lỗi sau chiếm phần lớn các trường hợp tưởng nhầm là “bo mạch hỏng” trong bài thực hành này:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Chọn sai bo mạch:&lt;/strong&gt; Chọn nhầm mục ESP32-S3, C3 hoặc một biến thể khác như “ESP32 Pico Kit” không khớp với NodeMCU-32S sẽ khiến quá trình nạp báo lỗi, hoặc nạp xong nhưng chân điều khiển không đúng như mong đợi. Hãy chọn đúng ESP32 Dev Module, NodeMCU-32S hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board = esp32dev&lt;/code&gt; theo nhãn mạch.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Không hiện cổng kết nối:&lt;/strong&gt; Kiểm tra cáp trước tiên, sau đó kiểm tra driver CH340 trên Windows hoặc quyền nhóm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; trên Linux. Cũng cần tránh chọn nhầm cổng còn sót lại từ thiết bị khác (chẳng hạn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyACM0&lt;/code&gt; của Pico trong khi ESP32 là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Chip tự reset trong lúc nạp:&lt;/strong&gt; Nếu động cơ hoặc chân VM của mạch cầu H vẫn đang nối vào chân 5 V của bo mạch, dòng tải đột ngột sẽ làm sụt nguồn và ngắt quá trình nạp giữa chừng. Hãy rút hoàn toàn dây VM cùng mọi dây động cơ, chỉ giữ lại cáp USB rồi nạp lại. Trong lúc chạy chương trình (bài nhịp tim), hiện tượng này sẽ làm chữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt; in lặp lại liên tục; còn trong lúc nạp, nó biểu hiện thành lỗi timeout.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lưu ý thêm: GPIO 1 và GPIO 3 là hai chân UART mà chip USB-serial đang sử dụng. Điều khiển nháy LED trên hai chân này sẽ gây nhiễu dữ liệu hiển thị trên Serial Monitor, vì vậy không sử dụng chúng trong bài này.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đã-làm&quot;&gt;Ví dụ đã làm&lt;/h2&gt;

&lt;p&gt;Bình dùng bo mạch NodeMCU-32S (chip CH340) mua từ Hshop trên laptop Windows. Lần cắm đầu tiên, đèn nguồn sáng nhưng Device Manager không xuất hiện cổng mới. Sau khi cài driver CH340, cổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COM5&lt;/code&gt; hiện ra. Bình dán đoạn mã ở trên vào Arduino IDE 2, chọn bo mạch NodeMCU-32S và tốc độ nạp 115200, nhưng lần nạp đầu vẫn thất bại do một mạch TB6612 từ ca thực hành trước còn dây VM nối vào chân 5 V của bo mạch. Bình rút dây VM, giữ nút BOOT và nạp lại thành công. Serial Monitor ở 115200 baud in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt;, sau đó luân phiên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;high&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;low&lt;/code&gt; đồng bộ với đèn LED. Vì GPIO 2 khớp với nhãn trên mạch này, nhánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#ifndef&lt;/code&gt; điều khiển đúng đèn tích hợp. Khi thử đổi tốc độ monitor xuống 9600, màn hình hiện toàn ký tự rác trong khi LED vẫn nháy đều — giúp phân biệt rõ lỗi tốc độ baud với lỗi hỏng chip.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chọn Arduino IDE 2 hoặc PlatformIO và chỉ cài đặt đúng một công cụ theo đường dẫn hoặc tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; ở trên.&lt;/li&gt;
  &lt;li&gt;Cắm cáp dữ liệu và xác nhận tên cổng mới xuất hiện (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; trên Linux hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COMx&lt;/code&gt; trên Windows), ghi lại tên cổng.&lt;/li&gt;
  &lt;li&gt;Chọn bo mạch ESP32 Dev Module, NodeMCU-32S hoặc cấu hình &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board = esp32dev&lt;/code&gt;; đặt tốc độ monitor ở 115200 baud.&lt;/li&gt;
  &lt;li&gt;Dán đoạn mã mẫu. Nếu LED theo nhãn mạch không nằm ở GPIO 2 và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; không sáng, sửa lại số chân và ghi chú vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Tiến hành nạp chương trình. Nếu quá trình nạp bị timeout, giữ nút BOOT và thử lại; nếu còn nối động cơ hoặc dây VM, rút ra hoàn toàn rồi mới nạp lại.&lt;/li&gt;
  &lt;li&gt;Mở Serial Monitor ở 115200 baud. Quan sát một dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt; tiếp theo là các dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;high&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;low&lt;/code&gt; đồng bộ với nhịp nháy của LED tích hợp. Chép tám dòng log vào sổ tay kèm tên cổng và loại chip USB-serial (CH340 hoặc CP2102).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Bài thực hành đạt khi đèn LED tích hợp nháy theo chương trình bạn nạp và có đoạn log dán kèm; đèn báo nguồn sáng sẵn khi cắm USB không được tính.&lt;/p&gt;

&lt;p&gt;An toàn: chỉ cấp nguồn qua cổng USB; không nối pin, động cơ hay dây VM. Chân 5 V trên bo mạch không dùng để cấp nguồn cho động cơ, và dòng kẹt bánh trong lúc nạp chính là nguyên nhân gây sụt áp cần tránh.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=3ATDCi4ApY4&quot;&gt;Get started with PlatformIO and the ESP 32&lt;/a&gt; — John Green Dev, tiếng Anh. Hướng dẫn tạo dự án PlatformIO, chọn bo mạch ESP32 và nạp chương trình — đúng bộ công cụ trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=sJtJJU3c3xE&quot;&gt;Lập trình ESP32 - Bài 1 - Cách nạp chương trình cho ESP32 bằng Arduino IDE - Tự học lập trình IOT&lt;/a&gt; — Điện thông minh E-smart, tiếng Việt. Hướng dẫn nạp chương trình cho ESP32 bằng Arduino IDE bằng tiếng Việt để đối chiếu quy trình kiểm tra bo mạch hoạt động.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Viết lại bốn dòng cấu hình chính trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; theo yêu cầu của bài và ghi lại đường dẫn quản lý bo mạch ESP32 cho Arduino IDE.&lt;/li&gt;
  &lt;li&gt;Dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; nhưng đèn trên mạch không sáng, trong khi Serial Monitor vẫn in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;high&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;low&lt;/code&gt;. Bạn cần kiểm tra thông tin gì trên bo mạch, và vì sao “GPIO 2” chỉ là giả thuyết ban đầu chứ không phải mặc định cho mọi mạch?&lt;/li&gt;
  &lt;li&gt;Quá trình nạp chỉ thành công khi giữ nút BOOT. Nút này nối với chân nào, và nút EN đóng vai trò gì trong thao tác nạp thủ công?&lt;/li&gt;
  &lt;li&gt;Serial Monitor in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt; lặp lại liên tục mà không chạy ổn định trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt;, trong khi mạch cầu H vẫn nối dây VM vào chân 5 V. Bạn cần tháo dây nào và hiện tượng điện nào đang khiến chip reset?&lt;/li&gt;
  &lt;li&gt;Đèn LED vẫn nháy bình thường nhưng Serial Monitor toàn ký tự rác. Hai thông số baud nào đang bị lệch nhau, và vì sao có thể kết luận bản thân chip vẫn hoạt động bình thường?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Đáp án&lt;/summary&gt;

1. Bốn khóa cấu hình: `platform = espressif32`, `board = esp32dev`, `framework = arduino`, `monitor_speed = 115200`. Đường dẫn cho Arduino IDE là `https://espressif.github.io/arduino-esp32/package_esp32_index.json`, sau đó cài gói **esp32 by Espressif Systems**.
2. Cần tra cứu chân GPIO nối với LED tích hợp trên nhãn mạch hoặc sơ đồ chân (pinout) của đúng bo mạch đang dùng. GPIO 2 phổ biến trên nhiều bản DevKit V1 và NodeMCU-32S nhưng không giống nhau trên mọi biến thể. Hãy đổi hằng số sang chân thực tế và ghi chú lại.
3. Nút BOOT kéo chân GPIO 0 xuống mức thấp để chip vào chế độ bootloader serial khi reset. Thao tác thủ công là giữ BOOT, nhấn nhả EN (reset), thả BOOT rồi nạp trong lúc chip đang chờ. Các bo mạch có mạch tự động reset (auto-reset) hoạt động tốt sẽ tự thực hiện bước này.
4. Cần rút dây VM và toàn bộ dây động cơ. Dòng tải làm sụt đường nguồn (brownout), khiến chip reset liên tục trước khi chạy ổn định hoặc nạp xong.
5. Tốc độ baud trong chương trình là 115200, còn tốc độ trên Serial Monitor đang đặt ở giá trị khác (thường là 9600). Việc đèn LED nháy đều chứng tỏ chương trình đang chạy bình thường; chỉ cần chỉnh lại tốc độ trên monitor mà không cần cài lại bộ công cụ.

&lt;/details&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Nếu bo mạch và cáp dữ liệu từ bài trước đã kết nối ổn định với máy tính, bạn không cần mua thêm gì. Nếu cổng kết nối không xuất hiện, hãy ưu tiên thay cáp trước khi nghĩ đến việc đổi bo mạch. Mức giá dưới đây được ghi nhận từ trang công khai của Hshop vào ngày 23 tháng 9 năm 2026.&lt;/p&gt;

&lt;p&gt;Bo mạch mặc định: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;ESP32 NodeMCU-32S CH340, 190 000 ₫&lt;/a&gt;. Lựa chọn bo mạch hoàn chỉnh khác: &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32, 265 000 ₫&lt;/a&gt;. Lưu ý &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;ESP32-S3-WROOM-1 giá 135 000 ₫&lt;/a&gt; là module trần, không thể tự hiển thị thành &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COMx&lt;/code&gt;. Cáp dữ liệu: &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Nếu so sánh thêm ở các cửa hàng khác: &lt;a href=&quot;https://shopee.vn/search?keyword=esp32%20devkit%20ch340&quot;&gt;Shopee ESP32 DevKit CH340&lt;/a&gt;. &lt;a href=&quot;https://www.thegioiic.com/esp32-devkitc-32u-module-wifi-bluetooth-2-4ghz&quot;&gt;Trang DevKitC-32U của Thế Giới IC&lt;/a&gt; là mẫu bo mạch phát triển; cần xác nhận có cổng USB và kiểm tra chuẩn đầu nối anten U.FL. &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; là cửa hàng linh kiện, không nên mua module trần tại đây để dùng làm bộ khởi đầu cho bài thực hành này. Các linh kiện còn lại của robot nằm ở &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;chương 00&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.platformio.org/en/latest/boards/espressif32/esp32dev.html&quot;&gt;Trang board &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt; của PlatformIO&lt;/a&gt; — tài liệu về mã bo mạch dùng trong tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; của bài này.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html&quot;&gt;Cài core Arduino cho ESP32&lt;/a&gt; — đường dẫn quản lý bo mạch và các bước cài đặt chính thức từ Espressif.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/en/support/download/documents&quot;&gt;Trang tải tài liệu Espressif&lt;/a&gt; — datasheet và tài liệu thiết kế để tra cứu khi chân LED trên mạch không phải GPIO 2.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chọn vi điều khiển cho robot nhỏ</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_01_mcu_landscape/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter02/02_01_mcu_landscape</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài này, bạn có thể so sánh ba lựa chọn thực tế cho một robot vi sai cỡ nhỏ: bo mạch ESP32 có cổng USB, Raspberry Pi Pico (hoặc Pico 2), và bo mạch họ Arduino Uno. Bạn nắm rõ mức logic, khả năng kết nối không dây tích hợp và ngôn ngữ lập trình buổi đầu của từng loại. Khi cần điều khiển xe bằng điện thoại về sau, bạn biết chọn ESP32 NodeMCU-32S, đồng thời phân biệt được module trần với các bản Pico không có Wi-Fi. Bạn cũng lập được trình tự khởi chạy bo mạch từng bước để không nhầm lỗi sụt nguồn với lỗi ngẫu nhiên trong chương trình, biết tính ngưỡng thực hành cho mức cao 3,3 V và loại bỏ tín hiệu 5 V khỏi các chân GPIO.&lt;/p&gt;

&lt;h2 id=&quot;cần-gì-trước-khi-học&quot;&gt;Cần gì trước khi học&lt;/h2&gt;

&lt;p&gt;Bạn chỉ cần biết chỉnh đồng hồ vạn năng sang thang đo điện áp một chiều và hiểu GPIO là chân mà vi điều khiển có thể xuất hoặc đọc tín hiệu. Kiến thức về điện trở nối tiếp trước LED ở chương 01 là đủ; bạn chưa cần kinh nghiệm nạp firmware. Danh sách linh kiện đầy đủ nằm ở bài mua sắm chương 00 (&lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;Hóa đơn linh kiện và cách mua&lt;/a&gt;). Bài này chỉ tập trung chọn bộ điều khiển trung tâm và cáp dữ liệu để máy tính nhận diện bo mạch.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Bước vào thế giới vi điều khiển lần đầu, bạn dễ choáng ngợp trước hàng chục cái tên: Arduino Uno, ESP32, Raspberry Pi Pico, STM32, Teensy… Mỗi bo mạch lại có điện áp khác nhau, số lõi CPU khác nhau và hệ sinh thái lập trình khác nhau. Chọn sai vi điều khiển không chỉ làm bạn tốn tiền mua lại — nó còn khiến bạn kẹt cứng khi cần nối Wi-Fi truyền dữ liệu telemetry, đọc đồng thời hai encoder tốc độ cao hay giao tiếp với cảm biến 3,3 V!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-ba-trục-kỹ-thuật-để-chọn-vi-điều-khiển-mcu-cho-robot&quot;&gt;Kiến thức cần nhớ: Ba Trục Kỹ thuật để Chọn Vi điều khiển (MCU) cho Robot&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Năng lực Tính toán &amp;amp; Ngoại vi Thời gian thực:&lt;/strong&gt; Một xe robot tự hành cần ít nhất 2 kênh PWM độc lập cho động cơ, 4 chân ngắt ngoài (hoặc bộ đếm phần cứng PCNT/PIO) cho 2 encoder, 1 bus I2C cho IMU và 1 cổng UART debug — ESP32 (2 lõi 240 MHz, Wi-Fi/BLE) và Raspberry Pi Pico (2 lõi ARM 133 MHz, bộ máy trạng thái PIO độc đáo) đều vượt xa Arduino Uno 8-bit 16 MHz!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mức Logic (3,3 V vs. 5 V) và Biên độ Nhiễu (Noise Margin):&lt;/strong&gt; Toàn bộ thế hệ cảm biến hiện đại (IMU MPU6050/ICM-20948, ToF VL53L0X, LiDAR) đều dùng &lt;strong&gt;3,3 V&lt;/strong&gt;. Chọn MCU 3,3 V (ESP32 / Pico) giúp nối trực tiếp cảm biến mà không cần dàn mạch chuyển mức cồng kềnh như trên Arduino Uno 5 V.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hệ sinh thái Công cụ &amp;amp; Cộng đồng:&lt;/strong&gt; ESP32 mạnh về C++/PlatformIO + kết nối không dây; Pico cực thân thiện với MicroPython (chạy thử ngay qua REPL) và C/C++ SDK.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-kiểm-tra-tương-thích-mức-logic-noise-margin-giữa-bo-33-v-và-bo-5-v&quot;&gt;Ví dụ làm từng bước: Kiểm tra tương thích mức logic (Noise Margin) giữa bo 3,3 V và bo 5 V&lt;/h3&gt;

&lt;p&gt;So sánh ngưỡng nhận diện mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; (\(V_{IH}\)) và giới hạn an toàn tối đa từ datasheet của &lt;strong&gt;ESP32 (3,3 V)&lt;/strong&gt; và &lt;strong&gt;Arduino Uno (5 V)&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Chiều truyền tín hiệu&lt;/th&gt;
      &lt;th&gt;Điện áp phát ra (\(V_{OH}\))&lt;/th&gt;
      &lt;th&gt;Ngưỡng nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; (\(V_{IH}\))&lt;/th&gt;
      &lt;th&gt;Trần an toàn (\(V_{\max}\))&lt;/th&gt;
      &lt;th&gt;Kết luận tương thích&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;ESP32 (3,3 V) → Arduino Uno (5 V)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(\approx 3.0\text{–}3.3\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(0.6 \times 5.0 = \mathbf{3.0\text{ V}}\) (tối thiểu; nhiều IC 5V cần \(3.5\text{ V}\))&lt;/td&gt;
      &lt;td&gt;\(5.5\text{ V}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Rủi ro biên hẹp&lt;/strong&gt; — dễ lỗi khi pin sụt áp nhẹ!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Arduino Uno (5 V) → ESP32 (3,3 V)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(\approx 4.8\text{–}5.0\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(0.75 \times 3.3 = 2.48\text{ V}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(3.6\text{ V}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;NGUY HIỂM!&lt;/strong&gt; Vượt trần \(1.4\text{ V}\) → hỏng chân ESP32!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;ESP32 (3,3 V) ↔ Cảm biến IMU (3,3 V)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(\approx 3.3\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(2.31\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(3.6\text{ V}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;An toàn &amp;amp; biên nhiễu rộng \(+1.0\text{ V}\)!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;giải-thích-4-bước-chọn-đúng-bo-mạch-mcu-ngay-từ-tuần-1&quot;&gt;Giải thích: 4 bước chọn đúng bo mạch MCU ngay từ tuần 1&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Ưu tiên chuẩn logic 3,3 V cho robot hiện đại:&lt;/strong&gt; ESP32 và Pico giao tiếp trực tiếp với gần như toàn bộ cảm biến số hiện đại (I2C/SPI) mà không lo cháy chân.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Đếm đủ số chân ngoại vi &lt;em&gt;trước&lt;/em&gt; khi chốt bo:&lt;/strong&gt; Liệt kê ra giấy: 4 chân driver + 2 chân PWM + 4 chân encoder + 2 chân I2C + 2 chân UART = &lt;strong&gt;14 chân GPIO an toàn&lt;/strong&gt; (đã trừ các chân strapping và input-only).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Chọn ESP32 nếu cần Wi-Fi/telemetry không dây, chọn Pico nếu thích lập trình tương tác MicroPython:&lt;/strong&gt; Cả hai đều đủ mạnh chạy vòng lặp PID 100 Hz dư sức.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Chạy bài kiểm tra “khởi chạy trần” (bare-board bring-up):&lt;/strong&gt; Cắm bo mới vào USB (chưa nối bất kỳ dây nào), nạp chương trình nhấp LED và đọc dòng chào qua Serial để xác nhận bo sống 100%.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Một cảm biến khoảng cách laser VL53L0X dùng nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2.8 V – 3.3 V&lt;/code&gt; và bus I2C 3,3 V. Nếu nối nó với &lt;strong&gt;ESP32&lt;/strong&gt; (3,3 V) thì cần mạch chuyển mức logic không? &lt;strong&gt;Không cần!&lt;/strong&gt; Cả hai cùng chung miền logic 3,3 V (chỉ cần điện trở kéo lên I2C nối lên 3,3 V).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-với-capstone-a&quot;&gt;Vì sao bài này gắn với Capstone A&lt;/h2&gt;

&lt;p&gt;Capstone A ở chương 07 là xe vi sai gồm hai bánh dẫn động, một bánh đa hướng và firmware chuyển đổi lệnh điều khiển thành tín hiệu PWM cho mạch cầu H TB6612 ở các bài sau. Luồng lệnh phải có cơ chế an toàn: nếu sau khoảng 300 ms không nhận được lệnh mới, hệ số chu kỳ (duty) của cả hai bánh phải đưa về 0; ở lần đầu cho phép bánh quay, bánh xe phải được nhấc khỏi mặt bàn. Vòng lặp điều khiển đó cần chạy trên một vi điều khiển đã được kiểm chứng. Đến chương 08, bài học về Wi-Fi và MQTT sẽ chuyển việc điều khiển từ xa (teleop) từ cáp USB sang điện thoại hoặc laptop. Vì ESP32 tích hợp sẵn Wi-Fi 2,4 GHz và BLE ngay trên module, đây là lựa chọn mặc định của môn học. Pico không có hậu tố W vẫn chạy tốt firmware chương 07 qua USB và rất tiện để thử nghiệm nhanh, nhưng sẽ không dùng được cho phần kết nối không dây ở chương 08 nếu bạn không đổi sang bản có W hoặc chuyển sang ESP32. Quyết định bạn ghi lại hôm nay sẽ định hình sơ đồ chân, bộ công cụ lập trình (toolchain) và phương án kết nối không dây cho cả học kỳ.&lt;/p&gt;

&lt;h2 id=&quot;ba-bo-mạch-ba-vai-trò-khác-nhau&quot;&gt;Ba bo mạch, ba vai trò khác nhau&lt;/h2&gt;

&lt;p&gt;Trong môn học này, ESP32 được dùng dưới dạng module lõi kép kiến trúc Xtensa gắn trên bo mạch phát triển (dev board), có sẵn Wi-Fi và BLE bên trong vỏ kim loại chống nhiễu, sử dụng mức logic GPIO 3,3 V. Thứ bạn cần mua không phải là module trần chỉ có vỏ kim loại. Một bo mạch dùng được ngay phải có cổng USB, mạch ổn áp 3,3 V, chip chuyển đổi USB-serial, nút BOOT và hàng chân cắm (header). Chip USB-serial phổ biến nhất là CH340 (rất thông dụng tại Việt Nam) hoặc CP2102 (thường được hệ điều hành tự nhận mà không cần cài thêm driver). Module trần không có các thành phần này. NodeMCU-32S dùng CH340 là cấu hình mặc định; Vietduino ESP32 cũng là một bo mạch hoàn chỉnh khác nếu bạn chuộng cách bố trí nhãn in trên mạch đó. Ngược lại, ESP32-S3-WROOM-1 là module trần, nên dù giá thấp hơn vẫn không đưa vào danh sách khởi đầu.&lt;/p&gt;

&lt;p&gt;Raspberry Pi Pico hoặc Pico 2 là lựa chọn phù hợp để thử nghiệm nhanh. Pico bản gốc dùng chip RP2040, còn Pico 2 dùng chip RP2350. Cả hai đều dùng GPIO 3,3 V và hỗ trợ môi trường tương tác REPL của MicroPython. Tuy nhiên, Pico bản gốc và Pico 2 không có chữ W đều không tích hợp sẵn Wi-Fi; chỉ bản Pico W và Pico 2 W mới có chip không dây. Bạn cần ghi chính xác tên in trên bo mạch vào sổ tay vì chân LED tích hợp và tệp firmware thay đổi tùy theo hậu tố này. Hãy chọn dòng Pico khi bạn muốn học qua REPL trước và sẵn sàng chờ đổi sang bản W hoặc chuyển sang ESP32 khi đến bài Wi-Fi ở chương 08.&lt;/p&gt;

&lt;p&gt;Các bo mạch họ Arduino Uno (dùng ATmega328P và các bản tương thích) hoạt động ở mức logic 5 V. Bạn cần nhận diện rõ dòng này để tránh nhầm lẫn với bộ điều khiển chính của robot. Uno không có Wi-Fi, và tín hiệu mức cao 5 V tuyệt đối không được nối trực tiếp vào chân 3,3 V. Tín hiệu Echo 5 V của cảm biến HC-SR04 hoặc một chân xuất tín hiệu từ Uno có thể làm hỏng chân GPIO của ESP32 hoặc Pico. Nếu đã có sẵn Uno, hãy giữ nó làm bo mạch tham chiếu 5 V trên bàn thí nghiệm, không dùng làm bộ điều khiển chính cho Capstone A.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/pcb_mcu_anatomy.png&quot; alt=&quot;Giải phẫu bo mạch: USB, ổn áp, hàng chân, và module&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Cổng USB giúp máy tính giao tiếp với bo mạch, mạch ổn áp tạo ra nguồn 3,3 V, còn hàng chân header là nơi duy nhất để đấu dây. Module trần chỉ gồm phần vỏ kim loại, thiếu toàn bộ các khối phụ trợ còn lại.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/ESP32_on_Lolin32_Lite_clone_board_cropped.jpg&quot; alt=&quot;Module ESP32 trên một bo mạch clone nhỏ&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy đối chiếu trực tiếp vỏ kim loại, cổng USB và hàng chân trên bo mạch thực tế của bạn trước khi áp dụng sơ đồ chân vẽ cho một biến thể khác.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Raspberry_Pi_Pico.jpg&quot; alt=&quot;Raspberry Pi Pico, đường MicroPython&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Nút BOOTSEL, cổng micro-USB và các nhãn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt; là những mốc cần nhận diện trên dòng mạch này. Ảnh minh họa là Pico bản gốc: không có kết nối không dây, LED tích hợp nằm ở GPIO 25.&lt;/p&gt;

&lt;h2 id=&quot;mức-logic-được-xác-định-bằng-con-số&quot;&gt;Mức logic được xác định bằng con số&lt;/h2&gt;

&lt;p&gt;Với ngõ vào CMOS, mức logic cao được tính theo tỉ lệ với điện áp nguồn của chính vi mạch đó. Một quy tắc ước lượng thường dùng trong học tập (không thay thế thông số \(V_{IH}\) chính thức trong datasheet) là chỉ coi chân ở mức cao khi điện áp đạt tối thiểu khoảng \(0{,}7\) lần điện áp nguồn:&lt;/p&gt;

\[0{,}7 \times 3{,}3\,\mathrm{V} \approx 2{,}3\,\mathrm{V}.\]

&lt;p&gt;Trên ESP32 và Pico, ngưỡng ước lượng này xấp xỉ 2,3 V. Giá trị \(V_{IL}\) và \(V_{IH}\) chính xác nằm trong bảng đặc tính điện của datasheet và thay đổi theo \(V_{DD}\); bạn sẽ cần tra cứu khi thiết kế mạch chia áp sát ngưỡng. Trước khi đi sâu vào đó, quy tắc vận hành an toàn hơn cả là chỉ đưa tín hiệu tối đa 3,3 V vào các chân GPIO trong môn học này.&lt;/p&gt;

&lt;p&gt;Giới hạn chịu áp là lý do không nên đặt Uno làm việc trực tiếp cùng các bo mạch 3,3 V. Điện áp cực đại tuyệt đối cho chân GPIO 3,3 V chỉ nhỉnh hơn điện áp nguồn một chút, vào khoảng:&lt;/p&gt;

\[3{,}3\,\mathrm{V} + 0{,}3\,\mathrm{V} = 3{,}6\,\mathrm{V}.\]

&lt;p&gt;Mức 5 V vượt xa giới hạn này. Khi cấp 5 V vào chân 3,3 V, diode bảo vệ bên trong sẽ dẫn dòng mạnh, làm nóng chân và có thể gây hỏng chip ngay lần cắm đầu tiên. Tín hiệu Echo 5 V của cảm biến siêu âm HC-SR04 cũng gây ra rủi ro tương tự. Kỹ thuật chuyển mức điện áp sẽ được trình bày ở bài sơ đồ chân, còn ngay hôm nay bạn cần giữ nguyên tắc không nối bất kỳ tín hiệu 5 V nào vào hàng chân GPIO.&lt;/p&gt;

&lt;h2 id=&quot;trình-tự-khởi-chạy-bo-mạch&quot;&gt;Trình tự khởi chạy bo mạch&lt;/h2&gt;

&lt;p&gt;Phần lớn các lỗi phần mềm trông có vẻ “ngẫu nhiên” trong tuần đầu thực chất bắt nguồn từ sụt áp nguồn. Khi nguồn sụt, chip tự khởi động lại (reset), bộ đếm quay về 0, khiến đoạn mã vừa sửa trông như nguyên nhân gây lỗi. Trình tự dưới đây thêm từng thành phần một để dễ dàng khoanh vùng nguyên nhân nếu có sự cố.&lt;/p&gt;

&lt;p&gt;Trước hết, máy tính phải nhận diện được bo mạch: với ESP32 là một cổng serial mới, còn với Pico ở chế độ bootloader là một ổ đĩa gắn ngoài (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt; trên Pico gốc hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RP2350&lt;/code&gt; trên Pico 2). Kế tiếp, cho LED tích hợp nháy theo chương trình bạn nạp để xác nhận xung nhịp và chân điều khiển hoạt động. Sau đó, gửi một dòng dữ liệu qua cổng serial ở tốc độ 115200 baud để kiểm tra cáp có truyền dữ liệu. Tiếp theo, đọc tín hiệu từ một nút nhấn hoặc dây nối xuống mass (GND) để xác nhận ngõ vào số hoạt động. Cuối cùng, xuất thử xung PWM trên một chân không tải (hoặc chỉ gắn LED kèm điện trở hạn dòng). Chỉ sau khi hoàn tất năm bước này mới được đấu mạch lái động cơ (driver) và pin. Động cơ TT khi bị kẹt trục có thể kéo dòng lớn làm sụt nguồn USB; nếu nối driver ngay từ ngày đầu, bạn rất dễ mất thời gian sửa một vòng lặp chương trình vốn không hề sai.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/tracks_mcu_ros.png&quot; alt=&quot;Từ sketch vi điều khiển tới phần ROS sau này&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Nửa bên trái của sơ đồ tương ứng với chương này: một vi điều khiển, bài thử nháy LED và một dòng serial. Phần điều khiển từ xa của Capstone A mở rộng trên chính con chip đó, xuất PWM tới TB6612 sau khi hoàn tất phần đấu dây driver. Phần ROS ở các chương sau dựa trên giả định rằng trình tự cơ bản này đã chạy ổn định.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-đã-làm&quot;&gt;Ví dụ đã làm&lt;/h2&gt;

&lt;p&gt;An muốn cuối học kỳ dùng điện thoại để điều khiển một robot hai bánh. Ở chương 07, firmware điều khiển từ xa sẽ xuất PWM tới TB6612, tự ngắt sau khoảng 300 ms nếu mất lệnh và yêu cầu nhấc bánh trong lần chạy đầu tiên. Đến chương 08, hệ thống điều khiển này sẽ chuyển sang chạy qua Wi-Fi. An ghi vào sổ: “Vi điều khiển chính là ESP32 NodeMCU-32S dùng chip CH340, vì chương 08 cần Wi-Fi và tuần này cần một bo mạch 3,3 V có thể nạp chương trình ngay.”&lt;/p&gt;

&lt;p&gt;Module trần ESP32-S3-WROOM-1 tuy rẻ hơn nhưng không cắm trực tiếp vào laptop được. Pico 2 bản không có W tuy dễ học với MicroPython nhưng lại thiếu kết nối không dây cho điều khiển qua điện thoại. Vì vậy, An chỉ mua một bộ điều khiển chính, sau đó ghi tiếp trình tự khởi chạy bên dưới: nhận cổng USB, nháy LED tích hợp, in dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt; ở 115200 baud, đọc nút nhấn kéo xuống mức thấp, xuất PWM không tải, rồi mới nối TB6612 và pin khi khung xe đã được nhấc lên. Arduino Uno chỉ giữ lại làm mốc tham chiếu 5 V.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Bài thực hành này có thể làm trên giấy trước khi linh kiện giao tới.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Kẻ bảng gồm ba hàng tương ứng với họ Uno, bo mạch ESP32 và Pico (hoặc Pico 2), với các cột: mức điện áp GPIO, sóng vô tuyến tích hợp, ngôn ngữ buổi đầu và vai trò trong Capstone A.&lt;/li&gt;
  &lt;li&gt;Khoanh tròn lựa chọn chính. Bên dưới bảng, liệt kê đủ sáu bước khởi chạy: máy tính nhận USB, nháy LED tích hợp, serial 115200 baud, đọc nút nhấn, xuất PWM không tải, cuối cùng mới nối driver và pin.&lt;/li&gt;
  &lt;li&gt;Ghi thêm một câu mô tả hiện tượng sụt áp (brownout): nguồn bị sụt khiến chip tự reset, dễ gây nhầm lẫn với lỗi phần mềm.&lt;/li&gt;
  &lt;li&gt;Nếu mua hàng trong tuần này, chép chính xác tên sản phẩm, mức giá tham khảo và ngày kiểm tra vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. Nếu đã có sẵn bo mạch trên bàn, ghi lại nhãn in trên mạch và tên chip USB-serial (nếu đọc được), chưa tiến hành nạp mã.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Kết quả cần trình bày rõ ràng trên một trang để người khác đọc hiểu được. Bài đạt khi bạn chốt rõ một vi điều khiển chính, nêu được vai trò của hai dòng còn lại và ghi đủ trình tự khởi chạy như trên. Cách viết chung chung như “thử cả hai rồi tính” không đạt yêu cầu.&lt;/p&gt;

&lt;p&gt;Lưu ý khi chọn mua: ảnh sản phẩm chỉ có vỏ kim loại mà không có cổng USB là module trần; cáp USB chỉ hỗ trợ sạc vẫn làm sáng đèn nguồn nhưng máy tính sẽ không tạo cổng kết nối. Cần kiểm tra đúng loại sản phẩm và cáp trước khi tìm lỗi ở driver.&lt;/p&gt;

&lt;p&gt;An toàn: chưa đấu nối pin, động cơ hay cảm biến 5 V trong lúc đang chọn bo mạch. Thao tác duy nhất trong bài này là đọc nhãn và thông số.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=oJs0cVYuQTw&quot;&gt;Raspberry Pi VS Arduino VS ESP32&lt;/a&gt; — TechteamGB, tiếng Anh. Giải thích vì sao Arduino, ESP32 và Raspberry Pi không thay thế trực tiếp cho nhau, khớp với bảng so sánh trong bài.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=L03jT5slWnw&quot;&gt;Raspberry Pi Pico - A Beginners Guide&lt;/a&gt; — Tinkernut, tiếng Anh. Giới thiệu Pico — lựa chọn thứ ba trong bài; chú ý quan sát cổng USB và sơ đồ chân trước khi so sánh với ESP32.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Điền bảng so sánh Uno, ESP32 và dòng Pico (gồm cả Pico gốc với Pico W, Pico 2 với Pico 2 W). Bắt buộc có một ô ghi rõ “GPIO 5 V”.&lt;/li&gt;
  &lt;li&gt;Một sinh viên nối mạch cầu H và pin 2S trước khi kiểm tra bất kỳ dòng serial nào, sau đó báo rằng bộ đếm “tự khởi động lại”. Hiện tượng điện nào đang xảy ra và sinh viên đó đã bỏ qua những bước nào?&lt;/li&gt;
  &lt;li&gt;Tính \(0{,}7 \times 3{,}3\) và \(3{,}3 + 0{,}3\) theo đơn vị vôn. Giải thích trong hai câu ý nghĩa của mỗi con số và lý do không được nối trực tiếp chân Echo 5 V vào GPIO.&lt;/li&gt;
  &lt;li&gt;Viết câu quyết định chọn vi điều khiển cho Capstone A. Nếu chọn ESP32, nêu rõ tên bo mạch phát triển và lý do không mua module WROOM trần. Nếu chọn Pico, nêu rõ phương án bổ sung Wi-Fi về sau.&lt;/li&gt;
  &lt;li&gt;Có ý kiến cho rằng Raspberry Pi 5 phải là bộ điều khiển chính vì “robot chạy Linux”. Trên thực tế, firmware của Capstone A trong học kỳ này chạy trên phần cứng nào?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Đáp án&lt;/summary&gt;

1. Họ Uno: GPIO 5 V, không tích hợp Wi-Fi, thường khởi đầu bằng Arduino C++, không phù hợp làm bộ điều khiển chính cho Capstone A khi dùng chung với cảm biến 3,3 V. Bo mạch ESP32: GPIO 3,3 V, có sẵn Wi-Fi và BLE, lập trình bằng Arduino qua core ESP32 (hoặc MicroPython về sau), là lựa chọn mặc định vì chương 08 cần kết nối không dây. Pico gốc và Pico 2 không W: GPIO 3,3 V, không tích hợp Wi-Fi, dùng MicroPython để thử nghiệm nhanh. Pico W và Pico 2 W: cùng mức logic 3,3 V, có thêm kết nối không dây.
2. Đó là hiện tượng sụt áp khiến chip tự reset (brownout). Sinh viên này đã bỏ qua chuỗi kiểm tra cơ bản gồm: nhận cổng USB, nháy LED tích hợp, kiểm tra serial, đọc nút nhấn và xuất PWM không tải. Việc bộ đếm quay về 0 là dấu hiệu chip vừa khởi động lại.
3. $$0{,}7 \times 3{,}3 = 2{,}31\,\mathrm{V}$$ (xấp xỉ 2,3 V) là ngưỡng ước lượng trong học tập để xác định tín hiệu đã đủ cao để tính là mức logic 1 hay chưa. $$3{,}3 + 0{,}3 = 3{,}6\,\mathrm{V}$$ là vùng giới hạn điện áp cực đại tuyệt đối của chân 3,3 V. Tín hiệu Echo 5 V vượt quá 3,6 V nên có thể làm hỏng chân, dù 5 V là mức cao hợp lệ trên Uno.
4. Câu chọn ESP32 đạt khi nêu đúng bo mạch có cổng USB (như NodeMCU-32S hoặc DevKit dùng CH340/CP2102), nêu nhu cầu Wi-Fi cho chương 08 và loại trừ module trần. Câu chọn Pico đạt khi nêu ưu tiên MicroPython, xác nhận bản không W thiếu Wi-Fi và nêu rõ kế hoạch nâng cấp lên bản W hoặc chuyển sang ESP32 ở chương 08.
5. Firmware điều khiển từ xa của Capstone A chạy trên vi điều khiển (ESP32 hoặc Pico). Raspberry Pi 5 là máy tính chạy Linux riêng biệt, không thay thế vai trò của vi điều khiển, cáp dữ liệu hay trình tự khởi chạy cơ bản này.

&lt;/details&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Danh sách linh kiện đầy đủ (gồm TB6612, động cơ và cảm biến) nằm ở bài mua sắm chương 00. Trong bài này, bạn chỉ cần mua bo mạch điều khiển và một sợi cáp dữ liệu. Giá dưới đây được ghi nhận từ trang công khai của Hshop vào ngày 23 tháng 9 năm 2026; hãy kiểm tra lại giá hiện tại trước khi đặt mua.&lt;/p&gt;

&lt;p&gt;Lựa chọn mặc định: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;ESP32 NodeMCU-32S CH340, 190 000 ₫&lt;/a&gt; (có sẵn cổng USB và chip CH340). Một bo mạch hoàn chỉnh khác là &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32, 265 000 ₫&lt;/a&gt;. Riêng &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;ESP32-S3-WROOM-1, 135 000 ₫&lt;/a&gt; là module trần, không phù hợp để khởi đầu dù giá rẻ hơn.&lt;/p&gt;

&lt;p&gt;Nếu đi theo hướng MicroPython: kết quả tìm kiếm &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;raspberry pi pico&lt;/a&gt; tại thời điểm kiểm tra hiển thị Pico 2 giá 195 000 ₫ và Pico 2 W giá 275 000 ₫. Trang sản phẩm tương ứng là &lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-rp2350&quot;&gt;Pico 2 (RP2350)&lt;/a&gt; và &lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-w-rp2350&quot;&gt;Pico 2 W&lt;/a&gt;. Lưu ý rằng Pico 2 bản không có W không tích hợp Wi-Fi.&lt;/p&gt;

&lt;p&gt;Cáp dữ liệu tương thích với các bo mạch dùng cổng micro-USB kể trên: &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;. Tránh dùng cáp chỉ sạc vì máy tính sẽ không nhận bo mạch.&lt;/p&gt;

&lt;p&gt;Nếu muốn so sánh thêm trên Shopee, có thể tìm &lt;a href=&quot;https://shopee.vn/search?keyword=esp32%20devkit%20ch340&quot;&gt;ESP32 DevKit CH340&lt;/a&gt; và &lt;a href=&quot;https://shopee.vn/search?keyword=raspberry%20pi%20pico%202&quot;&gt;Raspberry Pi Pico 2&lt;/a&gt;, chú ý kiểm tra ảnh xem có cổng USB hay không. &lt;a href=&quot;https://www.thegioiic.com/esp32-devkitc-32u-module-wifi-bluetooth-2-4ghz&quot;&gt;Trang ESP32-DevKitC-32U của Thế Giới IC&lt;/a&gt; cũng là một mẫu dev board; cần xem kỹ anten có dùng đầu nối U.FL và sản phẩm có kèm cáp hay không. &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; là cửa hàng linh kiện tại Thủ Đức, phù hợp để mua điện trở về sau, nhưng cần phân biệt kỹ giữa linh kiện rời và bo mạch phát triển có sẵn cổng USB.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;Datasheet ESP32 (PDF)&lt;/a&gt; — thông số điện áp GPIO, các chân strapping và bảng đặc tính điện đứng sau ngưỡng 2,3 V.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://datasheets.raspberrypi.com/pico/pico-datasheet.pdf&quot;&gt;Datasheet Raspberry Pi Pico&lt;/a&gt; — thông tin về LED tích hợp tại GPIO 25 và ý nghĩa các đường nguồn VBUS, VSYS, 3V3.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/&quot;&gt;Tài liệu vi điều khiển Raspberry Pi&lt;/a&gt; — tổng quan các phiên bản Pico, Pico W, Pico 2 và Pico 2 W trong cùng một họ sản phẩm.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 01</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_11_ch01_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_11_ch01_lab_checklist</id>
   <content type="html">&lt;p&gt;Chương 01 chỉ thực sự hoàn tất khi sổ lab của bạn có đủ &lt;strong&gt;bảy bằng chứng&lt;/strong&gt; cụ thể, mỗi bằng chứng đều có tiêu chí kiểm tra rõ ràng. Một dấu tick bên cạnh dòng chữ “tôi đã hiểu sơ đồ chân” không được tính là bằng chứng. Bài học này hướng dẫn bạn tự rà soát: thế nào là đạt thực chất, thế nào là đánh dấu hình thức, và cần quay lại bài nào khi một hạng mục chưa đạt. Bạn có thể hoàn thiện bảy cổng theo bất kỳ thứ tự nào, nhưng tuyệt đối không dùng ảnh của người khác thay thế cho phần mình còn thiếu.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ tự đánh giá được đủ bảy cổng đầu ra của Chương 01 dựa trên hiện vật thực tế: phép tính điện trở LED ở 3,3 V, ảnh sơ đồ thanh nguồn breadboard, bảng năm điện trở đã giải mã và đo ngoài mạch, kết quả xác định cực tính bằng thang đo diode, ảnh mạch lái ghi rõ tên chip &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt; (không ghi chung chung là “cầu H”), ảnh mối hàn (hoặc lịch hẹn làm bù nếu chưa có mỏ hàn), và đoạn phân tích sụt áp có nêu rõ nguồn pin động cơ riêng. Bạn cũng sẽ nhận diện được các dấu tick hình thức (thiếu số đo hoặc chụp nhầm chip) và chỉ ra đúng bài học cần mở lại để khắc phục.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Các hiện vật cần kiểm tra được tạo ra từ bài 01 (phép tính LED) và chuỗi bài 07 đến 10. Nếu còn thiếu dụng cụ, xem lại danh sách mua sắm ở Chương 00 bài 05. Bạn chỉ cần chuẩn bị sổ lab và các ảnh chụp của mình; vẫn chưa chạy xe, luôn giữ bánh nhấc khỏi bàn và chưa nối động cơ.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Kết thúc Chương 01 là một cột mốc quan trọng: bạn đã đi từ định luật Ohm trên giấy tới việc tự tay nhận diện linh kiện, đo kiểm an toàn bằng đồng hồ vạn năng, hàn dây chắc chắn và bảo vệ mạch khỏi ngược cực hay sụt áp. &lt;strong&gt;Checklist Chương 01&lt;/strong&gt; đóng vai trò bộ lọc kỹ thuật giúp bạn chắc chắn tầng điện–nguồn đã “sạch bóng lỗi” trước khi cắm bo vi điều khiển ESP32 hoặc Pico vào ở Chương 02.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bảy-cổng-kiểm-soát-chất-lượng-tầng-điện-tử&quot;&gt;Kiến thức cần nhớ: Bảy Cổng Kiểm soát Chất lượng Tầng Điện tử&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cổng Nguồn &amp;amp; Thông mạch:&lt;/strong&gt; Không có chạm chập &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC–GND&lt;/code&gt;, điện áp các đường nguồn (pin, 5 V Buck, 3,3 V) nằm đúng dung sai \(\pm 5\%\) cả lúc không tải lẫn lúc đóng tải giả.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cổng Linh kiện &amp;amp; Phân cực:&lt;/strong&gt; Đọc đúng điện trở (màu &amp;amp; SMD), xác định chính xác cực tính LED, diode và tụ hóa, kiểm chứng được mạch phân áp 5 V → 3,3 V bằng đồng hồ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cổng Cơ–Điện &amp;amp; An toàn:&lt;/strong&gt; Mối hàn bóng chắc, có ống co nhiệt chống chập, cầu chì gắn đúng cực dương pin và dây nguồn động cơ tách riêng khỏi nhánh logic.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-bảng-nghiệm-thu-định-lượng-cho-hệ-nguồn-trước-khi-cắm-esp32&quot;&gt;Ví dụ làm từng bước: Bảng nghiệm thu định lượng cho hệ nguồn trước khi cắm ESP32&lt;/h3&gt;

&lt;p&gt;Thay vì đánh dấu tick cảm tính, hãy điền và kiểm chứng đúng 4 con số đo thật sau trên bàn lab của bạn:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục nghiệm thu&lt;/th&gt;
      &lt;th&gt;Điều kiện đo&lt;/th&gt;
      &lt;th&gt;Ngưỡng đạt chuẩn&lt;/th&gt;
      &lt;th&gt;Ví dụ kết quả ĐẠT&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;1. Thông mạch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V–GND&lt;/code&gt; (tắt nguồn)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Thang Ohm / Thông mạch&lt;/td&gt;
      &lt;td&gt;Không kêu bíp, \(R &amp;gt; 1\text{ k}\Omega\)&lt;/td&gt;
      &lt;td&gt;\(R = 14.2\text{ k}\Omega\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;2. Đầu ra mạch Buck 5 V (không tải)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Thang DC V (20 V)&lt;/td&gt;
      &lt;td&gt;\([4.85\text{ V},\ 5.15\text{ V}]\)&lt;/td&gt;
      &lt;td&gt;\(5.02\text{ V}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;3. Sụt áp Buck khi đóng tải \(500\text{ mA}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Mắc tải giả \(10\ \Omega / 5\text{ W}\)&lt;/td&gt;
      &lt;td&gt;\(\Delta V &amp;lt; 0.15\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(4.96\text{ V}\) (\(\Delta V = 0.06\text{ V}\))&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;4. Mạch phân áp 5 V → 3,3 V&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Cấp 5,02 V vào đầu trên&lt;/td&gt;
      &lt;td&gt;\([3.0\text{ V},\ 3.45\text{ V}]\)&lt;/td&gt;
      &lt;td&gt;\(3.26\text{ V}\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Chỉ cần &lt;strong&gt;một&lt;/strong&gt; trong 4 chỉ số trên trượt ngưỡng (ví dụ Buck ra \(6.8\text{ V}\) do chưa vặn biến trở chỉnh áp!), bạn đã tự cứu sống bo ESP32 trị giá hàng trăm nghìn đồng trước khi cắm nó vào!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-4-bước-vượt-qua-cổng-kiểm-tra-chương-01-trung-thực&quot;&gt;Giải thích: 4 bước vượt qua cổng kiểm tra Chương 01 trung thực&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Không bao giờ “tick hình thức”:&lt;/strong&gt; Mọi hạng mục chưa có số đo ghi trong sổ lab đều coi là &lt;em&gt;chưa đạt&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Kiểm tra lặp lại sau khi rung lắc nhẹ cụm dây:&lt;/strong&gt; Cầm nhẹ cụm dây nguồn lắc qua lại trong khi đồng hồ vẫn kẹp đo điện áp — nếu số đo nhảy loạn xạ, mối hàn hoặc đầu nối đang lỏng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Chụp lại ảnh mặt trên và mặt dưới mạch vừa hàn:&lt;/strong&gt; Ảnh chụp sắc nét là bằng chứng tuyệt vời giúp bạn tự soi cầu thiếc (solder bridge) khi phóng to trên màn hình.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Lưu sơ đồ phân bổ đường nguồn vào thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docs/&lt;/code&gt;:&lt;/strong&gt; Sang Chương 02, bạn sẽ nối thẳng bo vi điều khiển vào đúng các đầu chờ đã kiểm chứng này.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Tại sao khi kiểm tra điện áp đầu ra của mạch Buck mới mua (loại có biến trở chỉnh áp như LM2596/MP1584), ta &lt;strong&gt;bắt buộc&lt;/strong&gt; phải đo và vặn chỉnh về 5,0 V &lt;em&gt;trước&lt;/em&gt; khi nối vào bo mạch robot? Vì nhiều mạch Buck xuất xưởng đang để mức áp mặc định gần bằng \(V_{\text{in}}\) (7–8 V) — cắm thẳng vào sẽ đốt cháy ngay toàn bộ mạch 5 V và 3,3 V!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Xe Capstone là hệ dẫn động vi sai dùng ESP32 hoặc Pico 3,3 V, mạch lái TB6612 hoặc DRV8833, hai động cơ TT dùng pin riêng và cảm biến HC-SR04 có chân echo 5 V. Sang Chương 02 bạn sẽ bắt đầu lắp ráp và cấp điện cho các khối này. Nếu sơ đồ breadboard chỉ là ảnh board trống không đánh dấu, bạn dễ tốn cả buổi chiều gỡ lỗi đèn LED không sáng; nếu ảnh driver chỉ ghi “cầu H”, rất dễ quên kéo chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; khiến bánh đứng yên; nếu chưa nắm vững đoạn phân tích sụt áp, hiện tượng mạch reset khi dùng chung nguồn USB sẽ bị nhầm thành lỗi chương trình. Buổi rà soát này giúp bạn phát hiện sớm mọi lỗ hổng ngay trên sổ ghi chép.&lt;/p&gt;

&lt;h2 id=&quot;cách-kiểm-tra-một-hạng-mục&quot;&gt;Cách kiểm tra một hạng mục&lt;/h2&gt;

&lt;p&gt;Mỗi cổng kiểm tra gồm một &lt;strong&gt;hiện vật cụ thể&lt;/strong&gt; (phép tính có đơn vị, ảnh chụp có chú thích chữ, đoạn phân tích kèm số đo) và một &lt;strong&gt;tiêu chí đối chiếu&lt;/strong&gt; rõ ràng (nằm trong dung sai, vạch tụ được gọi là âm, đọc rõ mã IC, có cụm từ “pin động cơ riêng”). Ngược lại, &lt;strong&gt;dấu tick hình thức&lt;/strong&gt; là khi chỉ đánh dấu hoàn thành nhưng thiếu phép đối chiếu — ví dụ ghi “xong” không kèm số ohm, “đã chụp” nhưng ảnh mờ không đọc được tên chip, hay “đã hiểu sụt áp” mà không có phân tích. Mọi mục hình thức đều được tính là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa đạt&lt;/code&gt; (còn mở).&lt;/p&gt;

&lt;p&gt;Khi có cổng chưa đạt, hướng xử lý rất gọn: mở lại đúng bài học tương ứng, làm lại đúng bước tạo hiện vật và ghi ngày cập nhật mới (không cần làm lại cả chương). Tuyệt đối không chuyển sang chạy thử động cơ ở Chương 02 khi cổng nhãn driver hoặc cổng phân tích sụt áp vẫn còn mở, vì hai cổng này liên quan trực tiếp đến nguy cơ hỏng phần cứng. Ngoại lệ duy nhất là trường hợp chưa có mỏ hàn: bạn được phép ghi một ngày hẹn cụ thể để thực hành bù và coi lịch hẹn đó là hiện vật tạm thời.&lt;/p&gt;

&lt;h2 id=&quot;bảy-cổng-kiểm-tra-đầu-ra&quot;&gt;Bảy cổng kiểm tra đầu ra&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Phép tính điện trở LED ở 3,3 V.&lt;/strong&gt; Trình bày đầy đủ phép tính chọn điện trở cho đèn LED báo trạng thái nối vào chân GPIO, ghi rõ điện áp thuận $V_F$ sử dụng (với LED đỏ, có thể lấy \(V_F = 2{,}0\,\mathrm{V}\) nếu chưa đo ở bài 10). Ví dụ với điện trở 220 Ω:&lt;/p&gt;

\[I = \frac{3{,}3 - 2{,}0}{220} = \frac{1{,}3}{220} \approx 5{,}9\,\mathrm{mA}\]

&lt;p&gt;&lt;em&gt;Tiêu chí đạt:&lt;/em&gt; Có đủ công thức, bước thay số, kết quả kèm đơn vị mA, và nhận xét dòng này nằm trong dải an toàn 5–10 mA cho LED nhỏ và GPIO 3,3 V (tốt nhất dùng đúng $V_F$ đã đo ở bài 10). Lấy thẳng 3,3 V chia cho điện trở mà bỏ quên $V_F$ sẽ bị tính là trượt. Với LED xanh có $V_F$ cao, kết quả dòng quá nhỏ chỉ đạt nếu bạn nhận xét đèn sẽ mờ và đề xuất đổi màu LED hoặc giảm điện trở phù hợp.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Sơ đồ thanh nguồn breadboard.&lt;/strong&gt; Ảnh chụp hoặc bản vẽ từ bài 10 thể hiện rõ nút năm lỗ, rãnh giữa cách điện và tình trạng từng thanh nguồn (liền mạch hay bị ngắt đôi ở giữa). &lt;em&gt;Tiêu chí đạt:&lt;/em&gt; Người khác chỉ cần nhìn bản vẽ này vẫn cắm đúng nút bạn định; ảnh chụp breadboard trống không có chú thích sẽ trượt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Bảng năm điện trở (vừa giải mã vừa đo).&lt;/strong&gt; Đủ 5 dòng, mỗi dòng có dãy màu vạch, trị số danh định, dung sai và số đo ngoài mạch, kèm kết luận đạt/trượt theo cửa sổ dung sai. Thiếu màu vạch, thiếu số đo, đo khi còn gắn trong mạch hoặc chỉ làm 4 dòng đều bị tính là trượt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Xác định cực tính bằng thang đo diode.&lt;/strong&gt; Kết quả đo một LED hoặc một diode kèm số điện áp thuận và câu kết luận rõ ràng dạng “chân này là anode”. &lt;em&gt;Tiêu chí đạt:&lt;/em&gt; Người khác có thể nối đúng LED đó từ 3,3 V qua điện trở của cổng 1 (anode về phía GPIO, cathode về mass) mà không cần hỏi thêm. Lưu ý: việc nhận diện vạch cực âm tụ hóa vẫn cần có trong sổ bài 10 và tuân thủ ở bài 09, nhưng không thay thế được phép đo diode của cổng 4.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Ảnh mạch lái động cơ có ghi rõ tên chip.&lt;/strong&gt; Ảnh chụp module mạch lái trên đó bạn chú thích rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt; kèm tên các chân điều khiển tương ứng (với TB6612 tối thiểu có &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;). Các nhãn chung chung như “cầu H”, “mạch động cơ” hay “board nhỏ” đều trượt dù ảnh rất nét. Nếu module chưa về tới, mục này được ghi dạng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dry-run&lt;/code&gt; có ngày và nêu rõ loại chip đã đặt mua.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Ảnh mối hàn hoặc lịch thực hành bù.&lt;/strong&gt; Ảnh mối hàn tập từ bài 08 kèm kết quả đo thông mạch và đối chiếu với hình mối hàn chuẩn. Mối kêu bíp nhưng vo cục, hoặc mối trông bóng nhưng hở mạch đều trượt. Nếu chưa có mỏ hàn, ghi rõ ngày hẹn thực hành bù kèm quy tắc an toàn giá đỡ và kính bảo hộ. Chụp ảnh mối hàn nhà máy có sẵn trên module mua về rồi nhận là bài tập của mình sẽ tính là trượt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Đoạn phân tích sụt áp (brownout).&lt;/strong&gt; Ghi chú từ bài 09 có số đo chân 5 V và 3,3 V khi chỉ cắm USB (nếu đã đo được), giả thuyết mạch reset do động cơ kéo sụt đường nguồn dùng chung, và đủ các giải pháp: pin động cơ riêng, chỉ nối chung mass, tụ 100–470 µF đúng cực ngang VM, dây đủ dày, nhấc bánh khỏi bàn, và firmware kéo PWM về 0 khi nguồn sụt hoặc mất lệnh quá khoảng 300 ms. Bất kỳ đoạn viết nào cho phép động cơ TT lấy nguồn từ chân 5 V của ESP32 đều trượt ngay lập tức. Bạn có thể ghi kèm lệnh cấm nối thẳng echo 5 V của HC-SR04 vào GPIO 3,3 V, nhưng câu này không thay thế được phần tách pin động cơ.&lt;/p&gt;

&lt;h2 id=&quot;nhận-diện-các-dấu-tick-hình-thức&quot;&gt;Nhận diện các dấu tick hình thức&lt;/h2&gt;

&lt;p&gt;Đối chiếu với bảy cổng trên: ghi “Điện trở: xong” trượt cổng 3 (thiếu giải mã và số đo); “Cực tính ổn” trượt cổng 4 (không ghi rõ anode và điện áp thuận); “Driver: cầu H trong kit” trượt cổng 5 (sang Chương 02 không phân biệt được &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; của TB6612 với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nSLEEP&lt;/code&gt; của DRV8833); “Đã hiểu cách hàn” trượt cổng 6 (không có ảnh lẫn ngày hẹn); “Sụt áp: động cơ ăn nhiều dòng” trượt cổng 7 (thiếu tách pin riêng, mass chung và ngưỡng cắt PWM 300 ms). Cách sửa không phải là viết thêm lời giải thích dài dòng, mà là bổ sung đúng hiện vật cổng yêu cầu.&lt;/p&gt;

&lt;p&gt;Khi dụng cụ đã có sẵn, việc bổ sung mỗi cổng trượt rất nhanh: cổng 3 chỉ cần đo lại năm điện trở; cổng 5 chỉ mất vài phút tra trang Pololu/TI để ghi lại đúng tên chân; cổng 6 nếu chưa có mỏ hàn thì đặt mua theo bài 08 và chốt ngày thực hành bù.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành&quot;&gt;Thực hành&lt;/h2&gt;

&lt;p&gt;Đây là buổi tự kiểm tra; chỉ lắp lại mạch khi cần tạo lại một hiện vật bị trượt.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Mở sổ lab và kẻ bảng gồm 7 dòng tương ứng với 7 cổng.&lt;/li&gt;
  &lt;li&gt;Cổng 1: viết lại đầy đủ phép tính LED 3,3 V trên một dòng mới (ngay cả khi bài 01 đã tính).&lt;/li&gt;
  &lt;li&gt;Đính kèm sơ đồ breadboard, bảng năm điện trở và kết quả xác định cực tính ở thang diode; sửa ngay bất kỳ mục nào không qua được tiêu chí đối chiếu.&lt;/li&gt;
  &lt;li&gt;Chú thích rõ mã IC (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt;) lên ảnh mạch lái (hoặc ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dry-run&lt;/code&gt; kèm ngày dự kiến nhận hàng).&lt;/li&gt;
  &lt;li&gt;Xem kỹ lại ảnh mối hàn dưới ánh sáng tốt; nếu mối vo cục thì làm lại trước khi chấm, hoặc ghi ngày hẹn bù nếu chưa có mỏ hàn.&lt;/li&gt;
  &lt;li&gt;Đọc lại đoạn phân tích sụt áp và kiểm tra chắc chắn có đủ ba từ khóa cốt lõi: “pin động cơ riêng”, “mass chung” và “300 ms”.&lt;/li&gt;
  &lt;li&gt;Đánh dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đạt&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trượt&lt;/code&gt; cho từng cổng, lưu trang với mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-11-audit&lt;/code&gt; kèm ngày kiểm tra. Với mỗi cổng trượt, ghi rõ số thứ tự bài học sẽ mở lại để sửa.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Toàn bộ buổi rà soát này không cấp nguồn quay bánh xe.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ&quot;&gt;Ví dụ&lt;/h2&gt;

&lt;p&gt;Giả sử một trang sổ ghi như sau: &lt;em&gt;“Điện trở LED xong. Ảnh breadboard đính kèm, không đánh dấu. Đo ba điện trở, hai con trông đúng. Cực tính: vạch chắc là âm. Driver là cầu H. Hàn tuần sau có lẽ. Sụt áp: đừng để mô-tơ kẹt.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Kết quả chấm từng cổng:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cổng 1 trượt:&lt;/strong&gt; chỉ ghi “xong”, không có phép tính.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cổng 2 trượt:&lt;/strong&gt; ảnh breadboard trống không chú thích không phải sơ đồ thanh nguồn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cổng 3 trượt:&lt;/strong&gt; mới đo ba con thay vì năm con, và “trông đúng” không phải đối chiếu giải mã với số đo.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cổng 4 trượt:&lt;/strong&gt; đoán vạch tụ hóa không thay thế được phép đo cực tính bằng thang diode.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cổng 5 trượt:&lt;/strong&gt; nhãn “cầu H” không nêu rõ mã chip và tên chân.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cổng 6 chưa đạt:&lt;/strong&gt; “tuần sau có lẽ” cần đổi thành một ngày cụ thể mới thành lịch bù hợp lệ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cổng 7 trượt:&lt;/strong&gt; thiếu pin riêng, thiếu mass chung, thiếu tụ đệm và thiếu quy tắc ngắt PWM 300 ms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hướng sửa cho buổi kế tiếp: làm lại thực tế trên bàn các cổng 1, 3, 4 và viết lại đoạn cổng 7; bổ sung chú thích lên ảnh cho cổng 2 và 5; chốt một ngày cụ thể cho cổng 6.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=HsLLq6Rm5tU&quot;&gt;Ohms Law Explained - The basics circuit theory&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Ôn lại nhanh định luật Ohm và công suất trước khi chốt checklist Chương 01.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;p&gt;Mỗi tình huống dưới đây là một trích đoạn sổ lab chưa đạt. Hãy chỉ ra cổng nào trượt, thiếu thông tin gì và cách sửa gọn nhất:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;”\(3{,}3 / 180 = 18\,\mathrm{mA}\), LED sẽ sáng.”&lt;/em&gt; — Thuộc cổng nào, và phép tính đã bỏ quên đại lượng nào?&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;“Năm điện trở: 220, 1k, 10k, 330, 470. Đều tốt.”&lt;/em&gt; — Thuộc cổng nào, và thiếu nửa bằng chứng nào?&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;“Chế độ diode: đồng hồ kêu bíp nên cực tính ổn.”&lt;/em&gt; — Thuộc cổng nào, và vì sao tiếng kêu chứng tỏ dùng sai chức năng?&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;“Ảnh lưu driver.jpg. Đây là cầu H mua trên Shopee, chân đấu như video.”&lt;/em&gt; — Cổng nào trượt, và cần ghi rõ tên mã nào để đạt?&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;“Mối hàn trông ổn, không có ảnh, mỏ hàn ở tiệm đến 15h thứ Sáu mới lấy. Đoạn sụt áp ghi cả hai mô-tơ TT dùng chung chân 5 V của ESP32 cho gọn dây, chân echo cắm thẳng vào GPIO 4.”&lt;/em&gt; — Cổng nào trượt, cổng nào có thể tính là lịch bù, và chi tiết nào thực sự gây nguy hiểm phần cứng?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
  &lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

  &lt;ol&gt;
    &lt;li&gt;Cổng 1 trượt do bỏ quên điện áp thuận $V_F$ của LED nên 18 mA không phải dòng thực tế. Sửa lại thành \(I = (3{,}3 - V_F) / 180\) có nêu rõ $V_F$ (ví dụ với \(V_F = 2{,}0\,\mathrm{V}\) thì dòng khoảng \(7{,}2\,\mathrm{mA}\)).&lt;/li&gt;
    &lt;li&gt;Cổng 3 trượt vì mới ghi trị số danh định mà thiếu toàn bộ 5 con số đo thực tế bằng đồng hồ ngoài mạch (và nhận xét dung sai).&lt;/li&gt;
    &lt;li&gt;Cổng 4 trượt vì tiếng kêu bíp thuộc thang thông mạch chứ không phải thang đo diode. Cần đo lại ở thang diode để ghi điện áp thuận và xác định rõ chân anode.&lt;/li&gt;
    &lt;li&gt;Cổng 5 trượt. Cần ghi rõ tên chip là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt; kèm tên chân tương ứng, bởi “cầu H” và “như video” không phân biệt được chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nSLEEP&lt;/code&gt;.&lt;/li&gt;
    &lt;li&gt;Cổng 6 có thể tính là lịch bù nếu ghi rõ ngày dương lịch cho mốc “15h thứ Sáu” và cam kết bổ sung ảnh mối hàn sau đó. Cổng 7 trượt nặng và rất nguy hiểm: tuyệt đối không cấp nguồn hai động cơ TT từ chân 5 V của ESP32 và không cắm thẳng echo 5 V vào GPIO 4; cần sửa lại theo bài 09 và lệnh cấm ở bài 07.&lt;/li&gt;
  &lt;/ol&gt;

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Module TB6612FNG của Pololu&lt;/a&gt; — đối chiếu tên chip và sơ đồ chân cho cổng 5 nếu dùng TB6612.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ti.com/product/DRV8833&quot;&gt;TI DRV8833&lt;/a&gt; — tài liệu tên chân cho phương án DRV8833 (tránh nhầm với sơ đồ TB6612).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;Datasheet ESP32 (PDF)&lt;/a&gt; — căn cứ giới hạn 3,3 V đứng sau cổng 1 và cổng 7.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.adafruit.com/adafruit-guide-excellent-soldering&quot;&gt;Hướng dẫn hàn của Adafruit&lt;/a&gt; — tiêu chuẩn hình dạng mối hàn để tự chấm cổng 6.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Không cần lập thêm bộ kit mới; chỉ đối chiếu các cổng còn mở với bài &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt; và mua đúng phần còn thiếu.&lt;/p&gt;

&lt;p&gt;Nếu thiếu dụng cụ hàn cho cổng 6 (giá tham khảo Hshop ngày 23 tháng 9 năm 2026): &lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;mỏ hàn 60 W&lt;/a&gt; 75.000 ₫, &lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;giá đỡ&lt;/a&gt; 40.000 ₫ và &lt;a href=&quot;https://hshop.vn/thiec-han-sunchi-kp26-0-8mm-sn63-pb37-solder-wire&quot;&gt;cuộn thiếc Sn63 0,8 mm&lt;/a&gt; 24.000 ₫. Nếu thiếu breadboard cho cổng 2: &lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;CAMMB-102&lt;/a&gt; 35.000 ₫. Nếu thiếu đồng hồ cho cổng 3 và 4: &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UNI-T UT33D+&lt;/a&gt;. Điện trở và LED lẻ có thể mua tại &lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; hoặc &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab — thông mạch, đo ohm và linh kiện dễ nhầm</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_10_component_id_lab/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_10_component_id_lab</id>
   <content type="html">&lt;p&gt;Buổi học này dành trọn cho thực hành trên bàn lab. Các bài trước đã giới thiệu lần lượt mã màu điện trở, cực tính diode và vạch tụ hóa; hôm nay bạn sẽ tổng hợp tất cả trên một breadboard không cấp điện và ghi lại toàn bộ thành một bảng số đo thực tế. Một bức ảnh chụp đống linh kiện không thể thay thế số liệu đo đạc, và mã màu chưa kiểm chứng bằng đồng hồ chỉ là phỏng đoán rủi ro nếu mang đi hàn lên xe.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau buổi thực hành này, bạn sẽ lập được sơ đồ kiểm tra thực tế trên breadboard (nhóm lỗ nào thông mạch, khe nào hở) và chụp ảnh lưu sổ. Bạn sẽ giải mã rồi đo ngoài mạch năm điện trở để quyết định đạt hay loại theo khoảng dung sai. Bạn sẽ dùng thang đo diode kiểm tra một LED và một diode, ghi lại điện áp thuận và xác định rõ chân nào là anode. Bạn cũng sẽ phân biệt rõ ba cặp linh kiện dễ nhầm lẫn (điện trở vạch màu với cuộn cảm vạch màu, tụ gốm với tụ tantali, transistor vỏ TO-92 với IC ổn áp vỏ TO-92) và tránh được ba nguồn sai số phổ biến: đo ohm khi linh kiện còn trong mạch, đồng hồ bị đảo cực que ở chế độ diode, và đọc nhầm điện trở năm vạch thành bốn vạch.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn cần ôn lại kiến thức từ bài 02 đến bài 05 về breadboard, cách dùng đồng hồ vạn năng (thang thông mạch, thang ohm, thang diode), mã màu điện trở, tụ điện, diode và LED. Buổi này không dùng mỏ hàn và không cấp nguồn cho TB6612. Nếu thiếu linh kiện nào, hãy viết rõ quy trình đo và đánh dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dry-run&lt;/code&gt; thay vì chép số liệu của người khác.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Lý thuyết đọc vạch màu, giải mã SMD hay tra bảng chân linh kiện chỉ thực sự trở thành phản xạ kỹ sư khi bạn tự tay cầm đồng hồ vạn năng đo kiểm từng linh kiện thật trên bàn! Bài thực hành này rèn cho bạn con mắt “bắt bệnh” các &lt;strong&gt;số đo giả&lt;/strong&gt; — những tình huống mà đồng hồ hiển thị một con số hoàn toàn hợp lý về mặt vật lý nhưng lại đánh lừa người đo thiếu kinh nghiệm (như chạm hai ngón tay vào đầu que đo hay đo điện trở khi nó vẫn đang hàn song song trên mạch).&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kỹ-thuật-đo-kiểm-linh-kiện-và-nguồn-gốc-số-đo-giả&quot;&gt;Kiến thức cần nhớ: Kỹ thuật Đo kiểm Linh kiện và Nguồn gốc Số đo Giả&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Hiệu ứng Điện trở Cơ thể Người:&lt;/strong&gt; Khi đo điện trở trị số lớn (\(&amp;gt; 100\text{ k}\Omega\)), nếu cả hai ngón tay bạn kẹp chặt hai đầu que đo, điện trở da tay (~\(50\text{–}300\text{ k}\Omega\)) mắc &lt;strong&gt;song song&lt;/strong&gt; với linh kiện sẽ kéo tụt kết quả đo xuống thấp hơn thực tế rất nhiều!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đo Trong mạch (In-Circuit) vs. Đo Ngoài mạch (Out-of-Circuit):&lt;/strong&gt; Khi linh kiện vẫn gắn trên PCB hoặc breadboard có nhánh song song khác, dòng đo của đồng hồ chia sang các nhánh phụ — kết quả đo điện trở trong mạch luôn &lt;strong&gt;nhỏ hơn hoặc bằng&lt;/strong&gt; giá trị thật của linh kiện.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thang đo Diode &amp;amp; Kiểm tra Phân cực:&lt;/strong&gt; Thang diode xuất ra điện áp thử ~\(2\text{–}3\text{ V}\) đủ làm sáng mờ các LED đỏ/xanh lá và hiển thị chính xác điện áp rơi thuận \(V_f\) của diode, giúp xác định cực tính Anode/Cathode ngay cả khi ký hiệu trên thân đã mờ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-sai-số-do-chạm-tay-khi-đo-điện-trở-1text-momega-và-đo-song-song-trong-mạch&quot;&gt;Ví dụ làm từng bước: Tính sai số do chạm tay khi đo điện trở \(1\text{ M}\Omega\) và đo song song trong mạch&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Tình huống 1 — Chạm hai tay vào que đo khi đo điện trở \(R = 1\text{ M}\Omega\):&lt;/strong&gt; giả sử điện trở cơ thể giữa hai tay bạn là \(R_{\text{người}} = 200\text{ k}\Omega = 0.2\text{ M}\Omega\). Hai điện trở mắc song song cho kết quả đồng hồ hiển thị:&lt;/p&gt;

\[R_{\text{đo}} = \frac{R \cdot R_{\text{người}}}{R + R_{\text{người}}} = \frac{1.0 \times 0.2}{1.0 + 0.2} = \frac{0.2}{1.2} \approx \mathbf{0.167\text{ M}\Omega = 167\text{ k}\Omega}!\]

&lt;p&gt;Chỉ vì chạm tay, bạn đọc sai tới &lt;strong&gt;83%&lt;/strong&gt; (tưởng nhầm điện trở \(1\text{ M}\Omega\) bị hỏng thành \(167\text{ k}\Omega\))!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tình huống 2 — Đo một điện trở \(10\text{ k}\Omega\) đang mắc song song với một điện trở \(10\text{ k}\Omega\) khác trên bo mạch:&lt;/strong&gt; đồng hồ sẽ đo đúng \(\dfrac{10 \times 10}{10 + 10} = \mathbf{5.0\text{ k}\Omega}\) — không phải điện trở bị hỏng, mà do nhánh song song chia dòng!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-quy-trình-4-bước-đo-kiểm-linh-kiện-chính-xác-không-mắc-bẫy&quot;&gt;Giải thích: Quy trình 4 bước đo kiểm linh kiện chính xác không mắc bẫy&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Tách ít nhất một chân khỏi mạch khi đo điện trở hoặc tụ:&lt;/strong&gt; Muốn biết trị số thật của linh kiện trên breadboard, hãy nhấc 1 chân ra khỏi hàng lỗ rồi mới chấm que đo.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Chỉ kẹp que đo bằng vỏ nhựa cách điện:&lt;/strong&gt; Đặc biệt khi đo điện trở từ \(10\text{ k}\Omega\) trở lên, tuyệt đối không để ngón tay chạm vào cả hai đầu kim loại que đo.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Xả tụ hóa trước khi đo thông mạch hoặc điện trở:&lt;/strong&gt; Tụ còn tích điện sẽ bơm ngược điện áp vào thang Ohm làm lệch số đo hoặc báo bíp giả trong 1 giây đầu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Ghi song song “Giá trị danh định (đọc mã)” và “Giá trị đo thật” vào sổ lab:&lt;/strong&gt; Tính độ lệch phần trăm \(\frac{\vert R_{\text{đo}} - R_{\text{mã}}\vert}{R_{\text{mã}}} \times 100\%\) để xác nhận nằm trong dung sai \(\pm 1\%\) hay \(\pm 5\%\).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Bạn đo một điện trở 4 vạch Đỏ–Đỏ–Nâu–Vàng kim (danh định \(220\ \Omega \pm 5\%\)) nhưng thấy đồng hồ hiển thị &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;216.4 Ω&lt;/code&gt;. Điện trở này có đạt chuẩn không? Độ lệch là \(\frac{\vert 216.4 - 220\vert}{220} = \mathbf{1.64\%} \le 5\%\) → &lt;strong&gt;hoàn toàn đạt chuẩn!&lt;/strong&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Xe đồ án gồm ESP32 hoặc Pico 3,3 V, mạch lái TB6612, hai động cơ TT dùng pin riêng và cảm biến HC-SR04 có chân echo 5 V. Chỉ một linh kiện nhận diện nhầm cũng đủ gây hỏng mạch: LED báo trên chân GPIO cần đúng điện trở đã đo chứ không phải con bị đọc lệch một bậc mười; tụ đệm 100–470 µF trên chân VM là tụ hóa nhôm có vạch đánh dấu cực âm, trong khi nhiều tụ tantali lại dùng vạch chỉ cực dương nên nhầm quy tắc sẽ làm cháy nổ trên đường nguồn động cơ; còn cắm nhầm IC ổn áp TO-92 vào vị trí transistor (hoặc ngược lại) có thể làm hỏng đường 3,3 V. Bảng số liệu của buổi lab này là bằng chứng bạn đã kiểm soát được từng linh kiện trước khi lắp ráp.&lt;/p&gt;

&lt;h2 id=&quot;dụng-cụ-và-linh-kiện-trên-bàn&quot;&gt;Dụng cụ và linh kiện trên bàn&lt;/h2&gt;

&lt;p&gt;Bạn cần chuẩn bị: một đồng hồ vạn năng, một breadboard, năm điện trở, một tụ hóa, một tụ gốm, một LED, một diode và vài sợi dây cắm — tất cả ở trạng thái &lt;strong&gt;không cấp điện&lt;/strong&gt;. Tụ gốm có dạng đĩa hoặc khối nhỏ không dấu cực, còn tụ hóa có vỏ trụ nhôm. Với diode, hãy đọc và ghi đúng mã in trên thân con bạn đang cầm (ví dụ phân biệt rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1N4001&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1N4007&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/resistor_color_code.png&quot; alt=&quot;Vạch màu sẽ được kiểm, không được tin suông&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Đặt bảng mã màu bên cạnh năm điện trở khi điền cột giá trị danh định. Nếu số đo trên đồng hồ khác với kết quả giải mã, hãy kiểm tra lại các vạch màu (kể cả khả năng đếm nhầm năm vạch thành bốn vạch) chứ không sửa số đo cho khớp với bảng.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/component_lookalikes.png&quot; alt=&quot;Vỏ giống nhau, ruột thì không&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hình trên minh họa các cặp linh kiện có ngoại hình tương tự nhau nhưng cấu tạo hoàn toàn khác. Trong danh sách phân biệt, bạn cần ghi rõ từng cặp, phép thử áp dụng và kết quả đo đặc trưng.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/resistors_assortment.jpg&quot; alt=&quot;Một bộ điện trở, trước khi món nào vào mạch&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy chọn năm trị số khác nhau mà bạn giải mã được, trong đó có ít nhất một giá trị ngoài cặp quen thuộc 220 Ω và 10 kΩ để rèn kỹ năng đọc thực chất.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/capacitor_polarity.png&quot; alt=&quot;Chân nào của tụ là cực âm&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Trên tụ hóa vỏ nhôm trong bộ kit, dải vạch luôn chỉ cực âm — nhưng không được áp dụng máy móc quy tắc này cho tụ tantali.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/LEDs.jpg&quot; alt=&quot;Cực LED là câu hỏi của đồng hồ nếu chân đã bị cắt&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Dù chân dài thường là anode và mặt vát trên vành thường là cathode, thói quen này không còn đáng tin khi chân đã bị cắt. Kết quả đo ở thang diode mới là căn cứ ghi vào sổ.&lt;/p&gt;

&lt;h2 id=&quot;các-bước-thực-hiện&quot;&gt;Các bước thực hiện&lt;/h2&gt;

&lt;p&gt;Toàn bộ buổi thực hành diễn ra trong trạng thái &lt;strong&gt;mất nguồn hoàn toàn&lt;/strong&gt; (không cắm USB, không nối pin, không gắn động cơ).&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Lập bản đồ thông mạch breadboard:&lt;/strong&gt; Chuyển đồng hồ sang thang thông mạch. Đo một nhóm năm lỗ ở khu vực giữa để xác nhận chúng nối chung một nút; đo qua rãnh giữa để xác nhận hở mạch; đo dọc từng thanh nguồn từ đầu đến cuối để kiểm tra xem thanh có bị ngắt đôi ở giữa không và đánh dấu rõ vị trí khe ngắt lên bản vẽ phác thảo. Bản đồ này giúp bạn tránh cảnh “LED không sáng” về sau chỉ vì cắm lệch nút.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Giải mã rồi đo năm điện trở:&lt;/strong&gt; Với từng điện trở, ghi dãy màu, trị số danh định và dung sai, sau đó đo ngoài mạch. Khi đo điện trở trị số lớn, không dùng ngón tay kẹp cả hai chân vì điện trở cơ thể người sẽ làm sai lệch kết quả. Một điện trở dung sai 5% (vạch cuối màu vàng kim) chỉ đạt khi nằm trong khoảng:&lt;/li&gt;
&lt;/ol&gt;

\[R_{\mathrm{danh}}(1 - 0{,}05) \le R_{\mathrm{do}} \le R_{\mathrm{danh}}(1 + 0{,}05)\]

&lt;p&gt;Nếu kết quả đo lệch khỏi cửa sổ, hãy đọc lại vạch màu; nếu vẫn nằm ngoài dung sai, tách riêng linh kiện đó ra khỏi nhóm đạt chuẩn. Đồng thời, chập hai que đo để ghi nhận điện trở nền của dây đo (thường vài phần mười ôm) — mức bù này rất đáng kể khi đo điện trở nhỏ cỡ 10 Ω, nhưng không đáng kể ở cỡ 10 kΩ.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Kiểm tra LED và diode ở thang đo diode:&lt;/strong&gt; Ghi lại điện áp thuận và xác định chân vật lý đang chạm que đỏ lúc hiện số đo (sau khi đã xác nhận cực tính que đo của đồng hồ, chân đó chính là anode). Ghi kèm trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; khi đảo que và dấu hiệu nhận dạng ngoài (mặt vát, chân ngắn hoặc vạch cathode). Thông thường LED đỏ đọc khoảng 1,6–2,2 V (có thể sáng mờ), còn diode silic khoảng 0,5–0,8 V. Nếu LED hiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; cả hai chiều, có thể LED đã hỏng hoặc điện áp thử của đồng hồ chưa đủ cao để làm sáng màu LED đó — hãy ghi rõ nhận định của bạn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ghi chú ba cặp linh kiện dễ nhầm lẫn:&lt;/strong&gt; Chép vào sổ cách phân biệt cả ba cặp dưới đây (ngay cả khi trên bàn bạn mới chỉ có một loại trong cặp):
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Điện trở vạch màu và cuộn cảm vạch màu:&lt;/strong&gt; Cuộn cảm nhỏ dạng ống cũng có thể in vạch màu giống điện trở, nhưng khi đo bằng thang ohm, điện trở sẽ khớp gần trị số mã màu còn cuộn cảm (vốn là cuộn dây dẫn) chỉ đo được vài ôm hoặc thấp hơn rất nhiều. Nếu cắm nhầm cuộn cảm thay cho điện trở hạn dòng LED, dòng sẽ tăng vọt làm hỏng LED hoặc GPIO.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Tụ gốm và tụ tantali:&lt;/strong&gt; Tụ gốm dạng đĩa không phân cực, còn tụ tantali có phân cực và vạch trên nhiều tụ tantali lại đánh dấu &lt;strong&gt;cực dương&lt;/strong&gt; (ngược hoàn toàn với tụ hóa nhôm). Nếu không chắc chắn chủng loại, tuyệt đối không mắc ngang đường nguồn VM.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Transistor TO-92 và IC ổn áp TO-92:&lt;/strong&gt; Cả hai dùng chung dạng vỏ bán trụ ba chân nên phải dựa hoàn toàn vào mã in trên thân. Mã kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;78L05&lt;/code&gt; là IC ổn áp (vào – mass – ra), còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S8050&lt;/code&gt; hay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BC547&lt;/code&gt; là transistor (thứ tự chân E–B–C phải tra đúng datasheet). Linh kiện bị mờ mất mã phải để riêng và không được dùng cho mạch nguồn ESP32.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;ba-lỗi-thường-tạo-ra-số-đo-giả&quot;&gt;Ba lỗi thường tạo ra số đo giả&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Đo điện trở khi linh kiện còn gắn trong mạch:&lt;/strong&gt; Khi còn hàn hoặc cắm chung với mạch khác, đồng hồ sẽ đo điện trở tương đương của cả mạng nhánh song song (một điện trở 220 Ω tốt có thể chỉ đọc ra 80 Ω). Luôn nhấc ít nhất một chân ra khỏi mạch trước khi đo.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nhầm cực tính que đo ở chế độ diode:&lt;/strong&gt; Phần lớn đồng hồ số đặt cực dương ở que đỏ, nhưng một số mẫu hoặc thao tác cắm nhầm lỗ có thể làm đảo chiều. Hãy kiểm chứng một lần trên diode đã biết rõ vạch cathode: que nào chạm đầu không vạch mà hiện điện áp thuận thì que đó mang cực dương, rồi ghi chú ngay đầu bảng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đỏ = dương anode&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đỏ bị đảo&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đọc điện trở năm vạch như bốn vạch:&lt;/strong&gt; Điện trở 5 vạch gồm 3 chữ số có nghĩa + hệ số nhân + dung sai, còn loại 4 vạch chỉ có 2 chữ số có nghĩa + hệ số nhân + dung sai. Ví dụ nâu-đen-đen-đỏ-nâu khác hoàn toàn công thức với nâu-đen-đen-vàng. Nếu số đo lệch đúng 10 lần hoặc 100 lần, hãy đếm lại số vạch trước tiên.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;bảng-ghi-chép-bắt-buộc-trong-sổ-lab&quot;&gt;Bảng ghi chép bắt buộc trong sổ lab&lt;/h2&gt;

&lt;p&gt;Chép bảng sau vào sổ và điền đầy đủ kết quả thực tế (lưu với mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-10-id-table&lt;/code&gt;):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;#&lt;/th&gt;
      &lt;th&gt;Linh kiện&lt;/th&gt;
      &lt;th&gt;Quan sát ban đầu&lt;/th&gt;
      &lt;th&gt;Danh định hoặc cực tính&lt;/th&gt;
      &lt;th&gt;Kết quả đo&lt;/th&gt;
      &lt;th&gt;Đạt?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;Hàng năm lỗ&lt;/td&gt;
      &lt;td&gt;Ghi rõ cột/lỗ&lt;/td&gt;
      &lt;td&gt;Một nút chung&lt;/td&gt;
      &lt;td&gt;Kêu hoặc hở&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;Rãnh giữa&lt;/td&gt;
      &lt;td&gt;Đo qua khe&lt;/td&gt;
      &lt;td&gt;Hở mạch&lt;/td&gt;
      &lt;td&gt;Kêu hoặc hở&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;Thanh nguồn đỏ&lt;/td&gt;
      &lt;td&gt;Từ đầu đến cuối&lt;/td&gt;
      &lt;td&gt;Liền hoặc ngắt đôi&lt;/td&gt;
      &lt;td&gt;Ghi vị trí ngắt&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4–8&lt;/td&gt;
      &lt;td&gt;Năm điện trở&lt;/td&gt;
      &lt;td&gt;Dãy màu vạch&lt;/td&gt;
      &lt;td&gt;Trị số Ω và %&lt;/td&gt;
      &lt;td&gt;Số đo Ω&lt;/td&gt;
      &lt;td&gt;Có/Không&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt;LED&lt;/td&gt;
      &lt;td&gt;Chân anode&lt;/td&gt;
      &lt;td&gt;Dài/ngắn, mặt vát&lt;/td&gt;
      &lt;td&gt;Điện áp thuận (V)&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;Diode&lt;/td&gt;
      &lt;td&gt;Chân anode&lt;/td&gt;
      &lt;td&gt;Vạch là cathode&lt;/td&gt;
      &lt;td&gt;Điện áp thuận (V)&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;11&lt;/td&gt;
      &lt;td&gt;Tụ hóa&lt;/td&gt;
      &lt;td&gt;Vị trí vạch&lt;/td&gt;
      &lt;td&gt;Cực âm&lt;/td&gt;
      &lt;td&gt;Tên tệp ảnh&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Các dòng điện trở chỉ đạt khi có đủ cả giá trị giải mã và giá trị đo ngoài mạch; dòng LED chỉ đạt khi có điện áp thuận và xác định rõ chân anode.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ&quot;&gt;Ví dụ&lt;/h2&gt;

&lt;p&gt;Xét một điện trở có bốn vạch nâu, đen, đỏ, vàng kim: tương ứng 1, 0, hai chữ số 0 và dung sai 5%, tức \(1{,}0\,\mathrm{k}\Omega \pm 5\%\). Cửa sổ dung sai là:&lt;/p&gt;

\[1000 \times 0{,}95 = 950,\qquad 1000 \times 1{,}05 = 1050\]

&lt;p&gt;Đồng hồ đo được 1.027 Ω, nằm trong khoảng 950–1.050 Ω nên đạt. Với điện trở thứ hai mã 220 Ω, khi đo trực tiếp lúc còn cắm cạnh mạch LED trên breadboard chỉ ra 86 Ω; sau khi nhấc một chân ra khỏi mạch, đồng hồ đọc 218 Ω — nằm gọn trong khoảng \(220 \times 0{,}95\) đến \(220 \times 1{,}05\) nên đạt (giá trị 86 Ω ban đầu là do ảnh hưởng nhánh song song).&lt;/p&gt;

&lt;p&gt;Điện trở thứ ba có năm vạch nhưng bị đọc nhầm theo quy tắc bốn vạch ra 1.000 Ω trong khi đồng hồ đo khoảng 100 Ω; sau khi đếm lại đủ năm vạch (ba chữ số có nghĩa cộng hệ số nhân), kết quả hoàn toàn khớp.&lt;/p&gt;

&lt;p&gt;Trên diode đã biết, đầu có vạch là cathode nên đầu kia là anode; đặt que đỏ vào anode và que đen vào vạch cathode cho 0,62 V, đảo que hiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt;. Trên LED đỏ, đặt que đỏ vào chân dài cho 1,85 V kèm ánh sáng mờ, chứng tỏ chân dài đúng là anode. Hai mức điện áp thuận khác nhau rõ rệt (~0,6 V với diode thường và ~1,8 V với LED đỏ) cũng giúp khẳng định không bị ghi nhầm giữa hai linh kiện.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=G1mZutobxDU&quot;&gt;Resistor Color Code Chart Tutorial Review - Physics&lt;/a&gt; — The Organic Chemistry Tutor, tiếng Anh. Đọc mã màu rồi đối chiếu bằng thang ohm để xác nhận giá trị trước khi lắp mạch.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=FsBgW0WDQSw&quot;&gt;CÁCH SỬ DỤNG ĐỒNG HỒ VẠN NĂNG ĐO ĐIỆN ĐƠN GIẢN&lt;/a&gt; — Sơn Tiện Ích, tiếng Việt. Thực hành sử dụng thang ohm và thang thông mạch giúp phân biệt chính xác các linh kiện dễ nhầm lẫn.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một điện trở danh định 470 Ω (vạch cuối màu vàng kim) đo ngoài mạch được 502 Ω. Hãy tính khoảng dung sai 5% và đưa ra kết luận đạt hay loại.&lt;/li&gt;
  &lt;li&gt;Ở thang đo diode (đã xác nhận que đỏ mang cực dương), đồng hồ hiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; khi que đỏ chạm chân dài LED và hiện 1,9 V khi que đỏ chạm chân ngắn. Chân nào là anode, và kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; mang ý nghĩa gì?&lt;/li&gt;
  &lt;li&gt;Một điện trở 1 kΩ nằm trên mạch đã tắt nguồn nhưng đo được 120 Ω. Bước xử lý đầu tiên bạn cần làm là gì, và vì sao chưa vội kết luận “đọc sai mã màu”?&lt;/li&gt;
  &lt;li&gt;Một linh kiện có các vạch màu giống mã 10 kΩ nhưng đo ngoài mạch chỉ ra 1,2 Ω. Đó nhiều khả năng là linh kiện gì, và có được dùng làm điện trở hạn dòng cho LED 3,3 V không?&lt;/li&gt;
  &lt;li&gt;Bạn gặp một linh kiện vỏ TO-92 bị mờ mất chữ in. Hãy viết ghi chú phân loại cho nó và cho biết có thể dùng nó cho mạch nguồn ESP32 hay không.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
  &lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

  &lt;ol&gt;
    &lt;li&gt;Khoảng dung sai 5% là từ \(470 \times 0{,}95 = 446{,}5\,\Omega\) đến \(470 \times 1{,}05 = 493{,}5\,\Omega\). Giá trị 502 Ω nằm ngoài khoảng này nên trượt, cần tách riêng.&lt;/li&gt;
    &lt;li&gt;Chân ngắn là anode (vì đặt que đỏ dương vào đó cho sụt thuận 1,9 V). Kết quả &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; là trạng thái phân cực ngược bình thường, không có nghĩa LED hỏng; trường hợp này cho thấy quy ước chân dài đã không còn đúng và cần tin theo đồng hồ.&lt;/li&gt;
    &lt;li&gt;Nhấc ít nhất một chân điện trở ra khỏi mạch rồi đo lại, vì các nhánh song song trong mạch làm tổng trở đo được thấp hơn thực tế. Tắt nguồn là điều kiện cần nhưng chưa đủ khi đo trong mạch.&lt;/li&gt;
    &lt;li&gt;Nhiều khả năng đó là cuộn cảm dạng vạch màu (hoặc linh kiện đã hỏng ngắn mạch). Tuyệt đối không dùng làm điện trở hạn dòng LED vì trở kháng quá nhỏ sẽ gây quá dòng làm hỏng LED và GPIO.&lt;/li&gt;
    &lt;li&gt;Ghi chú: “Vỏ TO-92, mất mã in, không rõ là transistor hay IC ổn áp và không biết thứ tự chân.” Tuyệt đối không dùng để cấp nguồn cho ESP32 vì rất dễ gây ngắn mạch hoặc quá áp đường nguồn.&lt;/li&gt;
  &lt;/ol&gt;

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-use-a-multimeter&quot;&gt;SparkFun: dùng đồng hồ vạn năng&lt;/a&gt; — ôn lại ba chức năng dùng trong buổi lab: thông mạch, đo ohm và thang diode.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/resistors/resistor-color-code&quot;&gt;SparkFun: mã màu điện trở&lt;/a&gt; — bảng tra nhanh cho cả điện trở 4 vạch và 5 vạch.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;Datasheet ESP32 (PDF)&lt;/a&gt; — tài liệu tham chiếu về giới hạn 3,3 V từ bài 07.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Nếu đã chuẩn bị đủ bộ dụng cụ từ Chương 00 theo &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;, bạn không cần mua thêm gì ở bài này.&lt;/p&gt;

&lt;p&gt;Nếu còn thiếu dụng cụ đo cơ bản, bạn có thể tham khảo breadboard &lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;test board CAMMB-102 trên Hshop&lt;/a&gt; (giá tham khảo 35.000 ₫ ngày 23 tháng 9 năm 2026) và đồng hồ vạn năng &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UNI-T UT33D+ tại Hshop&lt;/a&gt; (xem giá hiện hành trên trang). Điện trở lẻ, LED, diode và tụ hóa có thể mua trực tiếp tại &lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; hoặc &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;, và nhớ ghi lại chính xác mã in trên diode nhận được.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>ESD, cực tính, cầu chì và sụt áp</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_09_esd_polarity_brownout/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_09_esd_polarity_brownout</id>
   <content type="html">&lt;p&gt;Ba sự cố phần cứng rất dễ bị đổ lỗi nhầm cho phần mềm “chạy thiếu ổn định”: phóng tĩnh điện làm hỏng chân IC ngay trước khi bạn nạp code; cắm ngược cực pin phá hủy mạch lái ngay lúc vừa chạm giắc; và động cơ kẹt trục kéo sụt đường nguồn 5 V khiến ESP32 liên tục khởi động lại. Bài học này tách bạch cả ba cơ chế và giúp bạn xây dựng một giả thuyết kỹ thuật rõ ràng để kiểm chứng khi lắp động cơ về sau.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ hình thành thói quen chống tĩnh điện đúng mức: chạm vào vật kim loại nối đất rồi cầm mạch ở phần cạnh viền. Bạn sẽ hiểu tác hại của việc đảo cực đối với tụ hóa và chân VM của TB6612, luôn đo cực tính bằng đồng hồ trước lần cấp nguồn đầu tiên thay vì tin theo màu dây. Bạn sẽ biết cách chọn cầu chì tự phục hồi hoặc cầu chì 2–3 A (cao hơn dòng chạy không tải của hai động cơ TT nhưng thấp hơn mức dòng gây cháy dây pin). Cuối cùng, bạn sẽ giải thích được hiện tượng sụt áp (brownout) khi dùng chung nguồn 5 V lúc kẹt trục và trình bày đủ biện pháp xử lý: dùng pin động cơ riêng, chỉ nối chung mass, lắp tụ 100–470 µF đúng cực ngang chân VM, dùng dây động cơ đủ tiết diện, nhấc bánh khỏi mặt bàn, và lập trình firmware đưa PWM về 0 khi nguồn sụt hoặc mất lệnh quá khoảng 300 ms.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã nắm rõ cách xác định cực tính tụ hóa (bài 04), tách biệt chân VM và VCC (bài 07) và tính toán cơ bản theo định luật Ohm. Hôm nay bạn chỉ đo đường nguồn USB — không quay bánh xe và chưa nối pin động cơ.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Ba “sát thủ thầm lặng” thường xuyên đánh gục các dự án robot sinh viên nhất không hề xuất hiện trong thông báo lỗi trình biên dịch: &lt;strong&gt;phóng tĩnh điện (ESD)&lt;/strong&gt; đánh thủng cổng MOSFET mà không để lại vết cháy nào, &lt;strong&gt;cắm ngược cực nguồn&lt;/strong&gt; phá hủy IC ổn áp trong tích tắc, và &lt;strong&gt;sụt áp (brownout)&lt;/strong&gt; khiến ESP32 liên tục tự khởi động lại đúng khoảnh khắc hai bánh xe vừa quay! Bài này trang bị cho bạn bộ giáp điện tử để chặn đứng cả ba sát thủ ấy.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bảo-vệ-mạch-điện-tử-khỏi-esd-ngược-cực-và-sụt-áp&quot;&gt;Kiến thức cần nhớ: Bảo vệ Mạch Điện tử khỏi ESD, Ngược cực và Sụt áp&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Phóng tĩnh điện (ESD — Electrostatic Discharge):&lt;/strong&gt; Khi bạn đi trên sàn nhựa hay kéo áo khoác len, cơ thể tích điện tới \(3.000\text{–}10.000\text{ V}\)! Tuy dòng ngắn nhưng điện trường cực lớn đủ đánh thủng lớp oxit cổng dày vài nanomet của chip CMOS — giải pháp đơn giản là luôn chạm tay vào khung kim loại tiếp đất trước khi cầm bo mạch và cầm vào mép PCB.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bảo vệ Ngược cực (Reverse Polarity Protection):&lt;/strong&gt; Một diode Schottky nối tiếp (mất \(0.3\text{ V}\)), cầu chì + diode song song, hoặc mạch P-MOSFET chống ngược cực sẽ cứu toàn bộ bo mạch khi bạn vô tình đấu nhầm dây đỏ–đen.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sụt áp (Brownout) &amp;amp; Tách đường Nguồn:&lt;/strong&gt; Khi động cơ DC khởi động, dòng tăng vọt gấp \(3\text{–}5\times\) dòng chạy đều; theo định luật Ohm \(\Delta V = I \cdot R_{\text{dây+pin}}\), điện áp đường nguồn lập tức sụt sâu. Nếu vi điều khiển dùng chung đường nguồn yếu đó và sụt xuống dưới ngưỡng &lt;strong&gt;Brownout Detector&lt;/strong&gt; (~\(2.43\text{ V}\) trên ESP32), chip sẽ tự reset lặp đi lặp lại (&lt;em&gt;boot loop&lt;/em&gt;)!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-sụt-áp-gây-brownout-reset-khi-động-cơ-khởi-động&quot;&gt;Ví dụ làm từng bước: Tính sụt áp gây Brownout Reset khi động cơ khởi động&lt;/h3&gt;

&lt;p&gt;Giả sử xe dùng nguồn 5 V (hoặc pin 2S yếu) đi qua một sợi dây jumper mảnh dài + tiếp điểm breadboard có tổng điện trở \(R_{\text{đường}} = 0.5\ \Omega\). Khi 2 động cơ cùng khởi động đột ngột, dòng đỉnh đạt \(I_{\text{đỉnh}} = 2.5\text{ A}\):&lt;/p&gt;

\[\Delta V_{\text{sụt}} = I_{\text{đỉnh}} \cdot R_{\text{đường}} = 2.5\text{ A} \times 0.5\ \Omega = \mathbf{1.25\text{ V}}!\]

&lt;ul&gt;
  &lt;li&gt;Trên đường nguồn \(5.0\text{ V}\) chung: điện áp tại chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; của ESP32 tụt ngay xuống \(5.0 - 1.25 = \mathbf{3.75\text{ V}}\).&lt;/li&gt;
  &lt;li&gt;Qua bộ ổn áp LDO AMS1117 (cần điện áp rơi tối thiểu \(V_{\text{dropout}} \approx 1.1\text{ V}\)): điện áp đầu ra đường 3,3 V tụt xuống chỉ còn \(3.75 - 1.1 = \mathbf{2.65\text{ V}}\) (và chỉ cần thêm một gai nhiễu nhỏ \(0.25\text{ V}\) là chạm ngưỡng brownout \(2.43\text{ V}\) → &lt;strong&gt;ESP32 lập tức reset!&lt;/strong&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cách khắc phục bằng con số:&lt;/strong&gt; thay dây động cơ bằng dây 20 AWG hàn chắc (\(R_{\text{đường}} \approx 0.05\ \Omega\)) và đi nhánh sao (&lt;em&gt;star topology&lt;/em&gt;) tách riêng đường pin cấp cho động cơ khỏi nhánh Buck cấp cho ESP32 → độ sụt giảm &lt;strong&gt;10 lần&lt;/strong&gt; xuống chỉ còn \(0.125\text{ V}\)!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-tuyến-phòng-thủ-chống-hỏng-mạch-trên-xe-robot&quot;&gt;Giải thích: 4 tuyến phòng thủ chống hỏng mạch trên xe robot&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Phòng ESD bằng thói quen cầm mép bo:&lt;/strong&gt; Luôn chạm tay vào vật kim loại lớn trước khi chạm vào ESP32/IMU; cất bo mạch trong túi chống tĩnh điện khi không dùng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Chống ngược cực bằng cơ khí trước, điện tử sau:&lt;/strong&gt; Dùng giắc có ngàm định hướng (JST/XT30) kết hợp màu dây chuẩn Đỏ/Đen để không thể cắm ngược ngay từ thao tác tay.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Luôn đi nguồn hình sao (Star Power Distribution):&lt;/strong&gt; Tách hai nhánh dây riêng từ cực pin (sau công tắc &amp;amp; cầu chì): Nhánh 1 chạy thẳng tới Driver động cơ; Nhánh 2 chạy qua mạch Buck 5 V + tụ lọc tới ESP32.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Đọc thông báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Brownout detector was triggered&lt;/code&gt; trên Serial Monitor:&lt;/strong&gt; Khi thấy dòng chữ này trên ESP32, &lt;strong&gt;đừng&lt;/strong&gt; tắt bộ phát hiện brownout bằng phần mềm — hãy sửa ngay đường nguồn và dây dẫn!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu bạn thấy ESP32 chạy code nhấp LED bình thường khi cắm USB, nhưng cứ gửi lệnh cho động cơ quay ở tốc độ 100% là Serial Monitor in lại dòng khởi động &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rst:0x1 (POWERON_RESET)&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Brownout detector was triggered&lt;/code&gt;, lỗi nằm ở code hay ở phần cứng nguồn? &lt;strong&gt;100% ở phần cứng nguồn&lt;/strong&gt; — dòng khởi động động cơ đang kéo tụt đường nguồn nuôi ESP32!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Xe Capstone gồm vi điều khiển 3,3 V (ESP32 hoặc Pico), một mạch lái TB6612, hai động cơ TT và một cảm biến HC-SR04. Khi vận hành thực tế, hiện tượng kẹt bánh (va chạm vướng vật cản hoặc bị giữ bánh) chắc chắn sẽ xảy ra. Nếu chân VM đang nối chung vào đường 5 V nuôi vi điều khiển, dòng kẹt sẽ kéo tụt đường 5 V, làm nguồn 3,3 V sụp theo và khiến chip tự reset giữa chừng — biểu hiện bên ngoài rất giống lỗi phần mềm. Một dạng lỗi âm thầm khác là đưa thẳng chân echo 5 V của HC-SR04 vào chân GPIO (đã cấm ở bài 07). Cả hai đều là lỗi phần cứng mà việc sửa mã nguồn không thể khắc phục; tách riêng nguồn động cơ và chỉ nối chung một điểm mass là nền tảng để hệ thống hoạt động ổn định và dễ chẩn đoán.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_rails_3v3.png&quot; alt=&quot;Rail logic đứng cạnh rail mô-tơ&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hai hệ thống nguồn chỉ gặp nhau duy nhất tại điểm mass (GND): đường 3,3 V nuôi vi điều khiển và chân logic của mạch lái, còn nguồn động cơ nuôi chân VM. Việc nối chung nguồn USB vào đường động cơ chắc chắn dẫn đến sụt áp. Cảm biến HC-SR04 được cấp nguồn 5 V riêng cho mạch nuôi, nhưng chân echo vẫn tuyệt đối không được nối thẳng vào GPIO.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Thứ tự cấp nguồn, bánh còn nhấc khỏi bàn&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Trong buổi thực hành hôm nay, chỉ cấp nguồn logic qua USB và giữ hoàn toàn ngắt phần động cơ. Ở giai đoạn vận hành sau này, chân STBY phải giữ ở trạng thái chờ cho tới khi cả hai nguồn đã ổn định và chương trình sẵn sàng.&lt;/p&gt;

&lt;h2 id=&quot;phóng-tĩnh-điện-esd-ở-mức-thực-tế&quot;&gt;Phóng tĩnh điện (ESD) ở mức thực tế&lt;/h2&gt;

&lt;p&gt;Phóng tĩnh điện (ESD) có thể tạo ra xung điện áp lớn dù bạn không nhìn thấy tia lửa; chỉ cần bước trên thảm rồi chạm trực tiếp vào chân mạch Pico trần hay cực cổng MOSFET cũng đủ đánh thủng lớp ôxít bên trong IC. Thói quen phòng tránh rất đơn giản và hiệu quả: chạm tay vào một bề mặt kim loại nối đất (như vỏ thùng máy tính đang cắm điện) trước khi thao tác, luôn cầm mạch ở mép cạnh thay vì chạm vào linh kiện, giữ mạch trong túi chống tĩnh điện khi chưa dùng và không chuyền tay mạch trần ngang phòng. Các module hàn sẵn tuy bền hơn transistor rời nhưng vẫn cần tuân thủ thói quen này. Vòng đeo tay chống tĩnh điện là trang bị bổ sung tốt, nhưng không phải điều kiện bắt buộc cho bài lab này.&lt;/p&gt;

&lt;h2 id=&quot;kiểm-soát-cực-tính&quot;&gt;Kiểm soát cực tính&lt;/h2&gt;

&lt;p&gt;Cấp ngược cực cho tụ hóa nhôm sẽ làm tụ nóng lên nhanh chóng cho đến khi xì bung vỏ, thậm chí làm hỏng đường mạch đồng xung quanh; cấp ngược cực vào chân VM cũng phá hủy mạch lái bên trong. Luôn nhớ: dải vạch trên vỏ tụ hóa là &lt;strong&gt;cực âm&lt;/strong&gt; nối về mass; cực dương pin nối vào chân &lt;strong&gt;VM&lt;/strong&gt;, tuyệt đối không nối vào GPIO.&lt;/p&gt;

&lt;p&gt;Về lâu dài, bạn có thể lắp thêm diode Schottky nối tiếp trên dây dương hoặc dùng giắc chống cắm ngược như XT30 (lưu ý diode Schottky sẽ làm sụt vài phần mười vôn ở dòng động cơ nên cần cân nhắc trước khi lắp). Còn ngay ở bước hiện tại, quy tắc bắt buộc là đo điện áp pin bằng đồng hồ trước khi nối vào mạch lái: đặt que đỏ vào dây bạn cho là cực dương, que đen vào dây còn lại — nếu màn hình hiện số dương thì đúng, nếu hiện dấu trừ thì dừng ngay. Tuyệt đối không đoán cực chỉ dựa vào màu vỏ dây.&lt;/p&gt;

&lt;p&gt;Cảm biến HC-SR04 cũng có cực tính nguồn: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; nhận 5 V, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Trig&lt;/code&gt; là ngõ vào, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; là ngõ ra 5 V. Đảo ngược &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; sẽ làm hỏng cảm biến, còn cắm thẳng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; vào GPIO vi phạm giới hạn bài 07 — hãy luôn kiểm tra lại cả hai điểm này trên tờ sơ đồ chân trước khi cấp điện.&lt;/p&gt;

&lt;h2 id=&quot;vì-sao-cổng-usb-không-thể-nuôi-động-cơ&quot;&gt;Vì sao cổng USB không thể nuôi động cơ&lt;/h2&gt;

&lt;p&gt;Theo thông số động cơ TT 1:48 dùng trong khóa học, dòng chạy không tải của mỗi động cơ nằm trong khoảng 110–150 mA. Khi cả hai bánh quay trên không, tổng dòng tiêu thụ khoảng:&lt;/p&gt;

\[2 \times 130\,\mathrm{mA} \approx 0{,}26\,\mathrm{A}\]

&lt;p&gt;Làm tròn khoảng \(0{,}3\,\mathrm{A}\). Vì cổng USB tiêu chuẩn cấp khoảng 500 mA, lúc thử không tải nhìn bề ngoài có vẻ vẫn còn dư:&lt;/p&gt;

\[0{,}50 - 0{,}30 = 0{,}20\,\mathrm{A}\]

&lt;p&gt;Tuy nhiên đây là khoảng dư ảo. Dòng kẹt trục thực tế lớn gấp nhiều lần dòng không tải — chỉ riêng một động cơ TT bị kẹt đã đòi hỏi dòng vượt quá giới hạn 500 mA của cổng USB. Do đó, USB không bao giờ được dùng làm nguồn cho động cơ. Mức 1,2 A liên tục của TB6612 chỉ là giới hạn chịu dòng của chip chứ không phải nguồn tự cấp dòng; nguồn cung cấp dòng bắt buộc phải là pin nối vào chân VM.&lt;/p&gt;

&lt;h2 id=&quot;cơ-chế-sụt-áp-brownout-và-giải-pháp-thiết-kế&quot;&gt;Cơ chế sụt áp (brownout) và giải pháp thiết kế&lt;/h2&gt;

&lt;p&gt;Đường nguồn 5 V dùng chung luôn tồn tại điện trở ký sinh trên cáp và đầu cắm. Khi dòng kẹt tăng vọt, độ sụt áp trên đường dây được tính bằng:&lt;/p&gt;

\[\Delta V = I_{\mathrm{ket}} \times R_{\mathrm{duong}}\]

&lt;p&gt;Khi đường 5 V bị kéo tụt, mạch ổn áp 3,3 V mất khoảng chênh áp làm việc (dropout), dẫn đến ESP32 hoặc Pico bị reset ngay giữa lệnh điều khiển. Nếu thấy thông báo khởi động (boot banner) bất ngờ xuất hiện trên Serial Monitor ngay lúc xe nhận lệnh chạy, đó chính là dấu hiệu điển hình của sụt áp.&lt;/p&gt;

&lt;p&gt;Biện pháp khắc phục đồng bộ về phần cứng gồm bốn điểm:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tách nguồn riêng:&lt;/strong&gt; cấp nguồn động cơ bằng pin riêng (trong dải VM 4,5–10 V của bài 07), còn mạch logic tạm cấp qua USB. Hai nguồn chỉ nối chung đúng một điểm mass (và không để dòng động cơ chạy dọc theo thanh nguồn breadboard mảnh).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lắp tụ đệm gần mạch lái:&lt;/strong&gt; mắc một tụ hóa nhôm 100–470 µF (định mức 16 V hoặc 25 V, lớn hơn trần 10 V của VM) ngay sát chân VM và GND của driver, đúng chiều vạch âm về mass. Tụ giúp bù sườn dòng đột ngột khi động cơ khởi động hoặc kẹt.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dây dẫn và an toàn cơ khí:&lt;/strong&gt; dùng dây động cơ đủ tiết diện và luôn nhấc bánh xe khỏi mặt bàn khi chưa chủ định cho xe chạy (đặt bánh chạm bàn trong lúc thử nghiệm rất dễ tự tạo tình huống kẹt tải).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bảo vệ bằng firmware:&lt;/strong&gt; lập trình đưa duty PWM về 0 nếu phát hiện nguồn sụt hoặc khi mất lệnh điều khiển quá khoảng 300 ms, đồng thời có thể hạ chân STBY về trạng thái chờ để đảm bảo dừng hẳn khi bên gửi mất kết nối.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;chọn-cầu-chì-bảo-vệ&quot;&gt;Chọn cầu chì bảo vệ&lt;/h2&gt;

&lt;p&gt;Lắp một cầu chì thường hoặc cầu chì tự phục hồi (resettable fuse) định mức &lt;strong&gt;2–3 A&lt;/strong&gt; trên dây dương sát cực pin là biện pháp bảo vệ đơn giản và rẻ tiền. Mức 2–3 A đủ cao hơn dòng chạy bình thường (khoảng \(0{,}3\,\mathrm{A}\)) để không ngắt nhầm khi xe đang chạy, nhưng đủ thấp hơn dòng ngắn mạch hàng chục ampe có thể làm cháy dây pin. Chọn cầu chì dưới \(0{,}3\,\mathrm{A}\) sẽ khiến mạch ngắt liên tục. Lưu ý rằng cầu chì chỉ bảo vệ quá dòng/ngắn mạch, không thay thế việc tách pin riêng và không chống cắm ngược cực, nên vẫn phải đo cực tính trước khi đấu nối.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành&quot;&gt;Thực hành&lt;/h2&gt;

&lt;p&gt;Thực hiện bài lab khi &lt;strong&gt;chỉ cắm USB&lt;/strong&gt;, đã rút hoàn toàn động cơ và pin (nếu bánh đã lắp trên trục vẫn phải giữ nhấc khỏi mặt bàn).&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Chạm tay vào bề mặt kim loại nối đất rồi cầm mạch vi điều khiển bằng phần cạnh viền; ghi nhận đã thực hiện vào sổ.&lt;/li&gt;
  &lt;li&gt;Cắm duy nhất cáp USB vào mạch. Dùng đồng hồ ở thang DC V (que đen đặt tại mass) đo thực tế điện áp tại chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; và chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;, ghi lại chính xác con số đo được (không ghi chung chung là “ổn”).&lt;/li&gt;
  &lt;li&gt;Mở lại tờ sơ đồ chân bài 07 (hoặc phác nhanh bốn chân cảm biến), khoanh tròn chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; của HC-SR04 và ghi rõ chú thích cấm nối thẳng ngõ ra 5 V này vào chân GPIO.&lt;/li&gt;
  &lt;li&gt;Chép và điền số đo thực tế của bạn vào khung giả thuyết sau cho Chương 02: &lt;em&gt;“Khi chỉ cấp nguồn USB, board đo được ___ V tại chân 5 V và ___ V tại chân 3,3 V. Nếu về sau mạch bị reset khi động cơ TT dùng chung đường 5 V này, giả thuyết là dòng kẹt kéo đường nguồn rơi vào vùng sụt áp (brownout). Biện pháp xử lý: tách pin động cơ riêng, chỉ nối chung mass, lắp tụ 100–470 µF đúng cực (vạch âm về mass) ngang chân VM, dùng dây động cơ đủ dày, nhấc bánh khỏi bàn, lập trình kéo PWM về 0 khi nguồn sụt hoặc mất lệnh quá khoảng 300 ms, và lắp cầu chì 2–3 A trên dây dương pin.”&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;Không gắn động cơ để thử nghiệm đoạn giả thuyết này. Bài lab đạt khi có đủ hai con số đo áp và đoạn giả thuyết đã hoàn thiện, lưu trong sổ với mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-09-brownout&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nếu ngay khi chỉ cắm USB mà chân 5 V đã lệch xa mức 5 V, hãy dừng lại và ghi chú ngay — đó là dấu hiệu bất thường của cáp hoặc board cần xử lý trước khi làm tiếp.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ&quot;&gt;Ví dụ&lt;/h2&gt;

&lt;p&gt;Với hai động cơ TT 1:48 chạy không tải ở mức trung bình:&lt;/p&gt;

\[I_{\mathrm{chay}} = 2 \times 0{,}130 = 0{,}26\,\mathrm{A}\]

&lt;p&gt;So với định mức cổng USB \(0{,}50\,\mathrm{A}\), phần dư khoảng \(0{,}24\,\mathrm{A}\) khi bánh nhấc trên không rất dễ tạo cảm giác an toàn giả tạo. Khi xảy ra kẹt trục, dòng kẹt \(\gg 0{,}3\,\mathrm{A}\) và chỉ một động cơ kẹt đã vượt quá khả năng cổng 500 mA. Sơ đồ nguồn đúng phải là: USB cấp cho ESP32/Pico và nguồn logic 3,3 V của TB6612; pin riêng (4,5–10 V) cấp vào chân VM; nối chung một điểm mass; mắc tụ 220 µF hoặc 470 µF (vạch âm về mass) sát chân VM; gắn cầu chì 2 A hoặc 3 A trên dây dương pin; và firmware đưa PWM về 0 nếu mất lệnh quá khoảng 300 ms.&lt;/p&gt;

&lt;p&gt;Lưu ý không nên chọn cầu chì 0,5 A với hy vọng “bảo vệ kẹt trục”, vì mức đó quá sát dòng vận hành sẽ ngắt liên tục khiến người dùng có xu hướng nối tắt bỏ qua cầu chì. Hãy chọn cầu chì 2–3 A để chống ngắn mạch, còn việc bảo vệ kẹt trục xử lý bằng cơ chế timeout trong phần mềm.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [What is ESD and How to Prevent it – ATM&lt;/td&gt;
          &lt;td&gt;Digi-Key Electronics](https://www.youtube.com/watch?v=pWhtyz-UFZc) — DigiKey, tiếng Anh. Giải thích tác hại của phóng tĩnh điện lên chân IC và thói quen xả tĩnh điện trước khi cầm mạch.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=W770Z1yvXs8&quot;&gt;What is a fuse? the basics explained&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Minh họa nguyên lý ngắt mạch bảo vệ quá dòng của cầu chì.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=sF0KOVWj9p8&quot;&gt;The Best Protection for your Circuit is NOT a Fuse!…..but a Resettable Fuse? EB#54&lt;/a&gt; — GreatScott!, tiếng Anh. So sánh cầu chì thường với cầu chì tự phục hồi; lưu ý vẫn phải tính chọn dòng phù hợp theo bài học.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Giả sử hai động cơ TT có dòng không tải 150 mA mỗi chiếc trên cổng USB 500 mA. Tính tổng dòng khi chạy không tải và phần dòng còn dư. Vì sao phần dư này không cho phép dùng USB nuôi chân VM khi bánh chạm sàn?&lt;/li&gt;
  &lt;li&gt;Khi chỉ cắm USB, chân 5 V đo được 4,95 V và chân 3,3 V đo được 3,28 V. Sang Chương 02, mỗi khi nối hai động cơ vào chính chân 5 V đó thì Serial Monitor lại in ra dòng chữ khởi động của mạch. Hãy viết một câu giả thuyết nguyên nhân và nêu cách thay đổi nguồn để kiểm chứng.&lt;/li&gt;
  &lt;li&gt;Một tụ hóa 470 µF mắc ngang chân VM bị cắm ngược với dải vạch hướng về phía cực dương (xa mass). Hậu quả gì có thể xảy ra và cần sửa như thế nào?&lt;/li&gt;
  &lt;li&gt;Trong lúc đang lái xe tiến, laptop chuyển sang chế độ ngủ khiến lệnh điều khiển ngừng gửi. Firmware cần làm gì sau khoảng 300 ms, và vì sao cầu chì 3 A không thay thế được đoạn mã này?&lt;/li&gt;
  &lt;li&gt;Một người nối chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; của HC-SR04 vào chân GP của Pico vì thấy “cảm biến chạy 5 V và Pico cũng có chân 5 V ngay bên cạnh”. Giới hạn nào ở bài 07 cấm kết nối này, và bước kiểm tra nào giúp phát hiện sớm lỗi đảo ngược &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; của cảm biến?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
  &lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

  &lt;ol&gt;
    &lt;li&gt;Tổng dòng không tải là \(2 \times 0{,}150 = 0{,}30\,\mathrm{A}\), còn dư khoảng \(0{,}20\,\mathrm{A}\) so với cổng 500 mA. Khi bánh chạm sàn hoặc kẹt trục, dòng tăng gấp nhiều lần và vượt xa phần dư này nên tuyệt đối không lấy USB cấp cho VM.&lt;/li&gt;
    &lt;li&gt;Giả thuyết: dòng khởi động/kẹt của động cơ kéo sụt đường nguồn 5 V dùng chung khiến đường 3,3 V tụt theo vào vùng reset (brownout). Kiểm chứng bằng cách chuyển động cơ sang nguồn pin riêng, giữ chung một điểm mass và chạy lại đúng lệnh đó xem thông báo khởi động có biến mất không.&lt;/li&gt;
    &lt;li&gt;Tụ hóa nhôm cắm ngược cực trên nguồn pin sẽ nóng mạnh và có nguy cơ xì bung hoặc nổ. Vạch in trên vỏ là cực âm và bắt buộc nối về mass; phải đảo đúng chiều tụ trước khi cấp nguồn.&lt;/li&gt;
    &lt;li&gt;Firmware phải tự động đưa duty PWM về 0 (và có thể hạ chân STBY) sau khoảng 300 ms không nhận lệnh mới. Cầu chì 3 A chỉ ngắt khi ngắn mạch hoặc quá dòng rất lớn chứ không ngắt ở dòng chạy bình thường, nên không thể tự dừng bánh xe.&lt;/li&gt;
    &lt;li&gt;Giới hạn điện áp cực đại tuyệt đối của chân GPIO (nằm ở mức 3,3 V chứ không phải 5 V) cấm nối thẳng chân Echo vào chân GP; chân 5 V bên cạnh là chân nguồn chứ không phải ngõ vào logic. Việc đo cực tính và kiểm tra điện áp giữa VCC và GND bằng đồng hồ trước khi cắm cảm biến sẽ phát hiện ngay lỗi đảo nguồn.&lt;/li&gt;
  &lt;/ol&gt;

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;SparkFun: mức logic&lt;/a&gt; — giải thích dễ hiểu sự chênh lệch giữa ngõ ra 5 V và ngõ vào 3,3 V.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://batteryuniversity.com/article/bu-409-charging-lithium-ion&quot;&gt;Battery University, BU-409&lt;/a&gt; — nắm vững quy tắc sạc pin lithium trước khi cấp nguồn cho chân VM.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Module TB6612FNG của Pololu&lt;/a&gt; — minh họa kiến trúc tách biệt nguồn logic và nguồn động cơ giúp chống sụt áp.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Pin động cơ, động cơ TT, mạch lái và cảm biến HC-SR04 đều nằm trong danh sách chung ở &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Nếu chưa có đồng hồ vạn năng để đo đường nguồn USB, bạn có thể tham khảo &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UNI-T UT33D+ tại Hshop&lt;/a&gt; (kiểm tra giá hiện hành trước khi mua). Khi tới bước lắp tụ đệm VM, bạn có thể mua tụ hóa 100–470 µF (định mức 16 V hoặc 25 V) tại &lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; hoặc &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;, và nhớ kiểm tra kỹ điện áp định mức cùng vạch cực âm trước khi hàn.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Hàn dây và header cho robot</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_08_soldering_basics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_08_soldering_basics</id>
   <content type="html">&lt;p&gt;Trong phạm vi khóa học này, một mối hàn đạt yêu cầu là sợi dây động cơ vẫn dẫn điện tốt và ổn định sau khi khung xe rung lắc trong quá trình chạy. Bạn sẽ học cách tráng thiếc dây lõi bện, hàn vào chân header hoặc pad mạch thử, rồi bọc bảo vệ để lực uốn rơi vào phần vỏ dây chứ không dồn lên mối thiếc. Một mối hàn trông bóng bẩy nhưng nứt gãy ngay lần chạy thử đầu tiên vẫn là mối hàn hỏng.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ thực hiện đúng trình tự hàn chuẩn: làm nóng đồng thời pad và chân linh kiện, đưa thiếc vào mối nóng, rút dây thiếc trước rồi mới rút mũi mỏ hàn. Bạn sẽ nhận diện được mối hàn đạt chuẩn (hình nón lõm, bề mặt bóng, bám ướt đều cả hai mặt) và phân loại các lỗi thường gặp (mối tròn thành cục, mối nguội xỉn sần, cầu thiếc, bong pad đồng và cháy vỏ dây). Bạn sẽ giải thích được lý do hợp kim thiếc chì 60/40 hoặc Sn63 phù hợp cho thao tác hàn cơ bản (kèm thông gió tốt) cùng vai trò thực sự của nhựa thông (flux). Bạn cũng sẽ biết cách bọc gen co nhiệt, tạo vòng giảm lực kéo cho dây động cơ TT, tránh tráng thiếc vào đầu bấm Dupont, và kiểm tra mối hàn tập bằng thang thông mạch kèm ảnh chụp đối chiếu.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn đã biết sử dụng thang thông mạch từ bài 02 và phân biệt được hai đầu của thanh header. Buổi thực hành này chỉ dùng dây và mạch thử bỏ đi (có thể đặt tờ sơ đồ chân bài 07 bên cạnh để tham khảo), tuyệt đối không đặt khung xe hay pin lithium lên bàn. Hãy đọc kỹ mục An toàn trước khi cắm điện mỏ hàn.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Dây cắm breadboard rất tuyệt để thử nghiệm trong 10 phút, nhưng ngay khi xe robot chạy qua khe gạch trên sàn nhà, rung động cơ học sẽ làm dây tiếp xúc chập chờn và tạo ra những lỗi ma quái không thể tái hiện! &lt;strong&gt;Kỹ năng hàn thiếc (Soldering)&lt;/strong&gt; tạo ra liên kết luyện kim bền vững giữa dây đồng và pad mạch in — và bí mật của một mối hàn bóng đẹp nằm ở &lt;strong&gt;truyền nhiệt đúng thứ tự&lt;/strong&gt;, chứ không phải bôi thật nhiều thiếc.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-vật-lý-luyện-kim-của-mối-hàn-điện-tử&quot;&gt;Kiến thức cần nhớ: Vật lý Luyện kim của Mối hàn Điện tử&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cơ chế Liên kết Luyện kim (Intermetallic Bond):&lt;/strong&gt; Thiếc hàn không phải là “keo dán” — khi cả chân linh kiện và pad đồng đạt nhiệt độ nóng chảy (~\(183^\circ\text{C}\) với thiếc chì 63/37 hoặc ~\(217^\circ\text{C}\) với thiếc không chì SAC305), hợp kim thiếc hòa tan bề mặt đồng tạo thành lớp nối điện–cơ bền chắc.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vai trò của Nhựa thông (Flux):&lt;/strong&gt; Bề mặt đồng luôn có lớp oxit ngăn thiếc bám dính. Lõi nhựa thông (flux) bên trong cuộn thiếc bay hơi và khử lớp oxit ngay tại khoảnh khắc nung nóng — giữ mỏ hàn quá lâu làm cháy hết flux sẽ tạo ra &lt;strong&gt;mối hàn nguội&lt;/strong&gt; xỉn màu, nứt gãy.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Giảm lực kéo cơ học (Strain Relief):&lt;/strong&gt; Dây đồng nhiều sợi rất dễ gãy ngay sát chân mối hàn cứng khi bị uốn lặp đi lặp lại; ống co nhiệt (heat-shrink) và kẹp cố định dây bảo vệ mối hàn sống sót qua hàng trăm giờ chạy xe.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-sụt-áp-và-tỏa-nhiệt-trên-một-mối-hàn-nguội-cold-joint&quot;&gt;Ví dụ làm từng bước: Tính sụt áp và tỏa nhiệt trên một mối hàn nguội (Cold Joint)&lt;/h3&gt;

&lt;p&gt;Giả sử dây nguồn cấp cho cặp động cơ robot kéo dòng \(I = 2.0\text{ A}\) đi qua một mối hàn nối dây:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Mối hàn đạt chuẩn (bóng mượt, hình nón lõm):&lt;/strong&gt; điện trở tiếp xúc chỉ \(R_{\text{tốt}} = 0.01\ \Omega\):
    &lt;ul&gt;
      &lt;li&gt;Sụt áp: \(\Delta V = 2.0 \times 0.01 = \mathbf{0.02\text{ V}}\) (không đáng kể).&lt;/li&gt;
      &lt;li&gt;Công suất nhiệt: \(P = 2.0^2 \times 0.01 = \mathbf{0.04\text{ W}}\) (hoàn toàn mát lạnh).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mối hàn nguội / dính oxit (xỉn, vón cục):&lt;/strong&gt; điện trở tiếp xúc tăng vọt lên \(R_{\text{nguội}} = 0.8\ \Omega\):
    &lt;ul&gt;
      &lt;li&gt;Sụt áp: \(\Delta V = 2.0 \times 0.8 = \mathbf{1.6\text{ V}}\) (xe đột ngột mất lực hoặc reset!).&lt;/li&gt;
      &lt;li&gt;Công suất nhiệt dồn tại đúng hạt thiếc nhỏ: \(P = 2.0^2 \times 0.8 = \mathbf{3.2\text{ W}}\) — đủ làm chảy vỏ nhựa và bong mạch đồng!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-quy-trình-4-bước-tạo-mối-hàn-chuẩn-công-nghiệp&quot;&gt;Giải thích: Quy trình 4 bước tạo mối hàn chuẩn công nghiệp&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Lau sạch mũi hàn và tráng một lớp thiếc mỏng (tinning):&lt;/strong&gt; Mũi hàn đen bị oxy hóa không truyền nhiệt được; một giọt thiếc tươi trên đầu mũi giúp truyền nhiệt sang pad nhanh gấp nhiều lần.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Chạm mũi hàn vào &lt;em&gt;cả&lt;/em&gt; pad đồng và chân linh kiện (1–2 giây):&lt;/strong&gt; Làm nóng bề mặt cần hàn trước, tuyệt đối không nhỏ thiếc lên đầu mũi hàn rồi quệt sang!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Đưa sợi thiếc vào phía đối diện của chân linh kiện:&lt;/strong&gt; Thiếc sẽ tự chảy mượt bao quanh chân thành hình chóp núi lửa lõm (concave fillet); rút dây thiếc trước, rồi mới nhấc mũi hàn ra.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Giữ bất động 2 giây và kiểm tra bằng mắt + lực kéo nhẹ:&lt;/strong&gt; Mối hàn tốt phải bóng, mượt, không có gai nhọn và chịu được lực kéo nhẹ bằng tay không bong.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu bạn thấy mối hàn bị vón thành hình quả cầu tròn ngồi lên trên pad đồng (chân linh kiện không dính xuống pad), lỗi xảy ra ở bước nào? Bạn đã chỉ làm nóng chân linh kiện (hoặc nhỏ thiếc lên mũi hàn) mà &lt;strong&gt;chưa làm nóng đủ pad đồng&lt;/strong&gt; — chỉ cần thêm chút nhựa thông và chạm mũi hàn đều vào cả pad lẫn chân trong 2 giây, thiếc sẽ tự kéo mượt xuống pad!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Xe Capstone điều khiển hai động cơ TT qua TB6612 bằng nguồn pin riêng. Các sợi dây nối ra cặp chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO1&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO2&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BO1&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BO2&lt;/code&gt; thường xuyên bị uốn cong tại các điểm cố định trên khung. Một mối hàn nguội vẫn có thể kêu bíp khi đo thông mạch không tải, nhưng lại gây sụt áp đáng kể khi bánh xe kẹt, khiến xe chạy lệch hướng dù hai bánh nhận cùng mức PWM. Tương tự, ở cụm dây cảm biến HC-SR04, mối hàn kém chất lượng khiến dây echo 5 V chạm sang chân 3,3 V bên cạnh sẽ gây hỏng nặng hơn cả đứt dây. Dây Dupont chỉ dùng trên breadboard, còn đường động cơ cần mối hàn chuẩn, bọc gen co nhiệt và có vòng dây chùng giảm lực kéo.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Soldering_iron.jpg&quot; alt=&quot;Mỏ cắm điện phải có đế ngay khi nóng&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Bức ảnh trên minh họa phần dụng cụ chứ chưa phải thao tác chuẩn. Do mỏ hàn 60 W phổ thông không có núm chỉnh nhiệt độ, bạn kiểm soát nhiệt lượng bằng chính thời gian tiếp xúc giữa mũi mỏ và đồng. Giá đỡ mỏ hàn giúp giữ mũi nóng tách biệt khỏi sổ ghi chép, dây điện và tay bạn mỗi khi cần lấy thiếc.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/solder_joint_compare.png&quot; alt=&quot;Mối lõm bóng đặt cạnh cục thiếc&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy dùng hình trên làm chuẩn đối chiếu cho bài lab: mối hàn đạt (bên tốt) có dạng hình nón hơi lõm, bề mặt sáng bóng với thiếc chì, bám ướt đều cả pad lẫn chân và vẫn thấy rõ dáng chân linh kiện; mối loại là dạng giọt cầu tròn hoặc khối đục sần. Lưu ý rằng tiếng bíp thông mạch không thể biến một mối hàn xấu thành mối đạt.&lt;/p&gt;

&lt;h2 id=&quot;quy-trình-hàn-làm-nóng-đưa-thiếc-rồi-rút-ra&quot;&gt;Quy trình hàn: làm nóng, đưa thiếc rồi rút ra&lt;/h2&gt;

&lt;p&gt;Trước hết, làm sạch mũi mỏ hàn đến khi sáng rồi tráng một lớp thiếc mỏng lên đầu mũi; lớp thiếc này tạo cầu dẫn nhiệt vào mối hàn (mũi bị đen ôxy hóa chỉ làm cháy flux chứ không truyền nhiệt tốt). Vì mỏ hàn 60 W cắm trực tiếp nóng hơn mức cần thiết của pad header, thời gian chạm mũi là yếu tố quyết định — thông thường chỉ từ 1 đến 3 giây; tì mũi mỏ quá lâu (chẳng hạn 10 giây) sẽ làm bong tróc lớp đồng pad nhỏ.&lt;/p&gt;

&lt;p&gt;Đặt mũi mỏ hàn chạm đồng thời cả pad đồng và chân linh kiện để cả hai nóng lên cùng lúc. Đưa dây thiếc vào góc tiếp giáp giữa pad và chân (không lấy sẵn một giọt thiếc trên mũi mỏ rồi chấm sang). Thiếc phải chảy ra nhờ sức nóng từ pad và chân đồng; nếu thiếc chỉ chảy khi chạm vào mũi mỏ thì pad vẫn còn nguội. Khi thiếc đã chảy đều bám ướt cả hai mặt, &lt;strong&gt;rút dây thiếc ra trước&lt;/strong&gt;, sau đó mới &lt;strong&gt;rút mỏ hàn ra&lt;/strong&gt;. Không thổi hơi để làm nguội nhanh và giữ tuyệt đối cố định dây trong vài giây cho tới khi thiếc đông hoàn toàn — mọi rung lắc lúc thiếc đang đông đều tạo vết nứt ngầm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nhựa thông (flux)&lt;/strong&gt; giúp làm sạch lớp ôxy hóa để thiếc loang đều thay vì vo tròn. Dây thiếc lõi nhựa thông đã tích hợp sẵn một phần, nhưng với pad bị xỉn màu, bạn có thể chấm thêm một chút mỡ hàn. Với sản phẩm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIFE RL-426A&lt;/code&gt; ở mục mua hàng bên dưới, lưu ý đường dẫn web có chữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mo-han&lt;/code&gt; (trùng cách viết không dấu của “mỏ hàn” và “mỡ hàn”), nên hãy đọc kỹ tên sản phẩm trước khi đặt.&lt;/p&gt;

&lt;p&gt;Hợp kim thiếc chì Sn63/Pb37 và 60/40 nóng chảy quanh mức \(183^\circ\mathrm{C}\), thấp hơn đáng kể so với thiếc không chì (khoảng \(217^\circ\mathrm{C}\) và khó bám ướt hơn khi dùng mỏ không chỉnh nhiệt). Với thực hành cơ bản, thiếc chì dễ thao tác hơn miễn là bạn đảm bảo thông gió tốt và rửa sạch tay sau khi hàn. Khói flux gây kích ứng đường hô hấp nên hãy đặt quạt nhỏ thổi ngang bàn hoặc mở cửa sổ, đồng thời luôn đeo kính bảo hộ phòng trường hợp flux nóng bắn ra.&lt;/p&gt;

&lt;h2 id=&quot;cách-nhận-diện-chất-lượng-mối-hàn&quot;&gt;Cách nhận diện chất lượng mối hàn&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Mối hàn đạt:&lt;/strong&gt; hình nón lõm gọn, thiếc loang đều bám sát vào cả pad đồng lẫn chân linh kiện (không có khe rãnh tách rời), bề mặt mịn bóng (với Sn63 hoặc 60/40) và vẫn nhận ra dáng chân kim loại bên trong.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mối hàn nguội:&lt;/strong&gt; bề mặt đục và sần sùi như cát, do pad chưa đủ nóng hoặc dây bị xê dịch trong lúc thiếc đang đông.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Không bám ướt (vo cục):&lt;/strong&gt; thiếc cuộn tròn thành giọt cầu, thường để lộ phần đồng trần xung quanh chân.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cầu thiếc:&lt;/strong&gt; thiếc chảy tràn nối chập hai chân kề nhau (ví dụ nối nhầm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt; với chân bên cạnh).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bong pad đồng:&lt;/strong&gt; lá đồng bị tách khỏi phíp mạch do giữ nhiệt quá lâu hoặc giật dây lúc thiếc chưa đông. Trên mạch thử đây là bài học kinh nghiệm, nhưng trên module TB6612 sẽ làm hỏng cả mạch.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cháy vỏ dây:&lt;/strong&gt; mũi mỏ chạm vào vỏ nhựa cách điện; dù lõi đồng còn thông mạch nhưng dây rất dễ đứt gãy ngay tại vị trí vỏ bị chảy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Khi sửa một mối hàn xấu, hãy hút bỏ lớp thiếc cũ trước chứ không đắp thêm thiếc mới. Dùng bơm hút thiếc khi mối đang chảy lỏng để lấy phần thừa ra, chấm thêm chút flux rồi hàn lại gọn gàng một lần; tuyệt đối không dùng kìm hay mũi mỏ cạy mối thiếc đã đông cứng khỏi pad.&lt;/p&gt;

&lt;p&gt;Với đường dòng động cơ, hình dạng mối hàn ảnh hưởng trực tiếp đến thông số điện. Một mối hàn kém bám ướt chỉ có điện trở vài phần mười ôm vẫn kêu bíp ở thang thông mạch, nhưng khi gặp dòng kẹt sẽ gây sụt áp rõ rệt:&lt;/p&gt;

\[V_{\mathrm{sut}} = I \times R_{\mathrm{moi}}\]

&lt;p&gt;Chẳng hạn, một mối hàn lỗi có điện trở \(0{,}4\,\Omega\) ở dòng \(1{,}2\,\mathrm{A}\) sẽ làm sụt gần nửa vôn và tỏa khoảng nửa oát nhiệt ngay trên hạt thiếc, khiến một bên động cơ yếu hơn bên kia và xe chạy lệch dù chương trình không hề sai.&lt;/p&gt;

&lt;h2 id=&quot;giảm-lực-kéo-và-lưu-ý-với-đầu-bấm-dupont&quot;&gt;Giảm lực kéo và lưu ý với đầu bấm Dupont&lt;/h2&gt;

&lt;p&gt;Do dây động cơ thường xuyên rung và uốn, mối hàn không được chịu lực cơ học trực tiếp. Hãy tuốt khoảng vài milimét vỏ dây, xoắn gọn lõi bện và tráng thiếc một đoạn ngắn vừa bằng chiều dài pad. &lt;strong&gt;Luồn sẵn ống gen co nhiệt&lt;/strong&gt; vào dây trước khi hàn; sau khi hàn xong, kéo gen co bọc kín mối hàn lẫn một đoạn vỏ nhựa rồi hơ nóng nhẹ bằng thân mỏ để gần (không chạm trực tiếp làm thủng ống). Cố định thêm một vòng dây chùng hoặc dây rút nhựa để lực kéo tác động lên vỏ dây thay vì mối hàn. Nếu quên luồn gen co trước khi hàn vào đầu nối lớn, bạn sẽ phải cắt mối làm lại.&lt;/p&gt;

&lt;p&gt;Riêng với &lt;strong&gt;đầu bấm Dupont&lt;/strong&gt;, phần tiếp xúc bên trong là một lò xo đàn hồi. Tuyệt đối không tráng thiếc vào đầu dây bấm Dupont, vì thiếc sẽ thấm ngược lên sợi đồng làm mất độ đàn hồi và khiến dây gãy ngay sát mép thiếc sau vài chục lần cắm rút. Đầu bấm cần dùng đúng kìm bấm chuyên dụng, còn hàn thiếc chỉ áp dụng cho pad mạch hoặc chân header cố định.&lt;/p&gt;

&lt;h2 id=&quot;an-toàn&quot;&gt;An toàn&lt;/h2&gt;

&lt;p&gt;Mỏ hàn khi cắm điện phải luôn đặt trên giá đỡ. Luôn đeo kính bảo hộ, không vẩy mũi mỏ để hất thiếc thừa mà hãy lau trên bọt biển ẩm hoặc bùi nhùi đồng của giá đỡ. Rút phích cắm ngay khi tạm nghỉ, giữ khu vực hàn xa tầm tay trẻ em và rửa sạch tay sau khi thao tác với thiếc chì. Tuyệt đối không hàn trực tiếp lên vỏ cell pin lithium hay 18650 rời — hãy dùng đế pin đã có sẵn dây nối.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành&quot;&gt;Thực hành&lt;/h2&gt;

&lt;p&gt;Thực hành hoàn toàn trên dây và mạch thử rời; cất module driver và mọi loại pin vào ngăn kéo.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Chỉ cắm điện mỏ hàn sau khi đã đặt mỏ lên giá đỡ, đeo kính bảo hộ và mở quạt/cửa sổ tạo luồng gió thổi ngang bàn. Ghi lại loại hợp kim thiếc sử dụng (cuộn tiêu chuẩn trong bài là Sn63/Pb37 đường kính 0,8 mm).&lt;/li&gt;
  &lt;li&gt;Làm sạch và tráng một lớp thiếc mỏng lên mũi mỏ đến khi sáng bóng; nếu mũi bị đen ôxy hóa không ăn thiếc, hãy lau sạch trước khi chạm vào mạch.&lt;/li&gt;
  &lt;li&gt;Lấy một đoạn dây lõi bện vụn, tuốt khoảng 3 mm vỏ, xoắn chặt lõi đồng và tráng thiếc thành một bó gọn.&lt;/li&gt;
  &lt;li&gt;Hàn đầu dây đó vào chân header hoặc pad trên mạch thử theo đúng trình tự: làm nóng đồng thời pad và chân, đưa thiếc vào mối, rút dây thiếc trước rồi mới rút mỏ hàn, giữ yên đến khi thiếc đông hẳn.&lt;/li&gt;
  &lt;li&gt;Chụp ảnh cận cảnh mối hàn đặt cạnh hình so sánh (hoặc mở hình mẫu trên điện thoại trong cùng khung hình) để tự đánh giá. Nếu mối bị đục, vo cục hay không bám ướt, dùng bơm hút thiếc lấy ra và làm lại. Nếu lỡ làm bong pad, dừng lại, chuyển sang pad khác và ghi rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pad bong&lt;/code&gt; kèm ước lượng thời gian tì mũi vào sổ.&lt;/li&gt;
  &lt;li&gt;Đo thông mạch từ đầu dây tự do sang đầu chân tự do: mối kêu bíp nhưng hình dạng xấu vẫn tính là trượt, và mối trông bóng nhưng hở mạch cũng trượt.&lt;/li&gt;
  &lt;li&gt;Với mối nối giữa hai đoạn dây rời, bổ sung ống gen co nhiệt bảo vệ. Lưu ảnh kết quả với tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-08-joint&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sổ lab cần ghi đủ: loại hợp kim thiếc, đánh giá đối chiếu với hình mẫu, kết quả thông mạch và các lỗi thực tế đã gặp (cầu thiếc, bong pad, không bám ướt, cháy vỏ dây). Các lỗi ghi nhận được là dữ liệu học tập quan trọng; trang sổ chỉ ghi chữ “ok” không đạt yêu cầu.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ&quot;&gt;Ví dụ&lt;/h2&gt;

&lt;p&gt;Một sợi dây động cơ TT được tráng thiếc và hàn vào chân đực cắm vào mạch lái, nhưng mối hàn bị xám đục và vo tròn. Khi đo bằng đồng hồ, thang thông mạch vẫn kêu bíp. Lúc xe chạy không tải (dòng chỉ quanh 110–150 mA của động cơ TT 1:48), sụt áp nhỏ nên xe vẫn đi thẳng. Tuy nhiên khi bánh xe bị kẹt vào cuốn sách, dòng tăng lên khoảng \(1{,}2\,\mathrm{A}\); điện trở mối hàn lỗi đo được chừng \(0{,}4\,\Omega\) gây ra:&lt;/p&gt;

\[V_{\mathrm{sut}} = 1{,}2 \times 0{,}4 = 0{,}48\,\mathrm{V}\]

\[P = 1{,}2 \times 0{,}48 \approx 0{,}58\,\mathrm{W}\]

&lt;p&gt;Hơn nửa oát nhiệt tập trung trên một hạt thiếc nhỏ làm mối nóng nhanh, đồng thời động cơ bên đó bị hụt gần nửa vôn so với bên kia khiến xe kéo lệch dù hai kênh nhận cùng mức PWM. Cách khắc phục đúng là hút sạch mối cũ, tráng thiếc lại lõi đồng cẩn thận, hàn tạo vành lõm bám ướt tốt, bọc gen co nhiệt và thêm vòng giảm lực kéo, sau đó mới đo lại thông mạch khi mối đã nguội hẳn.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=rK38rpUy568&quot;&gt;iFixit’s Soldering 101: Beginners Guide&lt;/a&gt; — iFixit, tiếng Anh. Hướng dẫn 5 yếu tố cốt lõi của một mối hàn tốt: nhiệt độ, thiếc, thời gian, độ bóng và tránh thổi nguội mối.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Qps9woUGkvI&quot;&gt;Soldering Tutorial for Beginners: Five Easy Steps&lt;/a&gt; — oneTesla, tiếng Anh. Minh họa lặp lại trình tự hàn chuẩn trên một mạch nhỏ, giúp bạn làm quen nhịp thao tác trước khi hàn hàng chân header.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nêu chính xác trình tự tiếp xúc khi tạo một mối hàn: mũi mỏ chạm vào đâu, dây thiếc đưa vào đâu, thứ tự rút ra và thời điểm được phép di chuyển dây.&lt;/li&gt;
  &lt;li&gt;Một mối hàn lỗi có điện trở \(0{,}5\,\Omega\) nằm trên đường dây động cơ TT đang kẹt ở dòng \(1{,}0\,\mathrm{A}\). Tính độ sụt áp và công suất tỏa nhiệt tại mối hàn. Thang thông mạch có phát hiện được lỗi này không?&lt;/li&gt;
  &lt;li&gt;Bạn hàn xong dây vào chân header mới nhớ ra quên luồn ống gen co nhiệt. Cách xử lý đúng là gì, và lực kéo khi vận hành phải dồn vào vị trí nào?&lt;/li&gt;
  &lt;li&gt;Có người định tráng thiếc lên đầu bấm Dupont của dây động cơ “cho chắc khỏi tuột”. Hãy giải thích ngắn gọn lý do từ chối và nêu phương án đấu nối thay thế đúng kỹ thuật.&lt;/li&gt;
  &lt;li&gt;Gọi đúng tên bốn lỗi hàn sau: lá đồng tách khỏi mạch thử; bề mặt mối xám đục sần như cát; thiếc nối dính hai chân header cạnh nhau; lớp nhựa vỏ dây cháy nâu ngay sát một mối hàn trông vẫn bóng.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
  &lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

  &lt;ol&gt;
    &lt;li&gt;Mũi mỏ chạm đồng thời pad đồng và chân linh kiện; đưa dây thiếc trực tiếp vào mối nóng (không chấm từ mũi mỏ); rút dây thiếc ra trước rồi mới rút mỏ hàn; giữ dây đứng yên cho tới khi thiếc đông cứng hoàn toàn.&lt;/li&gt;
    &lt;li&gt;\(V = 1{,}0 \times 0{,}5 = 0{,}5\,\mathrm{V}\) và \(P = 0{,}5\,\mathrm{W}\). Thang thông mạch không phát hiện được lỗi này vì tiếng bíp chỉ báo có đường dẫn điện chứ không phân biệt được điện trở 0,5 Ω.&lt;/li&gt;
    &lt;li&gt;Nếu vỏ nhựa header chặn không luồn được ống gen qua, cần cắt bỏ mối cũ, luồn sẵn gen co vào dây rồi hàn lại. Dây rút nhựa hoặc vòng dây chùng phải giữ vào phần vỏ cách điện của dây chứ không kéo vào mối hàn.&lt;/li&gt;
    &lt;li&gt;Thiếc thấm vào đầu bấm Dupont làm mất độ đàn hồi lò xo và khiến sợi đồng dễ gãy sát mép thiếc. Cách đúng là bấm bằng kìm chuyên dụng, hoặc hàn dây động cơ trực tiếp vào chân header/pad kèm bọc gen co và giảm lực kéo.&lt;/li&gt;
    &lt;li&gt;Lần lượt là: bong pad đồng (lifted pad) do quá nhiệt hoặc giật dây; mối hàn nguội (cold joint); cầu thiếc (solder bridge); và cháy vỏ dây.&lt;/li&gt;
  &lt;/ol&gt;

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.adafruit.com/adafruit-guide-excellent-soldering&quot;&gt;Adafruit: hàn cho ra mối tốt&lt;/a&gt; — bộ ảnh đối chiếu rất trực quan giữa mối hàn đạt chuẩn và các dạng lỗi phổ biến.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-solder-through-hole-soldering/all&quot;&gt;SparkFun: hàn xuyên lỗ&lt;/a&gt; — hướng dẫn chi tiết kỹ thuật hàn xuyên lỗ và cách khắc phục từng lỗi.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Module TB6612FNG của Pololu&lt;/a&gt; — tham khảo hàng chân header thực tế bạn sẽ hàn sau khi đã thành thạo trên mạch thử.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Các mức giá dưới đây được ghi nhận trên Hshop ngày &lt;strong&gt;23 tháng 9 năm 2026&lt;/strong&gt; (cần kiểm tra lại giá hiện hành trước khi thanh toán). Phần còn lại của bộ robot nằm ở &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;; danh sách này chỉ liệt kê dụng cụ phục vụ riêng bài thực hành hàn:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Mỏ hàn 60 W WADFOW WEL3616, 75.000 ₫: &lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Giá đỡ tròn có bọt biển, 40.000 ₫: &lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thiếc hàn Sunchi 0,8 mm Sn63/Pb37, 24.000 ₫: &lt;a href=&quot;https://hshop.vn/thiec-han-sunchi-kp26-0-8mm-sn63-pb37-solder-wire&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Bơm hút thiếc Handskit GS-107, 55.000 ₫: &lt;a href=&quot;https://hshop.vn/hut-thiec-handskit-gs-107-desoldering-pump&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Kìm cắt chân 170 mm, 35.000 ₫: &lt;a href=&quot;https://hshop.vn/kim-cat-day-dien-nho-170-cat-chan-linh-kien-dien-tu&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Mỡ hàn RELIFE RL-426A, 25.000 ₫: &lt;a href=&quot;https://hshop.vn/mo-han-relife-rl-426a-30g-chinh-hang&quot;&gt;Hshop&lt;/a&gt; — lưu ý kiểm tra đúng sản phẩm là hũ mỡ hàn (vì đường dẫn có chữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mo-han&lt;/code&gt; dễ nhầm với mỏ hàn).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nếu Hshop tạm hết hàng, bạn có thể tìm thêm tại &lt;a href=&quot;https://shopee.vn/search?keyword=mo%20han%2060W&quot;&gt;Shopee, mỏ hàn 60W&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=thiec%20han&quot;&gt;Shopee, thiếc hàn&lt;/a&gt;, hoặc nhập cùng từ khóa trên &lt;a href=&quot;https://www.lazada.vn/catalog/?q=&quot;&gt;trang catalog Lazada&lt;/a&gt;. Thanh header và mạch thử rời có thể mua trực tiếp tại &lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; hoặc &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;. Đừng quên trang bị thêm kính bảo hộ vì đây là vật dụng bắt buộc khi hàn và cắt chân linh kiện.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Đọc datasheet và sơ đồ chân</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_07_datasheet_pinouts/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_07_datasheet_pinouts</id>
   <content type="html">&lt;p&gt;Datasheet là tài liệu kỹ thuật duy nhất có giá trị quyết định xem một kết nối dây có hợp lệ hay không. Bài học này thiết lập một trình tự đọc datasheet chuẩn gồm sáu bước, sau đó áp dụng trực tiếp cho module lái động cơ TB6612FNG của xe hai bánh và giới hạn chân GPIO trên ESP32 trước xung echo 5 V.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn có thể mở bất kỳ tài liệu PDF datasheet nào và tra cứu theo đúng một trình tự thống nhất: thông số cực đại tuyệt đối, sơ đồ và tên chân, điều kiện làm việc khuyến nghị, các đặc tính điện thực sự cần dùng, sơ đồ ứng dụng mẫu, và bản vẽ cơ khí vỏ IC để xác định chân số 1. Với module TB6612FNG, bạn sẽ trích xuất chính xác dải điện áp nguồn động cơ, dải nguồn logic, quy tắc chân STBY, dòng liên tục và dòng đỉnh, đồng thời vẽ lại sơ đồ các chân AIN1, AIN2, PWMA, BIN1, BIN2, PWMB, AO1, AO2, BO1, BO2 ra giấy. Bạn cũng sẽ chỉ ra đúng dòng thông số cực đại trên datasheet ESP32 cấm nối thẳng dây echo của HC-SR04 vào GPIO, và hiểu vì sao ký hiệu in trên các mạch tương thích giá rẻ chỉ được tin tưởng sau khi đã đối chiếu với đồng hồ đo và datasheet.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-có&quot;&gt;Kiến thức cần có&lt;/h2&gt;

&lt;p&gt;Bạn cần nắm định luật Ohm (bài 01), thói quen đo an toàn bằng đồng hồ vạn năng (bài 02) và cực tính linh kiện (bài 04). Bài này không cấp nguồn động cơ; bạn chỉ cần một quyển sổ ghi chép rõ ràng để mang sang dùng tiếp ở Chương 02.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Khi mới học điện tử, thói quen phổ biến nhất là gõ Google tìm một sơ đồ nối dây vẽ sẵn trên blog rồi cắm theo y hệt — cho đến ngày bo mạch mua về có nhãn chân hơi khác, hoặc một cảm biến bốc khói mà không biết tại sao! &lt;strong&gt;Datasheet&lt;/strong&gt; (tài liệu kỹ thuật chính hãng) là bản “hiến pháp” duy nhất đáng tin cậy 100% cho mọi con chip: bạn không cần đọc hết 800 trang, mà chỉ cần biết đúng &lt;strong&gt;6 trang cốt lõi&lt;/strong&gt; chứa điện áp trần, sơ đồ chân và bảng sự thật.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-giải-phẫu-một-tài-liệu-kỹ-thuật-datasheet&quot;&gt;Kiến thức cần nhớ: Giải phẫu Một Tài liệu Kỹ thuật (Datasheet)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Absolute Maximum Ratings (Giới hạn Tuyệt đối — Vùng Chết):&lt;/strong&gt; Cho biết mức điện áp/dòng/nhiệt độ mà chỉ cần chạm tới là linh kiện có thể hỏng vĩnh viễn — &lt;em&gt;không bao giờ&lt;/em&gt; thiết kế chạy sát ngưỡng này!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Recommended Operating Conditions (Vùng Làm việc Khuyến nghị):&lt;/strong&gt; Dải điện áp nguồn (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;), mức logic nhận diện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH/LOW&lt;/code&gt; (\(V_{IH}, V_{IL}\)) và dòng tải liên tục an toàn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pinout &amp;amp; Truth Table (Sơ đồ Chân &amp;amp; Bảng Sự thật):&lt;/strong&gt; Xác định chính xác chân nào là nguồn, chân nào là chân khởi động nhạy cảm (&lt;em&gt;strapping pins&lt;/em&gt; trên ESP32 như GPIO 0, 2, 12, 15), và tổ hợp mức logic nào làm động cơ quay thuận, nghịch hay phanh.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-áp-dụng-6-bước-đọc-datasheet-kiểm-tra-ngưỡng-33-v-của-esp32-và-mạch-phân-áp&quot;&gt;Ví dụ làm từng bước: Áp dụng 6 bước đọc Datasheet kiểm tra ngưỡng 3,3 V của ESP32 và mạch phân áp&lt;/h3&gt;

&lt;p&gt;Tra mục &lt;em&gt;Absolute Maximum Ratings&lt;/em&gt; của ESP32: điện áp tối đa trên bất kỳ chân GPIO nào là \(V_{\text{IO,max}} = V_{DD} + 0.3\text{ V} = 3.3 + 0.3 = \mathbf{3.6\text{ V}}\).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Nếu cảm biến siêu âm xuất tín hiệu Echo &lt;strong&gt;\(5.0\text{ V}\)&lt;/strong&gt; nối thẳng vào GPIO ESP32: \(5.0\text{ V} &amp;gt; 3.6\text{ V}\) (vượt trần \(1.4\text{ V}\)) → làm chảy diode kẹp bảo vệ bên trong chân chip!&lt;/li&gt;
  &lt;li&gt;Thiết kế nhanh mạch phân áp bảo vệ với hai điện trở phổ thông \(R_1 = 1.2\text{ k}\Omega\) và \(R_2 = 2.2\text{ k}\Omega\):
\(V_{\text{GPIO}} = 5.0 \times \frac{2.2}{1.2 + 2.2} = 5.0 \times \frac{2.2}{3.4} \approx \mathbf{3.24\text{ V}}!\)
Kiểm tra cả hai biên từ datasheet ESP32 (\(V_{IH} = 0.75 \times 3.3 = 2.475\text{ V}\) và trần \(3.6\text{ V}\)):
\(2.475\text{ V} &amp;lt; \mathbf{3.24\text{ V}} &amp;lt; 3.6\text{ V} \implies \text{nhận chắc mức HIGH và an toàn tuyệt đối!}\)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-quy-trình-4-bước-tra-datasheet-trong-3-phút&quot;&gt;Giải thích: Quy trình 4 bước tra Datasheet trong 3 phút&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Kiểm tra đúng mã hậu tố linh kiện:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt; khác hoàn toàn chân với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt;; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32-WROOM-32&lt;/code&gt; có chân khác &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32-S3&lt;/code&gt; — luôn đối chiếu đúng mã in trên lưng chip.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Đọc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Absolute Maximum&lt;/code&gt; trước &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Typical&lt;/code&gt;:&lt;/strong&gt; Đánh dấu ngay điện áp tối đa cho chân nguồn và chân tín hiệu để dựng rào chắn an toàn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Khoanh vùng các chân đặc biệt (Input-only, Strapping, ADC2):&lt;/strong&gt; Trên ESP32 cổ điển, GPIO 34–39 &lt;em&gt;chỉ đọc vào&lt;/em&gt; (không xuất PWM được), còn nhóm ADC2 bị khóa khi bật Wi-Fi!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Kiểm tra bảng sự thật (Truth Table) trước khi viết code:&lt;/strong&gt; Ví dụ trên TB6612FNG, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1=H, AIN2=H&lt;/code&gt; là phanh ngắn mạch (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Short Brake&lt;/code&gt;), còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1=L, AIN2=L&lt;/code&gt; là dừng thả trôi (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stop&lt;/code&gt;) — nắm bảng này giúp debug động cơ trong 30 giây.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Trên mạch phân áp bảo vệ Echo ở trên, nếu nguồn 5 V của mạch Buck dao động lên mức cao nhất \(5.25\text{ V}\), điện áp vào chân GPIO ESP32 là bao nhiêu? \(5.25 \times (2.2 / 3.4) \approx \mathbf{3.40\text{ V}} &amp;lt; 3.6\text{ V}\) — vẫn giữ nguyên biên an toàn!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quan-trọng&quot;&gt;Vì sao bài này quan trọng&lt;/h2&gt;

&lt;p&gt;Xe Capstone là hệ dẫn động vi sai gồm hai động cơ TT, module TB6612FNG và vi điều khiển 3,3 V (ESP32 hoặc Pico). Nguồn VM lấy từ pin động cơ (không lấy từ USB), nguồn VCC nối vào đường 3,3 V, còn chân echo của HC-SR04 xuất 5 V nên không được nối trực tiếp vào GPIO 3,3 V. Nếu chỉ nhớ tên chân một cách máy móc theo nhãn dán, buổi chạy thử đầu tiên rất dễ gặp các lỗi tưởng như do phần mềm: bánh xe đứng yên vì quên kéo chân STBY, hoặc cháy chân GPIO ngay khi cảm biến phản hồi. Trình tự đọc datasheet giúp bạn phát hiện các giới hạn cấm trước khi đấu dây.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;Cầu H đang làm gì với các chân ra mô-tơ&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Mỗi động cơ được mắc giữa một cặp chân ngõ ra (AO1/AO2 hoặc BO1/BO2). Chiều quay quyết định bởi cặp chân logic hướng, tốc độ quyết định bởi xung PWM, và cả hai đều vô hiệu nếu chân STBY đang giữ cầu H ở trạng thái chờ.&lt;/p&gt;

&lt;h2 id=&quot;trình-tự-sáu-bước-đọc-datasheet&quot;&gt;Trình tự sáu bước đọc datasheet&lt;/h2&gt;

&lt;p&gt;Hãy luôn đi theo đúng thứ tự sau mà không đảo lộn:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Thông số cực đại tuyệt đối (Absolute Maximum Ratings).&lt;/strong&gt; Đây là ngưỡng gây hỏng linh kiện — vượt qua giới hạn này nhà sản xuất không đảm bảo IC còn sống, tuyệt đối không phải điểm vận hành bình thường. Hãy chép đúng các dòng liên quan tới dây bạn định nối (điện áp nguồn, điện áp chân logic, dòng ngõ ra); phương án nào vượt ngưỡng này phải bị loại bỏ ngay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Sơ đồ chân và tên chân (Pinout &amp;amp; Pin Functions).&lt;/strong&gt; Bảng mô tả chân đóng vai trò từ điển kết nối: ví dụ AO1 chỉ trở thành “bánh trái” khi bạn quy ước rõ động cơ nào gắn vào đó. Trên TB6612FNG, bạn cần ghi rõ đầy đủ các chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMB&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BO1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BO2&lt;/code&gt;, cùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;. Phân tách rạch ròi nhóm chân đầu vào logic (nối GPIO) và nhóm chân ngõ ra công suất (nối động cơ) để tránh đưa dòng động cơ về vi điều khiển.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Điều kiện làm việc khuyến nghị (Recommended Operating Conditions).&lt;/strong&gt; Đây là vùng hoạt động ổn định mà mọi thông số đặc tính điện phía sau được đo đạc. Lưu ý rằng giới hạn trên trang thông số của module có thể chặt hơn giới hạn của con chip trần do ảnh hưởng của đường mạch đồng và hàng chân header; khi hai tài liệu khác nhau, luôn tuân theo giới hạn chặt hơn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Các đặc tính điện cần dùng (Electrical Characteristics).&lt;/strong&gt; Không cần đọc hết toàn bộ bảng dài mà chỉ trích đúng thông số bạn áp dụng: với mạch lái động cơ, đó là điện áp tối thiểu được nhận là mức cao ($V_{IH,\min}$), điều kiện kéo mức cao cho chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;, dòng liên tục và dòng đỉnh.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Sơ đồ ứng dụng mẫu (Typical Application Circuit).&lt;/strong&gt; Ghi nhận cấu trúc chuẩn: tụ lọc đặt sát chân VM, tụ lọc đặt sát chân VCC, nối mass chung, và các chân vào điều khiển từ vi điều khiển. Ngay cả khi module đã hàn sẵn tụ nhỏ, tụ lọc vẫn không thể thay thế sợi dây mass chung giữa ESP32 và mạch lái.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Bản vẽ cơ khí vỏ IC (Package Outline) để tìm chân số 1.&lt;/strong&gt; Bản vẽ tiêu chuẩn là hình chiếu nhìn từ mặt trên xuống (top view), trong đó chân số 1 nằm cạnh dấu chấm hoặc cạnh vát; lưu ý rằng nhìn từ mặt đáy sẽ bị lật ngược hàng chân. Nếu ký hiệu in lụa trên module mâu thuẫn với dấu chấm định vị, hãy dùng đồng hồ vạn năng đo kiểm tra để phân xử.&lt;/p&gt;

&lt;p&gt;Các mạch tương thích giá rẻ đôi khi in sai nhãn lụa (chẳng hạn ghi “5V” ở chân logic 3,3 V, đảo vị trí GND với VM, hoặc in lệch mũi tên chân ECHO/TRIG). Khi nghi ngờ một đường nguồn, hãy đo trực tiếp bằng đồng hồ ở chế độ DC V (que đen đặt tại mass) trên nguồn có giới hạn dòng rồi ghi kết quả thực tế ra giấy — số đo luôn có giá trị cao hơn chữ in.&lt;/p&gt;

&lt;h2 id=&quot;áp-dụng-sáu-bước-cho-tb6612fng&quot;&gt;Áp dụng sáu bước cho TB6612FNG&lt;/h2&gt;

&lt;p&gt;Hãy đối chiếu thông số của module TB6612FNG dùng trong khóa học với tài liệu tóm tắt chip Toshiba trên trang Pololu ở mục Đọc thêm (nếu thông số trên trang nhà sản xuất module chặt hơn thì ưu tiên làm theo mức chặt hơn).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 1 (Cực đại tuyệt đối):&lt;/strong&gt; Kiểm tra trước để loại bỏ mọi phương án ngược cực hoặc quá áp trước khi cắm giắc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 2 (Tên chân):&lt;/strong&gt; Động cơ A nhận tín hiệu chiều quay từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt; và tín hiệu tốc độ từ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt;; động cơ B tương ứng là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN2&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMB&lt;/code&gt;. Hai cặp ngõ ra nối dây động cơ là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO1&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO2&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BO1&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BO2&lt;/code&gt;. Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; không phải chân dư mà là công tắc bật/tắt cả hai cầu H: bảng chân lý chỉ cho phép động cơ chạy khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; ở mức cao. Vì nhiều module thiết kế kéo sẵn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; xuống mức thấp, quên nối chân này sẽ khiến cả hai bánh đứng yên dù mã nguồn đang gửi lệnh tiến.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 3 (Điều kiện khuyến nghị):&lt;/strong&gt; Module của khóa học có dải &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; từ 4,5 V đến 10 V. Một cell lithium đơn (~3,7 V) nằm dưới ngưỡng thấp này, còn bộ pin sạc đầy vượt 10 V lại vượt ngưỡng trên của module dù datasheet chip trần có thể cho phép cao hơn. Nguồn logic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; nằm trong dải 2,7 V đến 5,5 V, hoàn toàn tương thích với đường 3,3 V của vi điều khiển. Nguồn USB 5 V tuy nằm trong dải logic nhưng không dùng để cấp cho động cơ (xem chi tiết ở bài 09). Hai chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; được thiết kế tách biệt hoàn toàn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 4 (Đặc tính điện cần dùng):&lt;/strong&gt; Dòng liên tục mỗi kênh khoảng 1,2 A và dòng đỉnh khoảng 3,2 A. Hai động cơ TT quay không tải rút dòng rất nhỏ so với 1,2 A, nhưng khi kẹt trục dòng tăng rất mạnh; duy trì trạng thái kẹt vượt mức liên tục sẽ làm cháy mạch lái. Đồng thời, cần lưu ý ngưỡng nhận diện mức logic cao:&lt;/p&gt;

\[V_{IH,\min} = 0{,}7 \times V_{CC}\]

&lt;p&gt;Tại \(V_{CC} = 3{,}3\,\mathrm{V}\), ngưỡng này là \(2{,}31\,\mathrm{V}\), nên tín hiệu mức cao 3,3 V từ GPIO vượt qua dễ dàng. Ngược lại, nếu cấp \(V_{CC} = 5\,\mathrm{V}\), ngưỡng tối thiểu tăng lên \(3{,}5\,\mathrm{V}\) và chân GPIO 3,3 V &lt;strong&gt;không đạt&lt;/strong&gt; mức cao tiêu chuẩn. Phép tính này khẳng định bắt buộc phải cấp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; bằng 3,3 V khi điều khiển từ ESP32 hay Pico, không cấp 5 V rồi trông chờ mạch tự nhận.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bước 5 &amp;amp; 6 (Sơ đồ ứng dụng và bản vẽ vỏ):&lt;/strong&gt; Nối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; vào pin động cơ, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; vào 3,3 V, nối mass chung, đấu động cơ vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO1&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO2&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BO1&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BO2&lt;/code&gt;, và nối bốn chân GPIO tới &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; kèm tụ đệm đúng cực ngang chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; (sẽ chọn trị số cụ thể ở bài 09). Cuối cùng, đánh dấu rõ vị trí chân số 1 trên bản vẽ.&lt;/p&gt;

&lt;p&gt;Lưu ý rằng chiều “thuận kim đồng hồ” (CW) trong bảng chân lý datasheet phụ thuộc vào cách bạn đấu hai dây động cơ chứ không tự động khớp hướng tiến của khung xe. Khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; ở mức cao, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt; khác trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt; có xung thì động cơ quay; đảo trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt; sẽ đảo chiều quay; đưa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; xuống thấp sẽ chuyển mạch về chế độ chờ.&lt;/p&gt;

&lt;h2 id=&quot;giới-hạn-5-v-trên-chân-gpio-của-esp32&quot;&gt;Giới hạn 5 V trên chân GPIO của ESP32&lt;/h2&gt;

&lt;p&gt;Áp dụng đúng trình tự trên cho tài liệu PDF của Espressif: điện áp cực đại tuyệt đối trên chân GPIO của ESP32 chỉ cao hơn đường nguồn 3,3 V vài trăm milivôn, &lt;strong&gt;không phải 5 V&lt;/strong&gt;. Vùng làm việc khuyến nghị là mức nguồn vào/ra 3,3 V; mọi xung 5 V đều nằm ngoài hàng rào an toàn.&lt;/p&gt;

&lt;p&gt;Với cảm biến &lt;strong&gt;HC-SR04&lt;/strong&gt; (nuôi bằng 5 V), chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; xuất mức cao xấp xỉ 5 V khi có xung phản hồi, nên bảng thông số cực đại cấm tuyệt đối việc nối thẳng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; vào bất kỳ chân GPIO nào của ESP32. Chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Trig&lt;/code&gt; (ngõ vào kích cảm biến) thường nhận được mức cao 3,3 V, nhưng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; bắt buộc phải hạ mức. Tờ sơ đồ chân MCU hôm nay cần ghi rõ cảnh báo “không nối thẳng Echo” kèm con số giới hạn điện áp. Raspberry Pi Pico cũng có giới hạn tương tự (chân GPIO 3,3 V không chịu 5 V), đồng thời lưu ý khi lập trình phải dùng số chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GP&lt;/code&gt; trên sơ đồ chứ không đếm theo số thứ tự chân vật lý từ đầu USB.&lt;/p&gt;

&lt;p&gt;Nếu gặp mạch tương thích in nhãn “5V” cạnh một lỗ cắm bạn tưởng là GPIO, hãy cắm riêng USB và đo kiểm tra: bất kỳ lỗ nào đo được gần 5 V đều là chân nguồn, không phải chân nhận tín hiệu echo hay chân đấu LED trực tiếp.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành&quot;&gt;Thực hành&lt;/h2&gt;

&lt;p&gt;Bài thực hành này không quay động cơ; sản phẩm đầu ra là hai trang sơ đồ chân hoàn chỉnh.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Tạo hai trang ghi chép trong sổ lab mang tên “Sơ đồ chân MCU” và “Sơ đồ chân driver”, ghi rõ ngày thực hiện (nếu mạch chưa về, lập trang MCU từ PDF ESP32 và ghi chú &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa mua board&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Trên trang &lt;strong&gt;driver&lt;/strong&gt;, trình bày đủ sáu bước: ghi rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; 4,5–10 V, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; 2,7–5,5 V, quy tắc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; phải ở mức cao, dòng 1,2 A liên tục và 3,2 A đỉnh. Phác thảo hàng chân header và gắn đủ nhãn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMB&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BO1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BO2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; (nếu chữ in lụa trên module khác datasheet, kẻ hai cột đối chiếu).&lt;/li&gt;
  &lt;li&gt;Trên trang &lt;strong&gt;MCU&lt;/strong&gt;, chép lại giới hạn điện áp cực đại của GPIO cùng tên bảng thông số gốc; ghi rõ lệnh cấm nối thẳng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; 5 V của HC-SR04 vào GPIO. Gán trước các chân ngõ ra hợp lệ cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; (với ESP32 thông dụng, tránh chọn các chân dùng cho bộ nhớ flash và các chân chỉ hỗ trợ ngõ vào).&lt;/li&gt;
  &lt;li&gt;Không cấp nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;. Nếu có nguồn 3,3 V giới hạn dòng để cấp riêng phần logic (hoặc chỉ cắm USB cho mạch phát triển), đo kiểm tra chân ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; và chân ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; với que đen đặt tại mass; nếu chưa có thiết bị để đo an toàn, viết rõ các bước đo và đánh dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dry-run&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Lưu ảnh chụp hai trang ghi chép vào sổ với tên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-07-mcu-card&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-07-driver-card&lt;/code&gt; (đảm bảo đủ rõ nét để dùng lại ở Chương 02).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Dừng ngay nếu phát hiện chân nào nóng bất thường hoặc một chân bạn định dùng làm GPIO lại đo được gần 5 V.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [How To Read A Datasheet - Electronics with Becky Stern&lt;/td&gt;
          &lt;td&gt;DigiKey](https://www.youtube.com/watch?v=v2d5H5k_SAc) — DigiKey, tiếng Anh. Hướng dẫn trình tự đọc một datasheet cơ bản: tính năng, điện áp tối đa rồi tới sơ đồ chân.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=1EXXqWweTkI&quot;&gt;How To Read A Datasheet - Phil’s Lab #123&lt;/a&gt; — Phil’s Lab, tiếng Anh. Đi sâu hơn vào bảng đặc tính điện và đồ thị; bạn có thể dừng ở phần bảng chân của con IC mình đang sử dụng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;p&gt;Với mỗi câu hỏi dưới đây, hãy nêu rõ con số thông số, tên bảng chứa thông số đó và kết nối dây bị cấm tương ứng:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Một người định nối thẳng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; của HC-SR04 vào chân GPIO 4 của ESP32 với suy nghĩ bảng cực đại sẽ chịu được. Thông số nào cấm kết nối này, và bạn sẽ ghi nhận mức điện áp nào nếu đo xung echo khi cảm biến nuôi bằng 5 V?&lt;/li&gt;
  &lt;li&gt;Một mạch lái ghi chân logic cần 5 V trong khi mức cao của ESP32 là 3,3 V. Tính \(0{,}7 \times 5\) và \(0{,}7 \times 3{,}3\). Kết quả nào cho thấy ESP32 không đạt chuẩn điều khiển, và cần cấp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; bao nhiêu để khắc phục?&lt;/li&gt;
  &lt;li&gt;Có người định đấu tạm hai động cơ TT vào chân 5 V của ESP32 “chỉ để thử”. Biết cổng USB cấp khoảng 500 mA còn mạch lái chịu 1,2 A liên tục. Giới hạn nào bị vượt quá trước, và vì sao mức 1,2 A của mạch lái không cho phép lấy USB làm nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;?&lt;/li&gt;
  &lt;li&gt;Nếu chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; để hở và chương trình không kích lên mức cao, cơ chế nào của IC ngăn xung PWM làm bánh xe quay, và thao tác GPIO nào giải quyết việc này?&lt;/li&gt;
  &lt;li&gt;Giả sử bạn nhận được module DRV8833 thay vì TB6612. Vì sao không được chép nguyên các tên chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; từ trang ghi chép hôm nay sang module mới, và bạn phải mở tài liệu nào ở mục Đọc thêm?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
  &lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

  &lt;ol&gt;
    &lt;li&gt;Thông số cấm là giới hạn điện áp cực đại tuyệt đối (Absolute Maximum Ratings) của GPIO ESP32, vốn nằm sát mức nguồn 3,3 V chứ không phải 5 V. Xung echo khi cảm biến nuôi 5 V sẽ lên gần 5 V, nên không được nối thẳng mà cần mạch chuyển mức hoặc cầu chia áp.&lt;/li&gt;
    &lt;li&gt;\(0{,}7 \times 5 = 3{,}5\,\mathrm{V}\), lớn hơn mức cao 3,3 V của GPIO nên cấp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC = 5 V&lt;/code&gt; sẽ không đạt chuẩn mức cao. Với \(0{,}7 \times 3{,}3 = 2{,}31\,\mathrm{V}\), chân GPIO 3,3 V vượt qua dễ dàng; do đó phải cấp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC = 3,3 V&lt;/code&gt;.&lt;/li&gt;
    &lt;li&gt;Giới hạn 500 mA của cổng USB bị vượt quá trước (ngay cả một động cơ TT kẹt trục đã rút vượt mức này). Thông số 1,2 A chỉ là khả năng chịu dòng liên tục của mạch lái, không phải năng lực cấp dòng của cổng USB; nguồn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; bắt buộc phải lấy từ pin động cơ.&lt;/li&gt;
    &lt;li&gt;Khi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; ở mức thấp (hoặc bị kéo xuống thấp do để hở), IC khóa cả hai cầu H ở chế độ chờ (standby) nên mọi lệnh PWM đều vô hiệu. Chỉ cần xuất mức logic cao giữ ổn định trên chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; trong thời gian cho phép chạy.&lt;/li&gt;
    &lt;li&gt;Mỗi họ IC có sơ đồ và tên chân riêng (ví dụ DRV8833 dùng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nSLEEP&lt;/code&gt; thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;). Cần mở trang thông số TI DRV8833 ở mục Đọc thêm và thực hiện lại đủ 6 bước đọc datasheet cho đúng con chip đó.&lt;/li&gt;
  &lt;/ol&gt;

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Module TB6612FNG của Pololu&lt;/a&gt; — tóm tắt rõ ràng cách hoạt động của IC Toshiba: dải logic, chân STBY và mức dòng 1,2 A / 3,2 A (ưu tiên giới hạn chặt hơn nếu trang nhà sản xuất module quy định khác).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;Datasheet ESP32 (PDF)&lt;/a&gt; — bảng thông số cực đại tuyệt đối và danh sách chân GPIO.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ti.com/product/DRV8833&quot;&gt;TI DRV8833&lt;/a&gt; — tài liệu chính thức cho phương án mạch lái thay thế nếu bạn sử dụng DRV8833.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html&quot;&gt;Tài liệu Raspberry Pi Pico&lt;/a&gt; — giới hạn vào/ra 3,3 V, vị trí chân số 1 và bảng ánh xạ số chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GP&lt;/code&gt; cho lập trình.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Các linh kiện chính (ESP32 hoặc Pico, module TB6612, động cơ TT và HC-SR04) đã nằm trong danh sách ở bài &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;. Không mua rời IC TB6612FNG dạng chip dán chân mịn để tự hàn tay cho đồ án.&lt;/p&gt;

&lt;p&gt;Nếu chưa có đồng hồ vạn năng để kiểm tra nhãn in trên mạch, bạn có thể tham khảo &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UNI-T UT33D+ tại Hshop&lt;/a&gt; (kiểm tra giá hiện hành trước khi đặt) hoặc đối chiếu thêm tại &lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; và &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Giắc nối, driver động cơ và mạch vi điều khiển</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_06_connectors_drivers_boards/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_06_connectors_drivers_boards</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ phân biệt rõ dây cắm Dupont bước 2,54 mm (chỉ dùng cho tín hiệu) với các loại giắc nối được phép mang dòng động cơ, đồng thời tránh ép nhầm vỏ JST-XH vào giắc servo hay XT30 vào XT60. Bạn sẽ tính được điện áp thực tế động cơ TT nhận từ bộ pin 7,4 V khi qua TB6612 so với L298N, và hiểu giới hạn của phương án thay thế DRV8833 (tối đa 1,5 A, nhanh nóng khi kẹt). Bạn cũng sẽ phân biệt được mạch phát triển ESP32 có sẵn USB với module WROOM trần, chỉ ra đúng chân VBUS và 3V3 trên Raspberry Pi Pico, và giải thích vì sao chân echo của HC-SR04 không được nối thẳng vào GPIO 3,3 V.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-gì-trước&quot;&gt;Cần có gì trước&lt;/h2&gt;

&lt;p&gt;Bạn đã biết đo điện áp một chiều song song, nắm rõ dòng giới hạn của chân GPIO chỉ ở mức vài chục miliampe và biết động cơ TT kẹt trục tiêu thụ dòng lớn hơn nhiều. Bài này chưa điều khiển PWM động cơ mà dừng ở việc đọc ký hiệu IC, tên chân và tính toán sụt áp trước khi quay bánh xe đầu tiên.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Một chiếc xe robot có thuật toán PID tinh vi đến đâu cũng sẽ đâm sầm vào chân bàn nếu chỉ vì một sợi dây Dupont lỏng lẻo tuột khỏi chân driver khi xe chạy qua gờ gạch! Trong thực tế cơ điện tử, &lt;strong&gt;giắc nối&lt;/strong&gt;, &lt;strong&gt;mạch lái động cơ (motor driver)&lt;/strong&gt; và &lt;strong&gt;bo vi điều khiển&lt;/strong&gt; là ba điểm giao tiếp cơ–điện dễ sinh lỗi nhất nhưng lại thường bị người mới coi nhẹ nhất.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-giao-diện-cơđiện-mạch-lái-cầu-h-và-ngưỡng-điện-áp-bo-mạch&quot;&gt;Kiến thức cần nhớ: Giao diện Cơ–Điện, Mạch lái Cầu H và Ngưỡng Điện áp Bo mạch&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chuẩn Bước chân Giắc nối (Pitch):&lt;/strong&gt; Đầu nối điện tử khác nhau ở từng phần trăm milimét — chuẩn header/Dupont là &lt;strong&gt;2,54 mm&lt;/strong&gt; (0,1 inch), chuẩn JST-XH trên pin/bo mạch là &lt;strong&gt;2,50 mm&lt;/strong&gt;, còn JST-PH nhỏ hơn là &lt;strong&gt;2,00 mm&lt;/strong&gt;. Cố ép cắm chéo hai chuẩn sẽ làm cong chân và hỏng tiếp xúc!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nguyên lý Cầu H (H-Bridge):&lt;/strong&gt; Bốn công tắc bán dẫn bố trí hình chữ H quanh động cơ DC cho phép đảo chiều dòng điện (quay thuận/nghịch), phanh ngắn mạch (brake) hoặc thả trôi (coast), và điều tốc bằng xung PWM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ranh giới Điện áp giữa Đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIN / 5V&lt;/code&gt; và Chân Logic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;:&lt;/strong&gt; Trên bo ESP32/Pico, chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIN&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt; đi qua ổn áp để tạo nguồn 3,3 V cho chip, nhưng các chân tín hiệu GPIO nối thẳng vào silicon 3,3 V — không chịu được điện áp 5 V!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-độ-lệch-tích-lũy-khi-cắm-nhầm-giắc-jst-xh-250-mm-vào-header-254-mm&quot;&gt;Ví dụ làm từng bước: Tính độ lệch tích lũy khi cắm nhầm giắc JST-XH (2,50 mm) vào header 2,54 mm&lt;/h3&gt;

&lt;p&gt;Giả sử bạn cố cắm một đầu nối cái &lt;strong&gt;JST-XH 10 chân&lt;/strong&gt; (bước chân \(2.50\text{ mm}\)) vào hàng chân rào &lt;strong&gt;Dupont 10 chân&lt;/strong&gt; (bước chân \(2.54\text{ mm}\)):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Mỗi khoảng chân lệch nhau \(2.54 - 2.50 = 0.04\text{ mm}\).&lt;/li&gt;
  &lt;li&gt;Qua 9 khoảng chân (từ chân 1 đến chân 10), tổng độ lệch tích lũy là:
\(\Delta L = 9 \times 0.04\text{ mm} = \mathbf{0.36\text{ mm}}!\)
Với độ dày chân kim loại chỉ \(0.64\text{ mm}\), độ lệch \(0.36\text{ mm}\) đủ làm cong gãy chân số 9–10 hoặc nứt vỏ nhựa! Tương tự, tính lại sụt áp trên pin \(7.4\text{ V}\): qua &lt;strong&gt;L298N&lt;/strong&gt; động cơ chỉ còn \(7.4 - 2.0 = \mathbf{5.4\text{ V}}\) (mất 27% lực kéo), còn qua &lt;strong&gt;TB6612FNG&lt;/strong&gt; động cơ giữ được \(7.4 - 0.5 = \mathbf{6.9\text{ V}}\)!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-nguyên-tắc-chọn-giắc-nối-và-mạch-lái-bền-bỉ-cho-xe-robot&quot;&gt;Giải thích: 4 nguyên tắc chọn giắc nối và mạch lái bền bỉ cho xe robot&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Dùng giắc có khóa gài (JST / XT30) cho mọi đường nguồn và động cơ:&lt;/strong&gt; Giắc Dupont chỉ dùng trên bàn thử nghiệm breadboard; khi xe chạy rung lắc, giắc có ngàm chống cắm ngược cực vừa chắc chắn vừa chống cháy mạch.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Luôn nối chung mass (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;) giữa bo MCU và mạch lái động cơ:&lt;/strong&gt; Hai nguồn riêng (pin động cơ và USB/Buck logic) &lt;em&gt;bắt buộc&lt;/em&gt; phải nối chung một dây &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; chuẩn tham chiếu, nếu không tín hiệu PWM sẽ bị “trôi” vô nghĩa.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Kiểm tra chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; (Standby) trên TB6612FNG:&lt;/strong&gt; Rất nhiều người lắp xong động cơ không quay chỉ vì quên kéo chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; lên mức &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; (3,3 V)!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Ghi nhãn rõ điện áp trên từng cụm giắc:&lt;/strong&gt; Dán băng keo màu đánh dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7.4V BAT&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V BUCK&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.3V LOGIC&lt;/code&gt; ngay trên khung xe để không bao giờ cắm nhầm khi tháo lắp nhanh.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu hai động cơ trên xe có dòng làm việc mỗi chiếc \(0.6\text{ A}\) và dòng hãm trục (khi kẹt bánh) là \(1.8\text{ A}\), một kênh của TB6612FNG (dòng liên tục \(1.2\text{ A}\), dòng đỉnh \(3.2\text{ A}\)) có đáp ứng an toàn không? &lt;strong&gt;Có!&lt;/strong&gt; Cả dòng làm việc (\(0.6 &amp;lt; 1.2\text{ A}\)) lẫn dòng hãm ngắn hạn (\(1.8 &amp;lt; 3.2\text{ A}\)) đều nằm gọn trong giới hạn an toàn.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-xe-hai-bánh-thường-hỏng-ở-giắc-nối-và-mạch-lái&quot;&gt;Vì sao xe hai bánh thường hỏng ở giắc nối và mạch lái&lt;/h2&gt;

&lt;p&gt;Hệ thống xe đồ án gồm hai động cơ TT, một mạch lái TB6612, vi điều khiển ESP32 hoặc Pico chạy mức 3,3 V và cảm biến siêu âm HC-SR04. Dây cắm Dupont phù hợp để nối các chân điều khiển AIN1, AIN2, PWMA và STBY, nhưng không phù hợp để dẫn dòng kẹt động cơ vào chân VM — và càng không thể coi chân GPIO là nguồn VM.&lt;/p&gt;

&lt;p&gt;Với bộ pin 2S danh định 7,4 V, cầu H MOSFET chỉ sụt vài phần mười vôn, còn L298N (dù có cùng giá bán tại thời điểm khảo sát) lại sụt tới khoảng 2 V khiến động cơ chạy ở vùng yếu. Bên cạnh đó, chân echo của HC-SR04 xuất mức 5 V trong khi chân GPIO của ESP32 và Pico không chịu được 5 V; chỉ cần thêm một mạch chuyển mức giá khoảng 10.000 ₫ là giải quyết triệt để. Toàn bộ các vấn đề này đều được phòng tránh bằng cách đọc kỹ nhãn in trên mạch trước khi cắm pin.&lt;/p&gt;

&lt;h2 id=&quot;các-họ-giắc-nối-bước-chân-không-thể-ép-vừa&quot;&gt;Các họ giắc nối: bước chân không thể ép vừa&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Dây Dupont&lt;/strong&gt; (đầu cắm vuông đơn bước chân 2,54 mm) được thiết kế cho breadboard và đường tín hiệu logic nối vào TB6612. Do tiết diện lõi đồng mảnh và tiếp điểm nhỏ, dòng kẹt gần 1 A đi qua đầu bấm Dupont dễ gây nóng và làm sụt áp. Do đó, chỉ dùng Dupont cho tín hiệu; đường pin vào VM và đường ra động cơ cần dây tiết diện lớn hơn và được bắt chặt qua trạm vít hoặc hàn cố định.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JST-XH&lt;/strong&gt; là chuẩn giắc có bước chân 2,50 mm (thường gặp ở dây sạc cân bằng và giắc pin nhỏ). Dù 2,50 mm gần giống 2,54 mm, việc cố ép cắm vào hàng chân 2,54 mm sẽ làm cong gãy chân. &lt;strong&gt;Giắc servo kiểu JR&lt;/strong&gt; có ba chân bước 2,54 mm kèm gờ định hướng chống cắm ngược — tuyệt đối không gọt bỏ gờ này. Với dòng công suất lớn hơn, &lt;strong&gt;XT30&lt;/strong&gt; và &lt;strong&gt;XT60&lt;/strong&gt; dùng chân đồng dạng đạn và hoàn toàn không tương thích lẫn nhau hay với JST. Nếu hai đầu giắc không trượt vào nhẹ nhàng và khớp khít, hãy dừng ngay; cố dùng lực rất dễ làm đảo cực pin trên chân VM.&lt;/p&gt;

&lt;h2 id=&quot;so-sánh-sụt-áp-mạch-lái-ở-nguồn-74-v&quot;&gt;So sánh sụt áp mạch lái ở nguồn 7,4 V&lt;/h2&gt;

&lt;p&gt;Cầu H chỉ đóng vai trò đóng cắt điều hướng dòng điện cho động cơ chứ không tự sinh ra dòng: vi điều khiển ra lệnh chọn chiều quay và PWM, còn nguồn pin nối vào chân VM mới là nơi cung cấp dòng điện.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;Vi điều khiển lái cầu; pin cấp dòng cho mô-tơ&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Module TB6612FNG&lt;/strong&gt; dùng cầu MOSFET. Theo thông số trên Hshop (ngày 23 tháng 9 năm 2026), mạch hỗ trợ VM từ 4,5 V đến 10 V, nguồn logic từ 2,7 V đến 5,5 V, dòng liên tục 1,2 A và dòng đỉnh 3,2 A mỗi kênh; mức cao 3,3 V của GPIO hoàn toàn hợp lệ. Với độ sụt áp thực tế khoảng 0,3 V ở dòng trung bình:&lt;/p&gt;

\[V_\mathrm{motor} \approx 7.4 - 0.3 = 7.1\ \mathrm{V}\]

&lt;p&gt;Ngược lại, &lt;strong&gt;module L298N&lt;/strong&gt; (cùng giá 45.000 ₫ nhưng không chọn làm mặc định) sử dụng cầu Darlington nên sụt khoảng 2,0 V khi có tải:&lt;/p&gt;

\[V_\mathrm{motor} \approx 7.4 - 2.0 = 5.4\ \mathrm{V}\]

&lt;p&gt;Động cơ TT của khóa học hoạt động trong dải 3–9 V; ở 5,4 V động cơ vẫn quay nhưng chậm và yếu hơn hẳn mức 7,1 V, đặc biệt khi pin vơi dần. Ngoài ra, không nên lấy nguồn 5 V từ ổn áp tích hợp trên module L298N để nuôi ESP32.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Dosmotorsl298n.jpg&quot; alt=&quot;Module kiểu L298N: kéo được tải, và sụt áp lớn&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DRV8833&lt;/strong&gt; (giá tham khảo 25.000 ₫ trên Hshop) là lựa chọn thay thế gọn hơn, dải VM 2,7–10,8 V và tối đa 1,5 A mỗi kênh. Mạch hoạt động tốt khi mỗi kênh kéo một động cơ TT, nhưng vỏ nhỏ khiến chip nóng nhanh nếu bánh xe kẹt lâu; tuyệt đối không đấu song song hai động cơ lên cùng một kênh DRV8833. Riêng với TB6612, luôn nhớ kéo chân &lt;strong&gt;STBY&lt;/strong&gt; lên mức cao, bởi nếu để thấp hoặc bỏ nổi, cầu H sẽ ở chế độ ngủ và động cơ đứng yên dù mã nguồn không báo lỗi.&lt;/p&gt;

&lt;h2 id=&quot;mạch-vi-điều-khiển-và-giới-hạn-điện-áp-5-v&quot;&gt;Mạch vi điều khiển và giới hạn điện áp 5 V&lt;/h2&gt;

&lt;p&gt;Một &lt;strong&gt;mạch phát triển (dev board) ESP32&lt;/strong&gt; hoàn chỉnh có sẵn cổng USB, chip chuyển đổi USB-UART (như CH340 hoặc CP2102) nằm cạnh cổng USB, mạch ổn áp 3,3 V và module ESP32 bọc vỏ kim loại. Ngược lại, &lt;strong&gt;module ESP32-WROOM trần&lt;/strong&gt; chỉ gồm phần vỏ kim loại cùng hàng chân hàn viền ngoài, không có cổng USB lẫn mạch ổn áp. Nếu sản phẩm không có cổng USB, đó không phải mạch phát triển yêu cầu trong BOM.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/ESP32_on_Lolin32_Lite_clone_board_cropped.jpg&quot; alt=&quot;ESP32 trên board phát triển nhỏ, có USB và ổn áp&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/pcb_mcu_anatomy.png&quot; alt=&quot;Những thứ cần nhìn trên board vi điều khiển: USB, ổn áp, module, header&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Raspberry Pi Pico&lt;/strong&gt; có hai hàng chân header song song (bản gốc không có module Wi-Fi bọc kim loại). Trên board này, &lt;strong&gt;VBUS&lt;/strong&gt; là đường nguồn 5 V lấy từ cổng USB khi cắm máy tính, &lt;strong&gt;3V3&lt;/strong&gt; là đầu ra đã ổn áp, và các chân GPIO chạy ở mức logic 3,3 V. Không lấy nguồn từ chân 3V3 để chạy động cơ TT và không nhầm VBUS là chân tín hiệu GPIO.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Raspberry_Pi_Pico.jpg&quot; alt=&quot;Raspberry Pi Pico: header hai hàng, logic 3,3 V&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_rails_3v3.png&quot; alt=&quot;Logic 3,3 V, USB 5 V, nguồn mô-tơ tách khỏi rail đó&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Đối với cảm biến siêu âm &lt;strong&gt;HC-SR04&lt;/strong&gt;, chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Trig&lt;/code&gt; thường nhận tốt xung kích 3,3 V từ vi điều khiển, nhưng chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; xuất mức cao xấp xỉ 5 V nên tuyệt đối không cắm thẳng vào GPIO 3,3 V của ESP32 hay Pico. Cách an toàn và gọn nhất là dùng mạch chuyển mức logic 4 kênh (giá khoảng 10.000 ₫): nối phía 3,3 V với vi điều khiển, phía 5 V với cảm biến và nối mass chung. Cách thay thế là lắp cầu chia áp riêng cho chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; bằng điện trở 10 kΩ (phía trên) và 20 kΩ (phía dưới):&lt;/p&gt;

\[V_\mathrm{out} = 5 \times \frac{20}{10 + 20} \approx 3.3\ \mathrm{V}\]

&lt;p&gt;mức điện áp hoàn toàn an toàn cho chân 3,3 V. Không lắp cầu chia áp lên chân đầu vào cần mức cao 5 V nếu chưa kiểm chứng cảm biến có nhận được 3,3 V hay không; thông thường chỉ cần hạ mức chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt;, còn chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Trig&lt;/code&gt; có thể nối trực tiếp từ GPIO.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành&quot;&gt;Thực hành&lt;/h2&gt;

&lt;p&gt;Thực hiện toàn bộ các bước sau khi &lt;strong&gt;đã tháo pin và chưa gắn động cơ&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Chụp ảnh cận cảnh IC trên mạch lái đủ nét để đọc rõ ký hiệu (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L298N&lt;/code&gt;). Nếu ảnh chỉ thấy nhãn của bên bán module mà không thấy ký hiệu trên thân IC, hãy ghi chú rõ điều này.&lt;/li&gt;
  &lt;li&gt;Chụp ảnh chip USB-UART trên mạch vi điều khiển (thường là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CH340&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CP2102&lt;/code&gt; nằm gần cổng USB của ESP32). Nếu mạch không có cổng USB và chip nối tiếp, bạn đang cầm module trần.&lt;/li&gt;
  &lt;li&gt;Dựa theo thông số TB6612, vẽ nhanh sơ đồ chân lên giấy và ghi nhãn đầy đủ: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, sau đó đối chiếu với ký hiệu in lụa trên module (nếu đã có hàng). Xác nhận rõ: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; là nguồn động cơ (4,5–10 V), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; là nguồn logic (2,7–5,5 V, nối từ chân 3,3 V của MCU), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; nối mass chung, và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; phải kéo lên mức cao.&lt;/li&gt;
  &lt;li&gt;Trên ESP32 hoặc Pico, chỉ ra đúng ba vị trí: chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;, chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; (hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt;) và một chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO&lt;/code&gt;. Tự kiểm tra xem chân nào trong ba chân đó được phép nối thẳng vào chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Echo&lt;/code&gt; của HC-SR04 — đáp án đúng là &lt;strong&gt;không chân nào&lt;/strong&gt; cho đến khi tín hiệu echo đã đi qua mạch chuyển mức hoặc cầu chia áp.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả kỳ vọng:&lt;/strong&gt; Bạn có hai ảnh chụp sắc nét và một bản vẽ chân có nhãn rõ ràng (toàn bộ dây động cơ vẫn để trong túi), thể hiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VM&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; là hai chân tách biệt hoàn toàn. Nếu một module lạ nối chung hai chân này làm một, tuyệt đối không cấp pin 7,4 V vào đó vì sẽ đưa quá áp lên chân logic.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng quan sát&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Cách khắc phục&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ đứng yên dù firmware đang xuất PWM đúng&lt;/td&gt;
      &lt;td&gt;Chân STBY bị bỏ nổi hoặc ở mức thấp&lt;/td&gt;
      &lt;td&gt;Kéo chân STBY lên mức cao theo đúng đường nguồn logic 3,3 V&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Xe chạy yếu trên bộ pin 7,4 V mới sạc&lt;/td&gt;
      &lt;td&gt;Sụt áp lớn của cầu Darlington L298N (chỉ còn ~5,4 V)&lt;/td&gt;
      &lt;td&gt;Chuyển sang dùng TB6612; không nâng áp pin vượt giới hạn VM để bù&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đầu cắm Dupont nối vào chân VM bị nóng&lt;/td&gt;
      &lt;td&gt;Dùng dây tín hiệu mảnh dẫn dòng động cơ&lt;/td&gt;
      &lt;td&gt;Thay bằng dây nguồn tiết diện lớn hơn; chỉ dùng Dupont cho AIN1, AIN2, PWMA, STBY&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chân GPIO bị hỏng sau khi nối HC-SR04&lt;/td&gt;
      &lt;td&gt;Đưa trực tiếp tín hiệu Echo 5 V vào chân 3,3 V&lt;/td&gt;
      &lt;td&gt;Lắp mạch chuyển mức hoặc cầu chia áp cho Echo trước khi thử lại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hai đầu giắc không thể cắm vừa nếu không ấn mạnh&lt;/td&gt;
      &lt;td&gt;Nhầm lẫn giữa các họ JST-XH, JR, XT30, XT60&lt;/td&gt;
      &lt;td&gt;Dừng lại, dùng đúng chuẩn giắc tương thích và không gọt gờ định hướng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý an toàn:&lt;/strong&gt; Luôn nhấc bánh xe khỏi mặt bàn và tháo rời pin trong bài này; giữ ngón tay cách xa đầu kim loại que đo khi thao tác với nguồn điện. Mọi phép tính với nguồn 7,4 V hiện thực hiện trên giấy cho tới khi bạn đo thực tế trên thang DC lớn hơn 10 V ở các bài sau.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=YYMsS50x1UY&quot;&gt;H Bridge Motor Control Basics Explained&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Giải thích nguyên lý bốn khóa đóng cắt của mạch cầu H trước khi làm việc với module thực tế.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=f7aySy_0URE&quot;&gt;Logic Level Converters - Learn &amp;amp; Example Project - TXS0108E&lt;/a&gt; — DIY Machines, tiếng Anh. Minh họa cách đấu nối mạch chuyển mức logic giữa thiết bị 5 V và vi điều khiển 3,3 V.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Với nguồn pin 7,4 V, tính lại điện áp tại động cơ khi qua cầu MOSFET (sụt 0,3 V) và cầu Darlington (sụt 2,0 V). Động cơ TT hoạt động tốt hơn ở mức nào, và vì sao không chọn L298N dù cùng giá 45.000 ₫?&lt;/li&gt;
  &lt;li&gt;Thông số DRV8833 ghi tối đa 1,5 A mỗi kênh, nhưng khi bánh kẹt vài giây thì chip nóng bỏng. Bạn đã chạm vào giới hạn nào, và vì sao con số “tối đa” không cho phép mắc song song hai động cơ lên cùng một kênh?&lt;/li&gt;
  &lt;li&gt;Liệt kê ba chân trên module TB6612 phù hợp để nối bằng dây Dupont, và một đường điện không nên dùng dây Dupont lâu dài.&lt;/li&gt;
  &lt;li&gt;Trong túi có một module ESP32-WROOM trần và một mạch NodeMCU-32S. Mạch nào sẽ hiện cổng serial khi cắm cáp USB, và cần chụp ảnh con chip nào nằm cạnh cổng USB?&lt;/li&gt;
  &lt;li&gt;Dây Echo của HC-SR04 sắp nối vào một chân GP của Raspberry Pi Pico. Dây Echo xuất mức điện áp bao nhiêu, chân VBUS dùng để làm gì, và linh kiện nào trong giỏ hàng phải đặt giữa Echo và chân GP?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Cầu MOSFET: $7{,}4 - 0{,}3 = 7{,}1\ \mathrm{V}$; cầu Darlington: $7{,}4 - 2{,}0 = 5{,}4\ \mathrm{V}$. Động cơ TT (dải 3–9 V) chạy khỏe và nhanh hơn rõ rệt ở 7,1 V; cùng giá bán nhưng hiệu suất điện áp khác nhau hoàn toàn nên TB6612 là lựa chọn đúng.
2. Chip đã chạm giới hạn tản nhiệt của vỏ nhỏ gần trần dòng 1,5 A. Thông số tối đa là giới hạn chịu đựng đỉnh chứ không phải dòng vận hành liên tục cho hai động cơ mắc song song trên một kênh.
3. Dây Dupont phù hợp cho các chân tín hiệu như AIN1, AIN2, PWMA và STBY; không nên dùng lâu dài cho đường nguồn động cơ VM.
4. NodeMCU-32S có sẵn cổng USB để hiện cổng serial; cần chụp ảnh chip chuyển đổi USB-UART (CH340 hoặc CP2102) nằm cạnh cổng cắm. Module WROOM trần không có chip này.
5. Dây Echo xuất mức cao khoảng 5 V. VBUS là đường nguồn USB 5 V của Pico (không phải GPIO và không nối Echo vào đây). Cần đặt mạch chuyển mức 4 kênh (nối mass chung) hoặc cầu chia áp ở giữa dây Echo và chân GP.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;p&gt;Trang sản phẩm của Pololu mô tả chi tiết mạch lái &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG carrier&lt;/a&gt;, kèm trang thông số của phương án thay thế &lt;a href=&quot;https://www.ti.com/product/DRV8833&quot;&gt;DRV8833&lt;/a&gt;. Bài viết &lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;Logic levels&lt;/a&gt; của SparkFun giải thích rõ sự chênh lệch mức logic 3,3 V và 5 V, còn giới hạn chân GPIO cụ thể nằm trong &lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;ESP32 datasheet&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Các hạng mục còn lại của xe nằm ở bài &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;. Dưới đây là các mức giá tham khảo trên Hshop ngày 23 tháng 9 năm 2026 (cần kiểm tra lại giá hiện hành trước khi đặt):&lt;/p&gt;

&lt;p&gt;Mạch lái mặc định giá 45.000 ₫: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-tb6612fng-dc-motor-driver&quot;&gt;TB6612FNG&lt;/a&gt; (VM 4,5–10 V, logic 2,7–5,5 V, 1,2 A liên tục / 3,2 A đỉnh). Phương án nhỏ hơn giá 25.000 ₫: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-drv8833-dc-motor-driver&quot;&gt;DRV8833&lt;/a&gt; (VM 2,7–10,8 V, tối đa 1,5 A/kênh). Mạch &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;module L298N&lt;/a&gt; cũng có giá 45.000 ₫ nhưng không dùng làm mặc định do sụt áp lớn.&lt;/p&gt;

&lt;p&gt;Mạch chuyển mức cho chân Echo của HC-SR04 giá 10.000 ₫: &lt;a href=&quot;https://hshop.vn/mach-chuyen-muc-tin-hieu-logic-4-kenh&quot;&gt;mạch chuyển mức 4 kênh&lt;/a&gt;. Mạch phát triển ESP32 có sẵn USB giá 190.000 ₫: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;NodeMCU-32S CH340&lt;/a&gt;. Với Raspberry Pi Pico, bạn có thể tra cứu qua trang tìm kiếm Hshop: &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;tìm Raspberry Pi Pico&lt;/a&gt; (ngày 23 tháng 9 năm 2026 ghi nhận Pico 2 giá 195.000 ₫ và Pico 2 W giá 275.000 ₫). Luôn chọn mạch phát triển có sẵn cổng USB thay vì module trần trừ khi bạn đã có mạch nạp riêng.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Transistor, MOSFET, IC ổn áp và dạng vỏ</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_05_transistor_mosfet_ic_id/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_05_transistor_mosfet_ic_id</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ loại bỏ thói quen đoán sơ đồ chân linh kiện chỉ dựa vào hình dạng vỏ ngoài: cùng vỏ TO-92 hoặc TO-220 có thể là 2N2222, BC547, 2N3904, AMS1117, 7805 hay một MOSFET nhỏ — chỉ ký hiệu in trên thân kết hợp datasheet mới xác định đúng danh tính linh kiện. Bạn sẽ đánh số chân IC vỏ DIP chính xác từ dấu chấm hoặc rãnh khuyết theo chiều ngược kim đồng hồ, giải thích được vì sao cực cổng (gate) MOSFET khác với cực bazơ (base) của transistor lưỡng cực, và hiểu vì sao điện áp ngưỡng $V_{GS(th)}$ không phải mức điện áp để MOSFET dẫn bão hòa hoàn toàn khi lái động cơ. Bạn cũng sẽ so sánh vai trò công tắc đơn của 2N2222 với mạch cầu H TB6612, đồng thời tính được tổn hao nhiệt để giải thích vì sao không được cấp 12 V vào chân 5 V của ESP32.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-gì-trước&quot;&gt;Cần có gì trước&lt;/h2&gt;

&lt;p&gt;Bạn đã biết xác định catot của diode bằng vạch sơn và sử dụng thang đo diode trên linh kiện không cấp nguồn. Bạn cũng nhớ chân GPIO chỉ chịu dòng khoảng 12 mA đến 40 mA trong khi động cơ TT khi kẹt kéo dòng cỡ ampe. Bài này tập trung vào kỹ năng nhận dạng vỏ linh kiện cùng hai yếu tố tránh cháy hỏng: đúng thứ tự chân và kiểm soát công suất tỏa nhiệt.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Nếu vi điều khiển là “bộ não” thì &lt;strong&gt;transistor, MOSFET và IC ổn áp&lt;/strong&gt; chính là “cơ bắp và hệ tuần hoàn” của xe robot. Một chân GPIO chỉ đủ sức thắp một đèn LED nhỏ 10 mA, nhưng nhờ một con MOSFET công suất nhỏ bằng hạt đậu, tín hiệu 3,3 V yếu ớt ấy có thể đóng ngắt dòng điện hàng chục ampe cho động cơ! Ngược lại, chọn nhầm một IC ổn áp tuyến tính để hạ trực tiếp pin 8,4 V xuống 3,3 V sẽ biến bo mạch thành một chiếc lò sưởi mini chỉ sau 10 giây.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-linh-kiện-bán-dẫn-công-suất-và-ổn-áp-trên-robot&quot;&gt;Kiến thức cần nhớ: Linh kiện Bán dẫn Công suất và Ổn áp trên Robot&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Transistor Lưỡng cực (BJT) vs. MOSFET:&lt;/strong&gt; BJT điều khiển bằng &lt;em&gt;dòng điện&lt;/em&gt; cực Base (\(I_C = \beta I_B\), sụt áp \(V_{CE(\text{sat})} \approx 0.2\text{–}1\text{ V}\)); MOSFET điều khiển bằng &lt;em&gt;điện áp&lt;/em&gt; cực Gate (\(V_{GS}\)) với dòng kích gần bằng 0 và điện trở mở \(R_{DS(\text{on})}\) chỉ vài chục mili-ohm — mát hơn và hiệu quả hơn hẳn cho đóng cắt động cơ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cạm bẫy Điện áp Ngưỡng \(V_{GS(\text{th})}\):&lt;/strong&gt; Trong datasheet, \(V_{GS(\text{th})}\) là mức điện áp MOSFET &lt;em&gt;mới chớm mở&lt;/em&gt; (dòng chỉ vài trăm \(\mu\text{A}\)), &lt;strong&gt;không phải&lt;/strong&gt; mức mở bão hòa hoàn toàn! Với GPIO 3,3 V, phải chọn &lt;strong&gt;Logic-Level MOSFET&lt;/strong&gt; có thông số \(R_{DS(\text{on})}\) được đặc tả ngay tại \(V_{GS} = 2.5\text{–}3.3\text{ V}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ổn áp Tuyến tính (LDO như AMS1117) vs. Ổn áp Xung (Buck Converter):&lt;/strong&gt; LDO đốt toàn bộ phần điện áp dư \((V_{\text{in}} - V_{\text{out}}) \cdot I\) thành nhiệt; Buck đóng ngắt cuộn cảm ở tần số cao đạt hiệu suất \(85\text{–}95\%\), bắt buộc dùng khi chênh lệch áp lớn hoặc dòng tải cao.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-so-sánh-tỏa-nhiệt-giữa-ldo-tuyến-tính-và-mạch-buck-ở-dòng-04-a&quot;&gt;Ví dụ làm từng bước: So sánh tỏa nhiệt giữa LDO tuyến tính và mạch Buck ở dòng 0,4 A&lt;/h3&gt;

&lt;p&gt;Giả sử bạn hạ nguồn pin Li-ion 2S đầy (\(V_{\text{in}} = 8.4\text{ V}\)) xuống \(V_{\text{out}} = 3.3\text{ V}\) để nuôi ESP32 + cảm biến ăn dòng \(I = 0.4\text{ A}\) (công suất hữu ích \(P_{\text{out}} = 3.3 \times 0.4 = \mathbf{1.32\text{ W}}\)):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Loại mạch ổn áp&lt;/th&gt;
      &lt;th&gt;Công suất tỏa nhiệt \(P_{\text{nhiệt}}\)&lt;/th&gt;
      &lt;th&gt;Hiệu suất \(\eta\)&lt;/th&gt;
      &lt;th&gt;Hậu quả thực tế trên bàn lab&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;LDO tuyến tính (AMS1117-3.3)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\((8.4 - 3.3) \times 0.4 = \mathbf{2.04\text{ W}}\)&lt;/td&gt;
      &lt;td&gt;\(\dfrac{3.3}{8.4} = \mathbf{39.3\%}\)&lt;/td&gt;
      &lt;td&gt;Vỏ SOT-223 nóng vọt quá \(120^\circ\text{C}\) → tự ngắt nhiệt hoặc cháy!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Mạch hạ áp xung Buck (\(\eta = 90\%\))&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;$$1.32 \times \left(\dfrac{1}{0.90} - 1&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ight) = \mathbf{0.147\text{ W}}$$&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(90\%\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Mát hơn &lt;strong&gt;14 lần&lt;/strong&gt; — chạm tay chỉ hơi ấm nhẹ!&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;giải-thích-4-bước-chọn-đúng-linh-kiện-bán-dẫn-cho-robot&quot;&gt;Giải thích: 4 bước chọn đúng linh kiện bán dẫn cho robot&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Đọc kỹ dạng vỏ (TO-92, TO-220, SOT-23, SOT-223):&lt;/strong&gt; Dạng vỏ quyết định khả năng thoát nhiệt tối đa — cùng là ổn áp 3,3 V nhưng vỏ TO-220 chịu nhiệt tốt hơn vỏ SOT-23 gấp nhiều lần.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Luôn tra \(R_{DS(\text{on})}\) tại đúng điện áp GPIO (3,3 V):&lt;/strong&gt; Nếu datasheet chỉ ghi \(R_{DS(\text{on})}\) tại \(V_{GS} = 10\text{ V}\) (như IRF540N cổ điển), cắm vào ESP32 3,3 V sẽ làm MOSFET mở lửng, nóng rực và động cơ quay yếu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Dùng IC cầu H tích hợp (TB6612FNG / DRV8833) thay vì tự ráp 4 transistor rời:&lt;/strong&gt; IC tích hợp sẵn mạch bảo vệ chống ngắn mạch chéo (&lt;em&gt;shoot-through&lt;/em&gt;), diode dập xung và chân điều khiển PWM gọn gàng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Dùng Buck cho tầng hạ áp lớn, LDO cho tầng lọc sạch cuối:&lt;/strong&gt; Hạ 8,4 V → 5,0 V bằng Buck rồi dùng LDO tích hợp trên bo ESP32 hạ 5,0 V → 3,3 V là công thức tối ưu cả hiệu suất lẫn độ sạch nhiễu!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu LDO trên bo ESP32 hạ từ \(5.0\text{ V}\) (đầu ra Buck) xuống \(3.3\text{ V}\) ở dòng \(0.25\text{ A}\), công suất nhiệt trên LDO còn bao nhiêu? Chỉ \((5.0 - 3.3) \times 0.25 = \mathbf{0.425\text{ W}}\) — hoàn toàn nằm trong ngưỡng an toàn của vỏ SOT-223!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-xe-hai-bánh-không-dùng-transistor-rời-để-lái-động-cơ&quot;&gt;Vì sao xe hai bánh không dùng transistor rời để lái động cơ&lt;/h2&gt;

&lt;p&gt;Mạch ESP32 tích hợp sẵn IC ổn áp AMS1117-3.3 (hoặc tương đương) giữa nguồn USB 5 V và đường nguồn 3,3 V; Raspberry Pi Pico cũng có mạch ổn áp riêng giữa VBUS và 3V3. Nhờ đó vi điều khiển xuất mức logic 3,3 V tương thích với dải logic 2,7 V đến 5,5 V của TB6612. Tuy nhiên, sự có mặt của ổn áp trên mạch không đồng nghĩa bạn được phép đưa nguồn pin 12 V vào chân 5 V của board.&lt;/p&gt;

&lt;p&gt;Nhiều người mới nghĩ có thể dùng một transistor nhỏ làm công tắc bật/tắt động cơ TT trực tiếp từ chân GPIO. Thực tế, động cơ là tải cảm nên khi khóa ngắt bắt buộc phải có diode hồi tiếp, và dòng kẹt của động cơ TT dễ vượt quá giới hạn chịu dòng của 2N2222. Ngay cả với MOSFET, nếu chọn loại có ngưỡng $V_{GS(th)}$ quanh 2 V thì ở mức điều khiển 3,3 V kênh dẫn vẫn có thể chỉ mở một phần, gây nóng và sụt áp. Module TB6612 giải quyết trọn vẹn bài toán này cho hai kênh độc lập (1,2 A liên tục, 3,2 A đỉnh). Bài học này giúp bạn nhận biết chính xác các họ vỏ linh kiện để tránh cắm nhầm IC ổn áp vào vị trí transistor.&lt;/p&gt;

&lt;h2 id=&quot;vỏ-linh-kiện-và-nguyên-tắc-đọc-ký-hiệu-trên-thân&quot;&gt;Vỏ linh kiện và nguyên tắc đọc ký hiệu trên thân&lt;/h2&gt;

&lt;p&gt;TO-92 là dạng vỏ nhựa đen ba chân cỡ nhỏ, còn TO-220 là dạng vỏ công suất lớn hơn có tai kim loại bắt tản nhiệt. Rất nhiều loại linh kiện khác nhau dùng chung hai chuẩn vỏ này: 2N2222, BC547 và 2N3904 đều là transistor lưỡng cực NPN nhưng có thứ tự chân khác nhau — ngay cả cặp cùng họ nổi tiếng là P2N2222 và 2N2222 cũng đảo thứ tự chân cho nhau. Trong khi đó, AMS1117 và 7805 là IC ổn áp chứ không phải transistor đóng cắt. Nếu đấu một MOSFET vỏ TO-92 theo sơ đồ chân học thuộc của 2N2222, linh kiện sẽ hỏng ngay lập tức.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/component_lookalikes.png&quot; alt=&quot;Nhiều vỏ cùng bóng dáng nhưng không cùng sơ đồ chân&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Nguyên tắc bắt buộc là luôn đọc rõ ký hiệu in trên mặt phẳng của thân linh kiện rồi mở đúng datasheet tương ứng. Nếu chữ in bị mờ không đọc được, hãy xếp vào nhóm chưa xác định và tuyệt đối không cắm thử lên chân GPIO đang có điện.&lt;/p&gt;

&lt;p&gt;Đối với IC vỏ &lt;strong&gt;DIP&lt;/strong&gt; (hai hàng chân song song), bạn đặt đầu có rãnh khuyết hoặc dấu chấm hướng lên trên: chân số 1 nằm ở góc trên bên trái, sau đó đánh số tăng dần xuống hết cạnh trái rồi vòng lên hết cạnh phải (theo chiều ngược kim đồng hồ khi nhìn từ mặt trên). Quy tắc này cũng rất hữu ích khi bạn lắp các IC chuyển mức logic bắc ngang rãnh giữa breadboard.&lt;/p&gt;

&lt;h2 id=&quot;transistor-lưỡng-cực-mosfet-và-hiểu-lầm-về-v_gsth&quot;&gt;Transistor lưỡng cực, MOSFET và hiểu lầm về $V_{GS(th)}$&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Transistor lưỡng cực NPN&lt;/strong&gt; khi làm việc như công tắc cần có dòng điều khiển đi vào cực bazơ (thường chọn khoảng 1/10 dòng colectơ) qua một điện trở từ chân GPIO. Với tải 150 mA, dòng bazơ cần khoảng 15 mA — đã sát ngưỡng khuyến nghị của GPIO. Nếu động cơ kẹt ở 800 mA, dòng bazơ tương ứng lên tới ~80 mA, vượt xa khả năng cấp dòng của GPIO lẫn giới hạn của chính 2N2222. Ngoài ra, khi transistor ngắt đột ngột, cuộn dây động cơ sinh xung áp ngược nên bắt buộc phải mắc diode hồi tiếp (vạch catot hướng về cực dương nguồn động cơ) để bảo vệ cực colectơ.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MOSFET&lt;/strong&gt; hoạt động theo nguyên lý điều khiển bằng điện áp đặt lên cực cổng (gate) thay vì duy trì dòng liên tục. Khi đóng cắt thông thường, cực cổng gần như không rút dòng tĩnh; điện trở kênh dẫn $R_{DS(on)}$ phụ thuộc vào mức điện áp cổng so với cực nguồn ($V_{GS}$). Cần đặc biệt lưu ý: thông số điện áp ngưỡng $V_{GS(th)}$ trên datasheet chỉ là mốc mà tại đó MOSFET &lt;em&gt;bắt đầu&lt;/em&gt; dẫn một dòng thử rất nhỏ (thường khoảng 250 µA), &lt;strong&gt;không phải&lt;/strong&gt; mức điện áp để MOSFET dẫn bão hòa hoàn toàn. Nhiều MOSFET ghi $V_{GS(th)}$ trong khoảng 2–4 V nhưng chỉ đảm bảo $R_{DS(on)}$ thấp tại $V_{GS} = 4{,}5\ \mathrm{V}$ hoặc $10\ \mathrm{V}$. Nếu điều khiển cực cổng chỉ ở mức “vừa qua ngưỡng” (như 3,3 V), MOSFET sẽ rơi vào vùng tuyến tính, gây sụt áp lớn, tỏa nhiệt mạnh và làm động cơ chạy yếu.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MOSFET mức logic (logic-level MOSFET)&lt;/strong&gt; là loại được nhà sản xuất công bố $R_{DS(on)}$ thấp ngay ở mức $V_{GS}$ nhỏ (như 2,5 V hoặc 4,5 V). Dù vậy, dùng một MOSFET rời làm khóa phía thấp cho động cơ TT vẫn chỉ quay được một chiều và vẫn cần diode hồi tiếp. Vì robot cần điều khiển hai động cơ quay thuận/nghịch bằng PWM, ta dùng mạch cầu H tích hợp sẵn MOSFET là TB6612 thay vì tự ráp mảng transistor rời trên breadboard.&lt;/p&gt;

&lt;h2 id=&quot;ic-ổn-áp-tuyến-tính-và-công-suất-tỏa-nhiệt&quot;&gt;IC ổn áp tuyến tính và công suất tỏa nhiệt&lt;/h2&gt;

&lt;p&gt;Các IC ổn áp tuyến tính như 7805 duy trì điện áp đầu ra cố định 5 V bằng cách tiêu tán toàn bộ phần chênh lệch điện áp thành nhiệt năng. Công suất tỏa nhiệt trên ổn áp được tính bằng:&lt;/p&gt;

\[P = (V_\mathrm{in} - V_\mathrm{out}) \, I\]

&lt;p&gt;Xét trường hợp nhẹ với nguồn vào 9 V, đầu ra 5 V và dòng tải 200 mA:&lt;/p&gt;

\[P = (9 - 5) \times 0.2 = 0.8\ \mathrm{W}\]

&lt;p&gt;Vỏ TO-220 có tai kim loại tản nhiệt có thể chịu được mức 0,8 W này (vỏ sẽ hơi ấm). Bây giờ, xét tình huống đưa 12 V vào chân 5 V của mạch ESP32 (nơi IC ổn áp AMS1117-3.3 hạ xuống 3,3 V ở dòng tải 150 mA khi bật Wi-Fi):&lt;/p&gt;

\[P = (12 - 3.3) \times 0.15 \approx 1.3\ \mathrm{W}\]

&lt;p&gt;Công suất 1,3 W là quá lớn đối với vỏ SOT-223 nhỏ gắn trên một mạch in mỏng. Nguy hiểm hơn, trên nhiều mạch phát triển, chân ghi 5V nối chung trực tiếp với đường 5 V của cổng USB và chip giao tiếp UART, nên cấp 12 V vào đó có thể phá hủy cả chip UART lẫn cổng USB. Giới hạn điện áp vào tối đa ghi trên datasheet riêng của AMS1117 không bảo vệ phần còn lại của mạch. Do đó trên bàn lab, hãy luôn cấp nguồn ESP32/Pico qua USB, còn pin động cơ chỉ nối vào chân VM của TB6612 (4,5–10 V) và nối chung mass.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-phân-loại-bằng-ký-hiệu-trước-khi-đo-kiểm-tiếp-giáp&quot;&gt;Ví dụ: phân loại bằng ký hiệu trước khi đo kiểm tiếp giáp&lt;/h2&gt;

&lt;p&gt;Giả sử trên khay có bốn linh kiện không thể phân biệt chỉ qua dáng vỏ: TO-92 ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BC547&lt;/code&gt;, TO-92 ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2N2222&lt;/code&gt;, TO-220 ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7805&lt;/code&gt; và một MOSFET có ký hiệu đọc rõ. Ta phân loại ngay được: BC547 và 2N2222 thuộc họ transistor NPN (nhưng khác thứ tự chân), 7805 là IC ổn áp tuyến tính 5 V, và linh kiện cuối là MOSFET điều khiển bằng điện áp. Tuy nhiên, việc gọi tên họ vẫn chưa đủ để biết chân nào là cực phát (emitter) — bắt buộc phải mở đúng datasheet theo mã in trên thân.&lt;/p&gt;

&lt;p&gt;Khi linh kiện rời ngoài mạch và không cấp nguồn, thang đo diode trên đồng hồ có thể dùng để kiểm tra nhanh tình trạng tiếp giáp của transistor lưỡng cực (chứ không thay thế datasheet để chốt sơ đồ chân). Hai tiếp giáp bazơ–phát (B–E) và bazơ–góp (B–C) hoạt động giống hai diode, cho điện áp thuận quanh 0,6 V ở một chiều và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; ở chiều ngược lại; đường phát–góp (E–C) không được thông mạch cả hai chiều. Chân dùng chung cho cả hai tiếp giáp thuận là ứng viên cực bazơ. Phép đo này rất hữu ích để phát hiện nhầm lẫn: nếu một linh kiện vỏ ba chân không cho kết quả hai tiếp giáp diode như trên, rất có thể đó là một IC ổn áp như 7805 chứ không phải transistor.&lt;/p&gt;

&lt;p&gt;Dù đã kiểm tra tốt một con 2N2222, bạn vẫn không dùng nó để kéo động cơ TT trên xe đồ án; việc đảo chiều và hãm động cơ cần cầu H TB6612 chịu 1,2 A liên tục, 3,2 A đỉnh và nhận trực tiếp logic 3,3 V.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành&quot;&gt;Thực hành&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nếu có sẵn các linh kiện rời, hãy phân loại chúng theo chữ in trên thân thành bốn nhóm: transistor lưỡng cực, MOSFET, IC ổn áp và chưa xác định. Nếu chưa có linh kiện rời, thực hiện phân loại tương tự dựa trên các hình minh họa trong bài và các IC hàn sẵn trên board trong bộ kit.&lt;/li&gt;
  &lt;li&gt;Quan sát bất kỳ IC vỏ DIP nào bạn có (hoặc trong hình): tìm rãnh khuyết hoặc dấu chấm, xác định chân số 1 và đọc thứ tự chạy xuống cạnh trái rồi vòng lên cạnh phải.&lt;/li&gt;
  &lt;li&gt;Trên mạch ESP32, tìm IC ổn áp 3,3 V (thường thuộc họ AMS1117) nằm gần cổng USB; xác định vị trí ký hiệu in lụa của chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; và chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; (tuyệt đối không cấp 12 V để thử).&lt;/li&gt;
  &lt;li&gt;Ở trạng thái mất nguồn và linh kiện rời ngoài mạch, dùng thang đo diode kiểm tra một diode đã biết và một transistor lưỡng cực đã biết (nếu có) rồi ghi lại hai giá trị tiếp giáp. Nếu chưa có transistor rời, bỏ qua bước đo và giữ nguyên bảng phân loại ở bước 1; không đo thang diode trên linh kiện đang hàn trên mạch có điện.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả kỳ vọng:&lt;/strong&gt; Bạn hoàn thành bảng phân loại linh kiện có ghi rõ mã và họ (không quay động cơ trong bài này). Phép đo diode trên transistor lưỡng cực tốt cho điện áp khoảng 0,5–0,7 V trên hai tiếp giáp chung một chân và không chập giữa hai chân còn lại; IC ổn áp 7805 sẽ không cho kết quả giống như vậy.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng quan sát&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Cách khắc phục&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Hai linh kiện vỏ TO-92 nhưng cắm chung sơ đồ chân không chạy&lt;/td&gt;
      &lt;td&gt;Khác mã linh kiện, hoặc nhầm cặp 2N2222 với P2N2222&lt;/td&gt;
      &lt;td&gt;Đọc chính xác mã in trên thân và tra đúng datasheet từng loại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thang diode báo ngắn mạch cả hai chiều trên mọi chân&lt;/td&gt;
      &lt;td&gt;Linh kiện đã hỏng, hoặc đang đo khi còn hàn trên mạch có nhánh song song&lt;/td&gt;
      &lt;td&gt;Tháo linh kiện ra khỏi mạch; nếu vẫn chập thì loại bỏ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;IC ổn áp nóng gắt khi cấp 12 V vào chân 5 V&lt;/td&gt;
      &lt;td&gt;Tổn hao nhiệt tuyến tính lớn và quá áp trên đường nguồn USB&lt;/td&gt;
      &lt;td&gt;Ngắt nguồn 12 V ngay; cấp nguồn board bằng USB và nối pin động cơ riêng vào VM&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cực cổng MOSFET nguội nhưng động cơ quay yếu và thân FET rất nóng&lt;/td&gt;
      &lt;td&gt;Điện áp $V_{GS}$ chỉ gần mức ngưỡng $V_{GS(th)}$ nên kênh chưa dẫn bão hòa&lt;/td&gt;
      &lt;td&gt;Dùng mạch lái chuyên dụng TB6612 thay vì điều khiển MOSFET rời ở vùng ngưỡng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý an toàn:&lt;/strong&gt; Không cấp nguồn động cơ hay nguồn 12 V trong bài thực hành này; chỉ dùng thang đo diode trên linh kiện rời. Trên các mạch đã cấp điện về sau, cần cẩn thận vì tai kim loại của vỏ TO-220 có thể vừa nóng vừa nối trực tiếp với một chân điện bên trong.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=AwRJsze_9m4&quot;&gt;MOSFET Explained - How MOSFET Works&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Giải thích nguyên lý điều khiển kênh dẫn bằng cực cổng của MOSFET; lưu ý đây mới là linh kiện đóng cắt đơn, chưa phải mạch cầu H.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Trên một IC vỏ DIP có rãnh khuyết, khi nhìn từ mặt trên với rãnh hướng lên trên, chân số 1 nằm ở đâu và thứ tự chân chạy theo chiều nào?&lt;/li&gt;
  &lt;li&gt;Một datasheet MOSFET ghi $V_{GS(th)}$ từ 2 V đến 4 V tại dòng thử $250\ \mu\mathrm{A}$, nhưng thông số $R_{DS(on)}$ chỉ được công bố tại $V_{GS} = 10\ \mathrm{V}$. Vì sao không thể lập luận “ngưỡng chỉ 2 V nên chân GPIO 3,3 V kéo động cơ thoải mái”?&lt;/li&gt;
  &lt;li&gt;Tính công suất tỏa nhiệt của IC ổn áp 7805 khi hạ áp từ 12 V xuống 5 V ở dòng tải 300 mA. Vỏ TO-220 trong trường hợp này có cần gắn thêm tản nhiệt không?&lt;/li&gt;
  &lt;li&gt;Có người đề xuất dùng 2N2222 làm công tắc phía thấp cho động cơ TT có dòng kẹt 0,8 A, chọn dòng bazơ bằng 1/10 dòng colectơ lấy trực tiếp từ chân GPIO. Hãy tính dòng bazơ yêu cầu và nêu linh kiện bảo vệ còn thiếu khi transistor ngắt.&lt;/li&gt;
  &lt;li&gt;Vì sao một linh kiện vỏ TO-92 bị mờ mất chữ còn nguy hiểm hơn cả việc không có linh kiện thay thế nếu định dùng nó thay cho ổn áp 3,3 V của ESP32?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Chân số 1 nằm ở góc trên bên trái ngay cạnh rãnh khuyết; số chân tăng dần xuống cạnh trái rồi vòng lên cạnh phải (ngược chiều kim đồng hồ khi nhìn từ trên xuống).
2. Điện áp ngưỡng $V_{GS(th)}$ chỉ là mốc bắt đầu dẫn dòng rất nhỏ (250 µA). Việc $R_{DS(on)}$ chỉ đảm bảo ở 10 V cho thấy ở mức 3,3 V, MOSFET có thể chỉ mở một phần, tỏa nhiệt mạnh và gây sụt áp lớn khiến động cơ chạy yếu. Cần MOSFET mức logic phù hợp, và tốt nhất cho xe hai bánh vẫn là dùng mạch lái TB6612.
3. $P = (12 - 5) \times 0{,}3 = 2{,}1\ \mathrm{W}$. Bắt buộc phải gắn tản nhiệt vì vỏ TO-220 trần tỏa trên 2 W sẽ rất nóng, dễ gây bỏng; đồng thời đây cũng không phải cách hợp lý để cấp nguồn cho ESP32.
4. Dòng bazơ yêu cầu khoảng $0{,}8 / 10 = 80\ \mathrm{mA}$, vượt xa giới hạn 12–40 mA của chân GPIO. Ngoài ra mạch còn thiếu diode hồi tiếp mắc song song động cơ. Giải pháp đúng là dùng TB6612.
5. Một linh kiện TO-92 không rõ mã có thể là transistor lưỡng cực, MOSFET hoặc ổn áp sai mức điện áp và sai thứ tự chân; lắp thay thế bừa có thể đẩy thẳng 5 V lên đường nguồn 3,3 V làm hỏng chip ESP32.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;p&gt;Bài hướng dẫn của SparkFun giải thích rõ nguyên lý transistor lưỡng cực và MOSFET: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/transistors&quot;&gt;Transistors&lt;/a&gt;. Trang sản phẩm của Texas Instruments cung cấp đầy đủ đặc tính nhiệt và điện áp của họ ổn áp 7805: &lt;a href=&quot;https://www.ti.com/product/LM7805&quot;&gt;LM7805&lt;/a&gt;. Luôn đọc mã in thực tế trên linh kiện của bạn trước khi tra sơ đồ chân.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Bạn không cần mua transistor rời để hoàn thành xe vi sai vì phần đóng cắt đã có mạch lái TB6612 trong danh sách &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Nếu muốn mua thêm một diode và một transistor lưỡng cực rõ mã để thực hành bài đo tiếp giáp không nguồn, nên chọn mua tại các cửa hàng linh kiện dán nhãn mã đầy đủ như &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; hoặc &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;, hoặc tra cứu qua trang tìm kiếm &lt;a href=&quot;https://shopee.vn/search?keyword=2N2222&quot;&gt;Shopee, tìm 2N2222&lt;/a&gt; rồi kiểm tra kỹ chữ in khi nhận hàng. Mạch phát triển ESP32 (tích hợp sẵn ổn áp 3,3 V) có giá tham khảo 190.000 ₫ trên Hshop ngày 23 tháng 9 năm 2026: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;ESP32 NodeMCU-32S&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Tụ điện, diode và LED</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_04_cap_diode_led_id/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_04_cap_diode_led_id</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ nhận biết được vạch đánh dấu chân âm trên tụ hóa nhôm, đồng thời phân biệt tụ gốm đĩa hoặc tụ dán không phân cực (trừ khi thân linh kiện có ký hiệu cực riêng). Bạn sẽ đọc được mã tụ gốm ba chữ số như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;104&lt;/code&gt; thành 100 nF, xác định chân catot của diode qua vạch sơn, và kiểm tra cực tính LED bằng thang đo diode trên đồng hồ thay vì chỉ dựa vào độ dài chân. Bạn cũng sẽ tính toán và giải thích được vì sao một tụ điện 100 µF mắc ngang chân VM của mạch lái giúp làm mượt các xung ngắn nhưng không thể thay thế bộ pin riêng cho hai động cơ TT.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-gì-trước&quot;&gt;Cần có gì trước&lt;/h2&gt;

&lt;p&gt;Bạn đã vận dụng được định luật Ohm $V = IR$, biết chọn điện trở hạn dòng cho LED đỏ trên nguồn 3,3 V, và nắm quy tắc chỉ sử dụng thang đo diode trên linh kiện đã ngắt nguồn (que đỏ nằm ở lỗ V/Ω, không ở lỗ đo dòng). Nếu chưa có bộ kit trên tay, các hình minh họa trong bài vẫn đủ để học cách nhận diện cực tính; phần thực hành chỉ dùng phép thử diode không cấp nguồn.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Nếu điện trở là chiếc van hãm dòng chảy, thì &lt;strong&gt;tụ điện&lt;/strong&gt; là chiếc bình tích áp nhỏ giữ ổn định đường nguồn khi động cơ đột ngột khởi động, còn &lt;strong&gt;diode&lt;/strong&gt; là chiếc van một chiều ngăn dòng điện chảy ngược làm cháy vi điều khiển. Rất nhiều người mới lầm tưởng cắm một tụ hóa \(100\ \mu\text{F}\) to vào đường nguồn là đủ “cứu” một viên pin yếu — nhưng chỉ cần làm một phép tính năng lượng 3 dòng, bạn sẽ thấy ngay giới hạn thật sự của tụ đệm!&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-tụ-điện-diode-và-led-trong-mạch-robot&quot;&gt;Kiến thức cần nhớ: Tụ điện, Diode và LED trong Mạch Robot&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tụ điện (Tụ gốm \(0.1\ \mu\text{F}\) vs. Tụ hóa \(100\text{–}470\ \mu\text{F}\)):&lt;/strong&gt; Tích trữ năng lượng trong điện trường (\(E = \tfrac{1}{2}CV^2\)) và chống lại sự thay đổi đột ngột của điện áp (\(I = C\frac{dV}{dt}\)). Tụ gốm nhỏ đặt sát chân IC lọc nhiễu cao tần; tụ hóa lớn (có cực tính âm–dương!) gánh các xung dòng ngắn vài chục micro-giây.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Diode (Van một chiều &amp;amp; Bảo vệ Xung ngược):&lt;/strong&gt; Chỉ dẫn dòng từ Anode sang Cathode (đầu có vạch trắng/bạc) khi vượt ngưỡng sụt áp thuận (\(V_f \approx 0.7\text{ V}\) cho diode silicon &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1N4007&lt;/code&gt;, \(0.3\text{ V}\) cho diode Schottky &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1N5819&lt;/code&gt;). Cực kỳ quan trọng làm &lt;strong&gt;diode dập xung ngược (flyback)&lt;/strong&gt; khi tắt cuộn dây động cơ/rơ-le!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;LED (Diode Phát quang):&lt;/strong&gt; Là một diode phát sáng với \(V_f\) cao hơn (\(1.8\text{–}3.2\text{ V}\) tùy màu), chân dài là Anode (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;), cạnh vát phẳng trên thân nhựa là Cathode (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;), và &lt;em&gt;luôn&lt;/em&gt; cần điện trở hạn dòng.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-năng-lượng-và-thời-gian-gánh-tải-của-tụ-đệm-100-mutextf&quot;&gt;Ví dụ làm từng bước: Tính năng lượng và thời gian gánh tải của tụ đệm \(100\ \mu\text{F}\)&lt;/h3&gt;

&lt;p&gt;Xét một tụ hóa \(C = 100\ \mu\text{F} = 10^{-4}\text{ F}\) mắc trên đường nguồn \(V = 5.0\text{ V}\) của xe robot:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tổng năng lượng tích trữ trong tụ:&lt;/strong&gt;
\(E = \frac{1}{2} C V^2 = \frac{1}{2} (10^{-4}\text{ F})(5.0\text{ V})^2 = \mathbf{1.25 \times 10^{-3}\text{ J} = 1.25\text{ mJ}}!\)
&lt;em&gt;(So sánh với 1 viên pin 18650 chứa tới &lt;strong&gt;30.000 J&lt;/strong&gt; — lớn hơn gấp &lt;strong&gt;24 triệu lần&lt;/strong&gt;!)&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thời gian tụ gánh được xung dòng động cơ \(I = 1.0\text{ A}\) trước khi điện áp sụt quá \(\Delta V = 0.5\text{ V}\):&lt;/strong&gt;
Từ \(I = C\dfrac{\Delta V}{\Delta t}\), rút ra:
\(\Delta t = \frac{C \cdot \Delta V}{I} = \frac{10^{-4} \times 0.5}{1.0} = \mathbf{5 \times 10^{-5}\text{ s} = 50\ \mu\text{s}}!\)
Con số &lt;strong&gt;50 micro-giây&lt;/strong&gt; nói lên tất cả: tụ \(100\ \mu\text{F}\) cực kỳ hiệu quả để dập gai nhiễu chuyển mạch siêu ngắn, nhưng hoàn toàn &lt;em&gt;không thể&lt;/em&gt; thay thế một nguồn pin đủ dòng khi động cơ khởi động kéo dài vài trăm mili-giây!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;giải-thích-4-quy-tắc-quan-trọng-với-tụ-điện-diode-và-led&quot;&gt;Giải thích: 4 quy tắc quan trọng với Tụ điện, Diode và LED&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Không bao giờ cắm ngược cực tụ hóa:&lt;/strong&gt; Tụ hóa có dải sọc trắng dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt; đánh dấu chân âm; cắm ngược cực hoặc vượt điện áp định mức (ví dụ dùng tụ 6,3 V trên pin 8,4 V) có thể làm tụ phồng và nổ bốp! Luôn chọn điện áp tụ tối thiểu gấp \(1.5\text{–}2\times\) điện áp nguồn (dùng tụ 16 V hoặc 25 V cho mạch 5–8,4 V).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Đọc mã 3 số trên tụ gốm (đơn vị picofarad):&lt;/strong&gt; Mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;104&lt;/code&gt; nghĩa là \(10 \times 10^4\text{ pF} = 100{,}000\text{ pF} = \mathbf{100\text{ nF} = 0.1\ \mu\text{F}}\) — tụ lọc nhiễu kinh điển đặt sát mọi chân nguồn IC!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Kiểm tra diode bằng thang đo Diode trên đồng hồ:&lt;/strong&gt; Que đỏ vào Anode, que đen vào Cathode sẽ đọc \(0.5\text{–}0.7\text{ V}\) (silicon) hoặc \(0.2\text{–}0.3\text{ V}\) (Schottky); đảo ngược que phải đọc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; (hở mạch).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Nhận diện cực LED bằng cạnh vát, không chỉ bằng độ dài chân:&lt;/strong&gt; Khi chân LED đã bị cắt bằng nhau trên mạch cũ, cạnh vát phẳng trên vành nhựa (và bản cực lớn hơn bên trong bầu LED) luôn là &lt;strong&gt;Cathode (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Một tụ gốm nhỏ màu vàng in mã &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt;&lt;/strong&gt;, còn một tụ khác in &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;224&lt;/code&gt;&lt;/strong&gt;. Giá trị điện dung của chúng là bao nhiêu? &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt; = \(10 \times 10^3\text{ pF} = \mathbf{10\text{ nF} = 0.01\ \mu\text{F}}\); &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;224&lt;/code&gt; = \(22 \times 10^4\text{ pF} = \mathbf{220\text{ nF} = 0.22\ \mu\text{F}}\)!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-cần-phân-biệt-tụ-đệm-với-nguồn-pin-trên-xe-hai-bánh&quot;&gt;Vì sao cần phân biệt tụ đệm với nguồn pin trên xe hai bánh&lt;/h2&gt;

&lt;p&gt;Trên xe đồ án, ESP32 hoặc Pico hoạt động ở mức 3,3 V; mạch lái TB6612 nhận nguồn động cơ ở chân VM (4,5 V đến 10 V) và nguồn logic ở chân VCC (3,3 V). Một tụ hóa 100 µF mắc ngang chân VM đóng vai trò bộ đệm năng lượng cục bộ cho các xung đóng cắt PWM của động cơ TT, nhưng &lt;strong&gt;không phải là nguồn pin&lt;/strong&gt;: lượng năng lượng tích trữ trong tụ 100 µF cạn sạch trong chưa tới một mili giây khi động cơ kẹt trục. Do đó xe vẫn bắt buộc có bộ pin động cơ riêng, cách ly khỏi chân GPIO.&lt;/p&gt;

&lt;p&gt;Ngoài ra, đèn báo trạng thái là LED mắc kèm điện trở, còn diode hồi tiếp (nếu dùng mạch đóng cắt rời) thường là loại 1N4007 với vạch catot hướng về cực dương nguồn động cơ. Việc nhận diện đúng cực tính trước khi lắp mạch giúp tránh tình huống tụ hóa bị cắm ngược cực và xì nổ khi cấp điện.&lt;/p&gt;

&lt;h2 id=&quot;tụ-điện-vạch-cực-mã-số-và-giới-hạn-năng-lượng-của-tụ-100-µf&quot;&gt;Tụ điện: vạch cực, mã số và giới hạn năng lượng của tụ 100 µF&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Tụ hóa nhôm&lt;/strong&gt; là linh kiện phân cực. Dải vạch sáng chạy dọc một bên thân vỏ (thường in kèm dấu trừ) đánh dấu &lt;strong&gt;chân âm&lt;/strong&gt;. Trên đường nguồn một chiều, chân âm nối về mass (GND), chân còn lại nối lên đường nguồn cần lọc. Điện áp định mức in trên vỏ tụ phải lớn hơn điện áp tối đa mà đường nguồn đạt tới: với pin 2S sạc đầy ở 8,4 V, nên chọn tụ định mức 16 V hoặc 25 V, tuyệt đối không dùng loại 6,3 V.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/capacitor_polarity.png&quot; alt=&quot;Cực tính tụ hoá: vạch là chân âm&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Electrolytic_capacitor.jpg&quot; alt=&quot;Tụ hoá nhôm, vạch đánh dấu phía âm&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tụ gốm&lt;/strong&gt; dùng trong khóa học (dạng đĩa không vạch hoặc tụ dán SMD màu nâu không dấu cực) là linh kiện không phân cực, có thể cắm theo chiều nào cũng được. Không nên tự gán cực tính cho tụ gốm chỉ vì tụ hóa bên cạnh có cực. Lưu ý rằng một số tụ tantali có phân cực với quy ước ký hiệu ngược lại; nếu bạn không chủ động mua tụ tantali, đừng mặc định mọi tụ dán hình chữ nhật màu vàng nhạt là tantali.&lt;/p&gt;

&lt;p&gt;Tụ gốm xuyên lỗ thường dùng mã ba chữ số tính theo &lt;strong&gt;picofarad (pF)&lt;/strong&gt;, tương tự quy tắc mã điện trở SMD: hai chữ số đầu là phần có nghĩa, chữ số thứ ba là số mũ của 10. Ví dụ tụ ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;104&lt;/code&gt; có giá trị:&lt;/p&gt;

\[10 \times 10^{4}\ \mathrm{pF} = 100000\ \mathrm{pF} = 100\ \mathrm{nF} = 0.1\ \mu\mathrm{F}\]

&lt;p&gt;Tương tự, mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt; là 10 nF. Đơn vị gốc của mã là pF chứ không phải nF. Riêng tụ hóa có điện dung lớn nên nhà sản xuất luôn in thẳng giá trị lên vỏ (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100 µF 16 V&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Để thấy rõ giới hạn của tụ 100 µF trên đường nguồn động cơ, ta tính năng lượng tích trữ trong tụ theo công thức:&lt;/p&gt;

\[E = \frac{1}{2} C V^2\]

&lt;p&gt;Với $C = 100\ \mu\mathrm{F} = 100 \times 10^{-6}\ \mathrm{F}$ ở điện áp pin danh định 7,4 V:&lt;/p&gt;

\[E = \frac{1}{2} \times 100 \times 10^{-6} \times (7.4)^2 \approx 0.0027\ \mathrm{J}\]

&lt;p&gt;Một động cơ TT kẹt trục rút khoảng 0,8 A ở 7,4 V tiêu thụ công suất xấp xỉ 6 W. Thời gian tụ 100 µF duy trì nổi công suất đó chỉ khoảng $0{,}0027 / 6 \approx 0{,}5\ \mathrm{ms}$. Nửa mili giây năng lượng này rất có ích để bù sườn xung PWM và giảm ảnh hưởng điện cảm của dây pin dài, nhưng hoàn toàn không thể thay thế nguồn pin nuôi bánh xe — đặc biệt khi có hai động cơ chạy song song. Hãy đặt tụ 100 µF ngang chân VM và GND (vạch âm về GND) đi kèm một bộ pin thật cho TB6612, không lấy nguồn từ chân 3V3 của Pico hay ESP32.&lt;/p&gt;

&lt;h2 id=&quot;diode-và-led&quot;&gt;Diode và LED&lt;/h2&gt;

&lt;p&gt;Diode silic cho dòng điện đi qua dễ dàng theo một chiều (phân cực thuận) và chặn dòng ở chiều ngược lại. Vạch sơn quanh một đầu thân của diode 1N4007 đánh dấu &lt;strong&gt;catot&lt;/strong&gt; (đầu dòng điện quy ước đi ra khi dẫn thuận); đầu còn lại là &lt;strong&gt;anot&lt;/strong&gt;. Đèn LED đỏ hoạt động theo cùng nguyên lý, chỉ khác là điện áp rơi thuận vào khoảng 2 V thay vì ~0,7 V như diode silic thường.&lt;/p&gt;

&lt;p&gt;Theo quy ước sản xuất, chân dài của LED thường là anot và cạnh vát phẳng trên vành nhựa thường là catot. Tuy nhiên, từ “thường” không đảm bảo tuyệt đối vì chân linh kiện có thể đã bị cắt ngắn hoặc lệch chuẩn ở các lô hàng giá rẻ. Cách chắc chắn nhất là dùng &lt;strong&gt;thang đo diode&lt;/strong&gt; trên đồng hồ vạn năng khi linh kiện không cấp nguồn: đồng hồ sẽ hiển thị điện áp thuận ở một chiều và báo hở (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt;) ở chiều đảo ngược. Diode silic thường hiển thị khoảng 0,5 V đến 0,7 V; LED đỏ thường hiển thị khoảng 1,6 V đến 2,0 V kèm ánh sáng mờ do dòng thử nhỏ từ đồng hồ. Nếu cả hai chiều đều gần 0 V (chập) hoặc cả hai chiều đều &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; (đứt), linh kiện đã hỏng.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/LEDs.jpg&quot; alt=&quot;LED nhiều màu; độ dài chân chỉ là gợi ý&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/component_lookalikes.png&quot; alt=&quot;Các vỏ trông giống nhau cho đến khi đọc chữ in&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Trên mạch dán, linh kiện SMD màu nâu trơn thường là tụ gốm, còn linh kiện có in mã ba chữ số thường là điện trở — vẫn cần kiểm tra kỹ trước khi kết luận. Với mạch đóng cắt động cơ bằng transistor rời, cần mắc thêm một diode hồi tiếp (như 1N4007) song song động cơ với vạch catot hướng về cực dương nguồn để xả dòng cảm ứng khi khóa ngắt; module TB6612 đã tích hợp sẵn cơ chế bảo vệ này, nhưng bạn vẫn cần nhận diện được 1N4007 khi gặp trong bộ linh kiện.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-kết-quả-chuẩn-ở-thang-đo-diode&quot;&gt;Ví dụ: kết quả chuẩn ở thang đo diode&lt;/h2&gt;

&lt;p&gt;Chuẩn bị một diode 1N4007 và một LED đỏ rời ngoài mạch (không cấp nguồn). Khi chuyển đồng hồ sang thang đo diode (que đen ở COM, que đỏ ở V/Ω), dòng thử quy ước đi ra từ que đỏ.&lt;/p&gt;

&lt;p&gt;Với diode 1N4007, đặt que đỏ vào đầu không vạch (anot) và que đen vào đầu có vạch (catot), màn hình hiển thị khoảng 0,62 V; đảo ngược hai que thì màn hình báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Với LED đỏ, ở chiều thuận màn hình hiển thị khoảng 1,8 V và LED hơi phát sáng; đảo ngược hai que thì báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt;. Chân đang chạm que đỏ tại thời điểm đọc được 1,8 V chính là &lt;strong&gt;anot&lt;/strong&gt;. Nếu kết quả này trái với độ dài chân ban đầu, hãy tin đồng hồ và đánh dấu lại chân anot bằng bút trước khi cắt chân.&lt;/p&gt;

&lt;p&gt;Tóm lại, LED luôn cần điện trở nối tiếp vì điện áp rơi trên nó gần như cố định trong khi dòng tăng rất nhanh; còn tụ điện có điện áp thay đổi theo điện tích nên ta đánh giá bằng công thức năng lượng $E = \tfrac{1}{2}CV^2$.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành&quot;&gt;Thực hành&lt;/h2&gt;

&lt;p&gt;Bài lab này thực hiện hoàn toàn ở trạng thái &lt;strong&gt;không cấp nguồn&lt;/strong&gt; để đảm bảo không cắm ngược tụ hóa lên đường nguồn đang có điện.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Chuẩn bị một tụ hóa nhôm, một tụ gốm ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;104&lt;/code&gt; (nếu có), một diode có vạch (như 1N4007) và một đèn LED. Rút toàn bộ pin và cáp USB khỏi bàn.&lt;/li&gt;
  &lt;li&gt;Quan sát tụ hóa: xác định vạch chân âm và đọc điện áp định mức trên vỏ. Ghi chú rõ nếu định mức dưới 16 V thì không dùng được cho pin 2S (8,4 V).&lt;/li&gt;
  &lt;li&gt;Với tụ gốm ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;104&lt;/code&gt;, tự tính $10 \times 10^4\ \mathrm{pF} = 100\ \mathrm{nF}$ ra giấy trước khi đối chiếu.&lt;/li&gt;
  &lt;li&gt;Chuyển đồng hồ sang thang đo diode (que đen ở COM, que đỏ ở V/Ω). Đo 1N4007 theo cả hai chiều, ghi lại chiều cho giá trị ~0,5–0,7 V và xác nhận que đen lúc đó đang đặt ở đầu có vạch.&lt;/li&gt;
  &lt;li&gt;Đo LED theo cả hai chiều, ghi lại điện áp thuận và xác định chân anot ở que đỏ; so sánh với độ dài chân và mặt vát nhựa rồi ghi chú nếu có điểm không khớp.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả kỳ vọng:&lt;/strong&gt; Mỗi diode và LED chỉ dẫn theo đúng một chiều và báo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; ở chiều ngược lại; LED đỏ có thể sáng mờ ở chiều thuận. Trong bài này, bạn chỉ nhận diện tụ hóa bằng mắt và không nạp điện cho tụ (chỉ đo điện dung nếu đồng hồ có riêng thang đo tụ và linh kiện nằm ngoài mạch).&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng quan sát&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Cách khắc phục&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cả hai chiều đo diode đều hiện &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Đầu que tiếp xúc kém hoặc linh kiện bị đứt&lt;/td&gt;
      &lt;td&gt;Làm sạch chân linh kiện và đo lại; nếu vẫn hở thì loại bỏ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cả hai chiều đo đều gần 0 V&lt;/td&gt;
      &lt;td&gt;Linh kiện bị chập, hoặc đang nhầm thang thông mạch trên dây dẫn&lt;/td&gt;
      &lt;td&gt;Kiểm tra lại vị trí núm vặn; diode tốt không kêu bíp cả hai chiều&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LED sáng ở thang diode nhưng chân dài lại là catot&lt;/td&gt;
      &lt;td&gt;Chân đã bị cắt trước đó hoặc lô sản xuất ngược chuẩn&lt;/td&gt;
      &lt;td&gt;Đánh dấu lại chân anot theo kết quả đo của đồng hồ&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Muốn cấp điện ngược thử xem tụ hóa phản ứng thế nào&lt;/td&gt;
      &lt;td&gt;Gây nguy hiểm cháy nổ và hỏng linh kiện&lt;/td&gt;
      &lt;td&gt;Dừng ngay, không thử nghiệm cấp điện ngược cực cho tụ hóa&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nếu tụ hóa nhôm bị cấp điện ngược cực trên đường nguồn đang hoạt động, lớp màng oxit cách điện sẽ bị đánh thủng, dòng rò lớn làm sôi chất điện phân, tăng áp suất khiến van xả trên đỉnh vỏ tụ xì bung hoặc nổ, và linh kiện hỏng thường chuyển sang trạng thái ngắn mạch làm xả mạnh nguồn pin. Chúng ta tìm hiểu cơ chế này để phòng tránh và nhận biết tụ đã hỏng, tuyệt đối không lắp thử.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý an toàn:&lt;/strong&gt; Chỉ thao tác ở thang đo diode với linh kiện rời, rút toàn bộ USB và không nối nguồn VM. Không xả tụ dung lượng lớn bằng cách chập trực tiếp bằng tay hay đầu que cầm trần; dù tụ 100 µF trong kit khá nhỏ, việc rèn thói quen an toàn rất cần thiết cho các mạch công suất lớn về sau.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=Fwj_d3uO5g8&quot;&gt;Diodes Explained - The basics how diodes work working principle pn junction&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Giải thích nguyên lý hoạt động và cực tính anot/catot của diode cũng như đèn LED.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=X4EUwTwZ110&quot;&gt;Capacitors Explained - The basics how capacitors work working principle&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Minh họa quá trình tích phóng điện của tụ và cách nhận biết cực tính trên tụ hóa.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Quy đổi hai mã tụ gốm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;104&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;223&lt;/code&gt; sang đơn vị nanofarad (nF).&lt;/li&gt;
  &lt;li&gt;Một tụ 100 µF được nạp đến 7,4 V. Hãy tính năng lượng tích trữ $E$ và ước lượng thời gian $E/P$ mà tụ có thể duy trì cho một tải động cơ kẹt công suất 6 W.&lt;/li&gt;
  &lt;li&gt;Ở thang đo diode, đồng hồ hiển thị 0,58 V trên 1N4007 khi que đen đặt ở đầu có vạch và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; khi đảo que. Đầu nào là catot, và que nào đang đặt ở anot lúc đọc được 0,58 V?&lt;/li&gt;
  &lt;li&gt;Giả sử một túi LED bị cắt ngắn hai chân bằng nhau. Hãy mô tả cách tìm chân anot không cần cấp nguồn ngoài, và nêu thao tác cần tránh khi muốn “thử cho sáng rõ hơn”.&lt;/li&gt;
  &lt;li&gt;Một bạn mắc tụ hóa 100 µF đúng cực ngang chân 3V3 và GND của ESP32 rồi đấu hai động cơ TT trực tiếp vào chân 3V3 với suy nghĩ “tụ sẽ gánh giúp dòng kẹt”. Lập luận năng lượng này sai ở đâu, và chân VM của TB6612 phải đấu vào nguồn nào?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. `104` là $10 \times 10^4\ \mathrm{pF} = 100\ \mathrm{nF}$. `223` là $22 \times 10^3\ \mathrm{pF} = 22\ \mathrm{nF}$.
2. $E = \tfrac{1}{2} \times 100 \times 10^{-6} \times 7{,}4^2 \approx 0{,}0027\ \mathrm{J}$. Thời gian duy trì khoảng $0{,}0027 / 6 \approx 0{,}45\ \mathrm{ms}$ — chỉ đủ lọc xung ngắn chứ không thể cấp nguồn chạy liên tục.
3. Đầu có vạch là catot (nơi đặt que đen khi phân cực thuận), còn que đỏ đang đặt ở chân anot.
4. Dùng thang đo diode ở trạng thái không cấp nguồn; chân chạm que đỏ khi đồng hồ hiện điện áp thuận (và LED có thể sáng mờ) chính là anot. Tuyệt đối không chích thẳng nguồn 5 V hay GPIO không có điện trở hạn dòng để làm LED sáng rõ hơn.
5. Tụ 100 µF chỉ dự trữ năng lượng trong chưa đầy 1 ms khi động cơ kẹt, nên mạch ổn áp 3V3 vẫn bị rút dòng cỡ ampe và lập tức sụt áp hoặc hỏng. Chân VM của TB6612 phải nối vào pin động cơ riêng (trong dải 4,5–10 V), nối mass chung và chỉ dùng GPIO cho tín hiệu logic.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;p&gt;Các bài hướng dẫn ngắn gọn của SparkFun về cực tính và mã đọc: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/capacitors&quot;&gt;Capacitors&lt;/a&gt;, &lt;a href=&quot;https://learn.sparkfun.com/tutorials/diodes&quot;&gt;Diodes&lt;/a&gt; và &lt;a href=&quot;https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds&quot;&gt;Light-emitting diodes&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Toàn bộ danh sách vi điều khiển, TB6612 và động cơ TT nằm ở bài &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Nếu chưa có bộ LED, bạn có thể tham khảo bộ LED 3 mm trên Hshop giá 20.000 ₫ (ghi nhận ngày 23 tháng 9 năm 2026): &lt;a href=&quot;https://hshop.vn/bo-5-loai-led-sieu-sang-3mm-thong-dung-5-kind-3mm-transparent-color-led&quot;&gt;bộ 5 loại LED 3 mm&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Với diode 1N4007 và tụ hóa 100 µF (16 V hoặc 25 V), bạn có thể tìm qua trang tìm kiếm và tự kiểm tra điện áp định mức: &lt;a href=&quot;https://shopee.vn/search?keyword=1N4007&quot;&gt;Shopee, tìm 1N4007&lt;/a&gt; và &lt;a href=&quot;https://shopee.vn/search?keyword=t%E1%BB%A5%20100uF&quot;&gt;Shopee, tìm tụ 100uF&lt;/a&gt;. Nếu chỉ cần mua vài linh kiện lẻ thay vì cả bộ, bạn có thể mua trực tiếp tại &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; hoặc &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;. Luôn ghi chú rõ cực tính tụ và vạch diode trước khi lắp mạch.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Nhận điện trở: vạch màu và mã SMD</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_03_resistor_color_smd/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_03_resistor_color_smd</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ đọc thành thạo điện trở 4 vạch theo thứ tự: chữ số thứ nhất, chữ số thứ hai, hệ số nhân và dung sai, đồng thời tính được khoảng giá trị cho phép theo dung sai. Bạn sẽ nhận diện nhanh hai mã thông dụng như nâu-đen-đỏ-vàng (1 kΩ ±5%) và nâu-lam-cam-vàng (16 kΩ ±5%), nhận biết điện trở 5 vạch (ba chữ số có nghĩa cộng hệ số nhân), giải mã điện trở dán SMD ba số như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;472&lt;/code&gt;, và hiểu vai trò dấu thập phân của chữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; trong mã như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R22&lt;/code&gt;. Bạn cũng sẽ biết cách đối chiếu giá trị đọc được với đồng hồ vạn năng ở trạng thái ngắt nguồn và trừ sai số điện trở dây que đo khi đo trị số nhỏ.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-gì-trước&quot;&gt;Cần có gì trước&lt;/h2&gt;

&lt;p&gt;Bạn cần nắm định luật Ohm từ bài 01 (đặc biệt là $I = V/R$ và lý do đèn báo dùng điện trở quanh 180 Ω) cùng quy tắc đo điện trở của bài 02 (linh kiện không cấp nguồn, que đỏ cắm ở lỗ V/Ω). Bài này chưa yêu cầu hàn linh kiện dán (SMD), nhưng kiến thức đọc mã SMD vẫn rất hữu ích vì các linh kiện nhỏ hàn sẵn trên board ESP32 và module TB6612 đều dùng quy chuẩn này.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Lấy nhầm một chiếc điện trở \(220\ \Omega\) thay vì \(22\text{ k}\Omega\) nhìn bằng mắt thường chỉ khác nhau đúng &lt;strong&gt;một vạch màu&lt;/strong&gt; (Đỏ vs. Cam) — nhưng dòng điện qua mạch lại chênh nhau tới &lt;strong&gt;100 lần&lt;/strong&gt;, đủ biến một mạch phân áp êm ái thành một mạch làm cháy chân cảm biến! Thành thạo cách đọc &lt;strong&gt;vạch màu 4/5 vạch&lt;/strong&gt; và &lt;strong&gt;mã số điện trở dán SMD&lt;/strong&gt; (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;472&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4R7&lt;/code&gt;) giúp bạn đọc bo mạch thật nhanh như đọc chữ in.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-quy-chuẩn-mã-hóa-điện-trở-iec-60062&quot;&gt;Kiến thức cần nhớ: Quy chuẩn Mã hóa Điện trở (IEC 60062)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Điện trở Cắm lỗ (4 vạch &amp;amp; 5 vạch màu):&lt;/strong&gt; Mã hóa trị số theo ký hiệu khoa học \((\text{chữ số có nghĩa}) \times 10^{\text{hệ số nhân}} \pm \text{dung sai}\). Loại 4 vạch (thường sai số \(\pm 5\%\), vạch cuối vàng kim) có 2 chữ số có nghĩa; loại 5 vạch (thường \(\pm 1\%\), vạch cuối nâu) có 3 chữ số có nghĩa.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Điện trở Dán (SMD — mã 3 số, 4 số và ký hiệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt;):&lt;/strong&gt; Trên bo mạch ESP32 hay module cảm biến nhỏ xíu không thể vẽ vạch màu, nhà sản xuất in mã số: 2 (hoặc 3) chữ số đầu là giá trị, chữ số cuối là số chữ số &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; thêm vào; chữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; đóng vai trò dấu phẩy thập phân (ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4R7&lt;/code&gt; = \(4.7\ \Omega\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dãy Giá trị Chuẩn (E12 / E24):&lt;/strong&gt; Bạn sẽ không bao giờ mua được điện trở \(213\ \Omega\); linh kiện sản xuất theo các bội số chuẩn (như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10, 12, 15, 18, 22, 27, 33, 47, 68&lt;/code&gt;) để phủ kín mọi khoảng dung sai mà không cần tồn kho vô hạn trị số.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-giải-mã-4-điện-trở-phổ-biến-nhất-trên-xe-robot&quot;&gt;Ví dụ làm từng bước: Giải mã 4 điện trở phổ biến nhất trên xe robot&lt;/h3&gt;

&lt;p&gt;Hãy tự tính tay 4 mã gặp hằng ngày rồi kiểm tra lại bằng công thức:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Loại &amp;amp; Mã đọc được&lt;/th&gt;
      &lt;th&gt;Công thức giải mã&lt;/th&gt;
      &lt;th&gt;Trị số &amp;amp; Khoảng dung sai&lt;/th&gt;
      &lt;th&gt;Ứng dụng quen thuộc&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;4 vạch:&lt;/strong&gt; Vàng – Tím – Đỏ – Vàng kim&lt;/td&gt;
      &lt;td&gt;\(47 \times 10^2\ \Omega \pm 5\%\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(4.7\text{ k}\Omega\)&lt;/strong&gt; (\(4.465\text{–}4.935\text{ k}\Omega\))&lt;/td&gt;
      &lt;td&gt;Điện trở kéo lên (pull-up) bus I2C&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;5 vạch:&lt;/strong&gt; Nâu – Đen – Đen – Đỏ – Nâu&lt;/td&gt;
      &lt;td&gt;\(100 \times 10^2\ \Omega \pm 1\%\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(10\text{ k}\Omega\)&lt;/strong&gt; (\(9.9\text{–}10.1\text{ k}\Omega\))&lt;/td&gt;
      &lt;td&gt;Điện trở kéo xuống (pull-down) nút nhấn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;SMD 3 số:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;221&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(22 \times 10^1\ \Omega\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(220\ \Omega\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Điện trở hạn dòng LED trên bo mạch&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;SMD chữ R:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0R1&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4R7&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(0.1\ \Omega\) / \(4.7\ \Omega\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(0.1\ \Omega\)&lt;/strong&gt; / &lt;strong&gt;\(4.7\ \Omega\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Điện trở shunt đo dòng động cơ&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;giải-thích-4-mẹo-tránh-đọc-ngược-và-nhầm-màu-điện-trở&quot;&gt;Giải thích: 4 mẹo tránh đọc ngược và nhầm màu điện trở&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Tìm vạch dung sai để đặt đúng chiều đọc:&lt;/strong&gt; Vạch vàng kim (\(\pm 5\%\)) hoặc bạc (\(\pm 10\%\)) luôn nằm ở &lt;strong&gt;bên phải&lt;/strong&gt; và thường có khoảng cách rộng hơn một chút so với nhóm vạch trị số.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Cảnh giác với điện trở thân xanh 5 vạch (Nâu vs. Đỏ):&lt;/strong&gt; Dưới đèn bàn ánh sáng vàng, màu Nâu (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;), Đỏ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;) và Cam (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;) trên nền thân xanh rất dễ lẫn — nguyên tắc vàng: &lt;strong&gt;nghi ngờ là đo lại bằng đồng hồ vạn năng!&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Phân biệt mã SMD &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100&lt;/code&gt; (\(10\ \Omega\)) với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;101&lt;/code&gt; (\(100\ \Omega\)):&lt;/strong&gt; Chữ số cuối &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; nghĩa là nhân \(10^0 = 1\) (không thêm số 0 nào!), nên &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100&lt;/code&gt; là \(10\ \Omega\) chứ không phải \(100\ \Omega\)!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Sắp xếp khay linh kiện theo bội số 10:&lt;/strong&gt; Gom riêng hàng trăm ohm (\(220, 330\ \Omega\)), hàng kilo-ohm (\(1, 4.7, 10\text{ k}\Omega\)) và ghi nhãn trị số rõ trên từng ngăn hộp.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Một điện trở dán trên module động cơ in mã &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt;&lt;/strong&gt;, còn một điện trở khác in &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4701&lt;/code&gt;&lt;/strong&gt;. Trị số của chúng lần lượt là bao nhiêu? &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt; = \(10 \times 10^3 = \mathbf{10\text{ k}\Omega}\); &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4701&lt;/code&gt; (mã 4 số) = \(470 \times 10^1 = \mathbf{4.7\text{ k}\Omega}\)!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-xe-hai-bánh-cần-đọc-chính-xác-mã-điện-trở&quot;&gt;Vì sao xe hai bánh cần đọc chính xác mã điện trở&lt;/h2&gt;

&lt;p&gt;Đèn báo 3,3 V trên ESP32 hay Pico chỉ sáng đúng thiết kế nếu điện trở bạn cắm thực sự gần mức 180 Ω. Nếu nhầm sang nâu-đen-cam (10 kΩ thay vì 1 kΩ), dòng qua LED chỉ còn:&lt;/p&gt;

\[I = \frac{1.3}{10000} = 0.13\ \mathrm{mA}\]

&lt;p&gt;khiến đèn tối đến mức dễ nhầm là chân GPIO bị hỏng. Ngược lại, cắm nhầm điện trở vài ôm vào vị trí cần vài trăm ôm sẽ gây quá dòng trực tiếp cho chân vi điều khiển.&lt;/p&gt;

&lt;p&gt;Kỹ năng này còn áp dụng rộng rãi hơn ngoài breadboard: từ điện trở kéo lên cho nút nhấn, cầu chia áp cho chân echo của cảm biến HC-SR04 cho tới các điện trở dán trên module mạch lái động cơ. Việc đọc chính xác và đo kiểm linh kiện trước khi lắp mạch giúp loại trừ hoàn toàn các lỗi chủ quan do nhầm trị số.&lt;/p&gt;

&lt;h2 id=&quot;điện-trở-4-vạch-và-5-vạch-màu&quot;&gt;Điện trở 4 vạch và 5 vạch màu&lt;/h2&gt;

&lt;p&gt;Với điện trở 4 vạch tiêu chuẩn, bạn đặt đầu có các vạch nằm sát nhau về bên trái, còn vạch dung sai (thường là màu vàng nhũ hoặc bạc ở linh kiện phổ thông) về bên phải. Hai vạch đầu biểu thị hai chữ số có nghĩa, vạch thứ ba là hệ số nhân (lũy thừa của 10), và vạch thứ tư là dung sai — cho biết trị số thực tế được phép lệch bao nhiêu phần trăm so với trị số danh định.&lt;/p&gt;

&lt;p&gt;Bảng quy ước màu chữ số: đen 0, nâu 1, đỏ 2, cam 3, vàng 4, lục 5, lam 6, tím 7, xám 8, trắng 9. Hệ số nhân dùng cùng bảng số mũ của 10: đen ×1, nâu ×10, đỏ ×100, cam ×1.000, vàng ×10.000. Riêng màu vàng kim (nhũ vàng) ở vị trí hệ số nhân nghĩa là ×0,1, còn ở vị trí dung sai là ±5%; màu bạc ở vị trí dung sai là ±10%.&lt;/p&gt;

&lt;p&gt;Xét ví dụ đầu tiên: &lt;strong&gt;nâu – đen – đỏ – vàng (kim)&lt;/strong&gt;. Nâu = 1, đen = 0, đỏ = ×100:&lt;/p&gt;

\[10 \times 100 = 1000\ \Omega = 1\ \mathrm{k}\Omega\]

&lt;p&gt;Với dung sai ±5%, khoảng giá trị hợp lệ là từ 950 Ω đến 1.050 Ω. Do đó nếu đồng hồ đo được 980 Ω, linh kiện vẫn hoàn toàn đạt chuẩn.&lt;/p&gt;

&lt;p&gt;Xét ví dụ thứ hai: &lt;strong&gt;nâu – lam – cam – vàng (kim)&lt;/strong&gt;. Nâu = 1, lam = 6, cam = ×1.000:&lt;/p&gt;

\[16 \times 1000 = 16000\ \Omega = 16\ \mathrm{k}\Omega\]

&lt;p&gt;Khoảng dung sai ±5% tương ứng từ 15,2 kΩ đến 16,8 kΩ. Nếu bạn xoay ngược điện trở và bắt đầu đọc từ vạch vàng kim thì sẽ ra kết quả vô nghĩa vì vàng kim không đại diện cho chữ số có nghĩa.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/resistor_color_code.png&quot; alt=&quot;Mã bốn vạch, vạch dung sai nằm bên phải&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Điện trở &lt;strong&gt;5 vạch&lt;/strong&gt; bổ sung thêm một chữ số có nghĩa thứ ba trước hệ số nhân, còn vạch cuối cùng vẫn là dung sai. Ví dụ &lt;strong&gt;nâu – đen – đen – đỏ – nâu&lt;/strong&gt; tương ứng 1, 0, 0, ×100, ±1%:&lt;/p&gt;

\[100 \times 100 = 10\ \mathrm{k}\Omega\]

&lt;p&gt;với sai số ±1%. Bạn không bắt buộc phải mua bộ điện trở 5 vạch để làm robot, nhưng cần tạo thói quen đếm tổng số vạch trước khi đọc, bởi nếu đọc nhầm một con 5 vạch theo quy tắc 4 vạch thì hệ số nhân sẽ bị lệch hẳn một bậc mười.&lt;/p&gt;

&lt;h2 id=&quot;mã-điện-trở-dán-smd&quot;&gt;Mã điện trở dán SMD&lt;/h2&gt;

&lt;p&gt;Mã SMD 3 chữ số hoạt động theo cùng nguyên lý nhưng được in trực tiếp bằng số: hai chữ số đầu là phần có nghĩa, chữ số thứ ba là số mũ của 10. Ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt; là $10 \times 10^3 = 10\ \mathrm{k}\Omega$, còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;472&lt;/code&gt; là $47 \times 10^2 = 4700\ \Omega = 4{,}7\ \mathrm{k}\Omega$. Đặc biệt lưu ý mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100&lt;/code&gt; là $10 \times 10^0 = 10\ \Omega$, không phải 100 Ω — chữ số 0 cuối cùng là số mũ (nghĩa là thêm không chữ số 0).&lt;/p&gt;

&lt;p&gt;Đối với trị số nhỏ hơn 10 Ω, chữ cái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; đóng vai trò dấu phẩy thập phân: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R22&lt;/code&gt; nghĩa là 0,22 Ω và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4R7&lt;/code&gt; nghĩa là 4,7 Ω. Chữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; rất nhỏ trên thân linh kiện tối màu nhưng mang ý nghĩa quan trọng: dùng nhầm một điện trở 0,22 Ω làm điện trở hạn dòng LED trên 3,3 V gần như tương đương ngắn mạch.&lt;/p&gt;

&lt;p&gt;Mã SMD 4 chữ số mở rộng lên ba chữ số có nghĩa cộng một chữ số mũ: ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4701&lt;/code&gt; là $470 \times 10^1 = 4{,}7\ \mathrm{k}\Omega$. Riêng chuẩn &lt;strong&gt;EIA-96&lt;/strong&gt; (thường gặp trên điện trở dán sai số 1%) gồm hai chữ số và một chữ cái, trong đó hai chữ số là mã tra bảng chứ không phải trị số trực tiếp. Bạn không cần học thuộc bảng EIA-96; khi thấy ký hiệu gồm hai số kèm một chữ cái, hãy tra bảng hoặc đo bằng đồng hồ thay vì đoán theo kiểu “01 là 1 Ω”.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/resistors_assortment.jpg&quot; alt=&quot;Một hộp hỗn hợp: vạch, thân, và công suất không phải cùng một thông tin&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Kích thước thân điện trở phản ánh khả năng tản công suất chứ không cho biết trị số điện trở. Điện trở xuyên lỗ 1/4 W là cỡ mặc định trong chương này; các thân sứ lớn hơn thường chịu 1 W trở lên. Như đã tính ở bài 01, mạch LED chỉ tỏa khoảng 0,012 W nên cỡ 1/4 W rất thoải mái — nhưng điều đó không có nghĩa bạn có thể dùng một điện trở 10 Ω công suất 1/4 W làm tải giả cho động cơ đang kẹt.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-giải-mã-năm-trị-số-và-đối-chiếu-đồng-hồ&quot;&gt;Ví dụ: giải mã năm trị số và đối chiếu đồng hồ&lt;/h2&gt;

&lt;p&gt;Hãy tự giải năm mã sau trước khi xoay núm đồng hồ:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Nâu – đen – đỏ – vàng (kim) → $10 \times 100 = 1\ \mathrm{k}\Omega \pm 5\%$, khoảng hợp lệ 950 Ω đến 1.050 Ω.&lt;/li&gt;
  &lt;li&gt;Nâu – lam – cam – vàng (kim) → $16 \times 1000 = 16\ \mathrm{k}\Omega \pm 5\%$, khoảng hợp lệ 15,2 kΩ đến 16,8 kΩ.&lt;/li&gt;
  &lt;li&gt;Đỏ – đỏ – nâu – vàng (kim) → $22 \times 10 = 220\ \Omega \pm 5\%$, khoảng hợp lệ 209 Ω đến 231 Ω (phương án thay thế cho 180 Ω ở mạch LED).&lt;/li&gt;
  &lt;li&gt;SMD &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt; → $10 \times 10^3 = 10\ \mathrm{k}\Omega$.&lt;/li&gt;
  &lt;li&gt;SMD &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R22&lt;/code&gt; → 0,22 Ω (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; là dấu thập phân).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thay trị số mục 3 (220 Ω) vào công thức đèn báo bài 01 ($V_f = 2{,}0\ \mathrm{V}$ trên nguồn 3,3 V):&lt;/p&gt;

\[I = \frac{3.3 - 2.0}{220} = \frac{1.3}{220} \approx 5.9\ \mathrm{mA}\]

&lt;p&gt;Công suất trên điện trở lúc này là $P = I^2 R \approx (0{,}0059)^2 \times 220 \approx 0{,}008\ \mathrm{W}$, rất nhỏ so với mức 0,25 W. Nếu đọc nhầm sang nâu-đen-cam (10 kΩ), dòng tụt xuống 0,13 mA khiến đèn gần như không sáng; còn nếu nhầm sang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R22&lt;/code&gt; (0,22 Ω), dòng lý thuyết lên tới $I = 1{,}3 / 0{,}22 \approx 6\ \mathrm{A}$, vượt xa khả năng chịu đựng của cả LED lẫn chân GPIO.&lt;/p&gt;

&lt;p&gt;Khi đo kiểm trên đồng hồ, điện trở dung sai 5% chỉ cần rơi trong khoảng hợp lệ là đạt (ví dụ lệch thấp 2% vẫn là linh kiện tốt). Ở dải đo nhỏ, cần chú ý hai yếu tố gây sai số: thứ nhất, nhiệt độ tay người khi kẹp trực tiếp lên thân điện trở màng có thể làm số đo trôi nhẹ, nên chỉ giữ một chân hoặc đặt linh kiện trên mặt bàn; thứ hai, với điện trở dưới vài ôm, bản thân dây và que đo đồng hồ đã có điện trở vài phần mười ôm — hãy chập hai que đo để ghi nhận giá trị nền rồi trừ ra trước khi kết luận về một điện trở như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R22&lt;/code&gt;. Tuyệt đối không hiệu chuẩn đồng hồ bằng cách đo linh kiện trên mạch đang có điện.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành&quot;&gt;Thực hành&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chọn năm điện trở bất kỳ trong bộ kit (hoặc dùng năm ví dụ ở trên nếu kit chưa giao tới). Ghi rõ dãy vạch màu hoặc mã SMD, trị số danh định và khoảng dung sai cho phép ra giấy.&lt;/li&gt;
  &lt;li&gt;Đảm bảo mạch hoàn toàn mất nguồn và điện trở nằm rời ngoài mạch. Kiểm tra que đen ở COM, que đỏ ở lỗ V/Ω, vặn núm sang thang đo Ω (nếu đồng hồ chỉnh thang thủ công, bắt đầu từ thang lớn hơn trị số dự kiến).&lt;/li&gt;
  &lt;li&gt;Đo lần lượt từng điện trở và ghi kết quả bên cạnh giá trị đã giải mã. Nếu điện trở 5% rơi ngoài khoảng dung sai, kiểm tra lại xem bạn có đọc ngược từ vạch vàng kim hoặc đếm nhầm 5 vạch thành 4 vạch không.&lt;/li&gt;
  &lt;li&gt;Với bất kỳ điện trở nào dưới khoảng 10 Ω, chập hai que đo trước để ghi điện trở dây đo rồi trừ phần chênh lệch đó khỏi kết quả.&lt;/li&gt;
  &lt;li&gt;(Tùy chọn) Lắp lại điện trở 180 Ω hoặc 220 Ω vào mạch LED của bài 01 và đo trong mạch khi đã ngắt nguồn; nhận xét xem có nhánh linh kiện nào mắc song song ảnh hưởng đến số đo không (nếu không chắc chắn, hãy nhấc một chân điện trở lên khỏi breadboard để đo độc lập).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả kỳ vọng:&lt;/strong&gt; Các điện trở tốt và đọc đúng mã sẽ cho số đo nằm gọn trong khoảng dung sai (ví dụ 1 kΩ ±5% đo được 1,02 kΩ là đạt). Nếu bạn thử ở thang thông mạch, đồng hồ sẽ không kêu với cả năm con — điều đó là hoàn toàn bình thường.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng quan sát&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Cách khắc phục&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Kết quả đo gấp ~10 lần hoặc bằng ~0,1 lần giá trị đọc&lt;/td&gt;
      &lt;td&gt;Đọc ngược chiều từ vạch dung sai, hoặc đọc nhầm 5 vạch thành 4 vạch&lt;/td&gt;
      &lt;td&gt;Xoay vạch vàng kim/bạc về bên phải và đếm lại tổng số vạch&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điện trở tưởng là 1 kΩ nhưng đo gần 10 kΩ&lt;/td&gt;
      &lt;td&gt;Nhầm màu vạch hệ số nhân giữa đỏ và cam&lt;/td&gt;
      &lt;td&gt;Soi kỹ vạch thứ ba dưới ánh sáng trắng tốt (đỏ và cam rất dễ lẫn)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Điện trở trị số nhỏ luôn đo dư thêm ~0,4 Ω&lt;/td&gt;
      &lt;td&gt;Cộng thêm điện trở của chính cặp dây que đo&lt;/td&gt;
      &lt;td&gt;Chập hai que đo để ghi điện trở dây rồi trừ đi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Số đo điện trở nhảy loạn khi đặt trên board&lt;/td&gt;
      &lt;td&gt;Mạch vẫn đang được cấp nguồn&lt;/td&gt;
      &lt;td&gt;Ngắt hoàn toàn nguồn điện trước khi đo thang Ω&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thang thông mạch im lặng trên điện trở 220 Ω&lt;/td&gt;
      &lt;td&gt;Nhầm thang thông mạch với thang đo Ω&lt;/td&gt;
      &lt;td&gt;Chuyển núm xoay sang thang Ω; việc không kêu ở đây là đúng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý an toàn:&lt;/strong&gt; Không đo thang Ω trên đường nguồn đang có điện, trên pin hay chân VM của mạch lái; luôn giữ que đỏ ở lỗ V/Ω để tránh ngắn mạch khi chuyển sang đo áp trên chân 3,3 V sau đó.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=G1mZutobxDU&quot;&gt;Resistor Color Code Chart Tutorial Review - Physics&lt;/a&gt; — The Organic Chemistry Tutor, tiếng Anh. Hướng dẫn cách đọc điện trở 4 vạch màu và tính dung sai; hãy luôn đối chiếu lại bằng đồng hồ vạn năng.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[How to Read Resistor Color Codes&lt;/td&gt;
          &lt;td&gt;4-Band Resistor Values and Tolerance](https://www.youtube.com/watch?v=GLD7AgAYqwA) — Engineering Technology Simulation Learning Videos, tiếng Anh. Giải mã chậm từng vạch màu trên điện trở 4 vạch, phù hợp làm đối chứng cho bài thực hành.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Giải mã các điện trở 4 vạch sau (nêu trị số danh định và khoảng dung sai ±5%): vàng-tím-nâu-vàng(kim), cam-cam-đỏ-vàng(kim), và lam-xám-đen-vàng(kim).&lt;/li&gt;
  &lt;li&gt;Một người đọc một điện trở thành vàng(kim)-cam-tím-đỏ. Họ đã mắc lỗi gì, và trị số đúng là bao nhiêu khi đọc theo thứ tự đỏ-tím-cam-vàng(kim)?&lt;/li&gt;
  &lt;li&gt;Giải mã các điện trở SMD sau: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;222&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;510&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4R7&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1003&lt;/code&gt;. Mã nào trong số này (nếu có) có giá trị 10 kΩ?&lt;/li&gt;
  &lt;li&gt;Một điện trở 1 kΩ ±5% đo ngoài mạch được 980 Ω. Linh kiện này đạt hay loại, và độ lệch so với 1.000 Ω là bao nhiêu phần trăm?&lt;/li&gt;
  &lt;li&gt;Điện trở kéo lên (pull-up) cho nút nhấn trên chân GPIO 3,3 V theo thiết kế là 10 kΩ, nhưng thực tế bị cắm nhầm điện trở nâu-đen-đỏ-vàng(kim). Hãy tính dòng điện khi nhấn nút nối chân xuống mass và so sánh với khi dùng đúng 10 kΩ.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Vàng-tím-nâu-vàng(kim): $47 \times 10 = 470\ \Omega$, khoảng 446,5 Ω đến 493,5 Ω (làm tròn 447 Ω đến 494 Ω). Cam-cam-đỏ-vàng(kim): $33 \times 100 = 3{,}3\ \mathrm{k}\Omega$, khoảng 3,14 kΩ đến 3,47 kΩ. Lam-xám-đen-vàng(kim): $68 \times 1 = 68\ \Omega$, khoảng 65 Ω đến 71 Ω.
2. Họ đã đặt vạch dung sai (vàng kim) ở bên trái thay vì bên phải. Đọc đúng chiều đỏ-tím-cam-vàng(kim) cho $27 \times 1000 = 27\ \mathrm{k}\Omega \pm 5\%$.
3. `222` là $22 \times 10^2 = 2{,}2\ \mathrm{k}\Omega$; `510` là $51 \times 10^0 = 51\ \Omega$; `4R7` là 4,7 Ω; `1003` là $100 \times 10^3 = 100\ \mathrm{k}\Omega$. Không mã nào là 10 kΩ (mã 3 chữ số của 10 kΩ là `103`).
4. 980 Ω nằm gọn trong khoảng 950 Ω đến 1.050 Ω, với sai số $20/1000 = 2\% &amp;lt; 5\%$, nên giữ lại dùng bình thường.
5. Nâu-đen-đỏ-vàng(kim) là 1 kΩ, nên khi nhấn giữ nút, dòng qua điện trở là $I = 3{,}3 / 1000 = 3{,}3\ \mathrm{mA}$. Với điện trở kéo lên 10 kΩ, dòng chỉ là $3{,}3 / 10000 = 0{,}33\ \mathrm{mA}$. Dù cả hai đều nằm trong giới hạn an toàn, mức 10 kΩ tiêu thụ ít dòng hơn gấp 10 lần và phù hợp hơn cho mạch kéo lên nút nhấn.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;p&gt;Bài hướng dẫn của SparkFun tổng hợp đầy đủ mã màu, công suất và cách mắc điện trở: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/resistors&quot;&gt;Resistors&lt;/a&gt;. Khi màu vạch khó phân biệt dưới ánh sáng yếu, bạn có thể tra nhanh bằng &lt;a href=&quot;https://www.digikey.com/en/resources/conversion-calculators/conversion-calculator-resistor-color-code&quot;&gt;máy tính mã màu điện trở của Digi-Key&lt;/a&gt; rồi kiểm chứng lại trên đồng hồ vạn năng.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Toàn bộ bộ kit robot nằm ở bài &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;. Bài này chỉ cần thêm một bộ điện trở xuyên lỗ.&lt;/p&gt;

&lt;p&gt;Do thời điểm khảo sát Hshop không hiển thị bộ điện trở 1/4 W phổ thông, bạn có thể tìm mua qua trang tìm kiếm: &lt;a href=&quot;https://shopee.vn/search?keyword=b%E1%BB%99%20%C4%91i%E1%BB%87n%20tr%E1%BB%9F%201%2F4W&quot;&gt;Shopee, tìm bộ điện trở 1/4W&lt;/a&gt;. Hãy đọc kỹ mô tả để chọn loại 1/4 W, sai số 5% hoặc 1%, ưu tiên bộ có sẵn các trị số 180 Ω hoặc 220 Ω cùng 1 kΩ và 10 kΩ. Ngoài ra, bạn có thể mua điện trở lẻ có dán nhãn trị số tại các cửa hàng linh kiện như &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; hoặc &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;, và vẫn đo kiểm tra lại bằng đồng hồ trong bài thực hành.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Breadboard, đồng hồ vạn năng và cách đo an toàn</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_02_breadboard_meter/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_02_breadboard_meter</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ nhận diện chính xác cấu trúc dải năm lỗ, rãnh giữa và thanh nguồn trên breadboard để biết những lỗ nào thực sự nối chung với nhau. Bạn sẽ lường trước việc nhiều breadboard cỡ lớn bị ngắt đôi ở giữa thanh nguồn và kiểm chứng chỗ ngắt đó bằng thang thông mạch thay vì dựa vào trí nhớ. Bạn sẽ nắm vững quy tắc đo điện áp một chiều mắc song song với linh kiện, không đo dòng trực tiếp trên củ sạc USB trong bài này, và chỉ đo điện trở khi đã ngắt nguồn. Trước mỗi lần đo áp, bạn luôn chọn thang đo cao hơn mức điện áp dự kiến và không để ngón tay chạm vào phần kim loại đầu que đo.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-gì-trước&quot;&gt;Cần có gì trước&lt;/h2&gt;

&lt;p&gt;Bạn đã nắm được khái niệm vôn, ampe và lý do đèn LED báo trạng thái phải có điện trở nối tiếp. Bài này không lặp lại định luật Ohm mà tập trung vào kỹ năng thao tác trên bàn lab: hiểu rõ sơ đồ nối lỗ của breadboard và chọn đúng thang đo trên đồng hồ để không vô tình gây ngắn mạch. Bạn chỉ cần một đồng hồ vạn năng số và một breadboard không hàn.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Hàng trăm sinh viên điện tử đã từng dành trọn một buổi chiều kiểm tra lại từng dòng code chỉ vì một lỗi cực kỳ ngớ ngẩn trên &lt;strong&gt;breadboard&lt;/strong&gt;: đường thanh nguồn dài bên mép breadboard bị ngắt đôi ở chính giữa (chỗ vạch đỏ/xanh bị đứt quãng) mà mắt thường không để ý! Biết cách hình dung mạng lưới lá đồng ẩn dưới lớp nhựa breadboard và sử dụng &lt;strong&gt;đồng hồ vạn năng&lt;/strong&gt; đúng thang đo là kỹ năng cứu bạn khỏi 90% nỗi đau gỡ lỗi phần cứng.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kết-nối-breadboard-và-nguyên-lý-đồng-hồ-vạn-năng&quot;&gt;Kiến thức cần nhớ: Kết nối Breadboard và Nguyên lý Đồng hồ Vạn năng&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bản đồ Lá đồng Ẩn của Breadboard:&lt;/strong&gt; Mỗi hàng 5 lỗ ở khu vực giữa nối thông ngang với nhau (bị tách đôi bởi rãnh giữa dành cho IC); hai dải nguồn dọc hai bên nối thông theo chiều dọc (nhưng nhiều breadboard cỡ lớn tách làm hai nửa trên–dưới!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đo Song song (Vôn kế — Trở kháng cực lớn) vs. Đo Nối tiếp (Ampe kế — Trở kháng cực nhỏ):&lt;/strong&gt; Vôn kế có nội trở ~\(10\text{ M}\Omega\) nên kẹp &lt;em&gt;song song&lt;/em&gt; vào mạch mà không làm nhiễu dòng; ngược lại Ampe kế có điện trở shunt chỉ ~\(0.1\ \Omega\) nên &lt;strong&gt;bắt buộc phải cắt mạch mắc nối tiếp&lt;/strong&gt;!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đo Thông mạch &amp;amp; Điện trở (Chỉ đo khi TẮT nguồn):&lt;/strong&gt; Thang thông mạch phát ra một dòng thử cực nhỏ từ pin bên trong đồng hồ để kiểm tra xem hai điểm có thông nhau không; đo khi mạch đang có điện sẽ cho kết quả sai hoặc hỏng đồng hồ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-vì-sao-đo-nhầm-thang-ampe-song-song-nguồn-lại-làm-nổ-cầu-chì-đồng-hồ&quot;&gt;Ví dụ làm từng bước: Vì sao đo nhầm thang Ampe song song nguồn lại làm nổ cầu chì đồng hồ?&lt;/h3&gt;

&lt;p&gt;Giả sử bạn có một mạch đơn giản gồm nguồn &lt;strong&gt;\(V = 5.0\text{ V}\)&lt;/strong&gt; nối với điện trở tải &lt;strong&gt;\(R_{\text{tải}} = 500\ \Omega\)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Đo dòng ĐÚNG (cắt dây, mắc Ampe kế nối tiếp tải, nội trở shunt \(R_s = 0.1\ \Omega\)):&lt;/strong&gt;
\(I_{\text{đúng}} = \frac{5.0}{500 + 0.1} \approx \mathbf{10.0\text{ mA}} \quad (\text{an toàn tuyệt đối!}).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đo dòng SAI (để que ở lỗ Ampe rồi chạm song song trực tiếp hai cực nguồn 5 V):&lt;/strong&gt; dòng điện đi tắt hoàn toàn qua điện trở shunt \(0.1\ \Omega\):
\(I_{\text{sai}} = \frac{5.0\text{ V}}{0.1\ \Omega} = \mathbf{50\text{ A}}!\)
Dòng vọt lên &lt;strong&gt;50 A&lt;/strong&gt; làm &lt;strong&gt;đứt ngay cầu chì bảo vệ 10 A&lt;/strong&gt; (hoặc cháy mạch nếu đo trên cổng mA 200 mA)!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-bước-kiểm-tra-mạch-breadboard-bằng-đồng-hồ-vạn-năng&quot;&gt;Giải thích: 4 bước kiểm tra mạch breadboard bằng đồng hồ vạn năng&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Nối cầu thanh nguồn nếu breadboard bị tách giữa:&lt;/strong&gt; Dùng hai sợi dây ngắn nối liền nửa trên và nửa dưới của thanh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;, rồi kiểm tra bằng thang thông mạch.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Đo thông mạch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC–GND&lt;/code&gt; khi chưa cấp điện:&lt;/strong&gt; Đảm bảo không có chân linh kiện nào chạm chéo làm ngắn mạch.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Cấp nguồn và đo điện áp tại &lt;em&gt;chân linh kiện&lt;/em&gt; (không chỉ tại đầu nguồn):&lt;/strong&gt; Đặt que đen cố định vào một điểm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; chung, chấm que đỏ lần lượt tới chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; của từng IC để phát hiện dây cắm lỏng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Chỉ chuyển sang thang Ampe khi thực sự cần đo dòng:&lt;/strong&gt; Và ngay sau khi đọc xong, rút que đỏ trả về lỗ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VΩmA&lt;/code&gt; lập tức!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Bạn dùng đồng hồ thang DC V đo trực tiếp tại cực pin được \(5.00\text{ V}\), nhưng đo tại chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt; của cảm biến cắm cuối thanh nguồn breadboard lại chỉ được \(0.00\text{ V}\). Nguyên nhân khả dĩ nhất là gì? Thanh nguồn breadboard bị ngắt đôi ở giữa (hoặc sợi dây jumper nối thanh nguồn bị đứt lõi bên trong)!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-xe-hai-bánh-hay-gặp-lỗi-ở-bước-này&quot;&gt;Vì sao xe hai bánh hay gặp lỗi ở bước này&lt;/h2&gt;

&lt;p&gt;Trên xe đồ án, mạch ESP32 hoặc Pico, mạch lái TB6612 và hệ thống dây tín hiệu gặp nhau trên breadboard hoặc đầu cắm Dupont theo đúng quy tắc hình học: năm lỗ trong cùng một cột là một nút chung, còn rãnh giữa là rào cách điện. Một thanh nguồn bị ngắt ở giữa, một đầu dây cắm trượt khỏi kẹp lò xo, hay que đo đỏ còn quên ở lỗ đo dòng đều tạo ra triệu chứng giống như mạch bị chết trong khi chip thậm chí chưa được cấp điện.&lt;/p&gt;

&lt;p&gt;Mạch TB6612 nhận nguồn logic 3,3 V từ vi điều khiển và nguồn động cơ VM từ pin riêng. Nếu bạn cố xử lý tình trạng cầu H không chạy bằng cách cấp 5 V vào GPIO, một lỗi đo đạc đơn giản sẽ biến thành hỏng chân phần cứng. Chân echo của cảm biến HC-SR04 cũng xuất mức 5 V nên cần đo kiểm cẩn thận. Bài này rèn thói quen đo thông mạch và đo điện áp an toàn ngay khi các linh kiện còn nằm rời trên bàn.&lt;/p&gt;

&lt;h2 id=&quot;cấu-trúc-nối-dây-bên-trong-breadboard&quot;&gt;Cấu trúc nối dây bên trong breadboard&lt;/h2&gt;

&lt;p&gt;Bên dưới lớp nhựa đục lỗ của breadboard không hàn là các thanh kẹp lò xo bằng kim loại. Khi bạn cắm chân linh kiện vào một lỗ, nó được nối điện với tất cả các lỗ khác nằm trên cùng thanh kẹp đó. Do tiếp xúc cơ khí không kín khít hoàn toàn, chân linh kiện quá ngắn hoặc bị ôxy hóa có thể trông như đã cắm chặt nhưng thực tế vẫn hở mạch — vì vậy đồng hồ vạn năng mới là căn cứ cuối cùng xác nhận hai điểm có nối nhau hay không.&lt;/p&gt;

&lt;p&gt;Hai hàng dài chạy dọc hai mép là &lt;strong&gt;thanh nguồn&lt;/strong&gt;, thường in vạch màu đỏ và xanh (hoặc đen). Các lỗ nằm dọc theo một vạch màu liên tục là cùng một đường dẫn; thanh đỏ phía trên độc lập với thanh đỏ phía dưới. Đặc biệt, trên nhiều loại breadboard cỡ lớn (kể cả loại 830 lỗ thông dụng), thanh kẹp kim loại bị ngắt đôi tại chính giữa, tương ứng với khoảng hở trên vạch màu in. Nếu bạn cấp 5 V vào nửa bên trái thì nửa bên phải vẫn không có điện cho đến khi bạn cắm dây nối cầu qua khe giữa. Một số breadboard cỡ nhỏ không có khe ngắt này, nên đừng đoán theo thói quen mà hãy đo thông mạch trực tiếp chiếc bạn đang dùng.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/breadboard_anatomy.png&quot; alt=&quot;Cấu tạo breadboard: thanh nguồn, dải năm lỗ, rãnh giữa&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Khu vực trung tâm gồm các &lt;strong&gt;dải đầu cuối&lt;/strong&gt;, trong đó mỗi cột có năm lỗ phía trên rãnh giữa và năm lỗ phía dưới rãnh giữa. Năm lỗ trong cùng một nửa cột nối thông với nhau, nhưng hoàn toàn tách biệt với cột bên cạnh. &lt;strong&gt;Rãnh giữa&lt;/strong&gt; là đường cách điện giúp các IC vỏ DIP đặt bắc ngang qua mà hai hàng chân đối diện không chập vào nhau.&lt;/p&gt;

&lt;p&gt;Hai lỗi cắm mạch kinh điển trên breadboard là cắm cả hai chân LED vào cùng một nhóm năm lỗ (làm nối tắt LED) hoặc cắm hai linh kiện ở hai bên rãnh giữa mà quên dây nối cầu (làm hở mạch). Cả hai lỗi này nhìn bên ngoài đều rất gọn gàng nhưng mạch sẽ không hoạt động.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/breadboard.jpg&quot; alt=&quot;Breadboard không hàn, thanh nguồn chạy dọc mép&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;ý-nghĩa-của-từng-thang-đo-trên-đồng-hồ-vạn-năng&quot;&gt;Ý nghĩa của từng thang đo trên đồng hồ vạn năng&lt;/h2&gt;

&lt;p&gt;Ba thói quen sau giúp tránh hầu hết các sự cố cháy cầu chì đồng hồ trong phòng lab:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Que đen luôn cắm cố định ở lỗ &lt;strong&gt;COM&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Que đỏ cắm ở lỗ ghi &lt;strong&gt;V, Ω&lt;/strong&gt; (hoặc ký hiệu tương đương) cho mọi phép đo điện áp, điện trở và thông mạch.&lt;/li&gt;
  &lt;li&gt;Chỉ chuyển que đỏ sang lỗ đo dòng (&lt;strong&gt;A&lt;/strong&gt; hoặc &lt;strong&gt;mA&lt;/strong&gt;) khi thực sự cần đo dòng, và &lt;strong&gt;rút trả lại lỗ V, Ω ngay sau khi đo xong&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nếu quên que đỏ ở lỗ đo dòng rồi đem đo điện áp, điện trở shunt rất nhỏ bên trong đồng hồ sẽ nối tắt trực tiếp nguồn điện, làm đứt ngay cầu chì mA. Điều nguy hiểm là thang đo điện áp sau đó vẫn hoạt động bình thường nên bạn chỉ phát hiện hỏng cầu chì vào lần đo dòng kế tiếp.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Digital_Multimeter_Aka.jpg&quot; alt=&quot;Đồng hồ vạn năng số: núm, màn hình, ổ que đo&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Đo điện áp một chiều (DC V)&lt;/strong&gt; thực hiện bằng cách &lt;strong&gt;mắc song song&lt;/strong&gt;: giữ nguyên mạch đang hoạt động và đặt hai đầu que đo vào hai điểm cần xác định hiệu điện thế. Nếu đặt ngược que trên đồng hồ số, màn hình chỉ hiện dấu trừ mà không làm hỏng linh kiện. Hãy luôn chọn thang DC cao hơn điện áp dự kiến (ví dụ thang 20 V bao phủ tốt cả nguồn USB 5 V lẫn pin 2S đầy 8,4 V). Ngay cả với đồng hồ tự chọn thang (auto-ranging), bạn vẫn cần xoay đúng ký hiệu điện áp một chiều chứ không phải thang ôm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Đo dòng điện&lt;/strong&gt; thực hiện bằng cách &lt;strong&gt;mắc nối tiếp&lt;/strong&gt;: phải ngắt hở một nhánh dây để dòng điện đi xuyên qua đồng hồ, và chỉ thực hiện sau khi đã chuyển que đỏ sang lỗ đo dòng phù hợp. Trong bài này, tuyệt đối không đo dòng trực tiếp trên củ sạc USB hay chân GPIO, bởi củ sạc có thể đẩy dòng 1 A trở lên làm đứt ngay cầu chì mA nếu bạn kẹp ngang ngõ ra.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Đo điện trở (Ω) và thông mạch&lt;/strong&gt; chỉ được phép làm khi &lt;strong&gt;mạch đã ngắt hoàn toàn nguồn điện&lt;/strong&gt;, vì đồng hồ tự phát một điện áp thử nhỏ. Thang thông mạch phát tiếng bíp khi gặp dây dẫn hoặc tiếp điểm khép kín có điện trở rất nhỏ; một điện trở 180 Ω thường sẽ không kêu bíp ở thang này vì thông mạch chỉ báo hiệu “gần như ngắn mạch”. Khi cần biết trị số chính xác, hãy chuyển sang thang đo Ω.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-thực-tế-thanh-nguồn-tưởng-có-điện-nhưng-thực-chất-đang-hở&quot;&gt;Ví dụ thực tế: thanh nguồn tưởng có điện nhưng thực chất đang hở&lt;/h2&gt;

&lt;p&gt;Giả sử bạn nối nguồn từ củ sạc USB vào nửa bên trái của thanh đỏ và thanh xanh trên breadboard 830 lỗ (chưa gắn ESP32 hay linh kiện nào khác). Đặt đồng hồ ở thang 20 V DC, que đen ở COM, que đỏ ở lỗ V/Ω.&lt;/p&gt;

&lt;p&gt;Khi đo giữa thanh đỏ trái và thanh xanh trái, đồng hồ hiển thị 5,08 V — mức điện áp bình thường của củ sạc tốt. Giữ nguyên que đen ở thanh xanh trái và di chuyển que đỏ sang nửa phải của thanh đỏ (qua khe ngắt trên vạch in), màn hình chỉ hiện 0,02 V. Đây không phải sụt áp nhỏ trên dây mà là hở mạch. Bạn rút củ sạc để cắt hoàn toàn nguồn, chuyển đồng hồ sang thang thông mạch và đo dọc thanh đỏ qua khe giữa thì thấy im lặng. Sau khi cắm thêm một dây nối cầu qua khe giữa thanh đỏ và một dây qua khe giữa thanh xanh rồi cấp nguồn lại, nửa phải thanh đỏ đo được 5,07 V.&lt;/p&gt;

&lt;p&gt;Lý do tuyệt đối không kẹp lỗ đo dòng ngang củ sạc có thể thấy qua phép ước tính với điện trở shunt 10 A cỡ $0{,}01\ \Omega$:&lt;/p&gt;

\[I \approx \frac{5}{0.01} = 500\ \mathrm{A}\]

&lt;p&gt;Dĩ nhiên củ sạc thực tế sẽ kích hoạt bảo vệ và giới hạn dòng về khoảng 1–2 A, nhưng mức đó vẫn dư sức làm đứt cầu chì 200 mA bên trong đồng hồ. Bài toán đo dòng hai động cơ TT kẹt trục (mỗi chiếc cỡ 1 A) sẽ thực hiện về sau bằng cách mắc nối tiếp trên nguồn pin động cơ, phát một xung ngắn và cắm que đỏ vào lỗ đo dòng có giới hạn phù hợp.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành&quot;&gt;Thực hành&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Ngắt mọi nguồn điện. Cắm que đen vào COM, que đỏ vào lỗ V/Ω và xoay núm sang thang thông mạch.&lt;/li&gt;
  &lt;li&gt;Đo thông mạch một sợi dây jumper từ đầu này sang đầu kia để chắc chắn có tiếng bíp. Cắm một đầu dây vào thanh đỏ, đầu kia vào một nhóm năm lỗ. Đo thông mạch từ một lỗ khác của nhóm đó về một lỗ khác trên thanh đỏ (phải có tiếng bíp), sau đó chạm sang cột kế bên (phải im lặng).&lt;/li&gt;
  &lt;li&gt;Đo thông mạch xuyên qua rãnh giữa trên cùng một số cột và xác nhận không có tiếng bíp.&lt;/li&gt;
  &lt;li&gt;Kiểm tra chỗ ngắt giữa của thanh nguồn: đo từ nửa trái sang nửa phải thanh đỏ. Với loại 830 lỗ có khe ngắt, đồng hồ sẽ im; hãy cắm một dây nối cầu qua khe rồi đo lại để xác nhận đã kêu bíp. Lặp lại tương tự với thanh xanh.&lt;/li&gt;
  &lt;li&gt;Chuyển sang đo điện áp bằng củ sạc điện thoại hoặc pin dự phòng (không dùng GPIO hay chân 3V3). Nếu có mạch tách nguồn USB, đưa 5 V và GND vào thanh nguồn vừa kiểm tra; nếu đo trực tiếp đầu dây nguồn, đặt que đỏ vào cực 5 V và que đen vào GND. Chọn thang DC cao hơn 5 V (thường là 20 V) và ghi lại số đo.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả kỳ vọng:&lt;/strong&gt; Đồng hồ kêu bíp qua dây jumper tốt và trong cùng một nhóm năm lỗ; im lặng qua rãnh giữa và qua khe ngắt thanh nguồn trước khi nối cầu. Điện áp đo được của củ sạc nằm trong khoảng 4,7 V đến 5,3 V. Nếu đo ra gần 0 V dù thông mạch đã tốt, hãy kiểm tra lại xem bạn có đặt que nhầm thanh nguồn hoặc củ sạc chưa có điện không.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng quan sát&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Cách khắc phục&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Không kêu bíp dù dây jumper trông đã cắm sâu&lt;/td&gt;
      &lt;td&gt;Chân dây trượt khỏi lò xo hoặc kẹp lỗ đó bị giãn&lt;/td&gt;
      &lt;td&gt;Chuyển sang lỗ khác cùng cột; nếu cả cột tiếp xúc kém thì đánh dấu bỏ cột đó&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nửa trái thanh nguồn đo ~5 V, nửa phải ~0 V&lt;/td&gt;
      &lt;td&gt;Khe ngắt giữa thanh nguồn chưa được nối cầu&lt;/td&gt;
      &lt;td&gt;Cắm dây nối cầu qua khe giữa cả thanh đỏ lẫn thanh xanh rồi đo lại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thang thông mạch kêu bíp khi vẫn đang cắm nguồn&lt;/td&gt;
      &lt;td&gt;Đo ohm hoặc thông mạch trên mạch còn điện&lt;/td&gt;
      &lt;td&gt;Rút nguồn ngay trước khi đo thông mạch hoặc điện trở&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Màn hình lóe lên, sau đó thang mA luôn đọc 0 dù thang 20 V vẫn bình thường&lt;/td&gt;
      &lt;td&gt;Quên que đỏ ở lỗ đo dòng rồi kẹp ngang nguồn sạc&lt;/td&gt;
      &lt;td&gt;Cầu chì mA đã đứt; không thử kẹp lại lần nữa để kiểm chứng&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý an toàn:&lt;/strong&gt; Luôn bắt đầu ở thang đo áp cao hơn điện áp dự kiến (tránh để thang 200 mV khi đo pin 2S gần 8 V). Giữ ngón tay phía sau gờ bảo vệ, không chạm vào đầu kim loại của que đo. Không đo dòng trên cổng USB và không đưa nguồn 5 V của củ sạc vào chân GPIO của ESP32 hay Pico.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=FsBgW0WDQSw&quot;&gt;CÁCH SỬ DỤNG ĐỒNG HỒ VẠN NĂNG ĐO ĐIỆN ĐƠN GIẢN&lt;/a&gt; — Sơn Tiện Ích, tiếng Việt. Hướng dẫn bằng tiếng Việt cách chọn đúng lỗ cắm và thang đo khi đo điện áp, điện trở.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=W6mixXsn-Vc&quot;&gt;Breadboard tutorial: How to use a breadboard (for beginners)&lt;/a&gt; — LeftyMaker, tiếng Anh. Giải thích rõ sơ đồ nối sẵn bên trong breadboard và thói quen dùng thông mạch để kiểm chứng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Một breadboard có 30 cột ở mỗi bên rãnh giữa, đồng thời cả thanh nguồn trên và dưới đều bị ngắt đôi một lần ở giữa. Hỏi khu vực trung tâm có bao nhiêu nút năm lỗ độc lập, và có tất cả bao nhiêu đoạn thanh nguồn tách biệt?&lt;/li&gt;
  &lt;li&gt;Bạn cần đo điện áp rơi trên điện trở 180 Ω đang mắc nối tiếp với LED trên nguồn 3,3 V. Que đo cắm vào lỗ nào, núm vặn chọn thang gì, và có được tháo LED ra khi đo không?&lt;/li&gt;
  &lt;li&gt;Một sợi dây jumper đo thông mạch trực tiếp hai đầu kêu bíp rõ (khoảng 0,3 Ω), nhưng khi cắm một đầu vào lỗ breadboard thì đo từ lỗ bên cạnh cùng cột sang đầu dây kia lại không kêu. Điểm hở mạch nằm ở đâu?&lt;/li&gt;
  &lt;li&gt;Một bạn định “đo dòng USB” bằng cách kẹp hai que đồng hồ trực tiếp vào 5 V và GND, thấy màn hình lóe lên rồi từ đó thang mA luôn đọc 0 trong khi thang 20 V vẫn đo áp bình thường. Bộ phận nào đã hỏng?&lt;/li&gt;
  &lt;li&gt;Vì sao việc thang thông mạch không kêu bíp không đủ cơ sở để kết luận một điện trở 180 Ω là đúng trị số hay đã hỏng?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Mỗi cột có hai nút độc lập ở hai bên rãnh giữa nên tổng cộng có $30 \times 2 = 60$ nút năm lỗ. Với 4 thanh nguồn (đỏ trên, xanh trên, đỏ dưới, xanh dưới) đều tách đôi ở giữa, ta có 8 đoạn thanh nguồn độc lập.
2. Que đen cắm ở COM, que đỏ ở lỗ V/Ω, núm chọn thang DC V cao hơn 3,3 V. Đặt hai que đo song song vào hai đầu điện trở trong khi vẫn giữ LED trong mạch; nếu tháo LED sẽ làm hở mạch khiến điện áp rơi trên điện trở về 0 V.
3. Bản thân sợi dây vẫn tốt; điểm hở nằm ở tiếp xúc giữa chân dây và kẹp lò xo của lỗ cắm đó. Chỉ cần chuyển sang lỗ khác trong cùng cột, không cần bỏ sợi dây.
4. Cầu chì bảo vệ thang mA bên trong đồng hồ đã đứt do bị nối tắt ngang nguồn 5 V. Thang đo điện áp không đi qua cầu chì này nên vẫn hoạt động. Không thử chập lại lần nữa.
5. Thang thông mạch chỉ phát hiện mức điện trở rất thấp (gần ngắn mạch), nên cả điện trở 180 Ω lẫn 100 kΩ đều thường không kêu bíp. Phải dùng thang đo điện trở (Ω) trên linh kiện đã ngắt nguồn mới xác định được trị số thật.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;p&gt;Bài hướng dẫn của SparkFun giải thích rõ cấu tạo thanh nguồn, dải năm lỗ và rãnh giữa: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard&quot;&gt;How to use a breadboard&lt;/a&gt;. Bài hướng dẫn về đồng hồ vạn năng minh họa trực quan vị trí lỗ cắm cho từng chức năng: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-use-a-multimeter&quot;&gt;How to use a multimeter&lt;/a&gt; — hãy đọc kỹ mục đo dòng và tạo thói quen nhìn lại lỗ cắm que đỏ trước mỗi lần đo.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Toàn bộ danh sách linh kiện robot nằm tại &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;. Bài này chỉ liệt kê lại ba dụng cụ đo thử cơ bản nếu bạn chưa mua sẵn (giá tham khảo trên Hshop ngày 23 tháng 9 năm 2026; cần kiểm tra lại trang bán trước khi thanh toán).&lt;/p&gt;

&lt;p&gt;Breadboard 830 lỗ giá 35.000 ₫: &lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;test board CAMMB-102&lt;/a&gt;. Dây cắm đực-đực (bộ 40 sợi) giá 30.000 ₫: &lt;a href=&quot;https://hshop.vn/day-cam-breadboard-duc-duc-20cm-cap-det-40-soi-m-m-jumper-wire&quot;&gt;dây cắm breadboard đực-đực&lt;/a&gt;. Đồng hồ vạn năng UNI-T UT33D+ giá 285.000 ₫: &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;đồng hồ vạn năng UNI-T UT33D+&lt;/a&gt;. Nếu chọn mẫu đồng hồ thay thế khác, cần đảm bảo có còi thông mạch và thang đo mA có cầu chì bảo vệ.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Điện áp, dòng điện, công suất và định luật Ohm</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_01_voltage_current_ohm/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter01/01_01_voltage_current_ohm</id>
   <content type="html">&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ nêu đúng định nghĩa và đơn vị của điện áp, dòng điện, điện trở và công suất. Bạn sẽ vận dụng $V = IR$ để tính đại lượng còn thiếu trong một mạch vòng, đồng thời tính công suất tỏa nhiệt trên điện trở bằng $P = VI = I^2 R = V^2/R$. Bạn sẽ chọn được điện trở hạn dòng nối tiếp cho LED đỏ trên nguồn 3,3 V, kiểm chứng mức công suất 1/4 W là hoàn toàn dư dả, và giải thích được vì sao không thể xem động cơ TT như một điện trở để đấu thẳng vào chân GPIO của ESP32 hay Raspberry Pi Pico.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-gì-trước&quot;&gt;Cần có gì trước&lt;/h2&gt;

&lt;p&gt;Bạn chỉ cần nắm vững bốn phép tính số học cơ bản và giữ thói quen luôn ghi kèm đơn vị sau mỗi con số (viết mỗi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,008&lt;/code&gt; rất dễ gây nhầm giữa 8 mA và lỗi lệch dấu phẩy). Bài này có thể làm các ví dụ tính toán trên giấy mà chưa cần breadboard; nếu đã có sẵn đồng hồ vạn năng và LED đỏ, bạn có thể làm luôn phần thực hành.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Mọi sự cố cháy LED, sập nguồn vi điều khiển hay nóng rực dây dẫn trên xe robot đều có thể dự đoán trước chỉ bằng hai phương trình dài đúng ba ký tự: &lt;strong&gt;định luật Ohm (\(V = I R\))&lt;/strong&gt; và &lt;strong&gt;công suất điện (\(P = V I\))&lt;/strong&gt;. Thay vì đoán mò chọn đại một điện trở rồi “cắm thử xem có bốc khói không”, bài học này giúp bạn dùng phép tính tay sơ cấp để làm chủ hoàn toàn dòng chảy năng lượng trên mọi bo mạch robot.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-vật-lý-mạch-điện-một-chiều-dc-trên-robot&quot;&gt;Kiến thức cần nhớ: Vật lý Mạch điện Một chiều (DC) trên Robot&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Điện áp (\(V\)), Dòng điện (\(I\)) và Điện trở (\(R\)):&lt;/strong&gt; Hãy ví điện áp như &lt;em&gt;áp suất nước&lt;/em&gt;, dòng điện như &lt;em&gt;lưu lượng nước chảy qua ống&lt;/em&gt;, và điện trở như &lt;em&gt;độ hẹp của van&lt;/em&gt;. Định luật Ohm \(I = V / R\) khẳng định dòng điện tăng tỉ lệ thuận với hiệu điện thế và tỉ lệ nghịch với điện trở cản dòng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Công suất Tiêu tán &amp;amp; Giới hạn Nhiệt (\(P = V I = I^2 R\)):&lt;/strong&gt; Mọi dòng điện đi qua điện trở đều biến thành nhiệt. Điện trở chân cắm thông thường chỉ chịu được &lt;strong&gt;\(0.25\text{ W}\)&lt;/strong&gt; (1/4 W); vượt ngưỡng đó điện trở sẽ đổi màu nâu và cháy đứt!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Giới hạn Dòng Chân GPIO &amp;amp; Mạch Phân áp:&lt;/strong&gt; Chân GPIO của ESP32/Pico chỉ là chân &lt;em&gt;tín hiệu&lt;/em&gt; (cấp tối đa ~\(12\text{–}20\text{ mA}\)), tuyệt đối không đủ kéo động cơ (\(500\text{–}1{,}500\text{ mA}\)). Đồng thời, hai điện trở mắc nối tiếp tạo thành &lt;strong&gt;mạch phân áp&lt;/strong&gt; \(V_{\text{out}} = V_{\text{in}}\frac{R_2}{R_1+R_2}\) giúp hạ tín hiệu 5 V xuống mức an toàn 3,3 V.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-điện-trở-hạn-dòng-led-33-v-và-mạch-phân-áp-5-v--33-v&quot;&gt;Ví dụ làm từng bước: Tính điện trở hạn dòng LED (3,3 V) và Mạch phân áp 5 V → 3,3 V&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Chọn điện trở nối tiếp cho đèn LED đỏ trên chân GPIO 3,3 V:&lt;/strong&gt; biết điện áp rơi thuận của LED đỏ là \(V_f = 2.0\text{ V}\) và ta muốn dòng sáng êm là \(I = 10\text{ mA} = 0.01\text{ A}\):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Điện áp còn lại rơi trên điện trở: \(V_R = 3.3 - 2.0 = \mathbf{1.3\text{ V}}\).&lt;/li&gt;
  &lt;li&gt;Trị số điện trở lý thuyết: \(R = \dfrac{V_R}{I} = \dfrac{1.3}{0.01} = \mathbf{130\ \Omega}\).&lt;/li&gt;
  &lt;li&gt;Chọn trị số phổ thông gần nhất lớn hơn (\(R = 150\ \Omega\) hoặc \(220\ \Omega\)): với \(150\ \Omega\), dòng thực là \(I = 1.3 / 150 = \mathbf{8.67\text{ mA}}\) (rất an toàn cho GPIO!) và công suất tỏa nhiệt là:
\(P_R = V_R \cdot I = 1.3 \times 0.00867 \approx \mathbf{0.0113\text{ W} = 11.3\text{ mW}} \ll 250\text{ mW}!\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Hạ tín hiệu Echo 5,0 V của cảm biến siêu âm xuống mức an toàn 3,3 V:&lt;/strong&gt; chọn \(R_1 = 1\text{ k}\Omega\) (phía trên) và \(R_2 = 2\text{ k}\Omega\) (phía xuống GND):&lt;/p&gt;

\[V_{\text{out}} = 5.0 \times \frac{2{,}000}{1{,}000 + 2{,}000} = 5.0 \times \frac{2}{3} \approx \mathbf{3.33\text{ V}}!\]

&lt;h3 id=&quot;giải-thích-4-bước-tính-nhẩm-mọi-mạch-dc-nhỏ-trước-khi-cắm-điện&quot;&gt;Giải thích: 4 bước tính nhẩm mọi mạch DC nhỏ trước khi cắm điện&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Đọc điện áp nguồn và điện áp rơi của tải:&lt;/strong&gt; Luôn trừ đi điện áp rơi của LED/diode (\(V_f \approx 2.0\text{ V}\) cho LED đỏ, \(3.0\text{ V}\) cho LED xanh/trắng) trước khi chia cho dòng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Kiểm tra trần dòng của nguồn cấp (GPIO vs. Pin):&lt;/strong&gt; Nếu tải cần \(&amp;gt; 15\text{ mA}\), không nối thẳng vào GPIO mà phải lái qua MOSFET hoặc IC driver động cơ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Kiểm tra công suất tỏa nhiệt \(P = I^2 R\):&lt;/strong&gt; Luôn giữ công suất thực dưới \(50\%\) định mức của điện trở (tức \(&amp;lt; 0.125\text{ W}\) với điện trở 1/4 W) để linh kiện luôn mát tay.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Không dùng mạch phân áp điện trở để cấp &lt;em&gt;nguồn&lt;/em&gt;:&lt;/strong&gt; Mạch phân áp chỉ dùng cho &lt;em&gt;tín hiệu&lt;/em&gt; (dòng micro-ampe); nếu mắc tải nặng vào \(V_{\text{out}}\), điện áp sẽ sụt thê thảm — muốn cấp nguồn phải dùng IC ổn áp/buck!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Điều gì xảy ra nếu bạn quên mắc điện trở hạn dòng và nối thẳng LED đỏ (\(V_f = 2.0\text{ V}\)) vào nguồn 3,3 V có dòng lớn? Điện trở động của LED gần như bằng 0 (chỉ vài ohm), nên dòng vọt lên hàng trăm mA và &lt;strong&gt;đốt cháy LED ngay lập tức&lt;/strong&gt;!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;bài-học-này-nằm-ở-đâu-trên-xe-hai-bánh&quot;&gt;Bài học này nằm ở đâu trên xe hai bánh&lt;/h2&gt;

&lt;p&gt;Đồ án Capstone là một xe dẫn động vi sai hai bánh sử dụng ESP32 hoặc Pico ở mức logic 3,3 V, mạch lái TB6612FNG điều khiển hai động cơ TT từ nguồn pin riêng, và cảm biến HC-SR04 đo khoảng cách ở giai đoạn sau. Đèn báo trạng thái chương trình chỉ gồm một LED đỏ mắc nối tiếp điện trở trên đường nguồn 3,3 V: chọn điện trở quá lớn sẽ làm đèn tối đến mức tưởng nhầm firmware bị treo, còn bỏ quên điện trở sẽ làm hỏng LED hoặc chân GPIO.&lt;/p&gt;

&lt;p&gt;Trong khi đó, một động cơ TT khi kẹt trục có thể rút dòng cỡ ampe, còn chân GPIO chỉ nên cấp dòng an toàn quanh 12 mA và có ngưỡng nguy hiểm khoảng 40 mA. Nếu dòng động cơ lấy chung từ cổng USB đang nuôi vi điều khiển, đường nguồn 5 V sẽ sụt áp khiến mạch ổn áp không giữ nổi mức 3,3 V và chip bị reset đúng lúc bánh xe nhận lệnh chạy. Định luật Ohm giúp bạn tách bạch lỗi nguồn điện với lỗi phần mềm ngay từ bước tính toán.&lt;/p&gt;

&lt;h2 id=&quot;điện-áp-dòng-điện-và-điện-trở&quot;&gt;Điện áp, dòng điện và điện trở&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Điện áp&lt;/strong&gt; là năng lượng trên mỗi đơn vị điện tích, luôn được định nghĩa là hiệu điện thế giữa hai điểm. Một vôn tương ứng với một joule trên một coulomb. Nói một chân có điện áp 3,3 V nghĩa là điện tích di chuyển từ chân đó về mass giải phóng 3,3 J cho mỗi coulomb. Vì là hiệu giữa hai điểm nên chạm một que đo đơn lẻ không thể đo được điện áp.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dòng điện&lt;/strong&gt; là lượng điện tích đi qua một điểm trong mỗi giây, ký hiệu là $I$ với đơn vị ampe (1 A bằng 1 coulomb/giây). Ở quy mô chân GPIO và LED, đơn vị thường dùng là miliampe:&lt;/p&gt;

\[1\ \mathrm{mA} = 0.001\ \mathrm{A}\]

&lt;p&gt;Do đó 8 mA tương đương $0{,}008\ \mathrm{A}$. Động cơ TT hoạt động ở thang dòng lớn hơn nhiều: vài trăm miliampe khi chạy tự do và lên tới cỡ ampe khi kẹt trục. Toàn bộ sơ đồ trong khóa học tuân theo quy ước chiều dòng điện đi từ cực dương sang cực âm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Điện trở&lt;/strong&gt; $R$, đơn vị ôm ($\Omega$), đặc trưng cho mức độ cản trở dòng điện của linh kiện. Ở cùng một điện áp, điện trở càng lớn thì dòng điện càng nhỏ. Một sợi dây jumper chỉ có điện trở vài phần mười ôm, điện trở 180 Ω là mức cản dòng được tính toán có chủ đích, còn một công tắc đang hở có điện trở rất lớn khiến dòng điện coi như bằng 0.&lt;/p&gt;

&lt;p&gt;Với linh kiện thuần trở ở nhiệt độ ổn định, ba đại lượng liên hệ với nhau qua &lt;strong&gt;định luật Ohm&lt;/strong&gt;:&lt;/p&gt;

\[V = IR\]

&lt;p&gt;Từ đây ta suy ra $I = V/R$ và $R = V/I$. Lưu ý rằng định luật Ohm áp dụng chính xác cho điện trở, nhưng không mô tả tuyến tính cho LED. Đèn LED gần như không dẫn điện cho đến khi điện áp đặt lên nó đạt mức điện áp thuận $V_f$, sau đó dòng tăng rất dốc. Vì lý do này, LED luôn phải mắc kèm điện trở hạn dòng chứ không thể xem là một điện trở thông thường.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ohms_law_triangle.png&quot; alt=&quot;Tam giác định luật Ohm: che đại lượng cần tìm&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Tam giác trên là mẹo ghi nhớ nhanh cho $V = IR$: che đại lượng cần tìm để thấy phép tính tương ứng (che $V$ được $I \times R$, che $I$ được $V / R$). Lưu ý rằng công suất không nằm trong tam giác này.&lt;/p&gt;

&lt;h2 id=&quot;công-suất-và-mạch-led-nối-tiếp&quot;&gt;Công suất và mạch LED nối tiếp&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Công suất&lt;/strong&gt; là năng lượng tiêu thụ hoặc tỏa ra trong mỗi giây, đơn vị oát (W). Với linh kiện hai chân, công suất bằng tích điện áp rơi trên chính linh kiện đó và dòng điện chạy qua nó:&lt;/p&gt;

\[P = VI\]

&lt;p&gt;Đối với điện trở, thay $I = V/R$ vào công thức trên ta được:&lt;/p&gt;

\[P = \frac{V^2}{R}\]

&lt;p&gt;Và nếu thay $V = IR$ ta được:&lt;/p&gt;

\[P = I^2 R\]

&lt;p&gt;Cả ba biểu thức đều cho cùng một giá trị công suất trên điện trở. Tuy nhiên với LED, điện áp trên LED giữ gần mức $V_f$ còn dòng điện do điện trở hạn chế, nên ta tính công suất LED bằng $P = V_f I$ và công suất điện trở bằng $I^2 R$. Khi dùng $V^2/R$ cho điện trở, cần nhớ $V$ là điện áp rơi riêng trên điện trở chứ không phải điện áp toàn nguồn.&lt;/p&gt;

&lt;p&gt;Điện trở thân 1/4 W chịu được công suất tối đa 0,25 W trong điều kiện thoáng khí. Điện trở dùng cho đèn LED chỉ tỏa vài chục milioát nên rất mát; ngược lại, ép một điện trở nhỏ gánh dòng cỡ ampe sẽ làm cháy xém vỏ, và công thức $I^2 R$ cho phép bạn dự đoán trước điều này.&lt;/p&gt;

&lt;p&gt;Trong mạch nối tiếp, dòng điện tại mọi điểm đều bằng nhau và tổng các điện áp rơi bằng điện áp nguồn:&lt;/p&gt;

\[V_\mathrm{s} = V_R + V_f\]

&lt;p&gt;Suy ra $V_R = V_\mathrm{s} - V_f$ và $I = V_R / R$. Ngược lại, trong mạch song song, các nhánh chịu cùng điện áp còn tổng dòng điện bằng tổng dòng qua từng nhánh — đây chính là trường hợp hai động cơ đấu trên cùng một nguồn pin. Nguồn pin phải gánh tổng dòng của cả hai động cơ, và cổng USB của vi điều khiển không được thiết kế cho vai trò đó.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-toán-đèn-báo-trên-đường-33-v&quot;&gt;Ví dụ tính toán: đèn báo trên đường 3,3 V&lt;/h2&gt;

&lt;p&gt;Giả sử ta dùng nguồn 3,3 V để thắp một LED đỏ có điện áp thuận $V_f \approx 2{,}0\ \mathrm{V}$, với dòng mục tiêu 8 mA để đủ sáng trong phòng mà vẫn an toàn cho chân mạch. Trị số điện trở lý thuyết là:&lt;/p&gt;

\[R = \frac{V_\mathrm{s} - V_f}{I} = \frac{3.3 - 2.0}{0.008} = \frac{1.3}{0.008} = 162.5\ \Omega\]

&lt;p&gt;Chọn trị số tiêu chuẩn gần nhất lớn hơn là 180 Ω để dòng thực tế không vượt mức mục tiêu:&lt;/p&gt;

\[I = \frac{3.3 - 2.0}{180} = \frac{1.3}{180} \approx 7.2\ \mathrm{mA}\]

&lt;p&gt;Để kiểm tra độ an toàn nhiệt một cách thận trọng, ta tính thử với dòng mục tiêu 8 mA và điện trở 180 Ω:&lt;/p&gt;

\[P = I^2 R = (0.008)^2 \times 180 = 0.0115\ \mathrm{W} \approx 0.012\ \mathrm{W}\]

&lt;p&gt;So với định mức 1/4 W (0,25 W), điện trở chỉ hoạt động ở mức dưới 5% công suất cho phép. Nếu tính theo dòng thực tế 7,2 mA, công suất tỏa nhiệt trên điện trở còn nhỏ hơn nữa, khoảng 0,009 W — khớp với cả hai công thức còn lại khi dùng điện áp 1,3 V rơi riêng trên điện trở: $P = VI \approx 0{,}0072 \times 1{,}3 \approx 0{,}009\ \mathrm{W}$ và $P = V^2/R = 1{,}69/180 \approx 0{,}009\ \mathrm{W}$. Bản thân đèn LED tiêu thụ $P_\mathrm{LED} \approx 2{,}0 \times 0{,}0072 \approx 0{,}014\ \mathrm{W}$.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/led_series_resistor.png&quot; alt=&quot;Một vòng: nguồn, điện trở nối tiếp, LED, mass&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Dòng điện đi từ cực dương, qua điện trở, vào chân anot của LED, ra chân catot rồi về mass. Vì mắc nối tiếp nên dòng qua điện trở bằng đúng dòng qua LED. Nếu cắm ngược cực LED thì đèn không sáng, nhưng tuyệt đối không được bỏ điện trở khi xoay lại chiều. Không có điện trở hạn dòng, LED sẽ bị ép gần mức 3,3 V trong khi điện áp thuận chỉ khoảng 2 V, khiến dòng tăng vọt gấp nhiều lần ngưỡng 20 mA và nhanh chóng làm hỏng linh kiện.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-tính-toán-vì-sao-không-đấu-trực-tiếp-động-cơ-vào-gpio&quot;&gt;Ví dụ tính toán: vì sao không đấu trực tiếp động cơ vào GPIO&lt;/h2&gt;

&lt;p&gt;Giả sử một động cơ TT khi kẹt trục kéo dòng khoảng 0,8 A ở điện áp 6 V. Nếu tạm tính điện trở tương đương của cuộn dây khi kẹt theo định luật Ohm:&lt;/p&gt;

\[R_\mathrm{stall} \approx \frac{6}{0.8} = 7.5\ \Omega\]

&lt;p&gt;Nếu nối trực tiếp tải này vào chân GPIO 3,3 V, dòng điện ước tính sẽ là:&lt;/p&gt;

\[I \approx \frac{3.3}{7.5} \approx 0.44\ \mathrm{A} = 440\ \mathrm{mA}\]

&lt;p&gt;Trong khi đó, mức dòng khuyến nghị cho một chân GPIO chỉ khoảng 12 mA và ngưỡng tối đa thường nêu khoảng 40 mA. Dòng 440 mA vượt ngưỡng an toàn tới hàng chục lần, đủ làm cháy chân GPIO hoặc gây sụt áp ổn áp khiến vi điều khiển tự khởi động lại. Hơn nữa, động cơ là tải cảm kèm hộp số chứ không phải điện trở thuần 7,5 Ω: khi rô-to quay tự do, sức điện động ngược làm giảm dòng tiêu thụ, nên kết quả thử không tải không phản ánh đúng dòng đỉnh khi bánh xe bị kẹt vào vật cản.&lt;/p&gt;

&lt;p&gt;Vì vậy, dòng cấp cho động cơ phải đi qua chân VM của TB6612FNG từ nguồn pin riêng (dải 4,5 V đến 10 V). Chân GPIO chỉ truyền tín hiệu chiều quay và PWM vào ngõ logic (dải 2,7 V đến 5,5 V, tương thích mức cao 3,3 V). Khi cả hai động cơ TT cùng kẹt, tổng dòng yêu cầu lên tới khoảng 1,6 A, vượt xa mức ~0,5 A của cổng USB máy tính — do đó động cơ tuyệt đối không được lấy nguồn từ USB hay chân 3,3 V.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành&quot;&gt;Thực hành&lt;/h2&gt;

&lt;p&gt;Nếu đã có sẵn linh kiện, hãy lắp mạch đèn báo theo đúng các bước sau:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Ghi sẵn phép tính $R = (3{,}3 - 2{,}0) / 0{,}008 = 162{,}5\ \Omega$ và chọn điện trở 180 Ω ra giấy trước khi cắm bất kỳ linh kiện nào lên breadboard.&lt;/li&gt;
  &lt;li&gt;Mắc nối tiếp điện trở 180 Ω và LED đỏ giữa chân 3V3 và GND (lưu ý lấy từ chân nguồn 3V3 trên board, không lấy từ chân GPIO hay chân 5V). Chân dài của LED (anot) hướng về phía dương; cạnh vát phẳng trên vành nhựa (catot) hướng về phía GND.&lt;/li&gt;
  &lt;li&gt;Kiểm tra chắc chắn điện trở đã nằm đúng trong vòng nối tiếp rồi mới cấp nguồn. Quan sát ánh sáng LED từ góc nghiêng thay vì nhìn thẳng vào thấu kính.&lt;/li&gt;
  &lt;li&gt;Chuyển đồng hồ vạn năng sang thang đo điện áp một chiều (DC V), kiểm tra que đỏ đang ở lỗ đo áp, rồi đo trực tiếp hai đầu điện trở. Lấy điện áp đo được chia cho 180 Ω để tính dòng thực tế mà không cần ngắt mạch đo ampe (phép đo dòng sẽ học ở bài sau).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả kỳ vọng:&lt;/strong&gt; LED phát sáng đỏ rõ, không chói gắt; điện áp rơi trên điện trở 180 Ω nằm trong khoảng 1,1 V đến 1,5 V (tương ứng dòng khoảng 6 mA đến 8 mA) và thân điện trở không nóng. Nếu thay bằng điện trở 220 Ω, dòng còn $I = 1{,}3 / 220 \approx 5{,}9\ \mathrm{mA}$, đèn hơi dịu hơn nhưng vẫn đạt yêu cầu.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hiện tượng quan sát&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Cách khắc phục&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;LED không sáng, điện trở nguội&lt;/td&gt;
      &lt;td&gt;Cắm ngược cực LED&lt;/td&gt;
      &lt;td&gt;Đảo chiều hai chân LED, vẫn giữ nguyên điện trở trong mạch&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LED lóe sáng rồi tắt hẳn&lt;/td&gt;
      &lt;td&gt;Thiếu điện trở nối tiếp, hoặc cắm hai chân điện trở vào cùng hàng lỗ breadboard làm nối tắt&lt;/td&gt;
      &lt;td&gt;Thay LED mới, lắp lại đúng với điện trở 180 Ω trước khi kiểm tra chân mạch&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Board bị reset hoặc chân GPIO ngừng hoạt động&lt;/td&gt;
      &lt;td&gt;Đưa nguồn 5 V vào chân 3,3 V hoặc đấu LED giữa 5 V và chân GPIO&lt;/td&gt;
      &lt;td&gt;Ngắt nguồn ngay; chỉ đấu đèn giữa chân 3V3 và GND; kiểm tra lại chân bằng LED tốt kèm trở 180 Ω&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LED tối mờ gần như không thấy&lt;/td&gt;
      &lt;td&gt;Cắm nhầm điện trở trị số hàng chục kilôôm&lt;/td&gt;
      &lt;td&gt;Đo lại trị số điện trở; với 10 kΩ, $I = 1{,}3 / 10000 = 0{,}13\ \mathrm{mA}$ nên gần như tắt&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý an toàn:&lt;/strong&gt; Nếu đã lắp bánh xe, luôn nhấc bánh khỏi mặt bàn; không nối nguồn động cơ trong bài này và tuyệt đối không đưa nguồn VM hay nguồn USB 5 V vào chân GPIO.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=HsLLq6Rm5tU&quot;&gt;Ohms Law Explained - The basics circuit theory&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Minh họa trực quan mối liên hệ giữa điện áp, dòng điện, điện trở và công suất trên một mạch đơn giản.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Pd7jlqP4CRw&quot;&gt;video tập giảng: Định luật Ôm đối với toàn mạch- Trần Thị Hồng Nhung&lt;/a&gt; — Trường Đại học Giáo dục - ĐHQGHN, tiếng Việt. Bài giảng tiếng Việt về định luật Ohm cho toàn mạch, có đề cập sức điện động và điện trở trong.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Cần thắp sáng một LED xanh lá có $V_f = 2{,}2\ \mathrm{V}$ từ nguồn 5 V với dòng mục tiêu 10 mA (mắc giữa nguồn 5 V và GND, không dùng GPIO). Tính giá trị $R$, chọn trị số kế trên trong tập {220 Ω, 270 Ω, 330 Ω} và tính công suất tỏa nhiệt $P$ trên điện trở đã chọn.&lt;/li&gt;
  &lt;li&gt;Nếu không có sẵn điện trở 180 Ω, bạn thay bằng điện trở 220 Ω cho LED đỏ ($V_f = 2{,}0\ \mathrm{V}$) trên nguồn 3,3 V. Hãy tính dòng điện qua mạch và nhận xét độ sáng trong phòng.&lt;/li&gt;
  &lt;li&gt;Một động cơ TT tiêu thụ 0,15 A khi chạy không tải và 0,8 A khi kẹt trục. Khi hai động cơ chạy cùng lúc, trường hợp nào (nếu có) nằm trong giới hạn 0,5 A của cổng USB?&lt;/li&gt;
  &lt;li&gt;Một người đo được 3,3 V trên một đèn LED đang tối (mắc không có điện trở nối tiếp) và kết luận rằng vì có đủ điện áp nên LED còn tốt. Dựa vào $V_\mathrm{s} = V_R + V_f$, lập luận này sai ở điểm nào?&lt;/li&gt;
  &lt;li&gt;Với điện trở 180 Ω chịu điện áp rơi 1,3 V, hãy chứng minh ba công thức $P = VI$, $P = I^2 R$ và $P = V^2/R$ đều cho cùng một kết quả. Vì sao không được thay $V = 3{,}3\ \mathrm{V}$ vào công thức $V^2/R$ cho điện trở này?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. $R = (5 - 2{,}2) / 0{,}010 = 280\ \Omega$. Chọn trị số kế trên là 330 Ω. Khi đó $I = 2{,}8 / 330 \approx 8{,}5\ \mathrm{mA}$ và $P \approx 0{,}024\ \mathrm{W}$, rất nhỏ so với định mức 0,25 W. Lưu ý mạch đèn này lấy nguồn 5 V độc lập, không mắc vào chân GPIO.
2. $I = 1{,}3 / 220 \approx 5{,}9\ \mathrm{mA}$. Hoàn toàn dùng làm đèn báo trong phòng được (chỉ dịu hơn mức 8 mA một chút). Không tháo bỏ điện trở để làm đèn sáng hơn.
3. Khi chạy không tải: tổng dòng $0{,}30\ \mathrm{A} &amp;lt; 0{,}5\ \mathrm{A}$. Khi kẹt trục: tổng dòng $1{,}6\ \mathrm{A} &amp;gt; 0{,}5\ \mathrm{A}$, vượt xa giới hạn cổng USB. Do đó, chạy thử không tải có thể tưởng là ổn nhưng mạch sẽ reset ngay khi bánh xe gặp vật cản.
4. Khi không có điện trở, $V_R = 0$ nên toàn bộ 3,3 V đặt trực tiếp lên LED. Một LED đỏ còn tốt đang dẫn điện chỉ giữ điện áp quanh 2 V khi có điện trở hạn dòng; việc đo thấy trọn 3,3 V trên một LED tối chứng tỏ LED đang bị cắm ngược, hở mạch hoặc đã cháy đứt.
5. Với $I = 1{,}3 / 180 \approx 7{,}2\ \mathrm{mA}$, cả ba công thức đều cho kết quả xấp xỉ 0,0094 W. Không được thay 3,3 V vì điện trở chỉ gánh 1,3 V; phần 2,0 V còn lại rơi trên LED.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;p&gt;Tài liệu trực tuyến của All About Circuits trình bày chi tiết định nghĩa và phạm vi áp dụng của định luật Ohm trong mạch một chiều: &lt;a href=&quot;https://www.allaboutcircuits.com/textbook/direct-current/chpt-2/voltage-current-resistance-ohms-law/&quot;&gt;Voltage, current, resistance, and Ohm’s law&lt;/a&gt;. Bài hướng dẫn về LED của SparkFun bổ sung cách chọn điện trở hạn dòng thực tế: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds&quot;&gt;Light-emitting diodes&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Toàn bộ danh sách khung xe, mạch lái, vi điều khiển và cảm biến nằm ở bài &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM và cách mua bộ kit robot ở Việt Nam&lt;/a&gt;. Bài này chỉ bổ sung linh kiện cho mạch đèn báo nếu bạn chưa mua sẵn.&lt;/p&gt;

&lt;p&gt;Bộ LED 3 mm trên Hshop có giá tham khảo 20.000 ₫ (ghi nhận ngày 23 tháng 9 năm 2026): &lt;a href=&quot;https://hshop.vn/bo-5-loai-led-sieu-sang-3mm-thong-dung-5-kind-3mm-transparent-color-led&quot;&gt;bộ 5 loại LED 3 mm&lt;/a&gt;. Bạn dùng LED đỏ cho bài thực hành và nhớ kiểm tra lại giá trước khi đặt hàng.&lt;/p&gt;

&lt;p&gt;Với bộ điện trở xuyên lỗ 1/4 W (cần có trị số 180 Ω hoặc 220 Ω), do thời điểm khảo sát Hshop không hiển thị bộ 1/4 W thông dụng, bạn có thể tìm qua trang tìm kiếm: &lt;a href=&quot;https://shopee.vn/search?keyword=b%E1%BB%99%20%C4%91i%E1%BB%87n%20tr%E1%BB%9F%201%2F4W&quot;&gt;Shopee, tìm bộ điện trở 1/4W&lt;/a&gt;. Hãy chọn loại công suất 1/4 W, sai số 5% hoặc 1%, và luôn đo kiểm tra bằng đồng hồ trước khi cắm mạch thay vì chỉ tin vào nhãn túi.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Checklist lab chương 00: bàn đã sẵn sàng</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_07_ch00_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_07_ch00_lab_checklist</id>
   <content type="html">&lt;p&gt;Đây là bài kiểm tra đầu ra của Chương 00. Hãy dành 60–90 phút để rà soát toàn bộ bằng chứng bạn đã tạo từ bài 00-01 đến 00-06. Một dấu đánh dấu hoàn thành nhưng không có tệp, ảnh chụp hay dòng log kèm theo sẽ không được tính. Chương 01 sẽ bước vào đo đạc linh kiện thực tế, nên bạn cần đảm bảo mọi điều kiện an toàn và dụng cụ đều đã được kiểm chứng rõ ràng.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ &lt;strong&gt;rà soát&lt;/strong&gt; bàn lab theo một quy trình văn bản từng bước và &lt;strong&gt;gắn&lt;/strong&gt; bằng chứng cụ thể cho từng hạng mục: tổng ngân sách trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt;, tiếng bíp thông mạch của đồng hồ trên mối nối đã biết, tên cổng serial thực tế (hoặc kế hoạch kiểm tra rõ ràng nếu mạch chưa về), phát biểu Lộ trình A hoặc B, ảnh nhãn bộ sạc (hoặc ghi chú chưa mua pin), và thẻ quy trình cấp nguồn từ bài 00-03. Bạn cũng sẽ &lt;strong&gt;phân biệt&lt;/strong&gt; được những dụng cụ thiếu gây mất an toàn cho Chương 01 với những món Đợt 2 chưa cần gấp như khung xe, &lt;strong&gt;nhận diện&lt;/strong&gt; mục thiếu nào khiến bài đo điện đầu tiên trở nên rủi ro, và &lt;strong&gt;lập danh sách&lt;/strong&gt; chỉ gồm các món còn thiếu dựa trên bài 00-05 thay vì dựng thêm giỏ hàng mới.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-trước&quot;&gt;Cần có trước&lt;/h2&gt;

&lt;p&gt;Bạn cần chuẩn bị đầy đủ sổ lab, câu lộ trình, thẻ quy trình cấp nguồn, ảnh kiểm kê Đợt 1 và tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; từ bài 00-05. Nếu còn hạng mục nào bỏ dở, buổi kiểm tra sẽ ghi nhận trung thực tình trạng đó. Lúc này vẫn chưa cần robot chạy, chưa cần ROS 2 hay mối hàn nào; điều quan trọng nhất là sẵn sàng để trống những ô chưa thực sự đạt yêu cầu.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Trước mỗi chuyến bay, phi công dù có 10.000 giờ kinh nghiệm vẫn đọc từng dòng trong bảng kiểm (&lt;em&gt;pre-flight checklist&lt;/em&gt;) — bởi trí nhớ con người rất dễ bỏ sót một công tắc nhỏ khi đang hào hứng! Trong phòng lab robot cũng vậy: &lt;strong&gt;Checklist Chương 00&lt;/strong&gt; là cổng kiểm soát chất lượng cuối cùng để chắc chắn mặt bàn, nguồn điện, đồng hồ đo và thư mục làm việc của bạn đã đạt chuẩn trước khi cắm điện vào mạch thật ở Chương 01.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-tư-duy-kiểm-chứng-bằng-bằng-chứng-evidence-based-bring-up&quot;&gt;Kiến thức cần nhớ: Tư duy Kiểm chứng bằng Bằng chứng (Evidence-Based Bring-Up)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tiêu chuẩn “Đạt” phải đo được (Measurable Acceptance Criteria):&lt;/strong&gt; Trong kỹ thuật phần cứng, “đã xem qua” không phải bằng chứng; bằng chứng là một con số đo từ đồng hồ vạn năng, một ảnh chụp bàn lab gọn gàng, hoặc một dòng log biên dịch thành công.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Phân tách Miền Công suất (USB 500 mA vs. Pin Động cơ):&lt;/strong&gt; Cổng USB máy tính chỉ cung cấp an toàn tối đa \(500\text{ mA}\) ở \(5\text{ V}\) (\(2.5\text{ W}\)) — đủ cho mạch logic và cảm biến, nhưng hoàn toàn không đủ cho động cơ DC.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kỷ luật Rà soát Trước khi Nâng tầng:&lt;/strong&gt; Hoàn thành trọn vẹn một chương nhỏ giúp lỗi ở Chương 00 không bị kéo dài và phóng đại thành lỗi khó tìm ở Chương 03–06.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-kiểm-tra-ngân-sách-dòng-cổng-usb-500-ma-trước-khi-cấp-nguồn&quot;&gt;Ví dụ làm từng bước: Kiểm tra ngân sách dòng cổng USB (500 mA) trước khi cấp nguồn&lt;/h3&gt;

&lt;p&gt;Giả sử bạn đang thử nghiệm cụm logic trên bàn chỉ bằng cáp USB nối máy tính (giới hạn an toàn \(500\text{ mA}\)):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Bo mạch ESP32 (Wi-Fi tắt): &lt;strong&gt;\(80\text{ mA}\)&lt;/strong&gt; (đỉnh khởi động Wi-Fi có thể lên \(240\text{ mA}\)).&lt;/li&gt;
  &lt;li&gt;2 đèn LED báo trạng thái (\(10\text{ mA}\) mỗi đèn): &lt;strong&gt;\(20\text{ mA}\)&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Cảm biến IMU + 2 encoder quang: &lt;strong&gt;\(30\text{ mA}\)&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tổng dòng logic:&lt;/strong&gt; \(80 + 20 + 30 = \mathbf{130\text{ mA}} \le 500\text{ mA}\) → &lt;strong&gt;hoàn toàn an toàn&lt;/strong&gt; khi thử qua USB!&lt;/li&gt;
  &lt;li&gt;Giờ giả sử bạn lỡ nối cả 2 động cơ DC (dòng khởi động &lt;strong&gt;\(600\text{ mA}\)&lt;/strong&gt; mỗi chiếc = &lt;strong&gt;\(1{,}200\text{ mA}\)&lt;/strong&gt;) vào đường &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; của USB: tổng dòng vọt lên &lt;strong&gt;\(1{,}330\text{ mA}\)&lt;/strong&gt; (gấp &lt;strong&gt;2,66 lần&lt;/strong&gt; giới hạn USB!) → máy tính lập tức ngắt cổng USB hoặc làm sập nguồn ESP32!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-bước-hoàn-tất-cổng-kiểm-soát-chương-00&quot;&gt;Giải thích: 4 bước hoàn tất cổng kiểm soát Chương 00&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Xác minh mặt bàn &amp;amp; an toàn điện:&lt;/strong&gt; Không còn vụn kim loại, kính bảo hộ ở tầm tay, thẻ quy trình cấp nguồn đã viết rõ ràng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Xác minh bộ dụng cụ Đợt 1:&lt;/strong&gt; Đồng hồ vạn năng đo đúng điện áp 1 viên pin AA/18650 và kêu bíp rõ khi chập que đo.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Xác minh lộ trình &amp;amp; BOM:&lt;/strong&gt; Đã chọn dứt khoát Lộ trình A hoặc B và đối chiếu đủ linh kiện Đợt 1–2.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Xác minh Workspace:&lt;/strong&gt; Đã tạo cây thư mục chuẩn và ghi trang nhật ký đầu tiên vào sổ lab.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu đo một viên pin 18650 mới mua bằng đồng hồ vạn năng ở thang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;20V DC&lt;/code&gt; và thấy màn hình hiển thị &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-3.82 V&lt;/code&gt;&lt;/strong&gt; (có dấu trừ), điều đó có nghĩa là pin bị hỏng không? &lt;strong&gt;Không!&lt;/strong&gt; Dấu trừ chỉ báo bạn đang chạm que đỏ vào cực âm và que đen vào cực dương của pin — đảo lại que đo sẽ thấy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+3.82 V&lt;/code&gt; (điện áp lưu kho bình thường của Li-ion).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-liền-với-capstone&quot;&gt;Vì sao bài này gắn liền với Capstone&lt;/h2&gt;

&lt;p&gt;Capstone A yêu cầu dừng bánh xe khi mất lệnh quá khoảng 300 ms — hành vi này chỉ tin cậy trên một bàn làm việc mà bạn phân biệt rõ điểm mass chung với các phỏng đoán cảm tính. Tầng ROS 2 về sau cũng không thể khắc phục hậu quả của việc đo ohm trên mạch đang cấp điện ở Chương 01, hay việc nhận cell lithium mà chưa kiểm tra bộ sạc. Buổi rà soát này giúp phát hiện sớm các lỗ hổng an toàn ngay khi linh kiện còn đang trong túi.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Thứ tự trên thẻ lên nguồn là một mục buổi rà này phải thấy&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Nếu làm thất lạc thẻ quy trình, đừng tự vẽ lại từ trí nhớ giữa buổi kiểm tra rồi coi như thẻ cũ; hãy mở lại bài 00-03, viết lại cẩn thận và chụp ảnh lưu vào sổ. Thứ tự bắt buộc luôn là: mass chung, nguồn logic, tín hiệu điều khiển, và nguồn động cơ VM sau cùng khi bánh xe đã nhấc khỏi bàn.&lt;/p&gt;

&lt;h2 id=&quot;tiêu-chuẩn-nào-được-tính-là-bằng-chứng&quot;&gt;Tiêu chuẩn nào được tính là bằng chứng&lt;/h2&gt;

&lt;p&gt;Mỗi dấu tick chỉ hợp lệ khi đi kèm một bằng chứng cụ thể mà người khác có thể kiểm tra trực tiếp:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Bảng vật tư (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt;):&lt;/strong&gt; đạt khi tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; (hoặc bảng tương đương trong sổ) có con số tổng chi phí tự tính kèm ngày khảo sát giá. Ảnh chụp trang chủ một cửa hàng không được tính là bảng tổng hợp. Nếu bài 00-05 chưa xong, hạng mục này vẫn ở trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đồng hồ vạn năng:&lt;/strong&gt; đạt khi sổ ghi nhận kết quả chạm hai que đo có tiếng bíp thông mạch (hoặc ghi rõ lỗi thực tế như &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;không bíp; cần kiểm tra cầu chì hoặc pin&lt;/code&gt;). Ảnh đồng hồ trong giỏ hàng không chứng minh nó hoạt động. Nếu chưa có đồng hồ, hạng mục này vẫn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt; và các bài đo thông mạch Chương 01 phải chờ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cáp USB truyền dữ liệu:&lt;/strong&gt; đạt theo một trong hai cách: đã cắm mạch thấy tên cổng serial xuất hiện (dán vào sổ) và biến mất khi rút; hoặc nếu mạch chưa giao tới, sổ đã có kế hoạch rõ ràng: “chỉ cắm USB, không nối VM, ghi nhận tên cổng rồi rút”. Cáp chỉ làm sáng đèn nguồn trên mạch chưa đủ chứng minh khả năng truyền dữ liệu — hãy ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa chứng minh&lt;/code&gt; thay vì đánh dấu đạt.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Câu lộ trình:&lt;/strong&gt; đạt khi nêu rõ Lộ trình A hoặc B, loại vi điều khiển (ESP32 hoặc Pico 2) và cam kết không mua LiDAR trước khi đạt Capstone A. Một câu chung chung kiểu “tôi muốn học robot” không đạt yêu cầu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bộ sạc pin:&lt;/strong&gt; đạt khi có ảnh chụp đọc rõ nhãn thông số (Li-ion, 4,2 V, sạc từng cell), hoặc sổ ghi rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa mua cell&lt;/code&gt;. Có cell pin trần nhưng không có ghi chú là một rủi ro an toàn đang mở; ảnh TP4056 đang kẹp lên đế 2S là bằng chứng của một lỗi sai, không phải điều kiện đạt.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thẻ quy trình cấp nguồn:&lt;/strong&gt; đạt khi ảnh chụp hoặc bản giấy thể hiện đủ thứ tự: mass chung, nguồn logic USB, tín hiệu, nguồn VM cuối cùng, nhấc bánh khỏi bàn, quy tắc dừng PWM 300 ms, và lệnh cấm đưa nguồn động cơ hay echo 5 V vào GPIO 3,3 V. Thẻ viết sai thứ tự (đặt pin lên đầu) sẽ không đạt dù đã viết ra giấy.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Không gian làm việc:&lt;/strong&gt; đạt khi các thư mục từ bài 00-06 đã tồn tại đầy đủ và nhật ký cài đặt ghi rõ số phiên bản hoặc trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa cài&lt;/code&gt;. Một thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; trống kèm lý do trung thực là đạt; ngược lại, thư mục chứa nhiều mã nguồn tải về chưa dùng cần được dọn gọn trước khi đánh dấu hoàn thành.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;quy-trình-rà-soát-từng-bước&quot;&gt;Quy trình rà soát từng bước&lt;/h2&gt;

&lt;p&gt;Hãy thực hiện lần lượt theo đúng thứ tự dưới đây, không bỏ qua các mục bạn nghĩ mình đã làm tốt:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Mở sổ lab, tạo mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cửa ra Chương 00&lt;/code&gt; và liệt kê đủ 7 hạng mục bằng chứng trên để đánh dấu trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đạt&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Kiểm tra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt;, ghi lại tổng chi phí bằng VND và ngày khảo sát giá; nếu chưa có tệp thì đánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt;. Mở ảnh kiểm kê Đợt 1 từ bài 00-04 và đối chiếu từng món: đồng hồ, mỏ hàn, giá đỡ, thiếc hàn, kìm cắt, breadboard, dây cắm, cáp dữ liệu, vi điều khiển, LED và nút nhấn (ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;có&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thiếu&lt;/code&gt;). Khung xe và mạch lái thuộc Đợt 2 nên thiếu chúng không làm trượt Chương 00, nhưng có sẵn chúng cũng không bù đắp được việc thiếu đồng hồ.&lt;/li&gt;
  &lt;li&gt;Kiểm tra lại tiếng bíp thông mạch của đồng hồ ngay cả khi tuần trước bạn đã thử: đặt thang thông mạch trên thiết bị không cấp điện, chạm hai que đo vào nhau và ghi nhận kết quả. Nếu tiếng bíp thất bại, tuyệt đối không chuyển sang đo ohm trên cổng USB đang có điện.&lt;/li&gt;
  &lt;li&gt;Kiểm tra bằng chứng cáp USB: nếu đã có mạch trên bàn, cắm cáp (không nối pin động cơ), dán tên cổng serial vào sổ rồi rút ra. Nếu chưa có mạch, đọc lại kế hoạch đã viết để chắc chắn có điều khoản cấm nối VM ở lần cắm đầu. Thiếu cả tên cổng lẫn kế hoạch thì đánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Đọc lại câu lộ trình để xác nhận có quy tắc dừng 300 ms và chọn TB6612FNG (không phải L298N nếu bảng đã có dòng mạch lái). Nếu bảng vẫn để L298N, hãy đánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt; dòng mạch lái vì quy tắc sụt áp ở bài 00-05 chưa được áp dụng.&lt;/li&gt;
  &lt;li&gt;Kiểm tra bằng chứng bộ sạc: nhãn trên ảnh phải khớp chuẩn sạc từng cell 4,2 V, hoặc ghi chú xác nhận &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa mua cell&lt;/code&gt;. Nếu thấy TP4056 đang nối với đế 2S, hãy ngắt kết nối ngay và giữ mục này ở trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Đối chiếu thẻ quy trình cấp nguồn với hình minh họa đầu bài; nếu nguồn VM không nằm ở bước cuối cùng, hãy viết lại và chụp ảnh bản mới.&lt;/li&gt;
  &lt;li&gt;Mở thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/README.md&lt;/code&gt; để xác nhận nhật ký cài đặt khớp với chiếc máy tính trước mặt; không ghi đã cài Jazzy nếu đang dùng Windows.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Dấu hiệu buổi kiểm tra đạt chuẩn:&lt;/strong&gt; Mọi hạng mục đều được đánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đạt&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt; dựa trên bằng chứng thực tế. Với mỗi mục còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt;, bạn ghi rõ bài học cần quay lại để hoàn thiện (bài 00-04 nếu thiếu giá đỡ mỏ hàn, 00-05 nếu thiếu tổng BOM, 00-03 nếu thiếu thẻ cấp nguồn, 00-06 nếu thiếu cây thư mục).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dấu hiệu làm vội:&lt;/strong&gt; Đánh dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đạt&lt;/code&gt; cho tất cả các dòng nhưng chỉ kèm nhận xét “trông ổn”. Với bất kỳ mục nào không tìm thấy bằng chứng trong vòng một phút, hãy chuyển ngay về trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-sẵn&quot;&gt;Ví dụ làm sẵn&lt;/h2&gt;

&lt;p&gt;Ghi chú kiểm tra đầu ra của My sau khi đối chiếu thực tế như sau: tổng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; là 1.620.000 ₫ (giá ngày 23 tháng 9 năm 2026, lưu trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/&lt;/code&gt;); đồng hồ kêu bíp rõ khi chập que (đã thử lại hôm nay); cáp USB ghi nhận mạch chưa về kèm kế hoạch “chỉ cắm USB, không nối VM, ghi tên cổng rồi rút; cáp hiện có ở trạng thái chưa chứng minh cho tới khi hiện cổng”; câu lộ trình là “Lộ trình A, ESP32, TB6612FNG, không mua LiDAR trước khi đạt teleop nhấc bánh có dừng 300 ms; Lộ trình B trên máy Ubuntu phòng lab sau Chương 07”; pin ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa mua&lt;/code&gt;; thẻ cấp nguồn đã chụp ảnh đầy đủ bước VM cuối và nhấc bánh; nhật ký cài đặt ghi rõ chưa cài Arduino IDE trên laptop, chưa cài Jazzy và chỉ ghi tên Gazebo Harmonic.&lt;/p&gt;

&lt;p&gt;My chỉ đánh dấu đạt cho kế hoạch USB chứ không đánh dấu đạt cho cáp dữ liệu, giữ nguyên dòng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cáp chưa chứng minh&lt;/code&gt;. Những món còn thiếu của cô là giá đỡ mỏ hàn (40.000 ₫ theo giá ngày 23 tháng 9 năm 2026) và dây cắm. Khung xe thuộc Đợt 2 nên chưa chặn tiến độ Chương 01; thiếu giá đỡ chỉ tạm chặn phần thực hành hàn (không bật mỏ hàn), còn các bài đo và nhận diện linh kiện vẫn có thể bắt đầu bình thường.&lt;/p&gt;

&lt;p&gt;Trong khi đó, Khoa có đủ tiếng bíp đồng hồ, thẻ quy trình và câu lộ trình, nhưng ảnh chụp lại cho thấy mạch TP4056 đang kẹp qua đế hai cell pin vì “thấy vừa giắc”. Mục bộ sạc của anh bị đánh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt; vì bức ảnh là bằng chứng lỗi an toàn. Anh phải ngắt kết nối ngay, viết cam kết chỉ sạc riêng từng cell tới 4,2 V rồi mới được rà soát lại. Ngoài ra, tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; của Khoa chưa có dòng tổng, nên anh cần quay lại bài 00-05 hoàn thiện trước khi chốt Chương 00.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=52w3xeXrMU8&quot;&gt;EEVblog 1636 - Ultimate Multimeter Beginners Guide!&lt;/a&gt; — EEVblog, tiếng Anh. Ôn lại cách đo điện áp và điện trở trên đồng hồ vạn năng trước khi xác nhận bàn lab đã sẵn sàng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;p&gt;Các tình huống dưới đây yêu cầu bạn chỉ ra thiếu sót gây mất an toàn trong buổi rà soát:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Bài đo đầu tiên của Chương 01 sử dụng thang thông mạch và điện áp một chiều. Nếu mục nào trong danh sách đầu ra còn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt; sẽ khiến bài đo đó thiếu tin cậy hoặc mất an toàn, và bằng chứng nào giúp đóng mục đó?&lt;/li&gt;
  &lt;li&gt;Một sinh viên đánh dấu “cáp USB đã chứng minh” chỉ vì thấy đèn LED nguồn trên mạch sáng, mà không có tên cổng serial. Dấu tick này sai ở đâu, và sẽ gây ra rắc rối gì khi sang Chương 02?&lt;/li&gt;
  &lt;li&gt;Một bạn chưa viết thẻ quy trình cấp nguồn với lý do “Chương 01 chưa dùng động cơ nên để sau”. Hãy nêu ít nhất một hoạt động ngay trong Chương 01 (đo đạc, hàn hoặc bảo quản cell lithium đang có sẵn) vẫn chịu sự ràng buộc trực tiếp của tấm thẻ này.&lt;/li&gt;
  &lt;li&gt;Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; liệt kê L298N cùng đế pin 2S, bỏ trống dòng tổng, và câu lộ trình viết “học ROS trước”. Hãy phân tích vì sao các yếu tố này làm mất an toàn hoặc gây sai lệch cho bài thử động cơ về sau (dựa trên mức sụt áp ~2 V và quy tắc sạc pin).&lt;/li&gt;
  &lt;li&gt;Một sinh viên chưa mua pin, đã có tiếng bíp đồng hồ, đủ thẻ cấp nguồn, và giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; trống trên Windows kèm lý do rõ ràng. Bạn này đã được phép bắt đầu các bài nhận diện linh kiện không cấp nguồn của Chương 01 chưa, và vẫn bị cấm làm những gì?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Nếu mục đồng hồ vạn năng còn `mở` (chưa có tiếng bíp trên mối nối ngắn đã biết), mọi số đo &quot;0 Ω&quot; hay &quot;3,3 V&quot; về sau đều không đáng tin cậy, thậm chí dễ dẫn đến cắm nhầm lỗ đo dòng gây ngắn mạch nguồn. Cần đóng mục này bằng kết quả chạm hai que kêu bíp ghi rõ trong sổ. Thiếu khung xe không gây rủi ro này.
2. Đèn LED sáng chỉ chứng tỏ cáp có cấp nguồn 5 V. Cáp chỉ sạc sẽ không bao giờ tạo cổng serial, khiến buổi nạp firmware Chương 02 thất bại theo cách rất dễ nhầm thành lỗi hỏng mạch. Bằng chứng hợp lệ phải là tên cổng xuất hiện/biến mất theo thao tác cắm rút, hoặc một kế hoạch kiểm tra ghi rõ `chưa chứng minh`.
3. Thực hành hàn ở Chương 01 vẫn bắt buộc có giá đỡ, kính bảo hộ và thông gió; nếu đã có cell lithium trên kệ vẫn phải tuân thủ quy tắc sạc từng cell 4,2 V và không kẹp TP4056 qua đế 2S; ngoài ra việc đo nguồn USB 5 V khi quên que đỏ ở lỗ đo dòng cũng là tai nạn cùng nhóm.
4. L298N gây sụt khoảng 2 V khiến động cơ yếu và dễ nhầm sang lỗi phần mềm; đế 2S không đi kèm quy chuẩn sạc rõ ràng dễ dẫn đến kẹp TP4056 vào bộ pin 8,4 V; còn &quot;học ROS trước&quot; bỏ qua lớp timeout ngắt PWM 300 ms. Bỏ trống dòng tổng cũng cho thấy chưa kiểm soát được cấu hình đã đặt mua.
5. Được phép bắt đầu các bài nhận diện linh kiện không cấp nguồn và đo thông mạch trên mạch đã ngắt điện. Tuy nhiên, bạn này vẫn không được hàn khi chưa có giá đỡ, không được cấp nguồn động cơ, không được sạc pin lithium khi chưa kiểm tra bộ sạc, và không được lấy việc `ros_ws/` trống làm lý do trì hoãn thực hành phần cứng.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;p&gt;Bài kiểm tra đầu ra không bổ sung lý thuyết mới; chỉ mở lại các tài liệu sau cho những hạng mục đang ở trạng thái &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mở&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-use-a-multimeter&quot;&gt;Cách dùng đồng hồ&lt;/a&gt; — xem lại nếu phép thử thông mạch không kêu và cần đối chiếu vị trí núm vặn, lỗ cắm que đo.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;Mức logic&lt;/a&gt; — xem lại nếu bạn vừa phải bổ sung lệnh cấm đưa mức 5 V vào chân 3,3 V trên thẻ quy trình.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation.html&quot;&gt;Cài ROS 2 Jazzy&lt;/a&gt; — chỉ dùng để đối chiếu yêu cầu hệ điều hành Ubuntu 24.04; giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; trống trên Windows là kết quả đúng của Chương 00.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Chỉ mua bổ sung những món còn thiếu từ buổi rà soát, không mua dư. Danh sách vật tư chính vẫn nằm tại &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;bài 00-05&lt;/a&gt;. Bảng giá dụng cụ dưới đây được ghi nhận từ Hshop ngày &lt;strong&gt;23 tháng 9 năm 2026&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục còn thiếu theo buổi rà soát&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giá tham khảo&lt;/th&gt;
      &lt;th&gt;Đường dẫn&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Chưa có đồng hồ (hoặc đồng hồ không bíp cần thay mới)&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;285.000 ₫ hoặc 185.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UT33D+&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-ky-thuat-so-vom-wadfow-wdm1501-digital-multimeter-true-rms&quot;&gt;Wadfow WDM1501&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thiếu mỏ hàn&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;75.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;Wadfow 60 W&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thiếu giá đỡ mỏ hàn&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;40.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;giá đỡ&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thiếu thiếc hàn 0,8 mm&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;24.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/thiec-han-sunchi-kp26-0-8mm-sn63-pb37-solder-wire&quot;&gt;dây Sn63&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thiếu kìm cắt chân&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/kim-cat-day-dien-nho-170-cat-chan-linh-kien-dien-tu&quot;&gt;kìm 170&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thiếu breadboard&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;board 830 lỗ&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Chỉ cần chọn một mẫu đồng hồ vạn năng. Nếu thiếu dây cắm, cáp USB truyền dữ liệu hoặc kính bảo hộ, bạn có thể tìm qua các trang tìm kiếm: &lt;a href=&quot;https://shopee.vn/search?keyword=c%C3%A1p%20usb%20data&quot;&gt;Shopee, cáp USB data&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=d%C3%A2y%20jumper&quot;&gt;Lazada, dây jumper&lt;/a&gt; và &lt;a href=&quot;https://shopee.vn/search?keyword=k%C3%ADnh%20b%E1%BA%A3o%20h%E1%BB%99&quot;&gt;Shopee, kính bảo hộ&lt;/a&gt;. Ngoài ra, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; và &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; là hai địa chỉ cửa hàng linh kiện thuận tiện nếu bạn đến mua trực tiếp theo danh sách thiếu. Không cần mua thêm LiDAR, Raspberry Pi hay máy hiện sóng để hoàn thành Chương 00.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Không gian làm việc và toolchain</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_06_workspace_toolchain_preview/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_06_workspace_toolchain_preview</id>
   <content type="html">&lt;p&gt;Hãy dành khoảng 70 phút thực hiện bài học này trên chính chiếc máy tính bạn sẽ dùng để lập trình firmware. Bạn cần mở lại phát biểu lộ trình từ bài 00-02. Lúc này chưa cần có robot, chưa cần Ubuntu và cũng chưa cần biên dịch thành công một chương trình nào; mục tiêu duy nhất là dựng cây thư mục chuẩn và ghi một bản nhật ký cài đặt trung thực với tình trạng hiện tại của máy.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ &lt;strong&gt;khởi tạo&lt;/strong&gt; không gian làm việc gồm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/&lt;/code&gt;, một trong hai thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/esp32/&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/pico/&lt;/code&gt;, và một thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; trống kèm lời giải thích lý do. Bạn sẽ &lt;strong&gt;chọn&lt;/strong&gt; công cụ lập trình phù hợp cho hướng ESP32 (Arduino IDE 2.x hoặc PlatformIO, nhóm board &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;, tốc độ serial 115200, cáp USB truyền dữ liệu) hoặc hướng Pico 2 (chế độ BOOTSEL, tệp UF2 MicroPython, Thonny hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt;). Bạn sẽ &lt;strong&gt;ghi nhận&lt;/strong&gt; chính xác phiên bản phần mềm đang có (hoặc ghi thẳng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa cài&lt;/code&gt;), &lt;strong&gt;giải thích&lt;/strong&gt; vì sao ROS 2 Jazzy yêu cầu Ubuntu 24.04 và Windows Subsystem for Linux (WSL) chưa thay thế được bàn lab robot. Cuối cùng, bạn sẽ &lt;strong&gt;nhận diện&lt;/strong&gt; vai trò của Gazebo Harmonic mà chưa cần cài đặt, đồng thời &lt;strong&gt;ước lượng&lt;/strong&gt; thời gian truyền một byte serial nhỏ đến mức nào so với ngưỡng timeout teleop 300 ms.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-trước&quot;&gt;Cần có trước&lt;/h2&gt;

&lt;p&gt;Bạn chỉ cần biết tạo thư mục và cập nhật nội dung vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/lab-notes.md&lt;/code&gt;. Các bài từ 00-01 đến 00-04 đã chuẩn bị sẵn cấu trúc sổ lab, lộ trình, thẻ cấp nguồn và ảnh kiểm kê dụng cụ. Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; (hoặc ghi chú ngân sách có ngày) từ bài 00-05 nên đã hoàn thiện; nếu chưa xong, hãy ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thiếu bom.csv&lt;/code&gt; vào nhật ký thay vì tự điền giá ước đoán. Hiện tại vẫn chưa cần kỹ năng hàn.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Trong tuần đầu làm dự án robot, bạn chỉ có một file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;blink.ino&lt;/code&gt; duy nhất nên lưu ở đâu cũng thấy gọn. Nhưng đến tuần thứ tám — khi đã có firmware đọc encoder, mã PID, file cấu hình chân, sơ đồ nối dây và nhật ký thử nghiệm — một thư mục lộn xộn kiểu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;code_final_v3_fix2&lt;/code&gt; sẽ khiến bạn nạp nhầm bản cũ làm xe chạy giật lùi mà không hiểu vì sao! Thiết lập &lt;strong&gt;không gian làm việc (workspace) và chuỗi công cụ (toolchain)&lt;/strong&gt; bài bản ngay từ Chương 00 giúp mã nguồn của bạn tái lập được sau nhiều tháng.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-chuỗi-công-cụ-toolchain-biên-dịch-nhúng-hoạt-động-ra-sao&quot;&gt;Kiến thức cần nhớ: Chuỗi công cụ (Toolchain) Biên dịch Nhúng hoạt động ra sao?&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Biên dịch chéo (Cross-Compilation):&lt;/strong&gt; Máy tính của bạn dùng CPU x86_64/ARM64, còn robot dùng vi điều khiển kiến trúc Xtensa/RISC-V (ESP32) hoặc ARM Cortex-M0+ (RP2040). Toolchain (PlatformIO / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arm-none-eabi-gcc&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xtensa-esp32-elf-gcc&lt;/code&gt;) biên dịch mã C++ trên PC thành tệp nhị phân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bin&lt;/code&gt; chạy được trên chip nhỏ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Giao tiếp Nạp &amp;amp; Gỡ lỗi (USB-to-UART &amp;amp; Bootloader):&lt;/strong&gt; Khi bấm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Upload&lt;/code&gt;, PC kéo chân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BOOT&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EN&lt;/code&gt; đưa chip vào chế độ Bootloader, bơm từng gói nhị phân qua cổng nối tiếp UART ở tốc độ baud cao rồi khởi động lại MCU.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quản lý Phiên bản &amp;amp; Cây thư mục Chuẩn hóa:&lt;/strong&gt; Tách bạch rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docs/&lt;/code&gt; (sơ đồ chân, sổ lab), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cad/&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2_ws/&lt;/code&gt; giúp bạn đối chiếu ngay mã nguồn nào đã tạo ra kết quả đo nào.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-thời-gian-nạp-firmware-qua-cổng-usb-uart&quot;&gt;Ví dụ làm từng bước: Tính thời gian nạp Firmware qua cổng USB-UART&lt;/h3&gt;

&lt;p&gt;Giả sử dự án ESP32 của bạn biên dịch ra tệp nhị phân &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware.bin&lt;/code&gt; có dung lượng &lt;strong&gt;\(650\text{ KB}\)&lt;/strong&gt; (\(650 \times 1{,}024 = 665{,}600\text{ byte}\)). Giao thức UART chuẩn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8N1&lt;/code&gt; cần &lt;strong&gt;10 bit&lt;/strong&gt; cho mỗi byte (1 bit start + 8 bit dữ liệu + 1 bit stop):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Nếu nạp ở tốc độ mặc định chậm &lt;strong&gt;115.200 bps&lt;/strong&gt; (\(11{,}520\text{ byte/s}\)):
\(t_{\text{nạp}} = \frac{665{,}600}{11{,}520} \approx \mathbf{57.8\text{ giây}} \quad (\text{rất lâu mỗi lần sửa 1 dòng code!}).\)&lt;/li&gt;
  &lt;li&gt;Tăng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;upload_speed = 460800&lt;/code&gt; bps trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; (\(46{,}080\text{ byte/s}\)):
\(t_{\text{nạp}} = \frac{665{,}600}{46{,}080} \approx \mathbf{14.4\text{ giây}} \quad (\text{nhanh gấp }\mathbf{4\times}\text{!}).\)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-4-bước-dựng-workspace-bền-vững-cho-cả-khóa-học&quot;&gt;Giải thích: 4 bước dựng Workspace bền vững cho cả khóa học&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Dùng một thư mục gốc cố định không dấu, không khoảng trắng:&lt;/strong&gt; Ví dụ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robotics_lab/&lt;/code&gt; thay vì &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Đồ án Robot (mới)/&lt;/code&gt; — nhiều công cụ CMake/PlatformIO và ROS 2 bị lỗi đường dẫn khi gặp khoảng trắng hoặc ký tự tiếng Việt có dấu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Khóa phiên bản thư viện trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt;:&lt;/strong&gt; Ghi rõ phiên bản thư viện cảm biến để 3 tháng sau mở lại dự án vẫn biên dịch ra đúng tệp nhị phân ban đầu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Tách nhánh ESP32 (PlatformIO) và Pico (MicroPython) rõ ràng:&lt;/strong&gt; Mỗi bo mạch có trình nạp và giao diện serial riêng; ghi chép đúng tên cổng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COM3&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;) vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Giữ ROS 2 Jazzy &amp;amp; Gazebo ở chế độ “nhận diện trước”:&lt;/strong&gt; Chương 00 chỉ cần biết vai trò của chúng trên bản đồ tổng thể; chưa cần cài đặt nặng máy ngay từ tuần đầu.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Vì sao giao thức UART &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8N1&lt;/code&gt; cần &lt;strong&gt;10 bit&lt;/strong&gt; đường truyền để gửi &lt;strong&gt;8 bit&lt;/strong&gt; (1 byte) dữ liệu thực? Vì mỗi khung byte luôn kèm thêm 1 bit bắt đầu (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Start bit = 0&lt;/code&gt;) và 1 bit kết thúc (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stop bit = 1&lt;/code&gt;) để bên nhận đồng bộ nhịp đồng hồ mà không cần dây xung nhịp riêng!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-liền-với-capstone&quot;&gt;Vì sao bài này gắn liền với Capstone&lt;/h2&gt;

&lt;p&gt;Cơ chế timeout 300 ms của Chương 07 nằm trong firmware mà bạn có thể biên dịch lại bất cứ lúc nào trên một mạch đã nhận đúng cổng serial. Các topic ROS 2 về sau chỉ là lớp truyền thông đặt lên trên nền tảng đó. Nếu thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; trên laptop Windows chứa đầy các gói cài đặt dang dở, bạn sẽ mất thời gian sửa lỗi phần mềm trong khi chân PWM chưa từng được kiểm tra. Một thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; trống kèm lý do rõ ràng luôn hữu ích hơn một hướng dẫn cài đặt bỏ dở. Gazebo Harmonic hoàn toàn có thể chờ đến khi robot thực tế đã có hành vi dừng tin cậy để mô phỏng.&lt;/p&gt;

&lt;h2 id=&quot;cấu-trúc-thư-mục-và-quy-ước-lưu-trữ&quot;&gt;Cấu trúc thư mục và quy ước lưu trữ&lt;/h2&gt;

&lt;p&gt;Hãy đặt thư mục gốc của khóa học ở nơi dễ tìm trên máy tính và tuân thủ đúng tên các thư mục con sau:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/&lt;/code&gt;: lưu tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; và tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; (khi hoàn thành bài 00-05). Ảnh chụp thực hành có thể để cùng thư mục hoặc đặt trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/img/&lt;/code&gt;. Không dùng thư mục này để lưu các thư viện tải sẵn “phòng khi dùng”.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/esp32/&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/pico/&lt;/code&gt;: chứa mã nguồn cho vi điều khiển bạn đã chọn theo câu lộ trình. Nếu vẫn đang cân nhắc, hãy tạo thư mục khớp với lựa chọn ở bài 00-02 kèm ghi chú &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa chốt — xem lại trước khi trả tiền&lt;/code&gt;. Tránh tạo cả hai thư mục nếu bạn không thực sự dùng song song hai loại mạch.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt;: không gian làm việc cho ROS 2. Ở thời điểm hiện tại, thư mục này chỉ chứa duy nhất một tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; nêu rõ hệ điều hành máy tính cùng trạng thái “Jazzy chưa cài ở đây” (hoặc “Jazzy đã cài trên Ubuntu 24.04 này” kèm chuỗi phiên bản cụ thể). Không sao chép sẵn kho mã nguồn Nav2, driver LiDAR hay tệp bản đồ Gazebo vào đây.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cây thư mục mẫu cho người học dùng ESP32 trên Windows có dạng như sau:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;robot-course/
  notes/lab-notes.md
  notes/bom.csv
  firmware/esp32/README.md
  ros_ws/README.md
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu dùng Raspberry Pi Pico, bạn thay &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/esp32/&lt;/code&gt; bằng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/pico/&lt;/code&gt;. Cách đặt tên đơn giản và cố định giúp bạn dễ theo dõi chỉ dẫn ở các chương sau.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/pcb_mcu_anatomy.png&quot; alt=&quot;Toolchain đang nói chuyện với cái gì: USB, ổn áp, chân 3,3 V, mass&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy hình dung cấu tạo mạch theo chức năng từng khối: cổng USB truyền dữ liệu serial và cấp nguồn vào, mạch ổn áp tạo ra đường 3,3 V, các chân GPIO đảm nhiệm tín hiệu logic, và mass là điểm tham chiếu chung từ bài an toàn. Ở bước hôm nay, nhiệm vụ duy nhất của bộ công cụ (toolchain) là giao tiếp được với đường USB đó — hoặc ghi nhận rõ rằng bạn chưa có mạch trên bàn.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/ESP32_on_Lolin32_Lite_clone_board_cropped.jpg&quot; alt=&quot;Module ESP32 trên board nhỏ, não có Wi-Fi&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Raspberry_Pi_Pico.jpg&quot; alt=&quot;Raspberry Pi Pico, board MicroPython bắt đầu bằng USB&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hai bức ảnh trên đại diện cho hai lựa chọn vi điều khiển chính của khóa học. Mạch ESP32 thuận tiện nếu bạn định triển khai teleop qua Wi-Fi về sau, còn mạch dòng Pico (bao gồm Pico 2) phù hợp để bắt đầu ngay với MicroPython qua kết nối USB. Hãy chọn đúng theo mục tiêu đã viết trong lộ trình thay vì chạy theo sản phẩm đang giảm giá.&lt;/p&gt;

&lt;h2 id=&quot;nhánh-esp32-tổng-quan-công-cụ&quot;&gt;Nhánh ESP32: tổng quan công cụ&lt;/h2&gt;

&lt;p&gt;Có hai môi trường phát triển phù hợp cho ESP32. &lt;strong&gt;Arduino IDE 2.x&lt;/strong&gt; trực quan với giao diện đồ họa, trình quản lý board tích hợp và cửa sổ Serial Monitor sẵn có. &lt;strong&gt;PlatformIO&lt;/strong&gt; hoạt động trong trình soạn thảo như VS Code và cho phép cố định phiên bản thư viện trong tệp cấu hình. Bạn có thể chọn một trong hai để ghi vào nhật ký (và hoàn toàn có thể đổi sau này), nhưng không nên viết chung chung “cái nào cũng được” rồi bỏ trống nhật ký.&lt;/p&gt;

&lt;p&gt;Sau khi cài gói hỗ trợ ESP32, cấu hình phổ biến cho các mạch DevKit thông dụng thuộc nhóm &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;&lt;/strong&gt; (trong Arduino IDE thường hiển thị là “ESP32 Dev Module”, còn trong PlatformIO mã board là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;). Tên hiển thị trên menu có thể thay đổi theo từng phiên bản gói, nhưng mã nhận diện thì ổn định nên cần ghi lại. Tốc độ baud mặc định của Serial Monitor cho các chương trình đầu tiên trong khóa là &lt;strong&gt;115200&lt;/strong&gt;. Lưu ý rằng cáp USB chỉ sạc vẫn có thể làm sáng đèn nguồn trên mạch nhưng không bao giờ tạo ra cổng serial; bằng chứng duy nhất của cáp dữ liệu là một cổng serial mới xuất hiện khi cắm và biến mất khi rút.&lt;/p&gt;

&lt;p&gt;Hôm nay bạn chưa bắt buộc phải biên dịch chương trình nhấp nháy LED. Nếu IDE đã được cài đặt, hãy ghi chính xác phiên bản lấy từ mục About hoặc từ lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pio --version&lt;/code&gt;. Nếu chưa cài, hãy ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa cài Arduino IDE&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa cài PlatformIO&lt;/code&gt; kèm lý do thực tế (ví dụ: đang dùng máy phòng lab chưa có quyền cài).&lt;/p&gt;

&lt;p&gt;Trên đường truyền serial cấu hình 8N1, mỗi byte cần khoảng 10 chu kỳ bit. Ở tốc độ 115200 bit/s, thời gian truyền một byte là:&lt;/p&gt;

\[t_{\mathrm{byte}} \approx \frac{10}{115200}\,\mathrm{s} \approx 87\,\mu\mathrm{s}.\]

&lt;p&gt;Một lệnh điều khiển ngắn gồm 4 ký tự chỉ mất chưa tới nửa mili giây — nhỏ hơn hàng trăm lần so với ngưỡng timeout 300 ms của Chương 07. Nói cách khác, tốc độ baud không phải là cơ chế tự dừng khi mất tín hiệu (dead-man switch); nguyên nhân kích hoạt timeout là việc mất gói lệnh. Nắm rõ tỷ lệ này ngay từ đầu giúp bạn tránh việc “sửa” lỗi timeout bằng cách đổi tốc độ baud về sau.&lt;/p&gt;

&lt;h2 id=&quot;nhánh-pico-tổng-quan-công-cụ&quot;&gt;Nhánh Pico: tổng quan công cụ&lt;/h2&gt;

&lt;p&gt;Với dòng Raspberry Pi Pico, bạn giữ nút &lt;strong&gt;BOOTSEL&lt;/strong&gt;, cắm cáp USB truyền dữ liệu vào máy tính rồi thả nút ra để máy nhận diện mạch như một ổ đĩa USB. Sau đó, bạn sao chép tệp &lt;strong&gt;UF2 MicroPython&lt;/strong&gt; vào ổ đĩa này; mạch sẽ tự khởi động lại vào môi trường MicroPython. Để lập trình và tương tác, bạn có thể chọn &lt;strong&gt;Thonny&lt;/strong&gt; (trình soạn thảo đơn giản, tự nhận mạch) hoặc &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt;&lt;/strong&gt; (công cụ dòng lệnh trên terminal). Hãy chọn một công cụ và ghi vào nhật ký.&lt;/p&gt;

&lt;p&gt;Nếu ổ đĩa không xuất hiện, hãy kiểm tra lần lượt cáp USB, thao tác giữ nút BOOTSEL và cổng USB trên máy tính. Khi chưa có mạch, bạn chỉ cần ghi lại các bước này thành kế hoạch; không tải sẵn tệp UF2 từ các diễn đàn không rõ nguồn gốc vì rất dễ nạp nhầm phiên bản cho sai dòng chip. Hãy luôn lấy tệp từ trang tài liệu MicroPython chính thức của Raspberry Pi.&lt;/p&gt;

&lt;p&gt;Nhánh Pico không sử dụng mã &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt; và không bắt buộc đặt tốc độ 115200 cho giao diện REPL của MicroPython (dù các kết nối UART tự đấu về sau vẫn có thể dùng tốc độ này). Không trộn lẫn ghi chú của hai nhánh, và người học Pico vẫn để trống &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; với cùng lý do như nhánh ESP32.&lt;/p&gt;

&lt;h2 id=&quot;ros-2-jazzy-và-gazebo-harmonic-chỉ-ghi-nhận-tên&quot;&gt;ROS 2 Jazzy và Gazebo Harmonic: chỉ ghi nhận tên&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;ROS 2 Jazzy&lt;/strong&gt; là bản phân phối chính thức của khóa học, với hệ điều hành desktop được hỗ trợ là &lt;strong&gt;Ubuntu 24.04&lt;/strong&gt;. Nếu bạn đang dùng Windows, đừng coi Windows Subsystem for Linux (WSL) là môi trường thực hành robot; WSL có thể hỗ trợ đọc tài liệu, nhưng không phù hợp để kết nối phần cứng và quan sát chuyển động thực tế. Hãy ghi kế hoạch cài đặt là &lt;strong&gt;để sau&lt;/strong&gt; trên máy chạy Ubuntu 24.04 (máy cá nhân hoặc máy phòng lab). Đường dẫn cài đặt chính thức là &lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation.html&quot;&gt;docs.ros.org/en/jazzy/Installation.html&lt;/a&gt;; bạn chỉ nên cài đặt song song hôm nay nếu máy tính thực sự đang chạy Ubuntu 24.04 và vẫn ưu tiên thư mục firmware.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gazebo Harmonic&lt;/strong&gt; là bộ mô phỏng đồng bộ với ROS 2 Jazzy. Ở bài này bạn chỉ cần ghi tên công cụ vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.md&lt;/code&gt; chứ chưa cài đặt gói phần mềm; xây dựng một không gian mô phỏng khi chưa lắp khung xe thực tế rất dễ biến thành một dự án lạc hướng.&lt;/p&gt;

&lt;p&gt;Tương tự, &lt;strong&gt;micro-ROS&lt;/strong&gt; (gồm firmware trên ESP32/Pico và agent trên máy Ubuntu) chỉ nên triển khai sau khi thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/&lt;/code&gt; đã có chương trình biết đưa PWM về 0 an toàn. Giữ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; trống là cách nhắc nhở trực quan nhất về trình tự này.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành-tạo-cây-thư-mục-readme-và-nhật-ký-cài-đặt&quot;&gt;Thực hành: tạo cây thư mục, README và nhật ký cài đặt&lt;/h2&gt;

&lt;p&gt;Khởi tạo cây thư mục như hướng dẫn ở trên. Trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/esp32/README.md&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/pico/README.md&lt;/code&gt;, viết ngắn gọn ba thông tin: lộ trình đã chọn (A, hoặc B sau khi xong A), loại vi điều khiển, và công cụ lập trình dự kiến (Arduino IDE 2.x, PlatformIO, Thonny hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt;), kèm trạng thái đã cài hay chưa.&lt;/p&gt;

&lt;p&gt;Tạo tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/README.md&lt;/code&gt; ghi rõ hệ điều hành hiện tại của máy tính và khẳng định ROS 2 Jazzy cùng Gazebo Harmonic chưa được cài hôm nay (hoặc ghi rõ chuỗi phiên bản nếu bạn đang dùng Ubuntu 24.04 và đã cài Jazzy). Không sao chép log cài đặt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; từ các hướng dẫn mà bạn chưa thực sự chạy.&lt;/p&gt;

&lt;p&gt;Thêm mục &lt;strong&gt;Nhật ký cài đặt&lt;/strong&gt; vào sổ lab hôm nay. Mỗi dòng gồm tên công cụ, phiên bản thực tế (hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa cài&lt;/code&gt;) và ngày ghi nhận. Dưới đây là mẫu tham khảo để bạn điều chỉnh cho đúng với máy của mình:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;2026-09-23  OS: Windows 11
2026-09-23  Arduino IDE: chưa cài — sẽ dùng giờ lab thứ Năm
2026-09-23  PlatformIO: chưa cài
2026-09-23  kế hoạch serial: 115200, board id esp32dev, cáp chưa chứng minh
2026-09-23  ROS 2 Jazzy: chưa cài — máy lab Ubuntu 24.04 sau
2026-09-23  Gazebo Harmonic: chỉ gọi tên
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nếu theo nhánh Pico, hãy ghi rõ kế hoạch BOOTSEL và UF2 thay cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;. Bạn có thể dán trực tiếp nhật ký này vào trường đoạn serial trong sổ lab khi chưa có mạch để tránh để trống trường; nếu mạch đã nhận cổng serial, hãy dán tên cổng lên phía trên nhật ký.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kết quả cần đạt:&lt;/strong&gt; Đủ ba thư mục theo đúng quy ước; tệp README trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/&lt;/code&gt; nêu rõ lộ trình và vi điều khiển; tệp README trong &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; phản ánh trung thực hệ điều hành; nhật ký có ngày cụ thể và ghi thẳng &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa cài&lt;/code&gt; ở các mục chưa thực hiện.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cách xử lý khi lệch chuẩn:&lt;/strong&gt; Nếu bạn đã tạo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/src&lt;/code&gt; và tải sẵn nhiều kho mã nguồn theo video hướng dẫn, hãy xóa bớt để giữ lại duy nhất tệp README cho tới Chương 09. Nếu bạn đang ghi “WSL Ubuntu” như môi trường thực hành chính, hãy sửa lại vì WSL không thay thế được máy Ubuntu thực tế. Nếu đặt tốc độ baud 9600 theo thói quen Arduino cũ, hãy chuyển về 115200 cho các bài ESP32 của khóa. Nếu đã tải tệp UF2 từ nguồn không rõ ràng, hãy xóa và chuyển sang dùng đường dẫn tài liệu MicroPython chính thức của Raspberry Pi.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-sẵn&quot;&gt;Ví dụ làm sẵn&lt;/h2&gt;

&lt;p&gt;Trang dùng máy Windows, chọn Lộ trình A với ESP32 và đã chốt ngân sách ở bài 00-02. Cô tạo hai thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/esp32/&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt;. Cô chưa cài IDE trên laptop cá nhân vì máy phòng lab ở trường đã cài sẵn Arduino IDE 2.3 và cô muốn dùng chung một môi trường trước; nhật ký ghi rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Arduino IDE: chưa cài trên laptop cá nhân; sẽ kiểm tra phiên bản trên máy lab trường vào thứ Năm&lt;/code&gt;. Tệp README trong thư mục firmware ghi: “Lộ trình A, ESP32, mở rộng teleop Wi-Fi sau, công cụ Arduino IDE 2.x, nhóm board esp32dev, serial 115200”. Cô tính thời gian truyền một byte khoảng 87 µs và ghi nhận: “ngưỡng timeout 300 ms lớn gấp hơn 3000 lần thời gian truyền một byte, nên tốc độ baud không phải lớp bảo vệ an toàn”. Tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/README.md&lt;/code&gt; nêu rõ: “Máy chạy Windows 11. ROS 2 Jazzy chờ máy lab Ubuntu 24.04. Chưa cài Gazebo Harmonic. Không dùng WSL làm môi trường lab robot”.&lt;/p&gt;

&lt;p&gt;Một bạn khác dùng Ubuntu 24.04 có thể cài Jazzy ở buổi riêng và ghi chuỗi phiên bản thực tế vào sổ, nhưng vẫn phải chuẩn bị đầy đủ thư mục &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/&lt;/code&gt; tương tự. Trang không bao giờ sao chép số phiên bản từ máy khác vào nhật ký của mình.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=3ATDCi4ApY4&quot;&gt;Get started with PlatformIO and the ESP 32&lt;/a&gt; — John Green Dev, tiếng Anh. Giới thiệu nhanh quy trình mở dự án và nạp chương trình ESP32 bằng PlatformIO — bộ công cụ sẽ cài đặt ở Chương 02.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=W6mixXsn-Vc&quot;&gt;Breadboard tutorial: How to use a breadboard (for beginners)&lt;/a&gt; — LeftyMaker, tiếng Anh. Giải thích cấu tạo hàng lỗ thông nhau trên breadboard phục vụ các bài thực hành đầu tiên.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Nếu lộ trình của bạn chọn Pico 2 và MicroPython, bạn cần tạo thư mục firmware nào, thao tác với nút BOOTSEL thực hiện ra sao trước khi chép tệp, và hai công cụ nào hợp lệ để tương tác với REPL?&lt;/li&gt;
  &lt;li&gt;Khi cắm cáp USB, đèn LED trên mạch phát sáng nhưng máy tính không hiện cổng serial nào. Bạn cần nghi ngờ bộ phận nào trước tiên, và dấu hiệu nào chứng minh cáp đó truyền được dữ liệu?&lt;/li&gt;
  &lt;li&gt;Bạn đang dùng Windows và thấy một bài viết hướng dẫn cài ROS 2 Jazzy trên WSL ngay tối nay. Bạn nên viết gì vào &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/README.md&lt;/code&gt;, và hệ điều hành nào thực sự được khuyến nghị?&lt;/li&gt;
  &lt;li&gt;Ở tốc độ 115200 bit/s (cấu hình 8N1), hãy ước lượng thời gian truyền 1 byte và một lệnh gồm 4 byte. Khoảng thời gian này có chiếm tỷ lệ đáng kể so với ngưỡng 300 ms không?&lt;/li&gt;
  &lt;li&gt;Một dòng nhật ký ghi “PlatformIO latest” mà không có số phiên bản hay ngày tháng. Bạn cần bổ sung thông tin gì để dòng này trở thành bằng chứng hợp lệ?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Tạo thư mục `firmware/pico/`. Giữ nút BOOTSEL trong lúc cắm cáp USB truyền dữ liệu để máy tính nhận diện ổ đĩa rồi chép tệp UF2 MicroPython vào đó. Sau khi mạch khởi động lại, dùng Thonny hoặc `mpremote` để giao tiếp với REPL.
2. Nghi ngờ cáp USB chỉ có chức năng sạc. Cáp truyền dữ liệu chỉ được chứng minh khi cổng serial (hoặc ổ đĩa BOOTSEL) xuất hiện lúc cắm và biến mất lúc rút; đèn nguồn sáng chỉ chứng tỏ có điện áp 5 V.
3. Ghi rõ trong `ros_ws/README.md` rằng ROS 2 Jazzy chưa được cài đặt, WSL không thay thế được môi trường thực hành robot, và việc cài đặt sẽ thực hiện sau trên Ubuntu 24.04 (hệ điều hành desktop được khóa học hỗ trợ).
4. Một byte mất khoảng $10/115200 \approx 87\,\mu\mathrm{s}$, nên 4 byte mất khoảng $0{,}35\,\mathrm{ms}$ — chỉ xấp xỉ một phần nghìn của ngưỡng 300 ms. Điều này cho thấy timeout liên quan đến việc mất lệnh chứ không phải tốc độ baud.
5. Bổ sung ngày kiểm tra và chuỗi phiên bản cụ thể lấy từ lệnh `pio --version` hoặc hộp thoại About của IDE (hoặc ghi thẳng `chưa cài` nếu chưa cài đặt). Chữ &quot;latest&quot; không mang giá trị truy vết.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.platformio.org/&quot;&gt;Tài liệu PlatformIO&lt;/a&gt; — hướng dẫn lập trình ESP32 trên trình soạn thảo, bao gồm cấu hình mã board &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.arduino.cc/&quot;&gt;Tài liệu Arduino&lt;/a&gt; — tài liệu chính thức về Arduino IDE 2.x và công cụ Serial Monitor.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/esp-idf/en/latest/esp32/&quot;&gt;ESP-IDF cho ESP32&lt;/a&gt; — bộ framework gốc của Espressif; chưa bắt buộc cho Capstone A nhưng hữu ích để tra cứu khi gặp sai khác ở gói hỗ trợ board.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/micropython.html&quot;&gt;MicroPython cho Pico&lt;/a&gt; — hướng dẫn chính thức về BOOTSEL và tệp UF2 cho dòng Raspberry Pi Pico.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials.html&quot;&gt;Tutorial ROS 2 Jazzy&lt;/a&gt; — có thể xem lướt để hình dung cấu trúc bài học sau khi cài trên Ubuntu 24.04.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/&quot;&gt;Gazebo Harmonic&lt;/a&gt; — tài liệu giới thiệu bộ mô phỏng; chưa cần cài đặt từ bài này.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>BOM và cách mua bộ kit robot ở Việt Nam</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_05_bom_and_shopping/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_05_bom_and_shopping</id>
   <content type="html">&lt;p&gt;Thời lượng dự kiến là &lt;strong&gt;90–120 phút&lt;/strong&gt;, bao gồm cả phần thực hành lập bảng tính. Các mức giá trong bài được ghi nhận từ trang sản phẩm công khai của Hshop vào ngày &lt;strong&gt;23 tháng 9 năm 2026&lt;/strong&gt;. Do giá linh kiện thay đổi theo thời điểm, hãy xem các con số này là mốc tham khảo và kiểm tra lại trang bán hàng trước khi thanh toán.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu&quot;&gt;Mục tiêu&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ lập được bảng vật tư (BOM) cho xe vi sai theo Lộ trình A (Track A) phục vụ Capstone, biết chọn mạch ESP32 hoặc Pico theo thông số kỹ thuật thay vì theo chương trình giảm giá, giải thích được lý do không dùng L298N cho động cơ TT 3–6 V, ghép đúng bộ sạc cho pin lithium, và hoàn thiện tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; với đầy đủ nơi mua, từ khóa tìm kiếm tiếng Việt, phương án thay thế và cột theo dõi tình trạng nhận hàng.&lt;/p&gt;

&lt;h2 id=&quot;cần-gì-trước-khi-học&quot;&gt;Cần gì trước khi học&lt;/h2&gt;

&lt;p&gt;Bạn chỉ cần chuẩn bị sẵn công cụ ghi chép và quyết định chọn Lộ trình A hay Lộ trình B từ bài 00-02 để biết có cần Wi-Fi trên vi điều khiển ngay từ đầu hay không. Hiện tại chưa cần thuộc định luật Ohm; phần mạch lái và pin trong bài cung cấp đúng hai thông số quan trọng nhất để bạn không mua nhầm. Khi chưa có đồng hồ vạn năng, điện thoại để quay chụp kiện hàng cũng là một công cụ kiểm tra hữu ích.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Đi mua linh kiện robot lần đầu mà không có bảng vật tư (&lt;strong&gt;BOM — Bill of Materials&lt;/strong&gt;) rất giống việc đi siêu thị khi đang đói mà không có danh sách: bạn mang về một túi đầy bo mạch và cảm biến lẻ tẻ, nhưng khi ngồi lắp mới phát hiện mạch lái động cơ làm tụt mất 2 volt của pin, còn đầu nối pin lại không khớp với công tắc! Đọc hiểu BOM bằng con mắt kỹ sư giúp bạn chọn đúng từng cụm linh kiện tương thích điện áp, dòng điện và cơ khí ngay từ đầu.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-tư-duy-hệ-thống-đằng-sau-một-bảng-bom-robot&quot;&gt;Kiến thức cần nhớ: Tư duy Hệ thống đằng sau một Bảng BOM Robot&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Chuỗi Năng lượng (Pin → Cầu chì → Công tắc → Buck 5 V → Vi điều khiển &amp;amp; Driver):&lt;/strong&gt; Mọi linh kiện trên xe phải cùng nằm trong một “ngân sách điện áp và dòng điện” khép kín — điện áp pin phải lớn hơn điện áp định mức động cơ cộng sụt áp driver, nhưng nhỏ hơn giới hạn tối đa của mạch hạ áp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hiệu suất Mạch lái Động cơ (BJT L298N vs. MOSFET TB6612FNG):&lt;/strong&gt; Không phải cầu H nào cũng giống nhau! Mạch dùng transistor lưỡng cực cũ (L298N) sụt áp tới \(2\text{ V}\) và tỏa nhiệt dữ dội, trong khi mạch dùng MOSFET hiện đại (TB6612FNG / DRV8833) có điện trở mở \(R_{\text{DS(on)}}\) cực nhỏ, giữ pin bền hơn và xe khỏe hơn hẳn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quản lý Rủi ro Mua sắm theo Đợt:&lt;/strong&gt; Tách BOM thành các đợt tương ứng với từng chương học giúp bạn kiểm chứng từng khối hoạt động trước khi chi tiền nâng cấp lên SBC và LiDAR.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-so-sánh-tổn-hao-nhiệt-l298n-vs-tb6612fng-và-tính-thời-lượng-pin-2s&quot;&gt;Ví dụ làm từng bước: So sánh tổn hao nhiệt L298N vs. TB6612FNG và Tính thời lượng pin 2S&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Phần A — Tổn hao công suất trên mạch lái khi kéo dòng \(I = 1.0\text{ A}\) (2 động cơ chạy cùng lúc):&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;L298N (cầu H BJT, sụt áp \(V_{\text{drop}} \approx 2.0\text{ V}\)):&lt;/strong&gt;
\(P_{\text{nhiệt, L298N}} = V_{\text{drop}} \times I = 2.0\text{ V} \times 1.0\text{ A} = \mathbf{2.0\text{ W}}!\)
Nếu pin 2S chỉ có \(7.4\text{ V}\), động cơ chỉ còn nhận được \(7.4 - 2.0 = \mathbf{5.4\text{ V}}\) (mất tới 27% điện áp thành nhiệt đốt tản nhiệt!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;TB6612FNG (cầu H MOSFET, tổng \(R_{\text{on}} \approx 0.5\ \Omega\)):&lt;/strong&gt;
\(P_{\text{nhiệt, TB6612}} = I^2 \cdot R_{\text{on}} = (1.0)^2 \times 0.5 = \mathbf{0.5\text{ W}} \quad (\text{mát hơn }\mathbf{4\times}\text{!})\)
Điện áp sụt chỉ \(1.0 \times 0.5 = 0.5\text{ V}\), động cơ nhận đủ \(6.9\text{ V}\) mạnh mẽ hơn hẳn!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Phần B — Thời lượng chạy của bộ pin 2S 18650 (2.200 mAh, 7.4 V):&lt;/strong&gt; năng lượng định mức \(E = 7.4 \times 2.2 = \mathbf{16.28\text{ Wh}}\). Với dòng xả trung bình toàn xe \(0.8\text{ A}\) và giữ lại \(15\%\) dung lượng chống xả cạn:&lt;/p&gt;

\[t_{\text{chạy}} = \frac{2.2\text{ Ah} \times 0.85}{0.8\text{ A}} \approx \mathbf{2.34\text{ giờ}}.\]

&lt;h3 id=&quot;giải-thích-4-bước-rà-soát-bảng-bom-trước-khi-nhấn-nút-đặt-hàng&quot;&gt;Giải thích: 4 bước rà soát bảng BOM trước khi nhấn nút đặt hàng&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Khớp mức logic giữa MCU và ngoại vi:&lt;/strong&gt; ESP32/Pico dùng logic &lt;strong&gt;3,3 V&lt;/strong&gt;; nếu mua cảm biến xuất 5 V (như HC-SR04 bản cũ), bắt buộc phải có sẵn điện trở chia áp hoặc mạch chuyển mức trong BOM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Kiểm tra dòng hãm trục (stall current) của động cơ:&lt;/strong&gt; Driver động cơ và cầu chì phải chịu được dòng khởi động/kẹt bánh, không chỉ dòng không tải.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Mua dư linh kiện tiêu hao giá rẻ:&lt;/strong&gt; Điện trở, LED, tụ 100 µF, dây jumper và đầu nối nên mua dư 20–30% để không bị đứt mạch thực hành chỉ vì cháy 1 linh kiện giá 1.000 đồng.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Đồng bộ chuẩn giắc cắm (JST / XT30 / DC barrel):&lt;/strong&gt; Tránh cảnh pin một kiểu giắc, sạc một kiểu giắc và mạch công tắc lại là dây trần phải xoắn tay!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu bạn dùng động cơ vàng TT (điện áp định mức 6 V) với pin 2S (7.4 V) qua TB6612FNG (sụt áp 0.5 V), điện áp tối đa đặt lên động cơ là \(6.9\text{ V}\). Làm sao tránh quá áp? Giới hạn hệ số xung PWM tối đa ở khoảng \(6.0 / 6.9 \approx \mathbf{87\%}\) trong firmware!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-quyết-định-capstone&quot;&gt;Vì sao bài này quyết định Capstone&lt;/h2&gt;

&lt;p&gt;Bài teleop ở Chương 07 thường hỏng bởi những nguyên nhân phần cứng rất dễ tránh: mạch lái gây sụt khoảng 2 V, cổng USB phải gánh dòng kéo bánh xe, chân echo 5 V cắm thẳng vào chân GPIO 3,3 V, hoặc mạch sạc một cell 4,2 V bị kẹp lên bộ pin 8,4 V. ROS 2 không thể khắc phục các lỗi phần cứng này. Bảng BOM giúp đảm bảo phần cứng khớp hoàn toàn với firmware; mua đúng thông số là điều kiện đầu tiên để robot chạy ổn định.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/kit_catalog_overview.png&quot; alt=&quot;Các nhóm đồ trong kit&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Thứ tự nguồn: mass chung, logic, tín hiệu, nguồn motor bật sau cùng, bánh không chạm bàn&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;bạn-đang-mua-những-gì&quot;&gt;Bạn đang mua những gì&lt;/h2&gt;

&lt;p&gt;BOM (Bill of Materials) là bảng liệt kê mọi linh kiện và dụng cụ cần có trên bàn làm việc — kể cả những món bạn đã sở hữu sẵn như tua-vít — chứ không phải ảnh chụp giỏ hàng. Mỗi dòng trong bảng phải trả lời rõ: đó là gì, số lượng bao nhiêu, thông số kỹ thuật tối thiểu chấp nhận được, vai trò trên robot, phương án thay thế và nơi mua. Nếu bỏ trống cột thông số, bạn sẽ không phân biệt được linh kiện thay thế đúng chuẩn với hàng giá rẻ có ngoại hình tương tự.&lt;/p&gt;

&lt;p&gt;Robot mặc định của khóa học là &lt;strong&gt;xe dẫn động vi sai&lt;/strong&gt;: gồm hai bánh chủ động, một bánh đa hướng, vi điều khiển logic 3,3 V, mạch lái động cơ MOSFET, một cảm biến khoảng cách, và nguồn pin động cơ cách ly hoàn toàn khỏi đường 3,3 V. Các cảm biến như IMU, encoder bánh xe hay ToF là phần bổ sung sau khi xe đã chạy theo lệnh. Lộ trình B sẽ thêm máy tính cài Ubuntu 24.04 cho ROS 2 Jazzy. Bạn không nên mua LiDAR ngay tuần đầu chỉ để tạo cảm giác sẵn sàng; LiDAR quét phẳng (LD06 hoặc RPLIDAR A1), Raspberry Pi 5 hoặc Jetson và bộ pin có mạch BMS được xếp ở Đợt 4 bên dưới, đúng thời điểm các chương sau cần dùng.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;USB 5 V ---- logic ESP32 / Pico (GPIO 3,3 V)
                 |
                GND -------- mass chung với driver
                 |
2S 18650 ~7,4 V ---- VM của TB6612 ---- hai động cơ TT
                 |
                 +-- không nối vào chân 3,3 V
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_rails_3v3.png&quot; alt=&quot;Đường 3,3 V, USB 5 V, và đường nguồn motor riêng&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;danh-sách-lộ-trình-a-mặc-định&quot;&gt;Danh sách Lộ trình A mặc định&lt;/h2&gt;

&lt;p&gt;Danh sách được chia thành ba đợt để ngay cả khi khung xe giao chậm, bạn vẫn có thể bắt đầu Chương 01 đúng kế hoạch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Đợt 1 — bàn lab (để học Chương 01 ngay tuần này):&lt;/strong&gt; đồng hồ vạn năng, mỏ hàn khoảng 60 W kèm giá đỡ, thiếc hàn, kìm cắt, breadboard không hàn, dây cắm jumper, cáp USB truyền dữ liệu, mạch vi điều khiển, vài đèn LED và một nút nhấn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Đợt 2 — chuyển động (chuẩn bị trước Chương 05):&lt;/strong&gt; khung xe (hoặc động cơ TT rời kèm gá bắt và bánh đa hướng), mạch lái TB6612FNG (hoặc phương án nhỏ hơn là DRV8833), cảm biến siêu âm HC-SR04 và mạch chuyển mức logic 3,3 V / 5 V cho chân echo.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Đợt 3 — năng lượng (sau khi bạn đã biết đo điện áp):&lt;/strong&gt; hai cell pin 18650 có dung lượng thực tế, đế giữ pin và bộ sạc nạp &lt;strong&gt;từng cell riêng lẻ&lt;/strong&gt; lên 4,2 V. Lưu ý rằng đế pin nối tiếp gắn trên xe không phải là bộ sạc.&lt;/p&gt;

&lt;p&gt;Chưa cần đưa vào đơn hàng đầu tiên: RPLIDAR, Raspberry Pi 5, máy hiện sóng, nguồn 12 V 10 A, và các “combo xe robot” đóng gói sẵn mạch L298N cùng hộp 4 pin AA không rõ thông số.&lt;/p&gt;

&lt;h3 id=&quot;bảng-tổng-hợp-bộ-kit&quot;&gt;Bảng tổng hợp bộ kit&lt;/h3&gt;

&lt;p&gt;Các thông số dưới đây tổng hợp từ datasheet phổ biến và trang sản phẩm Hshop dẫn ở mục mua hàng. Khi nhận linh kiện, hãy luôn kiểm tra trực tiếp ký hiệu in trên thân IC.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;SL&lt;/th&gt;
      &lt;th&gt;Thông số chấp nhận&lt;/th&gt;
      &lt;th&gt;Vai trò trên Capstone&lt;/th&gt;
      &lt;th&gt;Gợi ý nên mua&lt;/th&gt;
      &lt;th&gt;Phương án thay thế&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Vi điều khiển&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;GPIO 3,3 V, có chip USB-UART, từ 20 chân khả dụng&lt;/td&gt;
      &lt;td&gt;Chạy firmware, về sau làm teleop Wi-Fi&lt;/td&gt;
      &lt;td&gt;Mạch phát triển ESP32 (~30 chân), chip CH340 hoặc CP2102&lt;/td&gt;
      &lt;td&gt;Pico 2 nếu ưu tiên teleop qua USB trước, Wi-Fi để sau&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung xe&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;Hai bánh chủ động, có sàn lắp mạch&lt;/td&gt;
      &lt;td&gt;Thân xe vi sai&lt;/td&gt;
      &lt;td&gt;Kit 2WD mica động cơ TT, hoặc kit 4WD của Hshop (chỉ dùng 2 bánh chủ động kèm bánh đa hướng)&lt;/td&gt;
      &lt;td&gt;Động cơ TT rời, 2 bánh xe, 2 gá bắt, 1 bánh đa hướng và 1 tấm đế cứng&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Động cơ TT&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2 bánh dẫn&lt;/td&gt;
      &lt;td&gt;Điện áp 3–9 V, tỉ số truyền khoảng 1:48&lt;/td&gt;
      &lt;td&gt;Dẫn động bánh trái và phải&lt;/td&gt;
      &lt;td&gt;Động cơ đi kèm khung xe&lt;/td&gt;
      &lt;td&gt;Động cơ TT 1:48 của Hshop (giá tham khảo 20.000 ₫/cái)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạch lái (Driver)&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;Cầu H MOSFET, VM khoảng 5–10 V, mỗi kênh ≥ 1 A liên tục&lt;/td&gt;
      &lt;td&gt;Điều khiển chiều quay và tốc độ PWM&lt;/td&gt;
      &lt;td&gt;Module TB6612FNG (VM 4,5–10 V, 1,2 A liên tục / 3,2 A đỉnh)&lt;/td&gt;
      &lt;td&gt;DRV8833 (VM 2,7–10,8 V, tối đa 1,5 A/kênh; dễ nóng khi kẹt bánh)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cảm biến khoảng cách&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;HC-SR04 hoặc cảm biến ToF nhỏ&lt;/td&gt;
      &lt;td&gt;Đo khoảng cách vật cản&lt;/td&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td&gt;Module VL53L0X nếu muốn dùng giao tiếp I²C và mức 3,3 V ngay từ đầu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chuyển mức logic&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;4 kênh 3,3 V ↔ 5 V&lt;/td&gt;
      &lt;td&gt;Hạ mức chân echo HC-SR04 về ESP32&lt;/td&gt;
      &lt;td&gt;Module chuyển mức MOSFET&lt;/td&gt;
      &lt;td&gt;Cầu chia áp chỉ cho chân echo (lưu ý trig 3,3 V có thể thiếu ổn định với một số module kém)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin động cơ&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2 cell&lt;/td&gt;
      &lt;td&gt;Li-ion 18650, dung lượng công bố ≤ 3500 mAh&lt;/td&gt;
      &lt;td&gt;Cấp nguồn VM qua mạch lái&lt;/td&gt;
      &lt;td&gt;Cell rời, tháo ra sạc &lt;strong&gt;bên ngoài&lt;/strong&gt; xe&lt;/td&gt;
      &lt;td&gt;6 pin AA (~9 V) nếu chưa muốn dùng lithium; không dùng pin vuông 9 V&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ sạc&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;4,2 V mỗi cell, tự ngắt khi đầy&lt;/td&gt;
      &lt;td&gt;Sạc riêng từng cell&lt;/td&gt;
      &lt;td&gt;Bộ sạc khay 4 ngăn 18650&lt;/td&gt;
      &lt;td&gt;TP4056 &lt;strong&gt;chỉ&lt;/strong&gt; dùng cho 1 cell đơn, tuyệt đối không kẹp lên đế 2S&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ vạn năng&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;Đo V DC, Ω, có còi thông mạch&lt;/td&gt;
      &lt;td&gt;Kiểm tra dây và nguồn&lt;/td&gt;
      &lt;td&gt;UNI-T UT33D+ hoặc mẫu phổ thông tương đương&lt;/td&gt;
      &lt;td&gt;Đồng hồ bất kỳ có còi thông mạch; thang đo dòng phải có cầu chì&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mỏ hàn&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;~60 W, có giá đỡ, thiếc 0,8 mm&lt;/td&gt;
      &lt;td&gt;Hàn dây động cơ và hàng chân header&lt;/td&gt;
      &lt;td&gt;Mỏ hàn 60 W thông thường là đủ&lt;/td&gt;
      &lt;td&gt;Trạm hàn chỉnh nhiệt để nâng cấp về sau&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạch thử&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;Breadboard 830 lỗ, dây cắm đực-đực và đực-cái&lt;/td&gt;
      &lt;td&gt;Thực hành Chương 01–03&lt;/td&gt;
      &lt;td&gt;—&lt;/td&gt;
      &lt;td&gt;Breadboard 400 lỗ nếu bố trí dây ngắn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ốc vít&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1 bộ&lt;/td&gt;
      &lt;td&gt;Vít M3, trụ đồng/nhựa cách điện&lt;/td&gt;
      &lt;td&gt;Cố định mạch cách ly khỏi bề mặt dẫn điện&lt;/td&gt;
      &lt;td&gt;Thường có sẵn trong bộ khung&lt;/td&gt;
      &lt;td&gt;Bộ ốc M3 mua tại cửa hàng cơ khí&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;vì-sao-mạch-lái-không-phải-cầu-h-nào-cũng-như-nhau&quot;&gt;Vì sao mạch lái không phải “cầu H nào cũng như nhau”&lt;/h2&gt;

&lt;p&gt;Trên Hshop tại thời điểm khảo sát, cả module L298N và TB6612FNG đều có giá &lt;strong&gt;45.000 ₫&lt;/strong&gt; (mã SKU HS0695V và HS2458V), nên sự khác biệt không nằm ở giá bán. L298N sử dụng cầu Darlington nên khi có dòng động cơ chạy qua, nó làm sụt khoảng 1,8 V đến 2,5 V giữa pin và động cơ. Ngược lại, TB6612FNG dùng cầu MOSFET nên gần như truyền trọn vẹn điện áp nguồn tới động cơ:&lt;/p&gt;

\[V_{motor} \approx V_{pin} - V_{roi}\]

&lt;p&gt;Với bộ pin hai cell có điện áp danh định 7,4 V (ước tính sụt 2,0 V trên Darlington và 0,3 V trên MOSFET):&lt;/p&gt;

\[V_{L298} \approx 7{,}4 - 2{,}0 = 5{,}4\,\text{V}\]

\[V_{TB6612} \approx 7{,}4 - 0{,}3 = 7{,}1\,\text{V}\]

&lt;p&gt;Theo trang sản phẩm động cơ TT tỉ số 1:48 trên Hshop, điện áp khuyến nghị là &lt;strong&gt;3–9 V&lt;/strong&gt;, dòng không tải &lt;strong&gt;110–150 mA&lt;/strong&gt;, tốc độ khoảng &lt;strong&gt;200–208 vòng/phút ở 5–6 V&lt;/strong&gt;, và mô-men &lt;strong&gt;0,8 kg·cm ở 6 V&lt;/strong&gt;. Như vậy, mức 5 V vốn đã nằm ở vùng chạy chậm và yếu. Nếu dùng 4 pin AA mới (~6 V) đi qua L298N, điện áp đến động cơ chỉ còn gần 4 V khiến xe di chuyển rất yếu. Ngoài ra, module L298N tích hợp IC ổn áp 7805, nhưng bạn không nên lấy 5 V từ mạch này để nuôi ESP32 khi nguồn động cơ vượt 12 V — và ngay cả khi bộ pin dưới 10 V, vẫn không nên nuôi vi điều khiển từ chân 5 V của mạch lái. Trong khi đó, chân logic của TB6612FNG hoạt động trong dải &lt;strong&gt;2,7–5,5 V&lt;/strong&gt;, tương thích trực tiếp với mức cao 3,3 V của GPIO. Lưu ý rằng chân STBY phải được kéo lên mức cao để đánh thức cầu H.&lt;/p&gt;

&lt;p&gt;Module DRV8833 có giá &lt;strong&gt;25.000 ₫&lt;/strong&gt;, dải VM &lt;strong&gt;2,7–10,8 V&lt;/strong&gt; và dòng tối đa &lt;strong&gt;1,5 A mỗi kênh&lt;/strong&gt; theo thông số trên Hshop. Mạch này dùng thay thế tốt khi mỗi kênh kéo một động cơ TT, nhưng do diện tích đồng tản nhiệt và vỏ IC nhỏ hơn TB6612 nên sẽ nhanh nóng nếu bánh bị kẹt lâu. Không mắc song song hai động cơ trên cùng một kênh DRV8833.&lt;/p&gt;

&lt;p&gt;Cần phân biệt rõ dòng kẹt trục với dòng không tải. Trang bán hàng không công bố dòng kẹt, vì vậy bạn cần dự phòng dòng gấp vài lần mức 150 mA và tự &lt;strong&gt;đo kiểm&lt;/strong&gt; về sau (nhấc bánh khỏi bàn, mắc đồng hồ nối tiếp và cấp một xung ngắn). Chọn mạch lái chỉ dựa vào nhãn quảng cáo “2 A” là chưa đủ căn cứ.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;Ý tưởng cầu H: vi điều khiển chỉ chọn chiều dòng, không phải nguồn cấp dòng cho motor&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;pin-đế-giữ-pin-và-bộ-sạc-là-một-tổ-hợp-thống-nhất&quot;&gt;Pin, đế giữ pin và bộ sạc là một tổ hợp thống nhất&lt;/h2&gt;

&lt;p&gt;Một cell 18650 có điện áp danh định khoảng 3,6–3,7 V và &lt;strong&gt;đầy ở 4,2 V&lt;/strong&gt;. Hai cell lắp trong đế “2×18650 nối tiếp” tạo ra &lt;strong&gt;7,4 V danh định và 8,4 V khi đầy&lt;/strong&gt;. Điện áp này chỉ nối vào chân VM của mạch lái, tuyệt đối không đưa vào GPIO hay vào mạch sạc TP4056.&lt;/p&gt;

&lt;p&gt;Các mạch TP4056 trên Hshop (bản USB-C giá 10.000 ₫ và bản mini-USB giá 2.000 ₫) là mạch sạc &lt;strong&gt;một cell&lt;/strong&gt;, hoạt động bằng cách nạp một cell lên 4,2 V ở dòng khoảng 1 A. Kẹp mạch này lên hai đầu đế pin 2S sẽ ép mạch 4,2 V làm việc với cụm 8,4 V hoặc gây lệch áp giữa hai cell — cả hai đều sai và mất an toàn. Cách đúng là sạc riêng từng cell trong bộ sạc có khay, đo điện áp hở mạch nằm trong khoảng 3,6 V đến 4,20 V rồi mới lắp vào đế trên xe. Bộ sạc 4 khay cổng USB trên Hshop có giá &lt;strong&gt;68.000 ₫&lt;/strong&gt; (hỗ trợ 14500 / 18650 / 21700); bạn nên thử nghiệm với một khay trước cho đến khi kiểm chứng được mạch ngắt ổn định.&lt;/p&gt;

&lt;p&gt;Dung lượng pin có giới hạn vật lý: cell 18650 thật hiếm khi vượt quá 3500 mAh, phổ biến ở mức 2000–3000 mAh. Hshop niêm yết cell Sunpower &lt;strong&gt;2000 mAh giá 35.000 ₫&lt;/strong&gt; và &lt;strong&gt;2500 mAh giá 45.000 ₫&lt;/strong&gt;. Những bài đăng rao bán cell 9900 mAh hay 12000 mAh ở tầm giá tương đương đều là thông số giả. Ký hiệu “10C” trên tên pin Sunpower là thông số dòng xả công bố, không phải dung lượng; không nên tin tuyệt đối vào hệ số C khi không có datasheet, và hai động cơ TT cũng không đòi hỏi dòng xả quá lớn như vậy.&lt;/p&gt;

&lt;p&gt;Cell pin phổ thông thường &lt;strong&gt;không có mạch bảo vệ&lt;/strong&gt; tích hợp. Chạm tua-vít qua hai cực có thể gây phóng dòng hàng chục ampe tức thời. Không bỏ cell trần vào túi cùng chìa khóa, không sạc trên bề mặt dễ cháy, và ngưng sử dụng ngay nếu cell bị phồng, tụt dưới 2,5 V hoặc nóng bất thường. Một lỗi phổ biến khác là dùng pin vuông 9 V: nội trở lớn khiến điện áp sụp ngay khi động cơ kẹt, làm vi điều khiển khởi động lại trong khi mạch lái vẫn giữ lệnh chạy.&lt;/p&gt;

&lt;p&gt;Khi thực hành trên bàn, hãy cấp nguồn vi điều khiển bằng cáp USB, nối mass chung giữa USB và mạch lái, và chỉ bật nguồn pin động cơ sau cùng khi bánh xe đã được nhấc lên.&lt;/p&gt;

&lt;h2 id=&quot;đợt-4--máy-tính-nhúng-trên-xe-lidar-và-pin-nâng-cấp-cho-chương-07-09-11&quot;&gt;Đợt 4 — máy tính nhúng trên xe, LiDAR và pin nâng cấp (cho Chương 07, 09, 11)&lt;/h2&gt;

&lt;p&gt;Các đợt 1 đến 3 ở trên đã đủ để xe hoàn thành teleop. Những món trong bảng dưới đây &lt;strong&gt;không&lt;/strong&gt; nằm trong đơn hàng tuần đầu mà chỉ mua khi học tới chương tương ứng. Cột giá được để trống để bạn tự điền đúng mức giá tại thời điểm mở trang mua hàng:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th&gt;Thời điểm&lt;/th&gt;
      &lt;th&gt;Vai trò trên lộ trình&lt;/th&gt;
      &lt;th&gt;Nguồn tìm kiếm tham khảo&lt;/th&gt;
      &lt;th&gt;Giá (điền khi mua)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Raspberry Pi 5 (RAM đủ chạy Ubuntu Server 24.04)&lt;/td&gt;
      &lt;td&gt;Đầu Chương 09 nếu muốn xe chạy độc lập không cần laptop&lt;/td&gt;
      &lt;td&gt;Máy tính chạy ROS 2 Jazzy, agent và bridge&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi&quot;&gt;Hshop, raspberry pi&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=raspberry%20pi%205&quot;&gt;Shopee, Raspberry Pi 5&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Jetson Orin Nano&lt;/td&gt;
      &lt;td&gt;Chỉ khi cần GPU xử lý trực tiếp trên xe&lt;/td&gt;
      &lt;td&gt;Lưu ý: JetPack 6 dùng Ubuntu 22.04 (không cài Jazzy gốc), JetPack 7 dùng Ubuntu 24.04. Đọc kỹ &lt;a href=&quot;https://docs.nvidia.com/jetson/orin-nano-devkit/user-guide/latest/quick_start.html&quot;&gt;quick start&lt;/a&gt; trước khi mua&lt;/td&gt;
      &lt;td&gt;Tìm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Jetson Orin Nano&lt;/code&gt; trên &lt;a href=&quot;https://shopee.vn/search?keyword=jetson%20orin%20nano&quot;&gt;Shopee&lt;/a&gt; hoặc nhà phân phối có hóa đơn rõ ràng&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LiDAR quét phẳng LD06 hoặc RPLIDAR A1&lt;/td&gt;
      &lt;td&gt;Chương 11, bài lab SLAM thực tế&lt;/td&gt;
      &lt;td&gt;Xuất dữ liệu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; cho &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; (cảm biến ToF một tia không thay thế được)&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/search?q=lidar&quot;&gt;Hshop, lidar&lt;/a&gt;, &lt;a href=&quot;https://www.slamtec.com/en/Lidar/A1&quot;&gt;Slamtec A1&lt;/a&gt;, &lt;a href=&quot;https://www.ldrobot.com/&quot;&gt;LDRobot&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin LiPo 2S có giắc cân bằng, LiFePO4 đúng sạc, hoặc dùng tiếp 18650 Đợt 3&lt;/td&gt;
      &lt;td&gt;Trước bài lắp khung Chương 07&lt;/td&gt;
      &lt;td&gt;Cấp nguồn VM (tuyệt đối không dùng lẫn chế độ sạc giữa các loại pin)&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/search?q=lipo&quot;&gt;Hshop, lipo&lt;/a&gt;; cell 18650 xem ở mục Nguồn bên dưới&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạch BMS đúng số cell và loại hóa học (nếu pack chưa có)&lt;/td&gt;
      &lt;td&gt;Mua cùng lúc với pin&lt;/td&gt;
      &lt;td&gt;Bảo vệ quá áp / dưới áp (không thay thế cầu chì)&lt;/td&gt;
      &lt;td&gt;Tìm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BMS 2S&lt;/code&gt; trên &lt;a href=&quot;https://shopee.vn/search?keyword=bms%202s&quot;&gt;Shopee&lt;/a&gt; hoặc &lt;a href=&quot;https://www.lazada.vn/catalog/?q=bms%202s&quot;&gt;Lazada&lt;/a&gt;; kiểm tra nhãn Li-ion hay LiFePO4&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạch hạ áp (buck) và cầu chì 2 A, 3 A&lt;/td&gt;
      &lt;td&gt;Chương 07&lt;/td&gt;
      &lt;td&gt;Tạo nguồn 5,0 V cho ESP32 và lắp cầu chì trên dây dương&lt;/td&gt;
      &lt;td&gt;Xem bài pin Chương 07; tham khảo &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt;, &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Raspberry Pi 5 và Jetson Orin Nano là hai lựa chọn thay thế nhau cho một máy Ubuntu trên xe, không cần mua cả hai; tương tự, chỉ chọn một trong hai mẫu LiDAR LD06 hoặc A1. Bạn sẽ học cách tính chọn cầu chì ở bài &lt;a href=&quot;/contents/vi/chapter07/07_00_batteries_power_safety/&quot;&gt;Pin, nguồn và an toàn&lt;/a&gt; và cách chọn đúng ảnh hệ điều hành Ubuntu 24.04 ở bài &lt;a href=&quot;/contents/vi/chapter09/09_00_linux_on_sbc/&quot;&gt;Linux trên SBC&lt;/a&gt;. Nguyên tắc không mua LiDAR sớm ở tuần đầu vẫn giữ nguyên cho đến Chương 11.&lt;/p&gt;

&lt;h2 id=&quot;kinh-nghiệm-đặt-mua-và-kiểm-hàng&quot;&gt;Kinh nghiệm đặt mua và kiểm hàng&lt;/h2&gt;

&lt;p&gt;Hãy luôn mua dựa trên thông số kỹ thuật thay vì ảnh minh họa hay nhãn giảm giá: một bộ L298N giảm giá 30% vẫn là loại mạch lái gây sụt áp lớn. Trước khi đặt hàng, cần đọc kỹ dải điện áp, dòng liên tục, mức logic và loại giắc cắm.&lt;/p&gt;

&lt;p&gt;Với Đợt 1, nên gom chung tại một cửa hàng để tránh cảnh thiếu sợi cáp USB mà phải chờ thêm đơn hàng khác. Tại Việt Nam, Hshop (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hshop.vn&lt;/code&gt;, địa chỉ 269/20 Lý Thường Kiệt, Phường Phú Thọ, TP.HCM) có mô tả kỹ thuật và mã SKU rõ ràng (các mã SKU kết thúc bằng chữ &lt;strong&gt;V&lt;/strong&gt; là giá công ty đã bao gồm VAT; cửa hàng cũng yêu cầu quay video mở hộp trong 7 ngày cho bảo hành cơ bản). Trên Shopee và Lazada, hãy lọc các gian hàng uy tín, đọc kỹ xem sản phẩm là mạch phát triển (dev board) hay module trần, và ưu tiên nơi có ảnh chụp rõ chữ in trên IC. Thế Giới IC (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thegioiic.com&lt;/code&gt;) và IC Đây Rồi (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;icdayroi.com&lt;/code&gt;, Thủ Đức) là các cửa hàng linh kiện rất phù hợp khi mua IC ổn áp hoặc module rời, nhưng cần chú ý phân biệt module ESP32 trần với mạch phát triển có sẵn cổng USB.&lt;/p&gt;

&lt;p&gt;Với cửa hàng điện tử chuyên dụng, bạn có thể chuyển khoản sau khi xác nhận còn hàng; với sàn thương mại điện tử, nên thanh toán qua hệ thống của sàn cho các món như dây jumper hay khung xe. Tránh chuyển khoản trực tiếp cho người bán không rõ nguồn gốc trên mạng xã hội để mua “pin 18650 đã test” mà không có chính sách đổi trả.&lt;/p&gt;

&lt;p&gt;Khi kiện hàng giao tới, hãy kiểm tra theo đúng 7 bước sau trước khi lắp ráp:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Quay video quá trình mở hộp và kiểm đếm từng túi linh kiện đối chiếu với &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Kiểm tra đồng hồ vạn năng: chạm hai que đo phải kêu bíp thông mạch, sau đó đo thử khoảng 5 V trên một củ sạc USB đáng tin cậy.&lt;/li&gt;
  &lt;li&gt;Kiểm tra vi điều khiển: cắm bằng cáp USB &lt;strong&gt;có truyền dữ liệu&lt;/strong&gt; vào máy tính và xác nhận xuất hiện cổng serial mới (cáp chỉ sạc sẽ không hiện cổng và dễ bị nhầm là mạch hỏng).&lt;/li&gt;
  &lt;li&gt;Khi chỉ cắm USB, đo chân 3,3 V phải đạt khoảng 3,3 V; nếu đo ra 5 V thì dừng ngay.&lt;/li&gt;
  &lt;li&gt;Kiểm tra mạch lái: đọc chữ in trên thân IC để chắc chắn là TB6612FNG hoặc DRV8833, không phải L298N.&lt;/li&gt;
  &lt;li&gt;Kiểm tra động cơ: xoay nhẹ trục bằng tay xem có bị sượng không và đối chiếu tỉ số truyền ghi trên bao bì với đơn đặt hàng.&lt;/li&gt;
  &lt;li&gt;Kiểm tra cell pin: vỏ không phồng, điện áp mỗi cell nằm trong khoảng 3,4 V đến 4,2 V; không lắp nối tiếp trước khi đo đủ cả hai viên.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Các liên kết dưới đây bao gồm &lt;strong&gt;trang sản phẩm đã ghi nhận giá ngày 23 tháng 9 năm 2026&lt;/strong&gt; và &lt;strong&gt;trang tìm kiếm theo từ khóa&lt;/strong&gt; (được ghi chú rõ). Với trang tìm kiếm, kết quả đầu tiên không phải lúc nào cũng đúng món cần mua nên bạn vẫn cần đọc kỹ thông số. Trên Lazada, bạn có thể tìm theo cấu trúc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://www.lazada.vn/catalog/?q=&lt;/code&gt; kèm từ khóa trên trình duyệt thông thường.&lt;/p&gt;

&lt;h3 id=&quot;vi-điều-khiển&quot;&gt;Vi điều khiển&lt;/h3&gt;

&lt;p&gt;Từ khóa gợi ý: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 NodeMCU 38 chân CH340&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 DevKit V1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Raspberry Pi Pico 2&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Trang Hshop đã kiểm: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;ESP32 NodeMCU-32S CH340 Ai-Thinker, 190 000 ₫&lt;/a&gt; — đây là mạch phát triển có sẵn USB, phù hợp để chọn mua.&lt;/li&gt;
  &lt;li&gt;Trang Hshop đã kiểm (phân khúc cao hơn): &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32, 265 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Trang Hshop đã kiểm (&lt;strong&gt;không&lt;/strong&gt; chọn làm mạch duy nhất ban đầu): &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;module ESP32-S3-WROOM-1, 135 000 ₫&lt;/a&gt; — đây là module trần, chưa có chip USB-UART.&lt;/li&gt;
  &lt;li&gt;Trang tìm kiếm Hshop: &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;raspberry pi pico&lt;/a&gt; (thời điểm khảo sát có Pico 2 giá 195.000 ₫ và Pico 2 W giá 275.000 ₫).&lt;/li&gt;
  &lt;li&gt;Trang Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/esp32-devkitc-32u-module-wifi-bluetooth-2-4ghz&quot;&gt;ESP32-DevKitC-32U&lt;/a&gt; — cần đọc kỹ xem bản dùng đầu nối anten U.FL này có kèm sẵn cáp anten ngoài hay không.&lt;/li&gt;
  &lt;li&gt;Tìm trên Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=esp32%20devkit%20ch340&quot;&gt;ESP32 DevKit&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Lưu ý: tránh các sản phẩm chỉ chụp ảnh module bọc kim loại không có cổng USB dù tiêu đề vẫn ghi “kit phát triển”.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;khung-xe-động-cơ-bánh-đa-hướng&quot;&gt;Khung xe, động cơ, bánh đa hướng&lt;/h3&gt;

&lt;p&gt;Từ khóa gợi ý: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khung xe robot 2 bánh TT&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ giảm tốc TT 1:48&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh đa hướng&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Tại thời điểm khảo sát, Hshop không có sẵn bộ khung 2WD mica giá rẻ, nhưng có &lt;a href=&quot;https://hshop.vn/khung-xe-mke-r01-4wd-robot-car-chassis-tt-motor&quot;&gt;khung MKE-R01 4WD động cơ TT, 245 000 ₫&lt;/a&gt;. Bạn có thể dùng hai động cơ cho xe Capstone (để hai chiếc còn lại dự phòng) kèm một bánh đa hướng, hoặc chỉ ghép cặp bánh trái/phải sau khi đã đo dòng cụ thể.&lt;/li&gt;
  &lt;li&gt;Linh kiện mua rời trên Hshop: &lt;a href=&quot;https://hshop.vn/dong-co-dc-giam-toc-tt-motor-ti-so-1-48&quot;&gt;động cơ TT 1:48, 20 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/banh-xe-dong-co-dc-giam-toc-v1-plastic-geared-tt-motor-65mm&quot;&gt;bánh 65 mm, 8 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/ga-bat-dong-co-dc-giam-toc-tt-motor-mounting-bracket&quot;&gt;gá bắt động cơ, 10 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/banh-xe-nhua-da-huong-3pi-ball-caster-wheel&quot;&gt;bánh đa hướng, 15 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Tìm trên Shopee (trang tìm kiếm chung): &lt;a href=&quot;https://shopee.vn/search?keyword=khung%20xe%20robot%202%20b%C3%A1nh%20TT&quot;&gt;khung xe 2 bánh TT&lt;/a&gt; — mức giá phổ biến khoảng &lt;strong&gt;90.000–180.000 ₫&lt;/strong&gt;; cần kiểm tra ảnh có đủ hai động cơ, bánh đa hướng và hộp pin.&lt;/li&gt;
  &lt;li&gt;Tìm trên Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=khung%20xe%20robot%202%20banh&quot;&gt;khung xe robot&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Tránh các bộ “kit robot” ghép sẵn L298N với hộp 4 pin AA.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;driver-động-cơ&quot;&gt;Driver động cơ&lt;/h3&gt;

&lt;p&gt;Từ khóa gợi ý: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt; (không chọn &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;module L298 giá rẻ&lt;/code&gt; làm mặc định).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Đã kiểm: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-tb6612fng-dc-motor-driver&quot;&gt;TB6612FNG, 45 000 ₫, SKU HS2458V&lt;/a&gt; — VM 4,5–10 V, logic 2,7–5,5 V, dòng 1,2 A liên tục, 3,2 A đỉnh, kích thước 20×20 mm.&lt;/li&gt;
  &lt;li&gt;Phương án thay thế đã kiểm: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-drv8833-dc-motor-driver&quot;&gt;DRV8833, 25 000 ₫&lt;/a&gt; — VM 2,7–10,8 V, dòng tối đa 1,5 A/kênh theo mô tả.&lt;/li&gt;
  &lt;li&gt;Sản phẩm tham khảo nhưng &lt;strong&gt;không&lt;/strong&gt; dùng làm mặc định: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;module L298N, cùng giá 45 000 ₫&lt;/a&gt; — chỉ cân nhắc nếu bạn nắm rõ mức sụt áp và dùng động cơ loại 12 V.&lt;/li&gt;
  &lt;li&gt;Tìm trên Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=tb6612fng&quot;&gt;TB6612FNG&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Mạch tham chiếu chuẩn nếu có nhu cầu nhập chính hãng về sau: &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu 713&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;cảm-biến&quot;&gt;Cảm biến&lt;/h3&gt;

&lt;p&gt;Từ khóa gợi ý: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HC-SR04&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VL53L0X&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đĩa encoder TT 20 xung&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Đã kiểm: &lt;a href=&quot;https://hshop.vn/cam-bien-sieu-am-srf04&quot;&gt;HC-SR04, 27 000 ₫&lt;/a&gt; — chân echo xuất mức 5 V nên cần mua kèm mạch chuyển mức logic.&lt;/li&gt;
  &lt;li&gt;Mạch chuyển mức đã kiểm: &lt;a href=&quot;https://hshop.vn/mach-chuyen-muc-tin-hieu-logic-4-kenh&quot;&gt;mạch chuyển mức 4 kênh, 10 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Linh kiện nâng cấp về sau (đã kiểm): &lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;GY-521 MPU6050, 85 000 ₫&lt;/a&gt; — giao tiếp I²C, cấp nguồn 3,3–5 V, logic 3,3 V; chưa cần ở giai đoạn đầu.&lt;/li&gt;
  &lt;li&gt;Phương án ToF thay HC-SR04 (đã kiểm): &lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-tof-laser-radar-vl53l0x&quot;&gt;module VL53L0X, 115 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Đĩa encoder (đã kiểm, kích thước nhỏ rất dễ thất lạc): &lt;a href=&quot;https://hshop.vn/dia-encoder-20-xung-dong-co-dc-giam-toc-v1&quot;&gt;đĩa 20 xung, 1 000 ₫&lt;/a&gt;. Mạch cảm biến quang đọc đĩa là món riêng, tìm theo từ khóa &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cảm biến tốc độ encoder hồng ngoại&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Tìm trên Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=hc-sr04&quot;&gt;HC-SR04&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;nguồn&quot;&gt;Nguồn&lt;/h3&gt;

&lt;p&gt;Từ khóa gợi ý: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pin 18650 2500mAh&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sạc 18650 4 ngăn&lt;/code&gt;. Chỉ tìm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TP4056&lt;/code&gt; khi bạn dùng đúng cho mục đích sạc một cell đơn.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Cell pin đã kiểm: &lt;a href=&quot;https://hshop.vn/pin-sac-18650-li-ion-rechargeable-battery-3-7v-2500mah-10c-sunpower&quot;&gt;Sunpower 2500 mAh, 45 000 ₫&lt;/a&gt; và &lt;a href=&quot;https://hshop.vn/pin-sac-18650-li-ion-rechargeable-battery-3-7v-2000mah-10c-sunpower&quot;&gt;Sunpower 2000 mAh, 35 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Đế pin chân hàn đã kiểm: &lt;a href=&quot;https://hshop.vn/hop-pin-2-co-18650&quot;&gt;đế 2×18650, 4 000 ₫&lt;/a&gt;. Lưu ý nhiều bộ khung xe đã kèm sẵn đế pin, tránh mua dư rồi mắc nối tiếp bốn cell.&lt;/li&gt;
  &lt;li&gt;Đế pin không hàn đã kiểm: &lt;a href=&quot;https://hshop.vn/hop-pin-ghep-noi-diy-solderless-18650-battery-holder&quot;&gt;đế DIY, 10 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Bộ sạc khay đã kiểm: &lt;a href=&quot;https://hshop.vn/bo-sac-pin-18650-li-ion-usb-battery-charger-yh-18650-4&quot;&gt;sạc 4 ngăn USB, 68 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Mạch sạc một cell đã kiểm (&lt;strong&gt;không&lt;/strong&gt; dùng cho cụm 2S): &lt;a href=&quot;https://hshop.vn/mach-sac-pin-tp4056-lithium-battery-charge-controller-usb-c&quot;&gt;TP4056 USB-C, 10 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Tìm trên Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=s%E1%BA%A1c%20pin%2018650%204%20ng%C4%83n&quot;&gt;sạc pin 18650&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Lưu ý: tránh các mạch “sạc nhanh 2 A” không rõ thiết kế giới hạn dòng và mọi cell pin quảng cáo trên 3500 mAh.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;dụng-cụ-và-mạch-thử&quot;&gt;Dụng cụ và mạch thử&lt;/h3&gt;

&lt;p&gt;Từ khóa gợi ý: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đồng hồ vạn năng&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mỏ hàn 60W&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;breadboard 830&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây breadboard đực cái&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Đồng hồ vạn năng đã kiểm: &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UNI-T UT33D+, 285 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-ky-thuat-so-vom-wadfow-wdm1501-digital-multimeter-true-rms&quot;&gt;Wadfow WDM1501, 185 000 ₫&lt;/a&gt;. Mẫu UT136C+ (560.000 ₫) là phương án nâng cấp về sau, chưa cần thiết cho giai đoạn bắt đầu.&lt;/li&gt;
  &lt;li&gt;Dụng cụ hàn đã kiểm: &lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;Wadfow WEL3616 60 W, 75 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;giá đỡ, 40 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/thiec-han-sunchi-kp26-0-8mm-sn63-pb37-solder-wire&quot;&gt;thiếc Sn63 0,8 mm, 24 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Kìm đã kiểm: &lt;a href=&quot;https://hshop.vn/kim-cat-day-dien-nho-170-cat-chan-linh-kien-dien-tu&quot;&gt;kìm cắt chân 170, 35 000 ₫&lt;/a&gt;; nếu muốn thêm kìm tuốt dây hạn chế cứa lõi đồng, tham khảo &lt;a href=&quot;https://hshop.vn/kim-tuot-day-dien-da-nang-wadfow-wbq8401-wire-stripper&quot;&gt;Wadfow WBQ8401, 130 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Mạch thử đã kiểm: &lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;breadboard 830 lỗ, 35 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/day-cam-breadboard-duc-duc-20cm-cap-det-40-soi-m-m-jumper-wire&quot;&gt;dây đực-đực, 30 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/day-cam-breadboard-duc-cai-20cm-cap-det-40-soi-m-f-jumper-wire&quot;&gt;dây đực-cái, 30 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/mach-cap-nguon-cho-breadboard-400-830-lo-mb-102&quot;&gt;mạch cấp nguồn breadboard, 25 000 ₫&lt;/a&gt; (nên hoãn dùng mạch cấp nguồn breadboard cho tới khi bạn phân biệt chắc chắn đường 3,3 V và 5 V).&lt;/li&gt;
  &lt;li&gt;Cáp USB đã kiểm: &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;. Cáp dài 30 cm (27.000 ₫) khá bất tiện khi đặt xe dưới sàn; nếu mạch dùng cổng USB-C thì chọn cáp USB-C truyền dữ liệu tương ứng.&lt;/li&gt;
  &lt;li&gt;Phụ kiện nhỏ đã kiểm: &lt;a href=&quot;https://hshop.vn/bo-5-loai-led-sieu-sang-3mm-thong-dung-5-kind-3mm-transparent-color-led&quot;&gt;bộ LED 3 mm, 20 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/nut-nhan-nha-tron-pbs-11b-12mm-kem-cap&quot;&gt;nút nhấn tròn, 10 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/day-dien-do-den&quot;&gt;dây điện đỏ đen 1 m, 8 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Bộ điện trở: tại thời điểm khảo sát, tìm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;điện trở&lt;/code&gt; trên Hshop không ra bộ 1/4 W thông dụng, nên bạn có thể tìm trên Shopee theo &lt;a href=&quot;https://shopee.vn/search?keyword=b%E1%BB%99%20%C4%91i%E1%BB%87n%20tr%E1%BB%9F%201%2F4W&quot;&gt;bộ điện trở 1/4W&lt;/a&gt; (giá thường khoảng &lt;strong&gt;30.000–80.000 ₫&lt;/strong&gt;), đảm bảo trong bộ có các trị số 220 Ω, 330 Ω, 1 kΩ và 10 kΩ.&lt;/li&gt;
  &lt;li&gt;Tìm dụng cụ trên Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=m%E1%BB%8F%20h%C3%A0n%2060W&quot;&gt;mỏ hàn 60W&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=%C4%91%E1%BB%93ng%20h%E1%BB%93%20v%E1%BA%A1n%20n%C4%83ng&quot;&gt;đồng hồ vạn năng&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Cửa hàng linh kiện rời: &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; (khu vực Thủ Đức) và &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nhắc lại nguyên tắc đi dây: dây Dupont chỉ dùng cho tín hiệu, còn dòng động cơ phải đi qua dây đỏ đen tiết diện lớn hơn, bắt chặt vào trạm vít hoặc hàn thẳng vào cực của mạch lái thay vì cắm qua hàng lỗ breadboard.&lt;/p&gt;

&lt;h2 id=&quot;giỏ-hàng-mẫu-bạn-lan-lộ-trình-a-trần-2000000-&quot;&gt;Giỏ hàng mẫu: bạn Lan, Lộ trình A, trần 2.000.000 ₫&lt;/h2&gt;

&lt;p&gt;Lan học theo Lộ trình A và muốn làm teleop Wi-Fi ở Chương 08, nên chọn mạch ESP32 NodeMCU-32S thay vì Pico. Vì mới tiếp xúc với pin lithium, cô mua bộ sạc 4 khay để sạc từng cell riêng và tuyệt đối không kẹp TP4056 lên đế nối tiếp. Với bộ khung 4WD, cô tháo bớt hai động cơ để dự phòng và gắn thêm bánh đa hướng. Bảng dưới đây dùng mốc giá Hshop đã ghi nhận ở trên:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hạng mục&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;SL&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Đơn giá (₫)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Thành tiền (₫)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 NodeMCU-32S CH340&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;190 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;190 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Khung MKE-R01 4WD (dùng 2 motor)&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;245 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;245 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bánh đa hướng&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TB6612FNG&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;45 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;45 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;27 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;27 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chuyển mức 4 kênh&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;10 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;10 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Breadboard 830 lỗ&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dây đực-đực và đực-cái&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;30 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;60 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cáp Ugreen Micro-USB 1 m&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;54 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;54 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;UNI-T UT33D+&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;285 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;285 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mỏ 60 W + đế&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1+1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;75 000 + 40 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;115 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thiếc 0,8 mm&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;24 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;24 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Kìm cắt&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;18650 2500 mAh&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;45 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;90 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sạc 4 khay&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;68 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;68 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MPU6050 (cô giữ, dù là tùy chọn)&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;85 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;85 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bộ LED + nút + dây 1 m&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;20 000 + 10 000 + 8 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;38 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Tổng linh kiện&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;strong&gt;1 421 000&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Cộng thêm bộ điện trở mua trên Shopee khoảng 50.000 ₫ và phí vận chuyển nội thành khoảng 30.000 ₫, tổng chi phí vào khoảng &lt;strong&gt;1.500.000 ₫&lt;/strong&gt;, nằm gọn trong ngưỡng 1.500.000–2.500.000 ₫ và vẫn còn dư ngân sách cho trụ đồng M3 cùng cáp USB dự phòng. Nếu đổi đồng hồ sang Wadfow (185.000 ₫) sẽ tiết kiệm thêm 100.000 ₫. Đổi sang Pico 2 (195.000 ₫) hầu như không rẻ hơn mà lại phải hoãn phần Wi-Fi. Lan cũng cân nhắc việc mua thêm một mạch TB6612 thứ hai để kéo cả bốn bánh thay vì dùng bánh đa hướng, nhưng xác định đó là một cấu hình robot khác nên ghi rõ là phương án bị loại thay vì âm thầm sửa giỏ hàng.&lt;/p&gt;

&lt;p&gt;Cô loại bỏ L298N dù cùng giá 45.000 ₫ vì ở nguồn 7,4 V, sụt áp 2,0 V chỉ còn lại 5,4 V — nằm sát vùng yếu (5–6 V, ~200 vòng/phút, 0,8 kg·cm ở 6 V) của động cơ TT. Mạch lái MOSFET giúp giữ điện áp gần mức 7 V để xe vận hành khỏe hơn.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành-lập-tệp-bomcsv-và-kiểm-tra-từng-dòng&quot;&gt;Thực hành: lập tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; và kiểm tra từng dòng&lt;/h2&gt;

&lt;p&gt;Tạo tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; trong thư mục ghi chú với dòng tiêu đề sau:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;part_id,name,qty,role,rating,vendor,url_or_search,unit_vnd,line_vnd,substitute,wave,received,photo,notes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Các bước thực hiện:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Chép danh sách mẫu của Lan, sau đó bỏ hoặc hoãn MPU6050 nếu vượt ngân sách của bạn. Thêm một dòng cho bộ điện trở, tạm ghi giá là “khoảng Shopee, chốt khi mở tin” cho tới khi có giá thực tế.&lt;/li&gt;
  &lt;li&gt;Duy trì đủ ba cột theo dõi thực tế: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;received&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yes&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;no&lt;/code&gt;), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;photo&lt;/code&gt; (tên tệp ảnh chụp) và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes&lt;/code&gt; (ký hiệu đọc trực tiếp trên thân IC).&lt;/li&gt;
  &lt;li&gt;Ở dòng mạch lái, ghi rõ dải điện áp VM và ghi chú “không lấy L298N” vào cột &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Ở dòng bộ sạc, ghi chú “mỗi khay một cell, không kẹp lên đế 2S”.&lt;/li&gt;
  &lt;li&gt;Nếu hàng chưa giao tới, để &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;received&lt;/code&gt; là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;no&lt;/code&gt; nhưng vẫn phải hoàn thiện toàn bộ bảng; khi mạch và đồng hồ về tới thì chụp ảnh bổ sung.&lt;/li&gt;
  &lt;li&gt;Tính tổng cột &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;line_vnd&lt;/code&gt;. Nếu vượt 2.500.000 ₫, cắt giảm theo thứ tự ưu tiên: cảm biến IMU, kìm tuốt dây, mạch cấp nguồn breadboard — không cắt đồng hồ vạn năng hay mạch lái động cơ.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kết quả cần đạt:&lt;/strong&gt; Một tổng chi phí có thể giải thích rõ ràng trong một phút; dòng mạch lái nêu rõ phương án thay thế là DRV8833 và loại bỏ L298N; dòng bộ sạc đảm bảo không dùng TP4056 cho đế pin nối tiếp.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Các lỗi thường gặp:&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tình huống gặp phải&lt;/th&gt;
      &lt;th&gt;Nguyên nhân&lt;/th&gt;
      &lt;th&gt;Cách khắc phục&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Giỏ hàng rẻ nhưng dùng mạch lái L298N&lt;/td&gt;
      &lt;td&gt;Bỏ qua yếu tố sụt áp cầu Darlington&lt;/td&gt;
      &lt;td&gt;Đổi sang TB6612FNG hoặc DRV8833; phần mềm không bù được sụt áp&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dòng ESP32 trỏ tới module WROOM trần&lt;/td&gt;
      &lt;td&gt;Không có cổng USB-UART&lt;/td&gt;
      &lt;td&gt;Đổi sang mạch phát triển (dev board) có sẵn cổng USB&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đặt hai cell 18650 kèm một mạch TP4056&lt;/td&gt;
      &lt;td&gt;Sạc 4,2 V không tương thích cụm 8,4 V&lt;/td&gt;
      &lt;td&gt;Đổi sang bộ sạc khay để nạp riêng từng cell&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cột dung lượng pin ghi 9900 mAh&lt;/td&gt;
      &lt;td&gt;Thông số công bố sai sự thật&lt;/td&gt;
      &lt;td&gt;Bỏ qua sản phẩm đó, chọn cell uy tín ≤ 3500 mAh&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chọn cáp USB dài 30 cm&lt;/td&gt;
      &lt;td&gt;Quá ngắn khi thử xe trên sàn hoặc bàn&lt;/td&gt;
      &lt;td&gt;Đổi sang cáp truyền dữ liệu dài 1 m&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dự định đi dây động cơ bằng dây Dupont&lt;/td&gt;
      &lt;td&gt;Tiếp điểm breadboard gây sụt áp và sinh nhiệt&lt;/td&gt;
      &lt;td&gt;Dùng dây đỏ đen tiết diện lớn nối qua trạm vít&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý an toàn:&lt;/strong&gt; Rủi ro thực sự của bài này nằm ở pin lithium, không phải ở tệp bảng tính. Luôn sạc pin ở nơi dễ quan sát, tránh làm chập hai cực đế pin, và tuyệt đối không nối nguồn VM vào chân 5 V hay 3,3 V của ESP32 để “thử pin”.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=RsZBxfpcLu8&quot;&gt;PART1: Building Low-Cost Differential Wheeled Robot For Machine Learning, SLAM, and ROS&lt;/a&gt; — Aleksandar Haber PhD, tiếng Anh. Minh họa một mẫu xe vi sai chi phí thấp và các linh kiện thực tế cần dùng; hãy đối chiếu với bảng BOM của khóa thay vì sao chép nguyên giá trong video.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[How To Read A Datasheet - Electronics with Becky Stern&lt;/td&gt;
          &lt;td&gt;DigiKey](https://www.youtube.com/watch?v=v2d5H5k_SAc) — DigiKey, tiếng Anh. Hướng dẫn cách đọc trang sản phẩm linh kiện tương tự đọc datasheet: chú ý điện áp, dòng điện và những thông số bị lược bỏ.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Giả sử cụm pin 2S đang ở mức 8,0 V. Mạch L298N làm sụt 2,2 V còn mạch TB6612 làm sụt 0,4 V. Động cơ TT nhận được bao nhiêu vôn trong mỗi trường hợp, và mức nào nằm tốt hơn trong dải 3–9 V của động cơ?&lt;/li&gt;
  &lt;li&gt;Bạn có một đế pin nối tiếp 2S và chỉ có một mạch sạc TP4056. Hãy viết đúng trình tự nạp cả hai cell mà không kẹp mạch sạc lên cụm nối tiếp.&lt;/li&gt;
  &lt;li&gt;Một bài đăng trên Shopee có tiêu đề “ESP32 WiFi Bluetooth”, nhưng ảnh chụp là module bọc vỏ kim loại có hàng chân hàn viền ngoài và không có cổng USB. Món này tương ứng với dòng nào trong BOM và còn thiếu phần nào?&lt;/li&gt;
  &lt;li&gt;Chân echo của HC-SR04 xuất mức 5 V trong khi chân GPIO của vi điều khiển chỉ chịu 3,3 V. Bạn cần thêm linh kiện nào, và dùng phép đo gì để chắc chắn chân echo chưa bị nối thẳng vào GPIO?&lt;/li&gt;
  &lt;li&gt;Tổng tiền linh kiện trước vận chuyển là 1.420.000 ₫, phí vận chuyển 35.000 ₫ và bộ điện trở 55.000 ₫. Nếu Lan mua thêm một mạch TB6612 nữa giá 45.000 ₫ thì tổng chi phí có còn dưới mức trần 2.000.000 ₫ không? Hãy viết phép tính cụ thể.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Qua L298N: $$8{,}0 - 2{,}2 = 5{,}8\,\text{V}$$. Qua TB6612: $$8{,}0 - 0{,}4 = 7{,}6\,\text{V}$$. Cả hai đều nằm trong dải 3–9 V, nhưng 7,6 V giúp động cơ khỏe hơn rõ rệt so với mốc mô-men chuẩn 6 V trên bảng thông số, còn 5,8 V nằm về phía yếu.
2. Tháo rời hai cell khỏi đế. Nạp cell A đơn lẻ cho đến khi TP4056 báo đầy và ngắt gần 4,2 V, sau đó lặp lại tương tự với cell B. Đo kiểm tra điện áp cả hai viên rồi mới lắp vào đế 2S. TP4056 chỉ dùng cho từng cell rời, không kẹp lên hai cực đế nối tiếp.
3. Đó là module trần, không phải mạch phát triển (dev board). Nó còn thiếu mạch giao tiếp USB-UART và mạch ổn áp 3,3 V; người mới không nên bắt đầu khóa học bằng module trần.
4. Cần bổ sung mạch chuyển mức logic 4 kênh (hoặc cầu chia áp trên riêng chân echo). Khi cảm biến đã cấp nguồn nhưng chưa nối qua mạch chuyển mức, kiểm tra thông mạch để chắc chắn mạng dây echo và chân GPIO không thông trực tiếp với nhau.
5. $$1\,420\,000 + 35\,000 + 55\,000 + 45\,000 = 1\,555\,000$$ ₫, vẫn thấp hơn trần 2.000.000 ₫. Tuy nhiên, thêm mạch lái thứ hai đồng nghĩa thay đổi thiết kế sang hệ bốn động cơ chứ không phải phụ tùng dự phòng thông thường, nên cần ghi rõ trong cột ghi chú.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Trang TB6612FNG của Pololu, mã 713&lt;/a&gt; — thiết kế mạch MOSFET chuẩn mà các module trên thị trường tham khảo theo.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ti.com/product/DRV8833&quot;&gt;Thư mục DRV8833 của TI&lt;/a&gt; — thông số giới hạn dòng và dải điện áp VM của phương án thay thế gọn hơn.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/category/11/brushed-dc-motor-drivers&quot;&gt;Nhóm driver DC chổi than của Pololu&lt;/a&gt; — giải thích vì sao thông số “2 A” trên mạch Darlington chưa phản ánh đầy đủ khả năng làm việc thực tế.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;SparkFun: logic levels&lt;/a&gt; — giải thích sự khác biệt giữa tín hiệu echo 5 V và chân GPIO 3,3 V.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://batteryuniversity.com/article/bu-409-charging-lithium-ion&quot;&gt;Battery University, BU-409, nạp lithium-ion&lt;/a&gt; — lý thuyết về mức đầy 4,20 V mỗi cell và yêu cầu đối với bộ sạc pin mắc nối tiếp.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;Datasheet ESP32 của Espressif&lt;/a&gt; — tài liệu gốc xác nhận điện áp cực đại trên chân GPIO không phải 5 V.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf&quot;&gt;Tài liệu module HC-SR04, SparkFun&lt;/a&gt; — thông số thời gian xung trig/echo và mức điện áp 5 V của chân echo.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Danh mục dụng cụ bàn lab</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_04_kit_catalog_tooling/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_04_kit_catalog_tooling</id>
   <content type="html">&lt;p&gt;Hãy dành khoảng 90 phút cho bài học này. Nếu đã có đồng hồ vạn năng, bạn hãy kiểm tra hoạt động của nó trên một mối nối đã biết rồi cất gọn. Nếu chưa có, bạn vẫn hoàn thành bước kiểm kê Đợt 1 và chụp ảnh ghi nhận những món còn thiếu. Lưu ý: không cắm điện làm nóng mỏ hàn trong bài này.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ &lt;strong&gt;nêu&lt;/strong&gt; được công dụng của từng dụng cụ trong Đợt 1 và &lt;strong&gt;mô tả&lt;/strong&gt; dấu hiệu thực tế chứng tỏ chúng hoạt động tốt. Bạn sẽ &lt;strong&gt;chọn&lt;/strong&gt; đúng thang đo điện áp một chiều và thông mạch trên đồng hồ, đồng thời &lt;strong&gt;từ chối&lt;/strong&gt; đo điện trở trên mạch đang có điện. Bạn sẽ &lt;strong&gt;mô tả&lt;/strong&gt; được mối hàn đạt chuẩn là kết quả của việc làm nóng đều pad và chân linh kiện (với thiếc Sn63 đường kính 0,8 mm và giá đỡ mỏ hàn) thay vì nhỏ một giọt thiếc lên kim loại nguội. Bạn sẽ &lt;strong&gt;phân biệt&lt;/strong&gt; rõ breadboard (chỉ dùng cho đường tín hiệu) với đường dây chịu dòng động cơ, và &lt;strong&gt;tách bạch&lt;/strong&gt; vai trò của kìm cắt với kìm tuốt dây theo loại hư hại mà mỗi dụng cụ giúp phòng tránh. Cuối cùng, bạn sẽ &lt;strong&gt;kiểm kê&lt;/strong&gt; bàn làm việc theo danh sách Đợt 1, chụp ảnh và liệt kê chính xác những món còn thiếu để bài 00-05 trở thành danh sách mua sắm có căn cứ.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-trước&quot;&gt;Cần có trước&lt;/h2&gt;

&lt;p&gt;Bạn đã hoàn thiện thẻ quy trình cấp nguồn từ bài 00-03 (ít nhất trên giấy), nắm rõ nguồn động cơ VM tách biệt với chân 3,3 V và mỏ hàn luôn đi kèm giá đỡ cùng kính bảo hộ. Chương 01 sẽ bắt đầu các bài đo thực tế bằng que đo; hôm nay bạn cần biết dụng cụ nào giải quyết câu hỏi nào để tránh gián đoạn giữa chừng.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Lập trình trên màn hình máy tính cho phép bạn đặt điểm dừng (breakpoint) để nhìn thấy mọi biến số. Nhưng khi mạch robot trên bàn không chạy, dòng điện vô hình và điện áp không hề phát sáng — &lt;strong&gt;dụng cụ bàn lab&lt;/strong&gt; chính là “đôi mắt và đôi tay” giúp bạn nhìn thấy thế giới vật lý! Hiểu đúng chức năng và giới hạn đo của đồng hồ vạn năng, mỏ hàn, kìm tuốt dây và thước kẹp giúp bạn bắt bệnh phần cứng trong 2 phút thay vì đoán mò suốt buổi tối.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-bốn-nhóm-dụng-cụ-cốt-lõi-trên-bàn-lab-robotics&quot;&gt;Kiến thức cần nhớ: Bốn nhóm dụng cụ cốt lõi trên bàn lab Robotics&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Nhóm Đo lường (Đồng hồ vạn năng, Thước kẹp):&lt;/strong&gt; Biến các đại lượng ẩn (điện áp, thông mạch, điện trở, dòng điện, kích thước cơ khí) thành con số định lượng có sai số xác định.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nhóm Lắp ráp Điện (Breadboard, Mỏ hàn nhiệt độ ổn định, Dây jumper):&lt;/strong&gt; Tạo kết nối điện chắc chắn với điện trở tiếp xúc thấp — tách bạch giai đoạn thử nghiệm nhanh (breadboard) với giai đoạn chịu rung động trên xe chạy thật (hàn/cosse).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nhóm Gia công Cơ khí &amp;amp; An toàn (Kìm cắt, Kìm tuốt dây, Kính bảo hộ, Đế gá PCB):&lt;/strong&gt; Giữ linh kiện cố định, cắt chân dây không bắn vào mắt và đảm bảo khoảng hở lắp đặt chính xác cho trục bánh xe và cảm biến.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-kiểm-định-sai-số-linh-kiện-bằng-đồng-hồ-vạn-năng&quot;&gt;Ví dụ làm từng bước: Kiểm định sai số linh kiện bằng đồng hồ vạn năng&lt;/h3&gt;

&lt;p&gt;Trước khi tin vào dụng cụ hay linh kiện mới mua, hãy kiểm tra dung sai bằng phép tính cụ thể:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Kiểm tra điện trở \(1\text{ k}\Omega\) dung sai \(\pm 5\%\) (vạch vàng kim):&lt;/strong&gt;
\(R_{\min} = 1{,}000 \times (1 - 0.05) = \mathbf{950\ \Omega}, \qquad R_{\max} = 1{,}000 \times (1 + 0.05) = \mathbf{1{,}050\ \Omega}.\)
Nếu đồng hồ đọc \(982\ \Omega\) → linh kiện đạt chuẩn! Nếu đồng hồ đọc \(10.2\text{ k}\Omega\) → bạn đã lấy nhầm sang điện trở \(10\text{ k}\Omega\)!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kiểm tra đường nguồn \(5.0\text{ V}\) cấp cho cảm biến (dung sai cho phép \(\pm 5\%\)):&lt;/strong&gt;
\(V_{\min} = 5.0 \times 0.95 = \mathbf{4.75\text{ V}}, \qquad V_{\max} = 5.0 \times 1.05 = \mathbf{5.25\text{ V}}.\)
Nếu đo lúc không tải được \(5.02\text{ V}\) nhưng vừa chạy động cơ tụt xuống \(4.40\text{ V}\) → nguồn bị sụt áp, cần kiểm tra pin hoặc mạch hạ áp!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;giải-thích-quy-tắc-4-bước-quản-lý-dụng-cụ-giúp-giảm-80-lỗi-vặt&quot;&gt;Giải thích: Quy tắc 4 bước quản lý dụng cụ giúp giảm 80% lỗi vặt&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Luôn kiểm tra que đo đồng hồ trước khi đo:&lt;/strong&gt; Chập hai đầu que đo ở thang Ohm; nếu hiển thị \(0.2\text{–}0.5\ \Omega\) là dây tốt, nếu nhảy số loạn xạ thì chính dây đo đã đứt ngầm.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Để đồng hồ ở thang DC V (20 V) sau khi dùng xong:&lt;/strong&gt; Không bao giờ bỏ quên que đỏ ở lỗ đo dòng 10 A — lần sau đo điện áp pin sẽ gây ngắn mạch tức thì!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Dùng đúng tiết diện dây cho đúng dòng:&lt;/strong&gt; Dây jumper mảnh (26–28 AWG) chỉ dùng cho tín hiệu logic (vài mA); dây cấp nguồn động cơ (1–3 A) phải dùng dây 20–22 AWG để tránh sụt áp và nóng chảy vỏ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Đầu tư trước cho dụng cụ đo và hàn cơ bản:&lt;/strong&gt; Một chiếc đồng hồ vạn năng chính xác và mỏ hàn chỉnh nhiệt tốt phục vụ bạn suốt nhiều năm, đáng giá hơn nhiều so với việc mua sớm cảm biến đắt tiền.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Một bộ mạch hạ áp buck được chỉnh về \(5.00\text{ V}\) bằng tua-vít, đồng hồ vạn năng có độ chính xác \(\pm 1\%\) đọc ra \(5.03\text{ V}\). Điện áp thật nằm trong khoảng nào? Sai số đo là \(\pm 0.05\text{ V}\), nên điện áp thật nằm trong \([4.98\text{ V},\ 5.08\text{ V}]\) — hoàn toàn an toàn cho mọi cảm biến 5 V!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-liền-với-capstone&quot;&gt;Vì sao bài này gắn liền với Capstone&lt;/h2&gt;

&lt;p&gt;Bài nghiệm thu Chương 07 thường gặp những lỗi phần cứng rất cơ bản: một sợi dây cắm trông có vẻ chắc chắn nhưng bị đứt lõi bên trong đầu bấm, một phép đo mass thực hiện nhầm ở thang ohm trên mạch đang cấp điện, hay dây động cơ cắm trực tiếp vào lò xo của breadboard. Những sự cố này dễ bị nhầm thành lỗi firmware và kéo bạn đi tìm lỗi ở tầng phần mềm hay ROS 2, trong khi công cụ gỡ lỗi thực sự chỉ là một tiếng bíp thông mạch. Danh mục dụng cụ trong bài giúp bạn tách bạch “lỗi timeout PWM” với “lỗi dây dẫn” trước khi hệ thống trở nên phức tạp hơn.&lt;/p&gt;

&lt;h2 id=&quot;dấu-hiệu-hoạt-động-tốt-của-từng-dụng-cụ&quot;&gt;Dấu hiệu hoạt động tốt của từng dụng cụ&lt;/h2&gt;

&lt;p&gt;Để đồng bộ với bảng vật tư ở bài 00-05, &lt;strong&gt;Đợt 1&lt;/strong&gt; là bộ dụng cụ và linh kiện nền tảng giúp bạn bắt đầu ngay Chương 01: đồng hồ vạn năng, mỏ hàn 60 W kèm giá đỡ, thiếc hàn, kìm cắt, breadboard không hàn, dây cắm (jumper), cáp USB truyền dữ liệu, mạch vi điều khiển, vài đèn LED và một nút nhấn. &lt;strong&gt;Đợt 2&lt;/strong&gt; gồm khung xe, mạch lái TB6612FNG và cảm biến HC-SR04. &lt;strong&gt;Đợt 3&lt;/strong&gt; gồm cell pin và bộ sạc riêng từng cell. Bài này tập trung vào các dụng cụ của Đợt 1; bản thân vi điều khiển chỉ là một mục trên phiếu kiểm kê chứ không phải dụng cụ thao tác.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/kit_catalog_overview.png&quot; alt=&quot;Chân dung các họ đồ trên bàn, không phải một bộ có thương hiệu&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hình minh họa giới thiệu các nhóm thiết bị trên bàn lab. Đồng hồ vạn năng, mỏ hàn, breadboard và dụng cụ cầm tay là nhóm bắt buộc ngay Đợt 1. Các thiết bị như nguồn bàn hay máy phân tích logic (nếu có trong hình) dành cho giai đoạn sau, chưa cần mua hôm nay. Hàng bên dưới gồm vi điều khiển và khung robot sẽ đặt mua theo bài 00-05 khi sẵn sàng.&lt;/p&gt;

&lt;h3 id=&quot;đồng-hồ-vạn-năng&quot;&gt;Đồng hồ vạn năng&lt;/h3&gt;

&lt;p&gt;Đồng hồ vạn năng số giúp bạn trả lời ba câu hỏi thường gặp nhất — và có một phép đo bạn không được thực hiện nhầm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Điện áp một chiều (DC V)&lt;/strong&gt; đo hiệu điện thế giữa hai điểm, ký hiệu chữ V kèm vạch thẳng. Cắm que đen vào mass (GND), đặt que đỏ lên điểm cần đo. Đường USB 5 V tốt sẽ đọc khoảng 5 V; đường 3,3 V tốt đọc khoảng 3,2–3,4 V. Kết quả 0,00 V nghĩa là mạch đang tắt, chạm sai chân hoặc que đen chưa tiếp xúc đúng mass — đừng vội kết luận đồng hồ hỏng trước khi kiểm tra que đo bằng thang thông mạch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thông mạch&lt;/strong&gt; phát tiếng bíp khi điện trở giữa hai que đo rất nhỏ. Hãy chạm trực tiếp hai đầu que đo vào nhau: bạn phải nghe tiếng bíp rõ và thấy màn hình hiện gần 0 Ω (đôi khi có vài phần mười ohm của chính dây đo). Tiếng bíp này là bằng chứng đồng hồ và dây đo hoạt động bình thường. Một sợi dây ngắn còn tốt sẽ kêu bíp; ký hiệu &lt;strong&gt;OL&lt;/strong&gt; nghĩa là mạch đang hở.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Đo điện trở (Ω)&lt;/strong&gt; cho biết giá trị trở kháng khi thang thông mạch không kêu. Chỉ đo điện trở trên linh kiện &lt;strong&gt;đã ngắt hoàn toàn nguồn điện&lt;/strong&gt;. Khi đo ohm, đồng hồ tự bơm một dòng thử nhỏ; nếu mạch vẫn đang cấp nguồn, dòng này xung đột với nguồn mạch khiến số đo sai lệch và có thể làm hỏng đồng hồ hoặc board. Tuyệt đối không đo ohm trên đường nguồn đang có điện.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý bẫy đo dòng điện:&lt;/strong&gt; Khi chuyển que đỏ sang lỗ cắm ghi A hoặc mA, đồng hồ trở thành gần như một đoạn dây nối tắt. Nếu quên rút que đỏ về lỗ đo áp mà đem đi đo điện áp, bạn sẽ làm ngắn mạch nguồn. Hãy luôn kiểm tra lại vị trí lỗ cắm của que đỏ trước mỗi lần đo áp.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Digital_Multimeter_Aka.jpg&quot; alt=&quot;Đồng hồ cầm tay; lỗ cắm quan trọng ngang vạch chọn&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Dù bạn dùng UNI-T UT33D+ hay Wadfow, thiết bị đều đạt yêu cầu miễn là có còi thông mạch và đo được điện áp một chiều. Hãy luôn kiểm chứng bằng tiếng bíp chập que trước khi tin vào bất kỳ con số đo nào trên robot.&lt;/p&gt;

&lt;h3 id=&quot;mỏ-hàn-giá-đỡ-và-thiếc-hàn&quot;&gt;Mỏ hàn, giá đỡ và thiếc hàn&lt;/h3&gt;

&lt;p&gt;Mối hàn tốt là sự liên kết nhiệt giữa pad đồng và chân linh kiện: bạn làm nóng cả hai chi tiết đó rồi mới đưa một lượng thiếc vừa đủ chảy vào mối nóng. Nếu làm chảy thiếc sẵn trên mũi mỏ rồi chấm lên chân nguội, mối hàn trông có vẻ đầy đặn nhưng rất dễ bong sau vài ngày. Mối hàn đạt chuẩn có bề mặt bóng, thiếc bám ướt đều cả hai mặt và không tạo cầu nối sang pad bên cạnh.&lt;/p&gt;

&lt;p&gt;Loại thiếc khuyến nghị trong khóa học là hợp kim Sn63/Pb37 đường kính 0,8 mm, đủ mảnh để thao tác gọn trên chân header. Giá đỡ mỏ hàn là trang bị bắt buộc: đặt mũi mỏ 60 W đang nóng lên dây điện hoặc mặt bàn rất dễ gây tai nạn. Luôn đeo kính bảo hộ khi cắt chân linh kiện vì mẩu chân cắt ra có thể bắn vào mắt, và mở cửa sổ hoặc dùng quạt thông gió để đẩy khói flux ra khỏi vùng thở. Chương 00 chưa yêu cầu thực hành hàn.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Soldering_iron.jpg&quot; alt=&quot;Mỏ không giá là ảnh của mối nguy, không phải bố trí bạn muốn&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Bức ảnh trên minh họa một chiếc mỏ hàn đặt trần không có giá đỡ — đây chính là trạng thái mất an toàn cần tránh. Mỏ hàn của bạn phải nằm vững trên giá đỡ mỗi khi bạn rời tay lấy thiếc.&lt;/p&gt;

&lt;h3 id=&quot;breadboard-không-dành-cho-dòng-động-cơ&quot;&gt;Breadboard không dành cho dòng động cơ&lt;/h3&gt;

&lt;p&gt;Breadboard không hàn hoạt động nhờ hệ thống lá lò xo bên dưới bề mặt nhựa: các lỗ trên cùng một hàng ngắn nối liền với nhau, còn rãnh giữa dùng để đặt IC bắc ngang qua. Lưu ý rằng thanh nguồn dọc mép trên nhiều loại breadboard 830 lỗ &lt;strong&gt;bị ngắt ở chính giữa&lt;/strong&gt;; nếu đo thông mạch từ đầu này sang đầu kia không kêu, đó là do khe ngắt cấu tạo chứ không phải lỗi.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/breadboard.jpg&quot; alt=&quot;Breadboard không hàn: ổn cho tín hiệu, sai cho dòng motor&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Breadboard hoạt động tốt khi một sợi dây cắm đo thông mạch từ đầu đến cuối hàng lỗ và cách ly hoàn toàn qua rãnh giữa. Tuy nhiên, tuyệt đối không dẫn dòng động cơ qua các lá lò xo này: động cơ TT khi kẹt trục rút dòng lớn, khiến điện trở tiếp xúc sinh nhiệt và gây sụt áp. Breadboard chỉ dành cho dây tín hiệu, còn dây động cơ phải đi vào trạm vít hoặc chân hàn của TB6612FNG.&lt;/p&gt;

&lt;h3 id=&quot;kìm-cắt-và-kìm-tuốt-dây&quot;&gt;Kìm cắt và kìm tuốt dây&lt;/h3&gt;

&lt;p&gt;Kìm cắt chân (cỡ 170 trong danh sách mua) dùng để cắt gọn chân linh kiện sau khi hàn; thao tác đạt khi vết cắt phẳng và mẩu chân được giữ lại không bắn lung tung. Nếu dùng kìm cắt để tuốt vỏ dây, lưỡi kìm rất dễ cứa vào lõi đồng khiến dây bị gãy ngầm về sau. Kìm tuốt dây chuyên dụng giúp bóc vỏ sạch mà không làm tổn thương lõi dẫn; tránh dùng dao rọc thay thế. Ngoài ra, dây cắm breadboard có thể đứt ngầm bên trong đầu bấm dù lớp nhựa bên ngoài vẫn nguyên vẹn, nên khi động cơ không chạy dù dây PWM trông bình thường, hãy kiểm tra thông mạch sợi dây đó trước tiên.&lt;/p&gt;

&lt;h3 id=&quot;các-thiết-bị-để-dành-cho-giai-đoạn-sau&quot;&gt;Các thiết bị để dành cho giai đoạn sau&lt;/h3&gt;

&lt;p&gt;Nguồn cấp có giới hạn dòng, máy phân tích logic (logic analyzer) và máy hiện sóng (oscilloscope) giúp gỡ lỗi nhanh hơn ở các dự án lớn, nhưng không thiết bị nào trong số này là cần thiết để hoàn thành Chương 00, và chúng cũng không thay thế được một sợi dây mass chung bị đấu thiếu. Hãy xếp chúng vào danh sách nâng cấp về sau.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành-kiểm-kê-bàn-làm-việc-theo-đợt-1&quot;&gt;Thực hành: kiểm kê bàn làm việc theo Đợt 1&lt;/h2&gt;

&lt;p&gt;Dọn gọn một góc bàn và bày ra toàn bộ các món thuộc Đợt 1 mà bạn thực sự đang sở hữu. Không mượn đồ từ nơi khác đặt vào chụp ảnh “cho đủ bộ”.&lt;/p&gt;

&lt;p&gt;Liệt kê đủ các mục sau vào sổ lab và đánh dấu &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;có&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thiếu&lt;/code&gt; cho từng món: đồng hồ vạn năng, mỏ hàn, giá đỡ, thiếc hàn, kìm cắt, breadboard, dây cắm, cáp USB truyền dữ liệu, mạch vi điều khiển, ít nhất một đèn LED và ít nhất một nút nhấn. Chụp ảnh toàn bộ bố trí và lưu vào sổ. Ở trường đoạn serial, ghi tên cổng nếu mạch nhận cổng khi chỉ cắm USB; ngược lại, ghi rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cáp chưa chứng minh — chưa có board&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cáp chưa chứng minh — không thấy cổng&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Nếu đã có đồng hồ, hãy kiểm tra ngay bằng cách chạm hai que đo ở thang thông mạch và ghi vào sổ “bíp trên mối nối ngắn đã biết”. Nếu không kêu bíp, đồng hồ chưa đủ tin cậy để sử dụng. Tuyệt đối không đo ohm trên mạch đang cắm điện. Nếu chưa có đồng hồ, ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thiếu đồng hồ&lt;/code&gt; và hoãn các bài đo của Chương 01 cho tới khi bổ sung đủ. Nếu có breadboard, hãy đo thông mạch một hàng lỗ và xác nhận hai bên rãnh giữa không thông nhau. Nếu có mỏ hàn, hãy kiểm tra giá đỡ đi kèm nhưng vẫn để mỏ rút điện.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kết quả cần đạt:&lt;/strong&gt; Một bức ảnh chụp thực tế, một danh sách rõ món nào còn thiếu, và xác nhận tiếng bíp của đồng hồ (hoặc ghi chú rõ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thiếu đồng hồ&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cách xử lý khi lệch chuẩn:&lt;/strong&gt; Không tính đồng hồ mượn của bạn bè vào ảnh kiểm kê. Nếu lỡ đo thông mạch trên mạch đang cắm điện, hãy rút nguồn ngay trước khi đo lại. Rút mọi dây động cơ nếu đang cắm trên breadboard. Bạn chỉ cần một đồng hồ vạn năng đạt chuẩn, không cần mua hai chiếc.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-sẵn&quot;&gt;Ví dụ làm sẵn&lt;/h2&gt;

&lt;p&gt;Ảnh kiểm kê của Quân có: đồng hồ Wadfow, mỏ hàn 60 W nhưng chưa có giá đỡ, một cuộn thiếc 0,8 mm, kìm cắt, chưa có breadboard, một sợi cáp USB chỉ sạc chưa kiểm chứng và chưa có vi điều khiển. Anh liệt kê các món thiếu: giá đỡ mỏ hàn, breadboard, dây cắm, cáp USB dữ liệu, vi điều khiển, LED và nút nhấn. Anh chạm hai que đo của đồng hồ và nghe tiếng bíp rõ, nên ghi dòng đồng hồ là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;có&lt;/code&gt; kèm bằng chứng “bíp trên mối nối ngắn đã biết”, đồng thời không đo ohm trên cổng USB đang cắm laptop.&lt;/p&gt;

&lt;p&gt;Quân chỉ tính trước chi phí cho các dụng cụ còn thiếu theo mốc giá Hshop ngày 23 tháng 9 năm 2026 (còn vi điều khiển để tính gộp ở bài 00-05). Vì đã có mỏ hàn, thiếc và kìm cắt, phần dụng cụ cần mua thêm ngay hôm nay chỉ là giá đỡ và breadboard:&lt;/p&gt;

\[40000 + 35000 = 75000\]

&lt;p&gt;đồng, cộng thêm dây cắm, cáp dữ liệu và mạch vi điều khiển theo bài 00-05. Nếu bắt đầu từ con số 0 (chỉ chọn một đồng hồ), bộ dụng cụ cơ bản gồm: Wadfow 185.000 ₫ (hoặc UT33D+ 285.000 ₫), mỏ hàn 75.000 ₫, giá đỡ 40.000 ₫, thiếc 24.000 ₫, kìm cắt 35.000 ₫ và breadboard 830 lỗ 35.000 ₫. Với đồng hồ Wadfow, tổng chi phí là:&lt;/p&gt;

\[185000 + 75000 + 40000 + 24000 + 35000 + 35000 = 394000\]

&lt;p&gt;đồng; nếu chọn UNI-T UT33D+, tổng là 494.000 ₫. Quân ghi lại cả hai mức tổng kèm ngày khảo sát 23 tháng 9 năm 2026 và không mua máy hiện sóng.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=52w3xeXrMU8&quot;&gt;EEVblog 1636 - Ultimate Multimeter Beginners Guide!&lt;/a&gt; — EEVblog, tiếng Anh. Hướng dẫn sử dụng đồng hồ vạn năng cho người mới, giúp bạn nắm vững cách chọn thang đo trước khi mua thêm dụng cụ.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=rK38rpUy568&quot;&gt;iFixit’s Soldering 101: Beginners Guide&lt;/a&gt; — iFixit, tiếng Anh. Giới thiệu tổng quan mỏ hàn và thiếc trong danh mục dụng cụ; kỹ thuật hàn chi tiết sẽ có bài riêng ở Chương 01.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Khi chạm hai que đo vào nhau ở thang thông mạch nhưng đồng hồ im lặng, bạn có nên dùng nó đo chân 3,3 V rồi tin kết quả đó không? Cần kiểm tra những gì trước?&lt;/li&gt;
  &lt;li&gt;Bạn muốn đo trị số của một điện trở 10 kΩ đang gắn trên đường nguồn của mạch ESP32 đang cấp điện. Vì sao phép đo này không được phép thực hiện, và cách đo đúng là gì?&lt;/li&gt;
  &lt;li&gt;Một mối hàn có hình dạng như giọt cầu nằm tròn trên chân linh kiện. Người hàn đã làm sai ở khâu nào (làm nóng mối hay đưa thiếc)? Thiếc Sn63 đường kính 0,8 mm hỗ trợ kiểm soát mối hàn như thế nào?&lt;/li&gt;
  &lt;li&gt;Có người cắm tạm dây động cơ đang kẹt trục vào lỗ breadboard vì ngại lấy tua-vít vặn vào trạm vít. Sự cố điện nào sẽ xảy ra, và dây động cơ cần được đấu vào đâu?&lt;/li&gt;
  &lt;li&gt;Ảnh kiểm kê Đợt 1 của bạn có đồng hồ và mỏ hàn, nhưng thiếu giá đỡ, thiếc hàn và kìm cắt. Món thiếu nào có thể gây cháy nổ hoặc bỏng ngay cả trước khi bước sang Chương 01?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Không. Đồng hồ không kêu bíp khi chập que chứng tỏ đường đo chưa thông. Cần kiểm tra lại vị trí núm xoay, lỗ cắm que đo và pin của đồng hồ trước khi đo điện áp.
2. Thang đo ohm tự bơm dòng thử nên sẽ xung đột với nguồn đang cấp trên mạch. Cần rút nguồn mạch, nhấc một chân điện trở ra khỏi mạch (nếu cần) rồi mới đo ở trạng thái mất điện.
3. Chân linh kiện và pad đồng chưa được làm nóng đủ; thiếc bị chấm từ mũi mỏ sang nên không bám ướt. Dây thiếc mảnh 0,8 mm giúp bạn cấp lượng thiếc nhỏ, đều trực tiếp vào mối nóng thay vì làm ngập thiếc.
4. Tiếp điểm lò xo của breadboard có điện trở tiếp xúc đáng kể đối với dòng động cơ, dẫn đến nóng chảy nhựa và sụt áp. Dây động cơ phải nối vào trạm vít của mạch lái hoặc đầu nối chịu dòng phù hợp; breadboard chỉ dùng cho đường tín hiệu.
5. Thiếu giá đỡ mỏ hàn. Đặt mỏ hàn nóng trực tiếp lên mặt bàn hoặc lên dây điện gây nguy cơ bỏng và cháy ngay lập tức. Thiếu thiếc hay kìm cắt chỉ làm gián đoạn thao tác, không thay thế được vai trò an toàn của giá đỡ.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-use-a-multimeter&quot;&gt;Cách dùng đồng hồ vạn năng (SparkFun)&lt;/a&gt; — hướng dẫn trực quan về đo điện áp, đo thông mạch và cách tránh bẫy lỗ đo dòng.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.arduino.cc/&quot;&gt;Tài liệu Arduino&lt;/a&gt; — không phải cẩm nang dụng cụ, mà là nơi bạn sẽ thực hành LED và nút nhấn trong danh sách Đợt 1 khi có mạch; không nên bắt đầu ngay bằng các bài hướng dẫn điều khiển động cơ từ trang chủ.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Chỉ mua những món còn thiếu từ bảng kiểm kê của bạn, tránh mua trùng. Danh sách vật tư đầy đủ cho robot vẫn nằm ở &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;bài 00-05&lt;/a&gt;. Bảng giá tham khảo dưới đây được ghi nhận từ Hshop ngày &lt;strong&gt;23 tháng 9 năm 2026&lt;/strong&gt;:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Dụng cụ&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Giá tham khảo&lt;/th&gt;
      &lt;th&gt;Đường dẫn&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ UNI-T UT33D+&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;285.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UT33D+&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đồng hồ Wadfow WDM1501&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;185.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-ky-thuat-so-vom-wadfow-wdm1501-digital-multimeter-true-rms&quot;&gt;WDM1501&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mỏ hàn Wadfow 60 W&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;75.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;WEL3616&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Giá đỡ mỏ hàn tròn&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;40.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;giá đỡ&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thiếc Sunchi 0,8 mm Sn63/Pb37&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;24.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/thiec-han-sunchi-kp26-0-8mm-sn63-pb37-solder-wire&quot;&gt;thiếc hàn&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Kìm cắt 170&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/kim-cat-day-dien-nho-170-cat-chan-linh-kien-dien-tu&quot;&gt;kìm cắt&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Breadboard 830 lỗ&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35.000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;breadboard&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Bạn chỉ cần chọn một trong hai mẫu đồng hồ trên. Với dây cắm, LED, nút nhấn và cáp USB truyền dữ liệu, bạn có thể tìm qua các trang tìm kiếm sau: &lt;a href=&quot;https://shopee.vn/search?keyword=d%C3%A2y%20c%E1%BA%AFm%20breadboard&quot;&gt;Shopee, dây cắm breadboard&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=d%C3%A2y%20jumper%20%C4%91%E1%BB%B1c%20%C4%91%E1%BB%B1c&quot;&gt;Lazada, dây jumper đực đực&lt;/a&gt;, và nếu cần kìm tuốt dây riêng thì xem &lt;a href=&quot;https://shopee.vn/search?keyword=k%C3%ACm%20tu%E1%BB%91t%20d%C3%A2y&quot;&gt;Shopee, kìm tuốt dây&lt;/a&gt;. Ngoài ra, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; và &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; là hai trang chủ cửa hàng linh kiện thuận tiện nếu bạn đến mua trực tiếp tại quầy. Chưa cần đưa máy hiện sóng, máy phân tích logic hay nguồn bàn giới hạn dòng vào hóa đơn giai đoạn này.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Tư duy an toàn cho lab robot</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_03_safety_mindset_lab/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_03_safety_mindset_lab</id>
   <content type="html">&lt;p&gt;Hãy dành khoảng 80 phút cho bài học này. Bạn hoàn toàn có thể hoàn thành bài trên giấy trước khi linh kiện giao tới. Nếu trên bàn của bạn đã có sẵn mỏ hàn hoặc cell pin lithium, mọi quy tắc trong bài cần được áp dụng ngay lập tức.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ &lt;strong&gt;gọi tên&lt;/strong&gt; được ba mối nguy thực tế trên bàn làm việc: năng lượng tích trữ, bánh xe đang quay và firmware quên ngắt PWM. Bạn sẽ &lt;strong&gt;viết&lt;/strong&gt; một thẻ quy trình cấp nguồn gọn trên một trang theo đúng thứ tự: nối mass chung, cấp nguồn logic từ USB, nối tín hiệu điều khiển, và cuối cùng mới cấp nguồn VM cho động cơ khi bánh xe đã nhấc khỏi mặt bàn. Bạn sẽ &lt;strong&gt;áp dụng&lt;/strong&gt; nguyên tắc an toàn pin lithium: sạc cell 18650 riêng từng viên đến 4,2 V, tuyệt đối không kẹp mạch TP4056 vào đế pin 2S (8,4 V khi đầy). Bạn cũng sẽ &lt;strong&gt;loại bỏ&lt;/strong&gt; mọi phương án đưa điện áp động cơ hay tín hiệu echo 5 V trực tiếp vào chân 3,3 V vì giới hạn cực đại tuyệt đối của GPIO ESP32 không chịu được 5 V; đồng thời &lt;strong&gt;thực hiện&lt;/strong&gt; danh sách kiểm tra cấp nguồn chỉ bằng USB (không gắn động cơ) và &lt;strong&gt;đối chiếu&lt;/strong&gt; sự cố với bảng xử lý lỗi.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-trước&quot;&gt;Cần có trước&lt;/h2&gt;

&lt;p&gt;Từ bài 00-01 và 00-02, bạn đã nắm rõ quy tắc dừng PWM sau 300 ms và đã chốt Lộ trình A hoặc B trong sổ lab. Lúc này chưa cần có sẵn linh kiện. Hãy ghi nhớ hai nguyên tắc nền tảng: chân GPIO chỉ dùng cho tín hiệu logic chứ không phải nguồn công suất, và một bánh xe còn chạm mặt bàn có thể kéo cả robot trượt đi ngay khi xung PWM khác 0.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Một viên pin Li-ion 18650 nhỏ gọn nằm lọt trong lòng bàn tay trông có vẻ vô hại — cho đến khi hai sợi dây đỏ và đen vô tình chạm nhau trên mặt bàn lộn xộn vụn thiếc hàn. Trong chưa đầy một giây, dòng điện vọt qua ngưỡng hàng chục ampe, lớp vỏ nhựa bốc khói và mạch vi điều khiển cháy rụi! &lt;strong&gt;Tư duy an toàn phòng lab&lt;/strong&gt; không phải là danh sách nội quy nhàm chán; đó là kỹ năng phòng thủ bằng vật lý giúp bạn bảo vệ đôi mắt, bàn tay và chính ví tiền của mình.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-vật-lý-của-sự-cố-điện-và-an-toàn-pin-lithium&quot;&gt;Kiến thức cần nhớ: Vật lý của Sự cố Điện và An toàn Pin Lithium&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Ngắn mạch &amp;amp; Công suất nhiệt Joule (\(P = I^2 R = V^2 / R\)):&lt;/strong&gt; Khi hai cực nguồn chạm trực tiếp, điện trở mạch giảm xuống chỉ còn nội trở pin \(r\) (vài chục mili-ohm), đẩy dòng điện và nhiệt lượng tăng vọt theo bình phương dòng điện!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;An toàn Hóa học Pin Lithium (Li-ion / LiPo):&lt;/strong&gt; Pin lithium có mật độ năng lượng rất cao và chất điện phân dễ cháy. Xả sâu dưới \(3.0\text{ V/cell}\), sạc quá \(4.2\text{ V/cell}\) hoặc ngắn mạch đều có thể gây phồng pin và thoát nhiệt mất kiểm soát (&lt;em&gt;thermal runaway&lt;/em&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trình tự cấp nguồn phân tầng (Power-On Sequence):&lt;/strong&gt; Luôn kiểm tra thông mạch nguồn–GND ở trạng thái tắt pin, cấp nguồn qua cổng USB giới hạn dòng 500 mA trước, rồi mới đóng cầu chì và bật công tắc pin động cơ.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-dòng-ngắn-mạch-của-bộ-pin-2s-18650&quot;&gt;Ví dụ làm từng bước: Tính dòng ngắn mạch của bộ pin 2S 18650&lt;/h3&gt;

&lt;p&gt;Xét một bộ pin 2S Li-ion sạc đầy có điện áp \(V = 2 \times 4.2\text{ V} = \mathbf{8.4\text{ V}}\) và tổng nội trở (2 cell + dây dẫn ngắn) chỉ khoảng \(r = 0.08\ \Omega\). Nếu hai đầu dây nguồn vô tình chập nhau không qua cầu chì:&lt;/p&gt;

\[I_{\text{ngắn mạch}} = \frac{V}{r} = \frac{8.4\text{ V}}{0.08\ \Omega} = \mathbf{105\text{ A}},\]

\[P_{\text{nhiệt}} = V \cdot I = 8.4 \times 105 = \mathbf{882\text{ W}}!\]

&lt;p&gt;Gần &lt;strong&gt;900 watt&lt;/strong&gt; nhiệt tỏa ra ngay trong hai viên pin và sợi dây đồng mảnh — tương đương một chiếc bàn ủi điện dồn vào một điểm tiếp xúc 2 mm! Giờ lắp nối tiếp một &lt;strong&gt;cầu chì 5 A&lt;/strong&gt; sát cực dương pin: ngay khi dòng vượt 10 A, dây chì chảy đứt trong vài mili-giây, ngắt hoàn toàn mạch trước khi pin kịp nóng lên!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-quy-trình-4-bước-cấp-nguồn-an-toàn-không-bao-giờ-bỏ-qua&quot;&gt;Giải thích: Quy trình 4 bước cấp nguồn an toàn không bao giờ bỏ qua&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Dọn sạch mặt bàn kim loại trước khi mở công tắc:&lt;/strong&gt; Vụn chân linh kiện, tua-vít hay kẹp kim loại nằm dưới gầm bo mạch là nguyên nhân số 1 gây chập mạch bất ngờ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Đo thông mạch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC–GND&lt;/code&gt; khi chưa có điện:&lt;/strong&gt; Dùng thang đo thông mạch/Ohm của đồng hồ vạn năng; nếu kêu bíp liên tục (\(R &amp;lt; 5\ \Omega\)), tuyệt đối &lt;em&gt;không&lt;/em&gt; cấp nguồn cho đến khi tìm ra điểm chạm!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Kiểm tra cực tính bằng màu dây chuẩn hóa:&lt;/strong&gt; Quy ước cứng Đỏ = Dương (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;), Đen = Âm (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;); chỉ cần cắm ngược cực 0,5 giây là đủ phá hủy IC ổn áp không có diode bảo vệ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Sờ nhanh kiểm tra nhiệt sau 5 giây đầu:&lt;/strong&gt; Sau khi bật nguồn, chạm nhẹ mu bàn tay gần IC ổn áp và driver động cơ; nếu nóng rát bất thường, tắt công tắc ngay lập tức!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Một cell pin 18650 đang đo được \(2.6\text{ V}\) (thấp hơn ngưỡng xả sâu \(3.0\text{ V}\)). Bạn có nên cắm ngay vào bộ sạc nhanh 2 A để “cứu” pin không? &lt;strong&gt;Tuyệt đối không!&lt;/strong&gt; Sạc dòng cao vào cell xả kiệt có thể tạo nhánh kim loại lithium (&lt;em&gt;dendrite&lt;/em&gt;) gây chập ngầm bên trong và cháy nổ — cần loại bỏ an toàn hoặc chỉ phục hồi cực chậm bằng bộ sạc chuyên dụng có giám sát nhiệt.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-liền-với-capstone&quot;&gt;Vì sao bài này gắn liền với Capstone&lt;/h2&gt;

&lt;p&gt;Bản demo Chương 07 chỉ đáng tin cậy khi xe chạy theo lệnh và dừng ngay khi mất lệnh. Điều đó trở nên vô nghĩa nếu ngay lần cấp nguồn đầu tiên, xe lao khỏi bàn hoặc điện áp pin động cơ đi thẳng vào chân 3,3 V. Cơ chế timeout chỉ hoạt động khi vi điều khiển còn sống và đang chạy chương trình; các topic ROS 2 về sau cũng không thể cứu một chân mạch đã bị cháy.&lt;/p&gt;

&lt;h2 id=&quot;ba-mối-nguy-chính-và-trình-tự-cấp-nguồn-an-toàn&quot;&gt;Ba mối nguy chính và trình tự cấp nguồn an toàn&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Năng lượng tích trữ&lt;/strong&gt; luôn sẵn sàng xả dòng lớn mà không báo trước, từ cổng USB, pin AA, cặp cell 18650 cho tới tụ điện trên mạch lái. Sự cố biểu hiện thành nhiệt lượng phá hủy mạch in, chân IC hoặc làm nóng phồng cell pin bị sạc sai quy cách. &lt;strong&gt;Bánh xe đang quay&lt;/strong&gt; có thể làm giật cáp hoặc kéo khung xe rơi khỏi bàn ngay khi PWM khởi tạo sai. &lt;strong&gt;Firmware quên ngắt PWM&lt;/strong&gt; xảy ra khi chương trình teleop bị treo sau lệnh “tiến”, cửa sổ serial bị đóng đột ngột hoặc laptop chuyển sang chế độ ngủ. Quy tắc 300 ms giúp chuyển trạng thái mất lệnh thành lệnh dừng, nhưng nó không bảo vệ được khoảnh khắc trước khi chương trình bắt đầu chạy và cũng không bảo vệ chân GPIO khỏi quá áp.&lt;/p&gt;

&lt;p&gt;Trình tự cấp nguồn an toàn chỉ gói gọn trong một trang giấy và cần thực hiện mỗi lần bật mạch, kể cả khi bạn chỉ muốn “thử nhanh”:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Mass chung (GND) trước:&lt;/strong&gt; nối chung mass của vi điều khiển, mass của mạch lái và cực âm pin thành một điểm tham chiếu chung vì dòng động cơ sẽ quay về đây. Nếu nối dây tín hiệu trước khi có mass chung, dòng điện có thể tìm đường hồi lưu qua chân GPIO và làm hỏng chip.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nguồn logic tiếp theo:&lt;/strong&gt; cắm USB vào vi điều khiển để tạo đường nguồn 3,3 V ổn định. Nguồn logic của mạch lái phải lấy từ đường nguồn ổn định này, không lấy từ pin động cơ.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tín hiệu điều khiển:&lt;/strong&gt; nối các đường PWM, chiều quay và chân standby (đảm bảo firmware đang đặt ở trạng thái dừng).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Nguồn động cơ VM sau cùng:&lt;/strong&gt; đóng công tắc cấp VM cuối cùng, sau khi đã chắc chắn bánh xe được nhấc khỏi mặt bàn. Nhấc bánh sau khi xe đã giật chạy luôn là quá muộn.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Nguồn lên theo mass chung, logic, tín hiệu, nguồn motor cuối, bánh khỏi mặt bàn&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy chép các bước đúng theo thứ tự trong hình. “Sau cùng” nghĩa là công tắc nguồn VM luôn đóng cuối cùng khi bánh xe đã ở trên không.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_rails_3v3.png&quot; alt=&quot;Logic 3,3 V, USB 5 V, mass, và ray motor riêng&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy xem sơ đồ màu dây như ranh giới cách ly: nguồn USB 5 V nuôi mạch vi điều khiển chứ không nuôi hai động cơ TT đang kẹt trục; GPIO thuộc miền logic 3,3 V; mass là điểm nối chung có chủ đích; còn đường nguồn động cơ (VM) lấy từ pin riêng và tuyệt đối không được nối vào chân GPIO hay chân 3,3 V.&lt;/p&gt;

&lt;h2 id=&quot;an-toàn-pin-lithium&quot;&gt;An toàn pin lithium&lt;/h2&gt;

&lt;p&gt;Một cell Li-ion 18650 có điện áp danh định khoảng 3,6–3,7 V và &lt;strong&gt;đạt 4,2 V khi sạc đầy&lt;/strong&gt;. Bạn phải sạc &lt;strong&gt;từng cell riêng lẻ&lt;/strong&gt; tới 4,2 V bằng bộ sạc ghi rõ dùng cho Li-ion / Li-ion 18650. Khi mắc nối tiếp hai cell trong đế 2S, điện áp danh định là 7,4 V và &lt;strong&gt;đạt 8,4 V khi đầy&lt;/strong&gt;. Cụm nối tiếp đó dùng để cấp cho chân VM của mạch lái, không phải đầu vào của mạch sạc một cell.&lt;/p&gt;

&lt;p&gt;Mạch sạc TP4056 được thiết kế để nạp một cell đơn lên 4,2 V. Kẹp TP4056 vào hai đầu đế 2S đồng nghĩa đặt mạch sạc 4,2 V lên bộ pin 8,4 V — đây là thao tác sai và nguy hiểm. Hãy tháo từng cell ra, sạc riêng lẻ rồi mới lắp lại vào đế.&lt;/p&gt;

&lt;p&gt;Nếu một cell đo được khoảng 2,1 V, nó đã tụt sâu dưới ngưỡng cắt an toàn thông thường (khoảng 2,5–3,0 V). Cố sạc lại ở dòng bình thường có thể gây kết tủa kim loại bên trong và dẫn đến ngắn mạch nội. Tuyệt đối không dùng TP4056 để “kích pin”, mà hãy đưa cell đó đi tái chế. Với cell bị phồng vỏ, pin đã hỏng: không sạc, không chọc thủng và không cất giữ “phòng khi cần”. Không để cell 18650 trần trong túi cùng chìa khóa hay vật kim loại vì dễ gây chập cực tức thời; pin luôn phải nằm trong đế hoặc hộp bảo quản. Trong những buổi thử động cơ đầu tiên, bạn hoàn toàn có thể dùng hộp pin AA để làm quen với việc tách nguồn VM khỏi nguồn logic trước khi chuyển sang pin lithium. Chưa mua pin luôn là trạng thái an toàn hơn việc dùng bộ sạc không khớp nhãn.&lt;/p&gt;

&lt;h2 id=&quot;bảo-vệ-chân-mạch-mỏ-hàn-và-mắt&quot;&gt;Bảo vệ chân mạch, mỏ hàn và mắt&lt;/h2&gt;

&lt;p&gt;Điện áp cực đại tuyệt đối trên chân GPIO của ESP32 &lt;strong&gt;không&lt;/strong&gt; phải 5 V; giới hạn của pad chỉ cao hơn đường nguồn 3,3 V vài phần mười vôn, hoàn toàn không chịu được mức 5 V hay điện áp pin động cơ. Chân GPIO của Raspberry Pi Pico cũng thuộc nhóm logic 3,3 V tương tự. Vì chân echo của HC-SR04 xuất mức cao khoảng 5 V còn pin động cơ thường ở mức 7–8 V, đường echo bắt buộc phải đi qua mạch chuyển mức logic, còn pin chỉ được nối vào chân VM.&lt;/p&gt;

&lt;p&gt;Luôn đeo kính bảo hộ khi cắt chân linh kiện và khi hàn, vì mẩu chân linh kiện cắt ra có thể bắn mạnh vào mắt. Khói nhựa thông (rosin) gây kích ứng nên cần làm việc ở nơi thông gió tốt, tránh cúi mặt trực tiếp phía trên mối hàn. Mỏ hàn đang nóng phải luôn đặt trên giá đỡ, không đặt lên dây điện hay mặt bàn. Hôm nay bạn chưa cần bật mỏ hàn, chỉ cần ghi rõ các quy tắc này ở vị trí dễ thấy nhất trên bàn làm việc.&lt;/p&gt;

&lt;p&gt;Phần bảo vệ bằng phần mềm vẫn là quy tắc timeout đã học ở bài trước:&lt;/p&gt;

\[t_{\mathrm{silence}} &amp;gt; 300\,\mathrm{ms} \implies \text{duty PWM} = 0.\]

&lt;p&gt;Hôm nay bạn chưa nạp đoạn mã này, nhưng cần nhớ rằng một bố trí không được coi là sẵn sàng nếu cách duy nhất để dừng robot là rút dây USB sau khi xe đã chạy khỏi thảm. Khi mất tín hiệu điều khiển, xe bắt buộc phải dừng thay vì duy trì tốc độ cũ.&lt;/p&gt;

&lt;h2 id=&quot;bảng-nhận-diện-lỗi-trước-khi-gặp&quot;&gt;Bảng nhận diện lỗi trước khi gặp&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tình huống thấy hoặc sắp làm&lt;/th&gt;
      &lt;th&gt;Nguyên nhân thường gặp&lt;/th&gt;
      &lt;th&gt;Cách xử lý&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cổng serial biến mất khi nối động cơ vào chân 5 V của mạch&lt;/td&gt;
      &lt;td&gt;Cổng USB không cấp nổi dòng khi hai động cơ kẹt; đường 3,3 V sụp áp&lt;/td&gt;
      &lt;td&gt;Tách động cơ sang nguồn VM riêng, giữ mass chung, chỉ dùng USB cho phần logic&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chân GPIO hỏng sau khi nối trực tiếp echo siêu âm&lt;/td&gt;
      &lt;td&gt;Đưa mức 5 V vào chân không chịu được 5 V&lt;/td&gt;
      &lt;td&gt;Dừng ngay; chân có thể đã hỏng; lắp mạch chuyển mức cho đường echo trước khi thử lại&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạch TP4056 kẹp lên hai đầu đế pin 2S&lt;/td&gt;
      &lt;td&gt;Đặt mạch sạc 4,2 V lên bộ pin 8,4 V&lt;/td&gt;
      &lt;td&gt;Ngắt kết nối ngay; tháo từng cell ra sạc riêng ngoài robot&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cell 18650 đo khoảng 2,1 V hoặc vỏ bị phồng&lt;/td&gt;
      &lt;td&gt;Pin xả quá sâu hoặc đã hư hại bên trong&lt;/td&gt;
      &lt;td&gt;Không sạc, không bỏ túi; đưa đi tái chế&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Để bánh chạm bàn khi nạp firmware mới “thử một chút”&lt;/td&gt;
      &lt;td&gt;Chuyển động ngoài ý muốn vì PWM có thể khác 0 trước khi vào vòng lặp&lt;/td&gt;
      &lt;td&gt;Nhấc bánh khỏi bàn trước; cấp VM sau cùng; vẫn giữ timeout trong mã nguồn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đặt mỏ hàn đè lên dây hoặc cắt chân linh kiện không đeo kính&lt;/td&gt;
      &lt;td&gt;Gây bỏng, chảy vỏ dây hoặc mẩu chân linh kiện bắn vào mắt&lt;/td&gt;
      &lt;td&gt;Dùng giá đỡ, đeo kính bảo hộ, đảm bảo thông gió; rút điện nếu bố trí chưa đạt&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;thực-hành-viết-thẻ-cấp-nguồn-và-kiểm-tra-chỉ-bằng-usb&quot;&gt;Thực hành: viết thẻ cấp nguồn và kiểm tra chỉ bằng USB&lt;/h2&gt;

&lt;p&gt;Thực hiện bài lab này khi &lt;strong&gt;không gắn động cơ vào mạch&lt;/strong&gt;. Nếu chưa có mạch vi điều khiển, bạn vẫn viết thẻ quy trình và ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa có board&lt;/code&gt; vào vị trí tên cổng serial.&lt;/p&gt;

&lt;p&gt;Lấy một tờ giấy rời, đặt tiêu đề &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LÊN NGUỒN&lt;/code&gt; và ghi bốn bước theo đúng thứ tự: (1) Mass chung (GND), (2) Nguồn logic từ USB, (3) Tín hiệu PWM và chiều quay ở trạng thái dừng, (4) Nguồn động cơ VM cuối cùng. Phía dưới ghi thêm bốn dòng cảnh báo: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NHẤC BÁNH TRƯỚC KHI CẤP VM&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MẤT LỆNH &amp;gt; 300 ms → PWM = 0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CẤM: đưa nguồn pin hoặc echo 5 V vào GPIO 3,3 V&lt;/code&gt;, và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;18650: sạc từng cell tới 4,2 V, không kẹp TP4056 qua đế 2S&lt;/code&gt;. Dán tờ giấy này ngay trước khu vực làm việc hoặc đặt cạnh bàn phím.&lt;/p&gt;

&lt;p&gt;Sau đó, thực hiện kiểm tra cấp nguồn chỉ bằng USB theo đúng trình tự:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Kiểm tra mặt bàn để chắc chắn không có động cơ, không có pin động cơ và không có dây VM. Ngắt hoàn toàn pin trước khi cắm USB; nếu đã có bánh xe, không để chúng nối với mạch lái đang cấp điện.&lt;/li&gt;
  &lt;li&gt;Chỉ cắm vi điều khiển vào máy tính bằng cáp USB truyền dữ liệu. Kiểm tra xem cổng serial mới (hoặc ổ đĩa BOOTSEL nếu dùng Pico) có xuất hiện không.&lt;/li&gt;
  &lt;li&gt;Dán tên cổng vào sổ lab; nếu chưa có mạch, ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;không có — chưa có board&lt;/code&gt; kèm kế hoạch: “khi có mạch sẽ chỉ cắm USB, không nối VM, ghi lại tên cổng rồi rút ra”. Sau khi kiểm tra xong thì rút cáp, không vội gắn động cơ.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nếu bạn đã có sẵn cell lithium, hãy chụp ảnh nhãn bộ sạc và ghi lại chính xác thông số hóa học pin trên nhãn; nếu bộ sạc mất nhãn thì ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sẽ không sạc&lt;/code&gt;. Nếu chưa có pin, ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa mua cell&lt;/code&gt;. Nếu đã có mỏ hàn, hãy kiểm tra chắc chắn đã có giá đỡ trước khi nghĩ đến việc cắm điện, đồng thời ghi chú kính bảo hộ đang có sẵn hay còn thiếu.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kết quả cần đạt:&lt;/strong&gt; Thẻ quy trình đặt ở chỗ nhìn thấy ngay mà không cần mở lại bài học. Sổ lab có ảnh chụp hoặc bản phác thẻ, có tên cổng serial thực tế (hoặc kế hoạch cụ thể), và có trạng thái sạc pin rõ ràng (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khớp&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sẽ không sạc&lt;/code&gt; hoặc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa mua cell&lt;/code&gt;). Toàn bộ bước USB không có sự xuất hiện của động cơ.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cách xử lý khi lệch chuẩn:&lt;/strong&gt; Nếu trên thẻ có mục cấp VM trước, hãy viết lại ngay. Luôn nhấc bánh cho mọi lần thử firmware mới đầu tiên. Mọi hướng dẫn lấy nguồn chân 5 V của DevKit để chạy động cơ đều xếp vào mục cấm. Nếu bộ sạc đang kẹp vào đế 2S chỉ vì vừa giắc, hãy tháo ra ngay và sạc từng cell riêng lẻ.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-sẵn&quot;&gt;Ví dụ làm sẵn&lt;/h2&gt;

&lt;p&gt;Một sinh viên định để bánh xe chạm mặt bàn “chỉ để thử nhanh đoạn mã mới”. Tuy nhiên, PWM chưa được kiểm chứng là khởi tạo ở mức 0, và bộ đếm timeout 300 ms chưa hoạt động trước khi hàm khởi tạo chạy xong. Cách làm đúng là giữ bánh nhấc khỏi bàn, ngắt nguồn VM, chỉ cấp nguồn logic qua USB trước, rồi mới chạy thử lần đầu ở trạng thái bánh trên không để tránh việc xe giật rơi khỏi bàn.&lt;/p&gt;

&lt;p&gt;Trong cùng buổi chiều, một viên 18650 đo được 2,1 V và có người định dùng TP4056 để “hồi sức”. Vì cell đã tụt sâu dưới ngưỡng an toàn, sổ lab ghi rõ không sạc, không bỏ túi và không kẹp lên đế 2S; chỉ những cell còn tốt mới được sạc riêng lẻ đến 4,2 V rồi mới lắp vào đế.&lt;/p&gt;

&lt;p&gt;Cũng trên bàn đó, sợi dây echo từ HC-SR04 suýt được cắm thẳng vào chân GPIO của ESP32. Do giới hạn cực đại tuyệt đối của pad không phải 5 V, dây này phải chờ lắp mạch chuyển mức logic — cùng nguyên tắc cấm như đối với điện áp pin động cơ.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; [What is ESD and How to Prevent it – ATM&lt;/td&gt;
          &lt;td&gt;Digi-Key Electronics](https://www.youtube.com/watch?v=pWhtyz-UFZc) — DigiKey, tiếng Anh. Giải thích về phóng tĩnh điện (ESD) và lý do chạm tay vào linh kiện khi chưa xả tĩnh điện có thể làm hỏng mạch trước khi robot kịp chạy.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=W770Z1yvXs8&quot;&gt;What is a fuse? the basics explained&lt;/a&gt; — The Engineering Mindset, tiếng Anh. Giải thích vai trò của cầu chì trên đường nguồn, đặt song song với quy tắc ngắt nguồn an toàn trong bài.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;p&gt;Các câu hỏi dưới đây là tình huống thực tế; hãy nêu hành động xử lý cụ thể thay vì chép lại định nghĩa.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Một bạn đặt khung xe trên bàn, bánh vẫn chạm mặt bàn và muốn nạp firmware “chỉ một giây”. Những điều kiện nào phải đảm bảo trước khi đóng nguồn VM, và vì sao quy tắc dừng 300 ms không đủ bảo vệ cho lần nạp đầu tiên?&lt;/li&gt;
  &lt;li&gt;Một cell 18650 đo được 2,1 V và có người đưa mạch TP4056 cho bạn. Bạn phải làm gì với cell đó và từ chối làm gì với đế pin 2S?&lt;/li&gt;
  &lt;li&gt;Dây echo của HC-SR04 sắp được cắm thẳng vào chân GPIO ESP32. Điện áp của dây này ở mức cao là bao nhiêu, và vì sao nhận xét “ESP32 chịu tốt” không phải là thông số kỹ thuật?&lt;/li&gt;
  &lt;li&gt;Bạn phát hiện mạch TP4056 đang kẹp vào đế chứa hai cell 18650 mắc nối tiếp (dùng cho nguồn VM sau này). Bạn cần ngắt bộ phận nào ngay lập tức, và nếu các cell còn tốt thì quy trình sạc đúng là gì?&lt;/li&gt;
  &lt;li&gt;Robot đang chạy teleop thì laptop chuyển sang chế độ ngủ, trong khi firmware chưa có bộ đếm thời gian mất lệnh. Bánh xe sẽ phản ứng thế nào, và điều kiện gán nào bắt buộc phải có trong vòng lặp điều khiển?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Phải nhấc bánh khỏi mặt bàn, nối xong mass chung, nguồn logic rồi tín hiệu, và giữ VM hở cho tới khi chắc chắn bánh đang trên không. Quy tắc timeout chỉ chạy sau khi firmware đã khởi động và vào vòng lặp; trong lúc nạp hoặc reset, trạng thái chân có thể thay đổi bất ngờ khiến bánh kéo cả mạch rơi khỏi bàn.
2. Không sạc lại cell này, không cất trong túi, không kẹp TP4056 vào cell hay vào đế 2S; mang cell đi tái chế đúng quy định.
3. Chân echo ở mức cao khoảng 5 V, trong khi giới hạn cực đại tuyệt đối của GPIO ESP32 không phải 5 V. Dây này bắt buộc qua mạch chuyển mức; nối thẳng có thể làm hỏng chân vi điều khiển.
4. Ngắt TP4056 khỏi đế 2S ngay lập tức. Tháo từng cell lành ra sạc riêng lẻ tới 4,2 V ngoài robot, sau đó mới lắp vào đế 2S để cấp nguồn VM.
5. Bánh xe sẽ giữ nguyên lệnh chạy cuối cùng. Vòng lặp firmware bắt buộc phải đưa duty PWM về 0 khi khoảng thời gian mất lệnh vượt quá khoảng 300 ms.

&lt;/details&gt;

&lt;p&gt;Trước khi bước vào lắp khung ở Chương 07, bạn sẽ học tiếp bài &lt;a href=&quot;/contents/vi/chapter07/07_00_batteries_power_safety/&quot;&gt;Pin, nguồn và an toàn trước khi lắp Capstone&lt;/a&gt; để phân biệt chi tiết Li-ion 18650, LiPo và LiFePO4, tính chọn cầu chì và vẽ sơ đồ mạch hạ áp (buck). Thẻ một trang của buổi hôm nay vẫn là nguyên tắc bắt buộc, còn bài ở Chương 07 sẽ cụ thể hóa thành sơ đồ dây.&lt;/p&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;Mức logic, SparkFun&lt;/a&gt; — giải thích vì sao mức 5 V và 3,3 V không thể thay thế trực tiếp cho nhau, chính là bài toán của chân echo.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://batteryuniversity.com/article/bu-409-charging-lithium-ion&quot;&gt;Battery University, BU-409, sạc lithium-ion&lt;/a&gt; — trình bày nguyên lý ngưỡng đầy 4,2 V và lý do pin Li-ion cần quy trình sạc chuyên biệt.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mua-ở-việt-nam&quot;&gt;Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Danh sách mua sắm đầy đủ cho robot nằm ở &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;bài 00-05&lt;/a&gt;. Các mức giá dưới đây được ghi nhận từ Hshop ngày &lt;strong&gt;23 tháng 9 năm 2026&lt;/strong&gt;. Bạn chỉ cần mua một đồng hồ vạn năng: &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UNI-T UT33D+&lt;/a&gt; giá 285.000 ₫ hoặc &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-ky-thuat-so-vom-wadfow-wdm1501-digital-multimeter-true-rms&quot;&gt;Wadfow WDM1501&lt;/a&gt; giá 185.000 ₫. Mỏ hàn và giá đỡ phải đi thành một cặp: &lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;Wadfow 60 W&lt;/a&gt; giá 75.000 ₫ và &lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;giá đỡ tròn&lt;/a&gt; giá 40.000 ₫ — mua mỏ hàn mà thiếu giá đỡ là vi phạm đúng quy tắc an toàn vừa học. Thiếc hàn, kìm cắt và breadboard được liệt kê ở bài 00-04.&lt;/p&gt;

&lt;p&gt;Với kính bảo hộ và bộ sạc một cell, bạn có thể tra cứu qua các trang tìm kiếm sau: &lt;a href=&quot;https://shopee.vn/search?keyword=k%C3%ADnh%20b%E1%BA%A3o%20h%E1%BB%99&quot;&gt;Shopee, kính bảo hộ&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=k%C3%ADnh%20b%E1%BA%A3o%20h%E1%BB%99&quot;&gt;Lazada, kính bảo hộ&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=s%E1%BA%A1c%20pin%2018650&quot;&gt;Shopee, sạc pin 18650&lt;/a&gt; và &lt;a href=&quot;https://www.lazada.vn/catalog/?q=s%E1%BA%A1c%20pin%2018650&quot;&gt;Lazada, sạc pin 18650&lt;/a&gt;. Vì đây là các trang tìm kiếm chung, hãy đọc kỹ nhãn sản phẩm và chỉ chọn bộ sạc hỗ trợ nạp riêng từng cell Li-ion tới 4,2 V. Bạn cũng có thể tham khảo trang chủ của &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; và &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; khi tìm linh kiện. Đừng mua LiDAR trong giai đoạn này.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Hai lộ trình: MCU tiết kiệm và ROS</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_02_learning_tracks_budget/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_02_learning_tracks_budget</id>
   <content type="html">&lt;p&gt;Hãy dành khoảng một giờ cho bài học này. Bạn cần chuẩn bị sẵn phát biểu Capstone A từ bài chào mừng và thông tin chính xác về máy tính mình đang sử dụng. Các mức giá nêu trong bài chỉ nhằm giúp bạn hình dung khung ngân sách, không phải danh sách mua hàng; danh sách mua sắm chính thức nằm ở &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;bài 00-05&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ &lt;strong&gt;chọn&lt;/strong&gt; được Lộ trình A hoặc Lộ trình B bằng một phát biểu ngắn gọn nêu rõ hệ điều hành máy tính, hạn mức ngân sách bằng VND và mục tiêu trong hai tháng tới. Bạn sẽ &lt;strong&gt;xác định&lt;/strong&gt; được cấu hình phần cứng của robot Lộ trình A gồm ESP32 hoặc Pico 2, động cơ giảm tốc TT, mạch lái TB6612FNG (thay vì L298N) và cảm biến HC-SR04 — đủ để nghiệm thu Capstone A mà không cần Ubuntu. Bạn sẽ &lt;strong&gt;tính&lt;/strong&gt; được mức sụt áp khoảng 2 V của mạch cầu Darlington và phân tích tác động của nó lên bộ pin 7,4 V. Bạn cũng sẽ &lt;strong&gt;sắp xếp&lt;/strong&gt; Ubuntu 24.04, ROS 2 Jazzy, Gazebo Harmonic và micro-ROS vào giai đoạn sau khi robot đã chạy ổn định (Raspberry Pi chỉ là tùy chọn mở rộng về sau), đồng thời &lt;strong&gt;cam kết bằng văn bản&lt;/strong&gt; không mua LiDAR trước khi nghiệm thu thành công Capstone A.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-trước&quot;&gt;Cần có trước&lt;/h2&gt;

&lt;p&gt;Từ bài 00-01, bạn đã nắm được yêu cầu của bài teleop nhấc bánh, quy tắc ngắt PWM sau 300 ms và đã tạo sổ lab đủ sáu trường. Hiện tại bạn chưa cần Ubuntu, ROS hay đơn hàng linh kiện nào. Ngay cả khi đã có sẵn Raspberry Pi hoặc laptop cài song song Linux, bạn vẫn bắt đầu từ cùng một khung xe vi sai; có sẵn phần cứng mạnh không phải lý do để bỏ qua phần firmware cơ bản.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Rất nhiều người mới tự học robot vội vã mua ngay một bộ kit hàng chục triệu gồm máy tính nhúng đắt tiền và cảm biến LiDAR quay vù vù — rồi chỉ sau hai tuần cả bộ kit nằm phủ bụi vì chưa biết cách cấp nguồn 5 V ổn định cho vi điều khiển! Chọn đúng &lt;strong&gt;lộ trình học và ngân sách&lt;/strong&gt; không phải chuyện tiết kiệm đơn thuần, mà là bài toán &lt;strong&gt;kiến trúc hệ thống&lt;/strong&gt;: chọn đúng tầng phần cứng cho đúng bài toán điều khiển bạn cần giải.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kiến-trúc-vi-điều-khiển-mcu-vs-máy-tính-nhúng-sbc--ros&quot;&gt;Kiến thức cần nhớ: Kiến trúc Vi điều khiển (MCU) vs. Máy tính nhúng (SBC / ROS)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Lộ trình A — Vi điều khiển thuần (MCU như ESP32 / Pico):&lt;/strong&gt; Chạy mã sát phần cứng (bare-metal / RTOS), tiêu thụ điện cực thấp (~\(0.5\text{–}1\text{ W}\)), định thời chính xác tới micro-giây — lý tưởng cho điều khiển động cơ, đọc encoder, PID và máy trạng thái.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lộ trình B — Máy tính nhúng + ROS 2 (SBC như Raspberry Pi + MCU):&lt;/strong&gt; Chạy Linux, có RAM hàng GB để xử lý bản đồ 2D/3D (SLAM), thị giác máy tính và lập kế hoạch đường đi — nhưng vẫn &lt;em&gt;cần&lt;/em&gt; một MCU cấp thấp lo vòng lặp động cơ thời gian thực!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ngân sách năng lượng &amp;amp; Tải trọng (SWaP — Size, Weight, and Power):&lt;/strong&gt; Tăng sức mạnh tính toán luôn kéo theo tăng dòng điện, nặng pin hơn và đòi hỏi động cơ khỏe hơn — một vòng xoắn thiết kế cốt lõi trong mọi dự án robot.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-so-sánh-ngân-sách-điện-năng-giữa-lộ-trình-a-mcu-và-lộ-trình-b-sbc--lidar&quot;&gt;Ví dụ làm từng bước: So sánh ngân sách điện năng giữa Lộ trình A (MCU) và Lộ trình B (SBC + LiDAR)&lt;/h3&gt;

&lt;p&gt;Giả sử khung xe dùng chung 2 động cơ DC tiêu thụ trung bình &lt;strong&gt;\(3.0\text{ W}\)&lt;/strong&gt; khi chạy, cấp nguồn từ bộ pin Li-ion 2S dung lượng &lt;strong&gt;\(15\text{ Wh}\)&lt;/strong&gt; (hiệu suất mạch hạ áp \(\eta = 85\%\)):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Thành phần tiêu thụ&lt;/th&gt;
      &lt;th&gt;Lộ trình A (ESP32 thuần)&lt;/th&gt;
      &lt;th&gt;Lộ trình B (Raspberry Pi 4 + LiDAR + ESP32)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Mạch điều khiển &amp;amp; cảm biến&lt;/td&gt;
      &lt;td&gt;ESP32 + IMU: \(0.15\text{ A} \times 5\text{ V} = \mathbf{0.75\text{ W}}\)&lt;/td&gt;
      &lt;td&gt;Pi 4 (\(5\text{ W}\)) + LiDAR (\(2\text{ W}\)) + ESP32 (\(0.75\text{ W}\)) = &lt;strong&gt;\(7.75\text{ W}\)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hai động cơ DC chạy đều&lt;/td&gt;
      &lt;td&gt;\(3.00\text{ W}\)&lt;/td&gt;
      &lt;td&gt;\(3.00\text{ W}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Tổng công suất đầu ra&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(3.75\text{ W}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(10.75\text{ W}\)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Thời gian chạy ước tính&lt;/strong&gt; (\(15\times 0.85 / P\))&lt;/td&gt;
      &lt;td&gt;\(\dfrac{12.75}{3.75} = \mathbf{3.4\text{ giờ}}\)&lt;/td&gt;
      &lt;td&gt;\(\dfrac{12.75}{10.75} \approx \mathbf{1.19\text{ giờ}}\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Chỉ riêng việc gắn thêm máy tính Linux và LiDAR đã làm công suất tăng gần &lt;strong&gt;3 lần&lt;/strong&gt; và cắt thời lượng pin từ &lt;strong&gt;3,4 giờ&lt;/strong&gt; xuống còn &lt;strong&gt;1,2 giờ&lt;/strong&gt; — chưa kể mạch hạ áp 5 V cho Pi 4 phải chịu được dòng đỉnh &lt;strong&gt;3 A&lt;/strong&gt; thay vì 0,5 A!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-chọn-lộ-trình-thông-minh-theo-4-bước&quot;&gt;Giải thích: Chọn lộ trình thông minh theo 4 bước&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Bắt đầu từ Lộ trình A để làm chủ tầng cơ điện tử:&lt;/strong&gt; Mọi hệ thống ROS 2 đều đứng trên một tầng MCU bên dưới; thành thạo ESP32/Pico trước giúp bạn không bị ngợp khi lên Linux.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Thiết kế khung cơ khí sẵn sàng nâng cấp:&lt;/strong&gt; Dùng chung một khung xe vi sai 2 bánh có khoảng trống trên tầng hai để sau này đặt thêm Raspberry Pi và LiDAR mà không phải mua lại từ đầu.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Tính dòng điện đỉnh trước khi mua bộ nguồn:&lt;/strong&gt; Mạch ổn áp cho SBC phải chịu dòng tức thời gấp 1,5 lần công suất trung bình để tránh sập nguồn (undervoltage throttling) khi CPU tăng tốc.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Khóa cam kết bằng ngân sách rõ ràng:&lt;/strong&gt; Chia mua sắm theo từng đợt (Đợt 1 điện tử → Đợt 2 MCU → Đợt 3 động cơ → Đợt 4 SBC/LiDAR) giúp loại bỏ linh kiện thừa.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu bạn muốn xe Lộ trình B (\(10.75\text{ W}\)) chạy liên tục đủ &lt;strong&gt;2,5 giờ&lt;/strong&gt; với hiệu suất hạ áp \(85\%\), dung lượng pin tối thiểu cần mua là bao nhiêu Wh? Ta cần \(E_{\text{pin}} = \dfrac{10.75 \times 2.5}{0.85} \approx \mathbf{31.6\text{ Wh}}\) (tương đương một bộ pin 2S dung lượng khoảng 4.300 mAh)!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-liền-với-capstone&quot;&gt;Vì sao bài này gắn liền với Capstone&lt;/h2&gt;

&lt;p&gt;Capstone A không phụ thuộc vào ROS. Bài nghiệm thu này chỉ cần một vi điều khiển kiểm soát được ngưỡng 300 ms và một mạch lái động cơ đáp ứng đúng tín hiệu PWM rồi ngắt an toàn khi mất lệnh. Nếu tháng đầu tiên chỉ trôi qua với việc phân vùng ổ đĩa cài Linux, bạn vẫn chưa kiểm chứng được dây động cơ. Lộ trình B hoàn toàn thực tế vì nó dẫn tới ROS 2 Jazzy, Gazebo Harmonic và micro-ROS, nhưng các công cụ đó chỉ là tầng truyền thông xây dựng phía trên nền tảng phần cứng đã đáng tin cậy. Chốt lộ trình ngay từ bây giờ giúp bạn tránh mua sắm cảm tính và không rơi vào cảnh mua LiDAR trước khi cơ chế tự dừng tồn tại.&lt;/p&gt;

&lt;h2 id=&quot;hai-lộ-trình-hợp-lệ-trên-cùng-một-khung-robot&quot;&gt;Hai lộ trình hợp lệ trên cùng một khung robot&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Lộ trình A&lt;/strong&gt; là hướng vi điều khiển tiết kiệm và hoàn toàn đủ để vượt qua Capstone A. Bộ xử lý trung tâm là mạch ESP32 hoặc Raspberry Pi Pico 2. Bạn nên chọn ESP32 nếu muốn mở rộng sang điều khiển teleop qua Wi-Fi ở giai đoạn sau (dùng điện thoại hoặc laptop cùng mạng gửi lệnh mà không cần cắm cáp USB). Ngược lại, Pico 2 phù hợp nếu bạn ưu tiên làm việc với MicroPython qua cáp USB ngay từ đầu. Cả hai mạch đều dùng mức logic 3,3 V, đều thực thi tốt quy tắc dừng 300 ms và không yêu cầu Ubuntu.&lt;/p&gt;

&lt;p&gt;Phần truyền động gồm hai động cơ giảm tốc TT lắp trên khung xe nhỏ cùng một bánh đa hướng (caster). Mạch lái động cơ khuyến nghị là &lt;strong&gt;TB6612FNG&lt;/strong&gt; dùng cầu H MOSFET. Nhiều video hướng dẫn thường dùng &lt;strong&gt;L298N&lt;/strong&gt; (cầu Darlington), nhưng ở dòng tải của động cơ, tầng Darlington gây sụt áp khoảng 2 V giữa pin và động cơ, trong khi cầu MOSFET chỉ sụt vài phần mười vôn. Với bộ pin hai cell có điện áp danh định gần 7,4 V:&lt;/p&gt;

\[V_{\mathrm{L298}} \approx 7{,}4\,\mathrm{V} - 2{,}0\,\mathrm{V} = 5{,}4\,\mathrm{V},\]

\[V_{\mathrm{TB6612}} \approx 7{,}4\,\mathrm{V} - 0{,}3\,\mathrm{V} = 7{,}1\,\mathrm{V}.\]

&lt;p&gt;Động cơ TT vốn dùng hộp số nhựa nhỏ và có tổn hao cơ khí; mất thêm 2 V là lý do cùng một bộ pin nhưng xe chạy yếu hẳn trên L298N trong khi hoạt động bình thường trên TB6612FNG. Trang sản phẩm carrier TB6612 của Pololu (&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;sản phẩm 713&lt;/a&gt;) là tài liệu tham khảo rất rõ ràng về con chip này, dù bạn hoàn toàn có thể mua module tương đương dùng cùng IC mà không cần nhập đúng mạch của Pololu. Nếu TB6612 hết hàng, DRV8833 là phương án thay thế dùng MOSFET gọn hơn — và vẫn tốt hơn L298N ở dải điện áp thấp.&lt;/p&gt;

&lt;p&gt;Cảm biến khoảng cách trong bộ khởi đầu là module siêu âm HC-SR04. Lưu ý rằng chân echo của cảm biến xuất mức cao khoảng 5 V nên tuyệt đối không cắm trực tiếp vào chân GPIO 3,3 V; bài 00-05 sẽ bổ sung mạch chuyển mức logic cho đường tín hiệu này. Lộ trình A không yêu cầu cài Ubuntu: gửi lệnh teleop qua cổng serial từ Windows, macOS hay Linux hiện có đều đủ để nghiệm thu.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lộ trình B&lt;/strong&gt; là sự mở rộng của Lộ trình A thêm một tầng phần mềm phía trên, sử dụng đúng khung xe, mạch lái và cơ chế timeout đó. Khi hệ thống teleop đã chạy ổn định, bạn dùng máy tính cài &lt;strong&gt;Ubuntu 24.04&lt;/strong&gt; và &lt;strong&gt;ROS 2 Jazzy&lt;/strong&gt;. &lt;strong&gt;Gazebo Harmonic&lt;/strong&gt; là bộ mô phỏng tương thích với bản phát hành này (triển khai ở Chương 10), còn &lt;strong&gt;micro-ROS&lt;/strong&gt; cho phép vi điều khiển giao tiếp trực tiếp với ROS 2 trên máy tính. Raspberry Pi có thể được bổ sung sau như một máy tính đồng hành tùy chọn trên xe, nhưng nó không phải thiết bị dùng để quay động cơ ở bước đầu và cũng không phải lý do để mua LiDAR sớm.&lt;/p&gt;

&lt;p&gt;Nếu cài micro-ROS trên một mạch chưa từng quay thử bánh xe, bạn sẽ phải gỡ cùng lúc hai lớp lỗi: một topic không có dữ liệu và một động cơ không có nguồn nhìn từ bên ngoài có biểu hiện giống hệt nhau. Đó là lý do khóa học luôn đặt tầng truyền thông phía sau thực hành bàn lab.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/tracks_mcu_ros.png&quot; alt=&quot;Lộ trình A bên trái, lộ trình B là tầng sau trên cùng robot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hãy đọc sơ đồ theo thứ tự: phần bên trái là robot bạn cần hoàn thiện trong tháng này, phần bên phải là tầng phần mềm mở rộng sau khi phần cứng đã ổn định. Nguyên tắc “không cần Ubuntu” áp dụng cho Lộ trình A đến hết Capstone A, còn “chỉ mua LiDAR sau khi Capstone A đáng tin cậy” là kỷ luật chi tiêu chung cho cả hai lộ trình. Một cảm biến LiDAR quay kéo theo bài toán nguồn riêng, tài liệu ROS phức tạp hơn và chi phí thường lên tới vài triệu đồng, nhưng lại không giúp quy tắc dừng 300 ms chính xác hơn.&lt;/p&gt;

&lt;h2 id=&quot;bảng-quyết-định&quot;&gt;Bảng quyết định&lt;/h2&gt;

&lt;p&gt;Hãy dùng bảng sau để chọn phương án phù hợp, chép dòng tương ứng vào sổ lab và đánh dấu rõ lựa chọn của bạn:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tình huống hiện tại&lt;/th&gt;
      &lt;th&gt;Lộ trình&lt;/th&gt;
      &lt;th&gt;Làm ngay&lt;/th&gt;
      &lt;th&gt;Để sau&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Hệ điều hành nào cũng được; ngân sách khoảng 1,5–2,0 triệu VND; mục tiêu là “gửi lệnh là xe chạy”&lt;/td&gt;
      &lt;td&gt;A&lt;/td&gt;
      &lt;td&gt;ESP32 hoặc Pico 2, động cơ TT, TB6612FNG, HC-SR04, mạch chuyển mức, dụng cụ và kế hoạch pin theo bài 00-05&lt;/td&gt;
      &lt;td&gt;Ubuntu, Raspberry Pi, LiDAR, Nav2, cài ROS 2 Jazzy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đang dùng Windows hoặc macOS và muốn học ROS về sau&lt;/td&gt;
      &lt;td&gt;A trước, sang B sau Chương 07&lt;/td&gt;
      &lt;td&gt;Lắp đúng robot Lộ trình A; ghi chú kế hoạch dùng máy Ubuntu 24.04 sau này&lt;/td&gt;
      &lt;td&gt;Không chia lại ổ cứng laptop vội; không mua LiDAR chỉ để có việc cho Raspberry Pi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Đã có sẵn máy chạy Ubuntu 24.04 và muốn tiếp cận ROS&lt;/td&gt;
      &lt;td&gt;B, nhưng vẫn lắp robot Lộ trình A trước&lt;/td&gt;
      &lt;td&gt;Làm phần điện tương tự Lộ trình A; có thể cài song song Jazzy trên máy đó&lt;/td&gt;
      &lt;td&gt;Không để mô phỏng Gazebo cản tiến độ Chương 07; không bỏ qua bàn lab&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ngân sách chủ yếu định chi cho Raspberry Pi kèm bộ LiDAR thấy trong video&lt;/td&gt;
      &lt;td&gt;Dừng lại và cân đối lại&lt;/td&gt;
      &lt;td&gt;Đặt lại mục tiêu là demo teleop nhấc bánh&lt;/td&gt;
      &lt;td&gt;Bộ kit đó không tự giải quyết quy tắc dừng 300 ms hay vấn đề sụt áp driver&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Một cảm biến LiDAR giá khoảng 4.000.000 ₫ đặt cạnh phần lõi phần cứng Lộ trình A khoảng 800.000 ₫ có tỷ lệ chi phí là:&lt;/p&gt;

\[\frac{4{.}000{.}000}{800{.}000} = 5\]

&lt;p&gt;lần, trong khi nó thậm chí không giúp bạn làm nhấp nháy đèn LED đầu tiên. Tỷ lệ này giải thích vì sao khóa học yêu cầu hoãn mua LiDAR cho tới sau nghiệm thu.&lt;/p&gt;

&lt;p&gt;Mọi con số về giá đều phải gắn kèm ngày khảo sát. Bài 00-05 lưu các mốc giá Hshop có ghi ngày cụ thể cho từng món bạn cần đặt mua, nên bài này không lặp lại bảng giá đó. Nếu một mạch vi điều khiển tăng hoặc giảm một trăm nghìn đồng, lộ trình học vẫn giữ nguyên; lộ trình chỉ thay đổi khi hệ điều hành, trần ngân sách hoặc mục tiêu học tập của bạn thay đổi.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành-viết-cam-kết-lộ-trình&quot;&gt;Thực hành: viết cam kết lộ trình&lt;/h2&gt;

&lt;p&gt;Mở &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/lab-notes.md&lt;/code&gt; và ghi ba dòng dưới ngày hôm nay: hệ điều hành máy tính đang dùng, hạn mức ngân sách bằng VND cho 30 ngày tới, và mục tiêu một câu. Nếu mục tiêu có nhắc tới lập bản đồ, SLAM hoặc Nav2, hãy bổ sung cụm “sau Capstone A”.&lt;/p&gt;

&lt;p&gt;Tiếp theo, viết rõ lựa chọn vi điều khiển: chọn “ESP32, ưu tiên serial USB trước và teleop Wi-Fi sau” hoặc “Pico 2, dùng MicroPython qua USB trước”. Bạn vẫn có thể đổi ý trước khi đặt mua, nhưng không được để trống lựa chọn này.&lt;/p&gt;

&lt;p&gt;Chép lại câu cam kết sau và ghi tên mình bên dưới: “Tôi sẽ không mua LiDAR trước khi bài teleop nhấc bánh Capstone A của tôi đạt yêu cầu, bao gồm quy tắc dừng PWM 300 ms.”&lt;/p&gt;

&lt;p&gt;Cuối cùng, thêm một dòng ghi chú chỉ về bài 00-05 như danh sách mua sắm duy nhất thay vì tạo thêm giỏ hàng thứ hai. Nếu bạn dự định dùng máy chung ở phòng lab cho Ubuntu về sau, hãy ghi rõ “dùng máy Ubuntu của lab, không dùng laptop cá nhân”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kết quả cần đạt:&lt;/strong&gt; Người đọc nhìn vào sổ biết ngay bạn đi lộ trình nào, chọn vi điều khiển gì, từ chối mua món đắt tiền nào, và trạng thái Ubuntu là “chưa cài”, “dùng máy lab sau” hay “đã cài nhưng vẫn ưu tiên bàn lab”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cách xử lý khi lệch chuẩn:&lt;/strong&gt; Nếu bạn chọn Lộ trình B nhưng chỉ định mua Raspberry Pi, hãy nhớ động cơ vẫn do ESP32 hoặc Pico 2 điều khiển và bổ sung các món đó theo bài 00-05. Nếu bạn chọn Lộ trình A nhưng định cài thử Jazzy trên Windows, hãy lưu trang hướng dẫn vào mục “sau Chương 07” và đóng tab lại vì Windows Subsystem for Linux (WSL) không thay thế được môi trường thực hành robot. Nếu ngân sách chưa ghi ngày, hãy tách rõ phần “lõi robot” với “dụng cụ” kèm ngày ước tính.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-sẵn&quot;&gt;Ví dụ làm sẵn&lt;/h2&gt;

&lt;p&gt;Minh dùng laptop Windows với trần ngân sách khoảng 1,5–2,0 triệu VND trong tháng này. Ban đầu anh đặt mục tiêu là “học ROS và SLAM”, sau đó viết lại thành “điều khiển xe hai bánh qua serial có quy tắc dừng 300 ms, rồi mới dùng máy Ubuntu của phòng lab để học Jazzy”. Dựa theo bảng quyết định, anh đi theo Lộ trình A trước và chọn ESP32 để sau này mở rộng sang teleop Wi-Fi ở Chương 07, trong khi giai đoạn đầu vẫn dùng serial USB. Anh không mua thêm Pico 2, không mua LiDAR, và không phân vùng lại ổ đĩa Windows tuần này vì sẽ dùng máy Ubuntu 24.04 của trường về sau. Danh sách mua sắm anh sử dụng là bài 00-05.&lt;/p&gt;

&lt;p&gt;Một bạn khác là Lan đã cài sẵn Ubuntu 24.04 trên laptop cá nhân. Lan có thể làm theo &lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation.html&quot;&gt;trang cài đặt Jazzy&lt;/a&gt; trong một buổi riêng song song và ghi phiên bản thực tế vào sổ lab, nhưng không được bỏ qua thực hành phần cứng. Robot của Lan vẫn gồm động cơ TT, TB6612FNG, HC-SR04 và firmware đưa PWM về 0 khi mất lệnh. Nếu Jazzy đã cài xong nhưng linh kiện vẫn nằm nguyên trong túi, Lan chưa hề đi trước tiến độ mà mới chỉ có tầng phần mềm. Gazebo Harmonic chỉ được ghi tên chứ chưa mở cho đến khi khung xe vượt qua bài teleop nhấc bánh, và micro-ROS (&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro.ros.org&lt;/a&gt;) cũng chờ đến khi mạch tự quay được bánh xe độc lập.&lt;/p&gt;

&lt;p&gt;Minh tự kiểm tra phép tính sụt áp để việc chọn mạch lái dựa trên cơ sở kỹ thuật thay vì cảm tính: ở nguồn 7,4 V, sụt áp 2 V của Darlington chỉ còn 5,4 V cho động cơ, còn sụt áp khoảng 0,3 V của MOSFET giữ lại 7,1 V. Anh ghi rõ “dùng TB6612FNG, không dùng L298N” vào câu lộ trình. Cả Minh và Lan đều kết thúc ghi chú bằng câu cam kết không mua LiDAR sớm và thống nhất lấy bài 00-05 làm danh sách mua sắm duy nhất.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=oJs0cVYuQTw&quot;&gt;Raspberry Pi VS Arduino VS ESP32&lt;/a&gt; — TechteamGB, tiếng Anh. Đối chiếu ba dòng mạch với hai lộ trình trong bài: làm chủ vi điều khiển trước, máy tính chạy ROS để sau.&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;[ROS 2 Series #1&lt;/td&gt;
          &lt;td&gt;Giới thiệu sơ lược về ROS&lt;/td&gt;
          &lt;td&gt;TeslaEdison](https://www.youtube.com/watch?v=_vovUjZM6os) — TeslaEdison, tiếng Việt. Video giới thiệu tổng quan ROS 2 bằng tiếng Việt; lưu ý không nhầm tên bản phân phối trong video với ROS 2 Jazzy của khóa học.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Bạn dùng laptop Windows, trần ngân sách 1.800.000 ₫ và muốn robot chạy trong tháng này. Bạn nên chọn lộ trình nào, vi điều khiển nào (nếu muốn làm Wi-Fi về sau) và mạch lái nào? Với Ubuntu trong tuần này, bạn nên xử lý thế nào?&lt;/li&gt;
  &lt;li&gt;Nếu máy tính của bạn đã cài sẵn Ubuntu 24.04, vì sao bạn vẫn phải lắp khung xe theo Lộ trình A trước dù được phép cài song song ROS 2 Jazzy?&lt;/li&gt;
  &lt;li&gt;Một bộ 4 pin AA tạo điện áp 6 V cấp qua L298N bị sụt khoảng 2 V. Điện áp thực tế tới động cơ là bao nhiêu? Vì sao đây là sự kết hợp kém với động cơ TT vốn đã yếu quanh mức 4 V?&lt;/li&gt;
  &lt;li&gt;Một cửa hàng gợi ý thêm cảm biến LiDAR giá 3.500.000 ₫ vào bộ Lộ trình A. Việc này có giúp nghiệm thu Capstone A dễ hơn không, và LiDAR nên xuất hiện ở giai đoạn nào?&lt;/li&gt;
  &lt;li&gt;micro-ROS nên được triển khai vào lúc nào so với ngày đầu tiên bánh xe đáp ứng lệnh serial? Nếu mở kho mã nguồn &lt;a href=&quot;https://github.com/linorobot/linorobot2&quot;&gt;linorobot2&lt;/a&gt; quá sớm, bạn dễ bị cuốn vào việc bỏ qua bước nền tảng nào?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Chọn Lộ trình A ngay từ đầu. Chọn ESP32 nếu có kế hoạch làm teleop Wi-Fi về sau (hoặc Pico 2 nếu muốn dùng MicroPython qua USB trước). Mạch lái là TB6612FNG. Việc cài Ubuntu để sau, không cài Jazzy trên Windows rồi coi đó là thực hành robot.
2. Jazzy giúp bạn quan sát topic trên máy tính, nhưng không kiểm chứng được đường nguồn VM, hiện tượng kẹt trục (stall) và cơ chế timeout 300 ms trên phần cứng thật. Cài song song để tìm hiểu thì được, nhưng không thể thay thế bàn lab.
3. Điện áp tại động cơ chỉ còn $6 - 2 = 4$ V. Mức 4 V nằm ở vùng rất yếu của hộp số TT ngay cả khi không tải, khiến xe bò chậm hoặc kẹt trục và dễ bị nhầm là động cơ hỏng.
4. Không giúp nghiệm thu dễ hơn. LiDAR không thực thi quy tắc timeout và không thay thế mạch lái MOSFET. Nó chỉ nên cân nhắc sau khi Capstone A đã chạy ổn định.
5. micro-ROS chỉ nên triển khai sau khi bánh xe đã đáp ứng tốt lệnh serial. Kho mã nguồn `linorobot2` mô tả một robot vi sai hoàn chỉnh cho ROS 2; xem nó trước khi có mạch lái và cơ chế timeout rất dễ khiến bạn sa đà vào việc chỉnh sửa tệp URDF trong khi phần cứng thực tế chưa hề được lắp.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/linorobot/linorobot2&quot;&gt;linorobot2&lt;/a&gt; — một hệ thống phần mềm xe vi sai mẫu cho ROS 2; nên đọc để hình dung đích đến của Lộ trình B thay vì coi là danh sách linh kiện tháng này.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; — tài liệu giới thiệu micro-ROS; hãy quay lại thực hành hướng dẫn sau khi khung xe đã vận hành độc lập.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation.html&quot;&gt;Cài ROS 2 Jazzy&lt;/a&gt; — hướng dẫn cài đặt chính thức trên Ubuntu 24.04; nếu bạn đang dùng Windows, hãy để dành cho máy lab về sau.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG, sản phẩm 713&lt;/a&gt; — tài liệu giải thích rõ lý do mạch lái MOSFET vượt trội hơn mạch Darlington ở điện áp thấp.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Toàn bộ danh sách mua sắm nằm tại &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;bài 00-05&lt;/a&gt;; đừng tự tạo giỏ hàng mới từ bốn liên kết tham khảo trên.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Chào mừng, kết quả học và cách học</title>
   <link href="https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_01_welcome_and_outcomes/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/vi/chapter00/00_01_welcome_and_outcomes</id>
   <content type="html">&lt;p&gt;Hãy dành khoảng 90 phút cho bài học này. Bạn chỉ cần một trình soạn thảo văn bản và một cuốn lịch; hiện tại chưa cần robot hay hệ điều hành Ubuntu. Mục tiêu của bài là giúp bạn lập một lộ trình học tập cụ thể và đủ rõ ràng để sử dụng xuyên suốt cho đến khi khung xe robot lăn bánh.&lt;/p&gt;

&lt;h2 id=&quot;mục-tiêu-học&quot;&gt;Mục tiêu học&lt;/h2&gt;

&lt;p&gt;Sau bài học này, bạn sẽ &lt;strong&gt;trình bày&lt;/strong&gt; được cấu trúc cốt lõi của khóa học, từ bước nhận diện linh kiện, vượt qua mốc điều khiển từ xa (teleop) ở Chương 07, cho đến việc đọc hiểu các tài liệu về học máy cho robot (robot learning) về sau. Bạn sẽ &lt;strong&gt;tính toán&lt;/strong&gt; được nhịp học 4–6 giờ mỗi tuần trong 12–16 tuần và phân bổ các mốc chương lên lịch cá nhân. Bạn cũng sẽ &lt;strong&gt;tạo&lt;/strong&gt; sổ lab gồm sáu trường thông tin bắt buộc: ngày, ảnh chụp, sơ đồ dây, đoạn log serial, lỗi gặp phải và cách khắc phục; đồng thời &lt;strong&gt;phân biệt&lt;/strong&gt; được ghi chép có bằng chứng với nhận xét cảm tính dựa trên việc có kèm log serial và mô tả lỗi hay không. Cuối cùng, bạn sẽ biết cách &lt;strong&gt;tìm&lt;/strong&gt; bản tiếng Việt hoặc tiếng Anh của cùng một bài học theo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chapter&lt;/code&gt; và &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order&lt;/code&gt;, và chủ động &lt;strong&gt;hoãn&lt;/strong&gt; việc mua LiDAR, dùng Nav2 hay cài đặt ROS 2 đầy đủ cho tới khi xe vi sai biết tự dừng khi mất lệnh điều khiển.&lt;/p&gt;

&lt;h2 id=&quot;cần-có-trước&quot;&gt;Cần có trước&lt;/h2&gt;

&lt;p&gt;Bạn chỉ cần đọc hiểu được tài liệu kỹ thuật ở tốc độ chậm và biết tạo thư mục trên máy tính. Lúc này bạn chưa cần đồng hồ vạn năng, mỏ hàn hay kiến thức nền về robot. Định luật Ohm sẽ được học ở Chương 01 thông qua thực hành đo đạc thay vì học thuộc lòng. Thói quen quan trọng nhất cần rèn ngay từ đầu là ghi chép trung thực những gì gặp lỗi; một dòng nhật ký chung chung như “bắt đầu khóa học” sẽ không giúp ích gì khi bạn cần gỡ lỗi ở tuần thứ chín.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Hãy tưởng tượng khoảnh khắc đầu tiên chiếc xe robot hai bánh bạn tự tay lắp ráp chạy thẳng đúng một mét, dừng êm ái trước vật cản rồi quay góc \(90^\circ\) mà không cần bạn chạm vào bàn phím. Đằng sau khoảnh khắc kỳ diệu ấy không phải là phép màu lập trình, mà là sự phối hợp nhịp nhàng giữa &lt;strong&gt;điện tử cơ bản&lt;/strong&gt;, &lt;strong&gt;vi điều khiển thời gian thực&lt;/strong&gt;, &lt;strong&gt;toán học động học–điều khiển&lt;/strong&gt; và &lt;strong&gt;kỷ luật kiểm thử phần cứng&lt;/strong&gt;. Bài mở đầu này đặt ra bản đồ tư duy để bạn bước từ bàn học lý thuyết sang bàn lab cơ điện tử một cách chắc chắn nhất.&lt;/p&gt;

&lt;h2 id=&quot;kiến-thức-cần-nhớ-kỹ-thuật-robot-robotics-là-gì-và-nghiên-cứu-những-gì&quot;&gt;Kiến thức cần nhớ: Kỹ thuật Robot (Robotics) là gì và nghiên cứu những gì?&lt;/h2&gt;

&lt;p&gt;Với sinh viên đại học, robot học là giao điểm thực chiến của ba trụ cột kỹ thuật cốt lõi:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Vòng lặp Cảm nhận – Lập kế hoạch – Hành động (Sense–Plan–Act):&lt;/strong&gt; Mọi robot tự hành đều lặp đi lặp lại một chu trình kín ở tần số cố định: cảm biến đo thế giới thực (&lt;em&gt;Sense&lt;/em&gt;) → thuật toán ước lượng trạng thái và tính lệnh điều khiển (&lt;em&gt;Plan&lt;/em&gt;) → động cơ thực thi lực/mô-men (&lt;em&gt;Act&lt;/em&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sự ràng buộc của Vật lý và Thời gian thực (Real-Time Constraints):&lt;/strong&gt; Khác với phần mềm web có thể chờ 200 ms không sao, một robot tự cân bằng hay xe chạy \(1\text{ m/s}\) bị trễ 200 ms đã lao thẳng vào tường (đi thêm \(20\text{ cm}\) mất kiểm soát!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kỹ thuật kiểm chứng theo tầng (Incremental Bring-Up):&lt;/strong&gt; Không bao giờ lắp hết mạch rồi mới cắm pin; kỹ sư robot kiểm chứng từng tầng độc lập — Nguồn → Vi điều khiển → Cảm biến → Động cơ → Điều khiển vòng kín.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;ví-dụ-làm-từng-bước-tính-ngân-sách-thời-gian-cho-vòng-lặp-điều-khiển-50text-hz&quot;&gt;Ví dụ làm từng bước: Tính ngân sách thời gian cho vòng lặp điều khiển \(50\text{ Hz}\)&lt;/h3&gt;

&lt;p&gt;Giả sử xe robot của bạn chạy vòng lặp điều khiển chính ở tần số \(f = 50\text{ Hz}\). Khi đó mỗi chu kỳ điều khiển chỉ có đúng:&lt;/p&gt;

\[T = \frac{1}{f} = \frac{1}{50\text{ s}^{-1}} = \mathbf{20\text{ ms}}.\]

&lt;p&gt;Ta cộng tay thời gian thực thi của 3 khối trong một chu kỳ trên ESP32:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cảm nhận (Sense):&lt;/strong&gt; đọc 2 bộ mã hóa vòng quay (encoder) + cảm biến IMU qua I2C mất &lt;strong&gt;\(2.5\text{ ms}\)&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lập kế hoạch (Plan):&lt;/strong&gt; tính vận tốc góc, chạy bộ điều khiển PID và cập nhật trạng thái mất &lt;strong&gt;\(0.8\text{ ms}\)&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hành động (Act):&lt;/strong&gt; cập nhật độ rộng xung PWM cho 2 động cơ + ghi log nhẹ mất &lt;strong&gt;\(0.7\text{ ms}\)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tổng thời gian tính toán là \(2.5 + 0.8 + 0.7 = \mathbf{4.0\text{ ms}} \ll 20\text{ ms}\) (chỉ chiếm \(20\%\) CPU, còn \(16\text{ ms}\) dự phòng!). Nhưng nếu bạn lỡ viết một lệnh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(50)&lt;/code&gt; để chờ cảm biến siêu âm, chu kỳ lập tức vọt lên \(54\text{ ms}\) (tần số tụt xuống \(18.5\text{ Hz}\)) và bộ điều khiển PID lập tức bị dao động!&lt;/p&gt;

&lt;h3 id=&quot;giải-thích-bốn-nguyên-tắc-vàng-để-tự-học-robotics-không-bỏ-cuộc-giữa-chừng&quot;&gt;Giải thích: Bốn nguyên tắc vàng để tự học Robotics không bỏ cuộc giữa chừng&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bước 1 — Tách biệt lỗi phần cứng với lỗi phần mềm:&lt;/strong&gt; Trước khi sửa 100 dòng code, luôn đo điện áp đường nguồn bằng đồng hồ vạn năng — hơn một nửa “lỗi code” ở người mới thực chất là dây cắm lỏng hoặc sụt áp pin.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 2 — Ghi sổ lab bằng con số đo được:&lt;/strong&gt; Đừng ghi &lt;em&gt;“xe chạy tạm ổn”&lt;/em&gt;; hãy ghi &lt;em&gt;“Pin 7.82 V, sai số quãng đường 1.00 m là +1.8 cm, dòng không tải 210 mA”&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 3 — Luôn có trạng thái an toàn mặc định:&lt;/strong&gt; Mọi firmware phải khởi động với động cơ tắt (PWM = 0) và có nút hoặc lệnh dừng khẩn cấp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bước 4 — Đi từ mô hình nhỏ kiểm chứng được bằng tay:&lt;/strong&gt; Hiểu rõ xe vi sai 2 bánh trên bàn làm việc giúp bạn mở rộng mượt mà lên robot tự hành chạy ROS 2 trong công nghiệp.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Kiểm tra nhanh.&lt;/strong&gt; Nếu xe robot chạy với vận tốc \(v = 0.8\text{ m/s}\) và vòng lặp điều khiển chạy ở \(f = 20\text{ Hz}\) (\(T = 50\text{ ms}\)), giữa hai lần cập nhật lệnh xe đi được bao xa? Đúng \(0.8 \times 0.05 = \mathbf{0.04\text{ m} = 4\text{ cm}}\) — đó là lý do xe chạy càng nhanh thì tần số vòng lặp phải càng cao!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;vì-sao-bài-này-gắn-liền-với-capstone&quot;&gt;Vì sao bài này gắn liền với Capstone&lt;/h2&gt;

&lt;p&gt;Chương 07 là mốc &lt;strong&gt;Capstone A&lt;/strong&gt;: một xe dẫn động vi sai gồm hai bánh chủ động và một bánh đa hướng (caster), được bạn điều khiển thủ công khi bánh xe đang nhấc khỏi mặt bàn. Điều khiển từ xa (teleop) nghĩa là bạn trực tiếp gửi lệnh lái chứ chưa để robot tự tìm đường. Quy tắc an toàn cốt lõi là nếu không có lệnh mới trong hơn 300 ms, chu kỳ xung (duty PWM) phải tự động đưa về 0. ROS 2 Jazzy chỉ xuất hiện sau khi bạn vượt qua bài kiểm tra này. Về bản chất, một topic trong ROS 2 chỉ là một kênh truyền thông điệp có tên nằm phía trên phần cứng mà bạn đã kiểm chứng, chứ bản thân topic không tạo ra mô-men xoắn. Gazebo Harmonic và micro-ROS cũng tuân theo nguyên tắc đó ở tầng mô phỏng và vi điều khiển. Nếu ngay từ bây giờ bạn chưa nắm rõ quy tắc dừng 300 ms, mọi việc cài đặt phần mềm về sau rất dễ trở thành cách né tránh thực hành trên bàn lab.&lt;/p&gt;

&lt;h2 id=&quot;lộ-trình-cốt-lõi-của-khóa-học&quot;&gt;Lộ trình cốt lõi của khóa học&lt;/h2&gt;

&lt;p&gt;Khóa học được thiết kế theo một tuyến thống nhất, với một số chương mở rộng tùy chọn ở giai đoạn cuối.&lt;/p&gt;

&lt;p&gt;Bạn sẽ bắt đầu bằng kỹ năng nhận diện phần cứng cơ bản: cấu trúc hàng lỗ trên breadboard, mạch lái động cơ (driver), phân biệt cáp USB chỉ sạc với cáp truyền dữ liệu, và kiểm tra sự tương thích giữa cell pin với bộ sạc. Chương 00 xây dựng thói quen làm việc an toàn trên bàn lab, còn Chương 01 chuyển các khái niệm điện áp, dòng điện và điện trở thành những con số đo đạc thực tế.&lt;/p&gt;

&lt;p&gt;Tiếp theo là phần firmware trên ESP32 hoặc Raspberry Pi Pico. Chương 02 hướng dẫn nạp chương trình vào vi điều khiển, còn Chương 03 đưa bạn vào cấu trúc mã nguồn đặc trưng cho robot: chân GPIO, định thời và điều chế độ rộng xung (PWM). PWM là kỹ thuật giữ một chân ở mức logic cao trong một tỷ lệ nhất định của mỗi chu kỳ ngắn, từ đó mạch lái động cơ chuyển đổi thành mức công suất cấp cho động cơ.&lt;/p&gt;

&lt;p&gt;Từ Chương 04 đến Chương 06, bạn bổ sung cảm biến, mạch lái động cơ và lắp ráp cơ khí cho hệ hai bánh chủ động. Chương 07 là bài nghiệm thu Capstone A: bạn gửi lệnh tiến, xoay hoặc dừng qua cổng serial USB (hoặc qua một trang Wi-Fi đơn giản nếu dùng ESP32) trong trạng thái bánh xe nhấc khỏi mặt bàn. Một bộ đếm thời gian sẽ lưu lại thời điểm nhận lệnh hợp lệ gần nhất. Gọi $t_{\mathrm{last}}$ là mốc thời gian đó và $t_{\mathrm{now}}$ là thời điểm hiện tại trong vòng lặp điều khiển. Khoảng thời gian mất lệnh được tính là:&lt;/p&gt;

\[t_{\mathrm{silence}} = t_{\mathrm{now}} - t_{\mathrm{last}}.\]

&lt;p&gt;Quy tắc dừng an toàn của khóa học là:&lt;/p&gt;

\[t_{\mathrm{silence}} &amp;gt; 300\,\mathrm{ms} \implies \text{duty PWM} = 0.\]

&lt;p&gt;Dấu lớn hơn ở đây là bất đẳng thức chặt: lệnh đến đúng mốc 300 ms vẫn được coi là hợp lệ. Hôm nay bạn chưa cần lập trình vòng lặp này, nhưng cần hiểu ý nghĩa vật lý của nó. Ở tốc độ chậm $0{,}3\,\mathrm{m/s}$, robot vẫn có thể trôi thêm khoảng 9 cm trong khoảng thời gian chờ đó:&lt;/p&gt;

\[s \approx 0{,}3\,\mathrm{m/s} \times 0{,}3\,\mathrm{s} = 0{,}09\,\mathrm{m}.\]

&lt;p&gt;Quãng đường 9 cm là đủ để làm giật đứt cáp USB hoặc kéo xe rơi khỏi bàn, vì vậy bài nghiệm thu bắt buộc phải nhấc bánh lên.&lt;/p&gt;

&lt;p&gt;Chỉ khi bản demo teleop hoạt động ổn định và lặp lại được, bạn mới chuyển sang tầng truyền thông. Chương 08 xây dựng giao tiếp và vòng điều khiển đơn giản trên phần cứng đã vận hành. Chương 09 cài đặt ROS 2 Jazzy (chỉ hỗ trợ trên Ubuntu 24.04); lúc này, một topic vận tốc thực sự có ý nghĩa vì bạn đã nắm rõ firmware phản ứng thế nào khi luồng tin nhắn bị ngắt. Chương 10 giới thiệu Gazebo Harmonic (bộ mô phỏng đi kèm Jazzy) và micro-ROS (giải pháp giúp vi điều khiển giao tiếp trực tiếp với ROS 2). Bạn chỉ nên triển khai micro-ROS sau khi vi điều khiển đã lái được khung xe độc lập, bởi nếu không, một topic im lặng và một động cơ mất điện sẽ gây ra triệu chứng giống hệt nhau.&lt;/p&gt;

&lt;p&gt;Chương 11 và 12 là hai chương tùy chọn, đòi hỏi đọc tài liệu một cách tỉnh táo. LeRobot là thư viện của Hugging Face dành cho tập dữ liệu robot và các chính sách điều khiển (policy) học từ dữ liệu. ACT dự đoán một chuỗi hành động ngắn phía trước, diffusion policy sinh hành động bằng quá trình khử nhiễu từng bước, còn VLA ánh xạ trực tiếp hình ảnh và câu lệnh thành hành động. Không mô hình nào trong số này thay thế được một mạch lái động cơ biết ngắt PWM an toàn. Khi đọc các tài liệu này, hãy chú ý vào giả thuyết, dữ liệu thực nghiệm và giới hạn của chúng; một kho mã nguồn không thể tự biến khung xe đầu tiên của bạn thành nền tảng nghiên cứu.&lt;/p&gt;

&lt;p&gt;Nhiều video hướng dẫn thường đặt các công cụ phức tạp ở bước đầu tiên, nhưng khóa học này chủ động hoãn chúng lại: chưa dùng LiDAR, chưa dùng Nav2 và chưa cài ROS 2 trong tuần này (trừ khi bạn chỉ đọc trước trang hướng dẫn trên máy đã cài sẵn Ubuntu 24.04). Thực hành trên bàn lab luôn được ưu tiên. Một chiếc xe lăn được vài giây chưa phải là robot tự hành, và điện áp pin động cơ tuyệt đối không được nối vào chân 3,3 V.&lt;/p&gt;

&lt;h2 id=&quot;nhịp-học-46-giờ-mỗi-tuần-trong-1216-tuần&quot;&gt;Nhịp học: 4–6 giờ mỗi tuần trong 12–16 tuần&lt;/h2&gt;

&lt;p&gt;Khối lượng học tập thiết kế là &lt;strong&gt;4–6 giờ mỗi tuần&lt;/strong&gt; trong khoảng &lt;strong&gt;12–16 tuần&lt;/strong&gt;. Với 14 tuần và 5 giờ mỗi tuần, tổng thời gian là:&lt;/p&gt;

\[5 \times 14 = 70\]

&lt;p&gt;giờ. Tương tự, 12 tuần với 6 giờ/tuần đạt 72 giờ, còn 16 tuần với 4 giờ/tuần đạt 64 giờ. Quỹ thời gian này đủ để hoàn thành từ Chương 00 đến Chương 10, miễn là mỗi tuần bạn đều có ít nhất một buổi thực hành trên bàn lab (hoặc chuẩn bị quy trình trên giấy trước khi linh kiện về). Một tuần 5 giờ có thể chia hợp lý thành khoảng 90 phút đọc tài liệu, 120 phút thực hành và 90 phút hoàn thiện ghi chép. Dồn 12 giờ vào một ngày Chủ nhật rồi bỏ trống ba tuần liền chỉ khiến bạn quên mất sơ đồ dây và trạng thái lỗi của chính mình.&lt;/p&gt;

&lt;p&gt;Bảng dưới đây gắn các mốc chương với bằng chứng đầu ra tương ứng:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Tuần&lt;/th&gt;
      &lt;th&gt;Chương&lt;/th&gt;
      &lt;th&gt;Bằng chứng bạn cần hoàn thành&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1–3&lt;/td&gt;
      &lt;td&gt;00 và 01&lt;/td&gt;
      &lt;td&gt;Thẻ quy trình cấp nguồn, tiếng bíp thông mạch của đồng hồ trên mối nối đã biết, và mục sổ lab ghi số đo điện áp hoặc trạng thái “không có” trung thực&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;4–6&lt;/td&gt;
      &lt;td&gt;02 và 03&lt;/td&gt;
      &lt;td&gt;Firmware chạy trên ESP32 hoặc Pico kèm đoạn log serial dán vào sổ lab&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7–11&lt;/td&gt;
      &lt;td&gt;04 tới 07&lt;/td&gt;
      &lt;td&gt;Demo teleop nhấc bánh; duty PWM tự động về 0 khi mất lệnh quá khoảng 300 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;12–14&lt;/td&gt;
      &lt;td&gt;08 tới 10&lt;/td&gt;
      &lt;td&gt;Một topic ROS 2 trên Ubuntu 24.04 điều khiển chuyển động đã kiểm chứng; ghi nhận rõ vai trò của Gazebo Harmonic và micro-ROS trong sổ lab&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;Sau, tùy chọn&lt;/td&gt;
      &lt;td&gt;11 và 12&lt;/td&gt;
      &lt;td&gt;Ghi chú đọc hiểu về LeRobot, ACT, diffusion policy hoặc VLA, phân biệt rõ phần nào đo được trên robot của bạn và phần nào không&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Nếu bận đột xuất trong một tuần, hãy lùi các mốc kế tiếp lại thay vì cắt bỏ bài kiểm tra nhấc bánh để chạy theo ROS 2 Jazzy.&lt;/p&gt;

&lt;h2 id=&quot;sổ-lab&quot;&gt;Sổ lab&lt;/h2&gt;

&lt;p&gt;Hãy duy trì một tệp duy nhất là &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/lab-notes.md&lt;/code&gt;. Mỗi mục ghi chép cần đủ sáu trường thông tin:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Ngày:&lt;/strong&gt; thời điểm thực hiện buổi lab.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ảnh:&lt;/strong&gt; tên tệp ảnh chụp bàn làm việc hoặc mạch; ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;không có&lt;/code&gt; nếu chưa có vật thể để chụp.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sơ đồ dây:&lt;/strong&gt; ảnh chụp bản vẽ tay bằng bút chì; nếu chưa lắp mạch thì ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa có mạch&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Đoạn serial:&lt;/strong&gt; nội dung sao chép trực tiếp từ Serial Monitor, shell Thonny, hoặc lệnh echo của ROS 2 về sau; nếu chưa có mạch thì ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;không có — chưa có board&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cái gì hỏng:&lt;/strong&gt; mô tả triệu chứng đo được cụ thể (ví dụ: cổng serial bị ngắt khi cấp nguồn hai động cơ từ chân 5 V USB), tránh ghi chung chung kiểu “động cơ hỏng”.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cái gì sửa được:&lt;/strong&gt; nêu rõ thao tác thay đổi đã thực hiện (kể cả ghi chú như “chưa cấp nguồn; đã viết xong quy tắc dừng 300 ms trước khi đặt mua linh kiện”).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Để trống bất kỳ trường nào đều làm giảm giá trị tra cứu của sổ lab thành ghi chú cảm tính.&lt;/p&gt;

&lt;p&gt;Trang web khóa học hỗ trợ song ngữ Việt – Anh. Nút chuyển ngôn ngữ ánh xạ bài học theo &lt;strong&gt;chapter&lt;/strong&gt; và &lt;strong&gt;order&lt;/strong&gt;, không theo tiêu đề. Bài này thuộc &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chapter: &quot;00&quot;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order: 1&lt;/code&gt;, và bản tiếng Anh tương ứng cũng dùng đúng cặp giá trị đó. Khi gặp một đoạn khó hiểu, bạn có thể chuyển ngôn ngữ để đối chiếu trực tiếp. Các thuật ngữ kỹ thuật như GPIO, PWM, breadboard, Capstone và ROS 2 được giữ thống nhất ở cả hai ngôn ngữ.&lt;/p&gt;

&lt;h2 id=&quot;hình-minh-họa&quot;&gt;Hình minh họa&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/kit_catalog_overview.png&quot; alt=&quot;Các họ linh kiện và dụng cụ mà khóa sẽ yêu cầu bạn nhận ra&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hình trên minh họa các nhóm linh kiện và dụng cụ sẽ xuất hiện trong khóa học, không phải giỏ hàng mua sắm. Danh sách vật tư cụ thể nằm ở bài 00-05; mua sắm theo hình minh họa rất dễ dẫn đến việc mua LiDAR trước khi có đồng hồ vạn năng.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/tracks_mcu_ros.png&quot; alt=&quot;Hai lộ trình học dùng chung một robot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Lộ trình A hoàn thành nghiệm thu Capstone A thuần bằng firmware vi điều khiển mà không cần Ubuntu. Lộ trình B sử dụng đúng khung xe đó, sau đó bổ sung tầng phần mềm gồm Ubuntu 24.04, ROS 2 Jazzy, Gazebo Harmonic và micro-ROS khi phần cứng đã chạy ổn định. Cả hai lộ trình đều dùng chung một bài nghiệm thu, và LiDAR chỉ nên cân nhắc sau khi bài kiểm tra đó đạt độ tin cậy.&lt;/p&gt;

&lt;h2 id=&quot;thực-hành-mở-sổ-lab-và-lập-lịch-14-tuần&quot;&gt;Thực hành: mở sổ lab và lập lịch 14 tuần&lt;/h2&gt;

&lt;p&gt;Tạo thư mục khóa học cùng tệp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/lab-notes.md&lt;/code&gt; và điền đủ sáu trường dưới ngày hôm nay. Ở trường sơ đồ dây, hãy viết rõ phát biểu Capstone A: xe hai bánh chủ động, nhấc bánh khỏi bàn, điều khiển qua serial hoặc trang Wi-Fi đơn giản, đưa duty PWM về 0 nếu mất lệnh quá 300 ms. Ở trường đoạn serial, ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;không có — chưa có board&lt;/code&gt; nếu chưa có cổng kết nối thực tế; tuyệt đối không tự bịa log.&lt;/p&gt;

&lt;p&gt;Tiếp theo, đặt ba khung giờ cố định trong bảy ngày tới với tổng thời lượng 4–6 giờ: Đọc tài liệu, Thực hành bàn lab và Ghi chú. Khi linh kiện chưa về, buổi thực hành có thể dành cho việc viết thẻ quy trình cấp nguồn. Chép bảng mốc chương vào sổ và điền tuần lịch thực tế tương ứng với các giai đoạn 1–3, 4–6, 7–11 và 12–14. Nếu có tuần bận, hãy dịch chuyển mốc lùi lại chứ không bỏ qua Chương 07. Có ba món bạn chưa nên mua trong tháng này: LiDAR, máy tính mua riêng chỉ để học ROS, và bộ sạc chưa đọc rõ nhãn thông số. Danh sách mua sắm chính thức nằm tại &lt;a href=&quot;/contents/vi/chapter00/00_05_bom_and_shopping/&quot;&gt;bài 00-05&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kết quả cần đạt:&lt;/strong&gt; Người đọc khác có thể tìm thấy đủ sáu trường thông tin trong vòng một phút, thấy rõ con số 300 ms trong phát biểu mục tiêu, và thấy các khung giờ lặp lại trên lịch của bạn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cách xử lý khi lệch chuẩn:&lt;/strong&gt; Nếu một trường chưa có dữ liệu, hãy điền &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;không có&lt;/code&gt; thay vì bỏ trống. Nếu bạn đang dồn 6 giờ vào riêng ngày Chủ nhật, hãy tách nhỏ ra nhiều buổi trong tuần. Các trang tài liệu về Nav2 nên được đánh dấu “đọc sau Chương 07”, còn việc chọn pin lithium rời hãy chờ đến bài 00-05 để khớp đúng loại sạc với hóa học của cell pin.&lt;/p&gt;

&lt;h2 id=&quot;ví-dụ-làm-sẵn&quot;&gt;Ví dụ làm sẵn&lt;/h2&gt;

&lt;p&gt;Hà bắt đầu học vào tối thứ Tư bằng laptop Windows và chưa có linh kiện. Kế hoạch 6 giờ mỗi tuần trong 12 tuần cho tổng $6 \times 12 = 72$ giờ, nhưng đến tuần 8 cô phải đi công tác nên Chương 07 rơi đúng vào tuần không có mặt ở nhà. Cô điều chỉnh sang kế hoạch 5 giờ mỗi tuần trong 14 tuần (tổng 70 giờ), vẫn đủ hoàn thành Chương 00–10 nếu duy trì đều buổi thực hành. Cô ghi rõ các mốc: tuần 1–3 học Chương 00–01, tuần 4–6 học Chương 02–03, tuần 7–11 học Chương 04–07, tuần 12–14 học Chương 08–10, còn Chương 11–12 để tùy chọn sau khi robot đã biết tự dừng khi mất lệnh.&lt;/p&gt;

&lt;p&gt;Trong cùng mục sổ lab, Hà chép lại quy tắc 300 ms. Trường serial ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;không có — chưa có board&lt;/code&gt;, ảnh ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;không có&lt;/code&gt;, lỗi ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chưa cấp nguồn&lt;/code&gt;, và cách khắc phục ghi &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đã chốt lịch theo từng tuần trước khi mở trang mua sắm&lt;/code&gt;. Khung giờ 20:00–21:30 tối thứ Năm được đặt làm buổi thực hành với tiêu đề “viết thẻ an toàn, chưa hàn”. Ghi chép như vậy là đạt yêu cầu.&lt;/p&gt;

&lt;h2 id=&quot;video-tham-khảo&quot;&gt;Video tham khảo&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Xem trước:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/watch?v=7TVWlADXwRw&quot;&gt;What Is ROS2? - Framework Overview&lt;/a&gt; — Raymond Andrade, tiếng Anh. Giới thiệu tổng quan kiến trúc ROS 2 sẽ dùng ở nửa sau khóa học: node, topic và mô hình chạy nhiều tiến trình.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=oJs0cVYuQTw&quot;&gt;Raspberry Pi VS Arduino VS ESP32&lt;/a&gt; — TechteamGB, tiếng Anh. So sánh ba dòng mạch phổ biến trên lộ trình để bạn chọn một vi điều khiển phù hợp cho giai đoạn đầu thay vì mua dàn trải.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.facebook.com/reel/1746806056360278&quot;&gt;Cơ điện tử học làm robot&lt;/a&gt; — Facebook Reel của Hoàng Quân (Cơ điện tử ICTU, &lt;a href=&quot;https://chinbi.com&quot;&gt;chinbi.com&lt;/a&gt;). Góc nhìn thực tế về quá trình sinh viên cơ điện tử tự làm robot từ bàn lab đến khi xe vận hành được.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bài-tập&quot;&gt;Bài tập&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Viết phát biểu Capstone A của riêng bạn, trong đó có đủ các yếu tố: hai bánh chủ động, bánh nhấc khỏi bàn, điều khiển qua serial hoặc Wi-Fi đơn giản, và tắt PWM khi mất lệnh quá khoảng 300 ms. Ghi kèm khoảng tuần dự kiến bạn có thể quay video nghiệm thu demo này.&lt;/li&gt;
  &lt;li&gt;So sánh hai kế hoạch học: Kế hoạch P là 4 giờ/tuần trong 16 tuần; Kế hoạch Q là 6 giờ/tuần trong 12 tuần. Tính tổng số giờ của mỗi kế hoạch và cho biết kế hoạch nào vẫn giữ được quỹ thời gian hợp lý mà không cắt xén tuần dành cho bài nghiệm thu nhấc bánh.&lt;/li&gt;
  &lt;li&gt;Một mục sổ lab chỉ có ngày, ảnh đèn LED sáng và dòng chữ “chạy được”, thiếu cả đoạn serial lẫn mục “cái gì hỏng”. Khi cần gỡ lỗi về sau, bạn sẽ mất hai thông tin thực tế quan trọng nào?&lt;/li&gt;
  &lt;li&gt;Một người học muốn bắt đầu tuần 2 bằng việc chạy notebook huấn luyện mô hình ACT trong LeRobot. Họ đang bỏ qua những mốc nền tảng nào, và những hành vi vật lý nào của robot chưa hề được notebook đó kiểm chứng?&lt;/li&gt;
  &lt;li&gt;Giả sử lệnh điều khiển được gửi đều đặn mỗi $100\,\mathrm{ms}$. Cần bao nhiêu chu kỳ mất lệnh liên tiếp để thỏa mãn điều kiện $t_{\mathrm{silence}} &amp;gt; 300\,\mathrm{ms}$? Sau đúng ba chu kỳ mất lệnh, động cơ đã bắt buộc phải dừng chưa?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Gợi ý đáp án&lt;/summary&gt;

1. Một phát biểu đạt yêu cầu: &quot;Capstone A đạt khi xe vi sai hai bánh của tôi, trong trạng thái bánh nhấc khỏi mặt bàn, chạy theo lệnh teleop qua serial hoặc một trang Wi-Fi đơn giản, và firmware tự đưa duty PWM về 0 nếu không nhận được lệnh mới trong hơn 300 ms.&quot; Bản demo này nằm trong khoảng tuần 7–11, với mốc nghiệm thu ở Chương 07.
2. Kế hoạch P có $4 \times 16 = 64$ giờ; Kế hoạch Q có $6 \times 12 = 72$ giờ. Kế hoạch P giãn thời gian rộng hơn nên có thể sắp xếp Chương 11–12 sau khi hoàn tất Chương 00–10. Kế hoạch Q có tổng giờ cao hơn nhưng gói gọn trong 12 tuần nên không có tuần dư, do đó giai đoạn tuần 7–11 vẫn phải dành trọn cho bài nghiệm thu nhấc bánh.
3. Bạn không biết mạch thực sự đã in ra dữ liệu gì trên cổng serial, và cũng không biết trước khi LED sáng đã từng xảy ra sự cố gì. Nếu lần sau mạch gặp lại lỗi tương tự, bạn không có mốc dữ liệu để đối chiếu.
4. Người học đã bỏ qua giai đoạn firmware, mạch lái động cơ và bài nghiệm thu Chương 07. Notebook huấn luyện không kiểm chứng được việc robot có tự dừng khi mất lệnh teleop quá 300 ms hay không, cũng không kiểm tra được nguồn động cơ có đang cách ly an toàn khỏi chân logic 3,3 V hay không.
5. Sau đúng 3 chu kỳ mất lệnh, $t_{\mathrm{silence}} = 300\,\mathrm{ms}$, chưa vượt quá ngưỡng chặt $300\,\mathrm{ms}$ nên động cơ vẫn được phép chạy. Đến chu kỳ mất lệnh thứ tư, $t_{\mathrm{silence}} = 400\,\mathrm{ms} &amp;gt; 300\,\mathrm{ms}$ và duty PWM bắt buộc phải về 0.

&lt;/details&gt;

&lt;h2 id=&quot;đọc-thêm&quot;&gt;Đọc thêm&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/&quot;&gt;Tài liệu ROS 2 Jazzy&lt;/a&gt; — tầng truyền thông sẽ dùng ở giai đoạn sau. Bạn có thể đọc phần giới thiệu để hình dung về topic, nhưng chưa cần cài đặt từ bài này.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/huggingface/lerobot&quot;&gt;LeRobot&lt;/a&gt; — thư viện dành cho Chương 11 và 12. Việc tải mã nguồn ngay tuần đầu không giúp lắp ráp khung xe.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.duckietown.com/&quot;&gt;Tài liệu Duckietown&lt;/a&gt; — một hệ thống robot giáo dục có tài liệu công khai đầy đủ, hữu ích để tham khảo cách tổ chức tài liệu cho xe tự hành nhỏ (không phải danh sách mua sắm của khóa này).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://manipulation.mit.edu/&quot;&gt;Khóa manipulation của MIT&lt;/a&gt; — tài liệu chuyên sâu về điều khiển thao tác và chính sách học máy; phù hợp để đọc thêm sau khi robot của bạn đã biết dừng an toàn.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Home simulation lab</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter12/12_08_home_simulation_lab/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter12/12_08_home_simulation_lab</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;40–60 minutes&lt;/strong&gt; to read and pick one path that matches the machine you already have. This lesson does not ask you to buy a workstation or rent a GPU during the core course weeks.&lt;/p&gt;

&lt;p&gt;This is a generic guide for learners who have finished the course path: when you need Isaac Sim, when Isaac Lab or MuJoCo is enough, and the first hardware step if you later want a humanoid. It does &lt;strong&gt;not&lt;/strong&gt; replace the Capstone kit, it is not required for the Chapter 12 checklist, and it is not a shopping list you must follow part by part.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end you can tell the four paths apart (no NVIDIA GPU / Mac, hourly cloud GPU, a 16 GB entry card, and the official Isaac Sim “Good” tier), state the official Isaac Sim baseline correctly, and write down one first experiment that fits your machine. You also know why a full humanoid is the wrong first build.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You have read lesson 12-05 and chosen a direction for the coming month. If you care about sim-to-real, revisit lesson 10-05. You do not need Isaac Sim installed.&lt;/p&gt;

&lt;h2 id=&quot;four-paths-in-brief&quot;&gt;Four paths, in brief&lt;/h2&gt;

&lt;p&gt;Isaac Sim and Isaac Lab do &lt;strong&gt;not&lt;/strong&gt; share the same hardware floor.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Isaac Sim&lt;/strong&gt; (scene authoring, RTX rendering, Asset Search): the official &lt;a href=&quot;https://docs.isaacsim.omniverse.nvidia.com/latest/installation/requirements.html&quot;&gt;requirements&lt;/a&gt; (the “latest” docs as of 2026-11-30) list a minimum x86_64 workstation as Ubuntu 22.04 or 24.04, or Windows 11; 32 GB RAM; about 50 GB of SSD; and a GeForce &lt;strong&gt;RTX 4080&lt;/strong&gt; with &lt;strong&gt;16 GB&lt;/strong&gt; VRAM. The “Good” column is an RTX 5080 (the table still shows 16 GB VRAM, at the named GPU class). GPUs &lt;strong&gt;without RT Cores&lt;/strong&gt; (A100, H100) &lt;strong&gt;are not supported&lt;/strong&gt;. Ubuntu 22.04 is deprecated and will be removed in a later release; Windows 10 is not supported. The Isaac Sim container runs only on Linux. Isaac Lab training on top of Isaac Sim usually needs extra RAM and VRAM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Isaac Lab&lt;/strong&gt; (Newton/Warp by default, Isaac Sim optional): the &lt;a href=&quot;https://docs.nvidia.com/isaaclab/isaac-lab/installation&quot;&gt;install guide&lt;/a&gt; says the default training stack needs an NVIDIA GPU that is &lt;strong&gt;Turing or newer&lt;/strong&gt; (compute capability 7.5+). Headless Newton simulation &lt;strong&gt;does not require RTX&lt;/strong&gt;. The RTX 4080 floor above applies only when you want RTX rendering or an Isaac Sim workflow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pick &lt;strong&gt;one&lt;/strong&gt; path. Do not stand up all four at once.&lt;/p&gt;

&lt;h3 id=&quot;1-a-mac-or-any-machine-without-an-nvidia-gpu&quot;&gt;1. A Mac, or any machine without an NVIDIA GPU&lt;/h3&gt;

&lt;p&gt;You can still learn most of the simulation ideas.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Inspect OpenUSD files (mass, joints, collision) with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usdview&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usd-core&lt;/code&gt;, and look at meshes in Blender. That is asset reading, not a full physics run in Isaac Sim — treat it as a preview.&lt;/li&gt;
  &lt;li&gt;Run &lt;strong&gt;sim-to-sim&lt;/strong&gt; in MuJoCo, for example a &lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt; policy check from the project docs (lesson 10-05).&lt;/li&gt;
  &lt;li&gt;Browse &lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt;, read each package’s profile report, and note the license before you download.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you need a real Isaac Sim session or a heavy training run, use path 2 by the hour, then shut the instance down.&lt;/p&gt;

&lt;h3 id=&quot;2-rent-a-cloud-gpu&quot;&gt;2. Rent a cloud GPU&lt;/h3&gt;

&lt;p&gt;This fits a few Isaac Sim or Isaac Lab sessions before you buy a tower.&lt;/p&gt;

&lt;p&gt;A practical card is the &lt;strong&gt;L40S&lt;/strong&gt; (48 GB, with RT Cores — not an A100 or H100). Public &lt;a href=&quot;https://www.runpod.io/pricing&quot;&gt;RunPod&lt;/a&gt; list prices were about &lt;strong&gt;USD 0.79/hour&lt;/strong&gt; (Community Cloud) and &lt;strong&gt;USD 1.09/hour&lt;/strong&gt; (Secure Cloud) as of 2026-11-30; other hosts such as Brev were often around &lt;strong&gt;USD 1.1–1.2/hour&lt;/strong&gt;. Rates move by day and region: recheck the vendor page before you start, stop the machine when you finish, and read the terms, region, and account rules.&lt;/p&gt;

&lt;p&gt;Do not pick an A100 or H100 for Isaac Sim. Keep the course habit: do not leave a training job running unattended for hours.&lt;/p&gt;

&lt;h3 id=&quot;3-entry-tier--a-16-gb-card-at-home&quot;&gt;3. Entry tier — a 16 GB card at home&lt;/h3&gt;

&lt;p&gt;Isaac Sim’s official VRAM floor is 16 GB, but the named GPU is an &lt;strong&gt;RTX 4080&lt;/strong&gt;. A cheaper 16 GB card (for example an RTX 5060 Ti 16 GB) &lt;strong&gt;meets the VRAM number&lt;/strong&gt; but is &lt;strong&gt;not&lt;/strong&gt; in NVIDIA’s “Minimum” GPU column; treat it as an experiment box for headless Isaac Lab and light scenes, not a promise that every Isaac Sim tutorial will run. An RTX 5070 12 GB &lt;strong&gt;misses&lt;/strong&gt; the 16 GB floor.&lt;/p&gt;

&lt;p&gt;Approximate list prices on GearVN, &lt;strong&gt;as of 2026-11-30&lt;/strong&gt;, always to be rechecked:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;RTX 5060 Ti 16 GB&lt;/td&gt;
      &lt;td&gt;14–21 million&lt;/td&gt;
      &lt;td&gt;Meets VRAM; below the official Isaac Sim GPU class&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;RTX 4080 Super (if still listed)&lt;/td&gt;
      &lt;td&gt;32–35 million&lt;/td&gt;
      &lt;td&gt;Close to the named minimum GPU; stock moves quickly&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;32 GB DDR5&lt;/td&gt;
      &lt;td&gt;3–8 million&lt;/td&gt;
      &lt;td&gt;Isaac Sim’s RAM floor is 32 GB, not 16 GB&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2 TB SSD&lt;/td&gt;
      &lt;td&gt;3–10 million&lt;/td&gt;
      &lt;td&gt;The “Good” column starts at 500 GB; 2 TB is more comfortable for datasets&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;850 W PSU&lt;/td&gt;
      &lt;td&gt;2.3–4 million&lt;/td&gt;
      &lt;td&gt;Size the supply to the card you actually buy&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A prebuilt tower with an RTX 5070 Ti was listed around 79 million, but that configuration shipped with 16 GB of RAM: you would still need to raise it to at least 32 GB for Isaac Sim. Those figures exclude tax, differing warranties, and used markets.&lt;/p&gt;

&lt;p&gt;OS: Ubuntu 24.04 is the more durable Linux choice (22.04 is on the way out); Windows 11 is also in the support table. macOS does not run Isaac Sim.&lt;/p&gt;

&lt;h3 id=&quot;4-full-tier--the-good-column-rtx-5080&quot;&gt;4. Full tier — the “Good” column: RTX 5080&lt;/h3&gt;

&lt;p&gt;If you will author Isaac Sim scenes often, use RTX rendering, or train with cameras, aim at the &lt;strong&gt;Good&lt;/strong&gt; column: an RTX 5080, 64 GB of RAM if you can, and a large SSD. A standalone RTX 5080 was listed on GearVN at about &lt;strong&gt;39–46 million VND&lt;/strong&gt; (2026-11-30, list prices, a range only). Add a CPU, board, 32–64 GB of RAM, an SSD, and a PSU; a complete machine costs substantially more. An RTX 5090 (cards listed around 104–130 million) is more than this course needs as a starting point.&lt;/p&gt;

&lt;h2 id=&quot;hardware-step-one-cycloidal-actuator-not-a-whole-humanoid&quot;&gt;Hardware step: one cycloidal actuator, not a whole humanoid&lt;/h2&gt;

&lt;p&gt;If your path is an open-source humanoid (for example Berkeley Humanoid Lite in lessons 06-03 and 10-05), &lt;strong&gt;do not&lt;/strong&gt; print and assemble a 16 kg robot first. A better first step is &lt;strong&gt;one&lt;/strong&gt; 3D-printed cycloidal actuator (6512 class) on a bench: supply, driver, encoder, angle and torque and backlash measurements, with a hardware power switch in reach.&lt;/p&gt;

&lt;p&gt;The public 6512 actuator BOM (motor, driver, bearings, filament) is about &lt;strong&gt;USD 190&lt;/strong&gt; at U.S. prices in the project docs; in Vietnam you must add shipping and equivalent local parts. Follow the &lt;a href=&quot;https://berkeley-humanoid-lite.gitbook.io/docs&quot;&gt;hardware documentation&lt;/a&gt; and the check in lesson 06-03. Remember: printed gearboxes wear; the v1.1.0 notes already point V2 toward off-the-shelf actuators.&lt;/p&gt;

&lt;h2 id=&quot;first-experiments-pick-one&quot;&gt;First experiments (pick one)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;No NVIDIA GPU:&lt;/strong&gt; download one SimReady package, read the license, open the USD in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usdview&lt;/code&gt; or Blender, and write down which profile features (joints, mass, collision) the package actually contains.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;MuJoCo / sim-to-sim:&lt;/strong&gt; run a Berkeley Humanoid Lite policy check on the machine you have, against the lesson 10-05 checklist.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;A Turing-or-newer GPU (including an hourly rental):&lt;/strong&gt; install Isaac Lab (headless Newton), open a small env (Cartpole, or a locomotion env) and read the observation and action spaces. You do not need to train to convergence.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;A machine that meets the Isaac Sim floor:&lt;/strong&gt; in &lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt; find a UR arm and a Robotiq gripper, then follow the &lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-assets-robot-pick-and-place&quot;&gt;pick-and-place walkthrough&lt;/a&gt; (UR17G + Robotiq 2F-140 + a beverage can) in Isaac Sim. Test joints, a small motion, and contact before you run the full grasp cycle.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Actuator bench:&lt;/strong&gt; print and assemble one cycloidal gearbox, run it under a light load, and record backlash; do not jump to a full leg or torso.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Generating props with the NVIDIA AI Blueprint for 3D object generation is optional later work: a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.glb&lt;/code&gt; mesh is &lt;strong&gt;not&lt;/strong&gt; yet SimReady (see the course reference page).&lt;/p&gt;

&lt;h2 id=&quot;safety&quot;&gt;Safety&lt;/h2&gt;

&lt;p&gt;Actuator motor drives and LiPo packs are not toys. Keep a hardware power switch in reach, as in Chapters 00 and 10. Vent a 3D printer. On a cloud host, stop the instance when you are done.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Write one sentence: which path (1–4) matches the machine you have, and which first experiment you will run. If you do not have the hardware, path 1 or 2 is a valid answer.&lt;/li&gt;
  &lt;li&gt;Does Isaac Sim run on an A100? Why? Guidance: it is not supported; those GPUs have no RT Cores.&lt;/li&gt;
  &lt;li&gt;A PC with an RTX 5060 Ti 16 GB and 16 GB of RAM is missing what, relative to the Isaac Sim minimum table?&lt;/li&gt;
  &lt;li&gt;Give one reason to bench a single actuator before you build a whole humanoid.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;The sentence names one path and one experiment; “do all of them” is not done. 2. No; A100/H100 have no RT Cores, and the Isaac Sim docs say they are not supported. 3. The named RTX 4080 GPU class, and the 32 GB RAM floor. 4. You prove the gearbox, backlash, and wiring before multiplying them across many joints; printed cycloidal boxes also wear and are weaker than metal.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.isaacsim.omniverse.nvidia.com/latest/installation/requirements.html&quot;&gt;Isaac Sim requirements&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nvidia.com/isaaclab/isaac-lab/installation&quot;&gt;Isaac Lab installation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central on Hugging Face&lt;/a&gt; and the &lt;a href=&quot;https://docs.omniverse.nvidia.com/simready/latest/simready-central.html&quot;&gt;docs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-assets-robot-pick-and-place&quot;&gt;Pick-and-place with SimReady assets&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt; — hardware docs and sim2sim&lt;/li&gt;
  &lt;li&gt;The course reference page (SimReady Central and the 3D object-generation blueprint)&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Curated next paths and full references</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter12/12_05_next_paths_refs/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter12/12_05_next_paths_refs</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~60 minutes&lt;/strong&gt;. Ten minutes on the figure, twenty on picking a path, thirty on a one-month plan you can reread without buying anything tonight.&lt;/p&gt;

&lt;p&gt;Finishing a first robotics course is not the end of the road—it is the moment the map finally becomes legible. Every specialization you might pursue next—from embedded real-time firmware and nonlinear control to visual SLAM, legged locomotion, or robot foundation models—builds directly on the exact stack you have assembled from Chapter 01 to Chapter 12.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will choose one path for the next month, not three. Path A stays on the field base: Nav2, a lidar when you are ready, and linorobot2 as a reference diff-drive stack while your own bridge remains the motor path. Path B is an arm and a LeRobot dataset, only if you later decide to buy hardware. Path C is theory: Modern Robotics, which is free, and the MIT manipulation course. You will write a plan that names the Capstone you already have and one safety habit you will not drop. You will leave the reference list as a map, not as five papers to finish over a weekend.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Chapters 08 through 11 are behind you in the course sequence, even if some labs are still rough. You can say what the 300 ms timeout does, what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; carries, and why a policy from another gripper is not a controller for your wheels. You have the dataset-card habit from lesson 12-01. You do not need a new board, a new sensor, or a clean build of Nav2 to write the plan. The plan comes first so the shopping tab does not write it for you.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-the-full-robotics-stack-and-four-specialization-trajectories&quot;&gt;Key ideas: The Full Robotics Stack and Four Specialization Trajectories&lt;/h2&gt;

&lt;p&gt;Seeing how every advanced subfield attaches to your existing Capstone foundation keeps your next learning steps focused:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Controls &amp;amp; State Estimation Track:&lt;/strong&gt; Extends Chapter 08 PID and kinematics into state-space control (LQR, MPC), Extended/Unscented Kalman Filters (EKF/UKF), and factor-graph estimation (GTSAM)—ideal for drones, self-driving vehicles, and legged robots.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Perception &amp;amp; Spatial AI Track:&lt;/strong&gt; Extends Chapter 11 LiDAR SLAM and pinhole vision into Visual-Inertial Odometry (VIO), 3D point-cloud registration, and neural scene representations (3D Gaussian Splatting / semantic mapping).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Manipulation &amp;amp; Robot Learning Track:&lt;/strong&gt; Extends MoveIt 2 and Chapter 12 LeRobot/ACT/Diffusion Policy into whole-body motion planning, contact-rich force control, sim-to-real reinforcement learning (Isaac Lab / MuJoCo), and VLA fine-tuning.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The course ran from a diff-drive you can hold to papers that publish actions for robots you do not have. The honest ending is one thread for a month. A weekend that opens RT-2, Diffusion Policy, a Nav2 tutorial, a gripper listing, and a Jetson listing finishes none of them. One path for a month beats five papers in a weekend. Timeouts, units, notes, and a power switch still apply on every path.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;Three paths. You will write one of them into the lab. You still need to recognize the other two, so a forum post does not drag you sideways in week one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Path A, field base.&lt;/strong&gt; You already have the robot. Chapter 11 toured a map, a planner, and a controller on Jazzy and did not finish them. The matching tutorial is the &lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/&quot;&gt;Nav2 getting started guide for Jazzy&lt;/a&gt;. Those pages expect a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt; from a small 360° lidar, not a single-point ranger. Budget 1.5–3.5 million VND and recheck when you are ready. Buying this week is not required to choose the path. &lt;a href=&quot;https://github.com/linorobot/linorobot2&quot;&gt;linorobot2&lt;/a&gt; is a reference for how a bringup, a URDF, and a Nav2 config sit together. You do not flash it over your firmware. Your bridge, your serial frame, and the 300 ms timeout stay. Two writers on the motors is a bug. One writer, yours, keeps the timeout in the process that writes PWM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Path B, arm and data.&lt;/strong&gt; LeRobot is the software entry if you later want joint-space demonstrations. SO-100-class arms, Koch, and LeKiwi are that project’s bodies. A dataset card comes before a payment: keys, units, fps, and the timeout still in firmware. An arm, a lidar, and a second computer do not arrive together. A leader-follower kit is often many millions of VND. These notes do not invent a shop URL. If you cannot name the action vector, you are not ready to shop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Path C, theory.&lt;/strong&gt; &lt;a href=&quot;https://hades.mech.northwestern.edu/index.php/Modern_Robotics&quot;&gt;Modern Robotics&lt;/a&gt; (Lynch and Park) is free on that site. A month is one chapter tied to the base, not the book. Copy the wheeled-mobile title you see on the page, the chapter that speaks \(v\) and \(\omega\). The &lt;a href=&quot;https://manipulation.csail.mit.edu/&quot;&gt;MIT manipulation course&lt;/a&gt; explains why VLA papers care about grippers you do not own. Read the notes. Buying a 6-DOF arm to follow the videos spends Path B’s money for Path C’s reason.&lt;/p&gt;

&lt;p&gt;The failure mode is a 6-DOF arm, a lidar, and a Jetson-class board in the same week. None of those removes the timeout on the Capstone, the only robot whose cutoff you have tested. The purchase that can wait, waits.&lt;/p&gt;

&lt;p&gt;Safety habits that are not optional on any path:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The 300 ms command timeout stays in the firmware you flash.&lt;/li&gt;
  &lt;li&gt;The first motion test of any new publisher is wheels-up, or the chassis on a stand, until the command is the command you think it is.&lt;/li&gt;
  &lt;li&gt;A hardware E-stop or a power switch you can hit without a keyboard stays in reach. A policy, a planner, and a typed “stop” can all fail with the node still alive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Units stay on the path too. Path A speaks &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;. Path B will speak joint targets the day an arm exists, and those targets do not overwrite \(v\) and \(\omega\) in the same column. Path C’s derivatives are only useful if you still know which number is radians per second on the wire.&lt;/p&gt;

&lt;h2 id=&quot;the-figure&quot;&gt;The figure&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_paths.png&quot; alt=&quot;Three paths for the next month&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Three boxes, one month, one choice. The green box is the field base: Nav2, a lidar, linorobot2 as a reference. The yellow box is arm and data: LeRobot and an SO-100-class arm, later. The blue box is theory: Modern Robotics and the MIT course. Under them: timeouts, units, notes, and a power switch still apply on every path. The bold line is the pace. You may read the title of a paper on a path you did not choose. You may not make that paper the second project.&lt;/p&gt;

&lt;h2 id=&quot;worked-reading-example&quot;&gt;Worked reading example&lt;/h2&gt;

&lt;p&gt;A plan that can be checked, unlike “path A and a bit of B, be careful, buy a lidar and an arm”:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;path: A
robot I already have: Capstone diff-drive, the one with the serial bridge
safety habit I will not drop: 300 ms with no fresh frame sets v = 0 and w = 0
also still true: wheels-up before any new publisher; power switch in reach
this month I will not buy: an arm, a Jetson-class board
first page I will open: https://docs.nav2.org/jazzy/getting_started/
lidar: not this week. If the base still tracks cmd_vel in a month, recheck a small 360 unit in the 1.5–3.5 million VND band
reference I will read and not flash over my firmware: https://github.com/linorobot/linorobot2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That plan names one path, the Capstone, and a 300 ms habit. linorobot2 is a reading reference, not a new motor driver. A small 360° lidar is about 1.5–3.5 million VND. A Pi 5, if the laptop is outgrown, starts near 2.4 million VND for a low-RAM board at &lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Hshop&lt;/a&gt;. Recheck the variant. An arm is the expensive optional path and is not priced here. Lidar or Pi, not both on day one, and not beside an arm.&lt;/p&gt;

&lt;h3 id=&quot;explanation-auditing-your-own-stack-readiness-with-a-concrete-bottleneck-check&quot;&gt;Explanation: Auditing Your Own Stack Readiness with a Concrete Bottleneck Check&lt;/h3&gt;

&lt;p&gt;When choosing your next project, avoid picking blindly by hype; instead, run a quick quantitative bottleneck audit on your current robot stack:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;List your four measured loop rates and bottlenecks:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Low-level MCU wheel PID: \(50\ \text{Hz}\) (period \(20\ \text{ms}\), jitter \(&amp;lt; 1\ \text{ms}\)).&lt;/li&gt;
      &lt;li&gt;Wheel + IMU odometry (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;): \(30\ \text{Hz}\) (drift \(\approx 3\%\) over 10 m).&lt;/li&gt;
      &lt;li&gt;2D LiDAR SLAM / Nav2 local planner: \(10\ \text{Hz}\) (CPU load 45% on SBC).&lt;/li&gt;
      &lt;li&gt;Vision / learned policy inference: \(5\ \text{Hz}\) (latency \(180\ \text{ms}\)).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Match the next skill track to the actual limiting factor:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;If your robot drifts \(&amp;gt; 5\%\) before loop closure \(\rightarrow\) study &lt;strong&gt;EKF sensor fusion&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_localization&lt;/code&gt;) and revisit Chapter 06/08 calibration first.&lt;/li&gt;
      &lt;li&gt;If navigation stalls in narrow corridors \(\rightarrow\) dive into &lt;strong&gt;Nav2 Smac/MPPI tuning&lt;/strong&gt; and Behavior Trees.&lt;/li&gt;
      &lt;li&gt;If mobile navigation is rock-solid and you want dexterous object interaction \(\rightarrow\) step into &lt;strong&gt;MoveIt 2&lt;/strong&gt; or a &lt;strong&gt;LeRobot/ACT&lt;/strong&gt; tabletop arm build.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Measuring before specializing ensures every new mathematical tool solves a real problem on your bench.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Write the plan into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; as a block you can follow for four weeks. Required lines:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;path: A or B or C
robot I already have: Capstone diff-drive
safety habit I will not drop: &amp;lt;one of: 300 ms timeout, wheels-up first motion, hardware E-stop&amp;gt;
this month I will not buy:
first link I will actually open:
what I will have in my notes after week one:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Week-one evidence depends on the letter. Path A: which Nav2 page you opened, and which part of linorobot2 you refused to copy onto the motor driver. Path B: a dataset card for an arm you do not own, joint units marked unknown until a servo datasheet, and \(v\), \(\omega\) kept on their own keys. Path C: a chapter or lecture title copied from the site, plus one relation you can tie to the base, or the question you will ask next week. Do not invent a title.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Expected.&lt;/strong&gt; Exactly one path letter. The robot line says Capstone, not “a mobile robot.” The safety line has a concrete habit, and the 300 ms timeout is either that habit or is still listed as remaining in force. The “will not buy” line includes at least the two items that are not on your path. There is no cart screenshot.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Plan&lt;/th&gt;
      &lt;th&gt;Problem&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Path “A+B+C, lightly”&lt;/td&gt;
      &lt;td&gt;That is the five-papers weekend in disguise. Pick one letter&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;First action is an arm, a lidar, and a Jetson in one order&lt;/td&gt;
      &lt;td&gt;You bought three paths. The lab asked for a month on one&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Path C, and the timeout is “not relevant because I am only reading”&lt;/td&gt;
      &lt;td&gt;Reading does not delete the firmware on the robot in the corner. The habit stays in force the next time you flash&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;linorobot2 flashed as the motor driver on day two&lt;/td&gt;
      &lt;td&gt;You replaced a timeout you understand with a stack you have not read. Read it. Keep your bridge&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Week-one note is “watch some videos”&lt;/td&gt;
      &lt;td&gt;Name the page and the paragraph you will be able to summarize&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;p&gt;Each answer names a path. A sentence that could apply to all three paths is not done.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Path A. Name one thing you will copy from a reading of linorobot2, and one thing you will not replace. Guidance: a fair copy is the shape of a bringup launch or the way a URDF names &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;. The thing you will not replace is your serial bridge and the 300 ms cutoff in the firmware that writes PWM. “I will use their stack” is the wrong answer.&lt;/li&gt;
  &lt;li&gt;Path B. Write the action keys you would insist on seeing before you pay for an SO-100-class arm. Guidance: joint names, units from the servo datasheet (radians or encoder counts, not a shrug), fps, and a camera key. Add a line that \(v\) and \(\omega\) of the Capstone are absent from those joint columns. If you cannot write the keys, the path is a later month.&lt;/li&gt;
  &lt;li&gt;Path C. From the Modern Robotics site, copy the wheeled-mobile title you will open, and name the Capstone quantity it should explain. Guidance: \(v\) and \(\omega\), or wheel rates into that pair. A chapter on grasps is the wrong click. The MIT course may replace the book if you quote its lecture title and still name \(v\) and \(\omega\).&lt;/li&gt;
  &lt;li&gt;Path A budget. You have not earned a lidar until &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; matches the bridge, wheels-down, with the timeout proven. What VND band will you recheck, and what if the listing is above it? Guidance: 1.5–3.5 million VND for a small 360° unit. Above the band, you wait. You do not add an arm to “complete the set.”&lt;/li&gt;
  &lt;li&gt;Write the week-four check for the path you chose, as a yes/no question your notes can answer. Guidance: Path A asks whether you can point at the Nav2 page that consumes your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; and at the firmware line that still times out at 300 ms. Path B asks whether the card uses one unit system and whether you bought nothing. Path C asks whether you can restate the wheeled relation and whether the timeout is still in the last firmware you flashed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;curated-references&quot;&gt;Curated references&lt;/h2&gt;

&lt;p&gt;The links the course actually uses, gathered so a later month does not depend on a search. Open the one that matches your path. Skim titles of the rest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The stack you already started&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/&quot;&gt;ROS 2 Jazzy documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/&quot;&gt;Nav2 getting started, Jazzy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/ros_installation&quot;&gt;Installing Gazebo with ROS, Harmonic&lt;/a&gt;. Jazzy’s paired simulator in that guide is Gazebo Harmonic.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/SteveMacenski/slam_toolbox&quot;&gt;slam_toolbox&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/linorobot/linorobot2&quot;&gt;linorobot2&lt;/a&gt;, a reference diff-drive stack. Keep your own bridge.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Learning libraries and papers, for reading&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;LeRobot docs&lt;/a&gt; and &lt;a href=&quot;https://github.com/huggingface/lerobot&quot;&gt;huggingface/lerobot&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.13705&quot;&gt;Zhao et al., ACT, arXiv:2304.13705&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2303.04137&quot;&gt;Chi et al., Diffusion Policy, arXiv:2303.04137&lt;/a&gt; and the &lt;a href=&quot;https://diffusion-policy.cs.columbia.edu/&quot;&gt;project page&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://robotics-transformer1.github.io/&quot;&gt;RT-1&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/2212.06817&quot;&gt;arXiv:2212.06817&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;RT-2, arXiv:2307.15818&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://robotics-transformer-x.github.io/&quot;&gt;Open X-Embodiment&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2406.09246&quot;&gt;OpenVLA, arXiv:2406.09246&lt;/a&gt; and &lt;a href=&quot;https://openvla.github.io/&quot;&gt;openvla.github.io&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pi.website/blog/pi0&quot;&gt;pi0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Home simulation / Isaac Lab, for later reading, not this month’s path&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/contents/en/chapter12/12_08_home_simulation_lab/&quot;&gt;Home simulation lab&lt;/a&gt; — optional lesson: Isaac Sim and Isaac Lab requirements, a Mac or hourly-cloud path, approximate Vietnam GPU price bands (2026-11-30), and a first hardware step of one cycloidal actuator rather than a whole humanoid.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt; — OpenUSD assets that already carry physics (Alpha, 2,000+ assets). Pick-and-place and sim-to-real notes are in lessons 12-01 and 10-05.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;An open-source humanoid, for later reading, not this month’s path&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt; (Hybrid Robotics, UC Berkeley; RSS 2025, &lt;a href=&quot;https://arxiv.org/abs/2504.17249&quot;&gt;arXiv:2504.17249&lt;/a&gt;) — a 3D-printed open-source humanoid with RL locomotion and zero-shot sim-to-real transfer. It fits branches 3–4 at the top of this lesson, after you have already chosen this month’s path. The cycloidal-ratio check is in lesson 06-03; the sim-to-real reading is in lesson 10-05.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Theory, free to read&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hades.mech.northwestern.edu/index.php/Modern_Robotics&quot;&gt;Modern Robotics&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://manipulation.csail.mit.edu/&quot;&gt;MIT manipulation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;A computer, only when Path A outgrows the laptop&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Raspberry Pi 5 at Hshop&lt;/a&gt;. Low-RAM boards have been listed from about 2.4 million VND. Recheck the variant.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Vision-Language-Action models overview</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter12/12_04_vla_overview/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter12/12_04_vla_overview</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~75 minutes&lt;/strong&gt;. You will read two systems carefully enough to fill a table, and you will leave the other three as names you can return to. No checkpoint is loaded.&lt;/p&gt;

&lt;p&gt;Imagine telling a robot in plain English, &lt;em&gt;“pick up the ripe banana and place it in the blue bowl,”&lt;/em&gt; and watching a single neural model read both the camera pixels and your sentence to emit smooth joint commands—even for a bowl color it never saw during teleoperation. &lt;strong&gt;Vision-Language-Action (VLA) models&lt;/strong&gt; graft robot action heads onto pretrained Vision-Language Models (VLMs), importing internet-scale semantic generalization into physical control.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will define a vision-language-action model as a map from an image plus a text instruction to an action on a particular robot. You will place RT-1, RT-2, Open X-Embodiment, OpenVLA, and pi0 on that map: what each one reads, what it writes, and whether those writes are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; or joint targets for the Capstone. You will state the embodiment gap in one concrete way, and you will keep the motor timeout above any sentence a person types. The lab table’s “train at home” cell for RT-2 is an honest no.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can read a project page. Lessons 12-01 through 12-03 gave you dataset keys, a chunk, and a reason not to average two demonstrations. Chapter 09’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; is still &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; in m/s and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; in rad/s. Chapter 11’s camera, if you have one, is a sensor on your base, not a wrist camera on a parallel gripper. A GPU-less laptop is the expected machine.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-pretrained-vlm-backbones-action-tokenization-and-hierarchical-system-12-control&quot;&gt;Key ideas: Pretrained VLM Backbones, Action Tokenization, and Hierarchical System 1/2 Control&lt;/h2&gt;

&lt;p&gt;Understanding modern VLAs (such as RT-2, OpenVLA, and \(\pi_0\)) connects three architectural ideas:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Vision-Language Backbone Transfer:&lt;/strong&gt; Pretraining on billions of image–text pairs teaches a VLM what “ripe,” “blue bowl,” and spatial relations (“to the left of”) mean. Fine-tuning on robot trajectories aligns those semantic representations directly to physical actions.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Discrete Action Tokenization vs. Continuous Flow/Diffusion Heads:&lt;/strong&gt; Early VLAs (OpenVLA/RT-2) quantize each continuous DOF into \(B = 256\) discrete bins and predict them as language tokens; newer architectures (e.g., \(\pi_0\)) attach a fast continuous flow-matching or diffusion action expert to avoid quantization jaggedness.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Latency &amp;amp; Safety Envelopes (System 2 vs. System 1):&lt;/strong&gt; Large 7B-parameter VLAs run at ~2–10 Hz on edge GPUs, so real deployments pair them with a high-rate local safety controller (50–200 Hz PID/impedance + joint-limit clamps) so a slow neural forward pass never destabilizes the motors.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Jazzy, Nav2, and the bridge made it possible to type a goal and watch a diff-drive track a path. The papers in this lesson type a goal in natural language and expect a network to emit the motor command. That is a real research line, and it is easy to misread as “the robot now understands sentences, so the firmware can retire.” It cannot. A checkpoint is a function from the cameras and the action vector of the robots that generated its data. RT-1’s mobile manipulator, a gripper in Open X-Embodiment, and the Capstone do not share that vector. Language does not repair the mismatch. This lesson is how you read the claim without pointing the claim at your wheels.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A vision-language-action model, a VLA, takes pixels and a string and returns an action. The string is an instruction: “pick up the apple,” “move to the door.” The action is whatever the training robot executed, tokenized or continuous. The promise, when it works, is that you can ask for a task in words instead of only in a joystick log. The limit, which the figure prints in red, is that another lab’s gripper is a different robot. Embodiment is part of the model. Swap the body and the same weights are a confident function aimed at the wrong motors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RT-1&lt;/strong&gt; (Robotics Transformer, Brohan et al.) is trained on demonstrations from the authors’ mobile manipulators. Read &lt;a href=&quot;https://robotics-transformer1.github.io/&quot;&gt;the project page&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/2212.06817&quot;&gt;arXiv:2212.06817&lt;/a&gt;. Input is a camera image plus a text instruction. Output is a discretized vector for that platform: arm, base, and gripper. Their base channel is not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; on your graph. You can read the page. You cannot drop the behavior onto the Capstone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RT-2&lt;/strong&gt; (&lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;arXiv:2307.15818&lt;/a&gt;) co-fine-tunes a vision-language model so robot actions are more tokens in the same stream. A phrase the robot never practiced can steer a skill it did practice, because the language side already knows the words. The fine-tune is a data-center job. “Understands language” is incomplete unless the next sentence names whose arms produced the action tokens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open X-Embodiment&lt;/strong&gt; (&lt;a href=&quot;https://robotics-transformer-x.github.io/&quot;&gt;project page&lt;/a&gt;) pools many labs and many robots, and trains RT-X models on that pool. A wrist camera, a parallel gripper, and a two-wheel base still contribute different images and different action vectors. A shared column name does not share the physics. Your problem is \(v\) and \(\omega\) under a forward camera, with a 300 ms timeout. A pile of other robots is not a brain for the base you left out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenVLA&lt;/strong&gt; (&lt;a href=&quot;https://arxiv.org/abs/2406.09246&quot;&gt;arXiv:2406.09246&lt;/a&gt;, &lt;a href=&quot;https://openvla.github.io/&quot;&gt;project page&lt;/a&gt;) publishes weights: image and instruction in, discretized arm-style actions out, trained on Open X-Embodiment data. Open weights mean a prepared machine can load a file. They do not mean a GPU-less laptop can train a multi-billion-parameter network, or run it at 50 Hz between serial frames. The action is an arm’s motion plus a gripper, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pi0&lt;/strong&gt; is the flow-matching end of the same idea. Read the &lt;a href=&quot;https://www.pi.website/blog/pi0&quot;&gt;Physical Intelligence blog&lt;/a&gt;. Images and language go in. Continuous actions come out, generated by a flow, a cousin of the denoiser in the previous lesson. The robots on the page are theirs. If the wording has shifted, quote what you see, and write that those robots are not the Capstone.&lt;/p&gt;

&lt;p&gt;A language command is not an E-stop. The string “stop” is another token. The network may still emit a small forward action. The 300 ms timeout does not parse English. It only notices that serial frames stopped. A live policy that keeps publishing resets the timer, just as a bad ACT chunk did. A typed stop is safe only after your code writes zero PWM, a zero &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;, or a hardware cutoff. Wheels-up remains the rule the first time a new node publishes.&lt;/p&gt;

&lt;h2 id=&quot;the-figure&quot;&gt;The figure&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_vla.png&quot; alt=&quot;Image and language go through weights to an action&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Four boxes: image, language, VLA weights, action. The arrows are the forward pass of the idea, not a ROS graph. The red line is the correction: another lab’s gripper is a different robot, and embodiment is part of the model. The footer lists RT-1, RT-2, Open X-Embodiment, OpenVLA, and pi0, with the instruction to read them and not retrain them. Your lab obeys the footer. Two rows of a table are the deliverable, not a fine-tune.&lt;/p&gt;

&lt;h2 id=&quot;worked-reading-example&quot;&gt;Worked reading example&lt;/h2&gt;

&lt;p&gt;Fill RT-1 yourself before you fill anyone else, using the project page if a cell here drifts. This row is the standard the lab is graded against: specific inputs, an action space that names a body, and a laptop cell that does not pretend.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;System&lt;/th&gt;
      &lt;th&gt;Inputs&lt;/th&gt;
      &lt;th&gt;Action space&lt;/th&gt;
      &lt;th&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; on your graph, or joint targets?&lt;/th&gt;
      &lt;th&gt;Laptop tonight?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;RT-1&lt;/td&gt;
      &lt;td&gt;RGB image from their robot’s camera, plus a text instruction&lt;/td&gt;
      &lt;td&gt;Discretized vector for their mobile manipulator: arm, base, gripper&lt;/td&gt;
      &lt;td&gt;Base motion lives inside their vector. It is not your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;. Not your joint targets either&lt;/td&gt;
      &lt;td&gt;Read the page: yes. Train: no. Run their inference stack on a GPU-less laptop: no&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;One number makes the embodiment concrete. Your bridge accepts two floats per tick. An RT-1-style action packs arm, base, and gripper into one discretized command for a platform with those joints. Even if two of the discrete bins are “base forward” and “base yaw,” the bin edges were chosen for their robot, their camera height, and their wheelbase. Mapping bin 7 onto \(v = 0.2\) m/s is a new controller you would have to design and test wheels-up. It is not a property of the checkpoint. The checkpoint has never seen your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation.images.cam&lt;/code&gt; or your 300 ms timeout.&lt;/p&gt;

&lt;p&gt;RT-2’s laptop cell, so the expected answer is visible before you copy it: training RT-2 at home is no. Co-fine-tuning the vision-language backbones those papers start from is not a classroom machine. Reading &lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;arXiv:2307.15818&lt;/a&gt; is the whole assignment if you pick that column.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-action-token-quantization-error-in-a-vla&quot;&gt;Explanation: Hand-Calculating Action-Token Quantization Error in a VLA&lt;/h3&gt;

&lt;p&gt;Many autoregressive VLAs (like OpenVLA) discretize each continuous action dimension (e.g., end-effector delta \(\Delta x \in [-0.05,\ +0.05]\ \text{m}\)) into \(B = 256\) uniform bins mapped onto the vocabulary’s least-used tokens. Let us check the exact spatial precision and bandwidth:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Bin width (quantization step \(\Delta q\)):&lt;/strong&gt;
\(\Delta q = \frac{0.05 - (-0.05)}{256 - 1} = \frac{0.10\ \text{m}}{255} = 0.000392\ \text{m} \approx 0.39\ \text{mm}.\)
The worst-case rounding error is half a bin: \(\pm 0.20\ \text{mm}\)—plenty accurate for tabletop pick-and-place, though coarse for tight-tolerance peg-in-hole insertion.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Encode a desired move \(\Delta x = +0.012\ \text{m}\) into a token bin index \(b \in \{0, \dots, 255\}\):&lt;/strong&gt;
\(b = \operatorname{round}\!\left(\frac{0.012 - (-0.05)}{0.10} \times 255\right) = \operatorname{round}(0.62 \times 255) = \operatorname{round}(158.1) = 158.\)
Decoding bin 158 back to meters gives \(-0.05 + \frac{158}{255}(0.10) = +0.01196\ \text{m}\) (error of only \(0.04\ \text{mm}\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Token generation latency check:&lt;/strong&gt; For a 7-DOF action (\(\Delta x, \Delta y, \Delta z, \Delta r, \Delta p, \Delta y_{\text{aw}}, \text{gripper}\)), generating 7 tokens autoregressively at \(15\ \text{ms/token}\) takes \(7 \times 15 = 105\ \text{ms}\) (\(\approx 9.5\ \text{Hz}\))—explaining why VLAs rely on action chunking or fast low-level controllers underneath.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Pick &lt;strong&gt;two&lt;/strong&gt; systems from this list: RT-2, Open X-Embodiment, OpenVLA, pi0. Do not pick RT-1; that row is the worked example. Open the pages, not a weight file.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;RT-2: &lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;arXiv:2307.15818&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Open X-Embodiment: &lt;a href=&quot;https://robotics-transformer-x.github.io/&quot;&gt;robotics-transformer-x.github.io&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;OpenVLA: &lt;a href=&quot;https://arxiv.org/abs/2406.09246&quot;&gt;arXiv:2406.09246&lt;/a&gt; and &lt;a href=&quot;https://openvla.github.io/&quot;&gt;openvla.github.io&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;pi0: &lt;a href=&quot;https://www.pi.website/blog/pi0&quot;&gt;pi.website/blog/pi0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Copy the table header into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; and add your two rows.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;System&lt;/th&gt;
      &lt;th&gt;Inputs&lt;/th&gt;
      &lt;th&gt;Action space&lt;/th&gt;
      &lt;th&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; on your graph, or joint targets?&lt;/th&gt;
      &lt;th&gt;Laptop tonight?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Expected.&lt;/strong&gt; Two filled rows. Inputs mention image and text where the page does. The action-space cell names a body that is not the Capstone, or explicitly says the page’s robots are not yours. The Twist cell is no for your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;, even when the other robot has wheels. If one of the two rows is RT-2, the laptop cell says no to training at home, in those words or plainer. A cell that says “maybe with Colab” is a no you have not finished writing. This course does not assign a hosted training run either.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What the table says&lt;/th&gt;
      &lt;th&gt;What is wrong&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;“Publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;” because RT-1 has a mobile base&lt;/td&gt;
      &lt;td&gt;Their base channel is not your topic, your units, or your wheel separation&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;“Open weights, so I can train it tonight” on the OpenVLA row&lt;/td&gt;
      &lt;td&gt;Open weights let you read a file name. They do not shrink a multi-billion-parameter training job onto a GPU-less laptop, and inference at control rate is a no on that machine too&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;“Open X-Embodiment includes many robots, so the Capstone is covered”&lt;/td&gt;
      &lt;td&gt;Coverage would require your sensors and your action vector in the data, in quantity. A slogan on a project page is not that&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;“The instruction was stop, so the timeout does not matter”&lt;/td&gt;
      &lt;td&gt;The timeout never sees the string. It sees serial frames. A policy that keeps publishing keeps the motors enabled&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Any row whose last cell is yes for training&lt;/td&gt;
      &lt;td&gt;Rewrite it. Reading is yes. Training these systems at home is no&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;shopping&quot;&gt;Shopping&lt;/h2&gt;

&lt;p&gt;No purchase supports this lesson. Do not buy a gripper, a Jetson-class board, or a second computer because a VLA demo had more cameras than you do. The optional camera from Chapter 11 is already the imaging discussion for this course. A camera on the Capstone still does not turn the base into the robot inside RT-1 or OpenVLA.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Your table includes RT-2. Write the laptop cell in a full sentence. Guidance: training at home is no. You may quote the abstract.&lt;/li&gt;
  &lt;li&gt;OpenVLA’s weights are public. Why is that not “runs on the laptop that flashed the ESP32”? Guidance: a multi-billion-parameter network, no GPU, and a command due every 20 ms. Loading a file is not that loop.&lt;/li&gt;
  &lt;li&gt;A classmate maps a gripper’s “open” bit onto \(\omega\). What does the Capstone do the first time the policy opens the gripper? Guidance: it yaws. Lesson 12-01’s card would have refused the alias by naming units.&lt;/li&gt;
  &lt;li&gt;The operator types “stop” and the policy keeps publishing \(v = 0.15\) every 20 ms. Does the 300 ms watchdog fire? Guidance: no. You need a zero serial frame or the hardware cutoff. The string is not on that wire.&lt;/li&gt;
  &lt;li&gt;Name one input Open X-Embodiment cannot invent for you. Guidance: the pose of your forward camera, your wheel separation, or an action vector that is only \((v, \omega)\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://robotics-transformer1.github.io/&quot;&gt;RT-1 project page&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/2212.06817&quot;&gt;RT-1 paper&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2307.15818&quot;&gt;RT-2&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://robotics-transformer-x.github.io/&quot;&gt;Open X-Embodiment&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2406.09246&quot;&gt;OpenVLA&lt;/a&gt; and the &lt;a href=&quot;https://openvla.github.io/&quot;&gt;project page&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pi.website/blog/pi0&quot;&gt;pi0, Physical Intelligence&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Diffusion Policy intuition</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter12/12_03_diffusion_policy/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter12/12_03_diffusion_policy</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~65 minutes&lt;/strong&gt;. You will read a project page, stare at one average, and write a paragraph. You will not denoise anything on a GPU.&lt;/p&gt;

&lt;p&gt;Imagine teaching a robot to push a T-shaped block around a post: half of your demonstrations go &lt;em&gt;left&lt;/em&gt; around the post, and half go &lt;em&gt;right&lt;/em&gt;. A standard MSE regression network averages the two valid strategies—and drives the robot straight into the center of the post! &lt;strong&gt;Diffusion Policy&lt;/strong&gt; eliminates mode averaging by treating action generation as iterative denoising: starting from pure Gaussian noise and sculpted step-by-step into one sharp, consistent action trajectory.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will explain why a diffusion policy generates an action trajectory by iterative denoising instead of emitting one regression. You will show, with two Capstone demonstrations around a chair, that the mean action drives into the chair. You will read the sketch \(x_{t-1} = f(x_t, \text{observation})\) as a picture of a learned denoiser, not as the paper’s update equation. You will contrast that picture with ACT in two paragraphs: one forward pass of a chunked CVAE, versus many steps that clean a trajectory.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 12-01 and 12-02 are done. You can say what a dataset key is, and you can say that an ACT chunk is open-loop for \(k/f\) seconds. The chair in this lesson is furniture, not a new robot. No GPU, no conda environment, no checkpoint. The project page is public.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-multimodal-action-distributions-score-matching-and-iterative-denoising-ddpmddim&quot;&gt;Key ideas: Multimodal Action Distributions, Score Matching, and Iterative Denoising (DDPM/DDIM)&lt;/h2&gt;

&lt;p&gt;Diffusion Policy brings generative modeling directly into visuomotor control through three pillars:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Multimodality &amp;amp; Failure of L2 Regression:&lt;/strong&gt; Minimizing mean-squared error \(\mathbb{E}[\lVert \hat{a} - a \rVert^2]\) always outputs the conditional &lt;em&gt;mean&lt;/em&gt; \(\mathbb{E}[a \mid o]\). When \(p(a \mid o)\) has two peaks (e.g., \(a = -1\) and \(a = +1\)), the mean is \(0\)—a catastrophic collision! Generative policies sample from the full distribution instead of averaging modes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Forward Noising &amp;amp; Learned Noise Predictor \(\epsilon_\theta\):&lt;/strong&gt; During training, Gaussian noise \(\epsilon \sim \mathcal{N}(0, I)\) is added to an expert action chunk \(a^0\) at noise level \(k\): \(a^k = \sqrt{\bar\alpha_k} a^0 + \sqrt{1 - \bar\alpha_k}\,\epsilon\). The network learns to predict \(\epsilon\) conditioned on image observations \(o_t\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reverse Denoising &amp;amp; Receding-Horizon Control:&lt;/strong&gt; At deployment, starting from random \(a^K \sim \mathcal{N}(0, I)\), the policy runs \(K\) (or fast DDIM 10–15) denoising steps to recover a clean action chunk \(a^0\), executes the first \(T_a\) steps, then replans from fresh observations.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nav2 picks one path and a controller tracks it. Diffusion Policy is a different proposal for where a path comes from when two drivers did not agree. In a corridor they pass a chair on two sides. The average of those drives aims at the chair. Jazzy will publish that average as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;, and the firmware will turn it into PWM. Nothing in Chapters 08 through 11 computes “this is the mean of two good ideas.” You notice the mean in the data. The 0.2 s abort from ACT still applies when the burst was denoised rather than regressed.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A regression policy looks at the observation and emits the action, or a chunk of actions, in one shot. Training nudges that output toward the demonstrated numbers. When the demonstrations share one solution, this works. When they split, squared error is a compromise. The compromise is not one of the demonstrated solutions.&lt;/p&gt;

&lt;p&gt;Chi, Feng, Du, Xu, Cousineau, Burchfiel, and Song frame the policy as conditional denoising. You start with a trajectory-shaped blob of noise. A network, conditioned on the observation, takes a small step toward a trajectory that could have come from the demonstrations. You repeat the step. The figure’s four boxes are those repeats: noise, less noise, almost an action, a trajectory. Early steps have no business being sent to motors. Later steps are a time series of \((v, \omega)\), or of joint targets, in the units of whatever robot produced the log. The observation is an input at every step even though the drawing only shows the action getting cleaner. Cover the camera and the denoiser has no reason to prefer the left pass over the right pass.&lt;/p&gt;

&lt;p&gt;The update you are allowed to memorize for this course is a sketch:&lt;/p&gt;

\[x_{t-1} = f(x_t, \text{observation})\]

&lt;p&gt;Here \(x_t\) is the noisy trajectory at step \(t\), and \(t\) counts denoising steps, not control ticks. The letter \(f\) stands for a learned denoiser. The paper’s real update predicts noise, mixes it with a schedule, and adds randomness so that two runs can choose two modes. If you implement the letter \(f\) as “subtract a constant,” you have not implemented Diffusion Policy. You have implemented the picture. That is the right depth for tonight.&lt;/p&gt;

&lt;p&gt;The object being cleaned is a short trajectory, and the training distribution can put probability on both ways around the chair. You execute one sample, not the average of a left sample and a right sample. Denoising is expensive, so implementations execute a prefix and denoise again. Ten steps at 100 ms followed by eight actions at 50 Hz is a prefix of \(8/50 = 0.16\) s, the same order as the ACT window. The watchdog still only notices silence.&lt;/p&gt;

&lt;p&gt;ACT asks a transformer once for a chunk. A CVAE latent selects a demonstration style, and the chunk is a regression given that style. Latency is one network call per query, plus the open-loop prefix. Ensemble can still average a new “stop” with an old “go.”&lt;/p&gt;

&lt;p&gt;Diffusion Policy does not emit that chunk in one regression. It starts from noise and applies a learned denoiser until the trajectory matches a mode the demonstrations support. Different draws can fall left or right of the chair, instead of into a latent you set to zero. You pay with many network calls per replan. Either method can still drive into the chair if you execute the wrong sample, average two samples, or drop the timeout. Choosing between them this week is reading, not a training bake-off.&lt;/p&gt;

&lt;h2 id=&quot;the-figure&quot;&gt;The figure&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_diffusion.png&quot; alt=&quot;From noise to a trajectory&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Left to right: a red box of noise, a yellow box of less noise, a pale box that is almost an action, a green box that is a trajectory. The arrows are denoising steps, not time along the robot’s path. Under the boxes the caption states the lesson’s arithmetic in words: a mean of “left or right” is straight into the obstacle. The footer names Chi et al., Diffusion Policy, 2023, and tells you to read it rather than train it as tonight’s lab. Believe the footer.&lt;/p&gt;

&lt;h2 id=&quot;worked-reading-example&quot;&gt;Worked reading example&lt;/h2&gt;

&lt;p&gt;Two demonstrations, one decision point, Capstone units. The chair is 0.40 m ahead of the camera, centered. Both drivers keep a modest forward speed and disagree only in yaw.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Demo&lt;/th&gt;
      &lt;th&gt;\(v\) (m/s)&lt;/th&gt;
      &lt;th&gt;\(\omega\) (rad/s)&lt;/th&gt;
      &lt;th&gt;Intent&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;L&lt;/td&gt;
      &lt;td&gt;0.15&lt;/td&gt;
      &lt;td&gt;\(+0.50\)&lt;/td&gt;
      &lt;td&gt;pass on the left&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;R&lt;/td&gt;
      &lt;td&gt;0.15&lt;/td&gt;
      &lt;td&gt;\(-0.50\)&lt;/td&gt;
      &lt;td&gt;pass on the right&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Mean&lt;/td&gt;
      &lt;td&gt;0.15&lt;/td&gt;
      &lt;td&gt;\(0.00\)&lt;/td&gt;
      &lt;td&gt;aimed at the chair&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The mean is not a cautious blend. Yaw cancels. Forward speed does not. Time to reach a chair 0.40 m ahead, if nobody yaws and the speed holds, is&lt;/p&gt;

\[\frac{0.40}{0.15} \approx 2.7\,\text{s}\]

&lt;p&gt;You do not get 2.7 s of warning inside a 0.16 s open-loop prefix. The prefix that carries the mean action is already pointed at the obstacle. A denoiser that has seen both demonstrations, conditioned on an image that shows the chair, should move a noise draw toward demo L or toward demo R. It should not be asked to output both and then averaged by your bridge. If you are unsure which mode you sampled, the safe command is \(v = 0\), not the mean.&lt;/p&gt;

&lt;p&gt;Hold the sketch next to those numbers. At a late denoising step, \(x_t\) might be a short sequence whose first action is near \((0.15, 0.48)\), a noisy left pass. Then \(f\), given the image, pulls it toward \((0.15, 0.50)\). At an early step, \(x_t\) is not yet a pass around anything, and sending it as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; is how you drive on pure noise. The index \(t\) in the sketch is not the 50 Hz tick. Mixing those clocks is how a student executes step “noise” as if it were step “trajectory.”&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-checking-mode-averaging-and-1-step-of-ddpm-denoising&quot;&gt;Explanation: Hand-Checking Mode Averaging and 1 Step of DDPM Denoising&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Part A — Why MSE regression crashes while Diffusion succeeds:&lt;/strong&gt;
Suppose at observation \(o\), 50% of expert demos command steering \(a = -1.0\ \text{rad}\) (go left around an obstacle) and 50% command \(a = +1.0\ \text{rad}\) (go right).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;An L2 regression network minimizes \(0.5(\hat a + 1)^2 + 0.5(\hat a - 1)^2 = \hat a^2 + 1\), whose unique minimum is \(\hat a = 0.0\ \text{rad}\) — &lt;strong&gt;straight into the obstacle&lt;/strong&gt;!&lt;/li&gt;
  &lt;li&gt;A diffusion policy samples \(a^K \sim \mathcal{N}(0, 1)\): if the initial noise seed is negative, the learned score field pulls it cleanly to \(-1.0\); if positive, it pulls it cleanly to \(+1.0\)—never landing on the forbidden middle \(0.0\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Part B — One concrete reverse denoising step:&lt;/strong&gt;
Suppose at denoising step \(k = 1\) (final step to clean action), schedule constants are \(\alpha_1 = 0.90,\ \bar\alpha_1 = 0.90\) (so \(\sqrt{\alpha_1} = 0.949,\ \sqrt{1 - \bar\alpha_1} = 0.316\)). Our noisy 1D action is \(a^1 = 1.15\), and the network predicts noise \(\epsilon_\theta(o, a^1, 1) = +0.60\). Applying the DDPM mean update:
\(a^0 = \frac{1}{\sqrt{\alpha_1}}\left(a^1 - \frac{1 - \alpha_1}{\sqrt{1 - \bar\alpha_1}}\,\epsilon_\theta\right) = \frac{1}{0.949}\left(1.15 - \frac{0.10}{0.316}(0.60)\right) = \frac{1.15 - 0.190}{0.949} = \frac{0.960}{0.949} = 1.01\ \text{rad}.\)
In one step, the noisy seed \(1.15\) snaps right onto the valid expert mode \(+1.0\ \text{rad}\).&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Open the project page &lt;a href=&quot;https://diffusion-policy.cs.columbia.edu/&quot;&gt;diffusion-policy.cs.columbia.edu&lt;/a&gt; and the paper’s abstract at &lt;a href=&quot;https://arxiv.org/abs/2303.04137&quot;&gt;arXiv:2303.04137&lt;/a&gt;. You are looking for the claim that the policy models a distribution over action sequences, and for any figure that shows a trajectory emerging or a multimodal task. Do not follow a “train” or “download checkpoint” link beyond reading that it exists.&lt;/p&gt;

&lt;p&gt;Then write one paragraph in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; on multimodality, using the Capstone. The scene is two demonstrations of passing a chair on different sides. The paragraph has to include the mean action, and it has to say that the mean hits the chair. Use the numbers from the worked example, or change them and recompute the time-to-contact so the new numbers still hit. End the paragraph with an explicit statement that you did not train a model and did not load weights.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Expected.&lt;/strong&gt; A paragraph a classmate can check without watching a video. It contains two sides, a cancelled yaw, a nonzero forward speed, the chair, and a refusal to train. A paragraph that says only “diffusion is better at multimodality” is not done, because it never mentions the mean.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mistake&lt;/th&gt;
      &lt;th&gt;Why the paragraph fails&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;You average the two demonstrations into one “neutral” episode and call the dataset cleaner&lt;/td&gt;
      &lt;td&gt;You deleted both successful passes and inserted the collision&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You treat a missing GPU, or an out-of-memory error, as a failed lab&lt;/td&gt;
      &lt;td&gt;The lab is the paragraph. Closing a training process is the recovery, not a result&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You write that diffusion is safer because it is iterative&lt;/td&gt;
      &lt;td&gt;Iteration produces a sample. The sample can say forward. Safety is still the timeout plus an explicit zero when the prefix is wrong&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You send an early denoising step to the motors because it already has the shape of \((v, \omega)\)&lt;/td&gt;
      &lt;td&gt;Early steps are noise with that shape. Only a finished trajectory is even a candidate command, and only in the dataset’s units&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Demo L is \((v, \omega) = (0.20, +0.80)\) and demo R is \((0.20, -0.40)\), both in m/s and rad/s. What is the mean action, and which way does it yaw? Guidance: mean \(v = 0.20\), mean \(\omega = +0.20\). The yaw does not cancel. The mean still need not be either demonstration. Say whether you would execute it toward a centered chair.&lt;/li&gt;
  &lt;li&gt;In the sketch, what does the subscript \(t\) count? Guidance: denoising steps. It does not count 20 ms control periods. Write one sentence that keeps the two clocks apart.&lt;/li&gt;
  &lt;li&gt;You execute a prefix of 8 actions at 50 Hz after the trajectory is clean. How long is that prefix, and does the 300 ms watchdog expire during it if you keep publishing? Guidance: \(8/50 = 0.16\) s. The watchdog does not expire, because publishing continues. Name the command you publish if the prefix points at the chair.&lt;/li&gt;
  &lt;li&gt;ACT commits with a latent and one forward pass. Diffusion Policy commits by drawing a trajectory out of noise. Give one cost of the second choice that the first choice does not pay on every replan. Guidance: many denoiser evaluations per replan, instead of one transformer pass. The cost is latency, not a license to skip the stop command.&lt;/li&gt;
  &lt;li&gt;Your paragraph claimed the mean hits the chair. Change the chair to sit 0.15 m ahead and keep \(v = 0.15\) with \(\omega = 0\). How soon is contact, and is that longer than a 0.16 s prefix? Guidance: \(0.15/0.15 = 1.0\) s of travel if the speed holds. One second is longer than the prefix, which means the prefix alone does not finish the crash, and it also means the prefix has already committed you to the bad direction. The fix is a zero, not a shorter essay.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://diffusion-policy.cs.columbia.edu/&quot;&gt;Diffusion Policy project page&lt;/a&gt;. Read the abstract of the idea and the figures. Leave the training commands unread in the practical sense: you are not running them.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2303.04137&quot;&gt;Chi et al., arXiv:2303.04137&lt;/a&gt;. The abstract is enough to confirm the authors and the claim. The noise schedule in the paper is the learned denoiser’s real machinery, beyond the sketch \(f\).&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Imitation learning and ACT overview</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter12/12_02_imitation_act/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter12/12_02_imitation_act</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~70 minutes&lt;/strong&gt;. Most of that is a slow read of one abstract and a numerical note about 0.2 s of open-loop motion. None of it is a training run.&lt;/p&gt;

&lt;p&gt;If you train a simple neural network to copy human robot demonstrations one frame at a time (Behavioral Cloning), a subtle disaster occurs: tiny 1-millimeter errors compound at every tick until the arm drifts into a state the human never demonstrated—and freezes or flails. &lt;strong&gt;Action Chunking with Transformers (ACT)&lt;/strong&gt; tames this compounding error by predicting an entire short &lt;em&gt;chunk&lt;/em&gt; of future actions at once and blending overlapping chunks smoothly.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will define behavior cloning as a map from observation to action, fitted on demonstrations rather than on a reward. You will state what ACT (Zhao et al., RSS 2023) actually outputs: a chunk of \(k\) future actions from a transformer, trained as a conditional variational autoencoder so more than one style of demonstration can exist. You will compute the open-loop window for \(k = 10\) at 50 Hz, connect that window to the Chapter 08 watchdog, and explain temporal ensemble as an average of overlapping chunks without re-deriving the paper.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 12-01’s dataset card is in your notes: camera, \(v\) in m/s, \(\omega\) in rad/s, one fps. You know the firmware zeros the motors 300 ms after the last good frame. You can read an abstract. You do not need a GPU, the ALOHA hardware, or a working install of the policy. If a classmate’s machine reports that it ran out of memory, that classmate started the wrong assignment.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-covariate-shift-in-behavioral-cloning-action-chunking-and-cvae-transformers&quot;&gt;Key ideas: Covariate Shift in Behavioral Cloning, Action Chunking, and CVAE Transformers&lt;/h2&gt;

&lt;p&gt;ACT solves three classical failure modes of imitation learning:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Compounding Error (Covariate Shift):&lt;/strong&gt; Under single-step behavioral cloning with per-step error probability \(\epsilon\) over horizon \(H\), expected cumulative error grows quadratically as \(O(\epsilon H^2)\) because each mistake pushes the robot further off the expert’s state distribution.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Action Chunking (Effective Horizon Reduction):&lt;/strong&gt; Predicting a sequence of \(k\) future actions \(a_{t:t+k-1}\) in one forward pass shrinks the effective decision horizon from \(H\) to \(H/k\), slashing compounding error and eliminating the “pausing/jittering” caused by noisy human demonstrations.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Conditional VAE + Temporal Ensembling:&lt;/strong&gt; Humans solve the same task in multiple valid ways (multimodality). ACT uses a Conditional Variational Autoencoder (CVAE) latent variable \(z\) during training to separate distinct styles, and &lt;strong&gt;temporal ensembling&lt;/strong&gt; at test time to exponentially average overlapping action chunks for silky-smooth motion.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Up through Nav2’s tour, the command on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; was one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; per tick, produced by a controller you could name. A learned policy changes the shape of that command. ACT does not emit “the next 20 ms.” It emits a short list of future actions and then lets the robot execute them while the network is not being asked again. That list is a burst of open-loop motion. The Capstone bridge can forward the burst. It cannot make the burst safe. The 300 ms timeout still matters, and it is not enough on its own: a timeout fires when frames stop arriving, and a live policy keeps frames arriving for the rest of a bad chunk. This lesson is how you read that burst before anyone suggests you train it.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;Behavior cloning fits a map from observation to the action a person sent. There is no reward in the definition. The map is only as good as the log: units, clock, and the timeout from lesson 12-01. Cloning \((v, \omega)\) onto a robot that accepts \((v, \omega)\) is aimed at the Capstone. Cloning an SO-101 joint vector onto that same base is still the wrong body.&lt;/p&gt;

&lt;p&gt;A squared-error head fails when demonstrations disagree. One person passes a cup on the left, another on the right, and the average points at the cup. Zhao, Kumar, Levine, and Finn train ACT as a conditional variational autoencoder so a latent can pick a style. A transformer decoder then predicts actions consistent with that style, the images, and the joint state. At playback the encoder is gone and the latent is taken from the prior, often at the prior’s center. The latent is permission to commit to one way of doing the task. It is not a collision check.&lt;/p&gt;

&lt;p&gt;The transformer outputs a &lt;strong&gt;chunk&lt;/strong&gt; of \(k\) future actions. That is the “action chunking” in the paper. At 50 Hz the control period is 20 ms. A network that needs 80 ms has already missed four deadlines if it must answer every period. A chunk of ten actions covers that gap, so the base executes a list while the next list is computed. The list is open-loop until you replan. ALOHA, the paper’s hardware, is a low-cost bimanual leader-follower rig for collecting those demonstrations. You are not building it. You are asking what a chunk does to a diff-drive that already has a watchdog.&lt;/p&gt;

&lt;p&gt;Temporal ensemble averages overlapping chunks at playback. Newer predictions get more weight. The paper’s decay constant can wait. The average hides small jitter. It does not stop the robot: the mean of “forward” and “stop” is “creep.” The stop is a zero you publish on purpose, or a timeout because publishing ceased.&lt;/p&gt;

&lt;p&gt;Chapter 08 clears the motors when no valid frame arrives for 300 ms. That covers a dead process or a fallen cable. It does not cover a live process posting the rest of a chunk that still says forward after a person steps in. Those frames look fresh. The abort belongs where the chunk is unpacked: drop the remaining steps and publish \((0, 0)\). Asking the network only when the chunk ends makes the whole chunk the open-loop window. Asking more often retires a bad prediction sooner, and you still need an explicit zero when the scene has changed.&lt;/p&gt;

&lt;h2 id=&quot;the-figure&quot;&gt;The figure&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_act.png&quot; alt=&quot;ACT turns images and joint state into a chunk&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The left box is images and joint state. On the Capstone that is the camera plus measured \((v, \omega)\), not a six-joint vector. The middle box is the chunk. The right box says execute, then replan. The red line is the part a summary skips: the chunk is open-loop and still needs a timeout and a stop. The footer cites Zhao et al., ACT / ALOHA, 2023. That is a citation, not a parts list.&lt;/p&gt;

&lt;h2 id=&quot;worked-reading-example&quot;&gt;Worked reading example&lt;/h2&gt;

&lt;p&gt;Use a teaching chunk, not the hyperparameter table from the paper. Take \(k = 10\) and a control rate of 50 Hz:&lt;/p&gt;

\[T_{\text{open}} = \frac{k}{f} = \frac{10}{50} = 0.2\,\text{s}\]

&lt;p&gt;Two tenths of a second is the open-loop burst if you execute the whole chunk before you look again. Steps are 0.02 s. The early steps command \(v = 0.25\) m/s and \(\omega = 0\). By step 9, at 0.18 s, \(v\) has tapered to 0.05 m/s. Nothing in the list yaws.&lt;/p&gt;

&lt;p&gt;At \(t = 0.08\) s a person steps in front of the camera. Six steps remain, 0.12 s of leftover command. A rough distance, using 0.20 m/s as a stand-in, is&lt;/p&gt;

\[0.20 \times 0.12 = 0.024\,\text{m}\]

&lt;p&gt;about 2.4 cm of commanded travel, before the wheels’ own stopping distance. The watchdog does not fire at 0.08 s: the bridge is still delivering fresh steps. Publish zeros for the rest of the window. Waiting out 300 ms is the slower option.&lt;/p&gt;

&lt;p&gt;Chunks discussed for ALOHA-style ACT are often nearer a hundred steps at 50 Hz:&lt;/p&gt;

\[\frac{100}{50} = 2.0\,\text{s}\]

&lt;p&gt;of open loop if you do not replan early. Two seconds of committed forward motion does not belong on the Capstone in a hallway. Knowing the paper uses chunking is not the same as accepting the paper’s chunk length on your base.&lt;/p&gt;

&lt;p&gt;Temporal ensemble on one overlapping instant: an older chunk still says \(v = 0.25\) at \(t = 0.20\), and a newer chunk, queried after the person appeared, says \(v = 0.00\). A plain average is \(0.125\) m/s. That is not a stop. Even a weighted average that leans toward the new chunk, say three quarters of the new value and one quarter of the old, is \(0.0625\) m/s, still forward. Ensemble reduces the command. It does not replace the zero.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-computing-act-temporal-ensembling-across-overlapping-chunks&quot;&gt;Explanation: Hand-Computing ACT Temporal Ensembling Across Overlapping Chunks&lt;/h3&gt;

&lt;p&gt;Suppose an ACT policy predicts chunks of length \(k = 3\), and at time \(t = 2\) three overlapping chunks have each proposed a target joint angle for the current step \(t = 2\):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Chunk from \(t = 0\) (oldest, index \(i = 0\)): predicted \(a^{(0)}_2 = 1.00\ \text{rad}\).&lt;/li&gt;
  &lt;li&gt;Chunk from \(t = 1\) (index \(i = 1\)): predicted \(a^{(1)}_2 = 1.10\ \text{rad}\).&lt;/li&gt;
  &lt;li&gt;Chunk from \(t = 2\) (newest, index \(i = 2\)): predicted \(a^{(2)}_2 = 1.16\ \text{rad}\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ACT applies exponential decay weights \(w_i = \exp(-m \cdot i)\) (with decay factor \(m = 0.5\), weighting older predictions higher or vice versa depending on convention; using \(w_{\text{age}} = e^{-0.5 \cdot \text{age}}\) to favor fresher observations):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compute unnormalized weights by age (0 = newest, 2 = oldest):&lt;/strong&gt;
\(w_0 = e^{0} = 1.000, \qquad w_1 = e^{-0.5} = 0.607, \qquad w_2 = e^{-1.0} = 0.368.\)
Sum of weights: \(W = 1.000 + 0.607 + 0.368 = 1.975\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute the blended executed command:&lt;/strong&gt;
\(a_2^{\text{exec}} = \frac{1.000(1.16) + 0.607(1.10) + 0.368(1.00)}{1.975} = \frac{1.160 + 0.668 + 0.368}{1.975} = \frac{2.196}{1.975} = 1.112\ \text{rad}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Why this works:&lt;/strong&gt; Instead of snapping abruptly from \(1.00\) to \(1.16\) between chunks, temporal ensembling acts as a learned low-pass filter that preserves responsiveness while suppressing jitter.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Reading only. Do not clone a training configuration, do not download weights, and do not start a job because a GPU happens to be free in a lab you do not have.&lt;/p&gt;

&lt;p&gt;Open the paper’s abstract page: &lt;a href=&quot;https://arxiv.org/abs/2304.13705&quot;&gt;Zhao et al., arXiv:2304.13705&lt;/a&gt;. From the abstract and the title, write four lines in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The authors and the venue line you can see (RSS 2023 is the venue these notes use; keep whatever the abstract page shows if you want a direct quotation).&lt;/li&gt;
  &lt;li&gt;What the method predicts: a chunk of future actions, not a single next command. The phrase “action chunking” should appear because you found it, not because you copied this sentence blindly. If you paraphrase, say so.&lt;/li&gt;
  &lt;li&gt;What the hardware in the title is for: low-cost bimanual demonstration hardware (ALOHA), which you are not buying and not assembling.&lt;/li&gt;
  &lt;li&gt;Your chunk-length implication for the Capstone, with the arithmetic visible: \(k = 10\), 50 Hz, \(T_{\text{open}} = 0.2\) s, and one sentence that names who cuts a bad chunk. The watchdog cuts it when the publisher dies. A live publisher of a bad chunk has to be cut by an explicit zero.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Expected.&lt;/strong&gt; A note of about half a page. The number 0.2 s appears, with the division that produced it. The stop command is named separately from the timeout. There is no training log.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What happened&lt;/th&gt;
      &lt;th&gt;What you should do&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;You launched a train script and the process died with an out-of-memory error&lt;/td&gt;
      &lt;td&gt;Close it. That error is not the assignment, and it is not evidence you “almost” trained ACT. A laptop without a serious GPU was never going to finish this paper’s training&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;The note says “the watchdog handles the chunk” and stops there&lt;/td&gt;
      &lt;td&gt;Rewrite the sentence. A watchdog handles silence. A chunk that keeps streaming is not silence&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You copied a chunk length from a random config file and skipped the division&lt;/td&gt;
      &lt;td&gt;The lab is the implication in seconds, on your control rate. A unitless \(k\) does not tell you how far the base can travel&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;The note treats ALOHA as the next purchase&lt;/td&gt;
      &lt;td&gt;ALOHA is the paper’s data-collection rig. Your robot for this course remains the Capstone&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Write behavior cloning as one sentence that uses your keys. Guidance: the map takes the camera and returns the commanded \(v\) and \(\omega\) the demonstrator sent. Reward is not in the sentence.&lt;/li&gt;
  &lt;li&gt;Two demonstrations pass a cup on opposite sides and a squared-error head averages them. What does the CVAE latent buy? Guidance: a commitment to one side. It does not buy a range sensor.&lt;/li&gt;
  &lt;li&gt;Recompute the open-loop time for \(k = 100\) at 50 Hz. Would you execute that whole chunk on the Capstone in a corridor? Guidance: \(100/50 = 2.0\) s, and the corridor answer is no.&lt;/li&gt;
  &lt;li&gt;An older chunk predicts \(v = 0.25\) and a newer chunk predicts \(v = 0\) at the same instant. You send the mean. Are the wheels stopped? Guidance: no. The mean is \(0.125\) m/s. Publish zero if a person is in the camera.&lt;/li&gt;
  &lt;li&gt;The bridge emits a fresh serial frame every 20 ms for a chunk that still says forward, after you have seen an obstacle. Does the 300 ms timer expire? Guidance: no. Publish zeros or stop the publisher.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2304.13705&quot;&gt;Zhao et al., Learning Fine-Grained Bimanual Manipulation with Low-Cost Hardware&lt;/a&gt;. Read the abstract and the method figures. Training instructions in the paper’s repository are outside this lab.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;LeRobot docs&lt;/a&gt;. ACT is named there as a policy you can read about. Naming it is the whole use of that page tonight.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>LeRobot / Hugging Face robotics intro</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter12/12_01_lerobot_intro/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter12/12_01_lerobot_intro</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~70 minutes&lt;/strong&gt;. About 25 minutes on the docs and the repository, 30 minutes on a dataset card, 15 minutes on the exercises.&lt;/p&gt;

&lt;p&gt;For decades, teaching a robot to fold a towel or plug in a cable meant hand-coding brittle perception pipelines and state machines for months. Open-source ecosystems like Hugging Face’s &lt;strong&gt;LeRobot&lt;/strong&gt; have flipped that workflow: standardize synchronized camera-and-joint datasets, train visuomotor policies directly from human teleoperation demonstrations, and evaluate them in simulation or on affordable desktop arms.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will name the three things the LeRobot project keeps on one contract: a dataset, a policy, and a robot. You will point at the SO-100 family, Koch v1.1, and LeKiwi as that project’s own low-cost arms and mobile bases, and you will say why none of them is the Capstone diff-drive. You will write a one-page dataset card for a fake episode whose keys are a camera, forward speed \(v\), and yaw rate \(\omega\), with units that match at record time and at play time. You will copy the repository’s one-sentence description from the README into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, with the date you read it.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can open a browser and a terminal. The Capstone already takes a forward speed in metres per second and a yaw rate in radians per second, and Chapter 08 still zeros the motors if no fresh frame arrives for 300 ms. Chapter 09’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; is the same pair: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;. You do not need a graphics card, a leader arm, or a second robot. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; is painful on your network, skip it. The graded artifacts are a quotation and a card.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-visuomotor-policies-synchronized-episode-datasets-and-control-loop-inference&quot;&gt;Key ideas: Visuomotor Policies, Synchronized Episode Datasets, and Control-Loop Inference&lt;/h2&gt;

&lt;p&gt;Modern robot learning platforms like LeRobot rest on three engineering pillars:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Visuomotor Policy Abstraction (\(\pi_\theta(a_t \mid o_t)\)):&lt;/strong&gt; Instead of hand-crafting explicit object poses and waypoints, a learned policy maps raw observations \(o_t\) (camera images + proprioceptive joint positions) directly to continuous action vectors \(a_t\) (target joint angles or end-effector deltas).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Standardized Episode Datasets:&lt;/strong&gt; Robot demonstrations are stored as time-indexed tables (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;episode_index&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_index&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation.state&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;timestamp&lt;/code&gt;) paired with compressed video streams—ensuring exact temporal alignment between what the robot &lt;em&gt;saw&lt;/em&gt; and what the human &lt;em&gt;commanded&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Closed-Loop Inference Latency Budget:&lt;/strong&gt; A neural policy that runs at 2 Hz on a CPU cannot stabilize a 30 Hz manipulation task; measuring inference latency vs. control period is just as critical for learned policies as for PID loops in Chapter 08.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capstone A made a diff-drive base that moves when firmware accepts \(v\) and \(\omega\). ROS 2 Jazzy gave those two numbers a topic. A learning library does not replace that stack. It adds a file. The file has to contain the observation the robot can actually produce later, and the action the firmware will actually accept later. If one note says the action is motor PWM and another says it is radians, you have logged one robot and played the log on another. Lessons 12-02 and 12-03 assume the keys exist and then argue about how a policy fills them. Lesson 12-04 is the same mistake at the scale of a published checkpoint. Lesson 12-05 asks you to pick a month; the month is optional, this contract is not.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;LeRobot&lt;/a&gt; is a Python library, a dataset format, and hardware configs maintained with Hugging Face. The index describes four verbs: teleoperate, record, train, deploy. A person drives, the log stores synchronized video plus the actions they sent, a policy imitates those actions, and deploy writes actions back to the robot. This course stops before train. The verbs are how you read the docs, not a homework script.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;dataset&lt;/strong&gt; lines up an image, a state, and an action on one clock. The project calls the file a LeRobotDataset. An arm log uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation.images.&amp;lt;camera&amp;gt;&lt;/code&gt;, joint positions, and joint targets. A diff-drive log uses a camera and a two-vector action. The format stores either. It will not warn you that you mixed them.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;policy&lt;/strong&gt; reads an observation and writes an action. ACT is the policy the next lesson reads. Weights expect a length and a meaning per entry. The right length in the wrong units is the wrong robot.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;robot&lt;/strong&gt; here is a body plus a config: motors, cameras, and the keys they fill. On the docs, SO-101 is the flagship low-cost arm and SO-100 is the earlier arm in that family. Koch v1.1 is a community arm they still document. LeKiwi is a mobile base with an arm. Those are the project’s embodiments. The Capstone is two driven wheels, no gripper, action \((v,\omega)\), and your timeout. SO-101 joint targets do not publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;. LeKiwi’s action includes the arm, so “mobile” does not merge the keys.&lt;/p&gt;

&lt;p&gt;Record time is a person driving while the logger writes. Play time is the policy writing while firmware applies the command. Same names, same units, both times. A camera 15 cm lower, or a flipped yaw sign, keeps the column &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt; and changes the motion. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps: 10&lt;/code&gt; means actions are 0.1 s apart. A 30 Hz camera dumped in without alignment pairs an image with the wrong command. The 300 ms timeout stays while you record. Commenting it out logs a robot you are not allowed to run. A late link that fills the log with \((0, 0)\) is the other bug: mark those frames, do not delete the watchdog.&lt;/p&gt;

&lt;h2 id=&quot;the-figure&quot;&gt;The figure&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch12_lerobot.png&quot; alt=&quot;Dataset, policy, and robot share keys&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Three boxes, two arrows. The dataset box holds observation and action. The arrow through the policy is play time: a row goes in, an action comes out, the robot executes it. The blue arrow labeled record runs the other way: the human drives the robot and the logger stores the same keys. The red line under the boxes is the rule you are enforcing with the card. The gray line says to read that card before a training run. Tonight the run does not start. The card does.&lt;/p&gt;

&lt;h2 id=&quot;worked-reading-example&quot;&gt;Worked reading example&lt;/h2&gt;

&lt;p&gt;This card describes a fake Capstone episode. It is not a file from the Hub, and it is not a format exporter you have to install. Two seconds, ten frames per second, twenty rows. The card sits at the top of the folder so a later script is not allowed to guess.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;robot_name: capstone_diff
fps: 10
episode_length_s: 2.0
observation.images.cam: RGB video, 480x640, 10 Hz, same clock as action
observation.state: [v_meas_m_s, w_meas_rad_s]
action: [v_cmd_m_s, w_cmd_rad_s]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Row 0 is action \((0.20, 0.00)\): 0.20 m/s forward, no yaw, about 2 cm in one 0.1 s period if the wheels are down. Row 8, a chair on the left of the image, is \((0.10, -0.40)\): slower, and \(-0.40\) rad/s. With \(z\) up, negative yaw is a right turn. Row 19 is \((0.00, 0.00)\), a driver stop, not a timeout.&lt;/p&gt;

&lt;p&gt;The same twenty rows become a different robot if the units drift. Suppose the teleop script still writes Chapter 07’s PWM-style commands in \([-100, 100]\), and the note you hand to a policy says the vector is m/s and rad/s. A later loader trusts the note. It reads 70 from the log and treats it as 70 m/s. No layer in a network checks that. The card is finished only when that contradiction is impossible: one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;names&lt;/code&gt; line, one unit, and both the logger and the playback bridge import that line.&lt;/p&gt;

&lt;p&gt;The row count is an arithmetic check, not a slogan:&lt;/p&gt;

\[N = \text{fps} \times T = 10 \times 2.0 = 20\]

&lt;p&gt;Twenty images with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps: 30&lt;/code&gt; on the card are not a 2.0 s episode. Somebody will stretch the turn. Write the count you can verify by listing the folder.&lt;/p&gt;

&lt;h3 id=&quot;explanation-sizing-a-lerobot-demonstration-dataset-and-control-loop-budget&quot;&gt;Explanation: Sizing a LeRobot Demonstration Dataset and Control Loop Budget&lt;/h3&gt;

&lt;p&gt;Suppose you collect \(E = 50\) teleoperation demonstrations of a 6-DOF desktop arm picking up a cube, each lasting \(T = 12\ \text{s}\) at \(f_s = 30\ \text{Hz}\) with one 480×640 RGB wrist camera and 6 joint angles (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float32&lt;/code&gt;):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Total state–action training transitions:&lt;/strong&gt;
\(N_{\text{frames}} = 50 \times 12 \times 30 = 18\,000\ \text{frames}.\)
Notice how modest \(18\,000\) frames is compared to internet-scale vision datasets—which is why data quality, consistent camera mounting, and covering recovery states matter far more than raw dataset size.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;State/action storage vs. video storage:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Proprioception + actions: \(18\,000 \times (6 + 6) \times 4\ \text{B} = 864\ \text{KB}\) (less than 1 MB!).&lt;/li&gt;
      &lt;li&gt;Uncompressed RGB video: \(18\,000 \times 640 \times 480 \times 3 \approx 16.6\ \text{GB}\); encoded with H.264/AV1 at 2 Mbps for 600 s total video: \(\frac{2\ \text{Mb/s} \times 600\ \text{s}}{8} = 150\ \text{MB}\) (&lt;strong&gt;110× smaller&lt;/strong&gt;!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Control-loop timing check:&lt;/strong&gt; At \(30\ \text{Hz}\), the entire pipeline (camera grab + image resize + neural forward pass + serial motor write) must complete within \(1000 / 30 = 33.3\ \text{ms}\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Software only. The base can stay on the shelf. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; gets two artifacts: a dated quotation, and a dataset card.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docs map.&lt;/strong&gt; Open the &lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;LeRobot docs index&lt;/a&gt;. Copy the four verbs in order: teleoperate, record, train, deploy. Under them write: this course stops before train. Then list the headings you actually open for install, datasets, imitation learning, and robots. Date the note. If a heading was renamed, write the new name. Do not invent one to match this lesson.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One sentence from the repository.&lt;/strong&gt; Open &lt;a href=&quot;https://github.com/huggingface/lerobot&quot;&gt;huggingface/lerobot&lt;/a&gt;. Under the repository name there is a one-line description. Copy that sentence into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; in quotation marks, with the date. Do not paraphrase it. If the line has changed since these notes were written, today’s line is the correct quotation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dataset card.&lt;/strong&gt; Rewrite the worked example so it is clearly yours. Change one number and keep the arithmetic consistent. A fair edit is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps: 5&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;episode_length_s: 3.0&lt;/code&gt;, which is 15 rows. Keep the keys &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;observation.images.cam&lt;/code&gt;, \(v\) in m/s, and \(\omega\) in rad/s. Add one row of numbers and one sentence that says those numbers are the same pair as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; on the Capstone bridge.&lt;/p&gt;

&lt;p&gt;Installing the library is optional. The only command this lesson accepts, if you want a terminal line in the notes, is a version listing:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip index versions lerobot
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected from the command.&lt;/strong&gt; A short list headed by the package name, or an error about the network, permissions, or an old &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt;. Both are acceptable. A version list is not a trained policy. If the command works, stop. Do not continue into a train entrypoint because the install happened to succeed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Expected from the writing.&lt;/strong&gt; Every mention of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt; on your card is m/s and rad/s, including the row you invented. The README line is in quotation marks. The docs snapshot has a date. There is no loss curve and no checkpoint path in the notes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you produced&lt;/th&gt;
      &lt;th&gt;What it actually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Logger comment says PWM, policy note says radians&lt;/td&gt;
      &lt;td&gt;Play time applies the policy to a different robot than record time&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps&lt;/code&gt; times seconds does not equal the image count&lt;/td&gt;
      &lt;td&gt;The turn is stretched. A gentle avoidance becomes a snap, or the reverse&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Teleop recorded with the 300 ms timeout removed&lt;/td&gt;
      &lt;td&gt;The log is of a base that keeps the last command when the link dies. Your real firmware will not do that, and you drove without the safety property&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Timeout still compiled in, link stalls, log full of sudden zeros&lt;/td&gt;
      &lt;td&gt;The policy will copy the stops. Fix the link or mark the frames&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A train command, because the docs list train as step three&lt;/td&gt;
      &lt;td&gt;Close it. The assignment is the card and the quotation&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;optional-hardware-not-this-week&quot;&gt;Optional hardware, not this week&lt;/h2&gt;

&lt;p&gt;This lesson buys nothing. An SO-100-class arm is an imported kit. With servos and shipping it is often many millions of VND in Vietnam. Recheck a listing the week you shop. These notes do not link a shop for an arm. The course does not require one. The card describes the Capstone you already have.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;On your card, which two numbers are the action, and what is the unit of each? Guidance: \(v\) in m/s and \(\omega\) in rad/s, the same pair as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt;. If a column still says PWM, the card is not done.&lt;/li&gt;
  &lt;li&gt;An SO-101 action is joint targets. Your bridge publishes two scalars. Why is “take the first two joints and call them \(v\) and \(\omega\)” not an adapter? Guidance: those entries are joint units on another body. Truncating the arm does not create a diff-drive.&lt;/li&gt;
  &lt;li&gt;You set fps to 5 and the episode to 3.0 s. How many rows? A classmate has 15 images and writes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fps: 10&lt;/code&gt;. How long does that card claim if the camera really ran at 5 Hz? Guidance: \(5 \times 3.0 = 15\) rows. Their card claims \(15/10 = 1.5\) s, so a 3.0 s folder is labeled at half its real duration and the yaw looks twice as fast.&lt;/li&gt;
  &lt;li&gt;The serial link drops for 0.5 s while recording. Timeout is 300 ms and still in the firmware. What does the log show, and what would it show if the timeout had been removed? Guidance: zeros after about 300 ms with the timeout; the last nonzero command held for the whole gap without it. Only the first log matches playback.&lt;/li&gt;
  &lt;li&gt;Paste the README sentence you quoted, then add one clause of your own. Guidance: the quotation is the line on the repository today, not a paraphrase. Your clause should say you are not training this week.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;try-it-later-a-pick-and-place-workcell-from-simready-central&quot;&gt;Try it later: a pick-and-place workcell from SimReady Central&lt;/h2&gt;

&lt;p&gt;Once you understand a LeRobot dataset on paper and want to see an industrial arm do a grasp-and-place in simulation, use &lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt;: find a UR or Franka arm and a Robotiq gripper that already carry joints, limits, and drives, then follow &lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-assets-robot-pick-and-place&quot;&gt;Build a Robot Pick-and-Place Simulation with SimReady Assets&lt;/a&gt; (UR17G + Robotiq 2F-140 + a beverage can) in Isaac Sim. This does &lt;strong&gt;not&lt;/strong&gt; replace a later SO-101 lab, and it is not this week’s assignment.&lt;/p&gt;

&lt;p&gt;Isaac Sim has its own hardware floor (RTX 4080, 16 GB VRAM, 32 GB RAM). If your machine is below that, read the asset and its license, or see the &lt;a href=&quot;/contents/en/chapter12/12_08_home_simulation_lab/&quot;&gt;home simulation lab&lt;/a&gt;. Always check the license on each package; do not assume every asset is CC-BY-4.0.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/lerobot/index&quot;&gt;LeRobot documentation&lt;/a&gt;. Use the index, then the sidebar you actually see. The train step is described there and is not this assignment.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/huggingface/lerobot&quot;&gt;huggingface/lerobot on GitHub&lt;/a&gt;. The one-line description under the title is part of the lab.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>MoveIt 2 optional peek</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter11/11_05_moveit_peek/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter11/11_05_moveit_peek</id>
   <content type="html">&lt;p&gt;Estimated time: about 40 minutes of reading and writing. A full MoveIt demo is not part of the time, because this course’s robot does not have an arm description.&lt;/p&gt;

&lt;p&gt;A mobile base moves in a flat 3-DOF world \((x, y, \theta)\), where a 2D occupancy grid tells you everything about collisions. Now bolt a 6-joint robot arm onto a table: suddenly the robot lives in a 6-dimensional &lt;strong&gt;configuration space&lt;/strong&gt; where moving the elbow to reach a mug might swing the forearm into a shelf. &lt;strong&gt;MoveIt 2&lt;/strong&gt; is the ROS 2 manipulation framework that solves inverse kinematics, checks 3D self-collisions, and plans smooth trajectories in that high-dimensional space.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will say what a planning scene contains, and why joint limits and collision objects belong in that scene before anyone talks about a trajectory. You will explain why the Capstone’s wheel joints are not MoveIt joints even if the URDF marks them continuous. You will list the URDF facts that must exist before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 launch&lt;/code&gt; of a MoveIt demo is a meaningful command, and you will treat &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg list | grep moveit&lt;/code&gt; as a sufficient software check. You will also write a budget warning for an imported learning arm without pretending you know today’s price.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You should know a URDF joint from the Gazebo chapter: revolute, continuous, and fixed are different promises. You should know the Capstone is a differential-drive base whose wheels take velocity, via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; bridge, not a goal angle. Nav2 from the previous lessons is the autonomy stack for that base. You do not need a servo, a gripper, or MoveIt installed in order to finish the written lab.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-configuration-space-c-space-inverse-kinematics-and-sampling-based-planning&quot;&gt;Key ideas: Configuration Space (C-Space), Inverse Kinematics, and Sampling-Based Planning&lt;/h2&gt;

&lt;p&gt;Manipulation planning in MoveIt 2 rests on three classical pillars of robotic kinematics and motion planning:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Workspace vs. Configuration Space (\(\mathcal{C}\)-Space):&lt;/strong&gt; The end-effector target lives in 3D/6D &lt;em&gt;workspace&lt;/em&gt; (\(SE(3)\)), while the arm’s actuators live in \(n\)-dimensional &lt;em&gt;joint space&lt;/em&gt; \(q = (q_1, \dots, q_n)^\top\). Forward kinematics \(x = f(q)\) is unique and fast; &lt;strong&gt;Inverse Kinematics (IK)&lt;/strong&gt; \(q = f^{-1}(x)\) can have multiple solutions (“elbow up” vs. “elbow down”), infinite solutions (redundant arms), or zero solutions (outside reach or singular).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Differential Kinematics &amp;amp; the Manipulator Jacobian \(J(q)\):&lt;/strong&gt; Small joint motions map to end-effector twists via \(\dot{x} = J(q)\dot{q}\). Near a &lt;em&gt;kinematic singularity&lt;/em&gt; (e.g., arm fully stretched straight), \(\det(J) = 0\) and the arm loses mobility in at least one direction while joint velocities explode.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sampling-Based Planners (OMPL — RRT-Connect / PRM):&lt;/strong&gt; Grid search (A*) fails completely in 6D C-space (\(100^6 = 10^{12}\) cells). MoveIt 2 uses randomized trees (RRT-Connect) that sample collision-free joint vectors \(q\) and connect them into smooth, time-parameterized trajectories.
    &lt;h2 id=&quot;why-this-lesson-sits-on-the-path&quot;&gt;Why this lesson sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The path is the Capstone base, then ROS 2 Jazzy, then Gazebo, then wheeled autonomy: a map, a Nav2 tour, a slow vision twist, and a behavior tree that ends in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;. MoveIt sits beside that path. It plans arms. It does not drive every joint. Putting the Capstone wheels into a planning group would ask for joint angles the wheels do not want. Recognize the tool, decline it for this robot, and learn what would have to change before an elective arm is worth a Saturday.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A planning scene holds the arm’s joint state, the allowed collisions, and the obstacles the arm must not hit: a table, a camera mast, the robot’s own links. The planner searches joint space for a path that stays inside those limits. A revolute joint’s lower and upper limits are the angles the servo can reach. Velocity limits keep a hobby servo from being commanded faster than it can track.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_moveit.png&quot; alt=&quot;MoveIt plans a joint-space path for an arm inside a planning scene; the differential-drive wheels stay outside that group&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The ROS interface most demos hide behind is MoveGroup. You send a goal for a named planning group, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arm&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gripper&lt;/code&gt;. MoveGroup checks the scene, calls a planner, and can execute the result through a trajectory controller. That execution path publishes joint trajectories or position goals for the joints in the group. It does not publish the differential-drive Twist your bridge already understands, and it should not be asked to.&lt;/p&gt;

&lt;p&gt;A configuration of an arm group with \(n\) revolute joints is&lt;/p&gt;

\[q = (q_1, q_2, \ldots, q_n),\]

&lt;p&gt;with each coordinate boxed in by its URDF limits,&lt;/p&gt;

\[q_i^{\min} \le q_i \le q_i^{\max}.\]

&lt;p&gt;A 6-DOF learning arm has \(n = 6\) if all six joints are in the group. The Capstone’s wheels are the wrong coordinates. Their useful input is a left speed and a right speed from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;. A wheel joint is often &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt;: no destination angle means “done.” A wrist roll can be continuous. A drive wheel cannot. Success is base velocity, not a hub angle of 1.2 rad. Leave wheels out of every planning group.&lt;/p&gt;

&lt;p&gt;If the arm overlaps the chassis at the home pose, every plan fails, or a plan ignores a link missing from the collision geometry. Fix the model before you swap planners. Limits wider than the servo stall the gearbox. Limits tighter than the hardware waste reach. Measure, then type the numbers.&lt;/p&gt;

&lt;p&gt;The course robot fails the entry test for a demo, and that is a success. A demo wants revolute joints, position limits, and a config package that names the group. A base with wheel joints and a laser mount is a Nav2 robot. Skip MoveIt until an arm is bolted on and the chain is written down.&lt;/p&gt;

&lt;p&gt;An SO-100 or other LeRobot-class arm is an import, not a cheap local grab, and this lesson will not invent a precise dong price. Once shipped, a 6-DOF learning-arm kit is often well above 5 million VND, plus a servo bus. Recheck any listing in the year you find it. There is no pressure to buy an arm. The wheeled Capstone does not get better because a box is on the way.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Read the MoveIt 2 home page until you can describe planning, the planning scene, and MoveGroup. Then write the lab’s five sentences. This paragraph is a quality bar, not text to submit unchanged.&lt;/p&gt;

&lt;p&gt;A planning scene is the arm plus nearby obstacles, updated as joint states change. Continuous wheel joints do not belong: they are driven by base velocity, not by a goal angle. Before a demo launch you need revolute arm joints, numeric limits, and a planning group that omits the wheels. The Capstone does not meet that description, so skip the launch. The command below is the only terminal check this lesson wants.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 pkg list | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;moveit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When MoveIt is installed, the expected output is a list of package names that contain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;moveit&lt;/code&gt;, for example lines mentioning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;moveit_ros&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;moveit_ros_move_group&lt;/code&gt;. The exact set depends on which metapackage you installed. When MoveIt is absent, the expected output is empty: the command prints nothing and returns to the prompt. Both results are acceptable. Do not hunt a demo launch that demands a robot description you do not have. A launch that dies looking for a URDF is not extra credit.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-solving-forward-kinematics-jacobian-and-singularity-for-a-2-link-arm&quot;&gt;Explanation: Hand-Solving Forward Kinematics, Jacobian, and Singularity for a 2-Link Arm&lt;/h3&gt;

&lt;p&gt;Consider a planar 2-link arm with link lengths \(L_1 = 0.50\ \text{m}\), \(L_2 = 0.40\ \text{m}\) and joint angles \((q_1, q_2)\):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Forward Kinematics (end-effector position):&lt;/strong&gt;
\(x = L_1\cos q_1 + L_2\cos(q_1 + q_2), \qquad y = L_1\sin q_1 + L_2\sin(q_1 + q_2).\)
At \(q_1 = 0^\circ, q_2 = 90^\circ\): \(x = 0.50(1) + 0.40(0) = 0.50\ \text{m}\), \(y = 0.50(0) + 0.40(1) = 0.40\ \text{m}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute the \(2\times 2\) Jacobian \(J(q)\) and its determinant:&lt;/strong&gt;
\(J(q) = \begin{bmatrix} -L_1\sin q_1 - L_2\sin(q_1+q_2) &amp;amp; -L_2\sin(q_1+q_2) \\ L_1\cos q_1 + L_2\cos(q_1+q_2) &amp;amp; L_2\cos(q_1+q_2) \end{bmatrix}, \qquad \det J(q) = L_1 L_2 \sin q_2.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Evaluate at \(q_2 = 90^\circ\) vs. \(q_2 = 0^\circ\):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;At \(q_2 = 90^\circ\): \(J = \begin{bmatrix} -0.40 &amp;amp; -0.40 \\ 0.50 &amp;amp; 0 \end{bmatrix}\), \(\det J = (0.50)(0.40)(1) = +0.20\ \text{m}^2\) — well-conditioned, maximum manipulability!&lt;/li&gt;
      &lt;li&gt;At \(q_2 = 0^\circ\) (fully stretched arm): \(\det J = 0.20 \sin(0) = 0\) — &lt;strong&gt;singular&lt;/strong&gt;! The arm physically cannot move radially outward along \(+x\) regardless of how fast its joints spin.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Open &lt;a href=&quot;https://moveit.picknik.ai/main/index.html&quot;&gt;the MoveIt 2 documentation&lt;/a&gt; and write five sentences in your own file, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;moveit-peek.md&lt;/code&gt;. They must cover all three of the following, in any order you like, as long as a reader can find them:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;What a planning scene is.&lt;/li&gt;
  &lt;li&gt;Why continuous wheel joints on the Capstone do not belong in a MoveIt planning group.&lt;/li&gt;
  &lt;li&gt;What you would need before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 launch&lt;/code&gt; of a MoveIt demo is reasonable: a URDF with revolute joints and limits, plus a named group that excludes the wheels.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then run the package-list command from the worked example. Expected output is either several lines containing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;moveit&lt;/code&gt;, pasted under the sentences, or a note that says the command printed nothing. Do not install MoveIt only to satisfy the grep. Do not start RViz for a panda or a fanuc demo unless you already have that description and you are curious after the sentences are done. Curiosity is optional. The five sentences are the lab.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt; prints nothing&lt;/td&gt;
      &lt;td&gt;MoveIt is not installed&lt;/td&gt;
      &lt;td&gt;Write “not installed” and keep the five sentences&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A demo launch asks for a robot description and exits&lt;/td&gt;
      &lt;td&gt;No arm URDF on the Capstone&lt;/td&gt;
      &lt;td&gt;Stop. The lab did not ask for that launch&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Every plan collides at the start pose&lt;/td&gt;
      &lt;td&gt;Arm links overlap the chassis in the URDF&lt;/td&gt;
      &lt;td&gt;Fix collision geometry before changing planners&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Planner commands a wheel angle&lt;/td&gt;
      &lt;td&gt;Wheel joints were left in the planning group&lt;/td&gt;
      &lt;td&gt;Remove continuous wheel joints from the group&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You are pricing an arm during the lab&lt;/td&gt;
      &lt;td&gt;The peek felt like a shopping task&lt;/td&gt;
      &lt;td&gt;It is not. Finish the sentences. Recheck money later&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;No Vietnam shopping list belongs here. If you price an arm months from now, use the budget band in the concepts section, confirm it against a real listing, and add the servo bus. Do not treat Hshop’s sensor pages as an arm catalog.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;h3 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;/h3&gt;

&lt;p&gt;Name three things that belong in a planning scene for an arm mounted on a desk, and one thing that should stay out because it is not geometry.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; In: the arm links, the desk as a collision object, the chassis if the arm can hit it. Out: a PWM frequency, which is an electrical parameter, not a shape. Joint limits are constraints on \(q\), so you may mention them as attached to the robot model rather than as a cardboard box.&lt;/p&gt;

&lt;h3 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;/h3&gt;

&lt;p&gt;A teammate marks both wheel joints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; and adds them to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arm&lt;/code&gt; planning group so “the whole robot is in MoveIt.” Explain the failure in terms of what the planner will try to choose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; The planner will search for wheel angles. The base needs wheel speeds from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;. State that success for a wheel is a velocity, not a pose component.&lt;/p&gt;

&lt;h3 id=&quot;exercise-3&quot;&gt;Exercise 3&lt;/h3&gt;

&lt;p&gt;List the minimum description you want in a URDF before any MoveIt demo launch, in the order you would check it. Include what you would do if the only joints you find are wheel joints.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Chain of revolute joints, lower and upper limits on each, a group name, wheels excluded. If you find only wheel joints, you stop and stay with Nav2.&lt;/p&gt;

&lt;h3 id=&quot;exercise-4&quot;&gt;Exercise 4&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg list | grep moveit&lt;/code&gt; returns you to the prompt with no lines. What is the lab status, and which command are you not required to run next?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; The software check passed in the negative: MoveIt is absent, and that is allowed. You are not required to launch a demo or to apt-install a stack you will not use this week.&lt;/p&gt;

&lt;h3 id=&quot;exercise-5&quot;&gt;Exercise 5&lt;/h3&gt;

&lt;p&gt;Write the short message you would send a teammate who wants to order a 6-DOF learning arm for the Capstone before the base navigates. Include the budget band and the servo bus, and do not invent a model-specific price.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Say the arm is an import, often well above 5 million VND once shipped, plus a servo bus, recheck later. Say the current robot does not need it for Nav2.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://moveit.picknik.ai/main/index.html&quot;&gt;MoveIt 2 documentation&lt;/a&gt; is the page the lab asks you to read for planning scene, MoveGroup, and what a config package assumes.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/configuration_and_development/first_time_robot_setup_guide/urdf/setup_urdf/&quot;&gt;Nav2’s URDF setup notes&lt;/a&gt; are the contrasting document: they describe the base you do have, which is the robot that should stay on the Nav2 side of this course.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Behavior trees peek (Nav2 BT)</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter11/11_04_bt_peek/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter11/11_04_bt_peek</id>
   <content type="html">&lt;p&gt;Estimated time: about 45 minutes, most of it spent reading one installed XML file and writing down names you actually saw.&lt;/p&gt;

&lt;p&gt;Writing autonomy logic with nested &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if/else&lt;/code&gt; statements works for two behaviors, becomes fragile at five, and collapses into unmaintainable spaghetti at twenty—especially when a robot must pause navigation on low battery, spin to recover localization, and resume its mission automatically. &lt;strong&gt;Behavior Trees (BTs)&lt;/strong&gt; replace tangled state machines with a modular, reactive tree ticked from the root at a steady rate—and they are the brain inside Nav2.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will define a Sequence as “every child must succeed, in order” and a Fallback as “the first success wins,” including what happens to children that are never ticked. You will connect those two control nodes to the recoveries you may have watched in the Nav2 tour: replan, spin, back up, clear a costmap. You will find the real XML under &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav2_bt_navigator&lt;/code&gt; after the apt packages are installed, and you will explain why editing that file in place, or expecting the tree to write PWM, is a mistake.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;The Nav2 tour in the previous lesson is the picture this one labels. You should remember that a goal in RViz became a path and then a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; Twist, and that the stack sometimes spins or backs up instead of pushing into a wall. You do not need a robot, a lidar, or a behavior-tree editor with a graphical canvas.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-reactive-execution-trees-control-nodes-and-blackboard-dataflow&quot;&gt;Key ideas: Reactive Execution Trees, Control Nodes, and Blackboard Dataflow&lt;/h2&gt;

&lt;p&gt;Behavior Trees organize complex robot decision-making around three clean primitives:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Three Return Statuses (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt;):&lt;/strong&gt; Every node in a BT returns one of three states on each tick. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt; lets long-duration actions (like driving to a waypoint) yield control back to the root every tick so higher-priority safety checks can preempt them immediately.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Core Control Nodes:&lt;/strong&gt; A &lt;strong&gt;Sequence&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&amp;gt;&lt;/code&gt;) ticks children left-to-right and succeeds only if &lt;em&gt;all&lt;/em&gt; succeed (logical AND); a &lt;strong&gt;Fallback / Selector&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt;) ticks left-to-right and succeeds as soon as &lt;em&gt;one&lt;/em&gt; succeeds (logical OR, ideal for recovery behaviors); &lt;strong&gt;Decorators&lt;/strong&gt; wrap a single child (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Retry(3)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RateController&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Inverter&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Blackboard Key–Value Store:&lt;/strong&gt; Instead of wiring spaghetti arguments between nodes, BT nodes share typed variables (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{goal}&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{path}&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{battery_ok}&lt;/code&gt;) through a scoped blackboard.
    &lt;h2 id=&quot;why-this-lesson-sits-on-the-path&quot;&gt;Why this lesson sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The path is the Capstone base, then ROS 2 Jazzy, then Gazebo, then autonomy. The TurtleBot3 tour chased a goal because Nav2’s receptionist is a behavior tree. Without the tree, a failed controller stops the robot until a human clicks again. With the tree, failure becomes a recovery and another attempt. Your bridge stays downstream of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;. The tree never becomes the H-bridge. This lesson only reads the policy the tour already ran.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A behavior tree is a tick-driven hierarchy. Something ticks the root many times a second. Each node returns a status: success, failure, or running. Control nodes do not move the robot. They decide which child is ticked next. Action nodes are the leaves that call into ROS: compute a path, follow a path, spin, wait, clear a costmap.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_bt.png&quot; alt=&quot;A small behavior tree: a fallback tries a plan-and-follow sequence, and on failure ticks a recovery&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A Sequence ticks children from first to last. The first failure fails the Sequence, and later siblings do not run. Every child succeeding means the Sequence succeeds. A child that is still running keeps the Sequence running. Think of a checklist: compute a path, then follow it. A failed path computation means you never start following.&lt;/p&gt;

&lt;p&gt;A Fallback ticks children in order and stops at the first success. Later children are not ticked. The Fallback fails only when every child has failed. That is “try the plan, otherwise recover.” A ReactiveFallback keeps checking the earlier children, so a newly clear path can preempt a spin. Nav2 uses that so a goal update does not wait out a recovery that is no longer needed.&lt;/p&gt;

&lt;p&gt;The navigate tree you will open on disk is longer than the sketch below, and it is the copyrighted project file, so this page does not paste it. The structure you should expect, in words, is a navigation branch that recomputes a path and follows it, wrapped so that failure falls through to recoveries. Those recoveries in Nav2’s own trees are actions you can recognize by name once the file is open: spin in place, back up a short distance, and clear a global or local costmap that has become painted with phantom obstacles. The planner and the controller from the tour are leaves the tree ticks. They are not replaced by the tree.&lt;/p&gt;

&lt;p&gt;The tree’s leaves talk to ROS action servers. Follow-path ends in the controller publishing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;. Spin and back up publish Twist as well, for a short, deliberate motion. Nowhere in that chain is a PWM duty cycle. If the Capstone wheels move during a recovery, it is because your bridge, or a simulated drive, subscribed to that Twist. Deleting the recovery branch to “simplify” the file makes the first blocked path a dead end. The tour looked clever because those leaves were still there.&lt;/p&gt;

&lt;p&gt;BehaviorTree.CPP is the library Nav2 uses. The project site is the place to read what Sequence and Fallback mean in the library’s own words. Nav2’s getting-started pages are the place that ties that library to the navigator you launched. You do not need to vendor the library or write a C++ node this week.&lt;/p&gt;

&lt;p&gt;Here is an original sketch, small enough to read aloud, and deliberately not the installed file. The comments name the real leaf types without copying their XML:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;root&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;BTCPP_format=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;4&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;BehaviorTree&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ID=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;CapstoneNavigateSketch&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;ReactiveFallback&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;NavigateOrRecover&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Sequence&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PlanThenFollow&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- Real trees put ComputePathToPose here and write a path. --&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- The next sibling is FollowPath, which tracks that path. --&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Sequence&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Sequence&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;OneRecovery&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- Real trees put Spin, BackUp, or ClearEntireCostmap here. --&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Sequence&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/ReactiveFallback&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/BehaviorTree&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/root&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If plan-and-follow succeeds, the recovery sequence is not ticked. If it fails, the fallback ticks the recovery. A later tick can try the plan again because the control is reactive. A plain Fallback would still stop at the first success, without that re-check. The file on disk chooses. Write down what it chose.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Install the same Nav2 packages as the tour if they are not present, source Jazzy, and locate the package instead of guessing a prefix:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-navigation2 ros-jazzy-nav2-bringup
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 pkg prefix nav2_bt_navigator
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The prefix on a stock apt install is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt;. The trees then live in:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt; /opt/ros/jazzy/share/nav2_bt_navigator/behavior_trees/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If the prefix command prints a different directory, because an overlay is sourced, list &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;share/nav2_bt_navigator/behavior_trees/&lt;/code&gt; under that prefix instead. Do not hard-code a home directory path you have not printed.&lt;/p&gt;

&lt;p&gt;Expected listing, the shape rather than a promise of every filename: one or more &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.xml&lt;/code&gt; files, and at least one name that contains &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigate&lt;/code&gt;. Open one of those navigate files in a pager:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;less /opt/ros/jazzy/share/nav2_bt_navigator/behavior_trees/navigate_to_pose_w_replanning_and_recovery.xml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;less&lt;/code&gt; says the file does not exist, run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; again and open the navigate-named file that does exist. Inside, find the first control node under the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BehaviorTree&lt;/code&gt; element. That tag is the root control. Then search the file for recovery-looking leaves. You are looking for names in the spirit of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Spin&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BackUp&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ClearEntireCostmap&lt;/code&gt;. Quote two that are actually in the file you opened. If a name differs by a suffix, copy the suffix. The sketch on this page is not evidence.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-tracing-3-ticks-of-a-nav2-style-recovery-behavior-tree&quot;&gt;Explanation: Hand-Tracing 3 Ticks of a Nav2-Style Recovery Behavior Tree&lt;/h3&gt;

&lt;p&gt;Consider a compact autonomous delivery BT rooted at a &lt;strong&gt;Fallback (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt;)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Child 1 (Sequence &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&amp;gt;&lt;/code&gt;):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[CheckBatteryOK]&lt;/code&gt; \(\rightarrow\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[NavigateToGoal]&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Child 2 (Fallback &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt; Recovery):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[SpinClearCostmap]&lt;/code&gt; \(\rightarrow\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[ReturnToDock]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trace the exact return values across three ticks:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tick 1 (Battery 85%, corridor clear):&lt;/strong&gt; Root ticks Child 1 (Sequence): &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CheckBatteryOK&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt; \(\rightarrow\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateToGoal&lt;/code&gt; starts driving and returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt; \(\rightarrow\) Child 1 returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt; \(\rightarrow\) Root returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt;. (Child 2 is never ticked.)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tick 2 (Battery 82%, doorway blocked by a box):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CheckBatteryOK&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt; \(\rightarrow\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateToGoal&lt;/code&gt; fails to find a path and returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt; \(\rightarrow\) Child 1 returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt;. Because Root is a &lt;strong&gt;Fallback&lt;/strong&gt;, it immediately ticks Child 2: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SpinClearCostmap&lt;/code&gt; spins \(90^\circ\) and returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RUNNING&lt;/code&gt; (then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUCCESS&lt;/code&gt; once clear).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tick 3 (Battery drops to 12% mid-drive):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CheckBatteryOK&lt;/code&gt; immediately returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt; \(\rightarrow\) Child 1 halts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateToGoal&lt;/code&gt; cleanly and returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAILURE&lt;/code&gt; \(\rightarrow\) Recovery invokes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReturnToDock&lt;/code&gt;. Notice how reactivity comes &lt;em&gt;for free&lt;/em&gt; from ticking the root on every cycle—no manual transition arrows required.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Do the worked example on your Jazzy machine. The deliverable is a short note with three parts.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;behavior_trees&lt;/code&gt; directory. Expected: a file list that includes an XML file whose name contains &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigate&lt;/code&gt;. Paste the list.&lt;/li&gt;
  &lt;li&gt;The tag of the root control node in the navigate file you opened, copied from the file, plus the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; attribute if it has one.&lt;/li&gt;
  &lt;li&gt;Two recovery actions you saw in that same file, copied by their tag names.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg prefix nav2_bt_navigator&lt;/code&gt; fails, the package is not installed or the shell is not sourced. Apt install, source again, and only then conclude the path differs. If the XML path differs from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy/share/...&lt;/code&gt;, the prefix command is the authority. Record the prefix you got.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Package not found&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Nav2 apt packages missing, or setup file not sourced&lt;/td&gt;
      &lt;td&gt;The install and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt; lines, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg prefix&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Directory missing under &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Overlay prefix, or a partial install&lt;/td&gt;
      &lt;td&gt;Use the printed prefix, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; that share path&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; shows XML, none with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigate&lt;/code&gt; in the name&lt;/td&gt;
      &lt;td&gt;You listed the wrong folder&lt;/td&gt;
      &lt;td&gt;Confirm the path ends in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;behavior_trees&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You edited the XML and the tour will not start&lt;/td&gt;
      &lt;td&gt;In-place edit, no copy&lt;/td&gt;
      &lt;td&gt;Restore from apt; copy to a workspace before any edit&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Recoveries disappeared from your copy&lt;/td&gt;
      &lt;td&gt;They were deleted to “simplify”&lt;/td&gt;
      &lt;td&gt;The tour’s spin and backup lived in those leaves&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You expect the tree to twitch a GPIO&lt;/td&gt;
      &lt;td&gt;The tree ends in Twist, like Nav2&lt;/td&gt;
      &lt;td&gt;PWM stays in your bridge&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Software only. There is nothing to buy, and a behavior-tree GUI is not required to read one file.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;h3 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;/h3&gt;

&lt;p&gt;A Sequence has three children. On this tick the first returns success and the second returns failure. What does the Sequence return, and does the third child run?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Sequence fails at the first failed child. The third child is not ticked. The answer is failure, with child three untouched.&lt;/p&gt;

&lt;h3 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;/h3&gt;

&lt;p&gt;A Fallback has three children: Spin returns failure, BackUp returns success, ClearCostmap has not been ticked. What does the Fallback return, and which children ran?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; First success wins. Spin ran and failed, BackUp ran and succeeded, ClearCostmap did not run. The Fallback returns success.&lt;/p&gt;

&lt;h3 id=&quot;exercise-3&quot;&gt;Exercise 3&lt;/h3&gt;

&lt;p&gt;You want to experiment with removing Wait from a recovery list. Why is changing the file under &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt; the wrong first edit, and where should a copy live?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Apt owns that directory. A later upgrade overwrites it, and a syntax error breaks every launch. Copy into a workspace package and point the launch at the copy.&lt;/p&gt;

&lt;h3 id=&quot;exercise-4&quot;&gt;Exercise 4&lt;/h3&gt;

&lt;p&gt;In two or three sentences, explain why a behavior tree does not drive PWM on the Capstone, even when a recovery spins the simulated TurtleBot3.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Name the leaf, the Twist, and the bridge. The simulated robot has its own drive that subscribes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;exercise-5&quot;&gt;Exercise 5&lt;/h3&gt;

&lt;p&gt;After &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt;, imagine the directory contains &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;navigate_to_pose_w_replanning_and_recovery.xml&lt;/code&gt;. What will you write down from inside the file, and what will you refuse to copy from the sketch on this page?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Write the real root control tag and two recovery tags from the file. Refuse to submit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateOrRecover&lt;/code&gt; unless that exact name is what you opened.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.behaviortree.dev/&quot;&gt;BehaviorTree.CPP&lt;/a&gt; documents the library Nav2 embeds, including Sequence, Fallback, and reactive variants.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/&quot;&gt;Nav2 getting started for Jazzy&lt;/a&gt; is the project page that links the navigator’s behavior trees to the stack you toured.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Basic vision pipeline for robots</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter11/11_03_vision_pipeline/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter11/11_03_vision_pipeline</id>
   <content type="html">&lt;p&gt;Estimated time: about 75 minutes when a USB camera enumerates as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video0&lt;/code&gt;, and about 45 minutes when you document a missing device and still walk the pipeline on paper.&lt;/p&gt;

&lt;p&gt;A 2D LiDAR sees the world as a flat horizontal slice of distances—it cannot tell a red stop sign from a cardboard box, nor can it see a low doorstep beneath its scan plane. Adding a camera gives your robot rich semantic and geometric perception, turning a stream of RGB pixel arrays into 3D bearings, AprilTag poses, and target-tracking velocity commands.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You can order the pipeline as light, then camera, then algorithm, and you will not retune thresholds while a window blows out the card. You can say what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam&lt;/code&gt; publishes, compute a centroid, act once at 5 to 10 Hz instead of every frame, state how an optical frame differs from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; under REP-103, and record a missing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video0&lt;/code&gt; as a result rather than a broken lab.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can run a short Python script and you know that an image is an array. You have met &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; in the Nav2 tour, so you know a velocity command has a linear part and an angular part. You do not need a depth camera, and this lesson never asks for a RealSense.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-pinhole-camera-geometry-lens-calibration-and-visual-servoing&quot;&gt;Key ideas: Pinhole Camera Geometry, Lens Calibration, and Visual Servoing&lt;/h2&gt;

&lt;p&gt;Robotic computer vision connects projective geometry to real-time control across three pillars:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Pinhole Projection &amp;amp; Intrinsic Matrix \(K\):&lt;/strong&gt; A 3D point \(P_c = (X, Y, Z)^\top\) in the camera frame projects to pixel coordinates \((u, v)\) via \(s\,[u, v, 1]^\top = K P_c\), where \(K = \begin{bmatrix} f_x &amp;amp; 0 &amp;amp; c_x \\ 0 &amp;amp; f_y &amp;amp; c_y \\ 0 &amp;amp; 0 &amp;amp; 1 \end{bmatrix}\) encodes focal lengths (in pixels) and principal point \((c_x, c_y)\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Radial/Tangential Distortion &amp;amp; Calibration:&lt;/strong&gt; Wide-angle robot cameras bend straight edges into barrel curves. Calibrating with a checkerboard estimates \(K\) and distortion coefficients \((k_1, k_2, p_1, p_2)\) so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image_proc&lt;/code&gt; can undistort pixels before geometry estimation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Image-Based Visual Servoing (IBVS):&lt;/strong&gt; Even without full 3D depth, the horizontal pixel error \(e_u = c_x - u_{\text{target}}\) maps directly to a proportional yaw rate \(\omega = K_p e_u\)—closing a clean perception-to-action feedback loop.
    &lt;h2 id=&quot;why-this-lesson-sits-on-the-path&quot;&gt;Why this lesson sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The path is the Capstone base, then ROS 2 Jazzy, then Gazebo, then autonomy. The base already bridges &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; to PWM. Jazzy is how a camera would publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/Image&lt;/code&gt; beside the lidar topics. A colored card on the desk shows thresholding fail in real light faster than a simulated camera. The autonomous act here is modest: yaw toward the card, or stop. That is the same Twist Nav2 publishes. A loop at 30 Hz fights the bridge. A loop at 5 to 10 Hz can share the robot with a later navigator.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;Start with the light. A red card under a bare bulb and the same card in afternoon sun are different measurements. If your eyes cannot see a distinct patch, OpenCV will not save the demo. Move the lamp or kill the backlight before you edit hue limits. The camera comes second: a USB UVC device, often &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video0&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v4l2_camera&lt;/code&gt; publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/Image&lt;/code&gt;. The algorithm comes third, and here it is a color threshold plus a centroid, not a neural detector.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_vision.png&quot; alt=&quot;A camera image becomes a threshold mask, then a centroid that can drive one slow yaw command&quot; /&gt;&lt;/p&gt;

&lt;p&gt;On a Jazzy machine with the binary installed, the driver shape is:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-usb-cam
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 run usb_cam usb_cam_node_exe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 launch usb_cam camera.launch.py&lt;/code&gt; adds a viewer. The image topic is commonly &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/image_raw&lt;/code&gt; of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/Image&lt;/code&gt;. If it is silent, fix the device path before any threshold. The lab does not require this node. It uses OpenCV directly.&lt;/p&gt;

&lt;p&gt;HSV thresholding is more stable than a raw red channel, and still fragile. Freeze one lower bound, one upper bound, and the lighting you used. The mask is white on the card. Moments turn it into a centroid. For pixels \((x_i, y_i)\),&lt;/p&gt;

\[c_x = \frac{\sum_i x_i}{N}, \qquad c_y = \frac{\sum_i y_i}{N},\]

&lt;p&gt;which is what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cv2.moments&lt;/code&gt; computes when it divides &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m10&lt;/code&gt; by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m00&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m01&lt;/code&gt; by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m00&lt;/code&gt;. Work a three-pixel mask by hand: white pixels at \((100, 80)\), \((102, 80)\), and \((101, 82)\). Then&lt;/p&gt;

\[c_x = \frac{100 + 102 + 101}{3} = 101, \qquad c_y = \frac{80 + 80 + 82}{3} \approx 80.67.\]

&lt;p&gt;If the image is 320 pixels wide, the horizontal center is 160. The blob sits left of center. A forward-facing camera sees that as “target on the left,” so a differential-drive base should yaw left, which is a positive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; in REP-103, until the centroid walks toward the center. If the area &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m00&lt;/code&gt; is tiny, there is no card. Publish nothing, or publish a zero twist, rather than chasing noise.&lt;/p&gt;

&lt;p&gt;Act slowly. A camera may deliver 30 frames a second. The motors should not get 30 new twists. Decide at 5 to 10 Hz, emit one Twist or a stop, and hold it. Full frame rate chatters a gearbox.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; gives the body &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; forward, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; left, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; up. An optical frame is different: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; out of the lens, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; right in the image, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; down in the image. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; is usually a body-style mount. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_optical_frame&lt;/code&gt; is what should be stamped on the image. The usual fixed joint from a body-style &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; uses roll \(-\pi/2\) and yaw \(-\pi/2\). Read the joint in your URDF anyway. A pixel centroid is not a point in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; until that rotation and a camera model exist. This lesson does not require the projection. Yaw toward the pixel.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;The following script is the whole algorithm without ROS. It opens the first camera, thresholds a broad orange band in HSV, and prints one centroid. Orange is only an example. If your card is blue, change the bounds and write the new numbers in your notes. Run it from a directory where you can see the terminal:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python3 card_centroid.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;cap&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;VideoCapture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isOpened&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;error: cannot open /dev/video0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;release&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;error: camera opened but read failed&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;hsv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cvtColor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;COLOR_BGR2HSV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;upper&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;inRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hsv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;moments&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;moments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;area&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;m00&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;area&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;centroid none area=0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;m10&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;moments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;m01&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;centroid u=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; v=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; area=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Hold an orange card in front of the lens under a steady lamp. A healthy print looks like:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;centroid u=312 v=248 area=1840
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Your integers will differ. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; grows as the card moves right. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;area&lt;/code&gt; grows as it comes closer. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;centroid none area=0&lt;/code&gt; means the bounds miss this lamp: change the light, then the bounds. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error: cannot open /dev/video0&lt;/code&gt; is a valid lab line. Record it. Do not invent a centroid.&lt;/p&gt;

&lt;p&gt;A later ROS node decides at 5 or 10 Hz. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;area&lt;/code&gt; is below 500, publish a zero Twist. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; is left of center, publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; near 0.3. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; is right of center, publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; near -0.3. Hold that command until the next tick. Enough to twitch the bridge toward a card. Not a navigator.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-projecting-a-3d-target-to-pixels-and-computing-yaw-command&quot;&gt;Explanation: Hand-Projecting a 3D Target to Pixels and Computing Yaw Command&lt;/h3&gt;

&lt;p&gt;Suppose your calibrated 640×480 camera has intrinsics \(f_x = f_y = 600\ \text{px}\) and principal point \((c_x, c_y) = (320, 240)\ \text{px}\). A fiducial target sits at \(P_c = (X, Y, Z) = (0.30,\ -0.15,\ 1.50)\ \text{m}\) in the camera optical frame (\(+X\) right, \(+Y\) down, \(+Z\) forward):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Project \(P_c\) onto pixel coordinates \((u, v)\) using \(K\):&lt;/strong&gt;
\(u = f_x \frac{X}{Z} + c_x = 600 \cdot \frac{0.30}{1.50} + 320 = 120 + 320 = 440\ \text{px},\)
\(v = f_y \frac{Y}{Z} + c_y = 600 \cdot \frac{-0.15}{1.50} + 240 = -60 + 240 = 180\ \text{px}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute bearing angle and visual-servoing yaw command:&lt;/strong&gt; The target appears \(120\ \text{px}\) to the &lt;em&gt;right&lt;/em&gt; of image center, corresponding to horizontal bearing angle:
\(\theta_{\text{err}} = \arctan\!\left(\frac{u - c_x}{f_x}\right) = \arctan\!\left(\frac{120}{600}\right) = \arctan(0.20) = 0.197\ \text{rad} \approx 11.3^\circ.\)
Under REP-103 (\(+\omega\) = turn left, \(-\omega\) = turn right), a proportional visual-servoing gain \(K_p = 1.5\ \text{s}^{-1}\) commands \(\omega = -1.5(0.197) = -0.296\ \text{rad/s}\)—turning right smoothly until \(u \to 320\ \text{px}\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Save the script as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;card_centroid.py&lt;/code&gt; and run it. Pick the expected output that matches the machine you actually have.&lt;/p&gt;

&lt;p&gt;If a camera is present and the card is in view, the expected line matches this pattern, with your own integers:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;centroid u=312 v=248 area=1840
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If the camera is present and the mask is empty, the expected line is:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;centroid none area=0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If no camera enumerates, the expected line is:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;error: cannot open /dev/video0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If the device is missing, also paste the complaint from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls /dev/video*&lt;/code&gt;. Then write four sentences: light, device, threshold, centroid, and why Twist is not published at frame rate.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error: cannot open /dev/video0&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;No UVC device, or the cable is charge-only&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls /dev/video*&lt;/code&gt; and a different port&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Camera opens, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;centroid none&lt;/code&gt; forever&lt;/td&gt;
      &lt;td&gt;Hue bounds fight the lamp, or the card is out of frame&lt;/td&gt;
      &lt;td&gt;Look at the card with your eyes, then the bounds&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Centroid jitters by tens of pixels on a still card&lt;/td&gt;
      &lt;td&gt;Mixed sun and indoor light, or a tiny mask&lt;/td&gt;
      &lt;td&gt;One lamp, raise the area cutoff&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheels buzz when you later connect Twist&lt;/td&gt;
      &lt;td&gt;You published at frame rate&lt;/td&gt;
      &lt;td&gt;Timer at 5 to 10 Hz, one command per tick&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Card moves right in the world but &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; moves left&lt;/td&gt;
      &lt;td&gt;Image is mirrored, or you swapped the yaw sign&lt;/td&gt;
      &lt;td&gt;Trust &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; increasing to the right in the array&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You tried to aim with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; axes&lt;/td&gt;
      &lt;td&gt;Optical &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; is down, body &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; is up&lt;/td&gt;
      &lt;td&gt;REP-103 optical versus body&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;buying-a-camera-in-vietnam&quot;&gt;Buying a camera in Vietnam&lt;/h2&gt;

&lt;p&gt;A depth camera is out of scope. Search Shopee for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera USB UVC&lt;/code&gt;. Ordinary webcams have recently been about 150 to 450 thousand VND. Recheck, and avoid charge-only cables.&lt;/p&gt;

&lt;p&gt;An IMX219-class Pi camera is the other common choice, often a few hundred thousand dong. A Waveshare IMX219-77 has been listed near 432.000 VND. Recheck the current page before you order. Either camera can feed the script or a ROS image topic. Neither has to be a RealSense.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;h3 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;/h3&gt;

&lt;p&gt;The card vanishes from the mask whenever a window opens, and it returns when you close the curtain, with the same hue numbers. Which stage do you debug, and which stage do you leave alone?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Light is first. Do not rewrite the moment formula. Say what you would change in the room.&lt;/p&gt;

&lt;h3 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;/h3&gt;

&lt;p&gt;A mask has white pixels only at \((10, 10)\), \((14, 10)\), \((12, 16)\), and \((12, 12)\). Compute \(c_x\) and \(c_y\). The image width is 64. Is the blob left or right of center?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Average the four &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; values and the four &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; values. Center is 32. Compare \(c_x\) to 32.&lt;/p&gt;

&lt;h3 id=&quot;exercise-3&quot;&gt;Exercise 3&lt;/h3&gt;

&lt;p&gt;Your camera publishes 30 images each second. Explain why the bridge should see a new Twist only 5 to 10 times each second, using the Capstone motors as the reason.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Talk about chatter and about sharing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; with a later navigator. One sentence on a timer is enough.&lt;/p&gt;

&lt;h3 id=&quot;exercise-4&quot;&gt;Exercise 4&lt;/h3&gt;

&lt;p&gt;State the three axis directions of an optical frame, and the three axis directions of a REP-103 body frame. Say which frame should be stamped on the image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Optical: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; out of the lens, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; right, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; down. Body: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; forward, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; left, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; up. The image uses the optical frame.&lt;/p&gt;

&lt;h3 id=&quot;exercise-5&quot;&gt;Exercise 5&lt;/h3&gt;

&lt;p&gt;The script prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error: cannot open /dev/video0&lt;/code&gt;. List three checks that teach you something without buying a camera, and state what you still owe in writing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; the device directory, try another USB port, record the exact error. You still owe the four-sentence pipeline note from the lab.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;The OpenCV &lt;a href=&quot;https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html&quot;&gt;thresholding tutorial&lt;/a&gt; is the reference for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inRange&lt;/code&gt; style segmentation and for looking at a mask before you trust a centroid.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ros-drivers/usb_cam&quot;&gt;usb_cam&lt;/a&gt; documents &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam_node_exe&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera.launch.py&lt;/code&gt; for a V4L2 camera on ROS 2.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; is the standard that separates body axes from the optical frame used on images.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Nav2 stack tour</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter11/11_02_nav2_tour/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter11/11_02_nav2_tour</id>
   <content type="html">&lt;p&gt;Estimated time: about 55 minutes for the software tour on a machine that can open RViz, or about 40 minutes if you only have a terminal and you stop after recording why the GUI launch did not run.&lt;/p&gt;

&lt;p&gt;Having a map of a building does not get a robot from the lobby to the kitchen—it still has to localize itself inside that map, plan a collision-free route around furniture, dodge a person stepping into the hallway, and back out gently if it gets boxed into a corner. &lt;strong&gt;Nav2&lt;/strong&gt; is ROS 2’s modular navigation stack that orchestrates all four jobs continuously in real time.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You can name the five blocks between a map goal and a velocity, point at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; as the only thing Nav2 owes your motor driver, run the Jazzy TurtleBot3 simulation as a tour with an initial pose, and say why that tour does not mean the Capstone navigates tonight. You can also match an empty costmap to missing odometry, a bad laser frame, a reliability mismatch, a wrong footprint, or a stray &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 11-01’s frame story is assumed: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; are different promises. You should know that the Capstone firmware bridge from the earlier chapter subscribes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; and writes PWM. You do not need a lidar on the desk for this tour, and you should not pause the tour to buy one.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-localization-amcl-costmaps--global-planning-and-local-trajectory-control&quot;&gt;Key ideas: Localization (AMCL), Costmaps &amp;amp; Global Planning, and Local Trajectory Control&lt;/h2&gt;

&lt;p&gt;Nav2 decomposes autonomous navigation into three cooperating mathematical layers:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Monte Carlo Localization (AMCL):&lt;/strong&gt; A particle filter maintains hundreds of weighted pose hypotheses \(\{(x_k, y_k, \theta_k, w_k)\}\) on the static map, propagating them with wheel odometry and re-weighting them against live LiDAR scans to publish the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map -&amp;gt; odom&lt;/code&gt; transform.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Layered Costmaps &amp;amp; Global Planners (A* / Smac):&lt;/strong&gt; Both global and local costmaps inflate every obstacle outward by the robot’s inscribed radius \(r_{\text{inscribed}}\) plus a decaying exponential safety cost. Global planners (NavFn / Smac) search this 2D/SE(2) grid for a minimum-cost path to the goal.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Local Controller (DWB / MPPI):&lt;/strong&gt; While the global path updates slowly (~1 Hz), the local controller runs at 10–20 Hz, sampling feasible \((v, \omega)\) velocity commands, rolling out short arcs, scoring each arc on obstacle clearance, path alignment, and goal progress, and publishing the winning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;.
    &lt;h2 id=&quot;why-this-lesson-sits-on-the-path&quot;&gt;Why this lesson sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The path is the Capstone base, then ROS 2 Jazzy, then Gazebo, then autonomy. Autonomy here means a stack that accepts a pose on a map and publishes a velocity. That stack is Nav2. It does not replace your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; bridge and it does not know which pin is the left motor. Watch it succeed on the TurtleBot3 the Nav2 project already wired up in simulation. Leave a parameter file for your own robot until a map, a scan, and odometry are real.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;Nav2 is not one node. On the Jazzy getting-started path it is a behavior-tree navigator, a planner, a controller, costmaps, and recoveries, talking to each other through actions and topics.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_nav2.png&quot; alt=&quot;Nav2: a goal enters the behavior-tree navigator, a planner and a controller consult costmaps, and a Twist leaves toward the base&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The behavior-tree navigator is the receptionist. A goal pose arrives from RViz. The tree decides whether to compute a path, follow it, or run a recovery. The next lesson opens that tree. Today you only need to know that the navigator is why the robot does not freeze forever at the first blocked cell.&lt;/p&gt;

&lt;p&gt;The planner writes a path on the global costmap. In current Nav2 builds that is often a Smac planner, or the older NavFn grid search, chosen as a plugin. The controller tracks the path on the local costmap. Jazzy configs name DWB, which scores short rolled-out trajectories, and MPPI, which samples many velocity commands. Both publish velocity. Neither publishes PWM.&lt;/p&gt;

&lt;p&gt;Costmaps are grids of how bad it is to stand in a cell. They need a map and a sensor stream whose quality of service connects. Laser scans are typically best effort. A subscription that insists on reliable delivery will not connect to a best-effort publisher, so the grid stays blank while Gazebo shows beams. The &lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Quality-of-Service-Settings.html&quot;&gt;Jazzy QoS article&lt;/a&gt; states the rule: a reliable subscription matches a reliable publisher only.&lt;/p&gt;

&lt;p&gt;The message that leaves the controller is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt;. In components,&lt;/p&gt;

\[v_x,\; v_y,\; v_z,\; \omega_x,\; \omega_y,\; \omega_z.\]

&lt;p&gt;A differential-drive Capstone uses \(v_x\) and \(\omega_z\). The sideways component \(v_y\) stays at zero because the robot cannot strafe. If a holonomic critic or a copied mecanum parameter file asks for \(v_y\), your bridge has no honest PWM for that axis. Configure the controller for a non-holonomic base, and treat a nonzero &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; as a bug in the command, not as a missing motor.&lt;/p&gt;

&lt;p&gt;If the controller fails, the tree may spin, back up, or clear a costmap full of old obstacles. You may see that if the goal sits behind a tight gap. You are not asked to author recoveries today.&lt;/p&gt;

&lt;p&gt;Footprint tells the costmap the body’s size. A footprint much larger than the chassis makes doorways illegal. A laser yaw off by half a turn paints obstacles behind the robot. Missing odometry is worse: the local controller integrates a twist it cannot correct, and the robot drifts while the global plan still looks fine.&lt;/p&gt;

&lt;p&gt;None of this knows your motor driver. Your bridge is the only translation from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; to PWM. Nav2 ending at Twist keeps the stack from growing a second copy of your H-bridge timing.&lt;/p&gt;

&lt;p&gt;A lidar note, kept short: the simulation tour publishes its own scan. You do not need the LD19-class sensor from lesson 11-01 to finish this page. When you later want Nav2 on the physical base, that lesson’s shopping note is the place to price a 360-degree scanner. A single-point ToF still will not fill a costmap the way a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt; does.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;This is a tour of a working robot that is not yours. The &lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/quickstart/quickstart/&quot;&gt;Jazzy quickstart&lt;/a&gt; installs Nav2 and launches a TurtleBot3 in simulation. Quote it as a visit, not as a template you retune.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-navigation2 ros-jazzy-nav2-bringup
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 launch nav2_bringup tb3_simulation_launch.py headless:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless&lt;/code&gt; defaults to true on that page. Left at the default, the 3D client does not start, and you lose the view you need in order to compare the Gazebo robot with the map. Set it false, as written.&lt;/p&gt;

&lt;p&gt;You should see RViz and a Gazebo world with a small differential-drive robot. Jazzy uses modern Gazebo; the quickstart’s gestures are the same. If the Nav2 panel is not already active, press Startup so the map appears.&lt;/p&gt;

&lt;p&gt;The robot does not know where it is until you tell it. Click &lt;strong&gt;2D Pose Estimate&lt;/strong&gt;, click the map where Gazebo shows the robot, and drag the heading. That pose lets AMCL finish the transform tree. Only then click the goal button (the quickstart labels it Navigaton2 Goal) and pick a free cell. In a second terminal:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /cmd_vel
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While the simulated robot moves, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; leaves zero and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; stays 0.0. That Twist is what a bridge would turn into PWM. This TurtleBot3 already has a simulated drive. Do not point your Capstone bridge at this tour. Do not edit costmap resolution, inflation, or controller critics. If the robot reaches a nearby goal, the tour worked.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-scoring-two-candidate-dwb-local-trajectories&quot;&gt;Explanation: Hand-Scoring Two Candidate DWB Local Trajectories&lt;/h3&gt;

&lt;p&gt;Suppose your robot (radius \(r = 0.12\ \text{m}\)) is following a global path at \(20\ \text{Hz}\) and the DWB controller evaluates two candidate velocity commands over a \(1.0\ \text{s}\) rollout using cost function:
\(J = 2.0\,d_{\text{path}} + 1.0\,d_{\text{goal}} + 3.0\,c_{\text{obs}},\)
where \(d_{\text{path}}\) is distance from the global path (m), \(d_{\text{goal}}\) is remaining distance to local sub-goal (m), and \(c_{\text{obs}} \in [0, 1]\) is normalized obstacle proximity cost (\(\infty\) if inside lethal radius):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Candidate \((v, \omega)\)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;\(d_{\text{path}}\) (m)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;\(d_{\text{goal}}\) (m)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;\(c_{\text{obs}}\)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Total Cost \(J\)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;A:&lt;/strong&gt; \((0.25,\ 0.00)\) straight&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.05&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.75&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.60 (grazes chair)&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;\(2(0.05)+1(0.75)+3(0.60) = \mathbf{2.65}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;B:&lt;/strong&gt; \((0.20,\ +0.40)\) gentle left&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.12&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.82&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.10 (wide clearance)&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;\(2(0.12)+1(0.82)+3(0.10) = \mathbf{1.36}\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Candidate &lt;strong&gt;B&lt;/strong&gt; wins decisively (\(1.36 &amp;lt; 2.65\)): it trades 7 cm of path deviation for a huge gain in obstacle clearance, smoothly steering around the chair without ever triggering a full replan.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Run the three commands in the worked example on a desktop session. Expected output is visual plus one topic sample:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A Gazebo window and an RViz window, because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless:=False&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;After Startup, if it was required, a map in RViz.&lt;/li&gt;
  &lt;li&gt;After &lt;strong&gt;2D Pose Estimate&lt;/strong&gt;, the robot model sitting on that map with a particle cloud.&lt;/li&gt;
  &lt;li&gt;After the goal, the robot moving in the simulation, and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; echo in this shape while it turns or drives:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;linear:
  x: 0.15
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The numbers will not match exactly. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; under &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear&lt;/code&gt; should stay 0.0 for this differential-drive model. Paste one real sample from your echo into your notes.&lt;/p&gt;

&lt;p&gt;If there is no display, or the launch errors before RViz, paste the error and still name the five blocks and the Twist on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;. The Capstone did not finish navigation homework. It still needs a map, a scan, odometry, a footprint, and your bridge.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;No 3D world&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless&lt;/code&gt; left at its default true&lt;/td&gt;
      &lt;td&gt;Relaunch with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless:=False&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Goal does nothing, transform tree incomplete&lt;/td&gt;
      &lt;td&gt;Initial pose never set&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;2D Pose Estimate&lt;/strong&gt; before the goal&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Global plan exists, local costmap empty&lt;/td&gt;
      &lt;td&gt;No odometry, or scan QoS is best effort while the subscription is reliable&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /odom&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan -v&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot plans through a wall, or refuses a wide door&lt;/td&gt;
      &lt;td&gt;Laser frame yaw is wrong, or the footprint does not match the body&lt;/td&gt;
      &lt;td&gt;URDF laser joint and footprint polygon&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Command asks the base to slide sideways&lt;/td&gt;
      &lt;td&gt;Nonzero &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; on a diff-drive robot&lt;/td&gt;
      &lt;td&gt;Controller must keep \(v_y = 0\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sim moves, Capstone wheels stay still&lt;/td&gt;
      &lt;td&gt;Nav2 never wrote PWM&lt;/td&gt;
      &lt;td&gt;Your bridge is a separate process&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The &lt;a href=&quot;https://docs.nav2.org/jazzy/configuration_and_development/first_time_robot_setup_guide/&quot;&gt;first-time robot setup guide&lt;/a&gt; and its &lt;a href=&quot;https://docs.nav2.org/jazzy/configuration_and_development/first_time_robot_setup_guide/urdf/setup_urdf/&quot;&gt;URDF page&lt;/a&gt; are where a later week attaches your chassis. They are not a second lab tonight.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;h3 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;/h3&gt;

&lt;p&gt;List the five Nav2 blocks from this lesson and write the message type that the controller publishes. State which process, yours or Nav2’s, turns that message into PWM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Behavior-tree navigator, planner, controller, costmaps, recoveries. The type is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt;. The bridge is yours.&lt;/p&gt;

&lt;h3 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;/h3&gt;

&lt;p&gt;You clicked a goal and the simulated robot never moved. You also never clicked &lt;strong&gt;2D Pose Estimate&lt;/strong&gt;. What is incomplete in TF, and which click fixes the tour without editing YAML?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; AMCL cannot publish a trustworthy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; until it has a start. The click is the pose estimate, then the goal again.&lt;/p&gt;

&lt;h3 id=&quot;exercise-3&quot;&gt;Exercise 3&lt;/h3&gt;

&lt;p&gt;A copied parameter file produces &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; of 0.2 while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; is 0.1. Explain why the Capstone differential base cannot execute that Twist, and what you want &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; to be.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; No sideways rollers. The honest command keeps \(v_y\) at zero and uses yaw rate to turn.&lt;/p&gt;

&lt;h3 id=&quot;exercise-4&quot;&gt;Exercise 4&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /scan&lt;/code&gt; prints ranges, but the local costmap stays empty. The echo used a best-effort compatible subscriber and the costmap node was left on the default reliable profile. Describe the mismatch without proposing a new motor driver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Reliability is a subscription property. A reliable subscriber does not connect to a best-effort scan. Point at QoS, not at PWM.&lt;/p&gt;

&lt;h3 id=&quot;exercise-5&quot;&gt;Exercise 5&lt;/h3&gt;

&lt;p&gt;In one paragraph, separate what the TurtleBot3 tour proved from what it did not prove about your Capstone. Mention the bridge explicitly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Proved: install line, initial pose, goal, Twist on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; for a robot Nav2 already supports. Did not prove: your map, your lidar, your footprint, or your H-bridge.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/&quot;&gt;Nav2 getting started for Jazzy&lt;/a&gt; indexes the quickstart and the behavior-tree pages.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/quickstart/quickstart/&quot;&gt;Quickstart&lt;/a&gt; is the TurtleBot3 tour, including &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headless&lt;/code&gt; defaulting to true.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/configuration_and_development/first_time_robot_setup_guide/&quot;&gt;First-time robot setup&lt;/a&gt; is the later path for a robot that is not a TurtleBot3.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Quality-of-Service-Settings.html&quot;&gt;QoS settings&lt;/a&gt; covers reliable versus best effort.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/ros_installation/&quot;&gt;Gazebo Harmonic with ROS&lt;/a&gt; is for when the simulator packages themselves fail to start.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>SLAM overview for beginners</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter11/11_01_slam_overview/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter11/11_01_slam_overview</id>
   <content type="html">&lt;p&gt;Estimated time: about 70 minutes with ROS 2 Jazzy already sourced, or about 50 minutes if you stop at the written interface contract because no lidar is on the desk.&lt;/p&gt;

&lt;p&gt;Drop a blindfolded robot into an unfamiliar building and ask it to draw a floor plan: to know where the walls are, it needs to know where &lt;em&gt;it&lt;/em&gt; is; yet to know where it is without GPS, it needs a map of the walls. That chicken-and-egg paradox—&lt;strong&gt;Simultaneous Localization and Mapping (SLAM)&lt;/strong&gt;—was one of the defining breakthroughs of modern robotics, solved by fusing wheel odometry with LiDAR scan matching and loop closure.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You can draw the three-edge transform chain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; expects, name the topics that must exist before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;online_async_launch.py&lt;/code&gt;, tell a bent hallway caused by a reversed encoder from a hallway that was never scanned, and say why a TF-Luna cannot fill &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info&lt;/code&gt; and you know a transform tree is named frames, not a picture of the room. From the Capstone base, wheel encoders produce odometry, and a sign error on one motor makes a straight command look like a turn. Gazebo is only a mental picture of a simulated laser here.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-occupancy-grids-bayesian-log-odds-updates-and-pose-graph-loop-closure&quot;&gt;Key ideas: Occupancy Grids, Bayesian Log-Odds Updates, and Pose-Graph Loop Closure&lt;/h2&gt;

&lt;p&gt;Modern 2D LiDAR SLAM (such as ROS 2 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt;) stands on three mathematical pillars:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Occupancy Grid Mapping (Log-Odds Bayes Filter):&lt;/strong&gt; The world is discretized into cells \(m_i \in \{0, 1\}\) (free vs. occupied). Storing each cell’s belief as &lt;strong&gt;log-odds&lt;/strong&gt; \(l_i = \log\frac{p(m_i = 1)}{1 - p(m_i = 1)}\) turns recursive Bayesian updates into fast addition: \(l_i^{(t)} = l_i^{(t-1)} + l_{\text{obs}} - l_0\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scan Matching (Front-End Odometry Correction):&lt;/strong&gt; Wheel odometry drifts continuously. Matching each incoming LiDAR scan against the local map (via ICP or correlative scan matching) computes the small \(SE(2)\) correction \((\Delta x, \Delta y, \Delta\theta)\) that snaps walls into sharp alignment.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pose-Graph Optimization &amp;amp; Loop Closure (Back-End):&lt;/strong&gt; When the robot revisits a corridor it mapped minutes earlier, recognizing the overlap adds a &lt;em&gt;loop-closure constraint&lt;/em&gt; between non-adjacent poses and solves a sparse nonlinear least-squares graph optimization to erase accumulated global drift.
    &lt;h2 id=&quot;why-this-lesson-sits-on-the-path&quot;&gt;Why this lesson sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The course path runs from the Capstone differential-drive base, through ROS 2 Jazzy, through Gazebo, and only then into autonomy. A map is the first autonomy artifact that is not a motor command. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt; estimates where the robot is and what the walls are, at the same time, once a planar lidar exists. Gazebo can publish a pretend scan. The Capstone robot cannot. Until a real &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt; and honest odometry exist, launching the mapper only reports missing interfaces. Later, Nav2 will consume this map. It will not draw it for you.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;SLAM here means localization and mapping together in a plane. The lidar measures ranges along many bearings. Wheel odometry says how the base thinks it moved between scans. The mapper stitches scans into a grid and publishes a correction so encoder error does not slowly twist the map.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch11_slam.png&quot; alt=&quot;Planar SLAM: a lidar sweep builds a map while the map-to-odom transform corrects wheel drift&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; keeps three frames apart. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; may jump when the mapper closes a loop. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt;, integrated from the wheels, may drift and must not jump, or a controller following it will lurch. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; is the body. For this course, SLAM publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt;, the robot publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; from wheel odometry, and the URDF publishes a static &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; to the laser frame. The lidar driver does not invent that mount.&lt;/p&gt;

&lt;p&gt;Composed, the pose of the body in the map is&lt;/p&gt;

\[T_{\mathrm{map}\rightarrow\mathrm{base}} = T_{\mathrm{map}\rightarrow\mathrm{odom}} \, T_{\mathrm{odom}\rightarrow\mathrm{base}}.\]

&lt;p&gt;If the middle transform is a lie, the left transform cannot save the map. Consider a concrete sign error. Track width \(b = 0.20\) m. Both wheels truly rolled one meter forward, so the robot went straight. The right encoder is wired with the wrong sign, so the odometry node sees \(\Delta s_l = 1.0\) m and \(\Delta s_r = -1.0\) m. With the usual planar model, forward travel and yaw are&lt;/p&gt;

\[\Delta s = \frac{\Delta s_r + \Delta s_l}{2}, \qquad \Delta \theta = \frac{\Delta s_r - \Delta s_l}{b}.\]

&lt;p&gt;That substitution gives \(\Delta s = 0\) and \(\Delta \theta = -10\) rad. The robot drove down the hallway. The odometry says it spun in place. Scan matching then drags the walls along that phantom spin, and the hallway in the map bends. Fix the encoder sign, or the left/right assignment, before you touch SLAM parameters. A scale error looks different: both wheels too large by the same factor make a straight hallway that is too long, not a banana.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt; carries &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angle_min&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angle_max&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angle_increment&lt;/code&gt;, the range limits, a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ranges&lt;/code&gt; array, and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; that is already in TF. A 360-degree lidar at half-degree steps has on the order of 720 ranges. A TF-Luna has one range along one ray. It can warn a bumper. It cannot feed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An MPU6050 can later help a filter hold yaw between scans. It still does not paint walls. Lidar drivers usually publish best effort, because a late scan is worse than a dropped one. A subscriber that demands reliable delivery sits silent beside that publisher. Use the Jazzy QoS page when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo&lt;/code&gt; shows samples and the mapper still says it sees nothing.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Install the Jazzy binary, source the distro, and look at the launch line that this course treats as the shape of the command:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-slam-toolbox
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 launch slam_toolbox online_async_launch.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That launch starts the online async mapper with the package default parameters. It will not build a map without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; and TF. A missing scan topic, a missing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; transform, or an unknown laser frame means the interface is absent. Stop the node. Do not invent a fake scan.&lt;/p&gt;

&lt;p&gt;Before you launch it on any robot, including a future Capstone bringup, the dry checks are:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 topic info /scan &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
ros2 topic info /odom &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
ros2 run tf2_ros tf2_echo odom base_link
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; should be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt; with a publisher count of at least one. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; should be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/msg/Odometry&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;child_frame_id&lt;/code&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;. Pushing the robot forward should change translation, and rotating it should change yaw. Only then does the launch have a chance. Healthy SLAM adds &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run tf2_ros tf2_echo map odom&lt;/code&gt;). That third echo is an outcome, not a transform you publish yourself.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-updating-an-occupancy-grid-cell-in-log-odds-space&quot;&gt;Explanation: Hand-Updating an Occupancy Grid Cell in Log-Odds Space&lt;/h3&gt;

&lt;p&gt;Suppose a map cell starts completely unknown with prior probability \(p_0 = 0.50\), so its initial log-odds is \(l_0 = \log(0.5 / 0.5) = 0.0\). Your LiDAR sensor model says:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;A laser &lt;em&gt;hit&lt;/em&gt; inside the cell implies \(p_{\text{hit}} = 0.80 \implies l_{\text{hit}} = \ln(0.80 / 0.20) = \ln(4) = +1.386\).&lt;/li&gt;
  &lt;li&gt;A laser ray &lt;em&gt;passing through&lt;/em&gt; the cell implies \(p_{\text{free}} = 0.30 \implies l_{\text{free}} = \ln(0.30 / 0.70) = -0.847\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trace the cell’s occupancy probability over three consecutive scans:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Scan 1 — LiDAR hit:&lt;/strong&gt; \(l_1 = 0.0 + 1.386 = +1.386 \implies p_1 = \frac{1}{1 + e^{-1.386}} = 0.800\) (80% occupied).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scan 2 — Second LiDAR hit:&lt;/strong&gt; \(l_2 = 1.386 + 1.386 = +2.772 \implies p_2 = \frac{1}{1 + e^{-2.772}} = \frac{1}{1 + 0.0625} = 0.941\) (94.1% occupied — wall confirmed!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scan 3 — Person walks away, laser passes through (free):&lt;/strong&gt; \(l_3 = 2.772 - 0.847 = +1.925 \implies p_3 = \frac{1}{1 + e^{-1.925}} = 0.873\) (belief drops smoothly rather than flipping wildly on one noisy ray).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That single addition rule is how SLAM builds crisp, noise-resistant maps in real time.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;This lab is an interface contract, not a mapping contest. Copy the following requirement list into your notes and mark each line present, absent, or impossible with the sensor you actually own.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; exists, type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; is the laser frame, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ranges&lt;/code&gt; covers a wide planar sweep rather than one number.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; exists, type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/msg/Odometry&lt;/code&gt;, and a straight push moves &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; without inventing a large yaw.&lt;/li&gt;
  &lt;li&gt;TF contains &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; from the robot and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; to the laser frame as a static mount. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; is reserved for SLAM.&lt;/li&gt;
  &lt;li&gt;If the only ranger on the robot is a TF-Luna, write the sentence “this sensor cannot feed slam_toolbox” and stop. Do not launch the mapper and call the empty window a map.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run the dry plan even when you expect failure:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 topic info /scan &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
ros2 topic info /odom &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Expected output when nothing is publishing, which is the honest result on a machine with no lidar driver:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Unknown topic &apos;/scan&apos;
Unknown topic &apos;/odom&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Record that text. Both publishers are absent, so the launch is not a failed demo. If a driver or a bag is up, paste the block that names the message type and the publisher count next to the matching contract line.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Hallway bends like a banana&lt;/td&gt;
      &lt;td&gt;One encoder sign is flipped, or left and right are swapped&lt;/td&gt;
      &lt;td&gt;Drive straight and watch yaw in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hallway is straight but the wrong length&lt;/td&gt;
      &lt;td&gt;Wheel radius or track width&lt;/td&gt;
      &lt;td&gt;Compare one floor meter with odom &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Launch prints TF errors and the map stays empty&lt;/td&gt;
      &lt;td&gt;No &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;, no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, or no laser frame&lt;/td&gt;
      &lt;td&gt;The three commands above&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Echo shows scans but SLAM stays quiet&lt;/td&gt;
      &lt;td&gt;Reliable subscriber, best-effort publisher&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /scan -v&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;The “lidar” message is one distance&lt;/td&gt;
      &lt;td&gt;TF-Luna or any single-point ToF&lt;/td&gt;
      &lt;td&gt;Do not connect it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slam_toolbox&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;buying-parts-in-vietnam&quot;&gt;Buying parts in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy a scanner only when you are ready to map. Student-scale planar lidars in the LD19, LD06, and YDLIDAR X2 class have recently sat around 1.5 to 3.5 million VND. Search Shopee with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lidar LD19 robot&lt;/code&gt;, read the current listing, and prefer a documented serial protocol. That is the first buy if you buy anything.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-dtof-lidar-rplidar-s2m1-r2e-30m-360-laser-range-scanner&quot;&gt;RPLIDAR S2 on Hshop&lt;/a&gt; is the expensive step up: 360 degrees, an Ethernet-style link, longer range. Recheck the live price. It is not the first student buy.&lt;/p&gt;

&lt;p&gt;A Raspberry Pi 5 class board is the computer that can run the mapper on the robot. &lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Hshop’s Pi 5 listing&lt;/a&gt; has started near 2.4 million VND for low-RAM boards. Recheck. The Pi does not replace the lidar. Open the &lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-dfrobot-tf-luna-tof-micro-single-point-ranging-lidar&quot;&gt;TF-Luna page&lt;/a&gt; and read “single-point” in the name: useful as a bumper, wrong for this lesson. The &lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;MPU6050&lt;/a&gt; is optional for a later filter, not a substitute for the laser, and not required for the contract lab.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;h3 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;/h3&gt;

&lt;p&gt;Draw the TF tree for a robot that is mapping a straight corridor. Label the publisher of each edge: SLAM, wheel odometry, or static URDF.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Three edges are enough: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; to the laser frame. If you add a fourth edge, say why it is static.&lt;/p&gt;

&lt;h3 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;/h3&gt;

&lt;p&gt;Using the sign-error numbers in the concepts section, recompute \(\Delta s\) and \(\Delta \theta\) if the right encoder is correct and the left encoder is the one that flipped sign. Say whether the phantom yaw changes direction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Keep \(b = 0.20\) m. Only \(\Delta s_l\) changes sign relative to the worked numbers. You do not need a robot.&lt;/p&gt;

&lt;h3 id=&quot;exercise-3&quot;&gt;Exercise 3&lt;/h3&gt;

&lt;p&gt;Write the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; contract you would hand to a teammate whose only ranger is a TF-Luna. Include message type, what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ranges&lt;/code&gt; must contain, and the explicit refusal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; One short paragraph. Name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt;. State that one float is not an array of bearings.&lt;/p&gt;

&lt;h3 id=&quot;exercise-4&quot;&gt;Exercise 4&lt;/h3&gt;

&lt;p&gt;A straight one-meter push changes odometry &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; by about one meter and changes yaw by less than a degree. The map hallway is still curved. Where do you look next, and where do you not look?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Honest odometry moves suspicion onto the laser mount yaw and onto TF. Do not start by buying an IMU.&lt;/p&gt;

&lt;h3 id=&quot;exercise-5&quot;&gt;Exercise 5&lt;/h3&gt;

&lt;p&gt;Write the exact &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info&lt;/code&gt; commands you will run before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;online_async_launch.py&lt;/code&gt;, and state what publisher count 0 means for the launch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Guidance.&lt;/strong&gt; Two commands, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;, with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-v&lt;/code&gt;. Publisher count 0 means the launch has nothing to fuse, so you stop.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/SteveMacenski/slam_toolbox&quot;&gt;slam_toolbox&lt;/a&gt; documents the online async launch and its scan plus odometry assumptions.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; defines &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Quality-of-Service-Settings.html&quot;&gt;Quality of service in Jazzy&lt;/a&gt; explains a reliable subscriber missing a best-effort scan.&lt;/li&gt;
  &lt;li&gt;The &lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-dfrobot-tf-luna-tof-micro-single-point-ranging-lidar&quot;&gt;TF-Luna listing&lt;/a&gt; shows the single-point wording on the product page.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Sim-to-real checklist</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter10/10_05_sim_to_real/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter10/10_05_sim_to_real</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~80 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Every roboticist remembers their first sim-to-real shock: the robot navigates a simulated apartment cleanly ten times in a row, then immediately bumps a doorframe or drifts off a rug five seconds after touching the real floor. The &lt;strong&gt;sim-to-real gap&lt;/strong&gt; is not mysterious bad luck—it is a countable list of unmodeled physics (latency, tire slip, battery sag, sensor noise) that you can measure, bound, and close systematically.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will compare one Capstone command in Gazebo Harmonic with the same command on the bench, using the same units, the same REP-105 frame names, and the same wheel sign. You will keep a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; in simulation and refuse &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; on the real robot, where the clock is the wall clock. You will measure how old a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; stamp is, connect that age to the 300 ms watchdog, and add the extra delay Wi-Fi or MQTT can insert. You will explain why an E-stop has to open the motor power path in hardware, not only publish a topic. You will fill three checklist rows with pass or fail and attach evidence: a command transcript or a measurement, not a feeling.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 10-02’s spawned model rolls forward at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; 0.2 in the Harmonic world. Lesson 10-04’s bridge turns that same Twist into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt; and then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; when the publisher stops. Lesson 09-05’s frame names, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; at minimum, are what both sides must use. The robot pack, the common ground, and the wheels-up habit from Chapter 07 still apply the moment a real driver is connected. You can do the writing part of the lab from logs you already captured.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-reality-gap-taxonomy-system-identification-and-domain-randomization&quot;&gt;Key ideas: Reality-Gap Taxonomy, System Identification, and Domain Randomization&lt;/h2&gt;

&lt;p&gt;Closing the sim-to-real gap builds on three pillars spanning classical control and robot learning:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Sources of the Reality Gap:&lt;/strong&gt; Physical discrepancies fall into four buckets: (1) &lt;em&gt;kinematic/inertial&lt;/em&gt; (wrong \(b, r, m, I\)), (2) &lt;em&gt;actuator dynamics&lt;/em&gt; (motor deadband, backlash, battery voltage sag), (3) &lt;em&gt;contact physics&lt;/em&gt; (floor friction variation, caster shimmy), and (4) &lt;em&gt;timing/sensing&lt;/em&gt; (USB serial latency, LiDAR multipath/dropouts).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;System Identification (SysID):&lt;/strong&gt; Measuring step responses, friction breakaway PWM, and round-trip latency on the physical robot lets you calibrate simulator parameters to match reality rather than guessing defaults.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Robust Control &amp;amp; Domain Randomization:&lt;/strong&gt; Rather than over-tuning gains to one idealized simulator floor, testing across perturbed friction (\(\mu \in [0.5, 1.0]\)), added latency (\(+20\text{–}50\ \text{ms}\)), and injected odometry noise ensures controllers survive real-world variability.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Simulation let you see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; without burning a motor. The floor is where that topic has to mean the same thing. Nav2 has no separate mode that fixes units, frame names, or a backwards wheel. A policy trained later in Harmonic transfers only as far as metres, frames, sign, and a stop still hold when the network stalls. Walk this gate before autonomy on the real base, and again when you change a wheel, a sensor mount, or the bridge scale. A poster does not catch a second odometry publisher. Evidence does.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch10_sim2real.png&quot; alt=&quot;Same command crossing from the Harmonic world to the Capstone on the floor&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;Distances in this course are metres. Speeds on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; are metres per second and radians per second. The URDF, the diff-drive plugin, and the bridge’s track \(L = 0.16\) are all in metres. The wire integer is not a third unit system. It exists only after lesson 10-04’s scale, where 0.5 m/s of tread is integer 100. A world left in millimetres, or a bridge that treats &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; as “about 20,” makes the same published number mean two different motions.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; names &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, and the sensor frames. The plugin, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, and any odometry from the bridge have to use those strings identically. A URDF link &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; with a scan &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_link&lt;/code&gt; leaves TF with no edge, so the cloud sits at the origin and the costmap looks like a tuning problem. REP-103 rides along: \(+x\) forward, \(+y\) left, \(+z\) up, positive yaw to the left.&lt;/p&gt;

&lt;p&gt;In the Harmonic world, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x = 0.2&lt;/code&gt; must translate the model along its \(+x\), both wheels rolling forward. On the bench the same Twist must roll both real wheels forward. At the course scale the payload is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt;. A motor with swapped leads rolls backward while the integer is positive. Fix the leads or the sign in one place, then repeat the Twist on both sides. Negating \(\omega\) only inside Nav2 hides the bug until the next teleop node.&lt;/p&gt;

&lt;p&gt;Time is the contract people copy blindly from a sim launch file. In Harmonic there is one clock, bridged one way:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nodes that share that time set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; true. Confirm a single publisher with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /clock&lt;/code&gt;. On the real robot you do not bridge a clock and you do not set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; true. A node left in sim time waits for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, its timers sit still, and the motors keep the last command. The cause is a copied launch argument.&lt;/p&gt;

&lt;p&gt;Latency is the part simulation will not volunteer. Stamp age is&lt;/p&gt;

\[\mathrm{age} = t_{\mathrm{now}} - t_{\mathrm{stamp}}\]

&lt;p&gt;with both times from the same clock. On a USB dry run the age is a few milliseconds. Chapter 08’s Wi-Fi and MQTT path can add tens or hundreds of milliseconds, and a stalled router can add seconds. The bridge already turns a silent Twist into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; after 0.3 s, and the firmware does it again if frames stop. An age past 0.3 s is already older than that timeout. Do not raise the watchdog to two seconds so the robot keeps sprinting after the joystick dies.&lt;/p&gt;

&lt;p&gt;The E-stop is not that timeout. A watchdog dies with the process, which is the failure you are covering. The emergency stop is a switch in the motor supply that cuts driver power when you hit it. A ROS topic or a GPIO is a polite extra. The switch still works when the ESP32 has locked up. Mount it where a hand reaches it without crossing the wheels.&lt;/p&gt;

&lt;p&gt;Power wiring is part of the same gate. Common ground between the logic and the motor driver, a fuse on the battery feed, and the first motion test with the wheels off the table. USB never feeds the motors. Those rules are older than ROS; sim-to-real is where people drop them because the simulator has no pack to catch fire.&lt;/p&gt;

&lt;p&gt;The course rangefinder, the &lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-dfrobot-tf-luna-tof-micro-single-point-ranging-lidar&quot;&gt;DFRobot TF-Luna&lt;/a&gt;, returns one range along its axis. It is not a 360° SLAM lidar. A Gazebo GPU lidar with a full ring is a different instrument. You may simulate a ring to learn Nav2, and you must not expect the TF-Luna to reproduce that ring. An IMU such as the &lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;GY-521&lt;/a&gt; needs the same rule: one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt;, the one in the URDF.&lt;/p&gt;

&lt;p&gt;Harmonic’s default contact is not your carpet, and the plugin is not the PID from lesson 08-03. Copying a simulated wheel-PID onto the floor oscillates or crawls. Retune on the real mechanism, wheels up first, then on the floor. The matching software failure is two odometry publishers, both broadcasting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /odom&lt;/code&gt; can look fine while TF flips. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /tf -v&lt;/code&gt; shows both broadcasters.&lt;/p&gt;

&lt;p&gt;Put the comparisons in one place and then test rows, not the whole table in the abstract.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt; &lt;/th&gt;
      &lt;th&gt;Simulation (Harmonic)&lt;/th&gt;
      &lt;th&gt;Real Capstone&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Linear unit on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;m/s&lt;/td&gt;
      &lt;td&gt;m/s, then one scale to −100…100 on the wire&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Angular unit&lt;/td&gt;
      &lt;td&gt;rad/s about \(+z\)&lt;/td&gt;
      &lt;td&gt;the same &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;, same sign&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Frames&lt;/td&gt;
      &lt;td&gt;REP-105 names from the URDF&lt;/td&gt;
      &lt;td&gt;the same strings in the bridge and any scan or IMU&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheel sign&lt;/td&gt;
      &lt;td&gt;\(+x\) moves the model forward&lt;/td&gt;
      &lt;td&gt;the same Twist rolls both real wheels forward&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Clock&lt;/td&gt;
      &lt;td&gt;one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, Gazebo to ROS, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; true&lt;/td&gt;
      &lt;td&gt;wall time, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; false, no fake &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Command age&lt;/td&gt;
      &lt;td&gt;usually a few milliseconds&lt;/td&gt;
      &lt;td&gt;measure it; MQTT or Wi-Fi from Chapter 08 adds more&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Stop when the link dies&lt;/td&gt;
      &lt;td&gt;pause the world, or publish zeros&lt;/td&gt;
      &lt;td&gt;firmware watchdog at 300 ms, plus a hardware E-stop&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;First motion&lt;/td&gt;
      &lt;td&gt;spawn, then a short Twist&lt;/td&gt;
      &lt;td&gt;wheels up, fused pack, common ground, then the same Twist&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ground contact&lt;/td&gt;
      &lt;td&gt;collision meshes on the plane&lt;/td&gt;
      &lt;td&gt;carpet and casters, which are not the sim friction&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Odometry&lt;/td&gt;
      &lt;td&gt;one publisher of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;one publisher; a second one fights TF&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Acceptance for the straight command, written before you touch the hardware.&lt;/p&gt;

&lt;p&gt;In simulation, with the lesson 10-02 stack up:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub &lt;span class=&quot;nt&quot;&gt;--times&lt;/span&gt; 20 /cmd_vel geometry_msgs/msg/Twist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;{linear: {x: 0.2, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Pass: the model in the Gazebo window translates forward and both wheels roll. Fail: it yaws, it drives backward, or only RViz moves.&lt;/p&gt;

&lt;p&gt;On the bench, wheels up, bridge on the real port, same Twist for a short burst and then silence. Pass: the parser or the bridge log shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt;, both wheels turn forward for less than a second, and they stop when the publisher stops, with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; on the wire. Fail: one wheel reverses, they keep turning after silence, or the integers are not 40.&lt;/p&gt;

&lt;p&gt;A third row you can close from the computer alone is the clock. In sim:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic info /clock
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Pass: one publisher, the bridge, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; is true only on the nodes that are supposed to follow Gazebo. On the robot launch, pass is the absence of that parameter set to true. Search the launch files for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; before you call the row done. A copied sim launch fails this row even when the wheels are not connected.&lt;/p&gt;

&lt;p&gt;Stamp age, if you log it in the bridge callback, uses the formula above. A USB loopback at a few milliseconds passes a “under 50 ms” note. A number near 0.3 s is a warning, not a pass, even if the wheels still moved.&lt;/p&gt;

&lt;h3 id=&quot;explanation-quantifying-the-sim-to-real-latency--slip-budget&quot;&gt;Explanation: Quantifying the Sim-to-Real Latency &amp;amp; Slip Budget&lt;/h3&gt;

&lt;p&gt;Suppose a controller works smoothly in Gazebo (zero communication latency, zero tire slip) at \(v = 0.40\ \text{m/s}\), stopping \(0.10\ \text{m}\) before a wall. Now move it to real hardware where you measure three reality-gap terms:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Unmodeled end-to-end latency (\(\tau_{\text{real}} = 65\ \text{ms}\)):&lt;/strong&gt; Camera/LiDAR exposure + USB bridge + MCU tick delay adds \(\Delta d_{\text{lat}} = v \cdot \tau_{\text{real}} = 0.40 \times 0.065 = 0.026\ \text{m}\) (2.6 cm) of extra travel before braking even begins.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lower real floor friction (tile \(\mu_{\text{real}} = 0.45\) vs. sim \(\mu_{\text{sim}} = 0.90\)):&lt;/strong&gt; Maximum braking deceleration drops in half (say from \(2.0\ \text{m/s}^2\) to \(1.0\ \text{m/s}^2\)). Braking distance \(v^2 / (2a)\) doubles from:
\(d_{\text{brake,sim}} = \frac{0.40^2}{2(2.0)} = 0.040\ \text{m} \quad \longrightarrow \quad d_{\text{brake,real}} = \frac{0.40^2}{2(1.0)} = 0.080\ \text{m}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Total real stopping overshoot:&lt;/strong&gt; \(0.026 + (0.080 - 0.040) = 0.066\ \text{m}\) — eating 66% of your 10 cm safety buffer! Adding a 10 cm inflation margin or cutting cruising speed to \(0.25\ \text{m/s}\) immediately restores full safety margin on hardware.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Open &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; and copy the table. You will not tick every row tonight. Choose three: wheel sign, clock, and one of command-age, E-stop, or single odometry publisher.&lt;/li&gt;
  &lt;li&gt;Wheel sign: run the 0.2 m/s burst in Harmonic and write pass or fail with what you saw in the world. On the bench, wheels up, run the same burst through the bridge. Record the parser line and which way each wheel turned. Stop as soon as the direction is obvious.&lt;/li&gt;
  &lt;li&gt;Clock: record &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /clock&lt;/code&gt; from the sim session, and record the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; lines you actually launch on the robot. A robot command with that parameter true is a fail, even if you did not plug the motors in.&lt;/li&gt;
  &lt;li&gt;Third row, your choice. For age, print one measured \(t_{\mathrm{now}} - t_{\mathrm{stamp}}\). For the E-stop, either find the switch in the motor supply or write fail and the sentence “stop is only a topic.” For odometry, run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /tf -v&lt;/code&gt; and name every node that publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Write one action for any failed row. “Retune later” is not an action. “Swap the left motor leads, then repeat the 0.2 m/s burst” is an action.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Expected&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Three rows, each with pass or fail and a piece of evidence. A fully passing wheel-sign pair looks like this:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sim: linear.x 0.2, model moved forward in Gazebo, both wheels rolled
bench: parser OK V,40,40, both wheels forward, then OK V,0,0 within 1 s of silence
clock: sim has one /clock publisher; robot launch does not set use_sim_time true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Your third row may legitimately fail. A fail with evidence passes the lab. A pass with no transcript does not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Sim drives well, floor oscillates or crawls&lt;/td&gt;
      &lt;td&gt;Simulated friction or plugin gains were copied onto carpet. Retune lesson 08-03 on the real wheels&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cloud or ranger sits at the origin in RViz&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_link&lt;/code&gt; in the message and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; in the URDF, or the reverse&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot jumps in RViz, echo of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; looks smooth&lt;/td&gt;
      &lt;td&gt;Two publishers on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /tf -v&lt;/code&gt; shows both&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nodes start, timers never fire, wheels keep the last PWM&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; true on the robot, and nothing publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Joystick released, robot runs for seconds&lt;/td&gt;
      &lt;td&gt;Watchdog longer than 300 ms, or Wi-Fi retained a stale command and nothing zeros it&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;E-stop “works” in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo&lt;/code&gt; and does nothing when the MCU locks&lt;/td&gt;
      &lt;td&gt;The stop is only a subscription. The supply is still connected&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;You already own the robot this lesson checks. No new computer is required. An emergency switch is the optional part if the motor supply cannot be opened by hand. Search Shopee or Lazada for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;công tắc khẩn 22mm&lt;/code&gt;. The 2026 band is about 30.000–80.000 VND; recheck before paying, and buy a normally closed contact so a hit opens the supply. Look on &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; if you still need a fuse holder. The TF-Luna link above documents a single-point sensor, not a 360° scanner to buy.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Simulation nodes use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; true. You copy the launch to the robot and forget to change it. What stops updating, and which topic is missing? Guidance: timers and TF waits stall because the robot publishes no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, while the last motor command can remain.&lt;/li&gt;
  &lt;li&gt;The same Twist drives forward in Gazebo and spins the left wheel backward on the bench. Which table row failed, and what do you change first? Guidance: the wheel-sign row; swap that motor’s leads or the sign in one bridge place, then repeat the 0.2 m/s burst on both sides.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /odom&lt;/code&gt; looks smooth, yet the model twitches in RViz. Which command shows the second broadcaster that the echo hides? Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /tf -v&lt;/code&gt;, looking for two nodes on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; edge.&lt;/li&gt;
  &lt;li&gt;A Wi-Fi path delivers a Twist whose stamp is 0.45 s behind &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_now&lt;/code&gt;. Write the age and say whether the watchdog should still be tracking that command. Guidance: age is \(t_{\mathrm{now}} - t_{\mathrm{stamp}} = 0.45\) s, which is already past the 0.3 s limit, so the bridge should be sending zeros.&lt;/li&gt;
  &lt;li&gt;Why is a topic named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/estop&lt;/code&gt; not an emergency stop, even if the bridge subscribes to it? Guidance: the topic vanishes when the node or the MCU wedges; a hardware switch has to open the motor supply by itself.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;try-it-if-you-have-a-gpu-berkeley-humanoid-lite--zero-shot-sim-to-real&quot;&gt;Try it if you have a GPU: Berkeley Humanoid Lite — zero-shot sim-to-real&lt;/h2&gt;

&lt;p&gt;The same &lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt; platform (Hybrid Robotics, UC Berkeley) trains a locomotion controller with reinforcement learning and transfers it to the physical robot with no extra state estimator. The &lt;a href=&quot;https://www.roboticsproceedings.org/rss21/p062.pdf&quot;&gt;RSS 2025 paper&lt;/a&gt; (&lt;a href=&quot;https://arxiv.org/abs/2504.17249&quot;&gt;arXiv:2504.17249&lt;/a&gt;) reports an Isaac Gym policy; the &lt;a href=&quot;https://github.com/HybridRobotics/berkeley-humanoid-lite&quot;&gt;current codebase&lt;/a&gt; trains in Isaac Lab, checks the policy in MuJoCo, then deploys on the Intel N95 computer over four CAN 2.0 buses. Teleoperation uses SteamVR.&lt;/p&gt;

&lt;p&gt;If you have an NVIDIA GPU, follow the &lt;a href=&quot;https://berkeley-humanoid-lite.gitbook.io/docs/getting-started-with-software&quot;&gt;Training Environment guide&lt;/a&gt; far enough to bring up the locomotion environment in Isaac Lab and read the observation and action spaces. You do not need to train to convergence. Without a GPU, read the locomotion section of the paper and map it onto this lesson’s sim-to-real checklist: the model, the latency, and what must not be copied from simulation onto the hardware. The cycloidal-gearbox check is in lesson 06-03.&lt;/p&gt;

&lt;h2 id=&quot;try-it-with-a-library-simready-central--openusd-assets-that-already-carry-physics&quot;&gt;Try it with a library: SimReady Central — OpenUSD assets that already carry physics&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://huggingface.co/spaces/nvidia/simready-central&quot;&gt;SimReady Central&lt;/a&gt; (NVIDIA, currently Alpha; announcement 2026-10-21) is a library of more than 2,000 OpenUSD assets on Hugging Face for training and evaluation scenes. “SimReady” means the package already has collision geometry, mass, friction, joints with limits and drives, semantic labels, and materials — not just a pretty mesh. Specifications, profiles, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;simready-validate&lt;/code&gt; live in &lt;a href=&quot;https://github.com/NVIDIA/simready-foundation&quot;&gt;SimReady Foundation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Find assets through the Hugging Face Space, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;simready-search&lt;/code&gt; Python package, or the Isaac Sim Asset Search extension (that extension lists CC-BY-4.0 packages only). Many assets are CC-BY-4.0, but not all of them (some FANUC packages are Apache-2.0; some Universal Robots packages need a careful license read): check each asset. Arms and grippers from FANUC, Franka, Robotiq, Universal Robots, and other OEMs are in the library.&lt;/p&gt;

&lt;p&gt;If your machine meets the official &lt;a href=&quot;https://docs.isaacsim.omniverse.nvidia.com/latest/installation/requirements.html&quot;&gt;Isaac Sim floor&lt;/a&gt; (minimum RTX 4080, 16 GB VRAM, 32 GB RAM; not an A100 or H100), walk through the &lt;a href=&quot;https://huggingface.co/blog/nvidia/simready-assets-robot-pick-and-place&quot;&gt;pick-and-place example&lt;/a&gt; (UR17G + Robotiq 2F-140). On a weaker machine, inspect the USD in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usdview&lt;/code&gt; or Blender, or follow the &lt;a href=&quot;/contents/en/chapter12/12_08_home_simulation_lab/&quot;&gt;home simulation lab&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; is the axis and rotation convention the wheel-sign row is checking.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; is the frame-name list both the simulator and the bridge have to share.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/gazebosim/ros_gz/blob/ros2/ros_gz_bridge/README.md&quot;&gt;ros_gz_bridge README&lt;/a&gt; is the one-way &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; form you must not copy onto the real robot.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Using-ros2-param.html&quot;&gt;Jazzy parameters&lt;/a&gt; shows where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; is set, which is the line to delete on the robot launch.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Bridging Capstone A firmware to ROS 2</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter10/10_04_bridge_capstone/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter10/10_04_bridge_capstone</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~90 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is the moment your two worlds—the physical Capstone A rover you wired in Chapter 07 and the ROS 2 ecosystem you built in Chapters 09–10—finally snap together. A clean bridge node translates standard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; commands into your MCU’s serial protocol and converts returning encoder ticks into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; and TF2, making your physical robot look identical to your Gazebo twin.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will run an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt; node that subscribes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, converts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; into left and right tread speeds with track \(L = 0.16\) m, and maps those speeds onto the Chapter 08 integers. You will document the scale in one place, 0.5 m/s of tread to the integer 100, and you will show that a straight 0.2 m/s command becomes the payload &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt;. You will write that payload as the length-delimited frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STX 0xAA&lt;/code&gt;, length, ASCII payload, XOR, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0A&lt;/code&gt;, and you will force &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; onto the wire when no Twist has arrived for 0.3 s. You will prove the node with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; PTY pair and a tiny parser, so the lab does not need a robot. You will state what still stops the motors if the bridge process itself is killed, and you will keep the wheels off the ground if a real driver is connected.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 08-01’s frame and 300 ms firmware timeout, lesson 08-04’s inverse kinematics, and a Jazzy shell with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt;. Python package &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyserial&lt;/code&gt; comes from apt as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3-serial&lt;/code&gt;, so it stays on the same interpreter as ROS. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; provides the fake serial port. You do not need Gazebo running, and you should not have the micro-ROS serial agent holding a port. The real Capstone is optional and comes last, wheels up.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-hardware-abstraction-layers-sign-convention-alignment-and-odometry-covariance&quot;&gt;Key ideas: Hardware Abstraction Layers, Sign-Convention Alignment, and Odometry Covariance&lt;/h2&gt;

&lt;p&gt;Bridging real firmware to ROS 2 relies on three core integration principles:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Hardware Abstraction Layer (HAL):&lt;/strong&gt; By exposing the exact same topic interface (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf&lt;/code&gt;) as Gazebo’s diff-drive plugin, every higher-level ROS 2 package (teleop, SLAM Toolbox, Nav2) runs unmodified on both simulation and real hardware.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;End-to-End Sign &amp;amp; Unit Alignment (REP-103):&lt;/strong&gt; A single minus-sign flip between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel.angular.z&lt;/code&gt; (\(+z\) = CCW left turn) and the serial left/right motor mapping will make autonomous navigation spin endlessly away from its goal. Verifying forward, spin-left, and encoder tick signs in lockstep eliminates the #1 integration bug.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Odometry Pose &amp;amp; Covariance Propagation:&lt;/strong&gt; Publishing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt; requires both the integrated pose \((x, y, \theta)\) and a realistic diagonal covariance matrix (larger uncertainty in yaw \(\sigma_\theta^2\) than in forward distance \(\sigma_x^2\)) so downstream EKF or SLAM fuses wheel odometry appropriately.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This node is the first time the ROS graph and the Capstone firmware share a command. Simulation already listens to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;. Nav2, and a learned policy later, should publish that same type. Neither knows what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xAA&lt;/code&gt; means, and the current ESP32 firmware does not know what a Twist is. The bridge is the only place where metres per second become the −100…100 integers. micro-ROS is allowed only after this file works: same topic names, same scale, same watchdog, with a parser log as evidence.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch10_serial_bridge.png&quot; alt=&quot;Twist in, Chapter 08 frame out, telemetry back&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; carries a linear vector and an angular vector. A differential-drive base honours two of those six numbers. Forward speed \(v\) is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt;, in metres per second. Yaw rate \(\omega\) is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;, in radians per second, positive for a left turn under REP-103. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; would be a sideways slide. The mechanism cannot do that, so the callback ignores &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; instead of secretly mixing it into the wheels.&lt;/p&gt;

&lt;p&gt;The inverse map from lesson 08-04, with track \(L\) in metres:&lt;/p&gt;

\[v_R = v + \omega \frac{L}{2}, \qquad v_L = v - \omega \frac{L}{2}\]

&lt;p&gt;The course base uses \(L = 0.16\), so \(L/2 = 0.08\). \(v_L\) and \(v_R\) are tread speeds in metres per second, positive forward. They are not PWM, and they are not the integers on the wire.&lt;/p&gt;

&lt;p&gt;The wire is still the Chapter 08 integer, −100…100. One scale constant joins the two worlds. This lesson defines full scale as 0.5 m/s of tread:&lt;/p&gt;

\[n = \mathrm{clip}\left(\mathrm{round}\left(v_{\mathrm{wheel}} \cdot \frac{100}{0.5}\right), -100, 100\right)\]

&lt;p&gt;So 0.5 m/s becomes 100, and 0.2 m/s becomes 40. The constant 0.5 is a choice written at the top of the file. If the Chapter 07 spin test showed a different tread speed at integer 100, change the constant and the comment together, on both sides. Casting metres to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; before scaling is a different bug: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int(0.2)&lt;/code&gt; is 0, and the robot sits still while the topic looks correct.&lt;/p&gt;

&lt;p&gt;The payload is ASCII, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,{left},{right}&lt;/code&gt;, left wheel first. The frame around it is unchanged: byte &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xAA&lt;/code&gt;, one length byte equal to the payload length, the payload, one XOR of every payload byte, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0A&lt;/code&gt;.&lt;/p&gt;

\[c = b_0 \oplus b_1 \oplus \cdots \oplus b_{n-1}\]

&lt;p&gt;The bridge repeats the latest frame about 20 times a second. The firmware timeout is 300 ms without a good frame, so a single frame followed by silence would stop the board even while ROS still wanted motion. A second timer inside the bridge watches the Twist. If none arrives for 0.3 s, the repeated command becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You need both stops. If teleop dies and the bridge stays up, the bridge sends the zeros. If the bridge dies or the USB cable leaves, the firmware watchdog is the only stop left, and only if the firmware implements it. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;finally&lt;/code&gt; block does not run when the process is killed or the cable is pulled. Test the quiet publisher, and do not claim you tested the killed process.&lt;/p&gt;

&lt;p&gt;Telemetry may come back as text. The node publishes it as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std_msgs/String&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/capstone_telem&lt;/code&gt; when bytes arrive, and otherwise just logs. Do not block the 20 Hz tick waiting for a line. The dry-run parser does not answer, so the evidence is the parser’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt; line.&lt;/p&gt;

&lt;p&gt;Save the node as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bridge.py&lt;/code&gt;. Parameters are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;port&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;baud&lt;/code&gt;, as in the Jazzy parameter guide. The dry run points &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;port&lt;/code&gt; at a PTY. The real board uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; after your user is in group &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;. Leave &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; unset so the 0.3 s check uses the wall clock. Sim time without a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; freezes that check.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/env python3
&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Capstone A bridge: Twist in, chapter 08 frame out.

Scale: 0.5 m/s of tread speed -&amp;gt; integer 100. Track L = 0.16 m.
No Twist for 0.3 s -&amp;gt; V,0,0 on the wire. Firmware watchdog is the
backup if this process dies.
&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rclpy&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rclpy.node&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Node&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;geometry_msgs.msg&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Twist&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std_msgs.msg&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;TRACK_M&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.16&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;FULL_SCALE_MPS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;WATCHDOG_S&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.3&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;V,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ascii&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;xor_byte&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;xor_byte&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xAA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xor_byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wheel_ints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TRACK_M&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;v_l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;v_r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FULL_SCALE_MPS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;100.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CapstoneBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;capstone_bridge&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;declare_parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/dev/ttyUSB0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;declare_parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;baud&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_parameter_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_value&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;baud&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;baud&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_parameter_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integer_value&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_twist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_logged&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_subscription&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Twist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;cmd_vel&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;on_twist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_publisher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;capstone_telem&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_timer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_twist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Twist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wheel_ints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;angular&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_twist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_twist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nanoseconds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-9&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WATCHDOG_S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;if &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_logged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;wire V,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_logged&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ascii&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;telem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;publish&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rclpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CapstoneBridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;rclpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;spin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;KeyboardInterrupt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;destroy_node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;rclpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;shutdown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;__main__&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A straight command does not catch a left/right swap, because both integers match. Positive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; must produce a larger right integer than left. That is the sign check in the lab.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Straight, \(v = 0.2\), \(\omega = 0\), \(L = 0.16\):&lt;/p&gt;

\[v_L = v_R = 0.2\]

\[n = \mathrm{round}\left(0.2 \cdot \frac{100}{0.5}\right) = 40\]

&lt;p&gt;Payload &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt;. A mild left turn, \(v = 0\), \(\omega = +0.5\):&lt;/p&gt;

\[v_L = -0.5 \times 0.08 = -0.04, \qquad v_R = 0.04\]

\[n_L = \mathrm{round}(-0.04 \times 200) = -8, \qquad n_R = 8\]

&lt;p&gt;Payload &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,-8,8&lt;/code&gt;. If the parser shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,8,-8&lt;/code&gt;, the return order in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_ints&lt;/code&gt; is backwards and the robot will turn the wrong way on the floor.&lt;/p&gt;

&lt;p&gt;The dry-run wiring is a pair of pseudo-terminals:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;socat python3-serial
socat &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; pty,raw,echo&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0 pty,raw,echo&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; prints two paths, for example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/pts/3&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/pts/4&lt;/code&gt;. The bridge parameter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;port&lt;/code&gt; is one of them. A parser adapted from lesson 08-01 listens on the other. Save it as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pty_parser.py&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytearray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;xor_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xAA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0A&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;check&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;xor_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;DROP&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python3 pty_parser.py /dev/pts/4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
python3 capstone_bridge.py &lt;span class=&quot;nt&quot;&gt;--ros-args&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; port:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/dev/pts/3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, from a fourth shell:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 topic pub &lt;span class=&quot;nt&quot;&gt;--once&lt;/span&gt; /cmd_vel geometry_msgs/msg/Twist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;{linear: {x: 0.2, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The parser should print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,40,40&lt;/code&gt; several times, about 20 Hz, and within a second of silence it should print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,0,0&lt;/code&gt;. The bridge log should show the same two strings when the integers change. That pair is the lab.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-tracing-one-full-bridge-cycle-twist---serial---ticks---odom&quot;&gt;Explanation: Hand-Tracing One Full Bridge Cycle (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist -&amp;gt; Serial -&amp;gt; Ticks -&amp;gt; Odom&lt;/code&gt;)&lt;/h3&gt;

&lt;p&gt;Trace one \(\Delta t = 0.05\ \text{s}\) (20 Hz) bridge cycle on a robot with \(b = 0.16\ \text{m}\), \(K = 10\,000\ \text{ticks/m}\), and open-loop gain \(100\ \text{PWM units} = 0.40\ \text{m/s}\):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Incoming ROS 2 command:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; delivers \(v = 0.20\ \text{m/s}\), \(\omega = +0.50\ \text{rad/s}\) (left turn).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Inverse kinematics &amp;amp; scaling to serial integers:&lt;/strong&gt;
\(v_R = 0.20 + 0.50(0.08) = 0.24\ \text{m/s} \implies u_R = \frac{0.24}{0.40} \times 100 = +60,\)
\(v_L = 0.20 - 0.50(0.08) = 0.16\ \text{m/s} \implies u_L = \frac{0.16}{0.40} \times 100 = +40.\)
Bridge sends &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,+40,+60\n&lt;/code&gt; over serial.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Returning encoder deltas from MCU:&lt;/strong&gt; After \(0.05\ \text{s}\), MCU reports \(\Delta N_L = +80\) ticks and \(\Delta N_R = +120\) ticks.
    &lt;ul&gt;
      &lt;li&gt;Wheel distances: \(\Delta s_L = 80/10000 = 0.008\ \text{m}\), \(\Delta s_R = 120/10000 = 0.012\ \text{m}\).&lt;/li&gt;
      &lt;li&gt;Body displacement &amp;amp; yaw increment:
\(\Delta s = \frac{0.012 + 0.008}{2} = 0.010\ \text{m}, \qquad \Delta\theta = \frac{0.012 - 0.008}{0.16} = +0.025\ \text{rad}.\)&lt;/li&gt;
      &lt;li&gt;Measured velocities published in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;: \(v_{\text{meas}} = 0.010 / 0.05 = 0.20\ \text{m/s}\), \(\omega_{\text{meas}} = 0.025 / 0.05 = +0.50\ \text{rad/s}\) — exact closed-loop round-trip match!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Install &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3-serial&lt;/code&gt;. Source Jazzy. Confirm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Start &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; as in the worked example. Write down both PTY paths. Leave &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; running.&lt;/li&gt;
  &lt;li&gt;Start &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pty_parser.py&lt;/code&gt; on the second path.&lt;/li&gt;
  &lt;li&gt;Start &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bridge.py&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-p port:=&lt;/code&gt; set to the first path. The node must stay up. A traceback on open means you swapped the paths or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; has exited.&lt;/li&gt;
  &lt;li&gt;Publish one Twist with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; 0.2 and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; 0, using the command above. Watch the parser.&lt;/li&gt;
  &lt;li&gt;Wait about a second without publishing again. The parser must move from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; even though the bridge process is still running.&lt;/li&gt;
  &lt;li&gt;Publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; 0.5 with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; 0. Confirm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,-8,8&lt;/code&gt;. If you see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,8,-8&lt;/code&gt;, fix &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_ints&lt;/code&gt; before any real motor turns.&lt;/li&gt;
  &lt;li&gt;Only if a Capstone is on the bench: wheels off the ground, driver powered from the robot pack, USB only for serial. Add your user to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3&lt;/code&gt; cannot open &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;, then log out and back in. Run the same node with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-p port:=/dev/ttyUSB0&lt;/code&gt;. Both wheels must nudge forward for the straight command and go quiet after you stop publishing. Unplug is a separate test you do once, with the wheels up: pull USB and confirm the firmware, not the bridge, is what zeros the PWM.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Expected&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After the straight publish, the parser prints:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;OK V,40,40
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;About a second after that publisher goes quiet, still with the bridge alive:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;OK V,0,0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The yaw check prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,-8,8&lt;/code&gt;. On a real board, both wheels turn forward for well under a second and then stop. They must not keep spinning after the publisher has stopped.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,8,-8&lt;/code&gt; on a positive yaw&lt;/td&gt;
      &lt;td&gt;Left and right are swapped in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_ints&lt;/code&gt; or in the payload order&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,0,0&lt;/code&gt; during the 0.2 m/s command, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,20,20&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Metres were cast to int, or full scale is 1.0 m/s instead of 0.5 m/s&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Parser stays on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,40&lt;/code&gt; after you stop publishing&lt;/td&gt;
      &lt;td&gt;The bridge watchdog is not in the tick that writes the frame; the firmware would also keep going if its own timeout is missing&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheels run on after you kill the bridge&lt;/td&gt;
      &lt;td&gt;The firmware watchdog is the only remaining stop, and it is not implemented or is much slower than 300 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Permission denied&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;The user is not in group &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;, or the login session predates the group change&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Parser prints nothing, bridge is up&lt;/td&gt;
      &lt;td&gt;The two programs are on the same PTY, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; was started without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo=0&lt;/code&gt; and a second reader stole the bytes&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;No new parts. The dry run is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; and Python. The hardware version uses the Capstone and the USB cable you already have. Search &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; for “ESP32” only if that board has died. A replacement DevKit on Shopee is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 DevKit V1 30 chân&lt;/code&gt;, 2026 band about 80.000–180.000 VND, recheck before paying. Do not power the motors from that USB cable.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Compute the wire integers for \(v = 0.2\) m/s and \(\omega = 0.5\) rad/s with \(L = 0.16\) and the 0.5 m/s scale. Guidance: left tread 0.16 m/s becomes 32, right tread 0.24 m/s becomes 48, so the payload is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,32,48&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;The publisher stops, the bridge process stays up, and one second passes. What payload is on the wire, and which timer put it there? Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; from the bridge’s 0.3 s Twist watchdog, not from the firmware, because frames are still arriving.&lt;/li&gt;
  &lt;li&gt;The bridge process is killed with the motors armed and the wheels up. What stops them, and on what deadline? Guidance: the Chapter 08 firmware watchdog, about 300 ms after the last good frame, because the bridge’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;finally&lt;/code&gt; block does not run.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls -l /dev/ttyUSB0&lt;/code&gt; shows group &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; and Python raises &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Permission denied&lt;/code&gt;. What do you change, and why is a new login required? Guidance: add your user to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt;; group membership is applied at login, so a new shell in the old session is not enough.&lt;/li&gt;
  &lt;li&gt;A Twist arrives with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y = 0.3&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x = 0&lt;/code&gt;. What integers do you send, and which component did you ignore? Guidance: send zeros from \(v\) and \(\omega\) only; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; is a sideslip the diff-drive cannot produce.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Using-ros2-param.html&quot;&gt;Jazzy parameters&lt;/a&gt; is the interface behind &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-p port:=&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;declare_parameter&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; fixes the sign of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_ints&lt;/code&gt; implements.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; is the frame naming you keep when this same Twist is used in simulation and on the bridge.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; is the later transport. Come back to it only after this parser shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,40,40&lt;/code&gt; and the watchdog line.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>micro-ROS on MCU (ESP32/Pico)</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter10/10_03_microros_mcu/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter10/10_03_microros_mcu</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~75 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Traditional embedded robots need a custom serial protocol on the MCU plus a hand-written Python bridge node on the host PC to translate bytes into ROS 2 topics. &lt;strong&gt;micro-ROS&lt;/strong&gt; collapses that boundary: by running a lightweight XRCE-DDS client right on your ESP32 or RP2040, the microcontroller itself becomes a first-class ROS 2 node publishing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; and subscribing to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will describe XRCE-DDS as a client on the microcontroller plus an agent on the PC that is the actual DDS participant in the ROS 2 graph. You will predict, before you run anything, that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; stays empty of MCU topics when the agent is down and still empty when the agent is up but no client has connected. You will start the Jazzy micro-ROS agent container for UDP and write the matching serial form with the device flags the agent README uses. You will state the course order out loud: the Python serial bridge in the next lesson comes first, and micro-ROS waits until that bridge’s units and the 300 ms watchdog already work. You will leave a written client topic plan that reuses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, telemetry out, and the same frame names as the simulator, even if you do not flash a board tonight.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 09-02’s picture of nodes and topics, lesson 08-01’s framed serial command, and lesson 10-01’s Jazzy shell. Docker is required for the agent command in the lab. You do not need a working micro-ROS firmware image tonight, and you should not rebuild the Capstone teleop around micro-ROS before lesson 10-04 passes. An ESP32 DevKit is the board this lesson talks about. A Pico may sit in a drawer; it is not the supported path below.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-xrce-dds-clientagent-architecture-static-memory-and-embedded-executors&quot;&gt;Key ideas: XRCE-DDS Client–Agent Architecture, Static Memory, and Embedded Executors&lt;/h2&gt;

&lt;p&gt;Running ROS 2 on a 320 KB RAM microcontroller relies on three architectural adaptations:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;XRCE-DDS (eXtremely Resource Constrained Environments):&lt;/strong&gt; Full DDS discovery and serialization are too heavy for an MCU. Instead, the MCU runs a tiny XRCE-DDS &lt;em&gt;client&lt;/em&gt; over UART/Wi-Fi that talks to a host-side &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;micro-ros-agent&lt;/code&gt;, which proxies topics into the standard ROS 2 DDS graph transparently.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Static Memory Allocation (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclc&lt;/code&gt;):&lt;/strong&gt; Dynamic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;free&lt;/code&gt; inside real-time firmware fragments RAM and causes unpredictable crashes after hours of operation. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclc&lt;/code&gt; pre-allocates fixed message buffers, executor handles, and timer slots at boot.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deterministic Embedded Executor (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclc_executor&lt;/code&gt;):&lt;/strong&gt; A single-threaded executor polls subscriptions and periodic timers in a strict, bounded order—preserving real-time control timing while speaking native ROS 2 message types.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Capstone firmware already accepts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,left,right&lt;/code&gt; and already drops torque when those frames stop. ROS 2 does not speak that frame, and this ESP32 does not speak full DDS. Lesson 10-04’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt; node is the translator you run first: ordinary Python, the protocol you already debugged, and a killed process still covered by the firmware watchdog. micro-ROS comes later. The client lives on the MCU, the agent on the PC turns XRCE into DDS, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; can reach the firmware without the Python scale function in between. Nav2 and a later learned policy still publish Twist either way. They will not debug an agent that never saw a client.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch10_microros.png&quot; alt=&quot;micro-ROS client on the ESP32, agent on the PC, then the ROS 2 graph&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;DDS is the middleware under ROS 2 topics. A normal &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclpy&lt;/code&gt; node is a participant. A small microcontroller runs that stack only with pain and RAM. XRCE-DDS is the client profile for that constraint. The board runs a micro-ROS client, and the client does not appear on the ROS graph by itself. The agent, a process on the computer, is the participant. When the client creates a subscriber or a publisher, the agent creates the matching ROS 2 entity and forwards the bytes.&lt;/p&gt;

&lt;p&gt;That fact is the empty topic list. With no agent, nothing acts for the board. With an agent that is only waiting, there is still no client session, so the entities were never created. Seeing only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/parameter_events&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/rosout&lt;/code&gt; means the agent is idle, not that the install failed.&lt;/p&gt;

&lt;p&gt;The course agent is the Docker image tagged for this distro, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;microros/micro-ros-agent:jazzy&lt;/code&gt;. UDP on the laptop, which is the lab tonight because it needs no board, follows the agent README’s shape:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--net&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;host microros/micro-ros-agent:jazzy udp4 &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt; 8888
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--net=host&lt;/code&gt; puts the container on the host network namespace so that, once a client does exist, DDS discovery on the laptop can see the agent. A bridge network without extra DDS configuration hides the agent from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; even after a successful session. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--rm&lt;/code&gt; deletes the container when you exit. Nothing is installed into the Jazzy underlay.&lt;/p&gt;

&lt;p&gt;Serial is the transport you would use when the ESP32 enumerates as a USB serial device. Docker must be allowed to see the host device nodes. The agent README’s shape is a privileged container with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev&lt;/code&gt; mounted, then the agent’s own serial arguments, including the device path:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--privileged&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /dev:/dev &lt;span class=&quot;nt&quot;&gt;--net&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;host &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  microros/micro-ros-agent:jazzy serial &lt;span class=&quot;nt&quot;&gt;--dev&lt;/span&gt; /dev/ttyUSB0 &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; 115200
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Flags before the image name are Docker. Arguments after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;serial&lt;/code&gt; are the agent: device and baud, matching Chapter 08. If a flag disagrees, the agent README wins. Do not run this container together with lesson 10-04’s Python bridge. One process may own &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;ESP32 is the board class micro-ROS treats as supported, with an ESP-IDF path aimed at this agent. The Pico has community ports: a tutorial may track another distro, and nothing promises that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;microros/micro-ros-agent:jazzy&lt;/code&gt; is the other end. Keep a Pico for MicroPython. Use an ESP32 DevKit for this agent.&lt;/p&gt;

&lt;p&gt;The client you eventually flash should use the simulator’s ROS names. It subscribes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; and publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std_msgs/msg/String&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/capstone_telem&lt;/code&gt;. If it publishes odometry, the frames are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, the REP-105 names from lesson 09-05. Wheel commands inside the firmware stay in the −100…100 integers after the same scale lesson 10-04 writes down. micro-ROS does not repeal kinematics.&lt;/p&gt;

&lt;p&gt;The watchdog does not move into the agent. The agent can crash, Docker can lose the device, and Wi-Fi UDP can go quiet. The firmware must still force the wheel command to zero when no fresh command arrives for 300 ms:&lt;/p&gt;

\[\Delta t &amp;gt; 0.3\,\mathrm{s} \Rightarrow (n_L, n_R) = (0, 0)\]

&lt;p&gt;That is the same number as the serial bridge. A client that applies the last Twist forever, because “DDS is reliable,” fails open. Reliability here is a property of the transport you hope to have. The stop is a property of the board.&lt;/p&gt;

&lt;p&gt;Do not power the motor driver from the USB port that flashes the ESP32 or carries the agent. The laptop supply sags, the chip resets, and the session dies mid-command. Motors stay on the fused pack, with a common ground, on the day you leave the dry run. Tonight you do not need that pack.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Write the topic plan on paper before you start Docker. One paragraph is enough, and it is half of the lab credit:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Node name I will look for: /capstone_mcu
Subscribes: /cmd_vel   geometry_msgs/msg/Twist
Publishes:  /capstone_telem   std_msgs/msg/String
Frames if odometry is added later: odom -&amp;gt; base_link
Local rule: no fresh command for 300 ms =&amp;gt; wheel integers 0,0
Not expected tonight: that node name in ros2 node list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then start the agent with no board attached:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;printenv &lt;/span&gt;ROS_DISTRO
docker run &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--net&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;host microros/micro-ros-agent:jazzy udp4 &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt; 8888
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DISTRO&lt;/code&gt; must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt; in the shell where you inspect the graph. Leave the container in the foreground. A healthy idle agent stays running and names &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udp4&lt;/code&gt; and port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8888&lt;/code&gt;. It does not print a client session, because you did not start one.&lt;/p&gt;

&lt;p&gt;Second terminal:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 node list
ros2 topic list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/capstone_mcu&lt;/code&gt; must not be on the list. Absence of the MCU node is the observation. A random demo publisher does not establish the Capstone contract. Skip it, or do not leave it as the robot’s interface.&lt;/p&gt;

&lt;p&gt;Stop the container with Ctrl-C so the next run does not find port 8888 already taken. When a real client connects later, the agent log gains a session and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 node list&lt;/code&gt; gains the node you named. Write that prediction in the notes tonight; the next hardware session checks it.&lt;/p&gt;

&lt;h3 id=&quot;explanation-estimating-ram--uart-overhead-of-a-micro-ros-node&quot;&gt;Explanation: Estimating RAM &amp;amp; UART Overhead of a micro-ROS Node&lt;/h3&gt;

&lt;p&gt;Before choosing between micro-ROS and a thin custom serial bridge on an MCU with \(264\ \text{KB}\) SRAM (such as RP2040) or \(320\ \text{KB}\) usable DRAM (ESP32), check the exact resource footprint:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Static memory budget for a minimal 2-topic node (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; sub + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; pub):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rclc&lt;/code&gt; + Micro-XRCE-DDS client static buffers (MTU = 512 B, 4 history slots): \(\approx 32\text{–}48\ \text{KB}\) RAM.&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/msg/Odometry&lt;/code&gt; struct (pose + twist + two \(6\times 6\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float64&lt;/code&gt; covariance matrices = \(72 \times 8 = 576\ \text{bytes}\) plus headers): \(\approx 0.72\ \text{KB}\) per message.&lt;/li&gt;
      &lt;li&gt;Total static footprint stays comfortably under \(64\ \text{KB}\) (~20% of ESP32 DRAM).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Serial wire budget at 115,200 baud (\(11.52\ \text{kB/s}\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Publishing a full &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Odometry&lt;/code&gt; message (\(\approx 720\ \text{B}\) with XRCE framing) at \(20\ \text{Hz}\) demands \(720 \times 20 = 14.4\ \text{kB/s}\) — &lt;strong&gt;exceeding&lt;/strong&gt; 115,200 baud!&lt;/li&gt;
      &lt;li&gt;Solution: bump UART to \(460\,800\) or \(921\,600\) baud (\(92.1\ \text{kB/s}\)), or publish compact wheel ticks (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Int32MultiArray&lt;/code&gt;, ~40 B) at 50 Hz (\(2.0\ \text{kB/s}\)) and compute full &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Odometry&lt;/code&gt; on the host.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Read this lesson’s architecture once with the agent README open beside it. Confirm the image tag is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt; and not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iron&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Write the topic plan in the worked example into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, including the 300 ms local stop and the frame names &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Run the UDP agent container exactly as written. Wait until the log is idle on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udp4&lt;/code&gt; port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8888&lt;/code&gt;. If the image pull starts, let it finish; the first pull is the slow part.&lt;/li&gt;
  &lt;li&gt;In another terminal, source Jazzy and run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 node list&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt;. Copy both into the notes. State in one sentence that the planned MCU node is absent because no client session exists.&lt;/li&gt;
  &lt;li&gt;Do not flash firmware tonight unless you already have a micro-ROS image and you want extra credit. If you cannot flash, the lab still passes: the idle agent log plus the written plan are the deliverable.&lt;/li&gt;
  &lt;li&gt;Stop the container. If you also sketched the serial command, include &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--privileged&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-v /dev:/dev&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--dev /dev/ttyUSB0&lt;/code&gt; in the notes, and write “do not run this together with the Python bridge.”&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Expected&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The container remains in the foreground. The log identifies UDP port 8888 and does not claim a connected client. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 node list&lt;/code&gt; does not contain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/capstone_mcu&lt;/code&gt; or whatever name you reserved in the plan. Your notes contain the subscription, the publication, and the 300 ms rule.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;agent waiting on udp4 port 8888
ros2 node list: no MCU node
lab-notes: cmd_vel in, capstone_telem out, frames odom and base_link
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker: command not found&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Docker is not installed. The lab needs the engine, not a second ROS install&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Permission denied on the Docker socket&lt;/td&gt;
      &lt;td&gt;Your user is not in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker&lt;/code&gt; group, or you did not start a new login after adding it. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo&lt;/code&gt; is a temporary way through&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Image tag &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt; pulls and runs&lt;/td&gt;
      &lt;td&gt;That agent speaks a different distro’s types. Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:jazzy&lt;/code&gt; to match the laptop&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 node list&lt;/code&gt; empty of MCU nodes, and you start reinstalling ROS&lt;/td&gt;
      &lt;td&gt;No client has connected. That empty list is the correct idle result&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Serial agent and the Python bridge both open &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Two owners of one device. Stop one of them&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Board resets when wheels are commanded&lt;/td&gt;
      &lt;td&gt;Motors were powered from the same USB link as the serial agent. Use the robot pack, not the laptop port&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A Pico tutorial’s build flags fail against this image&lt;/td&gt;
      &lt;td&gt;Pico micro-ROS is community-port territory, not the supported ESP32 path&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;The dry run needs Docker on the laptop, not a new PCB. When you flash, use an ESP32 DevKit like Capstone A. Search “ESP32” on &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt;. Shopee or Lazada keyword: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 DevKit V1 30 chân&lt;/code&gt;. The 2026 band is about 80.000–180.000 VND; recheck before you pay, and skip a bare module with no USB chip if you wanted a DevKit. Do not buy a Pico for this lesson, and do not power motors from the USB cable.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;The agent container is running and you have not flashed anything. What must &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; refuse to show, and why? Guidance: it must refuse the MCU’s topics, because the agent only creates those entities after a client session.&lt;/li&gt;
  &lt;li&gt;This file is numbered before the serial-bridge lesson. Why does the course still tell you to finish that bridge first? Guidance: the Capstone firmware already speaks the Chapter 08 frame, and micro-ROS is the step after the units and the 300 ms watchdog are proven.&lt;/li&gt;
  &lt;li&gt;Write the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run&lt;/code&gt; line for a serial agent on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; and mark which flags belong to Docker. Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--privileged&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-v /dev:/dev&lt;/code&gt; are Docker; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--dev&lt;/code&gt; and the baud belong to the agent after the word &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;serial&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Draft the client contract in four lines: subscribed topic and type, published topic and type, frame ids, timeout. Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; as Twist in, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/capstone_telem&lt;/code&gt; as String out, frames &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, zeros after 300 ms.&lt;/li&gt;
  &lt;li&gt;A classmate wants the Pico to be the micro-ROS board because it is already on the bench. What do you tell them about support, in two sentences? Guidance: ESP32 is the supported class for this agent; Pico ports are community work and are not the path this lesson asks you to buy or flash.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; is the project overview: client on the MCU, agent on the computer, XRCE-DDS in between.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/micro-ROS/micro-ROS-Agent&quot;&gt;micro-ROS agent repository&lt;/a&gt; holds the Docker commands this lesson copies, including the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udp4&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;serial&lt;/code&gt; transports. Prefer it if a flag drifts.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; is the frame-name contract the client plan reuses, so simulation and the board do not invent two trees.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>URDF bring-up in simulation</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter10/10_02_urdf_sim_bringup/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter10/10_02_urdf_sim_bringup</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~90 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A URDF sitting in RViz is just a statue—it shows your robot’s geometry, yet nothing moves, nothing collides, and gravity does not exist. Bringing that URDF into Gazebo Harmonic with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz-sim-diff-drive-system&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt; breathes physical life into the blueprint: wheel torque meets floor friction, encoders publish odometry, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;teleop_twist_keyboard&lt;/code&gt; drives your virtual rover across a simulated room.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will bring the Chapter 09 xacro into a running Harmonic world in a fixed order: expand the model, start &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, launch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf -r&lt;/code&gt;, spawn with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_sim create&lt;/code&gt;, then bridge &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; one way and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; both ways. You will set the diff-drive plugin’s wheel joint names, separation, and radius to the same numbers as the xacro, using the course example of track 0.16 m and wheel radius 0.033 m. You will publish a slow &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; and watch the wheels roll in the Gazebo world itself. You will name five bring-up failures that leave a pretty RViz model while the simulated robot does nothing or falls through the floor. You will keep &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; consistent with the bridged clock so timers and TF share one time base.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 10-01’s empty world launches and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. Lesson 09-06’s xacro exists, with a box body, cylindrical wheels, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; wheel joints, and inertial tags. Lesson 08-04’s inverse kinematics is the meaning of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;: \(v\) forward and \(\omega\) about \(+z\). You can source Jazzy in every new terminal. RViz is optional tonight; the pass condition is motion in the simulator.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-multibody-contact-friction-joint-actuation-plugins-and-state-publishers&quot;&gt;Key ideas: Multibody Contact Friction, Joint Actuation Plugins, and State Publishers&lt;/h2&gt;

&lt;p&gt;Simulation bring-up unites three distinct subsystems of a digital twin:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Coulomb Tire–Floor Friction (\(\mu_1, \mu_2\)):&lt;/strong&gt; A differential-drive tire needs high longitudinal friction (\(\mu \approx 0.8\text{–}1.0\)) to transmit traction without spinning out, while a passive caster sphere needs very low friction (\(\mu \approx 0.01\text{–}0.05\)) so it glides instead of dragging the nose off course.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Actuator Plugin vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2_control&lt;/code&gt;:&lt;/strong&gt; Gazebo’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive&lt;/code&gt; plugin subscribes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;, converts \((v, \omega)\) into wheel angular velocities using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;wheel_separation&amp;gt;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;wheel_radius&amp;gt;&lt;/code&gt;, integrates wheel motion, and publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt; plus &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom -&amp;gt; base_footprint&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Joint State &amp;amp; TF Consistency:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; reads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/joint_states&lt;/code&gt; to animate rotating wheel transforms, while the diff-drive plugin publishes the moving base transform—if either link name or wheel separation mismatches your URDF, RViz shows detached or drifting wheels.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The xacro was a drawing and a TF tree. Harmonic is the first time that drawing has mass, contact, and a clock. Nav2 will later publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;. In simulation the listener is Gazebo’s diff-drive system. On the real Capstone, lesson 10-04’s serial bridge is a second listener on the same topic. If the plugin’s track or radius disagrees with the xacro, every later bag inherits that scale error. Spawn order is the skill. A random stack of terminals looks like a physics bug.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch10_sim_bringup.png&quot; alt=&quot;Order of operations from xacro to a rolling model in Harmonic&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;Bring-up is an order, not a single command.&lt;/p&gt;

&lt;p&gt;First the description. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; expands properties into a URDF that both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; and the spawn tool can read. The course model uses a box body, cylindrical wheels, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; joints named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left_wheel_joint&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right_wheel_joint&lt;/code&gt;, and an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt; on every link that collides. Track is 0.16 m and wheel radius is 0.033 m. Those two numbers are about to be copied into the plugin. Change only one place and the picture and the roller disagree.&lt;/p&gt;

&lt;p&gt;Second, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;. It publishes the TF tree from the URDF. Fixed joints appear even when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joint_states&lt;/code&gt; is empty. In simulation it should use the Gazebo clock once that clock is bridged, otherwise its stamps live on a different timeline from the scan and the odometry you will add later.&lt;/p&gt;

&lt;p&gt;Third, the world. Launch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; so the server is running, not paused. The world name inside that file is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt;. Remember it.&lt;/p&gt;

&lt;p&gt;Fourth, spawn. The create tool inserts the URDF into a world that is already running. The world argument has to be that same name:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_sim create &lt;span class=&quot;nt&quot;&gt;-world&lt;/span&gt; empty &lt;span class=&quot;nt&quot;&gt;-file&lt;/span&gt; /tmp/capstone.urdf &lt;span class=&quot;nt&quot;&gt;-name&lt;/span&gt; capstone
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-world shapes&lt;/code&gt; while the server is running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; does not create a second universe. It fails to find the world. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-file&lt;/code&gt; wants the expanded URDF, not the raw &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.xacro&lt;/code&gt;, unless you have already expanded it. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-name capstone&lt;/code&gt; is the model name you will look for in the GUI.&lt;/p&gt;

&lt;p&gt;Fifth, the bridges. Run the help once if lesson 10-01’s notes are gone:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Clock travels from Gazebo to ROS and must not be driven the other way. The README form is one way, Gazebo to ROS:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; sits between the ROS type and the Gazebo type. Command velocity in this course uses the README’s bidirectional form, so a Twist published on the ROS graph is visible to the plugin and a Gazebo-side publisher would also be visible to ROS:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge /cmd_vel@geometry_msgs/msg/Twist@gz.msgs.Twist
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can pass both arguments to one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge&lt;/code&gt; process. Do not invent the opposite clock direction. A ROS node that publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; while Gazebo also publishes it gives every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; node two opinions.&lt;/p&gt;

&lt;p&gt;The plugin that turns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; into wheel motion is Gazebo Sim’s diff-drive system, loaded from the model. Put a short block in the xacro and keep the numbers lined up with the properties. The fields this course relies on are the joint names, the separation, the radius, and a speed cap:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;gazebo&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;plugin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;filename=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;gz-sim-diff-drive-system&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;gz::sim::systems::DiffDrive&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;left_joint&amp;gt;&lt;/span&gt;left_wheel_joint&lt;span class=&quot;nt&quot;&gt;&amp;lt;/left_joint&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;right_joint&amp;gt;&lt;/span&gt;right_wheel_joint&lt;span class=&quot;nt&quot;&gt;&amp;lt;/right_joint&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;wheel_separation&amp;gt;&lt;/span&gt;0.16&lt;span class=&quot;nt&quot;&gt;&amp;lt;/wheel_separation&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;wheel_radius&amp;gt;&lt;/span&gt;0.033&lt;span class=&quot;nt&quot;&gt;&amp;lt;/wheel_radius&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;max_linear_velocity&amp;gt;&lt;/span&gt;0.5&lt;span class=&quot;nt&quot;&gt;&amp;lt;/max_linear_velocity&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/gazebo&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left_joint&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right_joint&lt;/code&gt; must be the URDF joint names, not the link names. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_separation&lt;/code&gt; is the track \(L\) from lesson 08-04. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt; is the rolling radius. If the installed Harmonic demo uses a different tag set, copy those tag names and keep these meanings. Do not paste a long plugin of odometry fields you have not read.&lt;/p&gt;

&lt;p&gt;The kinematics the plugin is approximating are the same inverse map as the firmware bridge:&lt;/p&gt;

\[v_R = v + \omega \frac{L}{2}, \qquad v_L = v - \omega \frac{L}{2}\]

&lt;p&gt;with \(L = 0.16\). Distance per revolution of one wheel is&lt;/p&gt;

\[s = 2 \pi r\]

&lt;p&gt;with \(r = 0.033\), so one revolution is about 0.207 m of tread. If the plugin’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt; is 0.05 while the cylinder you drew is 0.033, the drawn wheel and the contact radius are different objects. The robot will not travel the distance you compute from the URDF.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; is a contract with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. After the clock bridge is up, start &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-p use_sim_time:=true&lt;/code&gt;, and put simulator sensors on that clock too. A node left on wall time stamps messages the simulator does not share: TF extrapolation errors, or a robot that moved in Gazebo while RViz is stale. The opposite mistake, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; true before any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; exists, freezes timers. Bridge the clock, echo it, then turn the parameter on.&lt;/p&gt;

&lt;p&gt;RViz is a camera, not the plant. RobotModel draws the URDF even when spawn failed. The pass condition is the model in the Gazebo window, wheels rotating when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; is published. With no display, use the odometry topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; shows and confirm its position changes during the Twist. Do not require RViz to prove the physics.&lt;/p&gt;

&lt;p&gt;Collision and inertia are what keep the body on the ground plane. A link with a visual and no collision does not touch the floor. A link with collision and no inertia is rejected or blows up the step. The chassis needs both, and each wheel needs both, because the wheels are the contact patches.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Assume the xacro from lesson 09-06 is at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws/src/capstone_description/urdf/capstone.urdf.xacro&lt;/code&gt;, with the plugin block added, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; 0.16, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_r&lt;/code&gt; 0.033. Expand it:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
xacro ~/ros2_ws/src/capstone_description/urdf/capstone.urdf.xacro &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; /tmp/capstone.urdf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Terminal A, description, using the Gazebo clock:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 run robot_state_publisher robot_state_publisher &lt;span class=&quot;nt&quot;&gt;--ros-args&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; use_sim_time:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; robot_description:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /tmp/capstone.urdf&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Terminal B, world:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 launch ros_gz_sim gz_sim.launch.py gz_args:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;empty.sdf -r&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Terminal C, after the window or the server log is up:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 run ros_gz_sim create &lt;span class=&quot;nt&quot;&gt;-world&lt;/span&gt; empty &lt;span class=&quot;nt&quot;&gt;-file&lt;/span&gt; /tmp/capstone.urdf &lt;span class=&quot;nt&quot;&gt;-name&lt;/span&gt; capstone
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You should see the box and two cylinders in the world, sitting on the plane, not sinking. Then the bridges, one process, clock first in the argument list so you remember which symbol is one-way:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 run ros_gz_bridge parameter_bridge &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  /cmd_vel@geometry_msgs/msg/Twist@gz.msgs.Twist
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Check the clock from a fifth shell:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /clock &lt;span class=&quot;nt&quot;&gt;--once&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The seconds field should be greater than zero because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; is set. If it is zero, the world is paused.&lt;/p&gt;

&lt;p&gt;A slow straight command, held briefly:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub &lt;span class=&quot;nt&quot;&gt;--times&lt;/span&gt; 20 /cmd_vel geometry_msgs/msg/Twist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;{linear: {x: 0.2, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Twenty messages is a short roll, not a drive across the room. In the Gazebo window the base moves along its \(+x\) and both wheels spin. When the publisher ends, the model must stop being driven. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; stays 0 because a diff-drive cannot sideslip. With \(L/2 = 0.08\), \(v = 0.2\) and \(\omega = 0\) ask both treads for 0.2 m/s. You need to see both wheels turn the same way.&lt;/p&gt;

&lt;h3 id=&quot;explanation-checking-wheel-slip-margin-and-torque-limit-in-gazebo&quot;&gt;Explanation: Checking Wheel Slip Margin and Torque Limit in Gazebo&lt;/h3&gt;

&lt;p&gt;Suppose your simulated robot has total mass \(m = 1.20\ \text{kg}\) with \(70\%\) of weight resting evenly on the two drive wheels (\(0.42\ \text{kg}\) per wheel, normal force \(N = 0.42 \times 9.81 = 4.12\ \text{N}\)), wheel radius \(r = 0.032\ \text{m}\), and tire–floor friction coefficient \(\mu = 0.80\).&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Maximum traction force per wheel before tire slip:&lt;/strong&gt;
\(F_{\max} = \mu N = 0.80 \times 4.12 = 3.30\ \text{N}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Corresponding maximum useful wheel torque:&lt;/strong&gt;
\(\tau_{\max} = F_{\max} \cdot r = 3.30 \times 0.032 = 0.106\ \text{N}\cdot\text{m}.\)
Setting Gazebo’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;max_wheel_torque&amp;gt;&lt;/code&gt; to \(100\ \text{N}\cdot\text{m}\) lets the simulator spin tires unrealistically and jump off the floor; setting it near your real motor’s \(0.15\text{–}0.30\ \text{N}\cdot\text{m}\) keeps simulation dynamics realistic.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Maximum linear acceleration without wheelspin (both wheels driving):&lt;/strong&gt;
\(a_{\max} = \frac{2 F_{\max}}{m} = \frac{6.60}{1.20} = 5.50\ \text{m/s}^2.\)
If your commanded &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;max_linear_acceleration&amp;gt;&lt;/code&gt; is \(1.0\ \text{m/s}^2\), you stay well inside the no-slip envelope (18% of traction limit), keeping simulated odometry clean.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Add the short diff-drive block to the xacro. Set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_separation&lt;/code&gt; to the track property and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt; to the wheel radius property. Re-run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp/capstone.urdf&lt;/code&gt; after every edit.&lt;/li&gt;
  &lt;li&gt;Start &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; and the expanded description.&lt;/li&gt;
  &lt;li&gt;Launch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf -r&lt;/code&gt;. Confirm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; still shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Spawn with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-world empty&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-file /tmp/capstone.urdf&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-name capstone&lt;/code&gt;. Look at the Gazebo view, not only at RViz. The body should rest on the plane.&lt;/li&gt;
  &lt;li&gt;Start one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge&lt;/code&gt; with the clock argument using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; argument using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt;, exactly as in the worked example. Echo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; once and confirm the clock is moving.&lt;/li&gt;
  &lt;li&gt;Publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; 0.2 for a short burst (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--times 20&lt;/code&gt; or a couple of seconds). Watch the world.&lt;/li&gt;
  &lt;li&gt;Stop the publisher. The model must stop being driven. Ctrl-C the bridge and publish again: the model should sit still, which proves the motion depended on the bridge.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Expected&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The model named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone&lt;/code&gt; is visible in the Harmonic world, above the ground plane. During the burst, both wheels roll in that world and the base translates forward. After the burst, it is no longer under power. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; echoes a non-zero time. Motion that exists only in an RViz RobotModel, while the Gazebo model sits or is absent, does not pass.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;capstone visible in the empty world
/clock sec &amp;gt; 0
wheels rolling in Gazebo while linear.x is 0.2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Spawn errors, or the model never appears&lt;/td&gt;
      &lt;td&gt;World name mismatch (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shapes&lt;/code&gt; versus &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt;), or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;create&lt;/code&gt; was pointed at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.xacro&lt;/code&gt; instead of the expanded URDF&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Model falls through the floor&lt;/td&gt;
      &lt;td&gt;A link has visual geometry and no collision, so nothing rests on the plane&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Instant explosion, or the model is rejected&lt;/td&gt;
      &lt;td&gt;Collision without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt;, or an inertia of zero&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheels spin like coins, or the base slides without rolling&lt;/td&gt;
      &lt;td&gt;Joint axis is not the axle. Lesson 09-06 used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;axis xyz=&quot;0 1 0&quot;/&amp;gt;&lt;/code&gt; after the cylinder roll&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; echoes in ROS, Gazebo never moves&lt;/td&gt;
      &lt;td&gt;Bridge not running, or the plugin joint names are not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left_wheel_joint&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right_wheel_joint&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TF is stale, or a node with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; never ticks&lt;/td&gt;
      &lt;td&gt;Clock not bridged, world paused because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; was omitted, or only some nodes are on sim time&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;Software only. The lab runs on the same Ubuntu 24.04 laptop as lesson 10-01. You do not need a new board, a lidar, or a robot on the desk. The physical Capstone stays powered down; this lesson never talks to a serial port.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;The xacro track is 0.16 and the plugin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_separation&lt;/code&gt; is 0.20. Which number does the rolling contact use, and what do you change? Guidance: the plugin’s separation drives the sim kinematics, so edit it to 0.16 and respawn rather than “fixing” it in RViz.&lt;/li&gt;
  &lt;li&gt;You send \(v = 0\) and \(\omega = +0.5\) rad/s. Which wheel command is larger under the course sign convention? Guidance: the right wheel, because positive yaw is a left turn when \(+x\) is forward and \(+y\) is left.&lt;/li&gt;
  &lt;li&gt;The launch uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;create&lt;/code&gt; is called with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-world shapes&lt;/code&gt;. What fails, and which flag do you edit? Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;create&lt;/code&gt; looks up a running world by name, so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-world&lt;/code&gt; must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt; while that SDF is the one you launched.&lt;/li&gt;
  &lt;li&gt;Duplicate the URDF, delete the chassis &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt;, and spawn the copy. Record the simulator’s complaint, then restore the tag. Guidance: Harmonic cannot integrate a colliding body that has no mass properties; the visual box is not a substitute.&lt;/li&gt;
  &lt;li&gt;Why is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; bridged with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; rather than with a second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt;? Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge -h&lt;/code&gt; marks &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; as Gazebo to ROS, and a bidirectional clock would let some other node publish a second time base.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/gazebosim/ros_gz/blob/ros2/ros_gz_bridge/README.md&quot;&gt;ros_gz_bridge README&lt;/a&gt; is the authority for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; forms used above. Re-read it if a bridge starts and the topic types do not match.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/getstarted/&quot;&gt;Gazebo Harmonic getting started&lt;/a&gt; covers the world process this spawn attaches to.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/ros_installation/&quot;&gt;Gazebo with ROS&lt;/a&gt; documents the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_sim&lt;/code&gt; launch and the Jazzy pairing.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; is the axis convention behind “positive yaw means left.”&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; is the frame chain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; is already publishing from the URDF.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Gazebo Harmonic intro and spawn</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter10/10_01_gazebo_harmonic/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter10/10_01_gazebo_harmonic</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~70 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Testing a brand-new autonomous navigation stack directly on physical hardware is slow, expensive, and risky: one flipped sign sends your robot into a desk leg. &lt;strong&gt;Gazebo Harmonic&lt;/strong&gt; gives you a physics-backed digital twin—complete with gravity, tire friction, LiDAR rays, and ROS 2 bridge topics—so you can break your code a hundred times in software before risking a single gear tooth on the floor.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will start a Gazebo Harmonic world from a ROS 2 Jazzy terminal and tell &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt; apart from the old &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; Classic binary. You will explain why this course pairs Jazzy with Harmonic, and why sourcing Humble or installing Classic packages sends you into a different simulator. You will use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; to see that a running world already publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, and you will connect the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; flag to the play button so a paused world is not mistaken for a broken install. You will read the direction symbols printed by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt; without leaving a bridge running. You will record either a GUI window or a headless server log as evidence that the empty world stayed up.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 09-01 is done: Ubuntu 24.04, ROS 2 Jazzy debs, and a terminal where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;. You can keep two terminals open and you know that Ctrl-C stops the foreground process. No robot, no URDF, and no micro-ROS firmware are required tonight. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt;, fix that before you install anything in this lesson.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-rigid-body-physics-engines-real-time-factor-and-ros_gz_bridge&quot;&gt;Key ideas: Rigid-Body Physics Engines, Real-Time Factor, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Running trustworthy robotics simulations rests on three computational pillars:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Discrete Physics Stepping (DART / Bullet):&lt;/strong&gt; Gazebo advances Newton–Euler equations in fixed discrete steps \(\Delta t_{\text{sim}}\) (commonly \(1\ \text{ms} = 0.001\ \text{s}\)). Smaller steps improve contact stability on stiff wheel–floor joints but require more CPU per simulated second.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Real-Time Factor (RTF) &amp;amp; Simulation Clock (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;):&lt;/strong&gt; \(\text{RTF} = \Delta t_{\text{sim}} / \Delta t_{\text{wall}}\). When \(\text{RTF} = 0.5\), two wall-clock seconds pass for every one simulated second. Setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; forces every ROS 2 node (controllers, TF2, Nav2) to read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; from Gazebo so timers stay physically synchronized.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Transport Bridging (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt;):&lt;/strong&gt; Gazebo Harmonic speaks native &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz-transport&lt;/code&gt; protobuf messages, while ROS 2 speaks DDS messages. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt; translates bidirectional topics (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/odom&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;) cleanly at the boundary.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capstone A already drives from the Chapter 08 frame, and Chapter 09 put topics, TF, and a xacro on the laptop. This lesson opens the simulator Nav2 will plan in, and that a later learning stack will record against the floor. The seam is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz&lt;/code&gt; family, not Classic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo_ros&lt;/code&gt;. Learn the process names while the world is empty, so a failed spawn next lesson is a model problem rather than a second install of the wrong Gazebo.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch10_gazebo_stack.png&quot; alt=&quot;Gazebo Harmonic beside ROS 2 Jazzy, with ros_gz between them&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;Gazebo Classic and Gazebo Sim are different codebases that share a nickname. Classic is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; command that old ROS 1 tutorials still show. Gazebo Sim is the current line: command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;, SDF worlds, topics you list with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic&lt;/code&gt;. Harmonic is the Gazebo Sim release Jazzy is built against. Humble’s match was Fortress. A tutorial that says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt install gazebo&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-humble-gazebo-ros-pkgs&lt;/code&gt; is not a tutorial for this chapter.&lt;/p&gt;

&lt;p&gt;Jazzy’s apt name for the integration is one metapackage:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-ros-gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That pulls the pieces this course actually calls: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_sim&lt;/code&gt; to launch and spawn, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt; to copy selected topics between the two graphs. Installing it does not start a world. It only puts the executables on the ROS path after you source Jazzy.&lt;/p&gt;

&lt;p&gt;Two stock worlds matter this week. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shapes.sdf&lt;/code&gt; is the getting-started world with a few primitive bodies, useful when you want to know that the renderer drew something. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; is the world the rest of Chapter 10 spawns the Capstone into. The launch shape from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz&lt;/code&gt; docs is:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 launch ros_gz_sim gz_sim.launch.py gz_args:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;empty.sdf -r&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The string inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz_args&lt;/code&gt; is handed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;. The token &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; means run. Without it the server loads the world and waits: the play button stays paused, and simulated time does not move. With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; the world is already running. A paused world is still a successful launch, and it will make every later &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; node look frozen because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; stays at zero.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; is a Gazebo topic before it is a ROS topic. While the server is up, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; lists it whether or not any ROS bridge exists. Bridging is the next lesson. The bridge’s direction symbols are easy to invert, so read them once tonight and do not start a bridge:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You want the help text only. The symbol between the ROS type and the Gazebo type is the direction. The forms this course will use, copied from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz_bridge&lt;/code&gt; README, are a bidirectional command velocity and a one-way clock. Bidirectional:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge /cmd_vel@geometry_msgs/msg/Twist@gz.msgs.Twist
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Clock, Gazebo toward ROS only:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run ros_gz_bridge parameter_bridge /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Do not swap those symbols “to see what happens” tonight. A second publisher on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; fights the simulator. Lesson 2 is where those two lines actually run.&lt;/p&gt;

&lt;p&gt;Simulated time and wall time are not the same quantity. The real-time factor is&lt;/p&gt;

\[\mathrm{RTF} = \frac{\Delta t_{\mathrm{sim}}}{\Delta t_{\mathrm{wall}}}\]

&lt;p&gt;When the GUI reports a factor near 1, one simulated second takes about one wall-clock second. If the factor falls, a 0.2 m/s command still means 0.2 m per simulated second, but a stopwatch runs ahead. An empty world on a normal laptop should sit near 1.&lt;/p&gt;

&lt;p&gt;Headless running is the same server without the GUI. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv DISPLAY&lt;/code&gt; is empty, the window cannot open. Add the server-only switch that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim --help&lt;/code&gt; documents, typically &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-s&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 launch ros_gz_sim gz_sim.launch.py gz_args:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;empty.sdf -r -s&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The lab passes on that log when the process stays up and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; lists &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. The laptop is the lab machine. A Pi 5 can host a lighter stack later; it is the wrong place to learn whether the GUI starts.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Three checks, in order, tell you which Gazebo you are about to launch.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;printenv &lt;/span&gt;ROS_DISTRO
gz sim &lt;span class=&quot;nt&quot;&gt;--help&lt;/span&gt;
apt-cache policy ros-jazzy-ros-gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first line must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;. The help text must belong to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;. If the shell says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz: command not found&lt;/code&gt;, source Jazzy and look at the apt policy. A missing candidate means the Jazzy apt source is absent or the machine is not Ubuntu 24.04. Do not “fix” that by installing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; package. Classic will not load this chapter’s launch file.&lt;/p&gt;

&lt;p&gt;When the policy shows an installed candidate, start the empty world in one terminal and leave it there:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
ros2 launch ros_gz_sim gz_sim.launch.py gz_args:&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;empty.sdf -r&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In a second terminal, source Jazzy again and list Gazebo topics:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /opt/ros/jazzy/setup.bash
gz topic &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You are looking for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; may be almost empty, because nothing has bridged the two graphs. Gazebo is publishing on its own graph. Ctrl-C the launch and the Gazebo topic list dies with the server. A picture of a closed window is not a running world.&lt;/p&gt;

&lt;h3 id=&quot;explanation-checking-simulation-step-budget-rtf-and-contact-penetration&quot;&gt;Explanation: Checking Simulation Step Budget, RTF, and Contact Penetration&lt;/h3&gt;

&lt;p&gt;Suppose Gazebo Harmonic runs with physics step \(\Delta t_{\text{sim}} = 0.001\ \text{s}\) (1000 Hz) and your laptop takes an average of \(0.65\ \text{ms}\) of wall-clock CPU time to solve each step (including a 360-ray 2D LiDAR sensor):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Predict Real-Time Factor (RTF):&lt;/strong&gt;
\(\text{RTF} = \min\!\left(1.0,\ \frac{\Delta t_{\text{sim}}}{t_{\text{compute}}}\right) = \min\!\left(1.0,\ \frac{1.0\ \text{ms}}{0.65\ \text{ms}}\right) = 1.00\)
(full real time, with 35% CPU headroom).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;What happens if you add a heavy 3D depth camera pushing \(t_{\text{compute}}\) to \(2.5\ \text{ms/step}\)?&lt;/strong&gt;
\(\text{RTF} = \frac{1.0\ \text{ms}}{2.5\ \text{ms}} = 0.40.\)
A simulated 10-second drive now takes \(10 / 0.40 = 25\ \text{s}\) of wall time. If your ROS 2 node forgot &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; and kept a \(0.30\ \text{s}\) wall-clock watchdog, it would see Gazebo’s \(0.20\ \text{s}\) sim-time topic interval arrive every \(0.20 / 0.40 = 0.50\ \text{s}\) wall time—tripping false watchdog stops!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Takeaway:&lt;/strong&gt; Always bridge &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;, set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time:=true&lt;/code&gt; on every node, and keep RTF healthy by simplifying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;collision&amp;gt;&lt;/code&gt; meshes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Confirm the distro with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt;. Stop if it is not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;, source &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy/setup.bash&lt;/code&gt;, and check again. If both Humble and Jazzy are installed, the last &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt; wins.&lt;/li&gt;
  &lt;li&gt;Install the integration metapackage: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt install ros-jazzy-ros-gz&lt;/code&gt;. Refuse any follow-up command that installs a package named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-humble-gazebo-ros-pkgs&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;In terminal A, launch the empty world with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz_args:=&quot;empty.sdf -r&quot;&lt;/code&gt;. Leave that process in the foreground.&lt;/li&gt;
  &lt;li&gt;If no window appears, print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv DISPLAY&lt;/code&gt;. When it is empty, stop the launch and start it again with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-s&lt;/code&gt; added inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz_args&lt;/code&gt;, as in the headless line above.&lt;/li&gt;
  &lt;li&gt;In terminal B, source Jazzy and run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt;. Confirm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; is present. Do not start &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run ros_gz_bridge parameter_bridge -h&lt;/code&gt; and write down which symbol is bidirectional and which symbol is Gazebo-to-ROS. Then quit the help. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; should still not show a bridged &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Optional five-minute detour: launch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shapes.sdf&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; once, see the primitives, then return to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf -r&lt;/code&gt;. The next lesson’s spawn names the world &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt;, so that is the server you leave running if you continue tonight.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Expected&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A window titled as a Gazebo Sim session, already running because of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;, or a server process that does not exit when you started with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-s&lt;/code&gt;. The second terminal prints a topic list that includes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. No &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter_bridge&lt;/code&gt; process is left running.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/clock
/stats
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Your list will be longer. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; is the line that matters. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; without a bridge does not need to show &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Unable to locate package ros-jazzy-ros-gz&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Ubuntu is not 24.04, or the Jazzy apt source from the install guide was never added&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; starts a different window, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt; is missing&lt;/td&gt;
      &lt;td&gt;Classic packages on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PATH&lt;/code&gt;. Remove that habit; this chapter uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Launch exits, or the GUI never appears&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DISPLAY&lt;/code&gt; is empty. Use the server-only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-s&lt;/code&gt; form, or run on a machine with a screen&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;The shell sourced Humble last. Jazzy’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz&lt;/code&gt; will not load in that environment&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz topic -l&lt;/code&gt; cannot connect&lt;/td&gt;
      &lt;td&gt;The launch terminal was closed. The server is not running&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Play button is paused and time stays at 0&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; was left off. Press play once to see time move, then relaunch with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;This lab is software on the laptop you already use for Jazzy. No sensor and no ESP32 are required. A Raspberry Pi 5 is an optional later robot computer, not tonight’s GUI machine. The low-RAM board on Hshop has recently started around 2.4 million VND; that is a 2026 estimate, so recheck before you pay. Board: &lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Raspberry Pi 5 made in UK&lt;/a&gt;. Kit: &lt;a href=&quot;https://hshop.vn/combo-raspberry-pi-5-ram-4-8gb-basic-kit&quot;&gt;basic kit&lt;/a&gt;. If the slugs move, search &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; for “Raspberry Pi 5”. Shopee or Lazada keyword: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Raspberry Pi 5 4GB&lt;/code&gt;, same 2026 caution. A listing far under the Hshop board usually omits the supply.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim --help&lt;/code&gt;. In two sentences, say why a missing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; binary is acceptable on this laptop. Guidance: the simulator for Jazzy is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt; from the Harmonic line, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; binary is Classic.&lt;/li&gt;
  &lt;li&gt;Launch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf&lt;/code&gt; once without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; and once with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;. Write what the play control does in each case and whether simulated time moves. Guidance: without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; the server loads the world paused, so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt; does not advance until you press play.&lt;/li&gt;
  &lt;li&gt;Simulated time moves 2.0 s while your stopwatch moves 4.0 s. Compute the real-time factor and say whether an empty world on your laptop should look like that. Guidance: RTF is simulated time divided by wall time, so the value is 0.5, and an empty world should be nearer to 1.&lt;/li&gt;
  &lt;li&gt;Name the apt package this lesson installs and two package names that would pull the wrong simulator. Guidance: install &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-ros-gz&lt;/code&gt;; do not install &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gazebo&lt;/code&gt; or a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-humble-gazebo-ros&lt;/code&gt; package.&lt;/li&gt;
  &lt;li&gt;Copy the exact launch command you will need in the next lesson, including the world file and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt;. Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz_args&lt;/code&gt; must contain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty.sdf -r&lt;/code&gt;, because the spawn tool will look up a world named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/getstarted/&quot;&gt;Gazebo Harmonic getting started&lt;/a&gt; shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gz sim&lt;/code&gt;, the shapes world, and the play control this lesson uses.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/ros_installation/&quot;&gt;Gazebo Harmonic with ROS&lt;/a&gt; is the pairing note: Jazzy with Harmonic, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_gz&lt;/code&gt; packages.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation/Ubuntu-Install-Debs.html&quot;&gt;Jazzy Ubuntu deb install&lt;/a&gt; is the apt-source page you need when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-ros-gz&lt;/code&gt; cannot be located.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/gazebosim/ros_gz/blob/ros2/ros_gz_bridge/README.md&quot;&gt;ros_gz_bridge README&lt;/a&gt; is the source of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&lt;/code&gt; command lines. Read it before lesson 2 runs them.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>URDF / xacro robot model basics</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter09/09_06_urdf_basics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter09/09_06_urdf_basics</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~90 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;How do RViz, Gazebo, and Nav2 all agree on where your robot’s wheels, bumper, and LiDAR sit without you re-typing coordinates into three different programs? They all read one XML blueprint: the &lt;strong&gt;URDF&lt;/strong&gt; (usually generated cleanly by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; macros). A good URDF is not just a pretty 3D mesh—it encodes the kinematic tree, collision hulls, and inertia tensors that govern physics simulation.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will write a URDF for a two-wheel base with a caster, separate visual, collision, and inertial tags, and load it through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; so the TF tree from lesson 09-05 appears without a hand-written static publisher. You will move one number (track width, wheel radius) into a xacro property and show that both wheel joints change together. You will also say which joint type a driven wheel uses (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt;) and which one a caster bracket uses (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fixed&lt;/code&gt;), and why a pretty mesh with no collision tag is a ghost in Gazebo.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 09-05’s frame names, lesson 08-04’s \(L\) and wheel radius, and a Jazzy desktop install so RViz can draw the model. XML well-formedness matters: a missing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;/link&amp;gt;&lt;/code&gt; fails the whole file. You do not need a 3D scanner. Boxes and cylinders are the right first model.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-kinematic-trees-rigid-body-inertia-tensors-and-parametric-cad-xacro&quot;&gt;Key ideas: Kinematic Trees, Rigid-Body Inertia Tensors, and Parametric CAD (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt;)&lt;/h2&gt;

&lt;p&gt;Writing a simulation-ready URDF connects three pillars of multibody dynamics and software design:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Links &amp;amp; Joints (Kinematic Tree):&lt;/strong&gt; Every rigid part is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;link&amp;gt;&lt;/code&gt; with three separate geometries: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;visual&amp;gt;&lt;/code&gt; (appearance), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;collision&amp;gt;&lt;/code&gt; (simplified boxes/cylinders for fast contact math), and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/code&gt; (mass + \(3\times 3\) inertia tensor). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;joint&amp;gt;&lt;/code&gt; tags (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fixed&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prismatic&lt;/code&gt;) define parent–child transforms and axes of motion, which &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; automatically broadcasts to TF2.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rigid-Body Inertia Tensor:&lt;/strong&gt; A physics engine cannot simulate rotation from mass alone—it needs the moments of inertia \(I_{xx}, I_{yy}, I_{zz}\) about the link’s center of mass. Setting inertia to zero or guessing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.0&lt;/code&gt; for a 50-gram wheel makes Gazebo explode or spin uncontrollably.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Parametric Macros (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt;):&lt;/strong&gt; Copying left/right wheel XML by hand invites sign typos. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; lets you define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt;, and a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;xacro:wheel side=&quot;left&quot; reflect=&quot;1&quot;/&amp;gt;&lt;/code&gt; macro so your Chapter 06 measurements propagate everywhere cleanly.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; reads the URDF and publishes the static tree every other node expects. Gazebo Harmonic in Chapter 10 spawns that same file and adds physics. Nav2’s footprint is the collision outline you write here, not the acrylic photo. A learned policy does not consume URDF directly, but the robot you record data from in Chapter 12 still has joints, limits, and a base frame. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; in the URDF and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; in the bridge disagree, every later frame is offset by a constant you will blame on SLAM. One file, one set of properties, xacro so you do not copy the left wheel by hand and forget to negate \(y\).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_urdf.png&quot; alt=&quot;Links and joints of a small diff-drive base&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;link&lt;/strong&gt; is a rigid body. A &lt;strong&gt;joint&lt;/strong&gt; connects a child link to a parent and says what motion is allowed. The tree has one root. For this base the root is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Joint types you need this week:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fixed&lt;/code&gt; does not move. Caster bracket, laser mount, IMU board.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; rotates without limit. Driven wheels. Do not use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt; with huge limits unless you enjoy a wheel that “hits the end stop” in the simulator.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt; is for a hinge with a lower and upper limit. That is an arm joint, Chapter 11’s MoveIt peek, not a wheel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each link can carry three different shapes. &lt;strong&gt;Visual&lt;/strong&gt; is what RViz draws. &lt;strong&gt;Collision&lt;/strong&gt; is what the simulator uses for contact. &lt;strong&gt;Inertial&lt;/strong&gt; is mass, centre of mass, and inertia, which Gazebo needs before dynamics mean anything. A link with only a visual is drawable and physically weightless. A wheel with collision and no inertia often makes the simulator refuse the model or explode the time step. Copy a plausible mass: a small robot body of 1.5 kg, a wheel of 0.05 kg. Wrong by a factor of two is acceptable tonight. Wrong by a factor of a thousand (grams typed as kilograms, or a mesh in millimetres) is not.&lt;/p&gt;

&lt;p&gt;The origin of a joint is the child’s pose in the parent, in metres and radians. Left wheel at \(y = +L/2\), right wheel at \(y = -L/2\), both with the spin axis along \(y\) if the cylinder was built along \(z\) and you roll it by \(\pi/2\). Get the axis wrong and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; spins the wheel like a coin instead of rolling. In the model below the visual cylinder’s axis is \(z\), the joint origin rolls it so the axle is \(y\), and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;axis xyz=&quot;0 1 0&quot;/&amp;gt;&lt;/code&gt; is the spin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;xacro&lt;/strong&gt; is XML with properties and macros. A property:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;track&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.16&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wheel_r&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.033&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wheel_w&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.02&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You reference it as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;${track/2}&lt;/code&gt;. Change &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; once when you remeasure the Capstone. The official tutorial &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Using-Xacro-to-Clean-Up-a-URDF-File.html&quot;&gt;using xacro&lt;/a&gt; shows macros; a property is enough until the file hurts to scroll.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; subscribes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joint_states&lt;/code&gt; and publishes TF for every joint. Fixed joints publish even when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joint_states&lt;/code&gt; is empty. Continuous joints stay at zero until something publishes a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/JointState&lt;/code&gt;. For a first RViz view, zeros are fine: the wheels exist, they just do not spin yet. Chapter 10’s diff-drive plugin is what will spin them.&lt;/p&gt;

&lt;p&gt;Check the file before you launch it:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;liburdfdom-tools
check_urdf &amp;lt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;xacro src/capstone_description/urdf/capstone.urdf.xacro&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt; a list of links and joints, root &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, no “not well-formed” XML error.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Put this in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws/src/capstone_description/urdf/capstone.urdf.xacro&lt;/code&gt; after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 pkg create capstone_description --build-type ament_cmake&lt;/code&gt;. The package needs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;urdf&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro&lt;/code&gt; dependencies and an install rule for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;urdf&lt;/code&gt; directory. The model itself:&lt;/p&gt;

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot;?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;robot&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:xacro=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://www.ros.org/wiki/xacro&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;capstone&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;track&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.16&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wheel_r&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.033&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:property&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wheel_w&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.02&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;link&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;base_link&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;visual&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;origin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 0.05&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;rpy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;geometry&amp;gt;&amp;lt;box&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;size=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.18 0.12 0.06&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&amp;lt;/geometry&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/visual&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;collision&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;origin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 0.05&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;rpy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;geometry&amp;gt;&amp;lt;box&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;size=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.18 0.12 0.06&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&amp;lt;/geometry&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/collision&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;mass&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1.5&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;inertia&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixx=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.004&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;iyy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.006&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;iyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;izz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.008&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/inertial&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/link&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:macro&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wheel&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;params=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;prefix y_sign&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;link&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${prefix}_wheel&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;visual&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;geometry&amp;gt;&amp;lt;cylinder&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;radius=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${wheel_r}&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;length=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${wheel_w}&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&amp;lt;/geometry&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/visual&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;collision&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;geometry&amp;gt;&amp;lt;cylinder&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;radius=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${wheel_r}&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;length=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${wheel_w}&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&amp;lt;/geometry&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/collision&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;inertial&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;mass&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0.05&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;inertia&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixx=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1e-4&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ixz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;iyy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1e-4&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;iyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;izz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1e-4&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;/inertial&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/link&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;joint&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${prefix}_wheel_joint&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;continuous&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;parent&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;link=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;base_link&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;child&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;link=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;${prefix}_wheel&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;origin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 ${y_sign * track/2} ${wheel_r}&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;rpy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-1.5708 0 0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;axis&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 1 0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/joint&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/xacro:macro&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:wheel&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;prefix=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;left&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;y_sign=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xacro:wheel&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;prefix=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;right&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;y_sign=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-1&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;link&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;caster&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;joint&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;caster_joint&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;fixed&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;parent&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;link=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;base_link&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;child&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;link=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;caster&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;origin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xyz=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-0.08 0 0.02&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;rpy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0 0 0&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/joint&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/robot&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The caster link is empty on purpose so you see a fixed joint in the tree without a mesh hunt. Add a sphere visual when the empty link bothers you. Do not leave the driven wheels empty: Gazebo needs collision there.&lt;/p&gt;

&lt;p&gt;Launch sketch, after the package installs the file:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LaunchDescription&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch.substitutions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PathJoinSubstitution&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch_ros.actions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Node&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch_ros.substitutions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FindPackageShare&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;generate_launch_description&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;xacro_file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PathJoinSubstitution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;FindPackageShare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;capstone_description&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;urdf&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;capstone.urdf.xacro&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;xacro &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xacro_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LaunchDescription&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;robot_state_publisher&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;executable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;robot_state_publisher&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;robot_description&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rviz2&lt;/code&gt;, Fixed Frame &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, add a RobotModel display, and set the description topic to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/robot_description&lt;/code&gt; if it is not already. &lt;strong&gt;Expected:&lt;/strong&gt; a box and two cylinders, left and right, axles on \(y\). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run tf2_tools view_frames&lt;/code&gt; shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; as parent of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left_wheel&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right_wheel&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caster&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Change &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.16&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.20&lt;/code&gt;, relaunch, and the wheels must move apart. If only one wheel moves, you hardcoded a coordinate and the property is unused.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-a-wheels-solid-cylinder-inertia-tensor-for-urdf&quot;&gt;Explanation: Hand-Calculating a Wheel’s Solid-Cylinder Inertia Tensor for URDF&lt;/h3&gt;

&lt;p&gt;Never paste dummy inertia numbers like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ixx=&quot;0.1&quot;&lt;/code&gt; into a URDF. Let us compute the exact principal moments of inertia for a real 2WD wheel modeled as a solid cylinder of mass \(m = 0.080\ \text{kg}\), radius \(r = 0.032\ \text{m}\), and width \(h = 0.026\ \text{m}\) (with the cylinder’s spin axis along local \(z\) before rotating \(90^\circ\) onto the \(y\) axle):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Spin-axis moment of inertia (\(I_{zz}\)):&lt;/strong&gt;
\(I_{zz} = \frac{1}{2} m r^2 = 0.5 \times 0.080 \times (0.032)^2 = 0.040 \times 0.001024 = 4.10 \times 10^{-5}\ \text{kg}\cdot\text{m}^2.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Transverse moments of inertia (\(I_{xx} = I_{yy}\)):&lt;/strong&gt;
\(I_{xx} = I_{yy} = \frac{1}{12} m (3r^2 + h^2) = \frac{0.080}{12}\bigl(3(0.001024) + (0.026)^2\bigr) = 0.006667 \times (0.003072 + 0.000676) = 2.50 \times 10^{-5}\ \text{kg}\cdot\text{m}^2.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check triangle inequality (physical validity):&lt;/strong&gt; A valid rigid body must satisfy \(I_{xx} + I_{yy} \ge I_{zz}\). Here \(2.50 + 2.50 = 5.00 \ge 4.10\) (\(\times 10^{-5}\)), and all off-diagonal products \(I_{xy} = I_{xz} = I_{yz} = 0\). Notice how tiny \(4.1 \times 10^{-5}\) is compared to a naive guess of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.1&lt;/code&gt; (which would be &lt;strong&gt;2,400× too heavy to spin&lt;/strong&gt;!).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check_urdf&lt;/code&gt; on the expanded xacro. Paste the link list into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Launch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt; and open RViz. Screenshot or a sentence: “two wheels, box body, fixed frame base_link.”&lt;/li&gt;
  &lt;li&gt;Set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; to your measured \(L\) from lesson 08-04. Rebuild or rely on xacro reading the source if you have not installed a copy. Confirm the wheel \(y\) values with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo base_link left_wheel&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Delete the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;collision&amp;gt;&lt;/code&gt; of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; in a throwaway copy and write one sentence about what Gazebo will think the body is. Guidance you should reach on your own before Chapter 10: a visual with no collision does not hit walls.&lt;/li&gt;
  &lt;li&gt;Name the joint type you would use for a servo horn that may only move from \(-90^\circ\) to \(90^\circ\). That is the MoveIt peek, not this base.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_description&lt;/code&gt; empty&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Command([&quot;xacro &quot;, path])&lt;/code&gt; did not run, or the file is not installed in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;share/&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheels inside the box&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;origin xyz&lt;/code&gt; uses centimetres, or \(z\) is 0 so the axle is on the floor of the box&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheel spins around the wrong axis in a later sim&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;axis&amp;gt;&lt;/code&gt; not aligned with the cylinder after the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rpy&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check_urdf&lt;/code&gt; says the child link is missing&lt;/td&gt;
      &lt;td&gt;Macro parameter typo. Expand with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xacro file.urdf.xacro&lt;/code&gt; and read the plain XML&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;RViz fixed frame error&lt;/td&gt;
      &lt;td&gt;You asked for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;. This model has no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; until SLAM&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;No parts for the model. If you are about to cut a new chassis and want the URDF to match the holes, a pair of matching wheels matters more than a new computer:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Search on Shopee or Lazada&lt;/th&gt;
      &lt;th&gt;2026 band&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Rubber robot wheel about 65 mm, sold as a pair&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh xe robot 65mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 20.000–60.000 VND each; buy two together&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Caster wheel&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh đa hướng robot 30mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 15.000–40.000 VND&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Digital caliper, if the tape from 08-04 is not enough for wheel diameter&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thước kẹp điện tử 150mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 80.000–200.000 VND&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Measure diameter and divide by two before you type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_r&lt;/code&gt;. A 65 mm tyre is \(r = 0.0325\) m, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;65&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Left wheel &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y = -0.08&lt;/code&gt; and you set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y_sign&lt;/code&gt; of left to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+1&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; 0.16. What did you swap? Guidance: the sign in the macro call, or you are echoing the right wheel. \(+y\) is left in REP-103.&lt;/li&gt;
  &lt;li&gt;Why is the caster &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fixed&lt;/code&gt; even though it swivels on the real robot? Guidance: for this course the caster is not actuated and you are not modelling its swivel. A fixed joint keeps a frame. Modelling the swivel is extra, and it is not a drive wheel.&lt;/li&gt;
  &lt;li&gt;Visual mesh is 1000 times too big. Which tag did you forget to scale, and what is the usual cause? Guidance: the mesh was exported in millimetres. Scale the mesh or the visual origin. Collision must get the same scale or contact happens a metre away from the drawing.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joint_states&lt;/code&gt; never arrives. Where are the wheels in RViz? Guidance: at the zero angle of a continuous joint. The link is still there. Spinning comes from a publisher, which Chapter 10 adds.&lt;/li&gt;
  &lt;li&gt;Write the single xacro property name you will refuse to duplicate in the bridge YAML. Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt;. One measurement, two consumers, and they must be the same metres.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Building-a-Visual-Robot-Model-with-URDF-from-Scratch.html&quot;&gt;Building a visual robot model with URDF&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Using-Xacro-to-Clean-Up-a-URDF-File.html&quot;&gt;Using xacro to clean up a URDF&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://wiki.ros.org/urdf/XML&quot;&gt;URDF XML specification&lt;/a&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;continuous&lt;/code&gt; versus &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;revolute&lt;/code&gt;. The wiki is old and the tags are still the tags.&lt;/li&gt;
  &lt;li&gt;Nav2’s Jazzy note on the robot model, &lt;a href=&quot;https://docs.nav2.org/jazzy/configuration_and_development/first_time_robot_setup_guide/urdf/setup_urdf/&quot;&gt;setting up the URDF&lt;/a&gt;, when you want the footprint step after this box-and-cylinder passes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check_urdf&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>TF2 and coordinate frames</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter09/09_05_tf2_frames/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter09/09_05_tf2_frames</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~90 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A LiDAR mounted on the front bumper reports an obstacle 0.80 meters directly ahead of &lt;em&gt;itself&lt;/em&gt;—but your wheels sit 15 centimeters farther back, and your global map lives in a room coordinate frame rotated by \(30^\circ\). Every time a robot mixes coordinates from two different frames without transforming them, it drives straight into the obstacle it just detected. &lt;strong&gt;TF2&lt;/strong&gt; is the live mathematical tree that keeps every sensor, link, and map frame rigorously aligned in space and time.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will publish a static transform from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;, echo it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt;, and draw the REP-105 chain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom → base_link → sensors&lt;/code&gt; from memory with the name of the node that should own each edge. You will recognise two publishers on the same edge as a bug, and you will place a camera optical frame instead of pretending the camera looks along \(+x\). Wheel odometry from Chapter 08 becomes the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt; edge only after this vocabulary is stable.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 09-02’s topics, and lesson 08-04’s body frame: \(+x\) forward, \(+y\) left, \(+z\) up, positive yaw a left turn. You can run two terminals with Jazzy sourced. RViz is installed if you took &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-desktop&lt;/code&gt;. No lidar is required; a static publisher stands in for the sensor mount.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-rigid-body-transforms-se3-homogeneous-matrices-and-the-rep-105-tree&quot;&gt;Key ideas: Rigid-Body Transforms (\(SE(3)\)), Homogeneous Matrices, and the REP-105 Tree&lt;/h2&gt;

&lt;p&gt;TF2 operationalizes classical 3D spatial geometry across three pillars:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Homogeneous Transformation Matrices (\(SE(2)\) / \(SE(3)\)):&lt;/strong&gt; A rigid transform from frame \(B\) to frame \(A\) combines a \(3\times 3\) rotation matrix \(R_{AB} \in SO(3)\) and translation vector \(p_{AB} \in \mathbb{R}^3\) into a \(4\times 4\) matrix \(T_{AB} = \begin{bmatrix} R_{AB} &amp;amp; p_{AB} \\ 0 &amp;amp; 1 \end{bmatrix}\). Chaining transforms along a tree reduces to simple matrix multiplication: \(T_{AC} = T_{AB} T_{BC}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Standard Mobile-Robot Frame Tree (REP-105):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map -&amp;gt; odom -&amp;gt; base_footprint -&amp;gt; base_link -&amp;gt; laser_frame&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom -&amp;gt; base_link&lt;/code&gt; is continuous and smooth (from wheel encoders/IMU) but drifts over time; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map -&amp;gt; odom&lt;/code&gt; absorbs discrete loop-closure corrections from SLAM/AMCL so local controllers never see sudden pose jumps.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Static vs. Dynamic Transforms &amp;amp; Time Interpolation:&lt;/strong&gt; Fixed sensor mounts (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link -&amp;gt; laser_frame&lt;/code&gt;) are broadcast once on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf_static&lt;/code&gt;, whereas moving joints and odometry stream on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tf&lt;/code&gt; with timestamps so TF2 can interpolate poses to the exact microsecond a scan was captured.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A laser point is a range and a bearing in the laser’s frame. A goal is a pose in the map. Nav2, SLAM, and a vision node do not “just know” where the sensor is bolted. TF2 is the shared tree that answers “where was frame A in frame B at time t.” If you name the laser &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_link&lt;/code&gt; in the URDF and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scan&lt;/code&gt; in the bridge, the scan is a set of numbers with no address. If two nodes both publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;, the robot jumps in RViz and the costmap smears. Chapter 10’s simulator and Chapter 11’s SLAM will not invent this tree for you. The Capstone’s measured sensor offsets belong here, in metres, before anyone tunes a planner.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_tf_tree.png&quot; alt=&quot;REP-105 frame chain&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A frame is a named right-handed coordinate system. A transform is a translation plus a rotation that takes points from a child frame into a parent frame. TF2 stores those edges with timestamps. You query them. You do not hard-code “the lidar is 8 cm forward” inside the SLAM node and again inside the detector.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; fixes the mobile-base names this course will use:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; is the world frame SLAM or localization owns. It may jump when the map corrects drift.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; is a smooth frame that starts where the robot started. It is allowed to drift. It is not allowed to jump.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; is attached to the robot body, typically at the axle midpoint for a diff-drive, with \(+x\) forward.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_footprint&lt;/code&gt; is the projection of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; onto the ground. Useful, optional for a first model.&lt;/li&gt;
  &lt;li&gt;Sensor frames (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imu_link&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt;) hang off &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; and are usually static.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Who publishes which edge:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Edge&lt;/th&gt;
      &lt;th&gt;Publisher&lt;/th&gt;
      &lt;th&gt;Character&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;SLAM or AMCL, later&lt;/td&gt;
      &lt;td&gt;May jump&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Wheel odometry (your bridge or a controller)&lt;/td&gt;
      &lt;td&gt;Continuous&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → laser&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;URDF via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, or a static publisher&lt;/td&gt;
      &lt;td&gt;Fixed bolt pattern&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;One parent, one broadcaster, per edge. A second odometry node “to compare” will fight the first unless you put the comparison on a different child frame such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom_raw&lt;/code&gt;. Do not publish both into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Time matters. A scan stamped 200 ms in the past needs the transform that was true then, not the transform that is true now, if the robot is turning. TF2 keeps a buffer for that. If the buffer does not contain the stamp, you get an extrapolation error. The usual causes are: the odometry stopped, the stamps use a different clock (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; on one node only), or the scan’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; is a name nobody publishes.&lt;/p&gt;

&lt;p&gt;Camera frames have a second convention. &lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; says a body frame looks along \(+x\). An optical frame looks along \(+z\), with \(+x\) right and \(+y\) down, because that is how image rows work. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; can follow the body convention. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_optical_frame&lt;/code&gt; is the frame you put in an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt; message’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt;. Swapping them makes a “left” pixel command a right turn. The rotation from link to optical is a fixed transform: roll \(-\pi/2\), then yaw \(-\pi/2\), which you can publish once and then stop thinking about it.&lt;/p&gt;

&lt;p&gt;Units are metres and radians. A “5” that means five centimetres will place the laser inside the axle and every cloud will look like a ball around the robot.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;The Capstone carries a tiny lidar 5 cm forward of the axle centre, 8 cm up, pointing forward. No roll, no pitch, no yaw relative to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;. Publish that as a static transform:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run tf2_ros static_transform_publisher &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--x&lt;/span&gt; 0.05 &lt;span class=&quot;nt&quot;&gt;--y&lt;/span&gt; 0.0 &lt;span class=&quot;nt&quot;&gt;--z&lt;/span&gt; 0.08 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--roll&lt;/span&gt; 0 &lt;span class=&quot;nt&quot;&gt;--pitch&lt;/span&gt; 0 &lt;span class=&quot;nt&quot;&gt;--yaw&lt;/span&gt; 0 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--frame-id&lt;/span&gt; base_link &lt;span class=&quot;nt&quot;&gt;--child-frame-id&lt;/span&gt; laser
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Second terminal:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run tf2_ros tf2_echo base_link laser
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected&lt;/strong&gt;, repeating at a few hertz:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;At time ...
- Translation: [0.050, 0.000, 0.080]
- Rotation: in Quaternion [0.000, 0.000, 0.000, 1.000]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The echo order is parent then child: “where is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;.” If you swap the names, the translation comes back negated, which is a useful check and a common accident.&lt;/p&gt;

&lt;p&gt;View the tree:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run tf2_tools view_frames
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That writes a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frames.pdf&lt;/code&gt; (and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.gv&lt;/code&gt;) in the current directory. Open it. You want a single arrow from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;. Two arrows into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; means two publishers.&lt;/p&gt;

&lt;p&gt;A point 1 m in front of the laser is \((1, 0, 0)\) in the laser frame. In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; it is about \((1.05, 0, 0.08)\). You do not add those numbers by hand in Nav2. You ask TF. The manual add is only the sanity check that the static numbers are in metres.&lt;/p&gt;

&lt;p&gt;Optical frame, if you already own a camera and want the name reserved:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run tf2_ros static_transform_publisher &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--x&lt;/span&gt; 0 &lt;span class=&quot;nt&quot;&gt;--y&lt;/span&gt; 0 &lt;span class=&quot;nt&quot;&gt;--z&lt;/span&gt; 0 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--roll&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;.5708 &lt;span class=&quot;nt&quot;&gt;--pitch&lt;/span&gt; 0 &lt;span class=&quot;nt&quot;&gt;--yaw&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;.5708 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--frame-id&lt;/span&gt; camera_link &lt;span class=&quot;nt&quot;&gt;--child-frame-id&lt;/span&gt; camera_optical_frame
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Do not attach the optical frame to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; and also attach &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; with a different offset unless you enjoy two cameras in RViz. Chain them: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link → camera_link → camera_optical_frame&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-multiplying-a-2d-tf2-chain-map---base_link---laser&quot;&gt;Explanation: Hand-Multiplying a 2D TF2 Chain (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map -&amp;gt; base_link -&amp;gt; laser&lt;/code&gt;)&lt;/h3&gt;

&lt;p&gt;Suppose your robot’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; is currently at \((x, y) = (2.0,\ 1.0)\ \text{m}\) in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; frame, facing \(\theta = 90^\circ\) (straight along \(+y_{\text{map}}\)). A LiDAR (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_frame&lt;/code&gt;) is mounted \(0.15\ \text{m}\) ahead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; along \(+x_{\text{base}}\) with zero rotation, and it detects a cone at \(p_{\text{laser}} = (0.80,\ 0.20)\ \text{m}\). Where is the cone in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; frame?&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Write the two \(3\times 3\) planar homogeneous transforms (\(\cos 90^\circ = 0,\ \sin 90^\circ = 1\)):&lt;/strong&gt;
\(T_{\text{map},\text{base}} = \begin{bmatrix} 0 &amp;amp; -1 &amp;amp; 2.0 \\ 1 &amp;amp; 0 &amp;amp; 1.0 \\ 0 &amp;amp; 0 &amp;amp; 1 \end{bmatrix}, \qquad T_{\text{base},\text{laser}} = \begin{bmatrix} 1 &amp;amp; 0 &amp;amp; 0.15 \\ 0 &amp;amp; 1 &amp;amp; 0 \\ 0 &amp;amp; 0 &amp;amp; 1 \end{bmatrix}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Transform the point into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; first:&lt;/strong&gt;
\(p_{\text{base}} = T_{\text{base},\text{laser}} \begin{bmatrix} 0.80 \\ 0.20 \\ 1 \end{bmatrix} = \begin{bmatrix} 0.80 + 0.15 \\ 0.20 \\ 1 \end{bmatrix} = \begin{bmatrix} 0.95 \\ 0.20 \\ 1 \end{bmatrix}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Transform from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;:&lt;/strong&gt;
\(p_{\text{map}} = \begin{bmatrix} 0 &amp;amp; -1 &amp;amp; 2.0 \\ 1 &amp;amp; 0 &amp;amp; 1.0 \\ 0 &amp;amp; 0 &amp;amp; 1 \end{bmatrix} \begin{bmatrix} 0.95 \\ 0.20 \\ 1 \end{bmatrix} = \begin{bmatrix} -0.20 + 2.0 \\ 0.95 + 1.0 \\ 1 \end{bmatrix} = \begin{bmatrix} 1.80 \\ 1.95 \\ 1 \end{bmatrix}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Physical sanity check:&lt;/strong&gt; Because the robot faces \(+y_{\text{map}}\), “0.95 m forward” adds to \(y\) (\(1.0 + 0.95 = 1.95\)) and “0.20 m to the left” subtracts from \(x\) (\(2.0 - 0.20 = 1.80\)). Every TF2 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lookup_transform&lt;/code&gt; call in ROS 2 performs this exact matrix chain!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Publish the static laser transform with your real offsets. If you have not measured yet, use the 5 cm / 8 cm example and write “unmeasured” in the notes. Measure later with the same tape as lesson 08-04.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; until the translation matches what you typed, to millimetres.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;view_frames&lt;/code&gt; and keep the PDF in the lab folder.&lt;/li&gt;
  &lt;li&gt;Start a second static publisher with the same parent and child but &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--x 0.5&lt;/code&gt;. Echo again. &lt;strong&gt;Expected:&lt;/strong&gt; the transform flips between 0.05 and 0.5, or RViz (if you add a TF display) jitters. Kill the intruder. This is the failure mode you will recognise when two odometry nodes fight.&lt;/li&gt;
  &lt;li&gt;Optional RViz: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rviz2&lt;/code&gt;, add a TF display, set Fixed Frame to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;. You should see the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt; axes 5 cm forward. If the fixed frame is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; and you have no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, RViz complains. That complaint is honest. You have not started SLAM.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; waits forever&lt;/td&gt;
      &lt;td&gt;Frame names differ by a character, or the publisher died. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /tf_static --once&lt;/code&gt; shows the raw message&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Axes point backward&lt;/td&gt;
      &lt;td&gt;Yaw of \(\pi\), or you measured “forward” toward the caster&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Extrapolation error in a later node&lt;/td&gt;
      &lt;td&gt;Stamps in the future, or one node on sim time. Compare &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;header.stamp&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /clock&lt;/code&gt; only after Gazebo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tree has &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt; twice&lt;/td&gt;
      &lt;td&gt;Two publishers. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;view_frames&lt;/code&gt; is the picture; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /tf -v&lt;/code&gt; shows the nodes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Camera “turns the wrong way”&lt;/td&gt;
      &lt;td&gt;Image &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera_link&lt;/code&gt; while pixels are in the optical frame&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;This lab needs no new parts. When you bolt a real sensor on, the frame only becomes true if the bracket matches the numbers.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Shop&lt;/th&gt;
      &lt;th&gt;Search&lt;/th&gt;
      &lt;th&gt;2026 band&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;MPU6050, if you want an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imu_link&lt;/code&gt; later, not for this echo&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;GY-521 on Hshop&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Shopee/Lazada &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 30.000–70.000 VND&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Small 360° lidar (LD19 / LD06 / YDLIDAR X2 class), only when you reach Chapter 11&lt;/td&gt;
      &lt;td&gt;Search “lidar” on &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt;; the RPLIDAR S2 listing there is a much longer-range unit&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lidar LD19 robot&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YDLIDAR X2&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 1.500.000–3.500.000 VND for the small units. The S2-class scanner is several times that. Recheck the page&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Standoffs / bracket&lt;/td&gt;
      &lt;td&gt;any shop&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trụ đồng M3 20mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 15.000–40.000 VND a pack&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A single-point TF-Luna (&lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-dfrobot-tf-luna-tof-micro-single-point-ranging-lidar&quot;&gt;Hshop listing&lt;/a&gt;) is a range sensor, not a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LaserScan&lt;/code&gt; frame for SLAM. Do not buy it to satisfy this lesson.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Echo says translation &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[0.05, 0, 0.08]&lt;/code&gt; and you wanted the laser on the centreline. Which number is the sideways error? Guidance: the middle one, \(y\). Zero means centred.&lt;/li&gt;
  &lt;li&gt;Why may &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map → odom&lt;/code&gt; jump while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt; must not? Guidance: the map edge is a correction. The odom edge is what controllers integrate. A jump there looks like the robot teleported and the local planner kicks.&lt;/li&gt;
  &lt;li&gt;Two nodes publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;. You want both estimates. How do you name the second child? Guidance: not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;. Use something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link_ekf&lt;/code&gt; only if a single fusion node is the one publishing the real edge. A raw second stream can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom_enc&lt;/code&gt; as a different parent or a debug child that nothing else uses.&lt;/li&gt;
  &lt;li&gt;A scan &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame_id&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser_frame&lt;/code&gt; and the URDF says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;laser&lt;/code&gt;. What does TF report? Guidance: a disconnect. The names are the API. Pick one and change the other.&lt;/li&gt;
  &lt;li&gt;In one sentence, where does lesson 08-04’s \(v, \omega\) enter this tree? Guidance: integrate them to a pose and publish that pose as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom → base_link&lt;/code&gt;. The static laser edge does not contain velocity.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Tf2/Introduction-To-Tf2.html&quot;&gt;Introduction to TF2&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Tf2/Writing-A-Tf2-Static-Broadcaster-Py.html&quot;&gt;Static transform publisher&lt;/a&gt; if you would rather write the node than pass CLI flags.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; and &lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf2_echo&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;view_frames&lt;/code&gt; help text on your install. The CLI flags gained the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--x&lt;/code&gt; form in newer distros; Jazzy accepts it.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Launch files, parameters, and QoS</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter09/09_04_launch_params_qos/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter09/09_04_launch_params_qos</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~90 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Opening six terminal tabs and typing six &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run&lt;/code&gt; commands by hand works exactly once—the second time, you forget a flag, mismatch a wheel radius, and spend an hour chasing a phantom bug. Launch files, YAML parameter files, and Quality of Service (QoS) profiles turn a fragile collection of terminals into a single reproducible robot configuration.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will start two nodes from one Python launch file, override a parameter from that file and from the command line, and produce a QoS incompatibility on purpose so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/chatter&lt;/code&gt; looks alive while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo&lt;/code&gt; stays silent. You will read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info -v&lt;/code&gt; well enough to say which side offered best-effort and which side demanded reliable. That diagnosis is the one you will reuse when a lidar driver and RViz refuse to meet in Chapter 11.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 09-01 and 09-02: Jazzy is sourced, and you have already echoed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/chatter&lt;/code&gt; from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;demo_nodes_cpp&lt;/code&gt;. You can create a file under &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws/src&lt;/code&gt;. You do not need the robot. A parameter, for this lesson, is a named value that belongs to a node (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt;, a serial port, a track width), not a command-line flag of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-declarative-orchestration-parameterization-and-dds-qos-compatibility&quot;&gt;Key ideas: Declarative Orchestration, Parameterization, and DDS QoS Compatibility&lt;/h2&gt;

&lt;p&gt;Production ROS 2 systems separate code from configuration using three mechanisms:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Launch Files (Declarative Process Graphs):&lt;/strong&gt; A Python launch file starts multiple nodes, remaps topic names (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/raw_cmd&lt;/code&gt; \(\rightarrow\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;), loads YAML parameters, and groups nodes under namespaces (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/robot1/...&lt;/code&gt;) in one deterministic command.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;YAML Parameters (Single Source of Truth):&lt;/strong&gt; Storing hardware constants (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kp&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ki&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kd&lt;/code&gt;) in YAML rather than hard-coding them in Python/C++ allows identical node binaries to run in Gazebo simulation and on real hardware simply by swapping parameter files.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DDS Quality of Service (QoS) Contracts:&lt;/strong&gt; QoS policies—especially &lt;strong&gt;Reliability&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;) and &lt;strong&gt;Durability&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VOLATILE&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRANSIENT_LOCAL&lt;/code&gt;)—follow a strict request-vs-offered matching rule. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt; subscriber will silently refuse to connect to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt; publisher!
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Typing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run&lt;/code&gt; four times is how a demo starts. It is not how a Capstone base starts. The bridge, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, the twist teleop, and later Nav2 all have to come up together, with the track width from lesson 08-04 written down as a parameter instead of a magic number in three files. QoS is the other half. A laser scan is a stream that may drop a packet. A map is a document that must arrive. ROS 2 will not deliver a best-effort scan to a reliable subscriber, and it often fails quietly. People then “fix” Nav2 for an afternoon when the graph was simply incompatible. Learn the quiet failure on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/chatter&lt;/code&gt; before you meet it on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_qos.png&quot; alt=&quot;Compatible and incompatible QoS&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;launch file&lt;/strong&gt; describes a process graph. In Jazzy the file you will write is Python, using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;launch&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;launch_ros&lt;/code&gt;. It can start nodes, pass parameters, and include other launch files. XML launch still exists; this course uses Python because the official beginner tutorials do, and because you can compute a path with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PathJoinSubstitution&lt;/code&gt; instead of hoping a relative path survives &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;parameter&lt;/strong&gt; is a typed value on a node: bool, int, double, string, or arrays of those. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 param list /talker&lt;/code&gt; shows what that node declared. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 param get&lt;/code&gt; reads one. Setting a parameter from the command line (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--ros-args -p name:=value&lt;/code&gt;) overrides the default for that process only. A YAML file is how you keep a set of overrides for a robot:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;capstone_bridge&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ros__parameters&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/dev/ttyUSB0&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;baud&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;115200&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;track_width&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0.16&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;cmd_timeout_ms&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;300&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The key &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros__parameters&lt;/code&gt; is required. A file that is just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width: 0.16&lt;/code&gt; at the top level does not land on the node. The node name must match. Lesson 08-04’s measured \(L\) belongs here, once, and the bridge reads it. Two copies, one in centimetres, was an exercise in that lesson for a reason.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;QoS&lt;/strong&gt; is a contract on a publisher and on a subscription. The policies that bite beginners are reliability, durability, and history depth.&lt;/p&gt;

&lt;p&gt;Reliability: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reliable&lt;/code&gt; retries, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;best_effort&lt;/code&gt; does not. A reliable subscription is compatible with a reliable publisher. A best-effort subscription is compatible with both. A reliable subscription is &lt;strong&gt;not&lt;/strong&gt; compatible with a best-effort publisher. Sensor drivers (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/LaserScan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt;) usually publish best-effort with a small queue, the profile people call sensor data. RViz’s default LaserScan display has, at times, expected reliable. The fix is to change the display QoS or the subscription, not to reboot the lidar.&lt;/p&gt;

&lt;p&gt;Durability: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; keeps no history for late joiners. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transient_local&lt;/code&gt; does, which is how a map published once can still be seen by a node that starts later. A volatile publisher does not satisfy a transient-local subscriber.&lt;/p&gt;

&lt;p&gt;History depth is the queue length. Depth 1 means “only the latest scan matters,” which is what you want for a 10 Hz lidar and what you do not want for a burst of short commands if you also refuse to let the watchdog expire them.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /scan -v&lt;/code&gt; prints the offered and requested profiles side by side. Incompatible endpoints show up as publishers and subscriptions that do not list each other as matched. Echo with the default QoS can sit there printing nothing. That is the lab.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Create a package and a launch file. From the sourced workspace:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~/ros2_ws/src
ros2 pkg create capstone_bringup &lt;span class=&quot;nt&quot;&gt;--build-type&lt;/span&gt; ament_python &lt;span class=&quot;nt&quot;&gt;--dependencies&lt;/span&gt; launch_ros
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; capstone_bringup/launch capstone_bringup/config
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bringup/launch/talker_listener.launch.py&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LaunchDescription&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;launch_ros.actions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Node&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;generate_launch_description&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LaunchDescription&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;demo_nodes_cpp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;executable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;talker&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;talker&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;use_sim_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}],&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;demo_nodes_cpp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;executable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;listener&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;listener&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Install rule: in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup.py&lt;/code&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data_files&lt;/code&gt; list must include the launch directory, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 launch&lt;/code&gt; will not see the file after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon build&lt;/code&gt;. A typical entry is:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glob&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glob&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# inside data_files:
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;share&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;package_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;launch&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;glob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;launch/*.launch.py&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~/ros2_ws
colcon build &lt;span class=&quot;nt&quot;&gt;--packages-select&lt;/span&gt; capstone_bringup
&lt;span class=&quot;nb&quot;&gt;source install&lt;/span&gt;/setup.bash
ros2 launch capstone_bringup talker_listener.launch.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt; one terminal prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Publishing: &apos;Hello World: N&apos;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I heard: [Hello World: N]&lt;/code&gt;. Ctrl-C stops both nodes. That is the point of launch.&lt;/p&gt;

&lt;p&gt;Parameter check, talker still running in another launch or via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 run&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 param get /talker use_sim_time
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Boolean value is: False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;QoS break, which you do on purpose. Terminal A publishes best-effort:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub /chatter std_msgs/msg/String &lt;span class=&quot;s2&quot;&gt;&quot;{data: hi}&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--qos-reliability&lt;/span&gt; best_effort
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Terminal B echoes with the default, which is reliable:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /chatter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt; the publisher counts messages, the echo prints nothing. Confirm:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic info /chatter &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You should see a publisher reliability of best effort and a subscription reliability of reliable, and no indication that they are connected. Now echo with a matching policy:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /chatter &lt;span class=&quot;nt&quot;&gt;--qos-reliability&lt;/span&gt; best_effort
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data: hi&lt;/code&gt; starts appearing. Put that pair of commands in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. It is the template for “RViz shows no scan.”&lt;/p&gt;

&lt;h3 id=&quot;explanation-checking-the-dds-qos-compatibility-matrix-by-hand&quot;&gt;Explanation: Checking the DDS QoS Compatibility Matrix by Hand&lt;/h3&gt;

&lt;p&gt;The most common “my topic is publishing, why is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo&lt;/code&gt; empty?” mystery in ROS 2 is a QoS mismatch. Check the compatibility table below (note `ert` is avoided; pure text cells):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Publisher (Offered)&lt;/th&gt;
      &lt;th&gt;Subscriber (Requested)&lt;/th&gt;
      &lt;th&gt;Matches?&lt;/th&gt;
      &lt;th&gt;Typical Robotics Use&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Yes&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt;, state transitions&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Yes&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Diagnostic viewer tapping a reliable topic&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Yes&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;High-rate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/imu/data&lt;/code&gt;, camera frames&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEST_EFFORT&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RELIABLE&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;NO (silent drop!)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;Classic bug: sensor QoS pub + default sub&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Durability check:&lt;/strong&gt; A map server publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt; &lt;em&gt;once&lt;/em&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRANSIENT_LOCAL&lt;/code&gt; durability so late-joining Nav2 nodes still receive the map. If your subscriber asks for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRANSIENT_LOCAL&lt;/code&gt; from a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VOLATILE&lt;/code&gt; publisher, DDS rejects the connection—always verify with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info -v &amp;lt;topic&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Build &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bringup&lt;/code&gt; and launch the talker and listener together. Save the log line that proves the listener heard the talker.&lt;/li&gt;
  &lt;li&gt;Add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bringup/config/bridge.yaml&lt;/code&gt; with the four keys in the concepts section (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;port&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;baud&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_timeout_ms&lt;/code&gt;). You will not have the bridge node yet. Validate the YAML loads as text: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3 -c &quot;import yaml; print(yaml.safe_load(open(&apos;config/bridge.yaml&apos;)))&quot;&lt;/code&gt; from the package directory. &lt;strong&gt;Expected:&lt;/strong&gt; a dict whose only top key is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bridge&lt;/code&gt;, and inside it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros__parameters&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Run the best-effort versus reliable experiment. Record &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info -v&lt;/code&gt; for the broken case and for the fixed echo.&lt;/li&gt;
  &lt;li&gt;Optional: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic hz /chatter&lt;/code&gt; on the working echo. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;topic pub&lt;/code&gt; without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--rate&lt;/code&gt; is slow. Pass &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r 10&lt;/code&gt; and expect a rate near 10.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file not found&lt;/code&gt; on launch&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup.py&lt;/code&gt; does not install &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;launch/&lt;/code&gt;, or you forgot to source the overlay after the build&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Listener silent, talker fine, QoS not the issue&lt;/td&gt;
      &lt;td&gt;You launched two talkers and no listener, or the listener node crashed on a parameter type error&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;YAML “loads” but the node ignores &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Missing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros__parameters&lt;/code&gt;, or the node name in the file does not match&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Echo works in one terminal and not another&lt;/td&gt;
      &lt;td&gt;One of them passed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--qos-reliability&lt;/code&gt;. Defaults differ from what you think&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; true and the talker freezes later&lt;/td&gt;
      &lt;td&gt;Nothing is publishing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/clock&lt;/code&gt;. Leave it false until Gazebo is in Chapter 10&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;No parts. The laptop from lesson 09-01 is the instrument. If you already bought a Pi 5 to be the robot computer, do this lab on the laptop first, then repeat &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; on the Pi. Do not debug QoS and a flaky microSD card on the same afternoon.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Your bridge YAML sets &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_timeout_ms: 300&lt;/code&gt; and the firmware also uses 300 ms. Which one fires if the laptop process dies but the USB cable stays plugged in? Guidance: the firmware watchdog. The YAML timeout only helps the bridge decide to &lt;em&gt;send&lt;/em&gt; zeros. If the bridge process is dead, it sends nothing, and the MCU timer is the one that stops the wheels.&lt;/li&gt;
  &lt;li&gt;A classmate publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; best-effort and opens RViz with reliability set to reliable. Quote the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;topic info -v&lt;/code&gt; line that settles the argument. Guidance: publisher reliability BEST_EFFORT, subscription RELIABLE, endpoints not matched.&lt;/li&gt;
  &lt;li&gt;Why is transient-local a reasonable durability for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/map&lt;/code&gt; and a poor one for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;? Guidance: a late RViz should still see the last map. A late bridge should not act on a Twist from ten minutes ago.&lt;/li&gt;
  &lt;li&gt;You want the launch file to refuse to start when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt; is missing. Is that a QoS problem? Guidance: no. It is a parameter declaration. The node should declare &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;track_width&lt;/code&gt; without a silent default of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.0&lt;/code&gt;, or the launch file should pass it explicitly.&lt;/li&gt;
  &lt;li&gt;Order these in a future &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone.launch.py&lt;/code&gt;: serial bridge, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot_state_publisher&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;teleop_twist_keyboard&lt;/code&gt;. What must be a parameter, not a hard-coded string? Guidance: the serial port and the track width. Teleop can start last; it only publishes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Launch/Launch-Main.html&quot;&gt;Launching multiple nodes&lt;/a&gt; and the &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Launch/Creating-Launch-Files.html&quot;&gt;creating a launch file&lt;/a&gt; tutorial.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Parameters/Understanding-ROS2-Parameters.html&quot;&gt;Understanding parameters&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Quality-of-Service-Settings.html&quot;&gt;About Quality of Service settings&lt;/a&gt;. Read the compatibility tables, not only the names.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo -h&lt;/code&gt; on your machine. The QoS flags in that help text are the ones the lab used.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Services and actions</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter09/09_03_services_actions/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter09/09_03_services_actions</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~75 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Topics are ideal for continuous streams like wheel speeds and laser scans, but terrible for saying “calibrate the IMU and tell me when you’re done” or “drive 5 meters down the hallway and let me abort if a dog runs in front of you.” ROS 2 provides &lt;strong&gt;services&lt;/strong&gt; for short request–response transactions and &lt;strong&gt;actions&lt;/strong&gt; for long-running, cancellable goals with live feedback.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will call an add-two-ints service and read a single response, then send a Fibonacci action goal and point at feedback versus the final result. You will choose among topic, service, and action for four robot jobs: streaming wheel speed, zeroing encoders, driving to a pose, and publishing a laser scan. Nav2’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateToPose&lt;/code&gt; should feel like the Fibonacci demo with a map attached, not like a new religion.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 09-02’s talker is comfortable. You can leave a process running in one terminal and type in another. No robot.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-synchronous-rpc-vs-preemptible-state-machines-in-distributed-robotics&quot;&gt;Key ideas: Synchronous RPC vs. Preemptible State Machines in Distributed Robotics&lt;/h2&gt;

&lt;p&gt;Choosing between topics, services, and actions matches the communication pattern to the task’s time scale:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Services (Request–Response RPC):&lt;/strong&gt; Short, blocking or async-future calls (milliseconds) that return a single reply—used for mode switches, parameter resets, or one-shot triggers like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/reset_odometry&lt;/code&gt;. Calling a slow service inside a control callback blocks the executor and starves timers.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Actions (Goal–Feedback–Result State Machine):&lt;/strong&gt; Under the hood, a ROS 2 Action is built from three services (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;send_goal&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cancel_goal&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_result&lt;/code&gt;) plus two topics (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;feedback&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status&lt;/code&gt;). It manages an explicit goal lifecycle: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ACCEPTED -&amp;gt; EXECUTING -&amp;gt; CANCELING -&amp;gt; CANCELED / SUCCEEDED / ABORTED&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cooperative Cancellation:&lt;/strong&gt; Sending a cancel request does not magically freeze motors; the Action Server must periodically poll &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;goal_handle.is_cancel_requested&lt;/code&gt;, ramp velocity to zero safely, and report &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CANCELED&lt;/code&gt;.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Streaming &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; on a topic is the right tool because a newer command should replace an older one and nobody should wait for a reply before the next 50 ms tick. “Are you at the goal?” is a bad topic if you also need cancel and progress. That is an action, and it is how Nav2 will take a pose in Chapter 11. A service is the short question: set a bool, trigger a calibration, return a sum. People who put a five-minute drive on a service discover that Ctrl-C is their only cancel. The Capstone watchdog stays a firmware timeout either way; an action cancel should also publish a zero Twist, or the wheels keep the last command.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_svc_action.png&quot; alt=&quot;Service reply versus an action timeline&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;service&lt;/strong&gt; is a request and a response. The client sends one message, the server sends one message, the call returns. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;example_interfaces/srv/AddTwoInts&lt;/code&gt; takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; and returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sum&lt;/code&gt;. Use it when the work is short and the caller can wait. Do not use it for a stream. A service server that blocks inside a motor loop will miss the deadline from lesson 08-05.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;action&lt;/strong&gt; is a goal, a stream of feedback, and one result, plus cancel. The Fibonacci tutorial sends an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order&lt;/code&gt; and feeds back the sequence as it grows. You can cancel a goal that is taking too long. Nav2’s navigate action has the same shape: goal pose in, feedback (distance remaining, current pose) along the way, result (success or abort) at the end. Recoveries live inside the behavior tree, which Chapter 11 only peeks at. You do not need that tree to see why an action exists.&lt;/p&gt;

&lt;p&gt;A practical split for this robot:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Job&lt;/th&gt;
      &lt;th&gt;Use&lt;/th&gt;
      &lt;th&gt;Why&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheel command at 10–50 Hz&lt;/td&gt;
      &lt;td&gt;Topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Latest message wins. No reply required&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Laser scan&lt;/td&gt;
      &lt;td&gt;Topic&lt;/td&gt;
      &lt;td&gt;Stream. Often best-effort QoS&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;“Zero the encoder counters”&lt;/td&gt;
      &lt;td&gt;Service&lt;/td&gt;
      &lt;td&gt;One question, one answer, milliseconds&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;“Drive to this pose”&lt;/td&gt;
      &lt;td&gt;Action&lt;/td&gt;
      &lt;td&gt;Minutes, cancel, feedback&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Battery voltage&lt;/td&gt;
      &lt;td&gt;Topic&lt;/td&gt;
      &lt;td&gt;Stream, slow is fine&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Cancelling an action does not by itself cut MOSFET gates. Your bridge must treat “no recent Twist” as stop. That rule already exists in the serial timeout. Keep it when the Twist starts coming from an action server’s controller instead of your keyboard.&lt;/p&gt;

&lt;h2 id=&quot;cancel-is-a-message-not-a-wish&quot;&gt;Cancel is a message, not a wish&lt;/h2&gt;

&lt;p&gt;The Fibonacci server keeps computing until the goal finishes or a cancel arrives. Watch the server terminal while you send &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order: 20&lt;/code&gt;. The feedback sequence grows. Ctrl-C on the &lt;em&gt;client&lt;/em&gt; cancels that goal in the CLI; the server process should still be alive and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 action list&lt;/code&gt; should still show &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/fibonacci&lt;/code&gt;. If the only way you know to stop a goal is to kill the server, you do not have cancel, you have a crash.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 action send_goal /fibonacci action_tutorials_interfaces/action/Fibonacci &lt;span class=&quot;s2&quot;&gt;&quot;{order: 20}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Interrupt it. Then immediately:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 action list
ros2 node list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/fibonacci&lt;/code&gt; remains, and the server node remains. A second short goal, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order: 5&lt;/code&gt;, should still return a result. That is the behavior you want from Nav2 later: one failed or cancelled navigate does not require you to restart the whole stack. It is also the behavior you do &lt;em&gt;not&lt;/em&gt; get from a service. There is no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 service cancel&lt;/code&gt;. The client waits, or you kill it, and the server may still be inside the call.&lt;/p&gt;

&lt;p&gt;Put the motor consequence in the same note. Cancelling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateToPose&lt;/code&gt; stops the action server from sending new feedback. It does not, by itself, force the last &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; to zero unless the controller or your bridge does that. The 300 ms firmware timeout from Chapter 08 is the backstop. An action-aware bridge publishes zeros on cancel &lt;em&gt;and&lt;/em&gt; on silence. Writing only one of those is how a robot creeps after you pressed stop in RViz.&lt;/p&gt;

&lt;h3 id=&quot;explanation-tracing-an-action-goal-lifecycle-and-feedback-budget&quot;&gt;Explanation: Tracing an Action Goal Lifecycle and Feedback Budget&lt;/h3&gt;

&lt;p&gt;Consider a custom action &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DriveDistance&lt;/code&gt; commanded to move \(d_{\text{goal}} = 2.00\ \text{m}\) at \(v = 0.25\ \text{m/s}\), publishing feedback at \(5\ \text{Hz}\) (every \(0.20\ \text{s}\)):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Goal acceptance (\(t = 0.0\ \text{s}\)):&lt;/strong&gt; Client sends &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target_m = 2.00&lt;/code&gt;. Server validates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0 &amp;lt; target_m &amp;lt;= 5.0&lt;/code&gt;, replies &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ACCEPTED&lt;/code&gt;, and transitions to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EXECUTING&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Feedback stream:&lt;/strong&gt; Every \(0.20\ \text{s}\), the robot advances \(\Delta d = 0.25 \times 0.20 = 0.05\ \text{m}\). At \(t = 2.0\ \text{s}\), feedback reports &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;traveled_m = 0.50&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;remaining_m = 1.50&lt;/code&gt; (25% complete).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mid-run cancellation at \(t = 3.2\ \text{s}\) (\(d = 0.80\ \text{m}\)):&lt;/strong&gt; Suppose an obstacle appears and the client calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cancel_goal_async()&lt;/code&gt;.
    &lt;ul&gt;
      &lt;li&gt;Within one feedback tick (\(\le 0.20\ \text{s}\)), the server sees &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is_cancel_requested == True&lt;/code&gt;, commands &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel = (0, 0)&lt;/code&gt;, transitions &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EXECUTING -&amp;gt; CANCELING -&amp;gt; CANCELED&lt;/code&gt;, and returns result &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;final_distance = 0.84 m, success = False&lt;/code&gt;.&lt;/li&gt;
      &lt;li&gt;Contrast this with a &lt;strong&gt;service&lt;/strong&gt;: once called, a blocking service has &lt;em&gt;no&lt;/em&gt; standard cancel channel and &lt;em&gt;no&lt;/em&gt; intermediate feedback for the full 8-second drive.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Service, two terminals. Tutorial: &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Services/Understanding-ROS2-Services.html&quot;&gt;Understanding services&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run demo_nodes_cpp add_two_ints_server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 service list
ros2 service &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; /add_two_ints
ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts &lt;span class=&quot;s2&quot;&gt;&quot;{a: 2, b: 3}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;example_interfaces/srv/AddTwoInts
requester: making request: example_interfaces.srv.AddTwoInts_Request(a=2, b=3)
response:
example_interfaces.srv.AddTwoInts_Response(sum=5)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If the server terminal is not running, the call waits. That wait is the reason a lost motor service feels like a hung teleop. Ctrl-C the client.&lt;/p&gt;

&lt;p&gt;Action, two terminals. Tutorial: &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Actions/Understanding-ROS2-Actions.html&quot;&gt;Understanding actions&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run action_tutorials_py fibonacci_action_server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 action list
ros2 action info /fibonacci
ros2 action send_goal /fibonacci action_tutorials_interfaces/action/Fibonacci &lt;span class=&quot;s2&quot;&gt;&quot;{order: 5}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt; feedback lines with a growing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sequence&lt;/code&gt;, then a result whose last numbers are the fifth Fibonacci step (the sequence includes the partial sums the server defines; read the result block rather than memorising a blog’s list). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 action send_goal&lt;/code&gt; also prints a goal id. Send order 20 and cancel is the point: in another terminal, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 action list&lt;/code&gt; still shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/fibonacci&lt;/code&gt;, and you can interrupt the client. The server terminal should not have to be killed to stop one goal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Service call sits forever&lt;/td&gt;
      &lt;td&gt;Server not running, or wrong service name&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package &apos;action_tutorials_py&apos; not found&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Desktop metapackage incomplete. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt install ros-jazzy-action-tutorials-py&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Feedback never arrives, result does&lt;/td&gt;
      &lt;td&gt;You used a client that hides feedback. The CLI &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;send_goal&lt;/code&gt; shows it&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You mapped “drive 2 m” to a service&lt;/td&gt;
      &lt;td&gt;Cancel and progress have nowhere to live. Make it an action, and still timeout the motors&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;Nothing. This is a laptop lab.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Classify “publish battery voltage ten times a second.” Guidance: topic.&lt;/li&gt;
  &lt;li&gt;Classify “Nav2, go to the kitchen.” Guidance: action. Name you will look up later: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigateToPose&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Your service call hangs for 30 seconds. What is the first process you check? Guidance: the server, with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 service list&lt;/code&gt; and the server terminal, before you reinstall ROS.&lt;/li&gt;
  &lt;li&gt;Why must an action cancel still produce a zero Twist on the Capstone? Guidance: the firmware holds the last wheel command until the 300 ms watchdog. Cancel has to be followed by silence or an explicit zero, or the robot coasts on the old PWM until the watchdog saves you.&lt;/li&gt;
  &lt;li&gt;Fibonacci &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order: 5&lt;/code&gt; is a stand-in. What is the “order” field analogous to on a mobile base? Guidance: the goal pose, not the PWM.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Services/Understanding-ROS2-Services.html&quot;&gt;Understanding services&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Actions/Understanding-ROS2-Actions.html&quot;&gt;Understanding actions&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.nav2.org/jazzy/getting_started/&quot;&gt;Nav2 getting started (Jazzy)&lt;/a&gt; for the navigate action you are not running yet.&lt;/li&gt;
  &lt;li&gt;The interface definition &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action_tutorials_interfaces/action/Fibonacci&lt;/code&gt; via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 interface show&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Nodes, topics, and messages</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter09/09_02_nodes_topics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter09/09_02_nodes_topics</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~75 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Imagine an orchestra where the violinist never waits for the drummer to finish a bar before reading the sheet music, yet everyone stays tightly synchronized through a shared tempo. That is the ROS 2 computation graph: independent &lt;strong&gt;nodes&lt;/strong&gt; publishing typed &lt;strong&gt;messages&lt;/strong&gt; over named &lt;strong&gt;topics&lt;/strong&gt;, decoupled in both time and code so you can swap a real LiDAR for a Gazebo simulator without changing a single line of your planner.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will start a talker and a listener from the Jazzy demos, name the topic and the message type that connect them, and read a rate with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic hz&lt;/code&gt;. You will publish one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; by hand and point at the two fields a differential-drive base actually uses. You will also say what an interface package is, so a later custom wheel message is a package and not a Python dict you hope the other node guesses.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 09-01: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt; in the terminal you are about to use. Chapter 08’s kinematics: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; will mean \(v\) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; will mean \(\omega\). No robot is required.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-computation-graphs-strongly-typed-messages-and-rate-contracts&quot;&gt;Key ideas: Computation Graphs, Strongly Typed Messages, and Rate Contracts&lt;/h2&gt;

&lt;p&gt;ROS 2 topics formalize three architectural ideas that scale from a 2-node rover to a 200-node autonomous car:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Directed Computation Graph:&lt;/strong&gt; Nodes are independent OS processes (or composable threads); topics are named, many-to-many asynchronous buses. Because publishers and subscribers never hold direct pointers to each other, any node can be recorded (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 bag&lt;/code&gt;), replayed, or replaced live.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Strongly Typed IDL Interfaces:&lt;/strong&gt; Standard messages like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt; enforce exact field names, types, and SI units (metres, radians, seconds — REP-103), eliminating the ad-hoc CSV parsing bugs of custom scripts.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Rate &amp;amp; Latency Contracts:&lt;/strong&gt; Every topic carries an implicit timing contract: control commands (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; at 20–50 Hz), odometry (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; at 20–50 Hz), and scans (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scan&lt;/code&gt; at 5–20 Hz). Knowing message size and publish rate lets you calculate network load before saturating Wi-Fi.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Capstone bridge is a node. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; is a topic. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; is a message. Until those three words are boring, Gazebo, Nav2, and a camera pipeline are just more nodes you cannot see. A learned policy that “publishes actions” is the same picture with a different message. If you cannot draw the talker–topic–listener graph, you cannot debug a silent robot.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_ros_graph.png&quot; alt=&quot;Talker, topic, listener&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;node&lt;/strong&gt; is one process (or one object inside a process) with a name in a graph. A &lt;strong&gt;topic&lt;/strong&gt; is a named bus. A &lt;strong&gt;publisher&lt;/strong&gt; writes messages of one type onto that bus. A &lt;strong&gt;subscription&lt;/strong&gt; reads them. Publishers do not call subscribers by name. Many subscribers may listen, which is why a logger and a motor bridge can both hear &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The message type is part of the contract. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std_msgs/msg/String&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt; are different types. A subscriber of one will not connect to a publisher of the other, even if the topic string matches. Interfaces live in packages. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 interface show geometry_msgs/msg/Twist&lt;/code&gt; prints the fields. For this course the living fields are:&lt;/p&gt;

\[v = \texttt{linear.x}, \qquad \omega = \texttt{angular.z}\]

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; stays 0. A non-zero &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt; is a sideways request the base cannot perform (lesson 08-04).&lt;/p&gt;

&lt;p&gt;Discovery is not instant magic across a hostile network, but on one laptop the demo nodes appear in a second. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 node list&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /chatter&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic echo /chatter&lt;/code&gt; are the four commands you will use until they are muscle memory. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic hz /chatter&lt;/code&gt; is how you learn that “it is publishing” has a rate, the same way lesson 08-05 had a period.&lt;/p&gt;

&lt;h2 id=&quot;a-rate-is-part-of-the-contract&quot;&gt;A rate is part of the contract&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic hz /chatter&lt;/code&gt; on the demo talker settles near 1 Hz. That number is not decoration. Lesson 08-05 already taught you that a motor loop which claims 50 Hz and delivers 12 Hz makes the derivative term lie. A topic has the same kind of honesty. When the Capstone bridge exists, you will subscribe to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; and you will also ask &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hz&lt;/code&gt;. A teleop keyboard that publishes only when a key changes can sit at 0 Hz while you are not touching it. The firmware watchdog then stops the wheels, which is correct, and it surprises people who thought “the topic exists” meant “the command is fresh.”&lt;/p&gt;

&lt;p&gt;Try this once the talker is running:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic hz /chatter
ros2 topic bw /chatter
ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /chatter &lt;span class=&quot;nt&quot;&gt;--field&lt;/span&gt; data
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;average rate: 1.000&lt;/code&gt; or very near it, a bandwidth of a few bytes per second, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--field&lt;/code&gt; form printing only the string, without the YAML wrapper. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bw&lt;/code&gt; on a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; later will be small too. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bw&lt;/code&gt; on a camera image will not. If you ever bridge images over a weak link, this command is how you notice before you blame the detector.&lt;/p&gt;

&lt;p&gt;A second concrete message, which you already met as a one-shot publish, deserves a sustained rate. In a terminal:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; 5 /cmd_vel geometry_msgs/msg/Twist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;{linear: {x: 0.0}, angular: {z: 0.0}}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In another:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic hz /cmd_vel
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt; about 5 Hz, and the robot still does not move, because nothing subscribes with a motor. Leave this running for ten seconds and then Ctrl-C. Write down the rate. Chapter 10’s bridge will treat “no message for 300 ms” as stop. At 5 Hz the gap between messages is 200 ms, inside that window. At 1 Hz the gap is 1000 ms and the watchdog will chop the command into pulses. That is a kinematics-and-timing bug you can now predict without the robot on the floor.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-topic-bandwidth-for-telemetry-vs-sensors&quot;&gt;Explanation: Hand-Calculating Topic Bandwidth for Telemetry vs. Sensors&lt;/h3&gt;

&lt;p&gt;Before streaming topics over Wi-Fi between an onboard SBC and a laptop, estimate their exact wire throughput:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Command topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt;):&lt;/strong&gt; Contains 6 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float64&lt;/code&gt; fields (\(6 \times 8 = 48\ \text{bytes}\) payload, \(\approx 80\ \text{bytes}\) with DDS headers). At \(50\ \text{Hz}\):
\(\text{BW}_{\text{cmd}} = 80 \times 50 = 4\,000\ \text{B/s} = 32\ \text{kbps}\)
— virtually zero load on Wi-Fi.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;2D LiDAR &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/scan&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/msg/LaserScan&lt;/code&gt;):&lt;/strong&gt; Suppose 720 range floats + 720 intensity floats (\(1440 \times 4 = 5\,760\ \text{bytes}\)) at \(10\ \text{Hz}\):
\(\text{BW}_{\text{scan}} = 5\,760 \times 10 \approx 57.6\ \text{kB/s} \approx 0.46\ \text{Mbps}\)
— still easy for Wi-Fi.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Raw RGB camera &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/image_raw&lt;/code&gt; (640×480×3 bytes = 921,600 bytes/frame) at \(30\ \text{Hz}\):&lt;/strong&gt;
\(\text{BW}_{\text{raw}} = 921\,600 \times 30 = 27.65\ \text{MB/s} = 221\ \text{Mbps}!\)
That single uncompressed camera topic will saturate typical 2.4 GHz Wi-Fi and starve &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;—proving why raw images should stay local on the robot or use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compressed&lt;/code&gt; transport.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Terminal A, after sourcing Jazzy:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 run demo_nodes_cpp talker
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected&lt;/strong&gt;, repeating:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[INFO] [talker]: Publishing: &apos;Hello World: 1&apos;
[INFO] [talker]: Publishing: &apos;Hello World: 2&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Terminal B:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; /chatter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;data: &apos;Hello World: 3&apos;
---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Terminal C:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 node list
ros2 topic info /chatter
ros2 topic hz /chatter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected shape:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/talker
/listener   # only if you also ran demo_nodes_cpp listener
Type: std_msgs/msg/String
average rate: 1.000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The talker publishes about 1 Hz. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hz&lt;/code&gt; says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;no messages&lt;/code&gt;, you echoed the wrong name or the talker’s terminal died.&lt;/p&gt;

&lt;p&gt;Stop the talker with Ctrl-C. Publish a single Twist, which is the dry run of the bridge:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic pub &lt;span class=&quot;nt&quot;&gt;--once&lt;/span&gt; /cmd_vel geometry_msgs/msg/Twist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;{linear: {x: 0.20, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.10}}&quot;&lt;/span&gt;
ros2 topic &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--once&lt;/span&gt; /cmd_vel
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nothing moves. There is no subscriber with a motor. That silence is correct. Write in your notes: “0.20 m/s forward, 0.10 rad/s left, no bridge yet.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Package &apos;demo_nodes_cpp&apos; not found&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Desktop install missing, or this shell is not sourced&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Echo never prints&lt;/td&gt;
      &lt;td&gt;Topic name typo, or QoS mismatch (next lessons). Demos match each other&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hz&lt;/code&gt; is far from 1&lt;/td&gt;
      &lt;td&gt;Another publisher on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/chatter&lt;/code&gt;, or a overloaded VM&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Twist pub errors on YAML&lt;/td&gt;
      &lt;td&gt;Quotes. Copy the command as one line. Colons and braces are YAML&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You expected the ESP32 to twitch&lt;/td&gt;
      &lt;td&gt;It is not on this graph yet. Chapter 10 builds the subscriber&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;No hardware. Optional later: the Pi 5 from lesson 09-01 if you want this graph off the laptop. Do not buy a “ROS robot kit” this week to see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/chatter&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;In one sentence, why can two terminals echo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/chatter&lt;/code&gt; at the same time? Guidance: a topic is a bus, not a phone call.&lt;/li&gt;
  &lt;li&gt;Which field of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Twist&lt;/code&gt; must stay near zero on the Capstone base, and why? Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.y&lt;/code&gt;. The mechanism has no sideways speed.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic info /cmd_vel&lt;/code&gt; after your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--once&lt;/code&gt; pub shows no publisher. Is that a bug? Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--once&lt;/code&gt; exits. The info is a snapshot of who is alive now.&lt;/li&gt;
  &lt;li&gt;Sketch the future graph: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;teleop_twist_keyboard&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone_bridge&lt;/code&gt; → serial frame. Name the message on the topic. Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/msg/Twist&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;What does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 interface show&lt;/code&gt; protect you from? Guidance: guessing field names. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; is yaw, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.yaw&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Nodes/Understanding-ROS2-Nodes.html&quot;&gt;Understanding nodes&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html&quot;&gt;Understanding topics&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/msg/Twist.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Lesson 08-04 in this course for the inverse that turns this message into wheel speeds.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Install ROS 2 Jazzy and a workspace</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter09/09_01_jazzy_install_ws/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter09/09_01_jazzy_install_ws</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~90 minutes&lt;/strong&gt;, longer if Ubuntu itself is new.&lt;/p&gt;

&lt;p&gt;Up to Chapter 08, your entire robot lived inside one microcontroller sketch and one Python script. The moment you add a LiDAR, a camera, SLAM, and a path planner, a single monolithic program collapses under its own weight. ROS 2 Jazzy solves this by turning a robot into a &lt;strong&gt;composable operating system of independent processes&lt;/strong&gt;—and your first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt; workspace is the foundation where every future package lives.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will install ROS 2 Jazzy Jalisco on Ubuntu 24.04 from the official deb packages, create an overlay workspace with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt;, and show that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; works in a fresh terminal only after you source the underlay and the overlay in the right order. You will recognise a mixed Humble/Jazzy environment from the error text, and you will write a one-line note about where this laptop sits relative to the Capstone MCU.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can use a terminal: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cd&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt&lt;/code&gt;, and a text editor. Ubuntu 24.04 (Noble) is the Tier-1 platform for Jazzy. A virtual machine or a second-hand mini PC is enough. The ESP32 from Chapters 02–08 stays on the robot; it does not run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt;. Chapter 08’s serial frame is the thing this computer will speak later. You do not need a lidar or a Pi for this lesson.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-modular-robotics-middleware-overlay-workspaces-and-build-systems&quot;&gt;Key ideas: Modular Robotics Middleware, Overlay Workspaces, and Build Systems&lt;/h2&gt;

&lt;p&gt;Setting up ROS 2 cleanly relies on three software-engineering concepts that prevent 90% of beginner environment issues:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Underlay vs. Overlay Workspaces:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt; is the immutable system &lt;em&gt;underlay&lt;/em&gt; containing standard packages. Your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws&lt;/code&gt; is an &lt;em&gt;overlay&lt;/em&gt; layered on top via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source install/setup.bash&lt;/code&gt;: packages in your overlay shadow identically named packages in the underlay without ever modifying system files.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Build &amp;amp; Dependency Graph (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ament&lt;/code&gt;):&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt; inspects every package’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package.xml&lt;/code&gt;, topologically sorts dependencies, and builds C++ (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ament_cmake&lt;/code&gt;) and Python (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ament_python&lt;/code&gt;) packages into isolated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;build/&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;install/&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log/&lt;/code&gt; trees.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DDS Domain Isolation (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt;):&lt;/strong&gt; ROS 2 uses Data Distribution Service (DDS) multicast discovery so nodes find each other automatically on a local network. Setting a unique &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; (0–101) prevents your laptop from accidentally commanding a classmate’s robot across the room.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capstone A proved the base moves under firmware. Jazzy is the messaging and tooling layer you will put beside that firmware, not a replacement for the motor watchdog. Every later command in Chapters 09–12 assumes one sourced distro. A workspace that sometimes sees Humble messages and sometimes Jazzy messages wastes a weekend that should have gone to TF and the bridge. Simulation in Chapter 10 uses the same install plus &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-ros-gz&lt;/code&gt;. Nav2 and LeRobot do not get a turn until &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; is boring.&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;ROS 2 is a set of libraries and command-line tools on top of DDS. A &lt;strong&gt;distro&lt;/strong&gt; is a frozen snapshot. Jazzy Jalisco targets Ubuntu 24.04. Humble targets 22.04. They are not drop-in substitutes, and sourcing both in one shell is a broken shell.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;underlay&lt;/strong&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt;, installed by apt. The &lt;strong&gt;overlay&lt;/strong&gt; is a workspace you build with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon&lt;/code&gt;, usually &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws&lt;/code&gt;. You source the underlay first, then the overlay. The overlay’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup.bash&lt;/code&gt; remembers the underlay it was built against. Open a new terminal and you are back to a naked shell until your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt; sources them again.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2&lt;/code&gt; is the command. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;topic list&lt;/code&gt; talks to a daemon that discovers nodes. An empty list is a valid result: it means “no topics yet,” not “install failed,” as long as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 doctor&lt;/code&gt; or a talker from the next lesson can run.&lt;/p&gt;

&lt;p&gt;Locale must be UTF-8 before you add the apt source. The official install fails early, with a clear message, when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;locale&lt;/code&gt; is POSIX. Fix that once.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch09_workspace.png&quot; alt=&quot;Underlay then overlay&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Windows and macOS are not the path this course debugs. If your only machine is Windows, install Ubuntu 24.04 in a virtual machine with at least 4 GB of RAM and 30 GB of disk, or use a spare PC. WSL2 can work and also fails in ways this lesson will not chase (GUI, USB serial, Gazebo). A native Ubuntu install is the lab that matches the docs.&lt;/p&gt;

&lt;h3 id=&quot;explanation-tracing-environment-resolution-and-dds-port-mapping-by-hand&quot;&gt;Explanation: Tracing Environment Resolution and DDS Port Mapping by Hand&lt;/h3&gt;

&lt;p&gt;Understanding how ROS 2 resolves packages and network ports turns mysterious “package not found” or “nodes can’t see each other” bugs into two-second checks:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Overlay path precedence (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMENT_PREFIX_PATH&lt;/code&gt;):&lt;/strong&gt; When you run:
    &lt;ul&gt;
      &lt;li&gt;First: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source /opt/ros/jazzy/setup.bash&lt;/code&gt; \(\rightarrow\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMENT_PREFIX_PATH=/opt/ros/jazzy&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Second: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source ~/ros2_ws/install/setup.bash&lt;/code&gt; \(\rightarrow\) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMENT_PREFIX_PATH=~/ros2_ws/install:/opt/ros/jazzy&lt;/code&gt;
ROS 2 searches left-to-right. If you source them in the &lt;em&gt;reverse&lt;/em&gt; order, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt; shadows your own edits!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;How &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; maps to UDP multicast discovery port:&lt;/strong&gt; Per the RTPS/DDS standard, the discovery port for domain \(D\) is:
\(\text{Port}(D) = 7400 + 250 \times D.\)
    &lt;ul&gt;
      &lt;li&gt;Default domain \(D = 0\): UDP port \(7400\).&lt;/li&gt;
      &lt;li&gt;Your lab bench \(D = 12\): UDP port \(7400 + 250(12) = 10\,400\).
Two machines on the same Wi-Fi with different &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DOMAIN_ID&lt;/code&gt; values literally listen on different UDP ports, giving clean multi-robot isolation with zero extra firewall rules.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Check the OS before any ROS command:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; /etc/os-release
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$VERSION_ID&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
locale
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VERSION_ID&lt;/code&gt; must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;24.04&lt;/code&gt;. If it is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;22.04&lt;/code&gt;, stop. That machine gets Humble or a reinstall, not a force-installed Jazzy.&lt;/p&gt;

&lt;p&gt;UTF-8, from the &lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation/Ubuntu-Install-Debs.html&quot;&gt;Ubuntu deb install&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt update &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;locales
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;locale-gen en_US en_US.UTF-8
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;update-locale &lt;span class=&quot;nv&quot;&gt;LC_ALL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;en_US.UTF-8 &lt;span class=&quot;nv&quot;&gt;LANG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;en_US.UTF-8
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;LANG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;en_US.UTF-8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Open a new terminal so the locale sticks. Then the apt source and the desktop install. The repository line changes when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2-apt-source&lt;/code&gt; updates, so copy it from the official page the week you install. The shape is:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;curl &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# follow the current &quot;ros2-apt-source&quot; block on the Jazzy install page&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt update
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt upgrade
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ros-jazzy-desktop
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;python3-colcon-common-extensions python3-rosdep
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-desktop&lt;/code&gt; includes RViz, demos, and the CLI. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-ros-base&lt;/code&gt; is the smaller install for a robot PC that has no screen. Use desktop on the learning machine.&lt;/p&gt;

&lt;p&gt;Source it, and make the source survive new terminals:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;source /opt/ros/jazzy/setup.bash&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.bashrc
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; ~/.bashrc
&lt;span class=&quot;nb&quot;&gt;printenv &lt;/span&gt;ROS_DISTRO
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Workspace, from the &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-Client-Libraries/Creating-A-Workspace/Creating-A-Workspace.html&quot;&gt;creating a workspace tutorial&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; ~/ros2_ws/src
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~/ros2_ws
colcon build
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;source &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;HOME/ros2_ws/install/setup.bash&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.bashrc
&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The backslash before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME&lt;/code&gt; matters inside double quotes, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt; stores a frozen path. Either a frozen absolute path or a correctly escaped &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME&lt;/code&gt; is fine. Look at the file:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;tail&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; 5 ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You want the underlay line &lt;strong&gt;above&lt;/strong&gt; the overlay line.&lt;/p&gt;

&lt;p&gt;Discovery check:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 topic list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected&lt;/strong&gt; on a quiet machine:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/parameter_events
/rosout
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Some installs show only those two, and a brand-new daemon can show an empty list for a moment. Run it twice. Then:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 pkg list | &lt;span class=&quot;nb&quot;&gt;head
&lt;/span&gt;which ros2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;which ros2&lt;/code&gt; should print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy/bin/ros2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Unable to locate package ros-jazzy-desktop&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Not Ubuntu 24.04, or the apt source step was skipped&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ROS_DISTRO&lt;/code&gt; empty in a new terminal&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt; was not sourced; on Ubuntu the terminal may be a login shell that skips it. Add the same line to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.profile&lt;/code&gt; or reopen the terminal from the menu&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;humble&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jazzy&lt;/code&gt; both appear in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo $CMAKE_PREFIX_PATH&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;You sourced two distros. Close every terminal. Keep one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source&lt;/code&gt; line&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon: command not found&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python3-colcon-common-extensions&lt;/code&gt; missing, or you built before sourcing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 topic list&lt;/code&gt; hangs&lt;/td&gt;
      &lt;td&gt;Firewall or a stale daemon. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 daemon stop&lt;/code&gt; then try again&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Disk full during &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt install&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Desktop wants several gigabytes. Free space or use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros-jazzy-ros-base&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Optional environment check:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ros2 doctor
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Warnings about network interfaces are common on laptops. Errors about the distro are not.&lt;/p&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;Software only, until you want a robot computer that is not your laptop. A used office PC that already runs Ubuntu 24.04 is the cheapest host. When you outgrow the laptop, the board this course points at is a Raspberry Pi 5 (4 GB is a fair start for Jazzy plus a camera; 8 GB is kinder to Nav2 later).&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Shop&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
      &lt;th&gt;2026 band&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Raspberry Pi 5&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Hshop, UK-made board&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Pick RAM on the page. The listed price moves with the variant&lt;/td&gt;
      &lt;td&gt;from about 2.4 million VND for the small-RAM board; 4 GB and 8 GB cost more. Recheck&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pi 5 kit (PSU, case, fan, card)&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/combo-raspberry-pi-5-ram-4-8gb-basic-kit&quot;&gt;Hshop basic kit&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;The 27 W USB-C supply matters. A phone charger is a brownout&lt;/td&gt;
      &lt;td&gt;kit price is above the bare board&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;microSD 64 GB A2&lt;/td&gt;
      &lt;td&gt;Shopee/Lazada &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thẻ nhớ microSD 64GB A2&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;For the Pi, not for the laptop lab&lt;/td&gt;
      &lt;td&gt;about 150.000–350.000 VND&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Shopee keyword if Hshop is out of stock: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Raspberry Pi 5 4GB chính hãng&lt;/code&gt;. Avoid listings that are “vỏ case” only. The ESP32 stays the motor computer. The Pi, later, is the ROS computer.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Paste the output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv ROS_DISTRO&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;which ros2&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. Guidance: both must say jazzy and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy/...&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;What is wrong with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt; that sources &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/ros2_ws/install/setup.bash&lt;/code&gt; and never sources &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/ros/jazzy&lt;/code&gt;? Guidance: the overlay setup usually chains the underlay it was built with. If you built before Jazzy existed in the environment, the chain is empty and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2&lt;/code&gt; disappears. Source the underlay, rebuild, then source the overlay.&lt;/li&gt;
  &lt;li&gt;A friend on Ubuntu 22.04 asks you to paste your install commands. What do you tell them? Guidance: those commands are for 24.04 and Jazzy. Humble is the matching distro for 22.04. Do not mix.&lt;/li&gt;
  &lt;li&gt;Why is the ESP32 not “in the workspace”? Guidance: the workspace is a Linux colcon tree. The MCU keeps Chapter 08’s firmware. Chapter 10 connects them with a bridge or micro-ROS, which is a later install.&lt;/li&gt;
  &lt;li&gt;After a reboot, the first terminal cannot find &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2&lt;/code&gt;. Name the file you open. Guidance: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bashrc&lt;/code&gt;, and confirm the terminal actually reads it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation/Ubuntu-Install-Debs.html&quot;&gt;Ubuntu (deb packages) for Jazzy&lt;/a&gt;. Use this page’s apt-source block, not a blog’s old &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sources.list&lt;/code&gt; line.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-Client-Libraries/Creating-A-Workspace/Creating-A-Workspace.html&quot;&gt;Creating a workspace&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-2000.html&quot;&gt;REP-2000&lt;/a&gt; for which distro matches which Ubuntu.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://colcon.readthedocs.io/en/released/user/quick-start.html&quot;&gt;colcon documentation&lt;/a&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colcon build --symlink-install&lt;/code&gt;, which you will want once packages contain Python.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Control-loop timing and jitter</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter08/08_05_loop_timing/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter08/08_05_loop_timing</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~75 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A control engineer tunes a PID loop on a quiet bench and gets razor-sharp step response; then someone adds a debug &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt; inside the loop, and the robot suddenly starts chattering. Nothing in the physics changed—what broke was &lt;strong&gt;loop timing and jitter&lt;/strong&gt;. In discrete-time control, your sample period \(\Delta t\) is not just a clock tick; it is embedded inside every derivative and integral gain.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will replace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; in the motor loop with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; deadline, log the achieved period for a few hundred ticks, and decide from the 95th percentile whether 50 Hz is honest. You will move &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt;, Wi-Fi, and JSON work off the fast path, and you will state what a late PID sample does to \(K_d\). The log from this lesson is part of the Capstone’s acceptance notes before any ROS 2 bridge is trusted.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 08-01 through 08-04 gave you a frame, an optional MQTT client, a PID update, and a kinematics function. You can read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; on an ESP32 or Pico and you know that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; waits twenty milliseconds &lt;em&gt;after&lt;/em&gt; the work, not &lt;em&gt;instead of&lt;/em&gt; a variable amount of work. A laptop serial monitor is enough. Motors may stay disconnected; the timing log does not need them, and it is safer that way until the numbers look calm.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-sampling-theory-timing-jitter-and-real-time-execution-budgets&quot;&gt;Key ideas: Sampling Theory, Timing Jitter, and Real-Time Execution Budgets&lt;/h2&gt;

&lt;p&gt;Understanding loop timing connects digital signal processing with embedded real-time systems:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Sampling Rate &amp;amp; Control Bandwidth:&lt;/strong&gt; A practical rule of thumb in digital control is to sample at \(10\times\text{–}20\times\) the closed-loop bandwidth \(f_c\) so the zero-order-hold phase lag (\(\approx \omega_c \Delta t / 2\)) stays small and discrete equations closely approximate continuous dynamics.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Period Jitter vs. Mean Frequency:&lt;/strong&gt; A loop that averages 50 Hz (\(20\ \text{ms}\)) by alternating \(5\ \text{ms}\) and \(35\ \text{ms}\) ticks is &lt;em&gt;not&lt;/em&gt; a 50 Hz controller. If firmware assumes a fixed \(\Delta t\) while actual intervals jitter, the effective derivative gain \(K_d / \Delta t_{\text{actual}}\) fluctuates wildly—injecting noise directly into motor PWM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CPU Utilization &amp;amp; Worst-Case Execution Time (WCET):&lt;/strong&gt; To guarantee zero missed deadlines, the worst-case loop body execution time \(C_{\max}\) (including ISR overhead and telemetry formatting) must stay strictly below the period \(T\), typically \(C_{\max}/T \le 0.70\).
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The derivative term divides by \(\Delta t\). If you claim \(\Delta t = 0.02\) while the real gap was 0.08 because MQTT reconnected, \(K_d\) is four times too strong for that sample and the wheel chatters. Odometry that multiplies speed by “20 ms” while the loop actually took 35 ms walks the map. ROS 2 will not repair a lie about time. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; in Chapter 10 is the simulator’s version of the same honesty: one clock, and everybody uses it. A policy that consumes actions at 10 Hz still sits on a current loop that must be boring.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_loop_timing.png&quot; alt=&quot;Fixed deadlines versus delay jitter&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A control period is the time from one &lt;em&gt;sample&lt;/em&gt; to the next sample, not the time you slept. If reading encoders takes 1 ms, PID takes 0.2 ms, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; runs after that, the period is about 21.2 ms and it grows whenever you print a paragraph. Jitter is the spread of those periods. A loop that is usually 20 ms and sometimes 90 ms has a pretty average and a bad tail. The tail is what saturates D and drops USB bytes.&lt;/p&gt;

&lt;p&gt;The deadline pattern:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;next = millis() + PERIOD_MS
every iteration:
    now = millis()
    if (int32_t)(now - next) &amp;lt; 0: return   # not due yet; do housekeeping
    dt_ms = now - prev
    prev = now
    next += PERIOD_MS
    if dt_ms &amp;gt; 5 * PERIOD_MS: dt_ms = PERIOD_MS  # after a stall, do not take a huge D step
    control_step(dt_ms / 1000.0)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Use a signed subtraction so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; rollover after ~49 days still works. Catching up with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next += PERIOD_MS&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next = now + PERIOD_MS&lt;/code&gt; keeps the average rate honest when you are only slightly late. If you are very late, skip ahead (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next = now + PERIOD_MS&lt;/code&gt;) so you do not fire ten PID updates back to back.&lt;/p&gt;

&lt;p&gt;What is allowed inside the 20 ms tick: read encoders, compute kinematics if a new command arrived, run PID, write PWM, update the watchdog. What is not: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt; of five lines, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;client.connect()&lt;/code&gt;, parsing a large JSON document, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; of any kind, or waiting on Wi-Fi. Telemetry goes out every 5th or 10th tick, one short line. MQTT &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;client.loop()&lt;/code&gt; may run on the ticks between deadlines, and a reconnect may run at 1 Hz from a separate counter. If reconnect blocks, the watchdog in lesson 08-02 still zeros the motors; the timing log tells you that you paid for that safety with a gap.&lt;/p&gt;

&lt;p&gt;A practical budget for this robot is 50 Hz (20 ms) for the motor loop and 10 Hz for telemetry. 100 Hz is fine if the log says the 95th percentile period is under about 12 ms for a 10 ms target. Faster than you can measure is not a badge of honor.&lt;/p&gt;

&lt;p&gt;The number to write down is not the mean alone. Sort the periods and report p50 and p95, or at least the maximum. A p95 inside \(0.8\) to \(1.2\) times the target is a pass for this course. A single 200 ms gap while “the mean is 21 ms” is a fail until you know which call caused it.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Forty periods, target 20 ms. Suppose thirty-eight of them are 20 or 21, one is 28, and one is 74 because a debug print dumped a buffer. The mean is about 22.5 ms, which looks acceptable, and p95 is 28 ms, which also looks acceptable if you compute the percentile without the story. The maximum is 74 ms. That one sample fed \(K_d\) a \(\Delta t\) almost four times the design value if you used the real gap, or it fed a stale derivative if you pretended \(\Delta t\) was 0.02. The lab asks you to print the max and to comment the line that caused it. “Mostly 20 ms” is not the report.&lt;/p&gt;

&lt;p&gt;A tiny host-side reducer:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;statistics&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;74&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;p50&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;median&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# p95 for a short list: sort and index
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;p95~&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.95&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;On the board, you do not need a full histogram. Keep a running max and a count of ticks that exceeded &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PERIOD_MS + 4&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;explanation-quantifying-blocking-print-overhead-and-jitter-error&quot;&gt;Explanation: Quantifying Blocking Print Overhead and Jitter Error&lt;/h3&gt;

&lt;p&gt;Suppose your control loop is scheduled for \(T = 20\ \text{ms}\) (50 Hz), and you add a 60-character debug log line printed synchronously over a 9600-baud UART:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compute the hidden blocking time of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.println&lt;/code&gt; at 9600 baud (10 bits/byte):&lt;/strong&gt;
\(t_{\text{print}} = \frac{60 \times 10\ \text{bits}}{9600\ \text{bits/s}} = 62.5\ \text{ms}!\)
Once the TX ring buffer fills, that single print statement stalls the MCU for &lt;strong&gt;62.5 ms&lt;/strong&gt;—more than &lt;strong&gt;3× longer&lt;/strong&gt; than your entire 20 ms control period, crashing your actual control rate down to \(\approx 15\ \text{Hz}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Switch to 115,200 baud and decimate logging to 10 Hz:&lt;/strong&gt; At 115,200 baud, 60 bytes take only \(600 / 115200 = 5.2\ \text{ms}\) (and fit entirely inside a 64-byte hardware TX buffer without blocking at all).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Measure jitter effect on derivative gain:&lt;/strong&gt; If a fixed-\(\Delta t\) PID assumes \(\Delta t = 20\ \text{ms}\) but a stray Wi-Fi interrupt stretches one tick to \(30\ \text{ms}\) (\(+50\%\) jitter), the true rate of change \(\Delta e / 0.030\) is miscomputed as \(\Delta e / 0.020\)—a &lt;strong&gt;50% error&lt;/strong&gt; in derivative action. Either pace ticks with hardware timers or pass measured &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt&lt;/code&gt; into the PID update.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Motors unplugged for the first log. Firmware prints one CSV line every tick for 5 seconds, then stops printing. Printing every tick &lt;em&gt;is&lt;/em&gt; the disturbance you are measuring, so also take a second log that prints only a summary.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dt_ms
20
21
20
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Summary line after 250 ticks:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;n=250 p50=20 p95=22 max=27 over=3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then enable the real workload: MQTT client or a burst of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt; inside the tick. Capture the summary again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Expected&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Build&lt;/th&gt;
      &lt;th&gt;p95&lt;/th&gt;
      &lt;th&gt;max&lt;/th&gt;
      &lt;th&gt;Verdict&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Deadline loop, summary only&lt;/td&gt;
      &lt;td&gt;≤ 24 ms&lt;/td&gt;
      &lt;td&gt;≤ 30 ms&lt;/td&gt;
      &lt;td&gt;pass for 50 Hz&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; plus a 40-character print every tick&lt;/td&gt;
      &lt;td&gt;often &amp;gt; 30 ms&lt;/td&gt;
      &lt;td&gt;prints dominate&lt;/td&gt;
      &lt;td&gt;fail, move the print&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MQTT reconnect inside the tick&lt;/td&gt;
      &lt;td&gt;one sample of hundreds of ms&lt;/td&gt;
      &lt;td&gt;the reconnect&lt;/td&gt;
      &lt;td&gt;fail, move reconnect off the tick&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Every period is 0 or 1&lt;/td&gt;
      &lt;td&gt;You forgot to wait for the deadline; the loop is spinning&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Periods climb forever: 20, 40, 60&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next&lt;/code&gt; is not updated, so one late tick stays late in the bookkeeping, or you stack &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;max is huge only when the cable is loose&lt;/td&gt;
      &lt;td&gt;USB serial blocking on a full buffer. Shorten the print&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;PID hisses only when telemetry is on&lt;/td&gt;
      &lt;td&gt;Logging is inside the tick. The gains were tuned against a lie&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pico and ESP32 disagree&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; on both is fine; a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep&lt;/code&gt; in MicroPython is the same bug as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;MicroPython note: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff()&lt;/code&gt; are the rollover-safe pair. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep_ms(20)&lt;/code&gt; after the work recreates the jitter figure.&lt;/p&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;No parts. If the USB serial drops characters and you blame the algorithm, a known-good cable is the substitute: search Shopee/Lazada for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cáp micro USB truyền dữ liệu&lt;/code&gt; (about 20.000–50.000 VND) and avoid charge-only cables. The ESP32 you already have is the instrument.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A tick’s real \(\Delta t\) is 0.08 s and the code still divides the derivative by 0.02. By what factor is that D term wrong? Guidance: four times too large, because the true rate of change is smaller than the one you assumed.&lt;/li&gt;
  &lt;li&gt;Why is the maximum a better alarm than the mean? Guidance: the mean hides one blocking call. The wheel feels the blocking call.&lt;/li&gt;
  &lt;li&gt;Rewrite the pseudo-code so a tick that is 200 ms late does not immediately run five makeup PID steps. Guidance: if the lateness exceeds one period, set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next = now + PERIOD_MS&lt;/code&gt; and clamp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Where do you put &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mosquitto&lt;/code&gt; traffic relative to the deadline? Guidance: not inside the due tick. Between deadlines, and never blocking past the next sample without the watchdog firing.&lt;/li&gt;
  &lt;li&gt;What will you paste into the Chapter 10 bridge issue if p95 was 45 ms on a 20 ms target? Guidance: “do not tune Nav2 until the base loop’s p95 is inside 20 percent of its period.” That is a real gate, not a slogan.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/time/millis/&quot;&gt;ESP32 Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis&lt;/code&gt;&lt;/a&gt; for the overflow behavior the signed subtract relies on.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.micropython.org/en/latest/library/time.html&quot;&gt;MicroPython &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_ms&lt;/code&gt;&lt;/a&gt; if the board runs Python.&lt;/li&gt;
  &lt;li&gt;The same timing honesty shows up as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_sim_time&lt;/code&gt; parameter in the &lt;a href=&quot;https://docs.ros.org/en/jazzy/How-To-Guides/Using-ros2-param.html&quot;&gt;Jazzy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros2 param&lt;/code&gt; guide&lt;/a&gt; once the simulator joins in Chapter 10.&lt;/li&gt;
  &lt;li&gt;Lesson 08-03’s derivative, which is the formula this period is dividing into.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Differential-drive kinematics</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter08/08_04_diff_kinematics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter08/08_04_diff_kinematics</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~75 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When a ROS 2 navigation stack decides the robot should curve gently around a chair, it knows nothing about your motor drivers or wheel diameters—it simply publishes a body twist \((v, \omega)\). Differential-drive kinematics is the exact two-way translator between that universal body language and the two physical tires touching the floor.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will measure the track width of your Capstone base, convert a body velocity \((v, \omega)\) into left and right tread speeds, and convert those speeds back to check the arithmetic. You will predict the turning radius of a joystick command before the robot moves, and you will explain why a differential-drive base cannot slide sideways. Those two equations are the body of every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; callback from Chapter 10 onward.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can identify which wheel is left when you sit on the robot looking forward. Chapter 07’s open-loop teleop already spins those wheels. A ruler or a tailor’s tape is the only new tool. PID from lesson 08-03 is optional here: kinematics produces speed targets, and open-loop PWM can still demonstrate the shape of a turn with the wheels in the air.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-nonholonomic-constraints-jacobian-mapping-and-dead-reckoning-odometry&quot;&gt;Key ideas: Nonholonomic Constraints, Jacobian Mapping, and Dead-Reckoning Odometry&lt;/h2&gt;

&lt;p&gt;Differential-drive kinematics bridges planar geometry and state estimation across three pillars:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Nonholonomic Rolling Constraint:&lt;/strong&gt; A rolling tire cannot slide sideways (\(\dot{y}_{\text{body}} = 0\)). In world coordinates \((x, y, \theta)\), this imposes the Pfaffian constraint \(-\dot{x}\sin\theta + \dot{y}\cos\theta = 0\)—meaning the robot can reach any \((x, y, \theta)\) pose (via parallel-parking maneuvers) but cannot instantaneously move sideways.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Forward &amp;amp; Inverse Velocity Jacobian:&lt;/strong&gt; The invertible \(2\times 2\) matrix \(J = \begin{bmatrix} 1/2 &amp;amp; 1/2 \\ 1/L &amp;amp; -1/L \end{bmatrix}\) maps wheel linear speeds \((v_R, v_L)\) to body twist \((v, \omega)\), and its inverse \(J^{-1} = \begin{bmatrix} 1 &amp;amp; L/2 \\ 1 &amp;amp; -L/2 \end{bmatrix}\) converts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; into wheel setpoints.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Odometry Integration (Euler vs. Midpoint / Exact Arc):&lt;/strong&gt; Integrating \(\dot{x} = v\cos\theta,\ \dot{y} = v\sin\theta,\ \dot{\theta} = \omega\) over step \(\Delta t\) produces wheel odometry. Using the midpoint heading \(\theta_k + \frac{1}{2}\omega\Delta t\) dramatically reduces truncation error during turns compared to naive Euler.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; is not a left motor and a right motor. Nav2, teleop_twist_keyboard, and any later policy publish forward speed and yaw rate in the robot’s body frame. Someone must invert that into two wheel speeds. If that someone uses the wrong track width, every map in Chapter 11 is scaled wrong even when the SLAM software is perfect. If the sign of \(\omega\) is flipped, the robot turns away from the goal and the planner “fails” for a mechanical reason. Write the inverse once, with units, and call it from the serial bridge.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_diff_kinematics.png&quot; alt=&quot;Top view of track width and wheel speeds&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;REP-103 puts \(+x\) forward, \(+y\) left, and \(+z\) up. Positive yaw rate \(\omega\) is a left turn: the right wheel is faster than the left. Wheel speeds \(v_R\) and \(v_L\) are tread speeds in metres per second, positive forward. \(L\) is the track width in metres, the distance between the two contact patches, not the width of the acrylic plate and not the distance between motor shafts if the tires bulge.&lt;/p&gt;

&lt;p&gt;The forward map, from wheels to the body:&lt;/p&gt;

\[v = \frac{v_R + v_L}{2}\]

\[\omega = \frac{v_R - v_L}{L}\]

&lt;p&gt;The inverse map, from a Twist to the wheels:&lt;/p&gt;

\[v_R = v + \omega \frac{L}{2}\]

\[v_L = v - \omega \frac{L}{2}\]

&lt;p&gt;If \(v = 0\) and \(\omega \neq 0\), the wheels are equal and opposite and the robot spins about its centre. The turning radius for \(v \neq 0\) is&lt;/p&gt;

\[R = \frac{v}{\omega}\]

&lt;p&gt;with the centre of the turn a distance \(R\) to the left of the robot when \(\omega &amp;gt; 0\) and \(v &amp;gt; 0\). There is no \(v_y\). A command that asks the base to slide sideways is not a small error; the mechanism cannot do it. Controllers that ignore that fact oscillate. You will see the same sentence again in the Nav2 tour.&lt;/p&gt;

&lt;p&gt;Wheel radius \(r\) enters only when you need shaft rate or encoder counts:&lt;/p&gt;

\[\dot{\theta} = \frac{v_{\text{wheel}}}{r}\]

&lt;p&gt;Keep \(r\) out of the \(v, \omega\) formulas. A common bug is to mix revolutions per second with metres per second in the same variable.&lt;/p&gt;

&lt;p&gt;Joystick axes are not metres per second. Pick a scale on purpose, for example full stick forward means \(v = 0.3\) m/s and full stick yaw means \(\omega = 1.0\) rad/s, and write the scale next to the equations. Lesson 08-01’s −100…100 integers are a third scale. One function converts m/s of tread into that integer after the inverse kinematics, using the speed you observed at PWM 100 during the Chapter 07 spin test. Do not hide the scale inside “it felt right.”&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Tape measurement: contact patch to contact patch, \(L = 0.16\) m. Wheel radius \(r = 0.033\) m, used only at the end. Command: \(v = 0.20\) m/s, \(\omega = 0.50\) rad/s (a gentle left turn).&lt;/p&gt;

\[\frac{L}{2} = 0.08\]

\[v_R = 0.20 + 0.50 \times 0.08 = 0.24 \text{ m/s}\]

\[v_L = 0.20 - 0.50 \times 0.08 = 0.16 \text{ m/s}\]

&lt;p&gt;Check:&lt;/p&gt;

\[v = \frac{0.24 + 0.16}{2} = 0.20\]

\[\omega = \frac{0.24 - 0.16}{0.16} = 0.50\]

&lt;p&gt;Turning radius \(R = 0.20 / 0.50 = 0.40\) m. On the floor, a piece of tape at the geometric centre of the axle should travel a circle of radius about 0.40 m. The right tire travels a larger circle than the left, which is why it is faster.&lt;/p&gt;

&lt;p&gt;Shaft rate of the right wheel, if you need it for PID:&lt;/p&gt;

\[\dot{\theta}_R = \frac{0.24}{0.033} \approx 7.27 \text{ rad/s}\]

&lt;p&gt;Python you can paste into the bridge later:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wheel_speeds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;omega&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;omega&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;omega&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;half&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# left, right
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;body_from_wheels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;body_from_wheels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wheel_speeds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A pure spin, \(v = 0\), \(\omega = 1.0\), \(L = 0.16\): \(v_R = 0.08\), \(v_L = -0.08\). If both numbers come out positive, you added \(\omega L\) to both wheels instead of splitting the sign.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-integrating-one-odometry-step-on-a-curved-arc&quot;&gt;Explanation: Hand-Integrating One Odometry Step on a Curved Arc&lt;/h3&gt;

&lt;p&gt;Using the lesson’s worked command \(v = 0.20\ \text{m/s}\), \(\omega = 0.50\ \text{rad/s}\) from initial pose \((x_0, y_0, \theta_0) = (0, 0, 0)\) over a \(\Delta t = 1.0\ \text{s}\) arc, compare naive Euler integration against midpoint (Runge–Kutta 2) integration:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Exact arc geometry (\(R = v/\omega = 0.40\ \text{m}\), \(\Delta\theta = \omega\Delta t = 0.50\ \text{rad}\)):&lt;/strong&gt;
\(x_{\text{exact}} = R\sin(\Delta\theta) = 0.40\sin(0.50) = 0.40(0.4794) = 0.1918\ \text{m},\)
\(y_{\text{exact}} = R(1 - \cos\Delta\theta) = 0.40(1 - 0.8776) = 0.0490\ \text{m}, \qquad \theta_1 = 0.50\ \text{rad}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Naive Euler integration (using \(\theta_0 = 0\) for the whole step):&lt;/strong&gt;
\(x_{\text{Euler}} = 0 + 0.20\cos(0)(1.0) = 0.2000\ \text{m}, \qquad y_{\text{Euler}} = 0 + 0.20\sin(0)(1.0) = 0.0000\ \text{m}\)
— completely missing the \(4.9\ \text{cm}\) sideways curve!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Midpoint integration (using \(\theta_{\text{mid}} = \theta_0 + \frac{1}{2}\Delta\theta = 0.25\ \text{rad}\)):&lt;/strong&gt;
\(x_{\text{mid}} = 0 + 0.20\cos(0.25) = 0.20(0.9689) = 0.1938\ \text{m},\)
\(y_{\text{mid}} = 0 + 0.20\sin(0.25) = 0.20(0.2474) = 0.0495\ \text{m}.\)
With one extra line of math (evaluating at \(\theta + \Delta\theta/2\)), the lateral error drops from &lt;strong&gt;49 mm&lt;/strong&gt; down to &lt;strong&gt;0.5 mm&lt;/strong&gt;!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Put the robot on a table, wheels up or a stand. Measure \(L\) three times and record the mean in metres.&lt;/li&gt;
  &lt;li&gt;Mark the tyre with chalk. Command equal wheel speeds for two seconds and confirm both marks move forward. That fixes the sign of \(v_L\) and \(v_R\) in your firmware.&lt;/li&gt;
  &lt;li&gt;Command the pure spin above at low PWM (map 0.08 m/s to whatever duty was “slow forward” in Chapter 07). The left wheel must turn backward.&lt;/li&gt;
  &lt;li&gt;Optional floor test, clear space, slow speed: drive \(v = 0.2\), \(\omega = 0.5\) for a quarter turn and compare the tape-measure radius with 0.40 m scaled by your real \(L\). Recompute \(R = v/\omega\) with your numbers before you drive.&lt;/li&gt;
  &lt;li&gt;Save the function and the measured \(L\) in the same file you will import from the Chapter 10 bridge.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Expected notes&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;L_mean = 0.158 m
equal command: both wheels forward
v=0, w&amp;gt;0: right forward, left backward
R predicted = 0.40 m, R taped = 0.45 m  (slip, do not &quot;fix&quot; with a random gain)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A 10 percent radius error on carpet is slip and scrub, not a failed formula. A reversed left wheel is a failed sign.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot curves the wrong way&lt;/td&gt;
      &lt;td&gt;\(\omega\) sign, or left/right swapped in the tuple&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Straight command drives a circle&lt;/td&gt;
      &lt;td&gt;One wheel’s PWM scale is softer, or one tyre is smaller. Kinematics will not hide that; PID might&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Radius is half of what you predicted&lt;/td&gt;
      &lt;td&gt;You used the plate width, or you used diameter as \(L\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Both wheels reverse on a left turn&lt;/td&gt;
      &lt;td&gt;You set \(v_L = v + \omega L/2\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Later, the map scale is nonsense&lt;/td&gt;
      &lt;td&gt;\(L\) or \(r\) was in centimetres inside a metre formula&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;No new electronics. A 3 m tailor tape from any stationery stall is enough (about 15.000–40.000 VND on Shopee, keyword &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thước dây 3m&lt;/code&gt;). If a tyre is oval or the foam tyre has flattened, replace the pair so \(r\) matches: search &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh xe robot cao su 65mm&lt;/code&gt; and expect roughly 20.000–60.000 VND per wheel. Buy both wheels together. Mixing diameters is a fake kinematics bug.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Your track is 0.20 m. Joystick asks for \(v = 0\), \(\omega = -0.8\) rad/s. Compute \(v_L\) and \(v_R\) and name the turn. Guidance: right wheel negative, left wheel positive, a right turn (negative yaw).&lt;/li&gt;
  &lt;li&gt;Both wheels at 0.3 m/s. What are \(v\) and \(\omega\)? Guidance: \(v = 0.3\), \(\omega = 0\), regardless of \(L\).&lt;/li&gt;
  &lt;li&gt;Why can you not satisfy \(v_x = 0\), \(v_y = 0.2\), \(\omega = 0\) with any \(v_L, v_R\)? Guidance: both formulas only produce motion along \(x\) and yaw. \(v_y\) is identically zero.&lt;/li&gt;
  &lt;li&gt;You measured \(L = 15.5\) cm and typed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.155&lt;/code&gt; in one file and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;15.5&lt;/code&gt; in the bridge. Which symptom shows up in \(R\)? Guidance: the radius is wrong by a factor of 100, which looks like “the robot pivots in place no matter what.”&lt;/li&gt;
  &lt;li&gt;Where does this function sit relative to PID? Guidance: Twist → this function → two m/s setpoints → two PID loops → two PWMs. Skipping PID means the m/s numbers are only hopes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103, Standard Units of Measure and Coordinate Conventions&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0105.html&quot;&gt;REP-105&lt;/a&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_link&lt;/code&gt;, which is the frame these equations live in.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hades.mech.northwestern.edu/index.php/Modern_Robotics&quot;&gt;Modern Robotics, Chapter 13&lt;/a&gt; (free online) if you want the wheeled-robot chapter after the two-line model feels small.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; in the &lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/msg/Twist.html&quot;&gt;Jazzy message docs&lt;/a&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; is \(v\), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; is \(\omega\), and the other four fields stay near zero on this base.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>PID intuition and tuning practice</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter08/08_03_pid_tuning/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter08/08_03_pid_tuning</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~90 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Walk into any factory, drone lab, or autonomous vehicle garage and over 90% of the low-level control loops keeping motors on target are variations of a single 100-year-old equation: &lt;strong&gt;PID&lt;/strong&gt;. Yet most beginners treat \(K_p, K_i, K_d\) as three mystery knobs to wiggle blindly. Once you see PID as three distinct physical forces—a spring pulling toward the target, a memory eliminating steady bias, and a damper braking overshoot—tuning turns from guesswork into method.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will write a discrete PID update for one wheel’s speed, using encoder counts as the measurement, and predict the sign of each term on a numerical step. You will tune in the order P, then D, then a little I, with the wheel in the air, and you will clamp the integrator so a stalled wheel cannot wind up. You will log setpoint, measured speed, and output for a single step and say which term is misbehaving if the plot oscillates or never quite arrives.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Chapter 04’s encoder counts are trustworthy: you can convert a difference of counts into a speed. Chapter 05’s driver accepts a signed PWM or a signed −100…100 command. Lesson 08-05’s fixed period is the right home for this code; if you have not measured the loop yet, still call the PID on a known \(\Delta t\) rather than “whenever &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt; happens to return.” Calculus you need is only “rate of change” and “accumulated area.”&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-feedback-control-theory-discrete-time-pid-and-practical-nonlinearities&quot;&gt;Key ideas: Feedback Control Theory, Discrete-Time PID, and Practical Nonlinearities&lt;/h2&gt;

&lt;p&gt;Mastering PID at an engineering level rests on three core pillars of classical control:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Proportional–Integral–Derivative Action:&lt;/strong&gt; Given error \(e(t) = r(t) - y(t)\), the control law \(u(t) = K_p e(t) + K_i \int_0^t e(\tau)\,d\tau + K_d \frac{de}{dt}\) balances responsiveness (\(K_p\)), zero steady-state offset against constant load/friction (\(K_i\)), and damping of overshoot (\(K_d\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Discrete-Time Implementation:&lt;/strong&gt; On an MCU running at period \(\Delta t\), integration becomes a running sum \(I_k = I_{k-1} + e_k \Delta t\) and differentiation becomes a finite difference \(D_k = (e_k - e_{k-1})/\Delta t\) (usually computed on &lt;em&gt;measurement&lt;/em&gt; \(-\Delta y_k/\Delta t\) to avoid “derivative kick” on step setpoint changes, and low-pass filtered to avoid amplifying encoder quantization noise).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Actuator Saturation &amp;amp; Integrator Windup:&lt;/strong&gt; Real motors cap out at \(\pm 100\%\) PWM. If the robot hits a wall while \(I_k\) keeps accumulating error, the integrator “winds up” to a huge number, causing sluggish recovery even after the error reverses—solved by clamping \(I_k\) (anti-windup).
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Open-loop PWM from Chapter 07 is enough to teleop on a smooth floor and a fresh battery. It is not enough once the battery sags, one wheel sees carpet, or Nav2 asks for 0.20 m/s and actually gets 0.11 m/s. It is also not enough when the two motors already differ: &lt;a href=&quot;/contents/en/chapter04/04_03_encoders_counts/&quot;&gt;lesson 04-03&lt;/a&gt; shows equal PWM still leaving one wheel a few percent ahead. Odometry in Chapter 11 integrates that error into a bent map. A learned policy in Chapter 12 that outputs wheel velocities assumes the wheels approximately do what they were told. PID on wheel speed is the small loop underneath &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;. Do not jump to balancing a two-wheel robot; get one wheel to hold a speed first.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_pid_step.png&quot; alt=&quot;P, then D, then I on a step&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;The error is setpoint minus measurement, in the same units on both sides. If the setpoint is 0.30 m/s of wheel tread and the encoder says 0.20 m/s, \(e = 0.10\) m/s. Positive error means “go faster forward.” If you flip the sign, the controller runs away. Check the sign before you touch the gains.&lt;/p&gt;

\[e(t) = r(t) - y(t)\]

\[u(t) = K_p e(t) + K_i \int_0^{t} e(\tau)\, d\tau + K_d \frac{de}{dt}\]

&lt;p&gt;\(u\) is the driver command, not a voltage you invent. If the driver wants −100…100, \(u\) lives on that scale and must be saturated there. The three gains have units. \(K_p\) is “command per (m/s).” A dimensionless \(K_p\) copied from a tutorial about degrees is meaningless on this robot.&lt;/p&gt;

&lt;p&gt;On a microcontroller the integral and derivative are sums and differences over the last period:&lt;/p&gt;

\[I_k = \mathrm{clamp}\big(I_{k-1} + e_k \Delta t,\; -I_{\max},\; I_{\max}\big)\]

\[u_k = \mathrm{sat}\Big(K_p e_k + K_i I_k + K_d \frac{e_k - e_{k-1}}{\Delta t}\Big)\]

&lt;p&gt;\(\mathrm{sat}\) clamps \(u\) to the driver range. The clamp on \(I\) is anti-windup. Without it, a wheel blocked by a hand for two seconds stores a huge integral. When you let go, \(u\) slams to the rail. Clamp \(I\), and also freeze \(I\) while \(u\) is already saturated and the error still has the same sign. That second rule is “conditional integration.” Either rule is enough for this lab; implement one and write its name in the log.&lt;/p&gt;

&lt;p&gt;Derivative of the error kicks when the setpoint steps, because \(e\) jumps even if the wheel has not moved. A calmer variant differentiates the measurement instead, with a minus sign, so a setpoint change does not spike \(u\). For a first tune, a small \(K_d\) on the error is fine if the step is modest.&lt;/p&gt;

&lt;p&gt;Tuning order on a wheel in the air:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;\(K_i = 0\), \(K_d = 0\). Raise \(K_p\) until a step from 0 to 0.3 m/s rises quickly and shows a small overshoot or a steady buzz. Back off a little.&lt;/li&gt;
  &lt;li&gt;Add \(K_d\) until the overshoot shrinks and the buzz does not get louder. Encoder noise loves a large \(K_d\). If the PWM hisses at rest, \(K_d\) is too high or the speed estimate is too raw. Average two or three count windows before you add more D.&lt;/li&gt;
  &lt;li&gt;Add a little \(K_i\) until the steady error (battery droop, friction) disappears over a second or two. If the wheel slowly oscillates with a period near a second, cut \(K_i\) in half.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ziegler–Nichols is a textbook recipe that expects you to bring the loop to the edge of instability. Skip it on a geared motor tied to a robot chassis. The manual order above is the lab.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;One sample, not a whole trace. \(\Delta t = 0.02\) s (50 Hz). Setpoint \(r = 0.30\) m/s. Measured \(y_k = 0.18\) m/s, so \(e_k = 0.12\). Previous error \(e_{k-1} = 0.20\). Integrator coming in: \(I_{k-1} = 0.04\). Gains: \(K_p = 80\), \(K_i = 40\), \(K_d = 0.5\), command saturated at ±100. \(I_{\max} = 0.5\).&lt;/p&gt;

\[I_k = 0.04 + 0.12 \times 0.02 = 0.0424\]

\[P = 80 \times 0.12 = 9.6\]

\[I_{\text{term}} = 40 \times 0.0424 = 1.696\]

\[D = 0.5 \times \frac{0.12 - 0.20}{0.02} = 0.5 \times (-4) = -2.0\]

\[u = 9.6 + 1.696 - 2.0 = 9.3\]

&lt;p&gt;The derivative is negative because the error got smaller: the wheel is already catching the setpoint, so D eases off the command. If your logged D is positive while the error is shrinking, the difference is backwards. Integer PWM code should round 9.3 to 9 and still log the float.&lt;/p&gt;

&lt;p&gt;A second mental check: measurement above the setpoint, \(e &amp;lt; 0\), must produce a smaller (or negative) \(u\) if the wheel can reverse. If positive error and negative error both increase PWM, the measurement sign is flipped relative to the driver’s forward direction. Fix that before any gain change. Swapping two motor wires and also negating the encoder is a classic double negative that hides until the robot curves.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-tracing-3-ticks-of-a-discrete-pid-wheel-speed-loop&quot;&gt;Explanation: Hand-Tracing 3 Ticks of a Discrete PID Wheel-Speed Loop&lt;/h3&gt;

&lt;p&gt;Let us step through 3 ticks of a discrete PID controller at \(\Delta t = 0.05\ \text{s}\) (20 Hz) with gains \(K_p = 2.0\), \(K_i = 4.0\ \text{s}^{-1}\), \(K_d = 0.05\ \text{s}\), target wheel speed \(r = 10\ \text{rad/s}\), and initial state \(y_0 = 0,\ I_0 = 0,\ e_0 = 10\):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Tick \(k = 1\) (measured \(y_1 = 2\ \text{rad/s}\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Error: \(e_1 = 10 - 2 = 8\).&lt;/li&gt;
      &lt;li&gt;Proportional: \(P_1 = 2.0(8) = 16.0\).&lt;/li&gt;
      &lt;li&gt;Integral: \(I_1 = 0 + 8(0.05) = 0.40 \implies K_i I_1 = 4.0(0.40) = 1.60\).&lt;/li&gt;
      &lt;li&gt;Derivative (on error): \(D_1 = 0.05 \cdot \frac{8 - 10}{0.05} = -2.0\) (braking because error is shrinking!).&lt;/li&gt;
      &lt;li&gt;Output: \(u_1 = 16.0 + 1.60 - 2.0 = 15.60\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tick \(k = 2\) (measured \(y_2 = 7\ \text{rad/s}\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Error: \(e_2 = 10 - 7 = 3\).&lt;/li&gt;
      &lt;li&gt;\(P_2 = 2.0(3) = 6.0\); \(I_2 = 0.40 + 3(0.05) = 0.55 \implies K_i I_2 = 2.20\); \(D_2 = 0.05 \cdot \frac{3 - 8}{0.05} = -5.0\).&lt;/li&gt;
      &lt;li&gt;Output: \(u_2 = 6.0 + 2.20 - 5.0 = 3.20\) (notice how \(D_2 = -5.0\) proactively applies brakes &lt;em&gt;before&lt;/em&gt; \(y\) overshoots 10!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tick \(k = 3\) (measured \(y_3 = 10\ \text{rad/s}\), right on target!):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Error: \(e_3 = 0\), so \(P_3 = 0\), \(I_3 = 0.55 + 0 = 0.55 \implies K_i I_3 = 2.20\), \(D_3 = 0.05 \cdot \frac{0 - 3}{0.05} = -3.0\).&lt;/li&gt;
      &lt;li&gt;Once speed settles (\(e_k = 0,\ \Delta e_k = 0\)), \(P = D = 0\) and the integral term \(K_i I_k = 2.20\) holds the exact steady PWM needed to overcome rolling friction!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Wheels off the ground. Battery connected as in Chapter 07. Driver logic supply and motor supply both present, common ground with the MCU.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; at 10 Hz and rotate the wheel by hand one revolution. Confirm the count sign matches the sign you will call “forward.”&lt;/li&gt;
  &lt;li&gt;Convert speed: \(y = \Delta \mathrm{counts} \times \frac{2\pi r}{N \Delta t}\) with \(r\) the wheel radius in metres and \(N\) the counts per revolution. Log \(y\) while you spin by hand. A slow hand spin should be a few tenths of a metre per second, not 40.&lt;/li&gt;
  &lt;li&gt;Implement the update with \(K_i = K_d = 0\) and a step to 0.25 m/s for 2 seconds, then 0.&lt;/li&gt;
  &lt;li&gt;Record CSV: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms,r,y,e,P,I,D,u&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Add D, then I, using the order above. Stop if the gearbox chatters.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Expected shape of the log&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;t_ms,r,y,u
0,0.25,0.00,20
200,0.25,0.16,14
800,0.25,0.24,8
2000,0.00,0.22,-15
2400,0.00,0.02,0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Numbers will not match yours. The shape must: \(u\) positive while \(y\) is below \(r\), \(y\) near \(r\) before the step ends, \(u\) opposite when the setpoint drops, and \(u\) near 0 at rest. After you stall the wheel with a finger for one second (gently, in the air), releasing it must not produce a full-scale twitch. If it does, \(I\) is not clamped.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheel runs away as soon as PID enables&lt;/td&gt;
      &lt;td&gt;Error sign, or encoder direction opposite the motor&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Steady buzz at the target&lt;/td&gt;
      &lt;td&gt;\(K_p\) or \(K_d\) too high, or \(\Delta t\) jitter from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt; every loop&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Never reaches the setpoint, \(u\) stuck at 40&lt;/td&gt;
      &lt;td&gt;No integral and the P term is saturated by a low gain against friction. On a free wheel, friction is small; if you need huge \(u\) in the air, the speed unit is wrong&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Fine in the air, crawls on carpet&lt;/td&gt;
      &lt;td&gt;That is load, not a broken PID. Raise I slightly or accept a lower max speed. Do not copy the in-air \(K_p\) onto the floor without a new log&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Integral explodes while you hold the wheel&lt;/td&gt;
      &lt;td&gt;\(I_{\max}\) missing&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;If Capstone A already has encoded gearmotors, buy nothing. If the chassis used plain TT motors, you need counts before this lesson is real.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Shop&lt;/th&gt;
      &lt;th&gt;Search&lt;/th&gt;
      &lt;th&gt;2026 band&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;JGA25 or TT gearmotor with Hall encoder, matched pair&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; search “encoder”&lt;/td&gt;
      &lt;td&gt;Shopee/Lazada: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ giảm tốc encoder Hall JGA25&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 90.000–250.000 VND each&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TB6612 or similar dual driver, if the L298N drop is too large for a 2S pack&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; search “TB6612”&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;module TB6612FNG&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 30.000–90.000 VND&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MPU6050 is not required for wheel PID&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;GY-521 on Hshop&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 30.000–70.000 VND, optional later for yaw&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Match the encoder voltage to the MCU. Many Hall boards want 5 V on VCC and still output 3.3 V-tolerant pulses; read the silkscreen before you tie them to a Pico’s 3.3 V pin. Substitutes: a magnetic encoder on the output shaft is fine; a “speed sensor” that only pulses once per revolution is too coarse for a 50 Hz loop.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Recompute the worked example with \(e_k = -0.05\) and \(e_{k-1} = 0.12\). What is the sign of \(u\) if \(I\) is still small? Guidance: P is negative, D is also negative because the error fell through zero toward more negative, so \(u\) should command a slowdown or a reverse.&lt;/li&gt;
  &lt;li&gt;Your CSV shows \(y\) stuck at 0 while \(u\) climbs to 100 and stays there. Which clamp failed? Guidance: the integrator. P alone would sit at \(K_p e\), which is constant if \(y\) is stuck.&lt;/li&gt;
  &lt;li&gt;Why is wheel-speed PID the wrong first tool for “drive exactly 1.00 m down the hallway”? Guidance: that is an outer position loop. Speed PID only makes the tread fast. Distance needs an integral of speed, and heading needs both wheels.&lt;/li&gt;
  &lt;li&gt;A blog uses \(K_p = 2\) for a heater in degrees. What experiment replaces copying that number? Guidance: the in-air step, raising \(K_p\) from a small value until the rise looks brisk.&lt;/li&gt;
  &lt;li&gt;Write the one-line contract between this lesson and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;: who outputs m/s, who outputs PWM? Guidance: kinematics (08-04) outputs two wheel speeds in m/s; this PID outputs PWM; the bridge must not skip the middle.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ni.com/en/shop/labview/pid-theory-explained.html&quot;&gt;NI’s PID primer&lt;/a&gt; for the three terms in plain language.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://fbswiki.org/wiki/index.php/Feedback_Systems&quot;&gt;Åström and Murray, Feedback Systems&lt;/a&gt; is the free book if you want anti-windup drawn properly. Chapter 10 of that book is optional depth, not tonight’s lab.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://controlguru.com/table-of-contents/&quot;&gt;Control Guru’s PID tuning introduction&lt;/a&gt; if you want another industrial voice that also starts from a step test.&lt;/li&gt;
  &lt;li&gt;Encoder counting from Chapter 04 of this course, which this loop trusts completely.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab: Wi-Fi and MQTT on ESP32</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter08/08_02_wifi_mqtt/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter08/08_02_wifi_mqtt</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~90 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Cutting the USB tether feels liberating—until your robot drives around a concrete pillar, Wi-Fi latency spikes from 4 ms to 450 ms, and your carefully tuned control loop goes wild. Wireless links like Wi-Fi and MQTT are extraordinary for telemetry, fleet monitoring, and high-level goal commands, provided you understand &lt;em&gt;which&lt;/em&gt; loops belong over wireless and &lt;em&gt;which&lt;/em&gt; must stay local on the MCU.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will run a Mosquitto broker on your laptop, publish a velocity-style command on a private topic, and subscribe to a telemetry topic from an ESP32 on the same 2.4 GHz LAN. You will explain QoS 0 versus QoS 1 in one concrete failure (a retained “drive” message that survives a reboot), and you will keep the Chapter 08-01 serial timeout as the path that still stops the wheels when Wi-Fi drops. The lab log should show one received JSON command and one watchdog stop.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 08-01’s frame and timeout exist in your notes, even if the MQTT board test is still ahead. You can join the ESP32 to a 2.4 GHz network. You know an IP address from a port. Chapter 07’s motor wiring stays wheels-up. A Pico without Wi-Fi cannot do this lab; a Pico W can, but the commands below are the ESP32 Arduino / PlatformIO path because that is the board most Capstone kits in Vietnam actually ship.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-publishsubscribe-middleware-network-latencyjitter-and-control-hierarchy&quot;&gt;Key ideas: Publish–Subscribe Middleware, Network Latency/Jitter, and Control Hierarchy&lt;/h2&gt;

&lt;p&gt;Wireless robotics networking combines three foundational concepts from distributed systems and control:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Publish–Subscribe Decoupling (MQTT Broker):&lt;/strong&gt; Unlike point-to-point TCP sockets, MQTT routes named topics (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot1/cmd&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;robot1/telemetry&lt;/code&gt;) through a broker. Publishers and subscribers do not need to know each other’s IP addresses, mirroring the exact topic abstraction you will use in ROS 2 (Chapter 09).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quality of Service (QoS 0, 1, 2) &amp;amp; Last Will:&lt;/strong&gt; QoS 0 (“at most once”) minimizes latency for high-rate telemetry; QoS 1 (“at least once”) guarantees delivery for state commands; and MQTT’s &lt;em&gt;Last Will and Testament&lt;/em&gt; (LWT) automatically notifies the fleet if a robot drops off Wi-Fi unexpectedly.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Control-Loop Separation Principle:&lt;/strong&gt; Because Wi-Fi introduces unbounded retry jitter (tens to hundreds of milliseconds), high-rate closed-loop wheel PID (50–200 Hz) must run &lt;strong&gt;locally on the MCU&lt;/strong&gt;, while wireless carries lower-rate setpoints (5–20 Hz) protected by a local watchdog.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ROS 2 is not MQTT, but the habits transfer. A topic name is a contract, a retained stale command is a loaded gun, and a transport that can vanish needs a local failsafe. When you reach Nav2, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; will also go quiet in a corner with bad Wi-Fi if you later put the robot PC on wireless. The watchdog you write here is the same idea as a ROS 2 deadline or a bridge that publishes zeros. Chapter 12’s learned policies are even less forgiving: a policy that outputs a chunk of motion still needs something underneath that drops torque when the link dies.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_mqtt_robot.png&quot; alt=&quot;MQTT topics and the serial failsafe&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;MQTT has three roles. The &lt;strong&gt;broker&lt;/strong&gt; accepts TCP connections and fans messages out. A &lt;strong&gt;publisher&lt;/strong&gt; sends a payload to a topic name. A &lt;strong&gt;subscriber&lt;/strong&gt; receives whatever is published on the names it asked for. The broker in this lab is Mosquitto on your laptop, bound to the lab LAN, port 1883, with no anonymous access from the public internet. Do not port-forward 1883 on a home router.&lt;/p&gt;

&lt;p&gt;Topic names for the Capstone robot:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;capstone/cmd      {&quot;v&quot;: 0.20, &quot;w&quot;: 0.00}
capstone/telem    {&quot;vl&quot;: 0.18, &quot;vr&quot;: 0.21, &quot;batt&quot;: 11.6, &quot;age_ms&quot;: 40}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt; is forward speed in metres per second and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt; is yaw rate in radians per second, the same meanings ROS 2 will use in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt;. The ESP32 may still convert those to the −100…100 serial scale internally. Write that conversion in one function so Chapter 10 does not invent a second one.&lt;/p&gt;

&lt;p&gt;QoS on MQTT is a delivery promise between one client and the broker. QoS 0 is at most once: fine for telemetry you will send again in 100 ms. QoS 1 is at least once: the broker may deliver a duplicate, so command handling must be idempotent (applying “v = 0.2” twice is fine; incrementing a counter twice is not). QoS 2 is exactly once and is more machinery than this robot needs.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;retained&lt;/strong&gt; flag is the trap. A retained &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone/cmd&lt;/code&gt; is stored by the broker and handed to the next subscriber immediately. If the last retained payload was &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;v&quot;: 0.3, &quot;w&quot;: 0.0}&lt;/code&gt; and the ESP32 reboots, the robot drives as soon as it reconnects. For commands, publish with retain &lt;strong&gt;false&lt;/strong&gt;, and publish an explicit zero on clean shutdown. For a static configuration topic, retain can be useful. Do not mix those habits.&lt;/p&gt;

&lt;p&gt;Wi-Fi on the ESP32 is 2.4 GHz only for the classic DevKit. A 5 GHz-only SSID looks like “the password is wrong.” &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MQTT_MAX_PACKET_SIZE&lt;/code&gt; in older PubSubClient builds is 256 bytes; a large JSON status will silently fail to publish. Keep telemetry short.&lt;/p&gt;

&lt;p&gt;The firmware rule that matters more than the library: store &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last_cmd_ms&lt;/code&gt; inside the MQTT callback, and in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop()&lt;/code&gt; if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis() - last_cmd_ms &amp;gt; 300&lt;/code&gt; force the motor command to zero. Parsing JSON must not be allowed to block the motor loop for tens of milliseconds. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;client.loop()&lt;/code&gt; or a reconnect attempt takes too long, you will measure that damage in lesson 08-05.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Laptop broker and a manual publisher, before the ESP32 is powered:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt update
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; mosquitto mosquitto-clients
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl &lt;span class=&quot;nb&quot;&gt;enable&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--now&lt;/span&gt; mosquitto
ss &lt;span class=&quot;nt&quot;&gt;-ltnp&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;1883
mosquitto_sub &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; 127.0.0.1 &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;capstone/#&apos;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In a second terminal:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mosquitto_pub &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; 127.0.0.1 &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; capstone/cmd &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;{&quot;v&quot;:0.2,&quot;w&quot;:0.0}&apos;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The subscriber prints the topic and the JSON once. Publish a second message with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r&lt;/code&gt; (retain) and restart &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mosquitto_sub&lt;/code&gt;: the retained message appears immediately, with no new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pub&lt;/code&gt;. That is the demo you will not use for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone/cmd&lt;/code&gt;. Clear a retained payload with an empty retained message:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mosquitto_pub &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt; 127.0.0.1 &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; capstone/cmd &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;PlatformIO &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; for a typical DevKit:&lt;/p&gt;

&lt;div class=&quot;language-ini highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;[env:esp32dev]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;platform&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;espressif32&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;board&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;esp32dev&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;framework&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;arduino&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;lib_deps&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;knolleary/PubSubClient@^2.8, bblanchon/ArduinoJson@^7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The callback only copies numbers and a timestamp. Motor code reads those globals. Pseudocode you should be able to map onto PubSubClient:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;on_message(topic, payload):
    if topic != &quot;capstone/cmd&quot;: return
    parse v, w as floats
    if |v| &amp;gt; 0.5 or |w| &amp;gt; 1.5: return   # refuse a wild command
    left, right = diff_drive(v, w)      # lesson 08-04
    set_pending(left, right)
    last_cmd_ms = millis()

loop():
    if millis() - last_cmd_ms &amp;gt; 300: set_pending(0, 0)
    apply_pending_to_driver()
    client.loop()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Find the laptop’s LAN address with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ip -4 addr&lt;/code&gt; and put that in the ESP32, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;127.0.0.1&lt;/code&gt;. From the board, localhost is the board itself.&lt;/p&gt;

&lt;h3 id=&quot;explanation-why-closed-loop-pid-fails-over-wi-fi-phase-lag-calculation&quot;&gt;Explanation: Why Closed-Loop PID Fails Over Wi-Fi (Phase-Lag Calculation)&lt;/h3&gt;

&lt;p&gt;Let us quantify why you should send &lt;em&gt;velocity setpoints&lt;/em&gt; over Wi-Fi/MQTT while keeping the PID loop on the ESP32:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Phase lag from network delay:&lt;/strong&gt; A pure time delay \(\tau_d\) introduces phase lag \(\Delta\phi(\omega) = -\omega \tau_d\) (in radians) at frequency \(\omega = 2\pi f\). A feedback loop loses stability when total phase lag reaches \(180^\circ\) (\(\pi\) rad) while loop gain is still \(\ge 1\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compare local MCU delay vs. Wi-Fi jitter:&lt;/strong&gt; Suppose your wheel velocity loop has a crossover frequency of \(f_c = 4\ \text{Hz}\) (\(\omega_c = 2\pi(4) = 25.13\ \text{rad/s}\)):
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;On-MCU loop (\(\tau_d = 5\ \text{ms} = 0.005\ \text{s}\)):&lt;/strong&gt; Phase lag is \(25.13 \times 0.005 = 0.126\ \text{rad} = 7.2^\circ\) — negligible, leaving plenty of phase margin.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Over Wi-Fi (typical spike \(\tau_d = 80\ \text{ms} = 0.080\ \text{s}\)):&lt;/strong&gt; Phase lag jumps to \(25.13 \times 0.080 = 2.01\ \text{rad} = 115.2^\circ\)! Added to the motor’s own mechanical lag (\(\approx 70^\circ\)), total lag exceeds \(185^\circ\) and the wheel breaks into violent oscillation.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Architectural takeaway:&lt;/strong&gt; Run inner velocity PID locally on the ESP32; use MQTT/Wi-Fi for outer supervisory commands and telemetry.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Start Mosquitto. Confirm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ss&lt;/code&gt; shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.0.0.0:1883&lt;/code&gt; or your LAN address, not a public interface you do not control.&lt;/li&gt;
  &lt;li&gt;Subscribe with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mosquitto_sub -h &amp;lt;laptop-ip&amp;gt; -t &apos;capstone/#&apos; -v&lt;/code&gt; from the laptop.&lt;/li&gt;
  &lt;li&gt;Flash the ESP32 with your SSID, password, and broker IP. Open the serial log at 115200.&lt;/li&gt;
  &lt;li&gt;Publish &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;v&quot;:0.0,&quot;w&quot;:0.0}&lt;/code&gt; and then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;v&quot;:0.2,&quot;w&quot;:0.0}&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Watch telemetry come back. Then stop publishing for one second.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Expected serial log (shape, not exact numbers)&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wifi ip 192.168.1.42
mqtt connected
cmd v=0.20 w=0.00 -&amp;gt; L=28 R=28
telem published
watchdog stop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected subscriber line&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;capstone/telem {&quot;vl&quot;:0.00,&quot;vr&quot;:0.00,&quot;batt&quot;:11.8,&quot;age_ms&quot;:320}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Wheels-up: you should hear both motors nudge and then go quiet after you stop publishing. If they keep humming, the watchdog is not in the path that writes PWM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 never gets an IP&lt;/td&gt;
      &lt;td&gt;5 GHz SSID, wrong password, or the AP isolates clients (“guest network”)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;IP is fine, MQTT fails&lt;/td&gt;
      &lt;td&gt;Broker firewall, Mosquitto listening on localhost only (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listener&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind_address&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mosquitto.conf&lt;/code&gt;)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot drives immediately on boot&lt;/td&gt;
      &lt;td&gt;Retained command. Clear it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mosquitto_pub -r -n&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Telemetry never appears&lt;/td&gt;
      &lt;td&gt;Packet bigger than the library limit, or you subscribed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone/cmd&lt;/code&gt; only&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Motors stutter while connected&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;client.loop()&lt;/code&gt; plus DHCP reconnect blocking the 50 Hz loop. Lesson 08-05&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;You already own an ESP32 if you built Capstone A. Buy a second DevKit only if you want the broker experiment while the robot stays assembled.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Shop&lt;/th&gt;
      &lt;th&gt;Search&lt;/th&gt;
      &lt;th&gt;2026 band&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 DevKit with Wi-Fi (not the ESP32-C3 unless you enjoy a different pin map)&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; search “ESP32”&lt;/td&gt;
      &lt;td&gt;Shopee/Lazada: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 DevKit V1 30 chân&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 80.000–180.000 VND&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2.4 GHz USB Wi-Fi adapter, only if the laptop has no 2.4 GHz radio&lt;/td&gt;
      &lt;td&gt;optional&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;USB wifi 2.4 GHz&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 80.000–200.000 VND&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A phone hotspot on 2.4 GHz is a fair substitute for a router during the lab. Do not use mobile data as the broker path; the broker and the ESP32 must see each other on one LAN. The Raspberry Pi 5 you may buy in Chapter 09 can host Mosquitto later; it is not required tonight. Pi 5 boards at &lt;a href=&quot;https://hshop.vn/may-tinh-raspberry-pi-5-made-in-uk&quot;&gt;Hshop&lt;/a&gt; start around a few million VND depending on RAM; recheck the variant price on the page.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Publish a retained &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{&quot;v&quot;:0.3,&quot;w&quot;:0.0}&lt;/code&gt;, reboot only the ESP32, and write down what the wheels do. Then clear the retain flag’s payload. Guidance: motion on boot means retain; after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-r -n&lt;/code&gt;, boot must be stationary.&lt;/li&gt;
  &lt;li&gt;Why is QoS 1 acceptable for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;capstone/cmd&lt;/code&gt; while a retained message is not? Guidance: a duplicate of the same setpoint is harmless; a stored setpoint from yesterday is not a duplicate, it is a surprise.&lt;/li&gt;
  &lt;li&gt;Your JSON uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt; one day and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt; the next. Which program is wrong? Guidance: both, until one schema is written down. Pick &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt; and reject unknown keys.&lt;/li&gt;
  &lt;li&gt;Estimate how old a command may be if the ESP32 reconnects Wi-Fi for 2 seconds and the last payload was not zero. Guidance: without a watchdog, 2 seconds of the old PWM; with a 300 ms watchdog, at most one extra control period after the link dies, then zeros.&lt;/li&gt;
  &lt;li&gt;Sketch the two-line comment you will copy into the ROS 2 bridge: “MQTT is optional. Serial timeout is mandatory.” Guidance: that sentence is the whole point of the figure.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://mosquitto.org/man/mosquitto-8.html&quot;&gt;Mosquitto man pages&lt;/a&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listener&lt;/code&gt; and where the broker binds.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/knolleary/pubsubclient&quot;&gt;PubSubClient API&lt;/a&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;connect&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt;, and the packet-size limit.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html&quot;&gt;MQTT 3.1.1 QoS&lt;/a&gt; section 4.3 if you want the delivery rules in the spec’s own words.&lt;/li&gt;
  &lt;li&gt;ROS 2 QoS in &lt;a href=&quot;https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Quality-of-Service-Settings.html&quot;&gt;the Jazzy concepts page&lt;/a&gt;, which you will meet properly in Chapter 09. Similar vocabulary, different transport.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Serial protocols beyond println</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter08/08_01_serial_protocols/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter08/08_01_serial_protocols</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;~90 minutes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Every beginner starts debugging microcontrollers with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.println(&quot;ok&quot;)&lt;/code&gt;, and for blinking LEDs that works fine. The moment a robot streams encoder ticks at 50 Hz while receiving velocity commands over the same wire, unstructured text collapses into garbled lines and dropped bytes. Moving beyond &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println&lt;/code&gt; means adopting framed packets, checksums, and deterministic state-machine parsers.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can design a small wheel-command frame that survives a split USB read, implement it on the host in Python and on an ESP32 or Pico, and explain why a bare &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println&lt;/code&gt; of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;40,-15&quot;&lt;/code&gt; is not a protocol. You will compute a one-byte XOR check, name the timeout that must zero the motors, and record one captured frame plus one deliberate failure in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. That skill is the contract Chapter 10’s ROS 2 bridge will reuse, so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; never becomes “whatever bytes happened to be in the buffer.”&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can blink an LED and print a line from Chapter 02, and you have a Capstone A teleop sketch from Chapter 07, even if it still sends raw text. You know a byte is 8 bits and that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200 8N1&lt;/code&gt; means 115200 bits per second, 8 data bits, no parity, one stop bit. A laptop with Python 3 is enough for the dry run. An ESP32 or Pico is the hardware version of the same lab.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-byte-stream-framing-error-detection-xorcrc-and-finite-state-parsers&quot;&gt;Key ideas: Byte-Stream Framing, Error Detection (XOR/CRC), and Finite-State Parsers&lt;/h2&gt;

&lt;p&gt;Robust robot serial links rely on three pillars from computer networking and embedded software:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Framing &amp;amp; Synchronization:&lt;/strong&gt; Because UART transmits raw bytes without packet boundaries, receivers must locate frame starts using either a unique delimiter (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt; in ASCII or COBS zero-byte framing in binary) or a sync header (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xAA 0x55&lt;/code&gt;) paired with an explicit payload length byte.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Integrity Verification (Checksum / CRC):&lt;/strong&gt; Electromagnetic noise from brushed DC motors can flip bits on a UART line. Appending an XOR checksum or 8-bit CRC (CRC-8) lets the receiver discard corrupted frames silently rather than feeding a bit-flipped &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v = 255&lt;/code&gt; into the motor driver.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Non-Blocking Finite-State Parsing:&lt;/strong&gt; Reading one byte at a time into a state machine (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WAIT_SYNC -&amp;gt; READ_LEN -&amp;gt; READ_PAYLOAD -&amp;gt; CHECK_CRC&lt;/code&gt;) guarantees bounded worst-case execution time per control tick and zero heap allocation.
    &lt;h2 id=&quot;why-this-sits-on-the-path&quot;&gt;Why this sits on the path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chapter 07 proved the robot can move when you type on a serial line. Chapter 08 turns that line into something a program can trust. Chapter 09 will introduce ROS 2 topics, which are also framed messages with types. Chapter 10’s bridge node is only a translator: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; in, this frame out. If the frame is ambiguous, every later stack (Nav2, a vision loop, a learned policy) inherits a robot that lunges when a byte is lost. Robot learning in Chapter 12 assumes the low-level command channel already fails safe.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ch08_serial_frame.png&quot; alt=&quot;Serial frame fields&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;concepts&quot;&gt;Concepts&lt;/h2&gt;

&lt;p&gt;A UART moves bytes, not messages. The USB-serial chip delivers whatever has arrived, which may be half a line, two lines, or a fragment left over from the previous read. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.println(&quot;40,-15&quot;)&lt;/code&gt; looks fine in the Arduino Serial Monitor because the monitor waits for a newline and then shows you text. A parser in a tight loop does not get that courtesy. It must answer three questions: where does a message start, how long is it, and did it arrive intact?&lt;/p&gt;

&lt;p&gt;Two framings are enough for this course. A &lt;strong&gt;delimiter frame&lt;/strong&gt; ends on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt; (byte &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x0A&lt;/code&gt;) and is easy to read in a terminal. A &lt;strong&gt;length frame&lt;/strong&gt; starts with a marker, then a count, then that many payload bytes, then a check. Length framing still works when the payload itself contains a newline or a comma. The Capstone bridge in Chapter 10 will speak the length frame. Keep the delimiter form as a debug view you can type by hand.&lt;/p&gt;

&lt;p&gt;The text payload stays human-readable:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;V,40,-15
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; means “velocity command,” &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;40&lt;/code&gt; is the left wheel in the same −100…100 units as Chapter 07, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-15&lt;/code&gt; is the right wheel. Units belong in the comment at the top of both programs. Do not silently switch one side to metres per second.&lt;/p&gt;

&lt;p&gt;The check in this lesson is an XOR of every payload byte, stored in one byte. It catches many single-bit errors and most truncated frames. It is not a security feature. A CRC would be the next step if you ever leave the lab bench; you do not need it to learn the state machine.&lt;/p&gt;

\[c = b_0 \oplus b_1 \oplus \cdots \oplus b_{n-1}\]

&lt;p&gt;The wire image is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STX | LEN | payload | c | END&lt;/code&gt;, with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STX = 0xAA&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;END = 0x0A&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEN&lt;/code&gt; is the payload length, not the length of the whole frame. Reject &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEN&lt;/code&gt; above 32 so a corrupted length cannot make you allocate a huge buffer on a microcontroller.&lt;/p&gt;

&lt;p&gt;The receiver is a small state machine: hunt for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xAA&lt;/code&gt;, read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEN&lt;/code&gt;, read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEN&lt;/code&gt; bytes, read the check byte, read the end byte, compare the XOR, then apply the command. Any surprise returns to “hunt.” If no complete good frame arrives for 300 ms, the applied wheel command becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0,0&lt;/code&gt;. That timeout is the safety property. A pretty parser without it will replay the last command forever when the USB cable falls out.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Build a frame for left = 40 and right = −15.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;wheel command out of range&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;V,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ascii&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xAA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You should see a line that starts with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aa&lt;/code&gt; and ends with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0a&lt;/code&gt;, and a payload that decodes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,40,-15&lt;/code&gt;. Re-run with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame(40, -15)&lt;/code&gt; twice: the hex must be identical. Change the right wheel to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-16&lt;/code&gt; and only the payload and the XOR byte should change. If you flip one payload byte by hand and recompute the XOR, the original check byte must no longer match. That is the test you will automate in the exercises.&lt;/p&gt;

&lt;p&gt;On the microcontroller, do not use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; while you wait for bytes. Append whatever &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read()&lt;/code&gt; returns into a small buffer and run the state machine once per loop. Chapter 08-05 measures that loop. For this lesson, correctness of the frame matters more than the rate.&lt;/p&gt;

&lt;p&gt;Host side, with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pyserial&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/dev/ttyUSB0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Replace the port with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; PTY from the lab when the robot is not on the bench. Wheels stay off the ground whenever a real motor driver is connected.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-computing-a-binary-frame-and-its-xor-checksum&quot;&gt;Explanation: Hand-Computing a Binary Frame and Its XOR Checksum&lt;/h3&gt;

&lt;p&gt;Suppose our compact binary velocity command has the format &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[SYNC=0xAA] [TYPE=0x01] [v_L: int8] [v_R: int8] [CHK]&lt;/code&gt;, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHK&lt;/code&gt; is the bitwise XOR of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TYPE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v_L&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v_R&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Encode command \(v_L = +40\), \(v_R = -20\) (signed 8-bit two’s complement):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TYPE&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x01&lt;/code&gt; (binary &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0000 0001&lt;/code&gt;).&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v_L = +40&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x28&lt;/code&gt; (binary &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0010 1000&lt;/code&gt;).&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v_R = -20&lt;/code&gt;: in two’s complement, \(256 - 20 = 236 = \text{0xEC}\) (binary &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1110 1100&lt;/code&gt;).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute the XOR checksum bit by bit:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x01 XOR 0x28&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0000 0001 ^ 0010 1000 = 0010 1001&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;).&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29 XOR 0xEC&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0010 1001 ^ 1110 1100 = 1100 0101&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xC5&lt;/code&gt;).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Final 5-byte wire packet:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AA 01 28 EC C5&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify bit-flip detection:&lt;/strong&gt; If motor EMI flips bit 7 of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v_R&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xEC&lt;/code&gt; becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x6C&lt;/code&gt;, which decodes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+108&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-20&lt;/code&gt;!), the receiver recomputes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x01 ^ 0x28 ^ 0x6C = 0x45 != 0xC5&lt;/code&gt; and immediately &lt;strong&gt;drops&lt;/strong&gt; the corrupted frame instead of violently reversing the right wheel.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Dry run, no robot required.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python3 &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;pyserial
socat &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; pty,raw,echo&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0 pty,raw,echo&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;socat&lt;/code&gt; prints two device names, for example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/pts/3&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/pts/4&lt;/code&gt;. Point the sender at one and the parser at the other. Parser sketch:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/dev/pts/4&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytearray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;xor_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xAA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0A&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;check&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;xor_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;DROP&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected output&lt;/strong&gt; after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;send(40, -15)&lt;/code&gt; from the other PTY:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;OK b&apos;V,40,-15&apos; decoded as V,40,-15
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Send ten frames in a loop. You should see ten &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt; lines and zero &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DROP&lt;/code&gt; lines. Then corrupt one frame by writing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&quot;\xAA\x05V,40&quot;&lt;/code&gt; with no check: the parser must not print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt;. Unplug is simulated by simply stopping the sender: your firmware notes should say the last command expires. On a real board, confirm with a serial log that both PWM pins go inactive within about 300 ms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failure modes&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Endless garbage, no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Baud mismatch, often 9600 vs 115200, or you opened the same PTY twice&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK&lt;/code&gt; never arrives, buffer grows&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEN&lt;/code&gt; does not match the payload you actually sent&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Motors run after the cable is pulled&lt;/td&gt;
      &lt;td&gt;Timeout not implemented; last command latched&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Works in Serial Monitor, fails in Python&lt;/td&gt;
      &lt;td&gt;Monitor adds a newline; your script forgot &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;END&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flush&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 resets when motors start&lt;/td&gt;
      &lt;td&gt;Motors powered from USB. Use the Chapter 07 pack and a common ground&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;where-to-buy-this-in-vietnam&quot;&gt;Where to buy this in Vietnam&lt;/h2&gt;

&lt;p&gt;The dry run needs only a laptop. The hardware version uses the same board as Capstone A.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Shop&lt;/th&gt;
      &lt;th&gt;Search on Shopee or Lazada&lt;/th&gt;
      &lt;th&gt;2026 band, check before paying&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 DevKit V1 (30-pin, WROOM-32)&lt;/td&gt;
      &lt;td&gt;Search “ESP32” on &lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 DevKit V1 30 chân&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 80.000–180.000 VND&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;USB-TTL CP2102 or CH340, 3.3 V&lt;/td&gt;
      &lt;td&gt;same&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CP2102 USB TTL 3.3V&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 25.000–60.000 VND&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pico or Pico W, if you stay on MicroPython&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/&quot;&gt;hshop.vn&lt;/a&gt; search “Raspberry Pi Pico”&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Raspberry Pi Pico&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;about 120.000–250.000 VND&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A 5 V USB-TTL module must still speak 3.3 V logic into an ESP32 RX pin. Power the motors from the robot pack, not from the laptop USB port. Prices move; the band is a budget check, not a quote.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Compute the XOR of the ASCII bytes in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,0,0&lt;/code&gt; by hand, then confirm with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame()&lt;/code&gt; function. Guidance: XOR is commutative, so order of bytes does not matter, and the result is a single byte in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x00&lt;/code&gt;–&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xFF&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Explain, in three sentences, a case where a newline-delimited parser accepts a command the length-frame parser rejects. Guidance: a payload that contains an embedded &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;, or a second command glued on before you finish the first, is the situation to describe.&lt;/li&gt;
  &lt;li&gt;Your log shows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OK V,40,-15&lt;/code&gt; then 2 seconds of silence while the wheels keep spinning. What line is missing in the firmware? Guidance: a timestamp of the last good frame and a comparison with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Change the protocol so a command outside −100…100 is dropped rather than clamped. Which side should enforce that, host or MCU? Guidance: the MCU must enforce it. The host check is only a convenience.&lt;/li&gt;
  &lt;li&gt;Write the sentence you will put in the Chapter 10 bridge comments: units on the wire, units in ROS, and the timeout. Guidance: ROS side is m/s and rad/s later; this wire is still −100…100 until that lesson converts them in one function.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://pyserial.readthedocs.io/en/latest/shortintro.html&quot;&gt;pyserial documentation&lt;/a&gt; for ports, timeouts, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flush&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/communication/serial/&quot;&gt;Arduino Serial reference&lt;/a&gt; if your Capstone firmware is C++ rather than MicroPython.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ros.org/reps/rep-0103.html&quot;&gt;REP-103&lt;/a&gt; for the axis directions you will eventually map onto this frame.&lt;/li&gt;
  &lt;li&gt;Chapter 07 teleop notes in this course, and Chapter 10 lesson 04, which consumes the frame you just froze.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 07 capstone checklist</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter07/07_06_ch07_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter07/07_06_ch07_lab_checklist</id>
   <content type="html">&lt;p&gt;This is the sign-off for the first moving base. You have evidence for four behaviors, or you have one named repair and you are not starting Chapter 08.&lt;/p&gt;

&lt;p&gt;Capstone A is the milestone where separate components—acrylic plate, gearmotors, H-bridge, buck converter, and serial firmware—become an actual teleoperated robot. Signing off the Chapter 07 checklist is not a formality; it is the interface contract that lets Chapters 08–10 layer PID loops, kinematics, and ROS 2 on top without fighting hidden hardware bugs.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Judge a lab notebook against four definition-of-done lines: teleop in four directions, stop on host timeout, battery secured with no bare lithium leads, and notes that hold a photo, the protocol, the sign map, and the voltages.&lt;/li&gt;
  &lt;li&gt;Read one failed acceptance (a robot that keeps rolling after the laptop sleeps, or a right wheel that is reversed) and name the single fix that must land before PID or ROS topics.&lt;/li&gt;
  &lt;li&gt;Separate a real pass from a notebook that has the right headings and empty measurements.&lt;/li&gt;
  &lt;li&gt;List only the parts still missing to close this capstone, with the same shops as the rest of the chapter.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 07-01 through 07-05 are the evidence: assembly, wheels-up sign map, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, timeout and slew, floor log. Chapter 05 is the H-bridge, STBY, the 7805 jumper, and the stall current behind the 2–3 A fuse. Chapter 06 is \(b\) and \(r\) (examples \(0.15~\mathrm{m}\) or your \(148~\mathrm{mm}\), and \(31.2~\mathrm{mm}\)). Chapter 08 is PID. Chapter 09 replaces the text line with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;). Neither chapter repairs a missing timeout or a reversed tire. This checklist does not implement ROS.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-interface-contracts-hardware-in-the-loop-readiness-and-regression-baselines&quot;&gt;Key ideas: Interface Contracts, Hardware-in-the-Loop Readiness, and Regression Baselines&lt;/h2&gt;

&lt;p&gt;A capstone readiness checklist operationalizes three systems-engineering disciplines:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Hardware–Software Interface Contract:&lt;/strong&gt; Locking baud rate, packet syntax, sign convention (\(+\text{cmd}\) = forward on both wheels), and timeout behavior creates a stable abstraction boundary between low-level firmware and higher-level ROS 2 bridges.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reproducible Regression Baselines:&lt;/strong&gt; Recording measured no-load current, full-speed wheel velocity, and battery sag gives you a quantitative baseline; if a future firmware change degrades behavior, you can prove whether hardware drifted or software broke.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Safety-First Verification Order:&lt;/strong&gt; Verifying emergency power cut, watchdog stop, and brownout immunity &lt;em&gt;before&lt;/em&gt; adding autonomous planners ensures that no downstream software bug can override physical safety.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PID amplifies the plant. A reversed right wheel turns a positive error into a larger wrong command. A topic graph has the same stuck-PWM bug as serial if firmware keeps the last duty when messages stop. Twist replaces the text line only after the four done-lines below are true. Until Chapter 08 calibrates, PWM 80 is not \(0.2~\mathrm{m/s}\).&lt;/p&gt;

&lt;h2 id=&quot;definition-of-done&quot;&gt;Definition of done&lt;/h2&gt;

&lt;p&gt;All four lines are required.&lt;/p&gt;

&lt;p&gt;Teleop matches the lesson 02 sign map. Forward is two positive counts inside \(\pm 180\), both tires toward the nose. Back is both negative. Left spin is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; because right-faster is a left yaw. Right spin is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt;. The host refreshes at least every \(200~\mathrm{ms}\).&lt;/p&gt;

&lt;p&gt;Host timeout works. After \(250~\mathrm{ms}\) without a valid line the targets go to 0, and the slew of 15 counts per \(20~\mathrm{ms}\) walks the duty down. From a last command of 60 that is about \(330~\mathrm{ms}\), inside the \(0.5~\mathrm{s}\) you measured with the MCU still on the buck. The battery switch was in your hand on the floor. The switch does not replace the timeout, and the timeout does not replace the switch.&lt;/p&gt;

&lt;p&gt;The battery is strapped between the axle and the caster. Lithium is in a holder or a BMS pack, with no dangling soldered bare leads. Positive goes switch, then 2–3 A fuse, then VM and the buck. The buck was \(5.0~\mathrm{V}\) before the ESP32 was attached. Motor wires are on screws. Battery negative, driver GND, buck GND, and MCU GND are one node.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; holds the wiring photo, a protocol snippet (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, 115200 8N1, \(200~\mathrm{ms}\), \(250~\mathrm{ms}\)), the sign map, unloaded pack voltage, buck output, and \(b\) and \(r\). A blank cell is a missing measurement.&lt;/p&gt;

&lt;h2 id=&quot;a-notebook-that-fails-and-the-fix-that-blocks-chapter-08&quot;&gt;A notebook that fails, and the fix that blocks Chapter 08&lt;/h2&gt;

&lt;p&gt;Here is a log that must not be signed.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Time&lt;/th&gt;
      &lt;th&gt;Command&lt;/th&gt;
      &lt;th&gt;What the robot did&lt;/th&gt;
      &lt;th&gt;Voltage&lt;/th&gt;
      &lt;th&gt;Fault hypothesis&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;0:10&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;straight for 2 s&lt;/td&gt;
      &lt;td&gt;8.10 V rest, 7.95 V after&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0:20&lt;/td&gt;
      &lt;td&gt;laptop lid closed&lt;/td&gt;
      &lt;td&gt;kept rolling, hit the far tape&lt;/td&gt;
      &lt;td&gt;7.9 V&lt;/td&gt;
      &lt;td&gt;timeout never written; LEDC held 80&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The straight row can be fine while the second row is fatal. Chapter 08 and Chapter 09 both stream commands from a host that may die. Until the \(250~\mathrm{ms}\) test is in the sketch, killing the sender leaves the last duty applied. Put the test back, confirm on the box that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; dies within \(0.5~\mathrm{s}\), then repeat the lane. Unplugging the battery by habit is not the design.&lt;/p&gt;

&lt;p&gt;A second fail: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; yaws, and the wheels-up photo shows the right flag backward. One inversion only, either the right motor’s screws or the right IN polarity, and write which. Both cancels. That wheel is a plant gain of \(-1\). PID will add effort the wrong way, and Chapter 09 will publish it as if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; were honest.&lt;/p&gt;

&lt;p&gt;A third fail: two 18650 cells with soldered bare leads taped to the plate. A snag shorts upstream of a missing fuse, and the timeout never gets a vote. Fit a holder or a 2S BMS pack, a switch, and a 2–3 A fuse, and photograph them. A TP4056 is still the wrong charger: 1S, full near \(4.2~\mathrm{V}\), while the pack is full near \(8.4~\mathrm{V}\).&lt;/p&gt;

&lt;p&gt;Sign when a fresh log shows four directions, a stop inside \(0.5~\mathrm{s}\), resting voltage before and after (a short session might read \(8.15~\mathrm{V}\) then \(8.05~\mathrm{V}\)), the photo, and the geometry. If the pack fell from \(8.20~\mathrm{V}\) to \(6.90~\mathrm{V}\) at \(0.60~\mathrm{A}\),&lt;/p&gt;

\[R \approx \frac{1.30}{0.60} \approx 2.2~\Omega,\]

&lt;p&gt;you do not sign. That sag is a supply repair, not a software waiver.&lt;/p&gt;

&lt;h2 id=&quot;what-chapter-08-and-chapter-09-are-allowed-to-assume&quot;&gt;What Chapter 08 and Chapter 09 are allowed to assume&lt;/h2&gt;

&lt;p&gt;Chapter 08 may assume positive left and positive right both roll forward, that you have seen PWM 0 stop the robot, and that \(b\) and \(r\) are the numbers in the notes. It builds a speed loop. It does not rediscover your IN pins.&lt;/p&gt;

&lt;p&gt;Chapter 09 may assume the base stops when the stream stops. Twist will carry &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; instead of the two PWM integers, converted with Chapter 06’s equations and Chapter 08’s calibration. Do not write “80 counts = \(0.2~\mathrm{m/s}\)” into the sign-off. The protocol snippet is the spec that adapter has to beat: visible, clamped, and dead-host safe.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/teleop_states.png&quot; alt=&quot;Run only while the command is fresh; silence is stop&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If the log cannot point at that STOP box with a measurement, the checklist is not done.&lt;/p&gt;

&lt;h3 id=&quot;explanation-auditing-a-capstone-a-acceptance-log-by-hand&quot;&gt;Explanation: Auditing a Capstone A Acceptance Log by Hand&lt;/h3&gt;

&lt;p&gt;Before signing off Chapter 07, walk through a sample three-metric audit to verify whether a robot is truly ready for Chapter 08:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Left–right speed symmetry:&lt;/strong&gt; At open-loop command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+60&lt;/code&gt; on both channels, the left wheel rolls \(1.00\ \text{m}\) in \(4.10\ \text{s}\) (\(v_L = 0.244\ \text{m/s}\)) and the right wheel rolls \(1.00\ \text{m}\) in \(3.95\ \text{s}\) (\(v_R = 0.253\ \text{m/s}\)). Relative mismatch is:
\(\frac{\lvert v_R - v_L \rvert}{(v_R + v_L)/2} = \frac{0.009}{0.2485} = 3.6\%.\)
A 3.6% open-loop motor mismatch is normal for hobby gearmotors and easily corrected by Chapter 08 PID—whereas a 25% mismatch would signal a binding axle or failing H-bridge channel that must be fixed mechanically first.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Watchdog response check:&lt;/strong&gt; Host stops transmitting at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t = 12,400 ms&lt;/code&gt;; telemetry logs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STATE=TIMEOUT_STOP, pwm=0&lt;/code&gt; at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t = 12,710 ms&lt;/code&gt;, confirming actual timeout latency \(\Delta t = 310\ \text{ms}\) (within a \(300 \pm 20\ \text{ms}\) spec).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verdict:&lt;/strong&gt; Both mechanical symmetry and safety latency are quantified and logged—clearing the gate into Chapter 08.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Repeat the floor sequence only after a failed row is repaired. Otherwise review the paper log. The battery switch stays in reach if the tires can touch the floor. Do not sign a robot you have not stopped with the timeout. Bare lithium leads stop the work.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; from lessons 01–05&lt;/td&gt;
      &lt;td&gt;The evidence&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot, only if a row must be re-run&lt;/td&gt;
      &lt;td&gt;The retest&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Meter&lt;/td&gt;
      &lt;td&gt;Recheck buck 5.0 V and pack voltage if the photo is old&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;This checklist&lt;/td&gt;
      &lt;td&gt;The four lines you initial&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Find the wiring photo: switch, fuse, strap, common ground, motor wires on screws, buck near \(5.0~\mathrm{V}\) before the ESP32 was attached.&lt;/li&gt;
  &lt;li&gt;Initial forward, back, left, and right only where the log words match the commands.&lt;/li&gt;
  &lt;li&gt;Find the timeout row: sender killed or USB unplugged, MCU still on the buck, stop within \(0.5~\mathrm{s}\). A missing row means the chapter is not done.&lt;/li&gt;
  &lt;li&gt;Find \(b\), \(r\), the protocol snippet, and the voltages. Date the moment Chapter 08 may start, or name the one repair that blocks it.&lt;/li&gt;
  &lt;li&gt;A re-run uses the lesson 05 lane. One repair, then the whole sequence, then a new log.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A short sign-off paragraph in the notes: four directions passed on a stated date, stop time you measured, pack voltages, \(b\), \(r\), and the sentence “Chapter 08 may start” or a single blocking repair. Empty tables do not count.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Headings copied, cells blank&lt;/td&gt;
      &lt;td&gt;The chapter was not done; fill them from a real run&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Stop “passes” only by pulling the battery&lt;/td&gt;
      &lt;td&gt;Timeout was never tested; pull-the-pack is the kill, not the deadline&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Both leads and firmware were “fixed”&lt;/td&gt;
      &lt;td&gt;The two inversions cancel; return to one&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Geometry missing&lt;/td&gt;
      &lt;td&gt;Chapter 08 will invent a track width&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;PWM 80 written as \(0.2~\mathrm{m/s}\)&lt;/td&gt;
      &lt;td&gt;False calibration; delete it before PID&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy only the line that is still empty. Prices move. Rough 2026 bands: chassis 60–150k VND, TT motor 25–45k each, TB6612 25–70k, LM2596 10–25k, 2S pack or 18650 holder 80–180k, switch 8–20k, fuse holder and 2–3 A fuse 10–25k, ESP32 70–150k. L298N substitutes for a missing TB6612. A TP4056 is not a 2S charger.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified L298N, about \(45.000\) VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;Hshop TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=LM2596&quot;&gt;LM2596&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=ESP32&quot;&gt;ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=khung%20xe%202WD&quot;&gt;Shopee khung 2WD&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=c%E1%BA%A7u%20ch%C3%AC%202A&quot;&gt;cầu chì&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=pin%202S%2018650&quot;&gt;pin 2S&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=TB6612&quot;&gt;Lazada TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=ESP32&quot;&gt;ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=LM2596&quot;&gt;Thế Giới IC LM2596&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;The log shows a straight &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; and then a robot that keeps rolling after the laptop sleeps. Which done-line fails, and what exactly has to be true on the box before you touch Chapter 08 or Chapter 09?&lt;/li&gt;
  &lt;li&gt;Four directions passed. The right-hand notes say the leads were swapped and the firmware sign was inverted “to be sure.” What does positive PWM do now, and what do you undo?&lt;/li&gt;
  &lt;li&gt;Rest voltage \(8.20~\mathrm{V}\), loaded \(6.90~\mathrm{V}\) at \(0.60~\mathrm{A}\), photo shows bare soldered cell leads. Which done-lines fail, and what is \(R\)?&lt;/li&gt;
  &lt;li&gt;A classmate sets PWM 80 equal to \(0.20~\mathrm{m/s}\) and scales &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; the same way, with \(b = 0.15~\mathrm{m}\). What \(\omega\) do they falsely get, and why is the slope illegal?&lt;/li&gt;
  &lt;li&gt;From a command of 60 the robot took \(1.2~\mathrm{s}\) to stop after the sender died. Slew is 15 counts per \(20~\mathrm{ms}\) and the deadline is \(250~\mathrm{ms}\). What stop time should you have seen?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;“Stops on host timeout” fails. On the box, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; then kill the sender: both tires stop within \(0.5~\mathrm{s}\) with the MCU still on the buck. Then repeat the lane. 2. The two inversions cancel, so positive still rolls that tire backward. Undo one, wheels up. 3. Bare leads fail the battery line, and \(R \approx 1.30/0.60 \approx 2.2~\Omega\) fails the supply. Fit a holder or BMS pack, a switch, and a 2–3 A fuse. 4. Slope \(0.20/80 = 0.0025\) m/s per count makes left \(-0.15\) and right \(+0.15\), so \(v = 0\) and \(\omega = 0.30/0.15 = 2\) rad/s. The slope was never measured. 5. Expected stop is \(250 + 4\times 20 = 330~\mathrm{ms}\). A \(1.2~\mathrm{s}\) roll means the deadline is missing or far longer than \(250~\mathrm{ms}\). Do not sign.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/&quot;&gt;geometry_msgs package index (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;ST L298 datasheet&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Field test, debug log, and acceptance</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter07/07_05_field_acceptance/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter07/07_05_field_acceptance</id>
   <content type="html">&lt;p&gt;The box test is over. This lesson puts the robot on the floor in a clear lane, runs four directions at the clamped PWM, and writes a log that a later chapter can trust. Passing is a set of observations, not a feeling that it “sort of drove.”&lt;/p&gt;

&lt;p&gt;A robot sitting on blocks on a desk can look invincible—wheels spinning crisply, telemetry streaming cleanly—until the moment its tires touch the floor. Real floor tests expose voltage sag under load, caster shimmy on tile seams, and watchdog latencies that bench tests hide. Field acceptance is the disciplined ritual of turning a rolling trial into a signed, quantitative verdict.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Run a scripted floor sequence in a clear \(2~\mathrm{m}\) lane: forward about \(2~\mathrm{s}\), stop, back, spin left, spin right, all inside the \(\pm 180\) clamp, with a hand on the battery switch.&lt;/li&gt;
  &lt;li&gt;Show that killing the sender or unplugging USB, while the MCU stays on the buck, ends motion within \(0.5~\mathrm{s}\).&lt;/li&gt;
  &lt;li&gt;Record battery voltage before the run and after it, and treat a large sag as a pack, wiring, or fuse problem.&lt;/li&gt;
  &lt;li&gt;Fill a debug log (time, command, what the robot did, voltage, fault hypothesis) and accept the robot only when the sign map, the timeout, the strapped battery, and the Chapter 06 geometry are all in the notes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 07-01 through 07-04 are the plant: strapped pack, fused positive lead, buck at 5.0 V, sign map, text protocol, slew, and timeout. Chapter 05 is the stall and the hot-driver rule. Chapter 06’s \(b\) and \(r\) are already in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; (course examples \(b = 0.15~\mathrm{m}\) or your \(148~\mathrm{mm}\), and \(r = 31.2~\mathrm{mm}\)). You still do not publish Twist. You may record distance and time. You may not stamp them onto PWM as a law. Chapter 08 earns m/s.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-battery-internal-resistance-acceptance-testing-and-structured-telemetry&quot;&gt;Key ideas: Battery Internal Resistance, Acceptance Testing, and Structured Telemetry&lt;/h2&gt;

&lt;p&gt;Field testing sits at the intersection of electrical physics and test engineering:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Battery Internal Resistance &amp;amp; Voltage Sag:&lt;/strong&gt; A real battery behaves as an ideal open-circuit voltage \(V_{\text{oc}}\) in series with an internal resistance \(R_{\text{int}}\). Under acceleration current \(I\), terminal voltage drops to \(V_{\text{term}} = V_{\text{oc}} - I R_{\text{int}}\). If \(V_{\text{term}}\) dips below your buck converter’s dropout threshold, the MCU browns out mid-turn.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Acceptance Criteria &amp;amp; Pass/Fail Envelopes:&lt;/strong&gt; Professional robotics teams define numeric acceptance thresholds &lt;em&gt;before&lt;/em&gt; the run (stopping distance on kill/timeout, lateral drift over 2 m, minimum rail voltage under launch) so confirmation bias cannot turn a borderline run into a “pass.”&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Time-Stamped Telemetry Logging:&lt;/strong&gt; Recording &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms, v_bat, cmd_l, cmd_r, state&lt;/code&gt; at fixed intervals lets you reconstruct transient faults after the run instead of staring at a robot wondering why it paused.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chapter 08’s PID assumes the sign is right and that a lost command stops the wheels. A reversed right tire makes the proportional term add the wrong yaw. Chapter 09 assumes the base is safe when the publisher dies, and a sleeping laptop is that publisher. Acceptance is the gate: four directions match the sign map, silence stops the robot, the battery cannot fall off, and the notes hold a wiring photo, the protocol, the voltages, and \(b\) and \(r\).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;The geometry the log must cite: $$b$$ between contact centers, $$r$$ from the roll test&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/diff_drive_kinematics.png&quot; alt=&quot;Forward, spin, and the mixed case you are about to request with PWM, not with m/s&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The arrows in the second figure are the motions. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; is both arrows forward. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; is the spin whose right arrow is forward and whose left arrow is back, a left yaw when the sign map is honest. The figure’s formulas stay in speed units. Your commands stay in counts.&lt;/p&gt;

&lt;h2 id=&quot;a-lane-a-kill-switch-and-voltages-that-move&quot;&gt;A lane, a kill switch, and voltages that move&lt;/h2&gt;

&lt;p&gt;Clear a \(2~\mathrm{m}\) lane with no table edge in the braking space. Hold the battery switch. Host refresh \(200~\mathrm{ms}\), firmware deadline \(250~\mathrm{ms}\), duties at 60 or 80, not 255.&lt;/p&gt;

&lt;p&gt;Sequence, each command refreshed until you ask for the next:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; for about \(2~\mathrm{s}\), both tires forward, robot tracks the lane.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 0 0&lt;/code&gt; or simply stop refreshing and let the timeout fire. The robot stops.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -80 -80&lt;/code&gt; for about \(2~\mathrm{s}\), straight back.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; for a short spin. The nose goes left.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt; for a short spin. The nose goes right.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then kill the sender, or unplug USB while the ESP32 stays on the buck. Motion ends within \(0.5~\mathrm{s}\). Lesson 04 predicts about \(330~\mathrm{ms}\) after a last &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt;. Three seconds of rolling is a fail, even if the directions looked fine.&lt;/p&gt;

&lt;p&gt;Resting voltage before and after: a healthy short run might move from \(8.15~\mathrm{V}\) to \(8.05~\mathrm{V}\). If you can measure under load, take \(8.20~\mathrm{V}\) at rest and \(6.90~\mathrm{V}\) at about \(0.60~\mathrm{A}\):&lt;/p&gt;

\[R \approx \frac{1.30}{0.60} \approx 2.2~\Omega.\]

&lt;p&gt;About \(2.2~\Omega\) is huge next to a few hundred milliohms. Suspect tired cells, thin wire, a warming polyfuse, or a holder. Fix that before you accept the base. A bad fuse shows up as sag, a trip, or a mid-lane reset. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt; already writes PWM 0 before STBY rises.&lt;/p&gt;

&lt;p&gt;A timed leg is an observation only. Covering \(1.6~\mathrm{m}\) in \(4.0~\mathrm{s}\) is&lt;/p&gt;

\[v_{obs} = \frac{1.6}{4.0} = 0.40~\mathrm{m/s}\]

&lt;p&gt;on that run, that floor, that duty. Write “at PWM 80, this run, about \(0.40~\mathrm{m/s}\).” Do not write “PWM 80 means \(0.40~\mathrm{m/s}\)” as a Twist constant.&lt;/p&gt;

&lt;h2 id=&quot;the-log-is-the-acceptance-instrument&quot;&gt;The log is the acceptance instrument&lt;/h2&gt;

&lt;p&gt;Each row is one event. The hypothesis column stays empty when the event matched the sign map. It is mandatory when something else happened.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Time&lt;/th&gt;
      &lt;th&gt;Command&lt;/th&gt;
      &lt;th&gt;What the robot did&lt;/th&gt;
      &lt;th&gt;Voltage&lt;/th&gt;
      &lt;th&gt;Fault hypothesis&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;0:00&lt;/td&gt;
      &lt;td&gt;rest&lt;/td&gt;
      &lt;td&gt;stopped, pack strapped&lt;/td&gt;
      &lt;td&gt;8.15 V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0:12&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; for 2 s&lt;/td&gt;
      &lt;td&gt;straight along the lane&lt;/td&gt;
      &lt;td&gt;7.9 V under load&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0:15&lt;/td&gt;
      &lt;td&gt;sender killed&lt;/td&gt;
      &lt;td&gt;stopped in well under 0.5 s&lt;/td&gt;
      &lt;td&gt;8.12 V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0:30&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;nose left&lt;/td&gt;
      &lt;td&gt;8.0 V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0:40&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;nose right&lt;/td&gt;
      &lt;td&gt;8.0 V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Do not average a bad row into a pass. If the sender dies and the robot rolls into the tape, the timeout was never coded and the last PWM stayed in LEDC. Put lesson 04’s check back, prove the box stop, then repeat the lane. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; yaws, one sign is wrong: swap that motor’s leads or the firmware polarity, not both. Chapter 08 does not start while either row is still open.&lt;/p&gt;

&lt;p&gt;Pass means all of these: four directions match the sign map; the timeout stop works; the battery is strapped, with no bare soldered lithium leads; the notes include the wiring photo, a protocol snippet (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, \(200~\mathrm{ms}\), \(250~\mathrm{ms}\)), the sign map, the voltages, and \(b\) and \(r\).&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-checking-voltage-sag-and-stopping-distance-on-a-field-run&quot;&gt;Explanation: Hand-Checking Voltage Sag and Stopping Distance on a Field Run&lt;/h3&gt;

&lt;p&gt;Suppose your 2S Li-Ion pack reads \(V_{\text{oc}} = 7.40\ \text{V}\) at rest and has combined pack + wiring resistance \(R_{\text{int}} = 0.45\ \Omega\). Your 5 V buck regulator requires at least \(V_{\text{in,min}} = 6.20\ \text{V}\) to maintain clean 5.0 V output.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Predict voltage sag during a hard launch (\(I_{\text{peak}} = 2.4\ \text{A}\)):&lt;/strong&gt;
\(V_{\text{term}} = 7.40 - (2.4)(0.45) = 7.40 - 1.08 = 6.32\ \text{V}.\)
At 7.40 V (half-charged), \(6.32\ \text{V} &amp;gt; 6.20\ \text{V}\) passes with a slim \(0.12\ \text{V}\) margin.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check the same launch near end-of-charge (\(V_{\text{oc}} = 6.80\ \text{V}\)):&lt;/strong&gt;
\(V_{\text{term}} = 6.80 - 1.08 = 5.72\ \text{V} &amp;lt; 6.20\ \text{V}!\)
The 5 V rail droops and the ESP32 reboots right on launch. Lowering the slew-rate limit or tightening a loose battery connector (cutting \(R_{\text{int}}\) to \(0.20\ \Omega\)) raises \(V_{\text{term}}\) to \(6.32\ \text{V}\) and eliminates the brownout.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check stopping distance on watchdog trip:&lt;/strong&gt; Cruising at \(v = 0.35\ \text{m/s}\) with watchdog \(T_{\text{wd}} = 0.25\ \text{s}\) and coasting deceleration \(a = 1.4\ \text{m/s}^2\), total stopping distance is:
\(d_{\text{total}} = v T_{\text{wd}} + \frac{v^2}{2a} = 0.35(0.25) + \frac{0.35^2}{2(1.4)} = 0.0875 + 0.0438 = 0.131\ \text{m} \approx 13\ \text{cm}.\)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;The lane is clear for \(2~\mathrm{m}\) plus room to stop, and you hold the battery switch. Duties stay at the practice values. If the robot heads for a person or a drop, open the switch. A wall stall is one second, then the switch. A smell or a tab you cannot touch ends the session.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Accepted power tree and firmware&lt;/td&gt;
      &lt;td&gt;Lessons 01–04&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Clear floor, tape at \(2~\mathrm{m}\)&lt;/td&gt;
      &lt;td&gt;The lane&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Multimeter&lt;/td&gt;
      &lt;td&gt;Before and after voltage&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;The log&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Battery switch in your hand&lt;/td&gt;
      &lt;td&gt;The kill&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Confirm wheels are back on, caster down, pack strapped, photo already in the notes. Read the resting voltage.&lt;/li&gt;
  &lt;li&gt;Stand with the switch. Run forward about \(2~\mathrm{s}\), stop, back, spin left, spin right. Speak the command as you send it so the log stays honest.&lt;/li&gt;
  &lt;li&gt;Kill the sender or unplug USB with the buck still powering the ESP32. Time the stop. It must be within \(0.5~\mathrm{s}\).&lt;/li&gt;
  &lt;li&gt;Read the resting voltage again. If you saw a deep sag under load, estimate \(R\) and write the hypothesis.&lt;/li&gt;
  &lt;li&gt;Mark pass or fail against the four criteria. A fail points at one repair, then a full repeat.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A filled log, a stop inside half a second, resting voltages that did not collapse, and the word pass only if directions, timeout, battery, and notes (photo, protocol, sign map, \(b\), \(r\)) are all present. An observed speed may appear as a labeled observation. It is not wired into the protocol.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Straight command draws a circle&lt;/td&gt;
      &lt;td&gt;One sign is still flipped; one fix only&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Keeps rolling after the laptop sleeps&lt;/td&gt;
      &lt;td&gt;Timeout not in the sketch&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nose lifts as it accelerates&lt;/td&gt;
      &lt;td&gt;Pack mass is behind the axle; return to lesson 01&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Voltage sags by a volt or more at modest current&lt;/td&gt;
      &lt;td&gt;Cells, holder, wire, or polyfuse&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Stop works only when USB unplug also resets the chip&lt;/td&gt;
      &lt;td&gt;Buck was not the logic supply; retest with the MCU still powered&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Acceptance often fails on a missing strap, a tired holder, or a fuse you never bought. Prices move. Rough 2026 bands: 2S pack or holder 80–180k VND, switch 8–20k, fuse holder and 2–3 A fuse 10–25k, LM2596 10–25k, TB6612 25–70k. A verified L298N at about 45.000 VND is only the substitute bridge. Do not buy a new chassis to avoid swapping one motor lead.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified L298N, about \(45.000\) VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=cau+chi&quot;&gt;Hshop cầu chì&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=LM2596&quot;&gt;LM2596&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=pin%202S%2018650&quot;&gt;Shopee pin 2S&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=c%C3%B4ng%20t%E1%BA%AFc%20ngu%E1%BB%93n&quot;&gt;công tắc&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=holder%2018650%202S&quot;&gt;Lazada holder 18650&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=cau%20chi&quot;&gt;Thế Giới IC cầu chì&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Resting pack \(8.20~\mathrm{V}\), under both motors \(6.90~\mathrm{V}\) at about \(0.60~\mathrm{A}\). Estimate \(R\). Is that a passable supply or a sag you stop for?&lt;/li&gt;
  &lt;li&gt;The forward leg covers \(1.6~\mathrm{m}\) in \(4.0~\mathrm{s}\) at PWM 80. What observed speed do you write, and what sentence are you forbidden to write into the Twist notes?&lt;/li&gt;
  &lt;li&gt;USB is unplugged, the ESP32 is still on the buck, and the robot rolls for about \(3~\mathrm{s}\). Which acceptance line fails, and which lesson’s code is missing?&lt;/li&gt;
  &lt;li&gt;The robot drove the lane, and the notes have voltages and a photo, but \(b\) and \(r\) are blank. Why is that still a fail before Chapter 08?&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt; yaws the nose left. Name the single repair. What second change would cancel it?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(R \approx 1.30/0.60 \approx 2.2~\Omega\). That is huge next to a healthy pack at a few hundred milliohms. Stop, and suspect cells, a holder, thin wire, or a warming polyfuse. 2. \(v_{obs} = 1.6/4.0 = 0.40~\mathrm{m/s}\) for this run only. You do not write “PWM 80 = \(0.40~\mathrm{m/s}\)” as a converter constant. Chapter 08 calibrates. 3. The timeout stop fails. Lesson 04’s \(250~\mathrm{ms}\) deadline is missing or not reached, so the last PWM stays. 4. Chapter 08’s kinematics and PID need the track width and the rolling radius. A blank geometry means the controller will invent \(b\). Acceptance asked for those numbers from Chapter 06. 5. Swap the right motor leads or the right firmware sign, one of them. Doing both returns the left yaw. Then repeat the four directions.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;ST L298 datasheet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://pyserial.readthedocs.io/en/latest/shortintro.html&quot;&gt;pySerial short introduction&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Firmware teleop loop (no ROS yet)</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter07/07_04_teleop_firmware/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter07/07_04_teleop_firmware</id>
   <content type="html">&lt;p&gt;Lesson 03 specified the lines. This lesson is the ESP32 sketch that parses them, clamps them, slews them, and drops them to zero when the host goes quiet. ROS is still not in the loop. The wheels stay in the air.&lt;/p&gt;

&lt;p&gt;The leap from “blinking an LED with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt;” to real robotics firmware happens the day you write your first non-blocking control loop. If your MCU freezes inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; while a wheel is spinning toward a staircase, your safety timeout is blind for half a second. Real firmware runs on a steady heartbeat—reading serial bytes without blocking, clamping commands, ramping PWM smoothly, and enforcing the watchdog on every tick.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Parse &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sscanf&lt;/code&gt;, clamp each target to \(\pm 180\), and ignore a bad line.&lt;/li&gt;
  &lt;li&gt;Zero both targets after \(250~\mathrm{ms}\) of silence, and slew the applied PWM by 15 counts every \(20~\mathrm{ms}\).&lt;/li&gt;
  &lt;li&gt;Drive two channels with STBY high, and edit the example pins to the lesson 02 sign map.&lt;/li&gt;
  &lt;li&gt;Send &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V l r\n&lt;/code&gt; from Python, kill the process, and see both tires stop within \(0.5~\mathrm{s}\) while the MCU stays on the buck.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You need lesson 07-01’s power tree, lesson 07-02’s sign map, and lesson 07-03’s grammar: 115200 8N1, newline-terminated text, host refresh \(200~\mathrm{ms}\), firmware deadline \(250~\mathrm{ms}\), clamp \(-180..180\). On a TB6612, STBY low keeps the outputs off, and PWM 0 with STBY high is a short brake, not a high-Z coast. Chapter 06’s \(b\) and \(r\) stay in the notes; this sketch does not read them. Positive still means that tire toward the nose. Twist, with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt;, comes after Chapter 08 calibrates and Chapter 09 carries topics. No ROS workspace today.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-non-blocking-event-loops-slew-rate-limiting-and-embedded-safety-states&quot;&gt;Key ideas: Non-Blocking Event Loops, Slew-Rate Limiting, and Embedded Safety States&lt;/h2&gt;

&lt;p&gt;Reliable teleop firmware combines three core concepts from real-time embedded systems:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Non-Blocking Cooperative Scheduling:&lt;/strong&gt; Polling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;micros()&lt;/code&gt; instead of calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; allows the MCU to service serial RX, update PWM outputs, and check safety deadlines at a deterministic rate (e.g., 50 Hz = 20 ms period) with minimal jitter.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Slew-Rate Limiting (Acceleration Ramping):&lt;/strong&gt; Jumping instantly from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-100%&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+100%&lt;/code&gt; PWM produces a current spike of nearly \(2 \times I_{\text{stall}}\) (because back-EMF temporarily &lt;em&gt;adds&lt;/em&gt; to battery voltage during plug-reversing!), stripping plastic gears and browning out the 5 V rail. Bounding \(\lvert u_k - u_{k-1} \rvert \le \Delta u_{\max}\) per tick limits both current and jerk.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Explicit Fault &amp;amp; Timeout State Machine:&lt;/strong&gt; Separating &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE/SAFE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ACTIVE&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TIMEOUT_STOP&lt;/code&gt; states ensures that an expired watchdog cleanly zeroes hardware outputs and requires a fresh valid command to resume.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The bug that matters is a command that sticks: the host dies and the last LEDC duty keeps the robot rolling on battery power. The timeout belongs in firmware. A later Twist stream needs the same deadline. Chapter 09 replaces the parser, not the stop.&lt;/p&gt;

&lt;p&gt;The figure’s sense and compute boxes are the encoder and the PID you do not have yet. This lesson is the act box: PWM, and a timeout that forces zero.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sense_compute_act.png&quot; alt=&quot;Act is PWM to the driver; the timeout that forces zero is already part of the box&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;clamp-timeout-and-a-slew-you-can-count&quot;&gt;Clamp, timeout, and a slew you can count&lt;/h2&gt;

&lt;p&gt;Targets are what the last legal line asked for, after the clamp. Applied PWM is what the bridge is receiving, and it is allowed to trail the target. Every \(20~\mathrm{ms}\) the applied value steps at most 15 counts toward the target:&lt;/p&gt;

\[\text{steps} = \left\lceil \frac{|\text{target} - \text{applied}|}{15} \right\rceil, \qquad t_{slew} = \text{steps} \times 20~\mathrm{ms}.\]

&lt;p&gt;From 0 to 60 is 4 steps, \(80~\mathrm{ms}\). From 0 to 180 is 12 steps, \(240~\mathrm{ms}\). The windings never see a full step.&lt;/p&gt;

&lt;p&gt;If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; minus the last valid line exceeds \(250~\mathrm{ms}\), both targets become 0 and the slew walks the duty down. After an unrefreshed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt;, targets fall at \(250~\mathrm{ms}\) and applied PWM hits 0 about \(80~\mathrm{ms}\) later:&lt;/p&gt;

\[250~\mathrm{ms} + 4 \times 20~\mathrm{ms} = 330~\mathrm{ms}.\]

&lt;p&gt;That lands inside the \(0.5~\mathrm{s}\) stop. A command of 180 takes \(250+240=490~\mathrm{ms}\), which is why the clamp and the slew were chosen together. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; zeros the targets immediately; the slew still softens the brake. An illegal line does not refresh the deadline.&lt;/p&gt;

&lt;p&gt;Example pins, which you must edit to the lesson 02 map: STBY 4, AIN1 16, AIN2 17, PWMA 18, BIN1 19, BIN2 21, PWMB 22. Swap an IN pair in code or the motor screws, not both. Arduino-ESP32 3.x uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach(pin, freq, resolution)&lt;/code&gt;; 2.x uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcSetup&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttachPin&lt;/code&gt;, and the sketch shows both. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.setTimeout(10)&lt;/code&gt; keeps the slew from blocking.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Edit pins and IN polarity to the lesson 07-02 sign map.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Positive rolls that tire toward the nose.&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_STBY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_AIN1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_AIN2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_PWMA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_BIN1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_BIN2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_PWMB&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_FREQ&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_RES&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_LIM&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SLEW_STEP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SLEW_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;appliedL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;appliedR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastSlew&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;approach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SLEW_STEP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SLEW_STEP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;applied&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;drive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pwmPin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pwmPin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_STBY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_AIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_AIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_BIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_BIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_AIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_AIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_BIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_BIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Arduino-ESP32 3.x:&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcAttach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWMA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_FREQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_RES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcAttach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWMB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_FREQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_RES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Older 2.x cores, instead of ledcAttach / ledcWrite(pin, duty):&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ledcSetup(0, PWM_FREQ, PWM_RES); ledcAttachPin(PIN_PWMA, 0);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ledcSetup(1, PWM_FREQ, PWM_RES); ledcAttachPin(PIN_PWMB, 1);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// then ledcWrite(0, duty) and ledcWrite(1, duty) — the channel, not the pin.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWMA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWMB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_STBY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastRx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastSlew&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;available&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readBytesUntil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;&apos;\n&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;\0&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;\r&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;\0&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;S&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;\0&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;targetL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;targetR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lastRx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sscanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;V %d %d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;targetL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;constrain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PWM_LIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_LIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;targetR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;constrain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PWM_LIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_LIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lastRx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Any other line is ignored.&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;targetL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;targetR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastSlew&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SLEW_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastSlew&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;appliedL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;approach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;appliedL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;appliedR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;approach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;appliedR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;drive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_AIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_AIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_PWMA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;appliedL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;drive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_BIN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_BIN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_PWMB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;appliedR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Opening the port often resets the ESP32 through DTR. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt; drives PWM 0 before STBY goes high, so the reset is a stop. Do not trust DTR on close. The timeout stops the robot while the MCU stays on the buck. Unplugging USB tests that only if the buck, not USB, is powering the ESP32. USB-only power floats the pins and is not a timeout test.&lt;/p&gt;

&lt;p&gt;Use the Chapter 02 port, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyACM0&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleep(0.2)&lt;/code&gt; is the \(200~\mathrm{ms}\) refresh, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;close()&lt;/code&gt; is silence.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/dev/ttyUSB0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;V 60 60&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A second run writes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60\n&lt;/code&gt; for one second. Left forward and right back is a right yaw on the lesson 02 map. If the flags disagree, edit that channel’s IN pins, not the screws, in this session.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-tracing-a-slew-rate-limiter-over-5-loop-ticks&quot;&gt;Explanation: Hand-Tracing a Slew-Rate Limiter Over 5 Loop Ticks&lt;/h3&gt;

&lt;p&gt;Suppose your firmware runs at \(f_s = 50\ \text{Hz}\) (\(\Delta t = 20\ \text{ms}\)) with command range \([-100, +100]\) and a maximum slew step of \(\Delta u_{\max} = 10\) units per tick (reaching full speed from rest in \(10 \times 20\ \text{ms} = 200\ \text{ms}\)).&lt;/p&gt;

&lt;p&gt;Trace the actual PWM output \(u_k\) when the operator suddenly commands a step from \(0\) to \(+45\) at \(k = 1\), then drops the connection after \(k = 4\):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Tick \(k\)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Time (ms)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Target \(u_{\text{cmd}}\)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Clamped step \(\operatorname{clip}(u_{\text{cmd}} - u_{k-1}, -10, +10)\)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Applied \(u_k\)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;20&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;+45&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;+10&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;40&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;+45&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;+10&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;3&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;60&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;+45&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;+10&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;4&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;80&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;+45&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;+10&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;strong&gt;40&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;5&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;100&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;+45&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;+5&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;strong&gt;45&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;If the operator instead commands a hard reverse from \(+45\) to \(-45\), the slew limiter takes 9 ticks (\(180\ \text{ms}\)) to pass smoothly through \(0\)—preventing the plug-reversal current spike that would otherwise reset the ESP32.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Both tires stay in the air, clear of the table edge, and one hand can open the battery switch. The first command is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt;, not 255. This lesson does not go on the floor. A smell or a tab you cannot touch means open the switch. No bare lithium leads on an armed bench.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Assembled robot, wheels up&lt;/td&gt;
      &lt;td&gt;The plant&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 on the 5.0 V buck, USB for the program&lt;/td&gt;
      &lt;td&gt;Logic stays up when you kill the script&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sign map from lesson 07-02&lt;/td&gt;
      &lt;td&gt;Pin polarity&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Python 3 with pyserial&lt;/td&gt;
      &lt;td&gt;The host&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Phone timer or the millis arithmetic&lt;/td&gt;
      &lt;td&gt;The 0.5 s stop&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Edit pins and IN polarity so positive matches lesson 02. Flash. STBY high, duties start at 0.&lt;/li&gt;
  &lt;li&gt;Wheels up. Send &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; for about one second. Both flags move toward the nose.&lt;/li&gt;
  &lt;li&gt;Kill the script. Both tires stop within \(0.5~\mathrm{s}\) with the battery switch still on.&lt;/li&gt;
  &lt;li&gt;Send &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt; for one second. Yaw matches the map (right spin: left forward, right back). Kill the sender again.&lt;/li&gt;
  &lt;li&gt;Type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60&lt;/code&gt; alone. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; stops. The broken line does not move one wheel.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;Notes record the pins you used, the stop time (about \(330~\mathrm{ms}\) after the last &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt;), and the yaw for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt;. The robot is still on the box. A stuck duty after the process dies fails the lab.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Nothing moves&lt;/td&gt;
      &lt;td&gt;STBY still low, wrong PWM pins, or VM off&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Motion continues after you kill Python&lt;/td&gt;
      &lt;td&gt;Timeout missing, or the MCU was not actually running&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Only the left tire matches the map&lt;/td&gt;
      &lt;td&gt;Right IN pair disagrees with lesson 02; fix firmware or the screws, not both&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A one-wheel twitch on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sscanf&lt;/code&gt; was not required to return 2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Compile error on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Core is 2.x; use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcSetup&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttachPin&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;No new part if lessons 01 and 02 were real. A data-capable USB cable, or pyserial, is the usual gap. ESP32 devkit roughly 70–150k VND, TB6612 25–70k, L298N about 45.000 VND if you map ENA/ENB to the PWM pins and keep the buck. Prices move.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified L298N, about \(45.000\) VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=ESP32&quot;&gt;Hshop ESP32&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=ESP32%20devkit&quot;&gt;Shopee ESP32&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=cap%20micro%20USB%20data&quot;&gt;Lazada cáp USB&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=ESP32&quot;&gt;ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=ESP32&quot;&gt;Thế Giới IC ESP32&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Applied PWM is 0 and the target becomes 180. How many slew steps, and how many milliseconds, until the bridge sees 180?&lt;/li&gt;
  &lt;li&gt;The last &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 60&lt;/code&gt; arrives at \(t = 0\) and nothing follows. When do the targets become 0, and when does applied PWM reach 0?&lt;/li&gt;
  &lt;li&gt;The line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 200 -300&lt;/code&gt; parses. What targets does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constrain&lt;/code&gt; store? What does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60&lt;/code&gt; do to those targets?&lt;/li&gt;
  &lt;li&gt;You delete only the timeout test and leave the slew. The Python process is killed and the port does not reset the chip. What do the tires do, and why is that the bug lesson 05 will refuse?&lt;/li&gt;
  &lt;li&gt;Lesson 02 says positive right is BIN1 low, BIN2 high. Which lines do you change, and why do the motor screws stay put?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(180/15 = 12\) steps, \(240~\mathrm{ms}\). 2. Targets become 0 at \(250~\mathrm{ms}\); applied PWM needs 4 more steps, so it is 0 at about \(330~\mathrm{ms}\). 3. Targets become \(180\) and \(-180\). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60&lt;/code&gt; is ignored, so the targets stay. 4. The last duty remains and the tires keep spinning. Lesson 05 then fails the kill-the-sender stop. 5. Swap the BIN1 and BIN2 writes for a positive right command. Leave the screws. A second inversion undoes the fix.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/communication/serial/&quot;&gt;Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://pyserial.readthedocs.io/en/latest/shortintro.html&quot;&gt;pySerial short introduction&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Teleop over serial: protocol design</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter07/07_03_teleop_protocol/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter07/07_03_teleop_protocol</id>
   <content type="html">&lt;p&gt;Lesson 02 defined what a positive number does to each tire. This lesson names the bytes. The firmware loop is lesson 04.&lt;/p&gt;

&lt;p&gt;Imagine driving a robot across a room over USB serial when a loose cable drops half a line of text—turning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 20 20&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 200&lt;/code&gt; and sending the chassis straight into a wall. A teleoperation protocol is not just a print format; it is a &lt;strong&gt;safety contract&lt;/strong&gt; that defines framing, bounds, sign conventions, and what the robot must do the instant the operator goes silent.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Specify a UTF-8, newline-terminated text protocol at 115200 8N1: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V &amp;lt;left&amp;gt; &amp;lt;right&amp;gt;&lt;/code&gt; with integer wheel commands, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; as an emergency stop.&lt;/li&gt;
  &lt;li&gt;State the clamp that firmware will apply, \(-180\) to \(+180\), and explain why the learning range is not the full \(0\)–\(255\) duty.&lt;/li&gt;
  &lt;li&gt;Require a host refresh at least every \(200~\mathrm{ms}\) against a firmware deadline of \(250~\mathrm{ms}\), so silence becomes PWM 0 rather than a latched command.&lt;/li&gt;
  &lt;li&gt;Reject a malformed line entirely, and show with five concrete frames which motion each legal line requests under the lesson 02 sign convention.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 07-01 is a powered chassis with a common ground and no PWM habit yet. Lesson 07-02 is a written sign map: positive command, both tires toward the nose, corrected by one inversion method only. Chapter 05’s H-bridge is how those signs become voltage. Chapter 06’s geometry is still&lt;/p&gt;

\[v = \frac{v_r + v_l}{2}, \qquad \omega = \frac{v_r - v_l}{b},\]

&lt;p&gt;with \(b\) the track you measured (example \(b = 0.15~\mathrm{m}\), \(r = 31.2~\mathrm{mm}\)). You can open a serial monitor. Lesson 04 writes the parser. You are not publishing ROS topics.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-serial-framing-deterministic-parsing-and-fail-safe-watchdogs&quot;&gt;Key ideas: Serial Framing, Deterministic Parsing, and Fail-Safe Watchdogs&lt;/h2&gt;

&lt;p&gt;Designing a clean host-to-MCU command protocol draws on three pillars of embedded communications:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Packet Framing &amp;amp; Delimiters:&lt;/strong&gt; UART delivers an unstructured byte stream, not discrete messages. Adding a clear frame boundary (newline-terminated ASCII lines like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L,R\n&lt;/code&gt; or binary start-byte + length + checksum) lets the parser resynchronize cleanly after a corrupted or truncated packet.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Range Clamping &amp;amp; Sign Conventions:&lt;/strong&gt; Never trust incoming bytes blindly. Clamping every parsed command to a known safe envelope (e.g., \([-100, +100]\)) and locking a single sign convention (positive = forward on &lt;em&gt;both&lt;/em&gt; wheels, matching REP-103) prevents runaway PWM and inverted turns.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Command Deadlines (Watchdog Timeout):&lt;/strong&gt; In real-time control, silence is a fault, not “keep doing the last thing.” Every valid packet resets a timestamp &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last_cmd_ms&lt;/code&gt;; if \(t_{\text{now}} - t_{\text{last}} &amp;gt; T_{\text{timeout}}\) (typically 250–500 ms), firmware forces both motors to zero.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Teleop is the first closed conversation with the two motors. Text is the conversation you can read in a serial monitor; binary can wait until the line is boring and correct. Later the same idea is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; is forward speed, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; is yaw rate, and a driver splits them with the equations above. In this chapter the integers are PWM counts, not m/s. Chapter 08 calibrates. Writing “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; means \(0.2~\mathrm{m/s}\)” is a false calibration.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/teleop_states.png&quot; alt=&quot;A legal `V` line runs; age over 250 ms or an `S` line stops&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Read the figure as the contract. IDLE is motors off, waiting. A valid &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; line enters RUN and refreshes a deadline. STOP is command age greater than \(250~\mathrm{ms}\), or a line that is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;. PWM goes to 0. A new valid &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; may return to RUN. Silence must not leave the last duty running.&lt;/p&gt;

&lt;h2 id=&quot;the-bytes-the-clamp-and-the-deadline&quot;&gt;The bytes, the clamp, and the deadline&lt;/h2&gt;

&lt;p&gt;Port settings are 115200 baud, 8 data bits, no parity, 1 stop bit. Each line is UTF-8 text ending in a newline, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;. A carriage return before the newline is noise the parser may strip; it is not a second command.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V &amp;lt;left&amp;gt; &amp;lt;right&amp;gt;&lt;/code&gt; carries two integers. Firmware in the next lesson clamps each of them into \(-180..180\). The cap is deliberate. Full-scale 255 on a fresh 2S pack is a jump you have not earned, and lesson 02’s first spins lived at 60–80. One hundred eighty counts is&lt;/p&gt;

\[D = \frac{180}{255} \approx 0.706\]

&lt;p&gt;of the bridge supply, which is plenty for a floor test and still short of a hard start into a wall. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; sets both targets to 0 immediately as an emergency stop. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 0 0&lt;/code&gt; is also a stop, requested as an ordinary command rather than as a panic.&lt;/p&gt;

&lt;p&gt;The host sends a fresh line at least every \(200~\mathrm{ms}\) while it wants motion. Firmware treats \(250~\mathrm{ms}\) without a valid &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; as a dead host and forces the targets to 0. The \(50~\mathrm{ms}\) gap covers a late USB packet, not a laptop that slept. Silence means PWM 0. A duty that stays at 80 after the cable goes quiet is a broken deadline.&lt;/p&gt;

&lt;p&gt;A line that does not match is ignored: previous legal targets stay until the deadline, and garbage must not become a spin. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80&lt;/code&gt; is missing a wheel. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 foo&lt;/code&gt; is not a zero on the right. The letter is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;worked-frames-under-one-sign-convention&quot;&gt;Worked frames under one sign convention&lt;/h2&gt;

&lt;p&gt;Positive on a wheel is forward, the nose direction from lesson 02. Right faster than left is a left yaw, the same sign as Chapter 06, because&lt;/p&gt;

\[\omega = \frac{v_r - v_l}{b}\]

&lt;p&gt;is positive when \(v_r &amp;gt; v_l\). The symbols in that display are speeds. The numbers in the frames below are PWM counts. The sign pattern matches. The units do not.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Line&lt;/th&gt;
      &lt;th&gt;Left PWM&lt;/th&gt;
      &lt;th&gt;Right PWM&lt;/th&gt;
      &lt;th&gt;What you asked&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(+80\)&lt;/td&gt;
      &lt;td&gt;\(+80\)&lt;/td&gt;
      &lt;td&gt;Both forward, straight&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -80 -80&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(-80\)&lt;/td&gt;
      &lt;td&gt;\(-80\)&lt;/td&gt;
      &lt;td&gt;Both backward&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(-60\)&lt;/td&gt;
      &lt;td&gt;\(+60\)&lt;/td&gt;
      &lt;td&gt;Spin left (right tire forward, left tire back)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 0 0&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;\(0\)&lt;/td&gt;
      &lt;td&gt;\(0\)&lt;/td&gt;
      &lt;td&gt;Stop&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;targets 0&lt;/td&gt;
      &lt;td&gt;targets 0&lt;/td&gt;
      &lt;td&gt;Emergency stop&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 60 -60&lt;/code&gt; is the opposite yaw, a spin to the right, when the sign map is honest. A forgotten inversion from lesson 02 yaws the wrong way. Fix it there, once.&lt;/p&gt;

&lt;p&gt;Count &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60\n&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;, space, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, space, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, newline, nine bytes. At 115200 8N1 each byte is 10 bit-times:&lt;/p&gt;

\[t \approx \frac{9 \times 10}{115200} \approx 0.78~\mathrm{ms}.\]

&lt;p&gt;The wire time is under a millisecond. The \(200~\mathrm{ms}\) refresh exists so firmware can notice a dead host. Binary would not have made the UART the bottleneck, and you would lose the ability to type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Do not pretend the body-speed formula has already been measured. Forcing PWM 80 to mean \(0.20~\mathrm{m/s}\) on both wheels gives&lt;/p&gt;

\[v = \frac{0.20+0.20}{2} = 0.20~\mathrm{m/s}, \qquad \omega = 0,\]

&lt;p&gt;the arithmetic is tidy and the physics is invented. Chapter 08 may time a measured meter and only then claim a speed. Until then, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; means forward on the sign map, at 80 counts.&lt;/p&gt;

&lt;h3 id=&quot;explanation-serial-bandwidth-frame-time-and-watchdog-margin-budget&quot;&gt;Explanation: Serial Bandwidth, Frame Time, and Watchdog Margin Budget&lt;/h3&gt;

&lt;p&gt;Can a 115,200 baud UART comfortably carry a 50 Hz teleop loop with telemetry replies? Let us calculate the exact timing budget by hand:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Byte transmission time at 115,200 baud (8N1 = 10 bits/byte):&lt;/strong&gt;
\(t_{\text{byte}} = \frac{10\ \text{bits}}{115\,200\ \text{bits/s}} = 86.8\ \mu\text{s/byte}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Command frame airtime:&lt;/strong&gt; A compact ASCII command like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V,+045,-030\n&lt;/code&gt; is 12 bytes long, taking:
\(t_{\text{frame}} = 12 \times 86.8\ \mu\text{s} = 1.04\ \text{ms}.\)
At 50 Hz (one frame every \(20\ \text{ms}\)), commands consume only \(1.04 / 20 = 5.2\%\) of the serial link bandwidth—leaving plenty of headroom for status telemetry.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Choosing the watchdog timeout:&lt;/strong&gt; If the host sends at \(20\ \text{ms}\) intervals and USB-serial buffering can introduce up to \(40\ \text{ms}\) of jitter, a timeout of \(T_{\text{timeout}} = 300\ \text{ms}\) tolerates 14 dropped frames before tripping, yet stops a \(0.30\ \text{m/s}\) robot within:
\(d_{\text{stop}} = v \cdot T_{\text{timeout}} = 0.30 \times 0.30 = 0.09\ \text{m} = 9\ \text{cm}\)
if the host crashes or the cable is pulled.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;You are typing, not driving. If an old sketch is still flashed and a motor lead is attached, open the battery switch before the serial monitor. The deliverable is five lines. Lesson 04 is the first sketch that acts on them, wheels up.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; and a pen, or a serial monitor&lt;/td&gt;
      &lt;td&gt;Where the five frames go&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sign map from lesson 07-02&lt;/td&gt;
      &lt;td&gt;The meaning of positive&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Optional ESP32, USB, no VM&lt;/td&gt;
      &lt;td&gt;So you can see characters echo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Calculator&lt;/td&gt;
      &lt;td&gt;The 9-byte timing check&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Write the port line in the notes: 115200 8N1, UTF-8, newline terminated, clamp \(-180..180\), host period \(200~\mathrm{ms}\), firmware deadline \(250~\mathrm{ms}\).&lt;/li&gt;
  &lt;li&gt;Under your sign map, write the five frames: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -80 -80&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 0 0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;. Next to each, write straight, back, spin left, stop, emergency stop.&lt;/li&gt;
  &lt;li&gt;Invent two illegal lines, for example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 foo&lt;/code&gt;. Next to each write “ignore, do not spin one wheel.”&lt;/li&gt;
  &lt;li&gt;If you open a monitor, type the five legal lines and confirm you can read them. Do not enable the driver.&lt;/li&gt;
  &lt;li&gt;Compute the transmit time of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60\n&lt;/code&gt; the way the worked example did, and file it beside the \(200~\mathrm{ms}\) rule so the two timescales stay distinct.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;The notes contain the five frames, the sign sentence “positive is forward on both wheels,” the clamp, both times (\(200~\mathrm{ms}\) host, \(250~\mathrm{ms}\) firmware), and an explicit remark that PWM counts are not meters per second. A photo of the monitor is optional. A moving robot is not a result of this lesson.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; described as spin right&lt;/td&gt;
      &lt;td&gt;The sign convention was flipped; right-faster is left yaw&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A single number accepted as left-only&lt;/td&gt;
      &lt;td&gt;The grammar is being half-applied; ignore the whole line&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Deadline written as 2 s “to be safe”&lt;/td&gt;
      &lt;td&gt;A sleep then drives the robot for seconds; use \(250~\mathrm{ms}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80 80&lt;/code&gt; labeled \(0.2~\mathrm{m/s}\)&lt;/td&gt;
      &lt;td&gt;False calibration; Chapter 08 has not happened&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Binary struct copied from a blog&lt;/td&gt;
      &lt;td&gt;You cannot type it, and you cannot see it fail&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;This lesson adds no required part. If you still have no ESP32 to type against, the devkit is the gap. Prices move. Rough 2026 band: ESP32 devkit \(70.000\)–\(150.000\) VND. A CP2102 or CH340 board is fine. A Pico is a substitute only if you already rewrote the later sketch’s LEDC calls; this course’s lesson 04 is Arduino-ESP32. Do not buy a second motor driver to “make the protocol faster.”&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified L298N, about \(45.000\) VND, only if you still lack a bridge: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=ESP32&quot;&gt;Hshop ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=ESP32%20devkit&quot;&gt;Shopee ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=ESP32&quot;&gt;Lazada ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=ESP32&quot;&gt;Thế Giới IC ESP32&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;How many bytes are in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60\n&lt;/code&gt;, and how long is that frame at 115200 8N1?&lt;/li&gt;
  &lt;li&gt;The last legal line is at \(t = 0\). The laptop sleeps. At what time must firmware force the targets to 0, and why was the host supposed to speak every \(200~\mathrm{ms}\)?&lt;/li&gt;
  &lt;li&gt;The monitor receives &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80&lt;/code&gt; and then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 200 -90&lt;/code&gt;. Which line is illegal, and what clamp will lesson 04 apply to the legal one?&lt;/li&gt;
  &lt;li&gt;Under the sign convention, why is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V -60 60&lt;/code&gt; a left spin? Using \(b = 0.15~\mathrm{m}\) only as a symbol check, what is the sign of \((\mathrm{right} - \mathrm{left})\), and why is the result still not a yaw rate in rad/s?&lt;/li&gt;
  &lt;li&gt;A friend packs the same two integers as two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int16&lt;/code&gt; bytes “to be faster.” What do you lose in the serial monitor, and does the \(0.78~\mathrm{ms}\) frame time justify it?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Nine bytes (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;, space, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, space, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;). Time \(\approx 9 \times 10 / 115200 \approx 0.78~\mathrm{ms}\). 2. Targets go to 0 at \(250~\mathrm{ms}\). The host period of \(200~\mathrm{ms}\) leaves about \(50~\mathrm{ms}\) of slack so a late but living host does not trip the stop. 3. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 80&lt;/code&gt; is illegal and ignored. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V 200 -90&lt;/code&gt; is legal and will be clamped to \(180\) and \(-90\). 4. Left command \(-60\), right \(+60\), so the right tire is forward and the left tire is backward: nose yaws left. The difference \((\mathrm{right}-\mathrm{left})\) is positive, matching the sign of \(\omega\) in Chapter 06, but both numbers are PWM counts. Dividing by \(b\) does not create rad/s. 5. You lose the ability to see and type the command. The text frame is already under \(1~\mathrm{ms}\), far below the \(200~\mathrm{ms}\) refresh, so binary does not fix liveness.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/communication/serial/&quot;&gt;Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://pyserial.readthedocs.io/en/latest/shortintro.html&quot;&gt;pySerial short introduction&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/&quot;&gt;geometry_msgs package index (Jazzy)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab: motor driver bring-up and spin test</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter07/07_02_driver_bringup/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter07/07_02_driver_bringup</id>
   <content type="html">&lt;p&gt;This is the first powered spin: wheels in the air, one motor at a time, until positive PWM rolls both tires toward the nose.&lt;/p&gt;

&lt;p&gt;Never test a motor driver for the first time with the robot sitting on the floor—that is how prototypes leap off desks. Bringing up one channel at a time with wheels elevated, a low PWM duty cycle, and a multimeter in series turns driver commissioning into a calm, repeatable procedure that reveals deadband, spin direction, and stall limits safely.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Bring the robot up on a box so both tires are clear of the table, and spin one motor at a time with PWM held to 60–80 counts out of 255.&lt;/li&gt;
  &lt;li&gt;Drive TB6612 STBY high before you blame the sketch, and record the IN pair that makes the left tire forward and the IN pair that makes the right tire forward.&lt;/li&gt;
  &lt;li&gt;Correct a backward tire by swapping that motor’s leads or by planning a firmware sign flip, and refuse to do both.&lt;/li&gt;
  &lt;li&gt;Measure no-load current, treat a stall as a one-second measurement, and stop if the driver smells or the tab is too hot to touch.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 07-01 left you a common ground, a fuse in the battery positive lead, and a buck at 5.0 V measured before the ESP32 was attached. Chapter 05 is the truth table: an H-bridge has a forward pair and a reverse pair, and both switches in one leg are a short. You can identify a TB6612 (STBY, PWMA, PWMB, AIN, BIN) or an L298N (ENA, ENB, INx, heatsink, 5 V jumper). Chapter 06 gave you \(b\) and \(r\), and it defined forward as the nose direction. A positive yaw in that chapter is the right wheel faster than the left, so the sign you discover today is the sign Chapter 08’s PID and Chapter 09’s topics will inherit. No ROS node is part of this lab.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-pwm-average-voltage-deadband-friction-and-h-bridge-control-logic&quot;&gt;Key ideas: PWM Average Voltage, Deadband Friction, and H-Bridge Control Logic&lt;/h2&gt;

&lt;p&gt;Driver bring-up translates digital GPIO pins into controlled mechanical motion through three core concepts:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;PWM Duty Cycle &amp;amp; Effective Armature Voltage:&lt;/strong&gt; Switching a supply voltage \(V_{\text{bat}}\) at frequency \(f_{\text{PWM}}\) (typically 10–25 kHz, above human hearing) with duty ratio \(D \in [0,1]\) delivers an average voltage \(V_{\text{eff}} = D \cdot V_{\text{bat}}\). Because the motor winding has inductance \(L_a\), current smooths out smoothly when \(f_{\text{PWM}} \gg R_a / (2\pi L_a)\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Static Friction Deadband:&lt;/strong&gt; Below a minimum duty cycle \(D_{\text{min}}\) (often 15%–30% on small gearmotors), motor torque \(\tau = K_t I\) cannot overcome static gear and brush friction. Knowing \(D_{\text{min}}\) prevents firmware from commanding unusable trickle PWM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;H-Bridge Truth Table &amp;amp; Standby Enable:&lt;/strong&gt; An H-bridge uses two direction pins (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt;), a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt; pin, and a global &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; (standby) pin. Setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1=1, AIN2=0&lt;/code&gt; drives forward; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0, 1&lt;/code&gt; drives reverse; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1, 1&lt;/code&gt; engages active short-brake; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0, 0&lt;/code&gt; coasts; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY=0&lt;/code&gt; puts all outputs into high-impedance safe shutoff.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A later &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; carries &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; forward and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; as yaw. Those become a left command and a right command. If the right tire’s positive is physically backward, every positive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; is a spin, and Chapter 08’s PID will try to cancel a wiring error. Find the sign here, wheels in the air. Lesson 03 only names the two numbers. They are not meters per second.&lt;/p&gt;

&lt;h2 id=&quot;one-channel-a-small-duty-stby-awake&quot;&gt;One channel, a small duty, STBY awake&lt;/h2&gt;

&lt;p&gt;Put the chassis on a box or a stack of books. Both tires must be in free air. A tire on the table at the first PWM is how a robot leaves the bench.&lt;/p&gt;

&lt;p&gt;Command one motor. Leave the other at PWM 0. The first duty is 60, 70, or at most 80 counts out of 255, not 255. On a pack sitting at \(7.6~\mathrm{V}\),&lt;/p&gt;

\[D = \frac{70}{255} \approx 0.275, \qquad V_{avg} \approx 0.275 \times 7.6~\mathrm{V} \approx 2.09~\mathrm{V}\]

&lt;p&gt;before the bridge drop. A TB6612 is a MOSFET bridge and gives most of that 2 V to the brushes, so the tire turns slowly and you can see the direction. An L298N may drop \(1.5\)–\(2~\mathrm{V}\) and the same duty may only creep; that is a reason to prefer the TB6612, not a reason to start at full scale. If you must raise the duty to see motion on an L298N, do it in small steps and keep the stall rule below.&lt;/p&gt;

&lt;p&gt;On a TB6612, STBY has to be high or both bridges stay in standby no matter what the IN pins and the PWM pins do. A floating STBY is the usual “the sketch is perfect and nothing moves” fault. Set it high with a wire to VCC for this lab, or with a GPIO you will drive high in lesson 04. On an L298N the matching trap is ENA or ENB left low, or the 5 V jumper removed while the logic pin has no other 5 V.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;Forward is one diagonal of the H; the other diagonal is reverse&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Dosmotorsl298n.jpg&quot; alt=&quot;L298N module: screw terminals for the motors, heatsink on the Darlingtons&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Watch the left tire against the nose. Record the IN pair that rolls it forward, then do the right tire alone. An example you must replace: left forward is AIN1 high, AIN2 low, PWMA 70; right forward is BIN1 high, BIN2 low, PWMB 70. If the right tire rolls backward, swap that motor’s two screws once, or leave the screws and invert the right-hand sign in lesson 04. Do not do both. Two inversions cancel, and lesson 05’s straight command becomes a spin.&lt;/p&gt;

&lt;h2 id=&quot;no-load-current-and-a-stall-you-do-not-hold&quot;&gt;No-load current, and a stall you do not hold&lt;/h2&gt;

&lt;p&gt;With one tire in the air at PWM 70, a yellow TT motor often draws on the order of \(0.10\)–\(0.20~\mathrm{A}\). Write the number your meter shows, in series with that motor or at the battery lead with the other motor off. Two motors later add. They do not multiply the stall rating into the no-load reading.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/stall_current.png&quot; alt=&quot;No-load current is the low end of the curve; stall is the high end&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you need a stall point, pinch the tire for one second, read the meter, and release. A seller stall of \(1.2~\mathrm{A}\) at \(6~\mathrm{V}\) is the order of magnitude, not a value to hold. At PWM 70 the stall current is lower than the full-voltage stall, and it still heats the winding and the tab. If the package smells, or you cannot touch the L298N tab or the TB6612, open the switch. The sentence for the next lesson: with your IN map, positive PWM rolls both tires forward, negative reverses them, and PWM 0 stops. That is the sign map.&lt;/p&gt;

&lt;h3 id=&quot;explanation-predicting-motor-speed-vs-pwm-duty-and-deadband-voltage&quot;&gt;Explanation: Predicting Motor Speed vs. PWM Duty and Deadband Voltage&lt;/h3&gt;

&lt;p&gt;Suppose your 2S battery measures \(V_{\text{bat}} = 7.6\ \text{V}\), your motor has armature resistance \(R_a = 4.5\ \Omega\), torque/back-EMF constant \(K_e = K_t = 0.008\ \text{V}\cdot\text{s/rad}\), and gearbox static friction torque reflected to the motor shaft is \(\tau_f = 0.0024\ \text{N}\cdot\text{m}\).&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Find the breakaway current and deadband duty cycle:&lt;/strong&gt; To just overcome static friction, the motor needs:
\(I_{\text{break}} = \frac{\tau_f}{K_t} = \frac{0.0024}{0.008} = 0.30\ \text{A}.\)
At zero speed (zero back-EMF), the required effective voltage is \(V_{\text{min}} = I_{\text{break}} R_a = 0.30 \times 4.5 = 1.35\ \text{V}\), which corresponds to a minimum PWM duty cycle of:
\(D_{\text{min}} = \frac{1.35}{7.6} = 17.8\% \quad (\text{8-bit PWM} \approx 45 / 255).\)
Any command below 18% duty simply warms the motor winding without turning the wheel!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Predict no-load speed at 50% duty (\(V_{\text{eff}} = 3.80\ \text{V}\)):&lt;/strong&gt; Subtracting the resistive drop of no-load running current (say \(I_0 = 0.15\ \text{A}\), drop \(0.675\ \text{V}\)), back-EMF is \(E_b = 3.80 - 0.675 = 3.125\ \text{V}\), so motor shaft speed is:
\(\omega_m = \frac{E_b}{K_e} = \frac{3.125}{0.008} = 390.6\ \text{rad/s} \approx 3730\ \text{RPM}.\)
Comparing your bench tachometer or wheel-turn count against this simple model immediately flags binding gears or a half-dead driver channel.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Tires are off the table. One motor at a time for the first spins. PWM stays in 60–80 out of 255 until the direction is obvious. The battery switch is your kill, and it is in reach. Stall measurements last one second. A hot tab or a smell ends the lab. Motor current still comes from the fused battery, not from USB. Fingers stay off the pinion.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot from lesson 07-01&lt;/td&gt;
      &lt;td&gt;Power tree already measured&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TB6612 or L298N&lt;/td&gt;
      &lt;td&gt;The bridge under test&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 or three jumpers&lt;/td&gt;
      &lt;td&gt;STBY or ENA high, IN pair, PWM&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ammeter or multimeter in series&lt;/td&gt;
      &lt;td&gt;No-load, and a one-second stall if you take one&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tape flag on each tire&lt;/td&gt;
      &lt;td&gt;Direction you can see from the nose&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;The sign map&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Box under the chassis. Confirm both tires clear the box and the table.&lt;/li&gt;
  &lt;li&gt;STBY high on a TB6612, or ENA/ENB available on an L298N. PWM 0. Battery on.&lt;/li&gt;
  &lt;li&gt;Left motor only. PWM 70. Try the two IN combinations that are not both-high. Record which pair rolls the tire toward the nose.&lt;/li&gt;
  &lt;li&gt;Repeat for the right motor. If a tire is backward, swap that motor’s two screws or mark “invert in firmware,” not both.&lt;/li&gt;
  &lt;li&gt;Measure no-load current at PWM 70 for each motor. Optional: one-second stall, then release. Write “positive PWM → both wheels forward” in your own pin names.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A sign map with pin levels, a no-load current for each motor (example shape: left \(0.12~\mathrm{A}\), right \(0.15~\mathrm{A}\) at PWM 70 — your numbers will differ), and a note that STBY or enable was high. Both flags, viewed from behind the robot, travel the nose direction when the command is positive. Nothing in the notes says the tire was fixed by swapping leads and by inverting code.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Both motors dead, code looks right&lt;/td&gt;
      &lt;td&gt;STBY low or floating; ENA/ENB low on an L298N&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Only one motor turns&lt;/td&gt;
      &lt;td&gt;That channel’s PWM pin, IN pair, or screw is open&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tire turns, then the driver smells&lt;/td&gt;
      &lt;td&gt;Duty too high or a stall held too long; switch off&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Left forward, right backward, same IN pattern&lt;/td&gt;
      &lt;td&gt;Swap the right motor leads or the firmware sign, once&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Current stays near stall in “free” air&lt;/td&gt;
      &lt;td&gt;Tire rubbing the plate, or gearbox binding&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;You should already own the driver. If lesson 07-01 left you with motors and no bridge, buy a TB6612 first. An L298N is an acceptable substitute and runs hotter; keep its heatsink, and still use the buck for the ESP32. Prices move. Rough 2026 bands: TB6612 module \(25.000\)–\(70.000\) VND, TT motor \(25.000\)–\(45.000\) VND each, L298N about \(45.000\) VND on the verified page below. A DRV8833 is fine for a much smaller motor and is a weak substitute for a TT pair at stall.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified L298N, about \(45.000\) VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;Hshop TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;Shopee TB6612&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=TB6612&quot;&gt;Lazada TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=TB6612&quot;&gt;Thế Giới IC TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;VM is \(8.0~\mathrm{V}\) and the first command is 80 counts out of 255. Compute \(D\) and \(V_{avg}\) before bridge drop. If an L298N then drops \(1.8~\mathrm{V}\), what is left for the brushes?&lt;/li&gt;
  &lt;li&gt;No-load at PWM 70 is \(0.14~\mathrm{A}\). A one-second pinch reads \(0.95~\mathrm{A}\). Why are both numbers plausible on the stall curve, and why do you release?&lt;/li&gt;
  &lt;li&gt;STBY is floating, AIN1 is high, AIN2 is low, PWM is 80. The tire is still. What is the first wire you add?&lt;/li&gt;
  &lt;li&gt;The right tire is backward. You swap its leads and you also multiply the right command by \(-1\) in the sketch you are about to write. What does positive PWM do on the floor?&lt;/li&gt;
  &lt;li&gt;Left forward is AIN1 = 1, AIN2 = 0. Right forward, before any fix, is BIN1 = 0, BIN2 = 1. Write the sign-map sentence lesson 04 must implement, without a second inversion.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(D = 80/255 \approx 0.314\), \(V_{avg} \approx 0.314 \times 8.0 \approx 2.51~\mathrm{V}\). After a \(1.8~\mathrm{V}\) drop the brushes see about \(0.7~\mathrm{V}\), which may not overcome friction. That is a TB6612 argument, not a reason to start at 255. 2. No-load is the low-current end, where back-EMF cancels most of the supply. The pinch collapses back-EMF and current rises toward stall. One second, then release; a hot tab or a smell means stop. 3. Drive STBY high. Until that pin is high the TB6612 stays in standby and the IN pins do not matter. 4. The two inversions cancel. Positive PWM still rolls that tire backward, the notebook says you “fixed” it, and a straight command spins. Do only one of the two fixes. 5. Positive left command: AIN1 high, AIN2 low. Positive right command: BIN1 low, BIN2 high. Negative swaps each pair. Do not also swap the right-hand screws.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;ST L298 datasheet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/communication/serial/&quot;&gt;Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Assemble chassis, motors, and power</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter07/07_01_assemble_power/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter07/07_01_assemble_power</id>
   <content type="html">&lt;p&gt;You bolt the motors, trail the caster, strap the battery, and prove the power tree with a meter. No PWM yet.&lt;/p&gt;

&lt;p&gt;The single most dramatic moment in any hardware build is the first time you plug in the battery—and seasoned engineers make sure it is the most &lt;em&gt;boring&lt;/em&gt; moment instead. By assembling the chassis with the mass centered low, wiring a clean star-ground power tree, and measuring every voltage rail with a multimeter &lt;em&gt;before&lt;/em&gt; the microcontroller ever touches a socket, you turn “smoke test” from a gamble into a guaranteed pass.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Mount both TT motors so the tires clear the plate, set the caster so its contact trails, and strap the battery so the center of gravity sits between the axle and the caster.&lt;/li&gt;
  &lt;li&gt;Wire battery positive through a switch and a 2–3 A fuse, then split that node to driver VM and to an LM2596 whose output you set to 5.0 V before the ESP32 is attached.&lt;/li&gt;
  &lt;li&gt;Tie battery negative, driver GND, buck GND, and MCU GND into one node, and keep motor current on the driver screw terminals.&lt;/li&gt;
  &lt;li&gt;Record unloaded battery voltage and buck output in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, with a wiring photo, and refuse USB as a motor supply.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Chapter 04 taught you that a sensor and a microcontroller share one ground. Chapter 05 taught the H-bridge: a TB6612 stays asleep while STBY is low, and an L298N’s onboard 7805 is not an ESP32 supply. Chapter 06 left two lengths in the notes, track width \(b\) between the tire contact centers and rolling radius \(r\) from a one-revolution roll. A typical bench here has \(b = 0.15~\mathrm{m}\) and \(r = 31.2~\mathrm{mm}\). Copy them into today’s notes. This lesson has no ROS node and no PWM.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-power-tree-architecture-buck-regulation-and-common-ground-reference&quot;&gt;Key ideas: Power-Tree Architecture, Buck Regulation, and Common-Ground Reference&lt;/h2&gt;

&lt;p&gt;Safe mobile-robot wiring stands on three electrical and mechanical pillars:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Power-Tree Topology &amp;amp; Voltage Regulation:&lt;/strong&gt; A 2S Li-Ion/LiPo battery swings from 8.4 V (full) down to ~6.4 V (discharged). Motor drivers handle this unregulated battery bus directly, while the microcontroller (3.3 V or 5 V) requires a switching buck regulator (high efficiency \(\eta \approx 85\text{–}92\%\)) or low-dropout linear regulator to stay within strict voltage limits.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Star Grounding &amp;amp; Ground-Bounce Prevention:&lt;/strong&gt; High motor currents (1–3 A) returning through a thin wire shared with the MCU create a voltage drop \(\Delta V = I R_{\text{wire}}\) that lifts the MCU’s ground reference and corrupts serial/sensor signals. Routing motor-driver ground and logic ground separately back to a single star point at the battery/switch eliminates ground bounce.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mass Distribution &amp;amp; Pitch Stability:&lt;/strong&gt; Placing the heaviest component (the battery pack) low and slightly ahead of the drive axle lowers the center of mass and keeps positive normal force on both drive tires and the swivel caster during acceleration.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ROS 2 is not in this chapter. Later a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; will carry &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; as forward speed and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; as yaw rate, and some node will split that twist into two wheel commands. The split is useless if the nose lifts, the buck was never set, or the microcontroller browns out when the motors start. Lessons 03 and 04 use a text line as a stand-in for that twist. A Twist message cannot strap a battery.&lt;/p&gt;

&lt;h2 id=&quot;where-the-mass-has-to-sit&quot;&gt;Where the mass has to sit&lt;/h2&gt;

&lt;p&gt;Tires must clear the acrylic, and the caster patch must trail the swivel pin. A rubbing tire is a stall. A reversed fork shimmies. Strap the pack so it cannot slide. From the side, the center of gravity has to sit between the axle and the caster; behind the axle the nose lifts.&lt;/p&gt;

&lt;p&gt;Take the axle as \(x = 0\) and the caster contact as \(x = +90~\mathrm{mm}\) toward the nose. Suppose the plate and motors are \(220~\mathrm{g}\) with their own center at \(x = +15~\mathrm{mm}\), and the pack is \(90~\mathrm{g}\). The combined center is&lt;/p&gt;

\[x_{cg} = \frac{220 \times 15 + 90 \times x_b}{310}.\]

&lt;p&gt;Strap the pack behind the axle at \(x_b = -40~\mathrm{mm}\) and&lt;/p&gt;

\[x_{cg} = \frac{3300 - 3600}{310} \approx -1.0~\mathrm{mm}.\]

&lt;p&gt;The mass is on the wrong side of the axle. The caster is unloaded and the nose lifts as soon as you accelerate. Strap the same pack between the axle and the caster at \(x_b = +35~\mathrm{mm}\) and&lt;/p&gt;

\[x_{cg} = \frac{3300 + 3150}{310} \approx 20.8~\mathrm{mm},\]

&lt;p&gt;which sits between \(0\) and \(90~\mathrm{mm}\). The caster then carries about \((20.8/90)\times 310~\mathrm{g} \approx 72~\mathrm{g}\). The nose stays down, and the axle still holds most of the weight. Write your own \(x_b\) beside \(b\) and \(r\).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;Track width $$b$$ and the caster you must keep loaded&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;the-power-tree-measured-before-the-mcu-is-attached&quot;&gt;The power tree, measured before the MCU is attached&lt;/h2&gt;

&lt;p&gt;Motor current and logic current share a battery and a ground, not a regulator and not a breadboard row.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_tree.png&quot; alt=&quot;Battery, fuse, driver VM, and a buck set to 5.0 V before the MCU&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Battery positive goes to a switch, then a 2–3 A fuse, then splits to driver VM and to the LM2596 input. Battery negative, driver GND, buck GND, and MCU GND are one node. Motor leads run from the driver screws to the motor tabs and never through a breadboard: those contacts are a few hundred milliamps, and a stall is amperes.&lt;/p&gt;

&lt;p&gt;Set the buck with the microcontroller unplugged. A fresh 2S pack often reads about \(8.2~\mathrm{V}\) unloaded (the window is roughly \(7.4\)–\(8.4~\mathrm{V}\)). Turn the potentiometer until the output is \(5.0~\mathrm{V}\); \(5.02~\mathrm{V}\) open-circuit is the number you want. Only then do you offer that pin to the ESP32, or you leave the ESP32 on USB for programming and still share ground. Do not feed VM into the 5 V or 3.3 V pin. Do not power motors from USB: a port is about \(500~\mathrm{mA}\), and two stalled TT motors are on the order of \(2.4~\mathrm{A}\).&lt;/p&gt;

&lt;p&gt;On an L298N the 5 V jumper enables the onboard 7805 from VM. On a 2S pack that is tolerable for the chip’s own tiny logic, and it is a bad ESP32 supply once Wi-Fi transmits. Prefer the buck. If VM is above \(12~\mathrm{V}\), remove the jumper. A quick heat check at the high end of 2S, with \(0.25~\mathrm{A}\) imagined through that 7805, is&lt;/p&gt;

\[P \approx (8.4 - 5.0) \times 0.25 = 0.85~\mathrm{W}\]

&lt;p&gt;in a small regulator. At \(12~\mathrm{V}\) the same current is \(1.75~\mathrm{W}\). The buck feeds the ESP32 either way.&lt;/p&gt;

&lt;p&gt;Lithium stays in a holder or a pack with a BMS. No dangling soldered bare leads. Charge with the matching charger, attended. A TP4056 is 1S only, full near \(4.2~\mathrm{V}\). One TP4056 on a 2S pack will not make \(8.4~\mathrm{V}\) and will not balance the cells.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-calculating-power-tree-current-efficiency-and-runtime&quot;&gt;Explanation: Hand-Calculating Power-Tree Current, Efficiency, and Runtime&lt;/h3&gt;

&lt;p&gt;Before plugging in the battery, verify your power tree with a three-line energy budget:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Load inventory on a 2S Li-Ion pack (nominal \(V_{\text{bat}} = 7.4\ \text{V}\), capacity \(2200\ \text{mAh}\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Two drive motors cruising at \(0.40\ \text{A}\) each directly on the battery rail: \(I_{\text{motors}} = 0.80\ \text{A}\).&lt;/li&gt;
      &lt;li&gt;ESP32 + sensors drawing \(0.25\ \text{A}\) at \(5.0\ \text{V}\) (\(P_{\text{logic}} = 1.25\ \text{W}\)) through a buck converter of efficiency \(\eta = 0.88\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Input current drawn by the buck converter from the 7.4 V pack:&lt;/strong&gt;
\(I_{\text{buck,in}} = \frac{P_{\text{logic}}}{\eta \, V_{\text{bat}}} = \frac{1.25}{0.88 \times 7.4} = 0.192\ \text{A}.\)
&lt;em&gt;(Compare with a linear 7805 regulator: it would draw the full \(0.25\ \text{A}\) and waste \((7.4 - 5.0) \times 0.25 = 0.60\ \text{W}\) as heat!)&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Total battery current and conservative runtime (using 80% usable capacity):&lt;/strong&gt;
\(I_{\text{total}} = 0.80 + 0.192 = 0.992\ \text{A} \approx 1.0\ \text{A},\)
\(t_{\text{run}} = \frac{0.80 \times 2.2\ \text{Ah}}{1.0\ \text{A}} = 1.76\ \text{hours} \approx 105\ \text{minutes}.\)
Checking this budget confirms your fuse/switch rating (e.g., 5 A switch, 3 A fuse) and ensures your battery will survive a full field test.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;No PWM in this lab, so the tires may rest on the bench. Keep the battery disconnected until the fuse, switch, and grounds are in. Lithium lives in a holder or a BMS pack. Charge it with the matching charger, attended. One TP4056 must not span a 2S pack. If the buck smells, open the switch.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;2WD acrylic chassis, two TT motors, tires, caster&lt;/td&gt;
      &lt;td&gt;The plant from Chapter 06&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2S pack or 18650 holder with BMS&lt;/td&gt;
      &lt;td&gt;Motor and logic source&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Switch, fuse holder, 2–3 A fuse&lt;/td&gt;
      &lt;td&gt;The series path in the positive lead&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TB6612 (preferred) or L298N&lt;/td&gt;
      &lt;td&gt;Driver VM only; outputs unused today&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LM2596 buck&lt;/td&gt;
      &lt;td&gt;5.0 V logic, adjusted before the MCU&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 devkit&lt;/td&gt;
      &lt;td&gt;Attached only after the buck is 5.0 V&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Multimeter, straps or screws, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;The measurement&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Bolt both motors. Spin each tire by hand so it clears the plate.&lt;/li&gt;
  &lt;li&gt;Mount the caster with the patch trailing. Copy \(b\) and \(r\) into today’s notes.&lt;/li&gt;
  &lt;li&gt;Strap the battery between the axle and the caster. Estimate \(x_{cg}\) and write it down.&lt;/li&gt;
  &lt;li&gt;Wire battery positive to the switch, the fuse, then VM and the buck input. Join the four grounds. Motor wires stay on the driver screws.&lt;/li&gt;
  &lt;li&gt;MCU absent, battery on. Measure pack voltage and buck output, set the buck to \(5.0~\mathrm{V}\), photograph the wiring, and only then attach the ESP32 to that 5 V rail or to USB.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; has a date, unloaded pack voltage (a healthy 2S example is \(8.15~\mathrm{V}\)), buck output within a few tens of millivolts of \(5.0~\mathrm{V}\) before the MCU is attached, your \(b\) and \(r\), and a line that motor wires do not enter the breadboard. The photo shows the switch, the fuse, the strap, and the common ground. No wheel has been commanded.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Nose lifts when you set the robot down&lt;/td&gt;
      &lt;td&gt;Pack is behind the axle; move it toward the caster&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tire scrapes the acrylic&lt;/td&gt;
      &lt;td&gt;Motor or wheel spacer is wrong; that scrape is a stall later&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Buck reads 8 V, or 12 V, on the output&lt;/td&gt;
      &lt;td&gt;Pot still at the end stop; do not attach the ESP32&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 reboots when you touch VM&lt;/td&gt;
      &lt;td&gt;Logic is on the 7805, or grounds are not one node&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You reached for a TP4056&lt;/td&gt;
      &lt;td&gt;That board is 1S; use a 2S charger and a BMS&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy the gap, not a second robot. Prices move. Rough 2026 bands: 2WD acrylic chassis 60–150k VND, TT motor 25–45k each, TB6612 25–70k, LM2596 10–25k, 2S pack or 18650 holder 80–180k, switch 8–20k, fuse holder and a 2–3 A fuse 10–25k, ESP32 devkit 70–150k. Verified L298N about 45.000 VND if there is no TB6612. The buck still feeds the ESP32. A 1S holder is not a 2S pack.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified L298N, about \(45.000\) VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=khung+xe+2wd&quot;&gt;Hshop chassis&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=LM2596&quot;&gt;buck&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=ESP32&quot;&gt;ESP32&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=khung%20xe%202WD%20acrylic&quot;&gt;Shopee khung 2WD&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=LM2596&quot;&gt;LM2596&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=c%E1%BA%A7u%20ch%C3%AC%202A&quot;&gt;cầu chì&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=ESP32&quot;&gt;Lazada ESP32&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=pin%2018650%202S&quot;&gt;pin 18650&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=LM2596&quot;&gt;Thế Giới IC LM2596&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=ESP32&quot;&gt;ESP32&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Plate and motors \(220~\mathrm{g}\) at \(x = +15~\mathrm{mm}\), pack \(90~\mathrm{g}\) at \(x_b = -20~\mathrm{mm}\), caster at \(+90~\mathrm{mm}\). Find \(x_{cg}\). Does the nose stay down?&lt;/li&gt;
  &lt;li&gt;Unloaded buck output is \(5.02~\mathrm{V}\). After the ESP32 joins, the same pin reads \(4.15~\mathrm{V}\). What do you check before you call the board dead?&lt;/li&gt;
  &lt;li&gt;A classmate wires one TP4056 across a 2S pack “so it can charge from USB.” What is wrong with the cell count, and what charger belongs there?&lt;/li&gt;
  &lt;li&gt;VM is \(8.4~\mathrm{V}\) and the L298N 5 V jumper is fitted. Estimate 7805 heat at \(0.25~\mathrm{A}\). Why is the buck still the ESP32 supply at this voltage?&lt;/li&gt;
  &lt;li&gt;Two motor leads are pushed into a breadboard, then into the driver. The fuse is on the buck output instead of the battery positive. Rewrite the path battery → switch → fuse → loads, and say what the breadboard cannot carry.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(x_{cg} = (3300 - 1800)/310 \approx 4.8~\mathrm{mm}\), still between axle and caster, so the nose stays down, but the caster is light. If it chatters, move the pack toward \(+35~\mathrm{mm}\). 2. Check common ground, check that USB and the buck are not both shoved into the 5 V pin, and look for a thin wire or a buck that sags under load. Do not turn the pot up with the MCU attached. 3. A TP4056 is 1S, full near \(4.2~\mathrm{V}\). A 2S pack is full near \(8.4~\mathrm{V}\) and needs a 2S charger plus a BMS. 4. \(P \approx (8.4-5)\times 0.25 = 0.85~\mathrm{W}\). Wi-Fi bursts exceed a quiet 0.25 A and the 7805 sags. The jumper may feed the L298’s own logic on 2S; the ESP32 stays on the buck. Remove the jumper above \(12~\mathrm{V}\). 5. Battery positive, switch, 2–3 A fuse, then VM and the buck input. Motor current stays on screws. A fuse downstream of the buck does not protect a VM short.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;ST L298 datasheet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG carrier&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/p/geometry_msgs/interfaces/msg/Twist.html&quot;&gt;geometry_msgs/Twist (Jazzy)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/communication/serial/&quot;&gt;Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 06 lab checklist</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter06/06_09_ch06_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter06/06_09_ch06_lab_checklist</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;50–70 minutes&lt;/strong&gt; to assemble the notes you already took and to fill the gaps. This is the chapter gate, not a new mechanism.&lt;/p&gt;

&lt;p&gt;At the end of Chapter 06, your robot still has no firmware running—and that is intentional. Experienced roboticists never debug mechanical wobble and software bugs at the same time; they close the mechanical gate first so that when the robot drifts in Chapter 07, they know with certainty whether to reach for a wrench or a keyboard.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; holds a track width and a rolling radius, an identified screw size, a gear ratio that was predicted and checked, one Grashof or belt or rack calculation, a strain-relief photo or a dated plan, and a list of parts still missing before Chapter 07. You can say, in a sentence, whether you passed on hardware or on a labeled drawing.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 01 through 08. If one of those labs is unfinished, this checklist is the index that tells you which one to reopen. You need the same notebook you have been writing millimeters into.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-mechanical-integration-gates-tolerance-stacking-and-configuration-management&quot;&gt;Key ideas: Mechanical Integration Gates, Tolerance Stacking, and Configuration Management&lt;/h2&gt;

&lt;p&gt;Closing the mechanical chapter builds three disciplined habits used in every real robotics lab:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Fault Isolation (Decoupling Mechanical vs. Software Bugs):&lt;/strong&gt; If a robot pulls left under equal PWM, the root cause could be wheel-radius mismatch, axle binding, asymmetric gear friction, or a firmware sign bug. Signing off mechanical symmetry &lt;em&gt;before&lt;/em&gt; writing teleop code eliminates half the search space immediately.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tolerance Stacking &amp;amp; Concentricity:&lt;/strong&gt; Small errors across motor brackets, shaft couplings, and wheel hubs add up. Checking runout (wobble), caster load sharing, and fastener retention verifies that the physical assembly matches the ideal kinematic model within acceptable bounds.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Single Source of Truth (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;):&lt;/strong&gt; Storing dated, measured values of \(b\), \(r\), \(G\), mass, and CoM location in one versioned note guarantees that Chapter 07 firmware, Chapter 08 kinematics, and Chapter 09 URDF files all share the exact same physical constants.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chapter 07 applies voltage to this chassis. Chapter 08 writes the kinematic equations in code. The ROS &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; and the URDF will copy \(b\), \(r\), and \(i\) from somewhere; the somewhere should be this page, dated, with the method beside the number. A missing caster or a missing nyloc is a shopping line, not a surprise during bring-up. The pass condition is evidence in the notes. A memory of having “basically measured it” does not open Chapter 07.&lt;/p&gt;

&lt;h2 id=&quot;the-gate&quot;&gt;The gate&lt;/h2&gt;

&lt;p&gt;Copy this table into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; and fill the evidence column with a number, a file name of a photo, or the words “drawing, not hardware.”&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Gate&lt;/th&gt;
      &lt;th&gt;What “done” looks like&lt;/th&gt;
      &lt;th&gt;Evidence&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Track width \(b\)&lt;/td&gt;
      &lt;td&gt;Millimeters between tread contact centers&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Rolling radius \(r\)&lt;/td&gt;
      &lt;td&gt;From \(s/(2\pi)\), both wheels&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Screw identity&lt;/td&gt;
      &lt;td&gt;M3 called out, nyloc distinguished from a plain nut&lt;/td&gt;
      &lt;td&gt;Photo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Gear ratio&lt;/td&gt;
      &lt;td&gt;Prediction and a check within about 10 percent, or a labeled paper prediction&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Linkage or belt or rack&lt;/td&gt;
      &lt;td&gt;One Grashof test, or a GT2 tooth ratio, or a rack travel&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Strain relief&lt;/td&gt;
      &lt;td&gt;Before/after photo, or a written plan if no wires exist yet&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Missing parts&lt;/td&gt;
      &lt;td&gt;Explicit list for Chapter 07 assembly&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Pass, in prose: every row has evidence, the ratio row says how it was checked, and the missing-parts row is either “none” or a list you can buy this week. A drawing-only path passes the reasoning gate. Hardware-ready is a second sentence on the same page, and you write it only when the plate, both wheels, the caster, and both motors have been in your hands for the checks in Lesson 08. Chapter 07 starts from hardware-ready.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;The two lengths the gate will not accept as guesses&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/gear_train_ratio.png&quot; alt=&quot;The ratio the gate wants next to those lengths&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A student rolls one wheel \(s = 188\) mm.&lt;/p&gt;

\[r = \frac{188}{2\pi} \approx 29.9\ \text{mm}.\]

&lt;p&gt;The other wheel gives 30.4 mm. They record both and use 30.2 mm as a working average only after they reseat the tires and the second test agrees within a millimeter. Track width is \(b = 152\) mm between contact centers. The gearbox stamp is 1:48, three input turns measured \(23^\circ\) against a prediction of&lt;/p&gt;

\[\frac{3}{48}\times 360^\circ = 22.5^\circ,\]

&lt;p&gt;which is about 2 percent high, inside the band. Their Grashof line is the Lesson 04 pair \(s + l = 120\) mm, \(p + q = 125\) mm. The strain-relief photo shows a zip tie and a loop. Missing parts: one nyloc bag. That page is a pass on hardware, with a shopping line that does not block a wheels-up spin if two nylocs are already on the motor mounts. It would block a claim that every fastener is done.&lt;/p&gt;

&lt;h3 id=&quot;explanation-quantifying-straight-line-drift-from-a-1-mm-wheel-radius-mismatch&quot;&gt;Explanation: Quantifying Straight-Line Drift from a 1 mm Wheel-Radius Mismatch&lt;/h3&gt;

&lt;p&gt;Why does the Chapter 06 gate insist that left and right rolling radii \(r_L, r_R\) agree within ~1 mm? Let us calculate the open-loop path curvature caused by a tiny radius mismatch.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Setup:&lt;/strong&gt; Suppose both wheels spin at the exact same angular rate \(\dot\phi_L = \dot\phi_R = 10\ \text{rad/s}\), track width is \(b = 0.150\ \text{m}\), left radius is \(r_L = 0.0310\ \text{m}\), and right radius is \(r_R = 0.0320\ \text{m}\) (just a 1.0 mm difference!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Resulting wheel linear speeds:&lt;/strong&gt;
\(v_L = 10 \times 0.0310 = 0.310\ \text{m/s}, \qquad v_R = 10 \times 0.0320 = 0.320\ \text{m/s}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Induced yaw rate and turning radius:&lt;/strong&gt;
\(v = \frac{0.320 + 0.310}{2} = 0.315\ \text{m/s}, \qquad \omega = \frac{0.320 - 0.310}{0.150} = 0.0667\ \text{rad/s},\)
\(R = \frac{v}{\omega} = \frac{0.315}{0.0667} = 4.725\ \text{m}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lateral drift over a 2-meter hallway test:&lt;/strong&gt; For small angles, lateral deviation is \(\Delta y \approx \frac{L^2}{2R} = \frac{2.0^2}{2 \times 4.725} = 0.42\ \text{m}\)! Even a 1 mm tire seating error makes an open-loop robot drift 42 cm off centerline in just 2 meters—proving why mechanical verification must precede firmware.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab-fill-the-gate-from-evidence-you-have&quot;&gt;Lab: fill the gate from evidence you have&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;This session does not apply power. If you repeat a hand spin or a tug test, the battery stays disconnected, as in Lessons 05 and 07. Do not “just tap 5 V” to see if a motor that failed the hand-drag check will free itself.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; and the photos from this chapter&lt;/td&gt;
      &lt;td&gt;The evidence&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chassis and tools, if you are closing hardware gaps&lt;/td&gt;
      &lt;td&gt;Only the failed rows&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Shopping list&lt;/td&gt;
      &lt;td&gt;The missing-parts row&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Open the notes from Lessons 01, 02, 03 or 08, 04, and 07. Copy numbers; do not retype them from memory if the original line is still there.&lt;/li&gt;
  &lt;li&gt;Fill the table. Where a photo exists, name the file. Where the kit was late, write “drawing.”&lt;/li&gt;
  &lt;li&gt;Recompute one check in the worked-example style so a transcription error dies here: \(r\) from \(s\), or \(\theta_{\text{out}}\) from \(i\).&lt;/li&gt;
  &lt;li&gt;Write the pass sentence and, if it is true, the hardware-ready sentence.&lt;/li&gt;
  &lt;li&gt;Turn the missing-parts row into the buy list below. GT2 appears only if your ratio includes a belt.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A completed table and two sentences. A classmate can perform Chapter 07’s mechanical setup from your notes without asking you for \(b\). Drawing-only notes say so in the pass sentence and list the hardware still required for the second sentence.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;\(b\) looks like a round 150 with no method&lt;/td&gt;
      &lt;td&gt;It was remembered; remeasure between contact centers&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ratio with no predicted angle beside it&lt;/td&gt;
      &lt;td&gt;The check was skipped; run Lesson 08&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hardware-ready claimed from a drawing&lt;/td&gt;
      &lt;td&gt;Split the two sentences&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Missing-parts row blank&lt;/td&gt;
      &lt;td&gt;It is a row, even when the list is “none”&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;One wheel’s \(r\) missing&lt;/td&gt;
      &lt;td&gt;Measure it; mismatched radii yaw a straight command&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy only the gaps on your missing-parts row. A complete bench does not need a second chassis “as spare” before Chapter 07. GT2 is on this list only if Lesson 04’s ratio uses a belt.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Gap&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;When&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Chassis&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khung xe robot 2 bánh&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;80.000–200.000&lt;/td&gt;
      &lt;td&gt;No plate yet&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;M3 kit and nylocs&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ốc M3&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;25.000–70.000&lt;/td&gt;
      &lt;td&gt;Lesson 02 sort came up short&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Caster&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh caster robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;12.000–40.000&lt;/td&gt;
      &lt;td&gt;Nose has no trailing wheel&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GT2 belt and pulley&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây đai GT2&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000 for a belt&lt;/td&gt;
      &lt;td&gt;Only if your drive uses one&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Search pages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=khung+xe+robot&quot;&gt;Hshop: khung&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=%E1%BB%91c%20M3&quot;&gt;Shopee: ốc M3&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=b%C3%A1nh%20caster&quot;&gt;Lazada: bánh caster&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=%E1%BB%91c%20M3&quot;&gt;Thế Giới IC: ốc&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prices move. Order the missing line before you book a Chapter 07 bench, and keep the receipt next to the notes so the part that arrives is the size you measured.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Your roll lengths are 196 mm and 191 mm. Compute both radii. Do you average them yet?&lt;/li&gt;
  &lt;li&gt;Three turns, stamp 1:34, measured output \(33^\circ\). Prediction, percent error, and pass or revisit?&lt;/li&gt;
  &lt;li&gt;A teammate’s \(b\) is “the motor spacing, 140 mm.” What do you ask them to remeasure, and which ROS symptom are you avoiding?&lt;/li&gt;
  &lt;li&gt;The strain-relief cell says “later.” Does the reasoning gate pass? Does hardware-ready pass?&lt;/li&gt;
  &lt;li&gt;Write your own pass sentence using the numbers you actually have, including the words “drawing” if that is the truth.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;\(r \approx 31.2\) mm and \(30.4\) mm. The split is several millimeters, so you reseat and remeasure before you average. 2. Prediction \(3/34 \times 360^\circ \approx 31.8^\circ\). Error $$&lt;/td&gt;
          &lt;td&gt;33-31.8&lt;/td&gt;
          &lt;td&gt;/31.8 \approx 4\(percent, inside 10 percent, so the ratio row can pass. 3. Ask for the distance between tread contact centers. You are avoiding a straight `cmd_vel` that drives a circle. 4. Reasoning fails until a photo or a dated written plan exists. Hardware-ready also waits on that plan being executed once wires exist, plus the rest of Lesson 08. 5. The sentence should name\)b\(,\)r\(,\)i$$, the linkage or belt or rack result, and the evidence type. No number you did not take.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ros-controls/ros2_controllers/tree/master/diff_drive_controller&quot;&gt;diff_drive_controller&lt;/a&gt; — the file where this table’s \(b\), \(r\), and wheel ticks-per-revolution eventually go.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://wiki.ros.org/diff_drive_controller&quot;&gt;ROS 1 diff_drive_controller wiki&lt;/a&gt; — parameter names in one page. ROS 1 text, same geometry.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://navigation.ros.org/setup_guides/odom/setup_odom.html&quot;&gt;Nav2 odometry setup&lt;/a&gt; — the consumer of the odometry those parameters create.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab: gear ratio measurement and assembly checklist</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter06/06_08_mechanisms_lab/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter06/06_08_mechanisms_lab</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;80–110 minutes&lt;/strong&gt;. This lesson is the hands-on. Power stays off for every mechanical check.&lt;/p&gt;

&lt;p&gt;Spec sheets lie—not maliciously, but because gearboxes get substituted in kits, tires compress under battery weight, and nominal ratios like “1:48” are often rounded from 48.2:1. A ten-minute bench calibration with two tape pointers turns an unknown kit into a known mechanical plant before you write a single line of control code.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you have a gear ratio you both predicted and checked, a backlash estimate at the wheel in degrees, and a written assembly checklist you can run before any voltage in Chapter 07. You can tell a simple idler from a ratio change, and you can multiply a gearbox stamp by an external belt when both exist.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 01–07. You need \(b\), \(r\), an M3 habit, the ratio definition \(i = N_{\text{out}}/N_{\text{in}} = \omega_{\text{in}}/\omega_{\text{out}}\), and the strain-relief tug. A cardboard pointer and a protractor, or a paper protractor, are enough instruments.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-experimental-system-identification-measurement-uncertainty-and-bias-vs-variance&quot;&gt;Key ideas: Experimental System Identification, Measurement Uncertainty, and Bias vs. Variance&lt;/h2&gt;

&lt;p&gt;This bench lab introduces experimental identification—the empirical half of robotics engineering:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Direct Mechanical Identification:&lt;/strong&gt; Counting input revolutions \(N_{\text{in}}\) per exact output revolution \(N_{\text{out}}\) measures the true gear ratio \(G = N_{\text{in}} / N_{\text{out}}\) without disassembling a sealed gearbox, while reversing direction under a light finger load reveals angular backlash \(\Delta\theta_{\text{bl}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Error Reduction by Multi-Revolution Averaging:&lt;/strong&gt; Measuring a single revolution with a \(\pm 2^\circ\) eye error yields \(2/360 = 0.56\%\) uncertainty. Averaging over \(K = 10\) revolutions divides the endpoint reading error by \(K\) (down to \(0.056\%\))—a standard metrology technique.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Left–Right Symmetry Check:&lt;/strong&gt; Differential-drive robots rely on matched left and right drivetrains. Comparing \(G_L\) vs. \(G_R\) and free-spin friction between both sides catches bent shafts or binding housings before PID tries to mask a mechanical defect.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chapter 07 is the first time this chassis is allowed to see voltage. The checklist below is the gate. A wheel that scrapes, a battery that slides, or a set screw on the round will waste that lab on a failure Lesson 07 already named. The ratio you measure is the ratio &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; will turn into meters. A result within about 10 percent of the nameplate is what molded teeth can support. If the kit is still in transit, the drawing prediction is the deliverable and you label it as a drawing. You do not invent a measured angle you did not take.&lt;/p&gt;

&lt;h2 id=&quot;what-you-are-checking&quot;&gt;What you are checking&lt;/h2&gt;

&lt;p&gt;Count teeth if you can see them, or read the stamp on the gearbox. The stamp is the prediction. The check is kinematic: a pointer on the input, a pointer on the output, a known input motion, a measured output angle. For a ratio \(i\), three turns of the input should produce&lt;/p&gt;

\[\theta_{\text{out}} = \frac{3}{i} \times 360^\circ.\]

&lt;p&gt;Hold the input still afterward and rock the wheel. The angle it moves before the input pointer must move is the backlash you will quote. Eyeball is enough. A few degrees at the wheel is ordinary molded gearing. Tens of degrees means a loose set screw or a skipped belt tooth, which is a different fault.&lt;/p&gt;

&lt;p&gt;A simple idler reverses direction and does not change \(i\). Count it as a mesh for the sign, not as a factor, unless it is a compound idler with two different tooth counts locked together. If a belt sits outside the gearbox, the wheel ratio is the product. Students lose a factor of two here more often than they mis-count a pinion.&lt;/p&gt;

&lt;p&gt;Then run the assembly list with the battery on the bench, not on the connector. Fasteners tight. Set screws on flats. Wheels clear of the plate. Caster spins and trails. Wires have a service loop and the solder does not bend. Battery cannot slide in its future pocket. Both motors turn by hand with similar drag. A motor that feels gritty or much stiffer than its twin gets a note before Chapter 07, not a higher PWM.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/gear_train_ratio.png&quot; alt=&quot;Name the input before you divide: the figure is the prediction, the pointers are the check&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A TT motor is stamped 1:48. Three input turns should give&lt;/p&gt;

\[\theta_{\text{out}} = \frac{3}{48} \times 360^\circ = 22.5^\circ.\]

&lt;p&gt;You measure \(24^\circ\) at the wheel pointer. The relative error is&lt;/p&gt;

\[\frac{24 - 22.5}{22.5} \approx 0.067,\]

&lt;p&gt;about 7 percent, inside the 10 percent band for molded teeth and a handmade pointer. You write \(i = 48\) and “checked, 24° versus 22.5°.” If the pointer had landed near \(45^\circ\), the error is a factor of two: a missed 2:1 belt, or a stamp you halved. If it had landed near \(67.5^\circ\), you used \(i = 16\) or you turned the wrong shaft. Backlash measured by rocking the wheel, say \(5^\circ\), is recorded next to the ratio and is not “fixed” by tightening the mesh until it binds.&lt;/p&gt;

&lt;p&gt;Direction: one external mesh, or an odd number of them, reverses the wheel relative to the motor. Your firmware sign in Lesson 05 has to agree with this observation, still with power off, by turning the shaft the way the motor will turn.&lt;/p&gt;

&lt;h3 id=&quot;explanation-computing-effective-encoder-ticks-per-meter-by-hand&quot;&gt;Explanation: Computing Effective Encoder Ticks per Meter by Hand&lt;/h3&gt;

&lt;p&gt;Once you measure the true gear ratio \(G\) and rolling radius \(r\) in this lab, combine them into the exact conversion constant your firmware and ROS 2 odometry need:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Measured bench constants:&lt;/strong&gt; Suppose your motor rear shaft has a magnetic encoder producing \(P_{\text{motor}} = 11\ \text{PPR}\) (pulses per motor revolution) on each channel, read in &lt;strong&gt;4× quadrature&lt;/strong&gt; mode (\(44\ \text{ticks/motor rev}\)). Your pointer test counts \(N_{\text{in}} = 450\) motor turns for \(N_{\text{out}} = 10\) wheel turns, giving \(G = 45.0\). Your loaded roll test measures \(r = 0.0315\ \text{m}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ticks per wheel revolution:&lt;/strong&gt;
\(N_{\text{wheel}} = 4 \times P_{\text{motor}} \times G = 44 \times 45.0 = 1980\ \text{ticks/wheel rev}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Wheel circumference and ticks per meter:&lt;/strong&gt;
\(C = 2\pi r = 2\pi (0.0315) = 0.1979\ \text{m/rev},\)
\(K_{\text{ticks/m}} = \frac{1980}{0.1979} \approx 10\,004\ \text{ticks/m} \quad (\approx 0.10\ \text{mm/tick}).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sanity check:&lt;/strong&gt; If the wheel advances 1,000 ticks in 0.50 s, linear wheel speed is \(v = \frac{1000 / 10004}{0.50} = 0.200\ \text{m/s}\). Recording \(10\,004\ \text{ticks/m}\) in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; eliminates guesswork across Chapters 07–11.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab-pointers-then-the-list&quot;&gt;Lab: pointers, then the list&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Power remains off. Battery is disconnected and sitting where a tug cannot land it on the terminals. Gear teeth pinch; turn shafts from the pointer, not from inside the mesh. If a shaft will not move, stop and look for a rub. Do not add voltage to “free” it.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Gearmotor or a scale gear drawing&lt;/td&gt;
      &lt;td&gt;The ratio&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Two pointers: tape flags or cardboard arrows&lt;/td&gt;
      &lt;td&gt;Input and output&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Protractor, or a printed one&lt;/td&gt;
      &lt;td&gt;Output angle and backlash&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hex key for the set screws&lt;/td&gt;
      &lt;td&gt;The flat&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Zip tie&lt;/td&gt;
      &lt;td&gt;Service loop, if a wire is already on the motor&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Prediction, measurement, checklist&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Write the prediction. Tooth counts if visible, otherwise the stamp. Compute \(\theta_{\text{out}}\) for exactly three input turns.&lt;/li&gt;
  &lt;li&gt;Tape a pointer to the input and one to the output. Mark a zero on the bench.&lt;/li&gt;
  &lt;li&gt;Turn the input exactly three revolutions. Measure the output angle. Compare with the prediction and write the percent difference.&lt;/li&gt;
  &lt;li&gt;Hold the input. Rock the output. Estimate backlash in degrees at the wheel.&lt;/li&gt;
  &lt;li&gt;Walk the checklist and tick each line in the notes: fasteners tight; set screws on flats; wheels not rubbing; caster spins and trails; service loop, solder joint quiet; battery cannot slide; both motors turn by hand with similar drag.&lt;/li&gt;
  &lt;li&gt;If you only have a drawing, do steps 1 and the sketches, write “drawing, paper result is the deliverable,” and still draft the checklist as the procedure you will run when the box arrives.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A ratio within about 10 percent of the nameplate when hardware is present, or a clearly labeled paper prediction when it is not. Backlash quoted in degrees. Every checklist line marked pass, fail, or “not on the bench yet.” Failed lines name the fix, not a plan to discover it during the powered lab.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Angle is the reciprocal of the prediction&lt;/td&gt;
      &lt;td&gt;Input and output names were swapped&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Angle is half or double&lt;/td&gt;
      &lt;td&gt;Gearbox and an external belt were not multiplied, or one of them was applied twice&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Direction wrong, ratio right&lt;/td&gt;
      &lt;td&gt;An idler was treated as a ratio change, or a mesh was left out of the sign count&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Output flops tens of degrees&lt;/td&gt;
      &lt;td&gt;Set screw, stripped hub, or a skipped belt tooth&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;One motor much stiffer by hand&lt;/td&gt;
      &lt;td&gt;Rub, or a damaged gearbox; do not power it to confirm&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy only what the checklist proved missing. A protractor app is enough if you can sight along the pointer. A TT motor with a visible stamp is the part that makes step 1 a reading instead of a guess.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;TT gearmotor&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ giảm tốc TT encoder&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;45.000–120.000 each&lt;/td&gt;
      &lt;td&gt;Stamp is the prediction&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;M3 kit, if the sort in Lesson 02 was short&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ốc M3&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;25.000–70.000&lt;/td&gt;
      &lt;td&gt;Fasteners line on the checklist&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Caster, if the nose still drags&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh caster robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;12.000–40.000&lt;/td&gt;
      &lt;td&gt;Must trail&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Search pages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=%C4%91%E1%BB%99ng+c%C6%A1+gi%E1%BA%A3m+t%E1%BB%91c+TT&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=%C4%91%E1%BB%99ng%20c%C6%A1%20gi%E1%BA%A3m%20t%E1%BB%91c%20TT%20encoder&quot;&gt;Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=%E1%BB%91c%20M3&quot;&gt;Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=%E1%BB%91c%20M3&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prices move. Do not buy a second gearbox to avoid measuring the first one.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Stamp 1:90. Predict the output angle for three input turns.&lt;/li&gt;
  &lt;li&gt;You measure \(12^\circ\) after those three turns on a 1:48 motor. Is that inside 10 percent of 22.5°? What do you check next?&lt;/li&gt;
  &lt;li&gt;A 20-tooth pinion drives a 20-tooth idler, which drives a 40-tooth gear. Give \(i\) and the direction of the output relative to the pinion.&lt;/li&gt;
  &lt;li&gt;Gearbox 1:48 and a GT2 stage with 20 teeth on the motor pulley and 40 teeth on the wheel pulley. What \(i\) does the wheel see? What is \(\theta_{\text{out}}\) for three motor turns?&lt;/li&gt;
  &lt;li&gt;List the seven checklist lines and star the two that, if skipped, most directly invent phantom odometry.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;\(\theta_{\text{out}} = 3/90 \times 360^\circ = 12^\circ\). 2. $$&lt;/td&gt;
          &lt;td&gt;12 - 22.5&lt;/td&gt;
          &lt;td&gt;/22.5 \approx 47\(percent, well outside the band. Check that you turned the motor shaft three times, that you read 1:48 and not a different stamp, and that a belt was not left out or added. 3.\)i = 40/20 = 2\(. The simple idler does not change the ratio. Two external meshes restore direction, so the output matches the pinion. 4.\)i = 48 \times (40/20) = 96\(. Three motor turns give\)3/96 \times 360^\circ = 11.25^\circ$$ at the wheel. 5. Fasteners tight; set screws on flats; wheels not rubbing; caster spins and trails; service loop with a quiet solder joint; battery cannot slide; similar hand drag. Star the set screw and the rub: both leave the encoder happy while the floor disagrees.&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Gear_ratio&quot;&gt;Gear ratio&lt;/a&gt; — compound trains, which is the gearbox-times-belt case.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.sdp-si.com/resources/elements-of-metric-gear-technology/index.php&quot;&gt;SDP-SI gear technology&lt;/a&gt; — backlash as a designed gap, in catalog language.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ros-controls/ros2_controllers/tree/master/diff_drive_controller&quot;&gt;diff_drive_controller&lt;/a&gt; — where your measured \(i\), \(b\), and \(r\) will be typed in.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Mechanical failure modes and strain relief</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter06/06_07_mech_failure_modes/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter06/06_07_mech_failure_modes</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;60–80 minutes&lt;/strong&gt;, including a power-off tug test and one strain-relief fix with a before-and-after photo.&lt;/p&gt;

&lt;p&gt;Watch a student robot drive smoothly on a desk, then watch what happens ten minutes later on a tiled hallway: a motor wire snaps flush with the solder tab, an encoder plug backs halfway out, and the robot starts spinning in circles. Almost all early field failures are not algorithmic—they are &lt;strong&gt;fatigue, vibration loosening, and missing strain relief&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can name the failure when a wheel clicks while the encoder still counts, when a plastic horn strips, when a solder joint breaks after the chassis flexes, and when a rubbing wheel smells like a stall. You can add strain relief so the joint sees no bend, leave a service loop, and write two failure modes you will check on Capstone day before you call the robot ready.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 01 through 06: \(b\) and \(r\), set screws on D-flats, gear ratio, caster trail, and a chassis material that can crack. Chapter 05 is where stall current became a number on your motor. This lab never applies that current. Power stays off.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-mechanical-fatigue-vibration-loosening-and-strain-relief&quot;&gt;Key ideas: Mechanical Fatigue, Vibration Loosening, and Strain Relief&lt;/h2&gt;

&lt;p&gt;Reliability on a rolling robot comes from anticipating three physical failure mechanisms before they strike:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Metal Fatigue &amp;amp; Bend Radius:&lt;/strong&gt; Copper wire strands survive thousands of flex cycles when bent gently, but fatigue and fracture rapidly when bent sharply right at a rigid solder joint (where solder wicks up the strands and creates a sudden stiffness discontinuity). Securing the cable insulation to the chassis &lt;em&gt;before&lt;/em&gt; the solder joint transfers all bending away from the joint.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Vibration-Induced Fastener Loosening:&lt;/strong&gt; Rolling over floor grout and gear tooth meshing excite continuous transverse micro-slip under bolt heads, gradually unwinding nuts. Positive retention (nyloc nuts, split washers, or medium-strength threadlocker) breaks this unwinding mechanism.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Connector Retention &amp;amp; Mechanical Keys:&lt;/strong&gt; Pin headers and Dupont jumpers rely on light friction and vibrate loose easily. Locking connectors (JST-XH, XT30/XT60) plus zip-tie anchor points eliminate intermittent opens that masquerade as “random firmware bugs”.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ROS will plot a smooth odometry trail from encoder ticks and from the \(i\), \(b\), and \(r\) in your notes. Several mechanical failures leave those ticks perfect and the floor path wrong. A set screw off the flat, a stripped hub, a wheel that rubs, and a caster that lifts all look, on a topic monitor, like a tuning problem. They are hand problems. You fix them with the battery disconnected, then you earn the right to tune. Strain relief is the one that shows up an hour into a demo: the joint flexed every bump, the copper fatigued, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; still publishes into a motor that is electrically gone.&lt;/p&gt;

&lt;h2 id=&quot;the-failures-you-can-find-with-your-hands&quot;&gt;The failures you can find with your hands&lt;/h2&gt;

&lt;p&gt;Set screw off the D-flat. The hub clicks, the wheel pauses, the motor and its encoder keep turning. The robot barely moves for the current you hear. Odometry adds \(r \Delta\theta\) as if the wheel had matched the motor. That is the classic odometry lie. Lesson 02 already seated the screw. This lesson is the re-check after transport, because a screw on the crown loosens.&lt;/p&gt;

&lt;p&gt;Horn or hub stripped in plastic. Servo horns and cheap wheel hubs cut their own teeth in soft plastic. Once the teeth are gone, the screw still feels tight and the output just rounds out the hole. Replace the horn or the hub. Glue over stripped teeth is a one-demo fiction.&lt;/p&gt;

&lt;p&gt;Wire fatigue at the solder joint. The chassis flexes, the joint is the stiff point, and the strand breaks inside the insulation a few millimeters from the shiny solder. The cable looks fine. Strain relief moves the bend away from that point: a zip tie, or a blob of hot glue, anchoring the wire to the plate so the solder sees no motion. Ahead of the anchor, leave a service loop, a deliberate slack curve. A wire pulled tight between a moving wheel pod and the controller has no loop to give, so the joint pays the bend. Do this on motor leads and on encoder leads. An encoder wire that fatigues looks like a dead sensor in Chapter 04’s firmware.&lt;/p&gt;

&lt;p&gt;Wheel rubbing the chassis. The tread kisses the plate or a screw head. Speed falls, current rises toward the stall value from Chapter 05, and after a few seconds the motor smells hot. You can feel the rub with the battery out: turn the wheel by hand and listen for a scrape once per revolution. Space the wheel, or file the offender, before you call it a driver problem.&lt;/p&gt;

&lt;p&gt;Caster shimmy at speed. The nose oscillates. Trail is reversed or too short, or the center of mass from Lesson 01 barely loads the caster, so the patch cannot settle. Fix trail and loading. A software damper will not invent trail.&lt;/p&gt;

&lt;p&gt;Battery sliding. A loose pack shifts the center of mass, the support triangle is suddenly empty under that mass, and the caster tips up. Then one drive wheel unloads and the robot yaws. A printed strap or a zip-tied pocket, from the material plan in Lesson 06, keeps the pack where you measured \(b\).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;The chassis regions these failures live in: wheels, axle line, caster, and the wires along the plate&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Use the figure as a map. Touch each region during the lab: both hubs, both wheel gaps, the caster fork, the battery footprint, and every wire that leaves a solder joint.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;The encoder reports one full motor revolution at the wheel shaft after the ratio, and the set screw slips that whole revolution. With \(r = 31.2\) mm,&lt;/p&gt;

\[s_{\text{phantom}} = 2\pi r = 2\pi \times 31.2 \approx 196\ \text{mm}.\]

&lt;p&gt;The map gains about 20 cm the tile never gave. Two such slips per meter of commanded travel is a 40 cm scale error that no covariance setting will honestly cover. The repair is mechanical: flat under the screw, tug the wheel, photograph it.&lt;/p&gt;

&lt;p&gt;A rubbing wheel is the thermal cousin. If the free-rolling motor in Chapter 05 drew a small no-load current and a rub pushes the operating point toward stall, the heat in the winding follows that current. You do not need a new number today. You need the scrape gone before voltage returns.&lt;/p&gt;

&lt;h3 id=&quot;explanation-estimating-impact-force-and-wire-strain-margin&quot;&gt;Explanation: Estimating Impact Force and Wire Strain Margin&lt;/h3&gt;

&lt;p&gt;Why does a tiny 0.5 kg robot bumping a wall at walking speed snap an unrestrained connector? A quick impulse calculation reveals the hidden forces:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Collision deceleration force:&lt;/strong&gt; Suppose your \(m = 0.8\ \text{kg}\) chassis drives at \(v = 0.5\ \text{m/s}\) into a rigid baseboard and stops over a deformation distance of \(d = 2\ \text{mm} = 0.002\ \text{m}\). By work-energy (\(F_{\text{avg}} d = \frac{1}{2} m v^2\)):
\(F_{\text{avg}} = \frac{0.5 \times 0.8 \times 0.5^2}{0.002} = \frac{0.10}{0.002} = 50\ \text{N}\)
— equivalent to a &lt;strong&gt;5 kg&lt;/strong&gt; weight slamming into the front edge (over \(6g\) of deceleration)!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Effect on an unstrapped 200 g battery pack:&lt;/strong&gt; Under \(6g\) deceleration, a \(0.20\ \text{kg}\) battery pulls forward on its power leads with \(F = 0.20 \times 6 \times 9.81 \approx 11.8\ \text{N}\), easily yanking a standard JST-PH plug (retention force \(\approx 5\text{–}8\ \text{N}\)) or tearing a thin solder pad right off a PCB.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Strain-relief fix:&lt;/strong&gt; Anchoring the battery mechanically with Velcro + a rigid stop and zip-tying the motor cable 15 mm behind the solder lug drops the tension reaching the electrical joint to nearly \(0\ \text{N}\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab-tug-test-and-one-relief&quot;&gt;Lab: tug test and one relief&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Battery disconnected, driver unplugged, USB unplugged if the board is on the robot. The tug test is a hand test. Hot glue is the only heat: keep the nozzle off insulation you care about, and off your fingers. Do not test strain relief by powering the motor and grabbing the wheel.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;The chassis, or a motor lead on the bench if the plate is late&lt;/td&gt;
      &lt;td&gt;The joints&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Zip ties, or a hot-glue stick and gun&lt;/td&gt;
      &lt;td&gt;One strain relief&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Phone&lt;/td&gt;
      &lt;td&gt;Before and after&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Two Capstone-day checks&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;With power off, tug every connector along its housing, not by the wire. A connector that creeps out gets reseated and, if the housing allows, a drop of glue on the housing only.&lt;/li&gt;
  &lt;li&gt;Flex each wire at the solder joint. If the joint bends, the relief is missing.&lt;/li&gt;
  &lt;li&gt;Photograph the worst joint. Add a zip tie or a glue anchor so the bend starts at least a centimeter away from the solder. Leave a service loop. Photograph again.&lt;/li&gt;
  &lt;li&gt;Twist each wheel. Confirm the set screw holds and the tread does not scrape.&lt;/li&gt;
  &lt;li&gt;Try to slide the battery. If it moves, note the strap you still owe.&lt;/li&gt;
  &lt;li&gt;Write two failure modes you will check on Capstone day, in the order you will check them, before anyone publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A before photo and an after photo of one relieved joint, and two written checks. A good pair is: set screw on the flat, and no scrape when the wheel turns by hand. The wire’s solder joint stays still while you wag the service loop.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheel clicks, shaft does not&lt;/td&gt;
      &lt;td&gt;Set screw off the flat, or a stripped hub&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Strand broken next to a perfect-looking joint&lt;/td&gt;
      &lt;td&gt;Fatigue; add relief on the replacement joint&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Scrape once per turn, motor smell in your memory of Chapter 05&lt;/td&gt;
      &lt;td&gt;Wheel rub&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Caster slaps side to side&lt;/td&gt;
      &lt;td&gt;Trail or center of mass&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Battery shifts when you tilt the plate&lt;/td&gt;
      &lt;td&gt;Strap missing; caster will unload on the first accel&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;You need a bag of zip ties and a hot-glue stick more than a new motor. Buy a spare hub only if the one you have is already rounded out.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Zip ties&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây rút nhựa&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;10.000–30.000&lt;/td&gt;
      &lt;td&gt;Strain relief and a temporary battery strap&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hot glue&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keo nến&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;Anchor the wire, keep glue off the contacts&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Spare TT wheel&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh xe cao su 65mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–45.000&lt;/td&gt;
      &lt;td&gt;If the hub is stripped&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Search pages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=d%C3%A2y%20r%C3%BAt%20nh%E1%BB%B1a&quot;&gt;Shopee: dây rút&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=keo%20n%E1%BA%BFn&quot;&gt;Lazada: keo nến&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=b%C3%A1nh+xe+cao+su&quot;&gt;Hshop: bánh xe&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=d%C3%A2y%20%C4%91i%E1%BB%87n&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prices move. A connector that fell off is a Chapter 01 soldering job, not a software issue.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;The robot creeps forward on the floor while the encoder count races. Name the failure and the hand check.&lt;/li&gt;
  &lt;li&gt;A servo horn’s plastic teeth are rounded. The screw is tight. What do you replace, and why is glue a weak plan?&lt;/li&gt;
  &lt;li&gt;Motor leads are soldered and pulled straight to the driver with no slack. What do you add, and what must the solder joint feel when you shake the wire?&lt;/li&gt;
  &lt;li&gt;Current is high and the speed is low, with a hot smell. The set screw holds. What mechanical interference do you look for?&lt;/li&gt;
  &lt;li&gt;Write your two Capstone-day checks as full sentences a teammate can perform with the battery still disconnected.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;The wheel is slipping on the shaft: set screw off the flat or a stripped hub. Twist the wheel by hand with power off; it should not click. 2. Replace the horn. Glue on rounded teeth lets the next stall finish the hole. 3. A zip tie or hot-glue anchor plus a service loop. The solder joint should stay still. 4. The wheel rubbing the chassis or a screw head. Turn it by hand and remove the scrape. 5. Any two from this lesson, performed power-off: screw on the flat, no scrape, caster trails and is loaded, battery does not slide, each solder joint has relief. The sentences should say what “pass” looks like.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.adafruit.com/wires-and-connectors/wire&quot;&gt;Adafruit, wires and connectors&lt;/a&gt; — wire, joints, and why the strand fails next to the solder.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/connector-basics/all&quot;&gt;SparkFun connector basics&lt;/a&gt; — housings you tug by the shell, not by the conductor.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://reprap.org/wiki/Print_Troubleshooting&quot;&gt;RepRap print troubleshooting&lt;/a&gt; — stripped plastic hubs and layer cracks, if the failed part was printed.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chassis materials: 3D-print vs laser-cut</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter06/06_06_materials_print_laser/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter06/06_06_materials_print_laser</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;70–90 minutes&lt;/strong&gt;. You can finish the lab from a photo of a broken printed part. Owning a printer is optional.&lt;/p&gt;

&lt;p&gt;Two brackets with identical CAD geometry can behave completely differently once bolted to a robot: one survives a 1-meter drop onto concrete while the other snaps cleanly in half at the first motor jolt. The secret is not magic plastic—it is &lt;strong&gt;anisotropy&lt;/strong&gt; in 3D-printed layers versus &lt;strong&gt;brittle fracture&lt;/strong&gt; in laser-cut acrylic plates.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can choose a bought acrylic 2WD plate for Capstone A and reserve 3D printing for brackets, sensor mounts, and a battery strap. You can predict how a 0.1–0.2 mm laser kerf changes a drawn hole, name the temperature at which PLA goes soft in a closed car, and orient a motor mount so bolt tension does not peel the printed layers apart.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You know the chassis geometry from Lesson 01 and the M3 fastener habit from Lesson 02. You do not need a CAD license. A pencil sketch with a thickness labeled in millimeters is enough.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-mechanics-of-materials-print-anisotropy-and-bending-stiffness&quot;&gt;Key ideas: Mechanics of Materials, Print Anisotropy, and Bending Stiffness&lt;/h2&gt;

&lt;p&gt;Choosing chassis materials draws on three core principles from solid mechanics and digital fabrication:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Beam Bending Stiffness (\(E I\)):&lt;/strong&gt; Under a load \(F\), a cantilevered plate or bracket of length \(L\), width \(w\), and thickness \(t\) deflects by \(\delta = \frac{F L^3}{3 E I}\), where \(I = \frac{w t^3}{12}\) is the second moment of area. Because stiffness scales with the &lt;strong&gt;cube of thickness&lt;/strong&gt; (\(t^3\)), doubling plate thickness cuts deflection by a factor of &lt;strong&gt;8&lt;/strong&gt;—far more effective than switching plastics.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;FDM Print Anisotropy:&lt;/strong&gt; Fused-deposition 3D prints (PLA, PETG) are strong &lt;em&gt;along&lt;/em&gt; the extruded filament lines (\(X\text{–}Y\) plane) but often 30%–50% weaker &lt;em&gt;across&lt;/em&gt; layer boundaries (\(Z\) axis). Orienting a motor bracket so bending tension pulls across layers invites delamination at the first bump.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Ductile vs. Brittle Failure &amp;amp; Stress Concentrations:&lt;/strong&gt; Cast acrylic is stiff and laser-cuts cleanly, yet it is brittle: sharp internal \(90^\circ\) corners concentrate stress and launch cracks. Adding small corner fillets (\(R \ge 2\ \text{mm}\)) and using PETG/aluminum at high-impact mounts prevents sudden shatter.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capstone A has to roll this month. A printed copy of an entire chassis spends the week on a machine you may not have, and a PLA plate left in a parked car can warp before the URDF is finished. Buy the acrylic 2WD frame. Print the pieces the kit does not include: a bracket that aims an ultrasonic sensor, a strap that stops the battery from sliding (Lesson 07), a small cover over a solder joint. ROS never sees the polymer name. It sees wheel positions. A cracked motor mount changes those positions and the \(b\) you measured becomes a lie. Material choice is how \(b\) stays the number in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;acrylic-plate-printed-bracket&quot;&gt;Acrylic plate, printed bracket&lt;/h2&gt;

&lt;p&gt;Cast acrylic around 3 mm is the usual kit plate. It is flat, it laser-cuts cleanly, and it is brittle. A screw tightened in a hole that is already tight wedges the plastic and a crack runs between holes. Give every M3 a clearance hole and a nut on the far side, the nyloc from Lesson 02. Acrylic is a poor snap-fit material. Design clips in PETG or keep clips out of the Capstone.&lt;/p&gt;

&lt;p&gt;Laser kerf is the width of material the beam removes, often 0.1–0.2 mm on a hobby cutter in acrylic. Shops cut the path you drew. A hole whose drawn diameter is the path diameter comes out larger by about that kerf:&lt;/p&gt;

\[d_{\text{cut}} \approx d_{\text{drawn}} + k.\]

&lt;p&gt;Draw a 3.2 mm clearance hole and a 0.2 mm kerf yields about 3.4 mm, which an M3 screw still passes through cleanly. Draw a 3.0 mm hole because you wanted the screw to bite the acrylic, and the cut lands near 3.2 mm, the screw rattles, and over-tightening starts the crack. Tell the shop the thickness, send DXF in millimeters, and keep paths closed. An open contour is a request the machine finishes in a direction you did not choose.&lt;/p&gt;

&lt;p&gt;PLA prints easily and softens in a closed car, around 55–60 °C, which is a normal afternoon in Vietnam. A bracket that was square at breakfast can creep by the time lab starts. PETG tolerates that heat better and strings more while printing, so the part needs a few minutes with a knife. For a motor mount the hidden requirement is orientation. Fused filament is weakest between layers. Bolt tension and motor torque that peel one layer off the next will split the ear of the bracket. Turn the model so the plane that tension would crack cuts across the layer lines. The crack then has to break a strand, not unzip a bond. A mount printed “flat and pretty” with the ear standing on layer boundaries is the one that fails on the first stall.&lt;/p&gt;

&lt;p&gt;You can do all of this without a printer. A campus lab or a shop will take an STL in millimeters and a named material, PLA or PETG, and a note about orientation if you care. A laser shop wants DXF, millimeters, closed paths, and a stated sheet thickness. Put those words in the email so the first cut is the one you measured.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;The plate you are deciding not to reprint: measure it, then print only the brackets it lacks&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The figure is the bought chassis. \(b\) and \(r\) are properties of how that plate holds the wheels. A printed part is allowed to hold a sensor. It is a risky place to reinvent the axle line a week before Chapter 07.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;You send a motor-slot drawing with an M3 hole at \(d_{\text{drawn}} = 3.2\) mm. The shop quotes kerf \(k = 0.15\) mm.&lt;/p&gt;

\[d_{\text{cut}} \approx 3.2 + 0.15 = 3.35\ \text{mm}.\]

&lt;p&gt;An M3 screw is about 3.0 mm on the crest, so 3.35 mm is a comfortable clearance. The nut does the clamping. If instead you draw 2.8 mm hoping for a threaded bite in acrylic,&lt;/p&gt;

\[d_{\text{cut}} \approx 2.8 + 0.15 = 2.95\ \text{mm},\]

&lt;p&gt;and the screw either will not start or will wedge. That wedge is the crack. On the printed battery strap, PLA at 58 °C in a closed cabin is at the softening range. PETG is the material you name for that strap if the robot ever waits in a car. The plate itself stays the acrylic kit.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-comparing-plate-stiffness-and-print-layer-stress&quot;&gt;Explanation: Hand-Comparing Plate Stiffness and Print-Layer Stress&lt;/h3&gt;

&lt;p&gt;Let us calculate two numbers that every robot builder should know before sending a file to a laser cutter or 3D printer:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;The \(t^3\) rule for chassis deflection:&lt;/strong&gt; Compare a \(3\ \text{mm}\) acrylic deck against a \(5\ \text{mm}\) acrylic deck of the same width \(w = 100\ \text{mm}\), supporting a \(5\ \text{N}\) battery overhang at \(L = 80\ \text{mm}\) (Young’s modulus \(E \approx 3.0\ \text{GPa} = 3000\ \text{N/mm}^2\)):
    &lt;ul&gt;
      &lt;li&gt;For \(t = 3\ \text{mm}\): \(I_3 = \frac{100 \times 3^3}{12} = 225\ \text{mm}^4 \implies \delta_3 = \frac{5 \times 80^3}{3 \times 3000 \times 225} = 1.26\ \text{mm}\).&lt;/li&gt;
      &lt;li&gt;For \(t = 5\ \text{mm}\): \(I_5 = \frac{100 \times 5^3}{12} = 1041.7\ \text{mm}^4 \implies \delta_5 = \frac{5 \times 80^3}{3 \times 3000 \times 1041.7} = 0.27\ \text{mm}\).
Adding just \(2\ \text{mm}\) of thickness reduces flex by a factor of \((5/3)^3 = 4.63\times\)!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bending stress vs. layer-bond strength in a 3D-printed motor L-bracket:&lt;/strong&gt; A \(4\ \text{mm}\)-thick, \(20\ \text{mm}\)-wide printed bracket holding a motor torque of \(0.50\ \text{N}\cdot\text{m}\) (\(500\ \text{N}\cdot\text{mm}\)) experiences peak bending stress:
\(\sigma_{\max} = \frac{6 M}{w t^2} = \frac{6 \times 500}{20 \times 4^2} = 9.38\ \text{MPa}.\)
If printed flat so tensile stress runs &lt;em&gt;along&lt;/em&gt; filaments (strength \(\approx 45\ \text{MPa}\)), safety factor is \(4.8\). If printed upright so bending pulls &lt;em&gt;across&lt;/em&gt; Z-layers (inter-layer strength \(\approx 15\ \text{MPa}\) with a notch factor of 2), safety factor drops below \(1.0\) and the bracket snaps along a layer line.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab-read-a-broken-layer-sketch-the-orientation&quot;&gt;Lab: read a broken layer, sketch the orientation&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Broken plastic is sharp along the layer that failed. Handle the edge, not the sliver. If you visit a laser shop, the bed and the offcut are hot; let the operator hand you the part. No printer is required, and no solvent is required. Do not “weld” acrylic with random glue while you are still identifying the failure.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;A broken printed part, or a clear photo of one&lt;/td&gt;
      &lt;td&gt;The layer autopsy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pencil, side view and front view&lt;/td&gt;
      &lt;td&gt;Bracket orientation&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Kerf arithmetic and the material sentence&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Look at the break. Layer lines are the fine stripes. Decide whether the crack ran between stripes or across them. Between stripes means the load peeled the weak plane.&lt;/li&gt;
  &lt;li&gt;Sketch a TT motor bracket: a wall on the chassis and an ear the motor bolts to. Mark the bolt axis. Draw the layer lines so they cross the plane the bolt would split. Write “print this face down” on the face that should touch the bed to make those lines.&lt;/li&gt;
  &lt;li&gt;Compute one kerf example for a hole you care about, using \(k = 0.2\) mm if the shop has not told you their number. Record drawn diameter and expected cut diameter.&lt;/li&gt;
  &lt;li&gt;Write the Capstone material plan in one sentence: bought acrylic chassis; printed material and part name for the bracket and the battery strap.&lt;/li&gt;
  &lt;li&gt;If you have neither a part nor a photo, use any failed print picture you can find in a lab, and say so.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A note that names the weak plane, a sketch with bed face and layer direction marked, and a kerf pair such as 3.2 mm drawn and 3.4 mm expected. The material sentence specifies acrylic for the plate and PLA or PETG for the bracket, with PETG called out if the strap sees a hot car.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Crack follows the stripes&lt;/td&gt;
      &lt;td&gt;Layers were the split plane; rotate the part&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Hole is tight and the acrylic crazes&lt;/td&gt;
      &lt;td&gt;Drawn hole ignored kerf, or the screw was used as a thread&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bracket warped after a sunny commute&lt;/td&gt;
      &lt;td&gt;PLA above about 55–60 °C&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Shop cut the outline and skipped holes&lt;/td&gt;
      &lt;td&gt;Open paths, or holes on a layer the shop had turned off&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;STL came back tiny&lt;/td&gt;
      &lt;td&gt;The file was in inches, or in centimeters, and the shop followed the numbers&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Spend the money on the acrylic 2WD chassis. Buy filament only if you, or your lab, will print the brackets. A laser job is a service, priced by the shop from your DXF; bring thickness and millimeter units in the same sentence as the file.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;2WD acrylic chassis&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khung xe robot 2 bánh&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;80.000–200.000&lt;/td&gt;
      &lt;td&gt;The Capstone plate&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;PLA filament, 1 kg&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nhựa PLA&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;250.000–450.000&lt;/td&gt;
      &lt;td&gt;Easy; keep it out of a closed car&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;PETG filament, 1 kg&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nhựa PETG&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;280.000–500.000&lt;/td&gt;
      &lt;td&gt;Better heat, more stringing&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Search pages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=khung+xe+robot&quot;&gt;Hshop: khung&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=nh%E1%BB%B1a%20PLA&quot;&gt;Shopee: nhựa PLA&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=nh%E1%BB%B1a%20PLA&quot;&gt;Lazada: nhựa PLA&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=nh%E1%BB%B1a%20PLA&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prices move. A local laser shop is often a sign on a makerspace door rather than a nationwide SKU. Ask for DXF, millimeters, closed paths, and the sheet thickness they stock, commonly 3 mm acrylic.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Drawn hole 3.4 mm, kerf 0.2 mm. What diameter do you expect on the acrylic, and does an M3 screw still pass?&lt;/li&gt;
  &lt;li&gt;You need the screw to bite printed plastic. What hardware from Lesson 02 replaces “threads cut in PLA by the screw itself”?&lt;/li&gt;
  &lt;li&gt;A motor ear cracked cleanly along the layer stripes the first time the wheel stalled. What was the weak plane, and how do you orient the reprint?&lt;/li&gt;
  &lt;li&gt;The robot sits in a car at about 58 °C. Which filament is the risky one for the battery strap, and which do you name instead?&lt;/li&gt;
  &lt;li&gt;List the file type, the unit, and the extra sentence you send a laser shop, then the file type and the two facts you send a print shop.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;About 3.6 mm. An M3 crest is near 3.0 mm, so the screw passes with extra clearance; keep a nut on the back. 2. A brass heat-set insert, then a machine screw, with a nyloc if the joint vibrates. 3. The interlayer bond was the crack plane. Reprint so layer lines cross that plane; mark the face that goes down on the bed. 4. PLA softens around 55–60 °C. Name PETG for that strap. 5. Laser: DXF, millimeters, closed paths, and the thickness. Print: STL, millimeters, and a named material, plus orientation if the shop will honor it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://reprap.org/wiki/Print_Troubleshooting&quot;&gt;RepRap print troubleshooting&lt;/a&gt; — layer splits, stringing, and the symptoms you just diagnosed.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Polylactic_acid&quot;&gt;Polylactic acid&lt;/a&gt; — why PLA’s useful temperature sits near a hot cabin.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Laser_cutting&quot;&gt;Laser cutting&lt;/a&gt; — kerf as removed width, which is the correction in the worked example.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Wheels, casters, and diff-drive mechanism diagrams</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter06/06_05_wheels_casters_diff/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter06/06_05_wheels_casters_diff</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;70–90 minutes&lt;/strong&gt;. If the robot already rolls, ten of those minutes are a hand rotation about the axle midpoint. If it does not, they are a velocity-arrow sketch.&lt;/p&gt;

&lt;p&gt;Push a shopping cart forward and its front wheels immediately swing neatly behind their pivots; push it backward and they momentarily shudder sideways before flipping around. That tiny offset between the swivel pin and the tire contact patch—called &lt;strong&gt;caster trail&lt;/strong&gt;—is the difference between a mobile robot that glides predictably and one that scrubs sideways and corrupts its own odometry.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can draw the wheel-speed arrows for straight motion, a gentle turn, and a spin in place, and you can compute the spin-in-place yaw rate \(\omega = 2 v_r / b\). You can explain caster trail, why a reversed caster shimmies, and why four fixed wheels waste the stall current you sized in Chapter 05. You can point at the axle midpoint as the pivot and connect &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; linear.x and angular.z to the two motors.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 01’s definitions: \(b\) between contact centers, \(r\) from a roll test, and&lt;/p&gt;

\[v = \frac{v_r + v_l}{2}, \qquad \omega = \frac{v_r - v_l}{b}.\]

&lt;p&gt;You know a bad \(b\) draws a circle on a straight command. Gears (Lesson 03) already set how motor radians become wheel radians.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-instantaneous-center-of-curvature-caster-trail-and-rolling-constraints&quot;&gt;Key ideas: Instantaneous Center of Curvature, Caster Trail, and Rolling Constraints&lt;/h2&gt;

&lt;p&gt;Wheeled mobility rests on three geometric ideas that bridge mechanism diagrams and mobile-robot kinematics:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Instantaneous Center of Curvature (ICC):&lt;/strong&gt; For any rigid body moving in a plane without lateral tire slip, the wheel axes of all fixed-axis wheels must intersect at a single point called the ICC. For a differential-drive robot, both driven wheels share one axle line, so the ICC can lie anywhere along that axle at signed distance \(R = v/\omega\) from the chassis midpoint.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Caster Trail &amp;amp; Self-Alignment:&lt;/strong&gt; A swivel caster offsets its wheel contact patch behind the vertical swivel axis by trail distance \(d_{\text{trail}} &amp;gt; 0\). Floor friction acting at the contact patch creates a restoring torque \(\tau = F_{\text{lat}} d_{\text{trail}}\) that aligns the caster wheel with the local velocity vector—just like a weathervane.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Wheel Scrub &amp;amp; Skid-Steer Trade-offs:&lt;/strong&gt; Whenever two fixed-axis wheels are placed on &lt;em&gt;parallel, non-collinear&lt;/em&gt; axles (as in a 4WD skid-steer robot), their axes never intersect at a finite ICC during a turn. Turning therefore requires deliberate lateral tire scrub, increasing torque demand and degrading pure wheel-encoder odometry.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capstone A is two driven wheels plus one caster. ROS will send &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; with linear.x as \(v\) and angular.z as \(\omega\). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; inverts the two equations and commands a left motor and a right motor. A sign error on one motor is a mechanical fact the controller cannot see: both wheels “forward” in software, one wheel backward on the floor, and the path is a circle about the slower side. You will find that sign in Chapter 07 with the wheels in the air, then confirm it on the floor, where the caster and the rubber finally matter. The arrows you draw today are the picture you will debug against.&lt;/p&gt;

&lt;h2 id=&quot;arrows-trail-and-the-wheel-that-scrubs&quot;&gt;Arrows, trail, and the wheel that scrubs&lt;/h2&gt;

&lt;p&gt;Straight ahead, both contact patches move forward at the same speed. Then \(v_r = v_l\), so \(v\) equals that speed and \(\omega = 0\). The instantaneous center of the motion sits infinitely far to the side: the axle is not rotating about a nearby point.&lt;/p&gt;

&lt;p&gt;A gentle left turn keeps both wheels forward and makes the right one faster. \(\omega\) is positive in the usual robot frame, x ahead and z up, which is a left yaw. The robot rotates about a point on the axle line, outside the left wheel. The caster swivels to follow.&lt;/p&gt;

&lt;p&gt;A spin in place is the pure case. Set \(v_l = -v_r\). Then&lt;/p&gt;

\[v = \frac{v_r + (-v_r)}{2} = 0, \qquad \omega = \frac{v_r - (-v_r)}{b} = \frac{2 v_r}{b}.\]

&lt;p&gt;The body speed is zero and the pivot is the midpoint of the axle, halfway between the contact centers. Mark that point on the plate. If you rotate the robot by hand on a smooth floor, the mark should stay put while the wheels orbit it. A mark at the caster, or a mark between the motor cans, walks in a circle and tells you the geometric center and the kinematic center were different points.&lt;/p&gt;

&lt;p&gt;Caster trail is why the third wheel behaves. On a swivel caster the contact patch sits behind the swivel axis, along the direction of travel. Drag at the patch pulls the wheel into line, the way a shopping-cart wheel settles. Trail is that offset. Mount the caster backwards and the contact patch leads the pivot. The wheel is then unstable and it shimmies, especially as speed rises. A ball caster has essentially no trail: the contact is on the swivel axis. It scuffs instead of aligning, adds friction you did not budget, and still holds the nose up. Capstone A wants a swivel caster with a visible trail, installed so the patch trails.&lt;/p&gt;

&lt;p&gt;Skid steering puts a motor on four fixed wheels, or locks the wheels so none can swivel. Any yaw requires the tires to scrub sideways. Scrub is sliding friction, and it asks the motors for current up near the stall value Chapter 05 treated as a thermal event, not as a normal turn. Capstone A spends that current on rolling. Two driven wheels and one trailing caster are the mechanism.&lt;/p&gt;

&lt;p&gt;The rubber diameter on the bag is still not the rolling radius. Reuse the paper roll from Lesson 01 whenever you change tires, load, or inflation of a soft tire. A larger right-hand radius on an otherwise straight command is a gentle turn you did not ask for.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;Where to put the ruler: contact centers and compressed radius&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/diff_drive_kinematics.png&quot; alt=&quot;Equal arrows, unequal arrows, and the spin that parks the pivot on the axle midpoint&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Use the two figures together. The first keeps \(b\) and \(r\) honest. The second is the arrow language: forward, spin, and the mixed case. When you wire motors in Chapter 07, label the wires to match these arrows before you trust a positive PWM to mean “forward.”&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Take the Lesson 01 chassis, \(b = 0.15\) m, and command a spin with \(v_r = 0.20\) m/s and \(v_l = -0.20\) m/s.&lt;/p&gt;

\[v = 0, \qquad \omega = \frac{2 \times 0.20}{0.15} = 2.667\ \text{rad/s}.\]

&lt;p&gt;A full revolution is \(2\pi\) radians, so the time to spin once is&lt;/p&gt;

\[T = \frac{2\pi}{2.667} \approx 2.36\ \text{s},\]

&lt;p&gt;provided the wheels really reach 0.20 m/s and do not scrub. With \(r = 31.2\) mm the wheel angular speed has magnitude&lt;/p&gt;

\[\dot{\theta} = \frac{0.20}{0.0312} \approx 6.41\ \text{rad/s},\]

&lt;p&gt;opposite signs on the two motors. If one motor lead is flipped, the command \(v = 0.20\), \(\omega = 0\) (both software speeds “positive”) becomes \(v_l = 0.20\) and \(v_r = -0.20\) on the floor. That is this spin, not a straight line. Wheels-up in Chapter 07: positive command, both tires forward. Floor test: the axle midpoint stays put only when you asked for a spin.&lt;/p&gt;

&lt;h3 id=&quot;explanation-computing-icc-distance-wheel-speeds-and-caster-heading&quot;&gt;Explanation: Computing ICC Distance, Wheel Speeds, and Caster Heading&lt;/h3&gt;

&lt;p&gt;Let us trace the velocity vector of every contact patch on a 2WD robot with track width \(b = 0.16\ \text{m}\) and a rear swivel caster mounted \(d_c = 0.10\ \text{m}\) directly behind the axle midpoint.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Locate the ICC:&lt;/strong&gt; Command \(v = 0.24\ \text{m/s}\) and \(\omega = 0.60\ \text{rad/s}\) (left turn). The ICC lies to the left of the axle midpoint at distance:
\(R = \frac{v}{\omega} = \frac{0.24}{0.60} = 0.40\ \text{m}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify the two driven-wheel speeds from the ICC:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Inner (left) wheel is at radius \(R_L = R - b/2 = 0.40 - 0.08 = 0.32\ \text{m} \implies v_L = \omega R_L = 0.60 \times 0.32 = 0.192\ \text{m/s}\).&lt;/li&gt;
      &lt;li&gt;Outer (right) wheel is at radius \(R_R = R + b/2 = 0.40 + 0.08 = 0.48\ \text{m} \implies v_R = \omega R_R = 0.60 \times 0.48 = 0.288\ \text{m/s}\).&lt;/li&gt;
      &lt;li&gt;Check: \((v_R + v_L)/2 = 0.24\ \text{m/s}\) and \((v_R - v_L)/b = 0.096 / 0.16 = 0.60\ \text{rad/s}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Find the steady-state caster swivel angle:&lt;/strong&gt; The caster pivot sits at \((x, y) = (-0.10,\ 0)\) in the body frame, while the ICC is at \((0,\ +0.40)\). The vector from the ICC to the caster pivot is \((-0.10,\ -0.40)\), so its perpendicular velocity vector points along \((+0.40,\ -0.10)\). The caster therefore swings outward by angle:
\(\psi = \arctan\!\left(\frac{d_c}{R}\right) = \arctan\!\left(\frac{0.10}{0.40}\right) = \arctan(0.25) \approx 14.0^\circ.\)
If the caster were fixed straight ahead instead of swiveling, it would scrub sideways at \(14^\circ\) throughout the entire turn.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab-find-the-pivot-or-draw-the-arrows&quot;&gt;Lab: find the pivot, or draw the arrows&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;If you rotate a built robot by hand, the battery is disconnected so a back-driven motor cannot put voltage on a driver. Fingers stay out of the gears. On a hard floor, spin slowly; a finger under a caster is a pinch.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Chassis from Lesson 01, or a scale top-view drawing&lt;/td&gt;
      &lt;td&gt;The mechanism&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tape and a pen&lt;/td&gt;
      &lt;td&gt;The predicted pivot&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Arrow sketch or the hand-test note&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Mark the midpoint of the segment that joins the two contact centers. That is the predicted spin pivot.&lt;/li&gt;
  &lt;li&gt;If the robot rolls, disconnect the battery. Rotate the chassis slowly in place by hand. Watch the mark. A good \(b\) measurement shows the mark staying on one spot of floor while both wheels circle it.&lt;/li&gt;
  &lt;li&gt;Note whether the caster trails. Look from the side: the patch should sit behind the swivel pin in the direction you push. If the fork is reversed, say so in the notes and turn it around before Chapter 07.&lt;/li&gt;
  &lt;li&gt;If you have no rolling chassis yet, draw three top views. Forward: equal arrows. Spin: opposite arrows, pivot marked. Gentle left: both arrows forward, right arrow longer. Label \(v\) and \(\omega\) on each.&lt;/li&gt;
  &lt;li&gt;Write the sign convention you will use in firmware: positive left wheel and positive right wheel both move the robot forward.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A mark that stays put during a careful hand spin, or three labeled arrow drawings. The notes say which caster orientation you will assemble, trail behind the swivel. The sign sentence is something Chapter 07 can test with the wheels up.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;The “midpoint” walks in a circle&lt;/td&gt;
      &lt;td&gt;The mark was between the motor bodies, or one wheel was skidding&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Caster chatters as soon as you push&lt;/td&gt;
      &lt;td&gt;Fork reversed, or almost no weight on the caster&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ball caster scuffs and yaws the plate&lt;/td&gt;
      &lt;td&gt;No trail; fit a swivel caster for Capstone A&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Forward command draws a circle once powered&lt;/td&gt;
      &lt;td&gt;One motor sign is flipped; confirm wheels-up in Chapter 07&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Straight line bends gently with equal commands&lt;/td&gt;
      &lt;td&gt;The two rolling radii disagree&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Lesson 01 bought the plate. This lesson is the wheel and the caster, if those lines were missing. A ball caster is a useful spare to feel “no trail” once. The robot you hand in uses a swivel caster.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Rubber wheel&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh xe cao su 65mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–45.000 each&lt;/td&gt;
      &lt;td&gt;Then roll-test \(r\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Swivel caster&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh caster robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;12.000–40.000&lt;/td&gt;
      &lt;td&gt;Trail visible behind the pin&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ball caster&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh đa hướng bi&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;Optional contrast; it scuffs&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Search pages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=b%C3%A1nh+xe+cao+su&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=b%C3%A1nh%20xe%20cao%20su%2065mm&quot;&gt;Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=b%C3%A1nh%20caster%20robot&quot;&gt;Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=b%C3%A1nh%20xe%20robot&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prices move. Match the hub to the TT D-shaft, or to the coupler bore you measured in Lesson 02.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;\(b = 0.18\) m, \(v_r = 0.15\) m/s, \(v_l = -0.15\) m/s. Find \(v\), \(\omega\), and the time for one full spin.&lt;/li&gt;
  &lt;li&gt;Both wheels at \(+0.22\) m/s, \(b = 0.15\) m. Find \(v\) and \(\omega\), and where the pivot is.&lt;/li&gt;
  &lt;li&gt;You push the robot and the caster wheel flips around and oscillates. What is wrong with the trail, and what do you change mechanically?&lt;/li&gt;
  &lt;li&gt;A four-fixed-wheel robot turns in place on carpet. Why does the current climb toward stall, and what mechanism does Capstone A use so a turn can roll?&lt;/li&gt;
  &lt;li&gt;linear.x = 0.2 and angular.z = 0 leave the robot circling. You have not changed \(b\). What single wiring fact do you test with the wheels up?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(v = 0\), \(\omega = 2\times 0.15/0.18 = 1.667\) rad/s, \(T = 2\pi/1.667 \approx 3.77\) s. 2. \(v = 0.22\) m/s, \(\omega = 0\). The pivot is not a nearby point on the axle; the heading is constant. 3. The contact patch is leading the swivel, so the caster is reversed or the fork has no trail. Remount it so the patch trails. 4. The tires must scrub sideways, and scrub demands stall-level torque. Capstone A uses two driven wheels and one trailing caster so the idle wheel swivels. 5. One motor’s sign is reversed. Wheels up, a positive command should spin both tires in the forward direction you defined in the lab.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ros-controls/ros2_controllers/tree/master/diff_drive_controller&quot;&gt;diff_drive_controller (ROS 2)&lt;/a&gt; — linear.x and angular.z become two wheel velocities through the equations in this lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://navigation.ros.org/setup_guides/odom/setup_odom.html&quot;&gt;Nav2 odometry setup&lt;/a&gt; — what you publish after the arrows are honest.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://wiki.ros.org/diff_drive_controller&quot;&gt;ROS 1 diff_drive_controller wiki&lt;/a&gt; — the same split into left and right wheel joints, documented for ROS 1 and still the right picture.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Siegwart, Nourbakhsh, and Scaramuzza, &lt;em&gt;Introduction to Autonomous Mobile Robots&lt;/em&gt;, is the book-length treatment of this kinematics. Use the links above for the parameters your ROS distro actually reads.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Linkages, cams, belts, and rack-and-pinion</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter06/06_04_linkages_belts_cams/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter06/06_04_linkages_belts_cams</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;80–100 minutes&lt;/strong&gt;, including a cardboard four-bar or a tooth count on a GT2 pulley if you have one.&lt;/p&gt;

&lt;p&gt;Wheels are not the only way robots move parts through space. Open a 3D printer, a robotic gripper, or a walking mechanism and you will immediately meet four-bar linkages, synchronous timing belts, rack-and-pinion rails, and cam followers—each turning a rotary motor into a precisely shaped path without heavy, expensive gearboxes.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can test a four-bar against Grashof’s condition and recognize a crank-rocker, compute a toothed-belt ratio from pulley teeth, and turn a pinion pitch diameter into rack travel per revolution. You can describe a cam rise and a dwell, and you can write down the extra encoder-to-wheel ratio a belt would insert before ROS ever sees the joint.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can form a gear ratio \(i = N_{\text{out}}/N_{\text{in}}\) (Lesson 03) and you know track width \(b\) from Lesson 01. Cardboard, four brads, and a ruler are enough hardware. A GT2 pulley is optional.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-planar-mechanisms-mobility-analysis-and-synchronous-transmission&quot;&gt;Key ideas: Planar Mechanisms, Mobility Analysis, and Synchronous Transmission&lt;/h2&gt;

&lt;p&gt;Designing non-wheel mechanisms rests on three foundational pillars of kinematics of machinery:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Planar Mobility (Grübler–Kutzbach Criterion):&lt;/strong&gt; A planar linkage with \(N\) links (including the fixed ground frame), \(J_1\) one-DOF joints (revolute pins or prismatic sliders), and \(J_2\) two-DOF joints (cams/gears) has mobility \(M = 3(N-1) - 2J_1 - J_2\). A standard four-bar linkage (\(N=4, J_1=4, J_2=0\)) has \(M = 3(3) - 8 = 1\): a single motor uniquely determines the entire mechanism’s pose.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Grashof’s Law for Four-Bar Linkages:&lt;/strong&gt; Ordering the four link lengths from shortest \(s\) to longest \(l\) with intermediates \(p, q\), at least one link can make a full \(360^\circ\) rotation if and only if \(s + l \le p + q\). Violating this turns a crank-rocker into a double-rocker that jams if driven by a continuous-rotation motor.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Synchronous Belts &amp;amp; Rack-and-Pinion Geometry:&lt;/strong&gt; Toothed timing belts (e.g., GT2 with pitch \(p = 2\ \text{mm}\)) eliminate slip by meshing pulley teeth with belt teeth, yielding exact linear travel \(\Delta x = N_{\text{teeth}} \cdot p\) per revolution—the exact same kinematic relation as a rack-and-pinion.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capstone A’s drive is two geared wheels, not a linkage. The mechanisms in this lesson still decide whether you understand the joints you will describe later. In ROS a joint is a name, a type, and a pair of limits. A four-bar that can only rock must be given those limits or the controller will command a pose the cardboard already proved impossible. A GT2 belt between a motor and a wheel multiplies the gearbox ratio from Lesson 03. Forget the belt and the same tick count means the wrong wheel angle, so odometry scale drifts exactly as it did when \(i\) was inverted. Cams show up in limit hardware and in some grippers. You do not need a cam to finish Capstone A. You do need the habit of writing every ratio between encoder and floor.&lt;/p&gt;

&lt;h2 id=&quot;four-bars-toothed-belts-racks-and-cams&quot;&gt;Four bars, toothed belts, racks, and cams&lt;/h2&gt;

&lt;p&gt;A four-bar is four rigid links pinned in a loop. Call the shortest length \(s\), the longest \(l\), and the other two \(p\) and \(q\). Grashof’s condition for at least one link to be able to turn a full revolution relative to the others is&lt;/p&gt;

\[s + l &amp;lt; p + q\]

&lt;p&gt;in the everyday case you want on the bench. Textbooks write \(\le\). Equality is the change-point linkage: it can fold flat and jam, so treat equality as a design you measure twice before you depend on it. When the inequality holds and the shortest link is next to the grounded link, you have a crank-rocker. The short link spins; the opposite link rocks. A windshield wiper is that motion. If \(s + l\) exceeds \(p + q\), no link can spin all the way. The mechanism rocks, and a motor commanded through a full turn will stall against the pin.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/four_bar_linkage.png&quot; alt=&quot;Four-bar: the crank spins, the rocker swings, the coupler connects them&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Read the figure as a loop. The ground link is the one screwed to the chassis. The crank is the link you would put on a motor. The rocker is the link that waves. The coupler is the floating link that joins them. Changing which link you bolt down changes the motion, even when the four lengths stay the same.&lt;/p&gt;

&lt;p&gt;A toothed belt, GT2 on these robots, carries the ratio in the pulley teeth:&lt;/p&gt;

\[i = \frac{N_{\text{driven}}}{N_{\text{driver}}}.\]

&lt;p&gt;There is no slip while the belt is tensioned and the teeth stay engaged. Direction reverses once, the same as one external gear mesh, unless an idler pulley flips it back. The belt length fixes the center distance. You do not “adjust the ratio” by sliding the motors apart. A belt that is too tight eats the sleeve bearings you decided to leave alone in Lesson 02. A belt that is loose skips a tooth under torque. The skip looks like backlash: a dead zone, then a jump, and the encoder never saw the jump because the motor pulley did move. Pluck the belt. A taut low note is the target. A ringing high note is too tight. A flop is too loose.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/belt_pulley.png&quot; alt=&quot;GT2 belt and pulleys: tooth counts set the ratio, tension sets whether the teeth stay engaged&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Rack and pinion turn rotation into a straight line. For one revolution of the pinion, rack travel is the pitch circumference,&lt;/p&gt;

\[x = \pi d,\]

&lt;p&gt;where \(d\) is the pitch diameter of the pinion, the diameter where the teeth effectively roll. A linear slide on a small arm, or a scanner carriage, is this device. Capstone A does not need one. The arithmetic is the same arithmetic as rolling radius: path length along the pitch circle.&lt;/p&gt;

&lt;p&gt;A cam is a shaped rotor against a follower. A rise lifts the follower. A dwell holds it still while the cam keeps turning. Robots meet cams in mechanical limiters and in a few grippers that must pause closed. The dwell is a hard motion limit. In a URDF you would express that pause as a joint limit or as a mechanism that is simply not a free joint. You will not design a cam for Capstone A.&lt;/p&gt;

&lt;p&gt;Whatever you add between the encoder and the tire, write the ratio. Gearbox 48, belt 2, wheel sees \(i = 96\). That product is what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; must use. A joint in ROS is not a place to hide a forgotten pulley.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Grashof first. Lengths \(s = 30\) mm, \(p = 55\) mm, \(q = 70\) mm, \(l = 90\) mm.&lt;/p&gt;

\[s + l = 120\ \text{mm}, \qquad p + q = 125\ \text{mm}.\]

&lt;p&gt;\(120 &amp;lt; 125\), so at least one link can revolve fully. Ground a 70 mm side and drive the 30 mm crank, and the opposite link rocks: a wiper. Change the long link to 110 mm and \(s + l = 140\) while \(p + q = 125\). The inequality fails. The cardboard will bind before a full turn. That bind is a joint limit, felt in your fingers.&lt;/p&gt;

&lt;p&gt;Rack next. A pinion with pitch diameter 12 mm moves the rack, per turn,&lt;/p&gt;

\[x = \pi \times 12 \approx 37.7\ \text{mm}.\]

&lt;p&gt;Half a turn is about 18.8 mm. If your drawing’s pinion is the outside diameter and the pitch diameter is smaller, the travel shrinks with it. Use the pitch diameter, and say which diameter you measured.&lt;/p&gt;

&lt;p&gt;Belt, for the notes: a 20-tooth motor pulley and a 60-tooth wheel pulley give \(i = 3\), same structure as the 12-into-36 gear pair. Three motor turns, one wheel turn, plus the gearbox that sits in front of the motor pulley.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-checking-grashofs-condition-and-gt2-belt-resolution&quot;&gt;Explanation: Hand-Checking Grashof’s Condition and GT2 Belt Resolution&lt;/h3&gt;

&lt;p&gt;Before cutting brackets or ordering pulleys, verify both mechanism feasibility and linear resolution with five lines of arithmetic:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Test a four-bar gripper linkage with Grashof’s law:&lt;/strong&gt; Suppose ground \(L_1 = 60\ \text{mm}\), input crank \(L_2 = 20\ \text{mm}\), coupler \(L_3 = 55\ \text{mm}\), and output rocker \(L_4 = 45\ \text{mm}\).
    &lt;ul&gt;
      &lt;li&gt;Shortest \(s = 20\), longest \(l = 60\), remaining \(p = 45,\ q = 55\).&lt;/li&gt;
      &lt;li&gt;Compare sums: \(s + l = 20 + 60 = 80 \le p + q = 45 + 55 = 100\).&lt;/li&gt;
      &lt;li&gt;Because \(s + l &amp;lt; p + q\) and the shortest link is the input crank, it is a valid &lt;strong&gt;crank-rocker&lt;/strong&gt;: the motor can spin continuously \(360^\circ\) without ever locking up.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute GT2 timing-belt linear travel and stepper resolution:&lt;/strong&gt; Suppose a 20-tooth GT2 pulley (pitch \(p = 2\ \text{mm}\)) is mounted on a 200-step/rev motor running at \(16\times\) microstepping (\(3200\ \text{microsteps/rev}\)):
    &lt;ul&gt;
      &lt;li&gt;Linear travel per full revolution: \(\Delta x_{\text{rev}} = 20 \times 2\ \text{mm} = 40\ \text{mm/rev}\).&lt;/li&gt;
      &lt;li&gt;Theoretical step resolution: \(\delta x = \frac{40\ \text{mm}}{3200} = 0.0125\ \text{mm/microstep}\) (\(12.5\ \mu\text{m}\)).&lt;/li&gt;
      &lt;li&gt;If the motor delivers \(\tau = 0.40\ \text{N}\cdot\text{m}\) on pitch radius \(r_p = \frac{40}{2\pi} \approx 6.37\ \text{mm}\), peak belt tension force is \(F = \tau / r_p = 0.40 / 0.00637 \approx 62.8\ \text{N}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab-cardboard-or-a-pulley-or-a-sketch&quot;&gt;Lab: cardboard, or a pulley, or a sketch&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Brads and thumbtacks are sharp. Keep them on the cardboard, not in a pocket. If you tension a real GT2 belt, power stays off and you keep fingers out of the pulleys. A too-tight belt can fling a loose set screw.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cardboard, four brads or pins, ruler&lt;/td&gt;
      &lt;td&gt;Four-bar&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Or one GT2 pulley pair and belt&lt;/td&gt;
      &lt;td&gt;Tooth count&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pencil&lt;/td&gt;
      &lt;td&gt;The non-Grashof sketch and the rack number&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Lengths, \(i\), or rack travel&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Cut four strips. Use the worked lengths 30, 55, 70, 90 mm between hole centers. Pin them in a loop, ground the 70 mm strip to the table, and turn the 30 mm crank. Watch the rocker.&lt;/li&gt;
  &lt;li&gt;Rebuild with a 110 mm longest link, same other lengths as the failing example, and feel the bind. Sketch both and label which one satisfies \(s + l &amp;lt; p + q\).&lt;/li&gt;
  &lt;li&gt;If you have GT2 hardware, count teeth on the driver and the driven pulley, compute \(i\), and note whether the belt flops, sings, or plucks. Skip the cardboard if the pulley count is the richer measurement, but still sketch one Grashof and one non-Grashof loop with lengths written on the links.&lt;/li&gt;
  &lt;li&gt;If you have neither pulleys nor brads, compute the 12 mm rack example and draw both four-bars to scale. Label the page “drawing.”&lt;/li&gt;
  &lt;li&gt;Write one sentence: encoder-to-wheel ratio for Capstone A, including a belt only if your robot has one.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;Two labeled sketches, one that cranks and one that binds, or a GT2 ratio plus those sketches. The rack number 37.7 mm per turn is in the notes if you did the arithmetic. The encoder-to-wheel sentence matches Lesson 03’s stamp unless a belt multiplies it.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;“Crank” will not spin&lt;/td&gt;
      &lt;td&gt;\(s + l\) is larger than \(p + q\), or the holes are so far from the ends that the real lengths differ from the labels&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Belt skips under a finger torque&lt;/td&gt;
      &lt;td&gt;Too loose, or teeth that do not match the belt pitch&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bearings get hot and the belt rings&lt;/td&gt;
      &lt;td&gt;Too tight&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheel angle from ticks is half what the floor shows&lt;/td&gt;
      &lt;td&gt;A 2:1 belt was left out of \(i\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Cardboard finishes the lab. Buy GT2 only if your drive or a later axis uses a belt. Capstone A’s two TT wheels usually do not.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;GT2 belt&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây đai GT2&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;State the length and the width, often 6 mm&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GT2 pulley&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pulley GT2&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;8.000–30.000&lt;/td&gt;
      &lt;td&gt;Tooth count is the ratio; match the shaft bore&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Rack and pinion set&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;thanh răng bánh răng&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;30.000–90.000&lt;/td&gt;
      &lt;td&gt;Optional; pitch diameter enters \(x = \pi d\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Search pages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=d%C3%A2y+%C4%91ai+GT2&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=d%C3%A2y%20%C4%91ai%20GT2&quot;&gt;Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=d%C3%A2y%20%C4%91ai%20GT2&quot;&gt;Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=GT2&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prices move. A pulley listing that hides the tooth count is not yet a ratio.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Links 25 mm, 40 mm, 45 mm, and 60 mm. Does \(s + l &amp;lt; p + q\) hold? If you ground a 45 mm link and drive the 25 mm link, what motion do you expect?&lt;/li&gt;
  &lt;li&gt;Links 40 mm, 50 mm, 55 mm, and 120 mm. Does a full crank exist?&lt;/li&gt;
  &lt;li&gt;Motor pulley 16 teeth, wheel pulley 48 teeth, gearbox stamped 1:30 in front of the motor pulley. What is \(i\) from motor shaft to wheel?&lt;/li&gt;
  &lt;li&gt;Pinion pitch diameter 20 mm. How far does the rack move in one turn, and in a quarter turn?&lt;/li&gt;
  &lt;li&gt;A cam has a rise over \(90^\circ\) and a dwell over the next \(180^\circ\). What does the follower do during the dwell, and do you need this part for Capstone A?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(s + l = 85\), \(p + q = 85\). Equality is the change-point case; a full relative turn is only the borderline Grashof linkage, and it can fold flat. Treat it as fragile. Driving the short link still wants to be a crank, and you should lengthen a side link slightly if you need a reliable spinner. 2. \(s + l = 160\), \(p + q = 105\). No link can fully rotate. 3. Belt \(i = 48/16 = 3\), times gearbox 30, so \(i = 90\). 4. \(x = \pi \times 20 \approx 62.8\) mm per turn, about 15.7 mm per quarter turn. 5. The follower holds still while the cam turns through that dwell. Capstone A does not need a cam.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;try-it-in-the-browser-teardown--a-3d-mechanism-you-can-take-apart&quot;&gt;Try it in the browser: Teardown — a 3D mechanism you can take apart&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/elvarofficial/teardown&quot;&gt;Teardown&lt;/a&gt; (elvarofficial, MIT) is an open-source Claude Code skill: describe a machine in plain words and you get a single offline HTML page (~800 KB, three.js). The machine explodes and reassembles; a click opens the part that is doing the work, and a live data panel follows the motion. Poses come from real dimensions and a single input, such as crank angle, stroke, or wind. The repo ships a V8 engine, Cessna 337 landing gear (rack-and-pinion), and a 20-leg Strandbeest. Install with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npx skills add elvarofficial/teardown -g -a claude-code&lt;/code&gt;, then run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm install &amp;amp;&amp;amp; npx playwright install chromium&lt;/code&gt; once in the installed folder (Node.js 20 or newer). The three official demos can be built with Node alone; Claude Code is only needed for a new machine.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Build the landing-gear demo: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node sims/build.mjs cessna-live&lt;/code&gt;. A 201 mm rack turns a 32-tooth, module 4 mm pinion through exactly 180°. Check by hand: pitch diameter \(32 \times 4 = 128\) mm, half circumference \(\pi \times 128 / 2 \approx 201\) mm — the same rack-travel formula \(x = \pi d\) used above.&lt;/li&gt;
  &lt;li&gt;Ask Teardown to build a four-bar with the worked-example lengths (30, 55, 70, and 90 mm), or a Strandbeest leg. Compare the panel angles and travels with a hand calculation (Grashof, coupler path).&lt;/li&gt;
  &lt;li&gt;Numbers in the panel come from an AI-written model, so check them against a textbook or your own arithmetic before using them in class.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Four-bar_linkage&quot;&gt;Four-bar linkage&lt;/a&gt; — crank-rocker, and what changes when a different link is grounded.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Grashof%27s_law&quot;&gt;Grashof’s law&lt;/a&gt; — the \(s + l\) test, including the equality case.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Timing_belt_(camshaft)&quot;&gt;Timing belt (toothed belt)&lt;/a&gt; — tooth engagement and why a slack belt skips. The page is written about engines; the mesh idea is the one on your GT2 pulleys.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Gears: spur, worm, planetary, ratio, backlash</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter06/06_03_gear_trains_ratios/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter06/06_03_gear_trains_ratios</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;80–100 minutes&lt;/strong&gt;, including a tooth count on a gearbox you own or on a clear photo of one.&lt;/p&gt;

&lt;p&gt;A bare hobby DC motor spins at 10,000 RPM—fast enough to act as a cooling fan, yet too weak to roll a half-kilogram robot over a doorstep. Gears are the mechanical transformers of robotics: they trade excess speed for usable torque, and understanding their ratio, efficiency, and backlash tells you both how fast your robot can climb a ramp and why its wheels twitch before reversing.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can compute a gear ratio from tooth counts, predict the output angle for a given input angle, and keep the sign of each external mesh straight. You can say what a worm stage is for, quote the textbook planetary ratio while trusting the number stamped on a TT motor, and feel backlash as lost motion that shows up in encoder ticks when the robot reverses.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can measure track width and rolling radius (Lesson 01) and you know a set screw must sit on a D-flat (Lesson 02). Tooth counts are integers. A calculator for one division is enough.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-gear-kinematics-power-conservation-and-nonlinear-backlash&quot;&gt;Key ideas: Gear Kinematics, Power Conservation, and Nonlinear Backlash&lt;/h2&gt;

&lt;p&gt;Gear trains sit directly between motor electrical dynamics and robot motion control:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Kinematic Velocity Ratio &amp;amp; Compound Trains:&lt;/strong&gt; For two meshing spur gears with tooth counts \(Z_{\text{in}}\) and \(Z_{\text{out}}\), equal pitch-line velocity enforces \(\omega_{\text{in}} Z_{\text{in}} = \omega_{\text{out}} Z_{\text{out}}\), giving gear ratio \(G = Z_{\text{out}}/Z_{\text{in}}\). Multi-stage (compound) trains multiply stage ratios: \(G_{\text{total}} = G_1 \cdot G_2 \cdots G_k\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Torque Multiplication &amp;amp; Efficiency:&lt;/strong&gt; Ideal power conservation gives \(\tau_{\text{out}} \omega_{\text{out}} = \tau_{\text{in}} \omega_{\text{in}}\), so \(\tau_{\text{out}} = \eta G \tau_{\text{in}}\), where \(\eta \in (0,1)\) accounts for tooth-sliding and bearing losses (high for spur/planetary \(\approx 0.85\text{–}0.95\) per stage; low for self-locking worm gears \(\approx 0.4\text{–}0.6\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Backlash as a Control Deadzone:&lt;/strong&gt; Manufacturing clearance between mating teeth creates angular play \(\Delta\theta_{\text{bl}}\). Every time motor torque reverses sign, the motor spins through \(\Delta\theta_{\text{bl}}\) while the wheel remains stationary—a classic nonlinearity that induces limit-cycle oscillations in high-gain PID loops.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The encoder in Chapter 04 counts the motor shaft, or sometimes a shaft partway down the gearbox. The wheel that touches the floor has been slowed by the ratio \(i\). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; turns ticks into meters only after that ratio, and the rolling radius, are both right. Swap which gear you call the input and you invert \(i\). Distances in odometry are then wrong by \(i^2\): a true ratio of 3 that you store as \(1/3\) stretches or shrinks travel by a factor of 9. The circle you saw from a bad track width in Lesson 01 has a cousin here, a robot whose map scale is simply the wrong number. Write \(i\) next to \(b\) and \(r\) in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;meshes-worms-planetaries-and-the-dead-zone&quot;&gt;Meshes, worms, planetaries, and the dead zone&lt;/h2&gt;

&lt;p&gt;For a pair of spur gears the ratio is&lt;/p&gt;

\[i = \frac{N_{\text{out}}}{N_{\text{in}}} = \frac{\omega_{\text{in}}}{\omega_{\text{out}}}.\]

&lt;p&gt;\(N\) is a tooth count. Torque at the output rises as speed falls, minus friction in the mesh and in the bearings. A 12-tooth pinion driving a 36-tooth gear has \(i = 36/12 = 3\). Three turns of the motor give one turn of the wheel. The wheel torque is about three times the pinion torque, and then a bit less, because teeth rub.&lt;/p&gt;

&lt;p&gt;Each external mesh reverses direction. The wheel gear turns opposite the pinion. A second mesh, pinion into an idler into the output, reverses twice, so the output turns the same way as the input. A simple idler changes direction and leaves the ratio alone, as long as it is one gear, not two gears locked together with different tooth counts. Compound gears multiply. If you also have the gearbox ratio and a belt ratio from Lesson 04, the wheel sees the product&lt;/p&gt;

\[i_{\text{wheel}} = i_{\text{gearbox}} \cdot i_{\text{belt}}.\]

&lt;p&gt;Forgetting either factor is the classic scale error.&lt;/p&gt;

&lt;p&gt;A worm mesh is a screw against a worm wheel. Ratios are high, often tens to one in a single stage, which is why a small arm can hold its pose when the motor power drops: many worm pairs cannot be back-driven. They are also inefficient. A lot of the electrical power you budgeted in Chapter 05 becomes heat in the mesh. That is a fair trade on a gripper that must stay shut, and a bad trade on Capstone A’s drive wheels, where you want the battery to become motion. Feel a worm output: you often cannot turn it from the wheel side with your fingers.&lt;/p&gt;

&lt;p&gt;A planetary set packs a sun, planets on a carrier, and a ring into the diameter of the ring. In the textbook case the ring is fixed, the sun is the input, and the carrier is the output. The ratio is&lt;/p&gt;

\[i = 1 + \frac{N_{\text{ring}}}{N_{\text{sun}}}.\]

&lt;p&gt;That formula is the one stage drawn in textbooks. A metal TT gearbox may be a planetary stack or a spur stack, and the number stamped on the motor, often 1:48 or 1:90 or 1:120, already includes every stage the factory put inside. Trust the stamp for that motor. Use the tooth-count formula when you can see the gears, and the stamp when you cannot.&lt;/p&gt;

&lt;p&gt;Backlash is the lost motion when torque reverses. Teeth are cut a little thin so they do not bind, and the gap shows up as a pause at the start of a turn. Rock the output shaft with the input held: the wheel moves a few degrees before the motor shaft must move. Encoder ticks recorded across that rock are a bias, a small count that does not correspond to floor motion. You will not remove backlash from a molded TT gearbox. You will measure it, eyeball it in degrees, and avoid control tricks that reverse direction every few milliseconds.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/gear_train_ratio.png&quot; alt=&quot;A compound gear train: tooth counts set the ratio, each external mesh flips direction&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The figure shows why you name the input before you divide. Follow torque from the motor to the wheel, multiply each stage’s \(N_{\text{out}}/N_{\text{in}}\), and count the external meshes if you care which way the wheel rolls. An idler drawn between two gears is a direction fixer. It is not an extra factor unless its two sides differ.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Pinion \(N_{\text{in}} = 12\), gear \(N_{\text{out}} = 36\).&lt;/p&gt;

\[i = \frac{36}{12} = 3.\]

&lt;p&gt;Turn the pinion \(90^\circ\). The output turns&lt;/p&gt;

\[\theta_{\text{out}} = \frac{90^\circ}{3} = 30^\circ,\]

&lt;p&gt;in the opposite direction, because there is one external mesh. Three full turns of the motor, \(1080^\circ\), bring the wheel around once. If you reverse the names and store \(i = 12/36 = 1/3\), a commanded wheel distance is wrong by&lt;/p&gt;

\[i_{\text{true}}^{2} = 9.\]

&lt;p&gt;Nine times too long, or nine times too short, depending on which side of the software multiplies. The fix is to point at the pinion, say “input” out loud, and only then divide.&lt;/p&gt;

&lt;p&gt;A stamped TT ratio of 1:48 is the number you keep even if a planetary picture in your head suggests \(1 + N_{\text{ring}}/N_{\text{sun}}\). The stamp already did that arithmetic for the stages you cannot see.&lt;/p&gt;

&lt;h3 id=&quot;explanation-compound-gear-ratio-reflected-inertia-and-backlash-error&quot;&gt;Explanation: Compound Gear Ratio, Reflected Inertia, and Backlash Error&lt;/h3&gt;

&lt;p&gt;Walk through a concrete 2-stage spur gearbox driving a wheel of radius \(r = 0.032\ \text{m}\):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compute total gear ratio:&lt;/strong&gt; Stage 1 meshes a 12-tooth pinion with a 48-tooth gear (\(G_1 = 48/12 = 4\)). Stage 2 meshes a 15-tooth pinion with a 60-tooth gear (\(G_2 = 60/15 = 4\)). Total reduction is:
\(G = G_1 \times G_2 = 4 \times 4 = 16 : 1.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Speed and torque at the wheel:&lt;/strong&gt; If the bare motor spins at \(\omega_m = 3200\ \text{RPM}\) with torque \(\tau_m = 0.025\ \text{N}\cdot\text{m}\) and stage efficiency \(\eta_1 = \eta_2 = 0.90\) (total \(\eta = 0.90^2 = 0.81\)):
    &lt;ul&gt;
      &lt;li&gt;Output shaft speed: \(\omega_w = 3200 / 16 = 200\ \text{RPM} = 20.94\ \text{rad/s}\), giving linear speed \(v = \omega_w r = 20.94 \times 0.032 = 0.67\ \text{m/s}\).&lt;/li&gt;
      &lt;li&gt;Output torque: \(\tau_w = \eta G \tau_m = 0.81 \times 16 \times 0.025 = 0.324\ \text{N}\cdot\text{m}\), giving forward traction force \(F = \tau_w / r = 0.324 / 0.032 = 10.1\ \text{N}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quantify linear deadband from backlash:&lt;/strong&gt; If the output shaft has \(2^\circ = 0.0349\ \text{rad}\) of measured backlash, the wheel tread can rock freely by:
\(\Delta s = r \cdot \Delta\theta_{\text{bl}} = 32\ \text{mm} \times 0.0349 = 1.12\ \text{mm}\)
on every direction reversal—invisible to a motor-mounted encoder until the teeth re-engage.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab-count-predict-compare&quot;&gt;Lab: count, predict, compare&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Power stays off. Gear teeth pinch. If you open a gearbox, the grease is slippery and the tiny clips spring off; counting through a clear photo is enough for this lesson. Do not run the motor from a battery while your fingers are in the mesh.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;A gearbox, a loose gear pair, or a sharp photo of gears you own&lt;/td&gt;
      &lt;td&gt;Tooth counts&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pencil and paper&lt;/td&gt;
      &lt;td&gt;The ratio and the angle&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;The number ROS will reuse&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Name the input gear, the one on the motor side. Name the output gear, the one toward the wheel.&lt;/li&gt;
  &lt;li&gt;Count teeth. On a closed TT motor, read the stamp (1:48 or whatever is printed) and say so.&lt;/li&gt;
  &lt;li&gt;Compute \(i = N_{\text{out}}/N_{\text{in}}\), or copy the stamp. Note how many external meshes reverse the direction.&lt;/li&gt;
  &lt;li&gt;Predict the output angle for a \(90^\circ\) input: \(\theta_{\text{out}} = 90^\circ / i\).&lt;/li&gt;
  &lt;li&gt;If the gears are in your hands, turn the input about a quarter turn against a pointer and compare. Rock the output and estimate backlash in degrees. Eyeball is enough.&lt;/li&gt;
  &lt;li&gt;If you only have a drawing, the paper prediction is the deliverable. Label it “drawing.”&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A written \(i\), a direction note, and a predicted angle. For the 12-and-36 pair, \(i = 3\) and \(\theta_{\text{out}} = 30^\circ\) opposite the input. Molded teeth on a real gearbox should land within about 10 percent of the stamp when you check them properly in Lesson 08. Backlash of a few degrees at the wheel is ordinary.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Ratio came out below 1 on a gearbox that is supposed to slow the motor&lt;/td&gt;
      &lt;td&gt;You called the wheel gear the input&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Direction of the wheel surprises you&lt;/td&gt;
      &lt;td&gt;You forgot one external mesh, or you counted an idler as a ratio change&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Output moves a few degrees before the input must move&lt;/td&gt;
      &lt;td&gt;Backlash; record it, do not “fix” it with glue&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Stamp says 1:48 and your single-stage formula says 4&lt;/td&gt;
      &lt;td&gt;You are looking at one stage of a stack; trust the stamp&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The gear train for Capstone A is already inside a TT gearmotor. Buy the motor with the ratio printed on it, and with an encoder if you can, so Chapter 04’s ticks have a shaft to count. Loose plastic gears are for the tooth-count lab if your motor is sealed.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;TT gearmotor with encoder&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ giảm tốc TT encoder&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;45.000–120.000 each&lt;/td&gt;
      &lt;td&gt;Read the stamp: 1:48 is common&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Plastic gear pack&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh răng nhựa robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;20.000–60.000&lt;/td&gt;
      &lt;td&gt;Useful when the gearbox is sealed and you still want to count teeth&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Search pages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=%C4%91%E1%BB%99ng+c%C6%A1+gi%E1%BA%A3m+t%E1%BB%91c+TT&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=%C4%91%E1%BB%99ng%20c%C6%A1%20gi%E1%BA%A3m%20t%E1%BB%91c%20TT%20encoder&quot;&gt;Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=%C4%91%E1%BB%99ng%20c%C6%A1%20TT%20encoder&quot;&gt;Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=%C4%91%E1%BB%99ng%20c%C6%A1%20gi%E1%BA%A3m%20t%E1%BB%91c&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prices move. Match the shaft to the wheel you bought in Lesson 01 before you pay.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A 15-tooth pinion drives a 60-tooth gear. Find \(i\), the output angle for one motor revolution, and the direction relative to the motor.&lt;/li&gt;
  &lt;li&gt;That pair is followed by a simple 20-tooth idler and then the 60-tooth gear, so the pinion meshes only with the idler. What is \(i\) now, and which way does the output turn relative to the pinion?&lt;/li&gt;
  &lt;li&gt;A fixed-ring planetary stage has \(N_{\text{sun}} = 12\) and \(N_{\text{ring}} = 36\). Compute the textbook \(i\). Your TT motor is stamped 1:48. Which number goes into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; for that motor?&lt;/li&gt;
  &lt;li&gt;You stored \(i = 1/4\) for a gearbox whose true ratio is 4. By what factor are odometry distances wrong?&lt;/li&gt;
  &lt;li&gt;You hold the motor shaft and the wheel still rocks about \(8^\circ\). What is that motion called, and what does it do to a tick count taken while the robot starts a turn?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(i = 60/15 = 4\). One motor revolution gives \(360^\circ/4 = 90^\circ\) at the output, opposite the motor, one external mesh. 2. A simple idler leaves \(i = 4\) and adds a second reversal, so the output turns the same way as the pinion. 3. Textbook stage: \(i = 1 + 36/12 = 4\). For the stamped motor, write 48. The stamp includes the whole stack. 4. By \(i^2 = 16\). 5. Backlash. Those degrees can show up as ticks, or as a pause before ticks move the wheel, so the start of the turn is biased. Record the angle; do not glue the mesh.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;try-it-on-paper-berkeley-humanoid-lite--a-3d-printed-cycloidal-gearbox&quot;&gt;Try it on paper: Berkeley Humanoid Lite — a 3D-printed cycloidal gearbox&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://lite.berkeley-humanoid.org/&quot;&gt;Berkeley Humanoid Lite&lt;/a&gt; (Hybrid Robotics, UC Berkeley) is an open-source, 3D-printed humanoid, 0.8 m tall and 16 kg, with a hardware cost under 5,000 USD at U.S. market prices. The joints use two sizes of 3D-printed cycloidal gearbox actuators (6512 and 5010); the remaining parts are off-the-shelf. Code is MIT; other assets are CC BY-SA 4.0.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;In a typical single-stage cycloidal reducer, a disc with \(Z\) lobes and a fixed ring with \(Z+1\) pins (rollers) has ratio \(i = Z\): one turn of the eccentric shaft advances the disc by \(1/Z\) turn. The official &lt;a href=&quot;https://berkeley-humanoid-lite.gitbook.io/docs/in-depth-contents/motor-characterization&quot;&gt;motor-characterization note&lt;/a&gt; multiplies reflected inertia by \(15^2\), so both gearbox sizes use \(i = 15\). Open the 6512/5010 CAD or print files and check whether \(Z\) is in fact 15.&lt;/li&gt;
  &lt;li&gt;Order-of-magnitude knee torque: if the stance leg supports nearly the full 16 kg and the horizontal moment arm at the knee is only about 0.15–0.20 m, the static moment is already about \(16 \times 9.81 \times 0.15 \approx 24\) N·m to about 31 N·m — in the same range as the 0–20 N·m commands used to measure 6512 stiffness in the RSS 2025 paper. This is a paper estimate, not a measurement on the robot.&lt;/li&gt;
  &lt;li&gt;A 60-hour durability test saw backlash grow from wear. The v1.1.0 notes (2025-09-07) say a planned V2 is exploring off-the-shelf actuators, because the printed cycloidal units are too fragile for high-performance work. The sim-to-real reading is in lesson 10-05; full sources are on the course reference page.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.sdp-si.com/resources/elements-of-metric-gear-technology/index.php&quot;&gt;SDP-SI, Elements of Metric Gear Technology&lt;/a&gt; — tooth geometry, ratio, and backlash from a gear catalog that teaches while it sells.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Gear_ratio&quot;&gt;Gear ratio&lt;/a&gt; — the \(N_{\text{out}}/N_{\text{in}}\) definition and compound trains.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Worm_drive&quot;&gt;Worm drive&lt;/a&gt; — high ratio, efficiency, and why many worms refuse to be back-driven.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Fasteners, bearings, shafts, and couplings</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter06/06_02_fasteners_bearings_shafts/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter06/06_02_fasteners_bearings_shafts</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;70–90 minutes&lt;/strong&gt;, including sorting a small screw kit and photographing a nyloc next to a plain nut.&lt;/p&gt;

&lt;p&gt;More robot demos have been ruined by a single loosened M3 set screw than by faulty control theory. A motor can deliver flawless torque and your PID loop can run at 500 Hz, yet if a hub slips half a millimeter on a D-shaft or a bearing binds axially against a bracket, the encoder sees motion that the tire never transmits to the floor.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can separate M2, M3, and M4 screws by measuring the thread diameter, choose a nyloc nut where a plain nut would walk off under vibration, and seat a set screw on the flat of a D-shaft. You can decide when a 608 ball bearing belongs on a printed idler and when the sleeve bearing already inside a TT gearbox should stay there, and you can match a coupler to the shaft diameters you actually bought.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can use a caliper or, failing that, a known nut as a gauge (Chapter 01 measuring habits). You know the chassis from Lesson 01 has wheels on motor shafts, and that those shafts will be asked to report honest rotation to an encoder later.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-machine-elements-preload-mechanics-and-shaft-power-transmission&quot;&gt;Key ideas: Machine Elements, Preload Mechanics, and Shaft Power Transmission&lt;/h2&gt;

&lt;p&gt;Mechanical joints are the physical boundary conditions of every robotics equation:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Threaded Fasteners &amp;amp; Clamping Preload:&lt;/strong&gt; A screw works like a stiff spring. Tightening it stretches the shank slightly to create clamping force \(F_p\), which generates friction between mating plates so shear loads are carried by friction rather than bending the screw threads. Vibration relaxes preload unless countered by nylon-insert locknuts or threadlocker.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bearings &amp;amp; Friction Regimes:&lt;/strong&gt; Plain bushings rely on sliding contact (higher friction, wear under radial load), whereas rolling-element ball bearings replace sliding with rolling contact, cutting friction by an order of magnitude and keeping shafts concentric so gear teeth maintain proper center distance.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Shafts, Keys, and Couplings:&lt;/strong&gt; Transmitting torque \(\tau\) from a motor shaft to a hub requires a positive mechanical lock (a D-flat, keyway, or clamping collar) plus a coupling that accommodates tiny angular or parallel misalignments without overloading the motor’s internal bearings.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capstone A’s odometry believes the wheel angle and the motor angle are the same angle, scaled by the gear ratio you will write down in Lesson 03. A set screw that misses the flat lets the wheel click while the encoder keeps a perfect count of the motor. The ROS pose then walks across the map while the robot hesitates on the tile. Fasteners are the reason that lie starts or never starts. A nyloc that stays put, a coupler that matches  the shaft, and a bearing that fits the job are the mechanical half of a trustworthy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odom&lt;/code&gt; topic.&lt;/p&gt;

&lt;h2 id=&quot;screws-nuts-and-the-one-shaft-flat-that-matters&quot;&gt;Screws, nuts, and the one shaft flat that matters&lt;/h2&gt;

&lt;p&gt;Metric screws are named by the outer diameter of the thread. M3 means about 3 mm across the crests, and it is the default on this robot: motor mounts, sensor brackets, and the plate itself. Standard M3 coarse pitch is 0.5 mm, so one full turn of the screw advances it half a millimeter into the nut. M2 is the small screw on some sensor boards and is easy to round off. M4 is stouter and often too large for a TT-motor slot. The usual shopping error is a bag labeled “M3” that is actually a mix.&lt;/p&gt;

&lt;p&gt;A plain nut is a hexagon with a clean thread. Vibration from a gearbox walks it off the screw over an afternoon of driving. A nyloc nut has a nylon insert at one end that grips the thread. Finger-start it until the insert bites, then finish with a driver. That grip is the plan for Capstone A. Medium (blue) threadlocker is a liquid backup for metal-on-metal threads you may never open. It is a poor plan for PLA: the solvent can crack the plastic. On a printed bracket, use a nyloc, or melt in a brass threaded insert and put the nyloc on the metal thread the insert gives you.&lt;/p&gt;

&lt;p&gt;The motor shaft on a hobby gearmotor is usually a D-shaft: a cylinder with one flat. The wheel hub or the coupler carries a set screw, a small screw that bites the shaft from the side. That screw must land on the flat. On the round part it raises a burr, the hub buzzes under torque, and the encoder, which is watching the motor, still counts. Odometry then reports motion the floor did not see. After you tighten the set screw, try to twist the wheel on the shaft with your fingers. A seated screw on the flat holds. A screw on the crown slips with a click.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Ball_bearing.jpg&quot; alt=&quot;A ball bearing: inner race, balls, outer race&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A ball bearing, like the one in the photo, has an inner race, a ring of balls, and an outer race. The common 608 is 8 mm bore, 22 mm outside diameter, and 7 mm wide, the skate bearing. It is a good printed idler for a belt, on an 8 mm shoulder or a printed axle that really is 8 mm. It is oversized for a yellow TT motor, whose shaft and whose gearbox were never designed around a 608. Cheap TT gearboxes ride on sleeve bearings, plain bushings. They are noisy and they are normal. Opening the gearbox mid-Capstone to “upgrade” them costs you the ratio stamp and a weekend. Leave them until the robot drives.&lt;/p&gt;

&lt;p&gt;Shaft meets shaft through a coupling when the motor and the load do not share one piece of steel. A rigid coupler demands that the two shafts be collinear. A little angular error becomes a cyclic load, a hot bearing, and a wheel that wobbles. A jaw coupler, with an elastomer spider between two hubs, forgives a small misalignment and is the kinder choice on a student plate. The diameter engraved on the coupler has to match the shaft: 3 mm, 4 mm, and 6 mm are all common on metal gearmotors, and they are not interchangeable. A 6 mm coupler on a 4 mm shaft clamps down to an oval and still slips. Measure the shaft with a caliper before you order. TT wheels usually include a D-hole matched to that motor, so you often need no coupler at all on Capstone A’s drive wheels.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;An M3 screw has pitch 0.5 mm. Four full turns, after the nut is started, take up&lt;/p&gt;

\[4 \times 0.5 = 2.0\ \text{mm}\]

&lt;p&gt;of thread. That is the difference between a snug motor mount and a screw that has bottomed out and started to crack an acrylic hole. Stop when the parts are seated, then about a quarter turn. On the shaft side, suppose the set screw misses the flat and the wheel slips one full revolution while the encoder counts that revolution as motion. With the rolling radius \(r = 31.2\) mm from Lesson 01, the odometry invents a distance&lt;/p&gt;

\[s = 2\pi r = 2\pi \times 31.2 \approx 196\ \text{mm}\]

&lt;p&gt;that the robot never traveled. One missed flat, one phantom 20 cm, every time the torque reverses and the hub clicks.&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-checking-set-screw-torque-transmission-on-a-d-shaft&quot;&gt;Explanation: Hand-Checking Set-Screw Torque Transmission on a D-Shaft&lt;/h3&gt;

&lt;p&gt;Why does seating a set screw squarely onto the D-flat matter so much more than tightening it onto a round shaft? Let us compare the two mechanics quantitatively.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Torque transmitted by friction alone (round shaft):&lt;/strong&gt; Suppose a small gearmotor produces stall torque \(\tau = 0.40\ \text{N}\cdot\text{m}\) on a shaft of radius \(r_s = 3\ \text{mm} = 0.003\ \text{m}\). The tangential force at the shaft surface is:
\(F_t = \frac{\tau}{r_s} = \frac{0.40}{0.003} = 133.3\ \text{N}.\)
With steel-on-steel friction \(\mu \approx 0.15\), holding \(133.3\ \text{N}\) purely by friction requires a normal clamping force of:
\(F_N = \frac{F_t}{\mu} = \frac{133.3}{0.15} = 889\ \text{N}\)
— far more than a tiny M3 set screw in an aluminum hub can sustain without stripping its threads!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Positive engagement on a D-flat:&lt;/strong&gt; When the set screw lands squarely on the machined flat (say offset \(d = 2.5\ \text{mm}\) from the shaft center), the flat physically blocks rotation by normal bearing stress rather than relying solely on friction. Even if the screw loosens slightly, the hub cannot spin freely until the screw backs completely out past the flat depth.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Practical engineering takeaway:&lt;/strong&gt; Always align the set screw perpendicular to the D-flat, add a drop of removable blue threadlocker, and never expect a friction-only collar to survive repeated motor reversals.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab-sort-the-screws-and-find-the-flat&quot;&gt;Lab: sort the screws and find the flat&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Power stays off. Set screws are small and like to launch off a hex key; point the hub away from your eyes. Threadlocker is optional and stays capped; do not sniff it, and do not drip it onto plastic “to be sure.”&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Mixed M2/M3/M4 screws and nuts, even a cheap assortment&lt;/td&gt;
      &lt;td&gt;The sort&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;One nyloc and one plain nut&lt;/td&gt;
      &lt;td&gt;The comparison&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Caliper, or one nut of each size you trust&lt;/td&gt;
      &lt;td&gt;The gauge&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A D-shaft hub or a photo of one you own&lt;/td&gt;
      &lt;td&gt;The flat&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Phone&lt;/td&gt;
      &lt;td&gt;The photo for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Measure the crest diameter of each screw, or try it in a known nut. M2 will not start in an M3 nut. M4 will not start in an M3 nut. M3 runs in smoothly.&lt;/li&gt;
  &lt;li&gt;Separate the pile into three cups. Write the count of M3 screws you actually have; Capstone A eats them.&lt;/li&gt;
  &lt;li&gt;Photograph a nyloc beside a plain nut. On the photo, mark the nylon ring.&lt;/li&gt;
  &lt;li&gt;If you have a wheeled hub, loosen the set screw, seat the D-shaft so the flat faces the screw, and tighten. Twist the wheel by hand. Record whether it holds.&lt;/li&gt;
  &lt;li&gt;If you have a 608, measure 8 × 22 × 7 mm and write one sentence on where it may live (a printed idler) and where it does not (inside the TT gearbox).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;Three labeled groups, a photo of nyloc versus plain nut, and a sentence in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; that names the shaft diameter you will shop for. A seated set screw holds the wheel against finger torque.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Screw almost starts, then jams&lt;/td&gt;
      &lt;td&gt;Wrong size, or a crossed thread; back it out&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nut spins on forever under vibration&lt;/td&gt;
      &lt;td&gt;Plain nut, and no nyloc on that joint&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheel buzzes, encoder still counts&lt;/td&gt;
      &lt;td&gt;Set screw is on the round of the D-shaft&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Coupler clamps hard and still slips&lt;/td&gt;
      &lt;td&gt;Bore is a size larger than the shaft&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Plastic boss cracks the day after threadlocker&lt;/td&gt;
      &lt;td&gt;The locker was the plan for PLA; switch to a nyloc or an insert&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;An M3 assortment plus a small bag of M3 nylocs covers Capstone A. Buy a 608 only when you are printing an idler. Buy a jaw coupler only when two shafts must meet, and only after the caliper reading.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;M3 screw kit&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ốc M3&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;25.000–70.000&lt;/td&gt;
      &lt;td&gt;Assortment of lengths; check for a hex key&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Nyloc nuts&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đai ốc nyloc M3&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000 a bag&lt;/td&gt;
      &lt;td&gt;Nylon insert visible at one end&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;608 bearing&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bạc đạn 608&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;5.000–20.000 each&lt;/td&gt;
      &lt;td&gt;8×22×7 mm, for an idler&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Jaw coupler&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khớp nối trục 4mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–45.000&lt;/td&gt;
      &lt;td&gt;Match the measured shaft, 3 mm or 4 mm or 6 mm&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Search, and compare two sellers. Prices move.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=%E1%BB%91c+M3&quot;&gt;Hshop: ốc M3&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=%C4%91ai%20%E1%BB%91c%20nyloc%20M3&quot;&gt;Shopee: đai ốc nyloc M3&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=b%E1%BA%A1c%20%C4%91%E1%BA%A1n%20608&quot;&gt;Lazada: bạc đạn 608&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=%E1%BB%91c%20v%C3%ADt&quot;&gt;Thế Giới IC: ốc vít&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A screw measures 2.9 mm across the crests and advances 0.5 mm per turn. What designation is it, and how far does it advance in six turns?&lt;/li&gt;
  &lt;li&gt;You have a plain nut on a motor mount that you will drive every lab session. What do you replace it with, and why is blue threadlocker the wrong first plan if that mount is PLA?&lt;/li&gt;
  &lt;li&gt;The wheel hub clicks when you twist it, and the encoder count still increases when the motor is powered later with the wheels up. Where is the set screw sitting?&lt;/li&gt;
  &lt;li&gt;Your caliper reads 4.0 mm on a metal gearmotor shaft. Which coupler bore do you order, and what happens with a 6 mm bore?&lt;/li&gt;
  &lt;li&gt;A friend wants to press 608 bearings into a yellow TT gearbox “so it rolls better.” What do you tell them to do with the sleeve bearings during Capstone A?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;M3, because the crest is about 3 mm and the pitch is the standard 0.5 mm. Six turns advance \(6 \times 0.5 = 3.0\) mm. 2. A nyloc. Blue threadlocker can crack PLA; an insert plus a nyloc keeps the chemistry on metal. 3. On the round of the D-shaft, not on the flat. Loosen, rotate the flat under the screw, retighten, and twist-test. 4. A 4 mm bore. A 6 mm bore clamps to an oval and slips under torque. 5. Leave the sleeve bearings in place. A 608 is 8×22×7 mm and belongs on a printed idler, not inside that gearbox.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.mcmaster.com/&quot;&gt;McMaster-Carr catalog&lt;/a&gt; — look up screw, nyloc, shaft collar, and jaw coupling by name so you learn the dimensions. Use it as a picture dictionary. Ordering from overseas is outside this course.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://reprap.org/wiki/Threaded_insert&quot;&gt;RepRap threaded inserts&lt;/a&gt; — heat-set brass inserts for printed plastic, which is the durable way to put an M3 thread in PLA.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Ball_bearing&quot;&gt;Ball bearing (overview)&lt;/a&gt; — races and the 608 size in context.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Differential-drive chassis anatomy</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter06/06_01_chassis_anatomy/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter06/06_01_chassis_anatomy</id>
   <content type="html">&lt;p&gt;Estimated time: &lt;strong&gt;70–90 minutes&lt;/strong&gt;, including a ruler measurement on a real 2WD chassis or on a careful scale drawing if the kit has not arrived.&lt;/p&gt;

&lt;p&gt;Every autonomous mobile robot—from a $30 classroom rover to an Amazon warehouse tug—begins with the same deceptively simple geometry: two powered wheels sharing an axle and a third passive contact holding the frame level. Yet a two-millimeter mistake with a ruler on day one will haunt your SLAM map months later, bending straight hallways into gentle arcs even when your software has zero bugs.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can point to the two lengths a differential-drive robot actually uses: track width \(b\), taken between the tire contact centers, and rolling radius \(r\), taken from a one-revolution roll test. You can turn a pair of wheel speeds into body speed and yaw rate, place the battery so the caster stays on the floor, and write \(b\) and \(r\) in millimeters so Chapter 07 assembly and the later ROS &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; inherit the same geometry.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can read a millimeter ruler. You know a DC gearmotor turns a wheel (Chapter 05) and that encoder ticks will later stand in for wheel rotation (Chapter 04). You do not need ROS installed, and you do not need the chassis on the bench if you can draw it to scale.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-planar-rigid-body-kinematics-static-stability-and-geometric-calibration&quot;&gt;Key ideas: Planar Rigid-Body Kinematics, Static Stability, and Geometric Calibration&lt;/h2&gt;

&lt;p&gt;Understanding differential-drive anatomy connects three classical engineering pillars directly to modern robotics software:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Planar Rigid-Body Kinematics (\(SE(2)\)):&lt;/strong&gt; A mobile base on a flat floor has three pose coordinates \((x, y, \theta)\), yet only two wheel inputs \((\omega_L, \omega_R)\). Because rubber tires resist sideways sliding, the chassis obeys a &lt;em&gt;nonholonomic constraint&lt;/em&gt; (\(\dot{y}_{\text{body}} = 0\)), mapping wheel velocities linearly to body speed \(v\) and yaw rate \(\omega\) via track width \(b\) and rolling radius \(r\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Static Stability &amp;amp; Support Polygon:&lt;/strong&gt; A chassis remains upright without tipping if and only if the vertical projection of its center of mass (CoM) falls strictly inside the convex hull of its ground contact points (the support triangle formed by the two drive patches and the caster). Placing the battery low and near the axle maximizes tipping margin during hard braking.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kinematic Parameter Identification:&lt;/strong&gt; Odometry integrates wheel encoder ticks over thousands of revolutions. Systematic errors in \(b\) scale angular error proportionally, while errors in \(r\) scale distance error proportionally—making loaded roll tests the foundation of accurate odometry and URDF modeling.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capstone A is this chassis: two driven wheels on one axis, plus a caster or a skid that holds the third corner down. Chapter 08 expands the kinematics. The controller you meet on the ROS path, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt;, converts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; into two wheel commands and converts encoder ticks back into odometry using the same \(b\) and \(r\) you measure here. The URDF in Chapter 09–10 places the wheel links with those lengths. A wrong \(b\) shows up as a circle when you command a straight line, because the controller spends the whole motion “correcting” a turn that the geometry invented. Measure once, write the millimeters in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, and refuse to round them into “about 15 cm” later.&lt;/p&gt;

&lt;h2 id=&quot;two-wheels-one-caster-two-lengths&quot;&gt;Two wheels, one caster, two lengths&lt;/h2&gt;

&lt;p&gt;Look at the plate from above. The left motor and the right motor face outward, and the tires sit on a common axle line. The robot steers by making those tires travel at different speeds. A third support, a swivel caster or a smooth skid, stops the plate from dragging its nose. Capstone A uses the caster. A skid is acceptable on a tiny indoor plate and noisy on tile.&lt;/p&gt;

&lt;p&gt;Track width \(b\) is the distance between the tire contact centers, the middle of each rubber patch that actually touches the ground. The gap between the motor bodies is a mounting dimension for the plate drawing. Kinematics leaves it unused. If you measure from the outside of one tire to the outside of the other, you have included a tread width. Slide each end of the ruler to the middle of the tread, with the robot loaded the way it will roll, and record millimeters.&lt;/p&gt;

&lt;p&gt;Rolling radius \(r\) is the radius the floor sees after the rubber compresses. The number printed on a wheel bag is a free diameter. Under the battery it shrinks. Mark the tire, roll it exactly one revolution along paper, measure the path length \(s\), and compute&lt;/p&gt;

\[r = \frac{s}{2\pi}.\]

&lt;p&gt;A wheel labeled 65 mm that rolls 196 mm in one turn has&lt;/p&gt;

\[r = \frac{196}{2\pi} \approx 31.2\ \text{mm},\]

&lt;p&gt;while half the label is 32.5 mm. That gap is a few percent of every meter your odometry will claim. Weigh the wheel with something close to its share of the robot, or roll the whole chassis straight if both wheels can turn freely. Repeat for the other tire. The two radii should agree within about a millimeter. A larger split means one tire is seated crooked, and a straight command will already yaw.&lt;/p&gt;

&lt;p&gt;The support triangle is the two contact patches plus the caster. The vertical through the center of mass has to land inside that triangle. A battery stood up behind the axle, or a pack hung past the wheels, walks that point out of the triangle. The nose wheelies on acceleration, or the caster, barely loaded, shimmies because its trail never settles. Sit the pack low, between the wheels or a little ahead of the axle, and press the caster mount: it should take a steady share of the weight while both drive wheels stay down.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Use \(b = 0.15\) m, \(v_l = 0.20\) m/s, and \(v_r = 0.30\) m/s. Wheel speeds are linear speeds of the contact patches, positive forward. Body speed and yaw rate are&lt;/p&gt;

\[v = \frac{v_r + v_l}{2} = \frac{0.30 + 0.20}{2} = 0.25\ \text{m/s},\]

\[\omega = \frac{v_r - v_l}{b} = \frac{0.30 - 0.20}{0.15} = 0.667\ \text{rad/s}.\]

&lt;p&gt;In one second the midpoint of the axle travels 25 cm along the heading and the heading changes by \(0.667 \times 180/\pi \approx 38^\circ\). Positive \(\omega\) here means the right wheel is faster, so the robot yaws left when left and right match the nose. If the same wheel speeds are paired with a track width of 0.18 m, because someone measured across the motor cans, the reported yaw rate falls to \(0.556\) rad/s. The controller that trusts 0.18 m will command a wheel-speed difference to cancel a turn the real 0.15 m robot is already making, and the floor path closes into a circle.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/chassis_measures.png&quot; alt=&quot;Track width between contact centers, and the roll that gives rolling radius&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/diff_drive_kinematics.png&quot; alt=&quot;Body speed and yaw rate drawn from the two wheel velocities&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The first figure is the ruler picture: \(b\) from tread center to tread center, \(r\) from the compressed tire. The second figure is the velocity picture behind the two equations. Equal forward arrows give \(\omega = 0\). Unequal arrows yaw the robot about a point that still lies on the axle line. Chapter 08 names that point and the turning radius. Today you only need \(b\), \(r\), \(v\), and \(\omega\) written down.&lt;/p&gt;

&lt;h3 id=&quot;explanation-propagating-a-5-track-width-error-into-a-full-turn-drift&quot;&gt;Explanation: Propagating a 5% Track-Width Error into a Full-Turn Drift&lt;/h3&gt;

&lt;p&gt;Why does measuring across the motor cans instead of the tire contact centers ruin a straight-line or square-path test? Let us compute the exact angular error by hand.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Matrix form of differential-drive kinematics:&lt;/strong&gt; Writing the forward map from wheel angular rates \((\dot\phi_R, \dot\phi_L)\) to body twist \((v, \omega)\) gives:
\(\begin{bmatrix} v \\ \omega \end{bmatrix} = \begin{bmatrix} r/2 &amp;amp; r/2 \\ r/b &amp;amp; -r/b \end{bmatrix} \begin{bmatrix} \dot\phi_R \\ \dot\phi_L \end{bmatrix}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;True vs. assumed geometry:&lt;/strong&gt; Suppose the true contact-patch track width is \(b_{\text{true}} = 0.150\ \text{m}\) and \(r = 0.0312\ \text{m}\), but you mistakenly enter \(b_{\text{model}} = 0.165\ \text{m}\) (+10% error) into the controller.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Command a \(90^\circ\) (\(\pi/2\ \text{rad}\)) in-place turn:&lt;/strong&gt; The controller sets \(v = 0\) and chooses opposite wheel speeds so that \(\omega_{\text{model}} \cdot T = \pi/2\), meaning the wheel differential satisfies:
\((v_R - v_L) T = b_{\text{model}} \frac{\pi}{2} = 0.165 \times 1.5708 = 0.2592\ \text{m}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute the actual physical rotation on the floor:&lt;/strong&gt; The real robot divides that same wheel difference by its &lt;em&gt;true&lt;/em&gt; track width \(b_{\text{true}} = 0.150\ \text{m}\):
\(\Delta\theta_{\text{real}} = \frac{0.2592}{0.150} = 1.728\ \text{rad} = 99.0^\circ.\)
A 10% ruler mistake causes the robot to overshoot every right-angle corner by \(9^\circ\)—after four corners of a square room, your heading is off by \(36^\circ\)!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab-measure-b-and-r&quot;&gt;Lab: measure b and r&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Power stays off. If a battery is already strapped on, disconnect it before a metal ruler crosses the plate, so a motor lead cannot short. This lab has no spinning shaft under voltage.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;2WD chassis, or a scale drawing of the kit you ordered&lt;/td&gt;
      &lt;td&gt;The object you measure&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ruler or caliper, in millimeters&lt;/td&gt;
      &lt;td&gt;Track width&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Paper, tape, pen&lt;/td&gt;
      &lt;td&gt;One-revolution roll for \(r\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;The numbers Chapter 07 and ROS will copy&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Sit the chassis on a flat table the way it will roll. Put the battery in its future place if you already have it.&lt;/li&gt;
  &lt;li&gt;Mark the center of each tread. Measure \(b\) between those marks. Write millimeters, and write beside it the motor-can gap you deliberately left out of the controller.&lt;/li&gt;
  &lt;li&gt;Mark one tire and the paper. Roll exactly one revolution under about that wheel’s share of the weight. Measure \(s\).&lt;/li&gt;
  &lt;li&gt;Compute \(r = s / (2\pi)\) and keep one decimal place in millimeters. Repeat for the other wheel.&lt;/li&gt;
  &lt;li&gt;Press at the caster. Note whether it carries weight. If the battery is still in a box, sketch where it must sit so the mass stays inside the support triangle.&lt;/li&gt;
  &lt;li&gt;If the kit has not arrived, do steps 2–5 on a careful scale drawing and title the note “drawing, not hardware.”&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A dated pair in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, for example \(b = 148\) mm and \(r = 31.2\) mm, with the two wheels agreeing on \(r\) within about 1 mm. The note says whether the numbers came from hardware or from a drawing.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What went wrong&lt;/th&gt;
      &lt;th&gt;What it does later&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;\(b\) taken between motor bodies&lt;/td&gt;
      &lt;td&gt;A straight &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; drives a circle&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;\(r\) copied as half the label diameter&lt;/td&gt;
      &lt;td&gt;Odometry distance runs long or short on every meter&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Battery perched behind the axle&lt;/td&gt;
      &lt;td&gt;Wheelie, or a caster that shimmies&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Left and right radii differ by several millimeters&lt;/td&gt;
      &lt;td&gt;A straight command already yaws&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy a finished 2WD acrylic chassis so Capstone A can be built this month. Print small brackets later (Lesson 06). A plate with TT-motor slots and a caster hole is the usual student kit. Rubber wheels and the caster are often separate lines on the same listing.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;2WD chassis&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khung xe robot 2 bánh&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;80.000–200.000&lt;/td&gt;
      &lt;td&gt;Acrylic plate, motor slots, caster hole&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Rubber wheel&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh xe cao su 65mm&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–45.000 each&lt;/td&gt;
      &lt;td&gt;The label diameter is the start of the roll test, not \(r\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Swivel caster&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh caster robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;12.000–40.000&lt;/td&gt;
      &lt;td&gt;A trailing swivel wheel for Capstone A&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Search pages, because shop slugs change and street prices move:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=khung+xe+robot&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=khung%20xe%20robot%202%20b%C3%A1nh&quot;&gt;Shopee&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=khung%20xe%20robot%202%20b%C3%A1nh&quot;&gt;Lazada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=khung%20xe%20robot&quot;&gt;Thế Giới IC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check that the motor slots match a TT gearbox and that a caster is included or sold next to the plate.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A roll test gives \(s = 200\) mm. Compute \(r\) in millimeters to one decimal place.&lt;/li&gt;
  &lt;li&gt;With \(b = 0.16\) m, \(v_l = 0.25\) m/s, and \(v_r = 0.25\) m/s, find \(v\) and \(\omega\). What does the robot do?&lt;/li&gt;
  &lt;li&gt;Same \(b\), with \(v_l = 0.10\) m/s and \(v_r = 0.30\) m/s. Find \(v\) and \(\omega\).&lt;/li&gt;
  &lt;li&gt;A classmate measured from the outside of the left tire to the outside of the right tire and got 180 mm. Each tread is 20 mm wide. What \(b\) belongs in the controller?&lt;/li&gt;
  &lt;li&gt;The pack sits on a tower behind the axle and the caster lifts when you push the robot forward. Where do you move the pack, and what path does a straight ROS command draw if you leave the tower in place?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(r = 200/(2\pi) \approx 31.8\) mm. 2. \(v = 0.25\) m/s and \(\omega = 0\), so the robot drives straight. 3. \(v = 0.20\) m/s and \(\omega = (0.30-0.10)/0.16 = 1.25\) rad/s, a left yaw when the right wheel is the fast one. 4. Each contact center lies half a tread in from the outside face, so subtract one full tread width: \(b = 160\) mm. 5. Move the pack low and inside the support triangle, a little ahead of the axle if the caster was light. Left on the tower, the caster unloads, heading wanders, and a straight command looks crooked on the floor.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ros-controls/ros2_controllers/tree/master/diff_drive_controller&quot;&gt;diff_drive_controller in ros2_controllers&lt;/a&gt; — wheel separation and wheel radius are parameters of this package. Jazzy’s controller docs are generated from this tree; the package index also lives under &lt;a href=&quot;https://docs.ros.org/en/jazzy/index.html&quot;&gt;docs.ros.org/en/jazzy&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://wiki.ros.org/diff_drive_controller&quot;&gt;ROS 1 diff_drive_controller wiki&lt;/a&gt; — parameter names in plain language. The page is ROS 1; the geometry it asks for is the geometry ROS 2 still uses.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://navigation.ros.org/setup_guides/odom/setup_odom.html&quot;&gt;Nav2 odometry setup&lt;/a&gt; — how a bad odometry source shows up once you leave teleop and ask the robot to navigate.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 05 lab checklist</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter05/05_07_ch05_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter05/05_07_ch05_lab_checklist</id>
   <content type="html">&lt;p&gt;This lesson is a gate. Chapter 06 will put your drive on a chassis, and Chapter 07 will assemble signed left and right PWM, a battery, and a fuse. You pass the gate when the table below is filled with your own evidence: a named driver, a truth table, a motor supply that is not USB, a stall or no-load note, a servo-pulse calculation even if you skipped the horn, a bang-bang stop you can describe, a wiring photo, and two failure modes you either hit or deliberately rehearsed. A blank row means you are still in Chapter 05.&lt;/p&gt;

&lt;p&gt;Before any spacecraft leaves a cleanroom, engineers run a formal readiness gate: every power rail, actuator channel, and thermal margin must be backed by a measured number rather than a memory. Your Chapter 05 checklist plays the exact same role on the workbench—it is the line separating a drive stage you &lt;em&gt;hope&lt;/em&gt; works from one you can bolt onto a rolling robot without burning a driver IC.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Complete the sign-off table with measured or computed evidence, not with a retelling of the lesson titles.&lt;/li&gt;
  &lt;li&gt;Show a wiring photo where motor current and USB are separate supplies that share a ground.&lt;/li&gt;
  &lt;li&gt;State a pass condition you could explain out loud to a lab partner before you start Chapter 06 mechanics.&lt;/li&gt;
  &lt;li&gt;List only the parts you still lack (driver, TT motor, fuse) and the search you will use to buy them.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 05-01 through 05-06 are the source of every row. You need &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; open, the multimeter, and the photo you took when the wires were actually on the driver. If a hardware row was impossible, the row still needs the written calculation this checklist names. “I will do it later” is not a pass.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-electromechanical-verification-power-budgets-and-gate-discipline&quot;&gt;Key ideas: Electromechanical Verification, Power Budgets, and Gate Discipline&lt;/h2&gt;

&lt;p&gt;A checklist is not bureaucracy; it compresses three pillars of robotics engineering into a single auditable table:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Electromechanical Actuation (DC Motors &amp;amp; H-Bridges):&lt;/strong&gt; A brushed DC motor converts electrical power \(P_e = V I\) into mechanical power \(P_m = \tau \omega\) minus copper losses \(I^2 R_a\). At stall (\(\omega = 0\)), back-EMF vanishes and current spikes to \(I_{\text{stall}} = V / R_a\), which dictates your H-bridge peak-current rating and thermal margin.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Power Distribution &amp;amp; Decoupling:&lt;/strong&gt; Inductive loads inject voltage spikes and current dips onto the supply rail whenever PWM switches. Separating logic rails (3.3 V / 5 V) from motor rails with a common star ground and bulk capacitance prevents brownout resets on the microcontroller.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Systems Verification &amp;amp; Traceability:&lt;/strong&gt; In systems engineering, a &lt;em&gt;gate&lt;/em&gt; requires measured evidence (bench voltage, no-load current, thermal rise, polarity sign) logged with units and dates so downstream integration (Chapter 07 firmware and ROS 2) inherits verified constants instead of guesses.
    &lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capstone A is a two-wheel robot whose later ROS 2 base receives a twist and emits two wheel efforts. That software is only as honest as the signs you recorded here. A truth table copied from a classmate, a servo pulse you have never calculated, or a stall current you have never compared with your fuse, becomes a robot that yaws the wrong way, browns out, or drives through a timeout. The gate is deliberately boring. Chapter 06’s brackets and Chapter 07’s harness assume these answers already exist in your notebook.&lt;/p&gt;

&lt;h2 id=&quot;what-each-row-is-asking&quot;&gt;What each row is asking&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Driver identified.&lt;/strong&gt; Write the chip, not the color of the PCB: L298N, TB6612, or DRV8833. Add the one pin that bites people on that chip. For a TB6612 that sentence is “STBY must be high or the motors stay asleep.” For an L298N it is “the 5 V jumper comes off if VM is above 12 V, and the ESP32 is not fed from the onboard 7805.” For a DRV8833 it is the motor range, about 2.7–10.6 V, and the fact that a TT pair at stall is a lot to ask.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Truth table recorded.&lt;/strong&gt; Four rows, or the rows your chip actually documents, with IN1, IN2, PWM, and what the shaft did. Circle the forward triple Chapter 07 will call positive. If both inputs equal produced a brake on your board, say so. That row is not optional color.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Motor supply separated from USB.&lt;/strong&gt; The photo should show a battery or a bench supply on VM, USB on the microcontroller only, and a ground wire between them. A caption that says “common ground” without the wire in frame does not pass.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stall or no-load note.&lt;/strong&gt; A measured no-load current for one motor, plus either a one-second stall reading taken under a current limit or the computed watts from the seller’s stall current. The fuse sentence sits in this same note: the fuse value is above running current and is there to open on a short.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Servo pulse.&lt;/strong&gt; If you swept a horn, paste the three angles. If you had no servo, write&lt;/p&gt;

\[\frac{1.5~\mathrm{ms}}{20~\mathrm{ms}} = 7.5\%\]

&lt;p&gt;and one sentence: a 7.5% duty on a 1 kHz pin is a 75 µs pulse, so the command you will use is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeMicroseconds(1500)&lt;/code&gt; from a servo library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bang-bang behavior.&lt;/strong&gt; Three cases in your own numbers: a far reading, a reading under 20 cm, and a timeout or 0. All three named, with PWM 80 forward only on the valid far reading. Say that a future &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; does not override the stop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wiring photo.&lt;/strong&gt; One image, pins readable enough that a partner can find VM, ground, and the PWM wire.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Two failure modes.&lt;/strong&gt; These must be yours. Hitting them counts. Rehearsing them counts: unplugging the echo wire and watching the stop, or computing the USB stall current and writing “I will not power VM from USB” because 2.4 A is not 0.5 A. A copied list from this page, with no verb you performed, does not count.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A notebook claims: TB6612, STBY tied to 3.3 V, forward is IN1 = 1, IN2 = 0, PWM &amp;gt; 0, VM from 2S, USB only on the ESP32, no-load 0.18 A at 7.4 V, seller stall 1.2 A at 6 V. Check the stall arithmetic before you accept the row.&lt;/p&gt;

\[R \approx \frac{6}{1.2} = 5~\Omega\]

\[P = I^2 R = (1.2)^2 \times 5 = 7.2~\mathrm{W}\]

&lt;p&gt;The note “about 7 W if stalled at the rated 6 V” matches. Two motors would be about 2.4 A at that stall. A running guess of 0.2 A per motor plus 0.1 A of logic is 0.5 A, so a 2 A fuse in the battery positive lead sits above running current and still has a job during a dead short. Their servo row says \(1500/20000 = 0.075\). Their bang-bang row says 180 mm stops, 900 mm runs at PWM 80, timeout stops. They rehearsed a floating STBY (wheels dead until the wire was added) and a timeout that they had first treated as “far,” then fixed. That notebook passes. A notebook with the chip name and empty measurement cells does not.&lt;/p&gt;

&lt;h2 id=&quot;figures&quot;&gt;Figures&lt;/h2&gt;

&lt;p&gt;Use these three as the pictures you should be able to narrate while you sign the table.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;H-bridge path you traced when you wrote the truth table&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/stall_current.png&quot; alt=&quot;Stall versus no-load, the curve behind your current note&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sense_compute_act.png&quot; alt=&quot;Sense, compute, act, including the stop that a failed read must take&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;explanation-hand-verifying-a-driver-thermal--current-margin-gate&quot;&gt;Explanation: Hand-Verifying a Driver Thermal &amp;amp; Current Margin Gate&lt;/h3&gt;

&lt;p&gt;Suppose your checklist records a 6 V battery rail driving two identical brushed DC gearmotors through a dual H-bridge whose on-resistance per channel is \(R_{\text{DS(on)}} = 0.5\ \Omega\) (high-side plus low-side combined) and continuous current limit is \(1.2\ \text{A}\).&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Measure winding resistance and predict stall current:&lt;/strong&gt; With the motor disconnected, a multimeter reads \(R_a = 4.0\ \Omega\) across the terminals. Under a 6.0 V rail, the worst-case stall current is:
\(I_{\text{stall}} = \frac{V}{R_a + R_{\text{DS(on)}}} = \frac{6.0}{4.0 + 0.5} = 1.33\ \text{A}.\)
Notice this already exceeds the \(1.2\ \text{A}\) continuous rating—meaning a brief stall is survivable only if your firmware limits duty cycle or cuts power on stall.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check steady-state thermal dissipation at load:&lt;/strong&gt; During normal driving, you measure \(I_{\text{load}} = 0.60\ \text{A}\) per channel. Each channel dissipates:
\(P_{\text{ch}} = I_{\text{load}}^2 R_{\text{DS(on)}} = (0.60)^2 (0.5) = 0.18\ \text{W}.\)
With both channels running, total IC heat is \(0.36\ \text{W}\). If the package thermal resistance is \(R_{\theta JA} = 60\ ^\circ\text{C/W}\) in a \(25\ ^\circ\text{C}\) room, the junction temperature rises to \(25 + 0.36 \times 60 = 46.6\ ^\circ\text{C}\)—completely safe.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify the gate verdict:&lt;/strong&gt; At \(1.33\ \text{A}\) stall on both channels simultaneously, heat jumps to \(2 \times (1.33)^2 (0.5) = 1.77\ \text{W}\) (\(\Delta T = 106\ ^\circ\text{C}\), pushing the chip past \(131\ ^\circ\text{C}\)). Writing this exact calculation in your lab notebook tells you &lt;em&gt;why&lt;/em&gt; a stall must never be held—turning a checklist row into quantitative engineering protection.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;This sign-off does not ask you to repeat every experiment at full power. If a row is missing because a stall would have been unsafe, compute it and say why you refused the stall. Do not plug VM into USB “just for the photo.” Wheels stay up if you re-run a truth-table row. Hand on the power switch if you re-run the floor stop.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; and the wiring photo&lt;/td&gt;
      &lt;td&gt;The evidence&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Multimeter&lt;/td&gt;
      &lt;td&gt;Only if a voltage cell is still blank&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;The driver, one motor, the range sensor&lt;/td&gt;
      &lt;td&gt;Only to fill a row you skipped&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Fuse and holder, if none is in the kit&lt;/td&gt;
      &lt;td&gt;The Chapter 07 part you can buy now&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Copy the sign-off table into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Fill each cell from notes you already have. Where a cell is empty, do the smallest measurement or calculation that makes it true.&lt;/li&gt;
  &lt;li&gt;Attach or link the wiring photo. Caption it with the chip name and “VM source: …”.&lt;/li&gt;
  &lt;li&gt;Read the pass paragraph below out loud. If you hesitate on a row, that row is not done.&lt;/li&gt;
  &lt;li&gt;Write the buy list only for gaps: driver, TT motor, fuse. Skip parts you already own.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A completed table and a one-paragraph pass statement in your own words. Your partner, shown only the photo and the table, can point to the forward motor state and to the sensor condition that forces PWM 0.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Every cell says “yes” with no number&lt;/td&gt;
      &lt;td&gt;Replace “yes” with a voltage, a current, a pulse width, or a distance.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Truth table has no brake/coast row&lt;/td&gt;
      &lt;td&gt;Go back to lesson 05-01 and record the both-inputs-equal state on your chip.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Photo shows one USB cable and no battery&lt;/td&gt;
      &lt;td&gt;Motor supply is not separated. Retake the photo with VM on the pack.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Servo row blank because you had no horn&lt;/td&gt;
      &lt;td&gt;Write the 7.5% calculation and the 75 µs contrast at 1 kHz.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Failure modes are the textbook list&lt;/td&gt;
      &lt;td&gt;Name two you caused or rehearsed, including what the shaft did.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;sign-off-table&quot;&gt;Sign-off table&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Row&lt;/th&gt;
      &lt;th&gt;Your evidence&lt;/th&gt;
      &lt;th&gt;Done&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Driver chip and the pin that bites (STBY, 7805 jumper, or VM range)&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Truth table: IN1, IN2, PWM, direction, including coast or brake&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Motor supply separated from USB, common ground (photo)&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;No-load current, and stall amps or computed stall watts&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Servo: angles, or \(1.5/20 = 7.5\%\) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeMicroseconds(1500)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bang-bang: far, under 20 cm, timeout/0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wiring photo filed&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Two failure modes you hit or rehearsed&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Pass means&lt;/strong&gt; every row above is filled, the photo matches the supply story, and you can say what the shaft does when the range sensor times out. With that, you may start Chapter 06 mechanics and you are ready for Chapter 07 to trust your forward sign, your fuse, and your habit of setting PWM to 0 before anything else.&lt;/p&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy only the gap. The usual gaps at this gate are a driver (TB6612 preferred, L298N acceptable), one or two TT gearmotors, and a 2–3 A fuse with a holder in the battery positive lead. Prices move. Rough bands: L298N module about 35.000–70.000 VND, TB6612 module about 40.000–120.000 VND, TT motor about 25.000–45.000 VND, fuse and holder about 5.000–25.000 VND.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified L298N page, about 45.000 VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=motor%20TT&quot;&gt;motor TT&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=L298N&quot;&gt;L298N&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Which single cell, if left as the word “yes,” fails the gate even when the other cells are numeric?&lt;/li&gt;
  &lt;li&gt;Your stall note says 1.2 A at 6 V. Compute \(R\) and \(P\) and say whether “about 7 W” would pass.&lt;/li&gt;
  &lt;li&gt;You had no servo. Write the exact calculation and the function call that belong in that row.&lt;/li&gt;
  &lt;li&gt;Name the three bang-bang cases the row must contain.&lt;/li&gt;
  &lt;li&gt;Chapter 06 starts when which spoken sentence is true? Include the timeout behavior.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;p&gt;A cell that says only “yes” fails that row; the gate wants a number, a pin name, or a photo. \(R \approx 5~\Omega\) and \(P = 7.2~\mathrm{W}\), so “about 7 W” passes the arithmetic. With no servo, write \(1.5/20 = 7.5\%\) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeMicroseconds(1500)&lt;/code&gt;. The three cases are a valid far reading (PWM 80 forward), a reading under 20 cm (PWM 0), and a timeout or 0 (PWM 0). You may start Chapter 06 when you can say that a timed-out range sensor forces the motors off, and the rest of the table is filled with your own evidence.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;ST L298 datasheet: &lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;https://www.st.com/resource/en/datasheet/l298.pdf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Pololu TB6612FNG: &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;https://www.pololu.com/product/713&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;SparkFun motor selection (stall versus no-load): &lt;a href=&quot;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&quot;&gt;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;ROS 2 topics, the later home of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt;: &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html&quot;&gt;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab: closed-loop intro sense → compute → act</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter05/05_06_closed_loop_intro/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter05/05_06_closed_loop_intro</id>
   <content type="html">&lt;p&gt;In 1788, James Watt bolted two spinning brass balls onto a steam engine — the centrifugal flyball governor. When the engine sped up, the balls swung outward and pinched the steam valve shut; when it slowed down under load, they dropped and opened the valve wider. Without a single line of code, Watt had harnessed the most powerful idea in all of engineering: &lt;strong&gt;negative feedback&lt;/strong&gt;. On your robot, open-loop PWM (“set duty = 50%”) fails the instant the battery drains or the floor shifts from tile to carpet; &lt;strong&gt;closed-loop control&lt;/strong&gt; (&lt;em&gt;sense → compute → act&lt;/em&gt;) measures actual speed every 10 milliseconds and automatically compensates.&lt;/p&gt;

&lt;p&gt;This culminating lab of Chapter 05 brings every chapter together — electronics, MCU timers, encoders, H-bridges, and feedback control — into one living closed-loop wheel controller.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Every later controller on this robot is the same three boxes: sense, compute, act. This lab closes them with the rudest computer that still counts: if the range is inside 20 cm, the wheel PWM is zero; otherwise a modest forward PWM is allowed. That law is bang-bang. PID arrives in Chapter 08. What you practice now is the safety shape those later laws must keep: a failed sensor means stop.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Draw the sense, compute, and act loop for a range sensor and one motor command.&lt;/li&gt;
  &lt;li&gt;Implement a bang-bang rule at 20 cm, with a timeout that forces PWM to zero.&lt;/li&gt;
  &lt;li&gt;Add a simple hysteresis band so the motor does not chatter at the threshold.&lt;/li&gt;
  &lt;li&gt;Say where a ROS 2 subscriber of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; will sit in this diagram, and which stop must remain in firmware.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You need a range reading from the ultrasonic or time-of-flight lesson in Chapter 04, in millimetres or in centimetres you can convert. You need one motor channel whose forward truth table you wrote in lesson 05-01, on a driver you can name from lesson 05-02. Wheels come off the ground before they touch the floor. A cardboard target and a power switch you can hit with your hand are part of the apparatus.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-open-loop-vs-closed-loop-control-proportionalintegral-action-and-anti-windup&quot;&gt;Key ideas: Open-Loop vs. Closed-Loop Control, Proportional–Integral Action, and Anti-Windup&lt;/h2&gt;

&lt;p&gt;Four control-theory pillars turn a spinning motor into a precision velocity actuator:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Error-Driven Feedback (\(e_k = r_k - y_k\)):&lt;/strong&gt; At each discrete tick \(k\) (period \(\Delta t\)), the controller compares desired wheel velocity \(r_k\) against encoder-measured velocity \(y_k\) and computes command \(u_k\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Proportional (P) vs. Integral (I) Action:&lt;/strong&gt; Pure proportional control \(u_k = K_p e_k\) responds instantly to error, yet &lt;strong&gt;always leaves steady-state error&lt;/strong&gt; against constant friction or gravity (if \(e = 0\), \(u = 0\), so the wheel stops!). Adding an integral term \(I_k = I_{k-1} + K_i e_k \Delta t\) accumulates persistent error until steady-state error reaches &lt;strong&gt;exact zero&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Feedforward + Feedback:&lt;/strong&gt; Combining a rough open-loop model \(u_{\text{ff}} = k_v r_k\) with PI feedback lets the feedback loop handle only &lt;em&gt;disturbances&lt;/em&gt; (battery droop, carpet friction), cutting rise time in half.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Actuator Saturation &amp;amp; Integrator Anti-Windup:&lt;/strong&gt; PWM is bounded to \([-u_{\max}, +u_{\max}]\). If a wheel jams while the integrator keeps summing error blindly, the integral term grows enormous (&lt;em&gt;windup&lt;/em&gt;) and causes violent overshoot once released. Clamping the integrator whenever output saturates eliminates windup cleanly.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Capstone A will drive toward things and, more importantly, stop before it hits them. Chapter 07 assembles two signed PWMs. Chapter 08 replaces this on/off compute block with a PID that can hold a speed or a heading. The sense and act blocks stay. When ROS 2 is introduced, a subscriber to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; is another way to fill the compute block: some other process proposes a twist, and your node turns it into left and right PWM. The range stop does not move out into that topic. Firmware still owns the last look at the sensor. If the topic says “forward” and the range says “too close” or “I have no idea,” the wheels stop. A planner that goes silent must not be interpreted as a clear hallway.&lt;/p&gt;

&lt;h2 id=&quot;bang-bang-in-one-sentence&quot;&gt;Bang-bang, in one sentence&lt;/h2&gt;

&lt;p&gt;Read the range. If it is under 20 cm, command PWM 0. If it is clearly farther, command PWM 80 in the forward direction from your truth table. Eighty on an 8-bit scale is a slow creep, which is what you want while a hand is still the target.&lt;/p&gt;

\[\mathrm{PWM} =
\begin{cases}
0 &amp;amp; \text{if range} &amp;lt; 20~\mathrm{cm} \\
80 &amp;amp; \text{otherwise, when the reading is valid}
\end{cases}\]

&lt;p&gt;The “otherwise” is the dangerous word. A sensor that times out, returns a maximum sentinel, or returns 0 because the echo never came, is not a valid “otherwise.” Those readings take the stop branch. A failed sensor must not mean “the path is clear, full speed.”&lt;/p&gt;

&lt;p&gt;Hysteresis keeps the shaft from buzzing when you hold a hand near 20 cm. Stop when the range falls below 20 cm. Stay stopped until the range rises above 25 cm. Between those two numbers, keep the previous command. The motor then switches a little less often, and the gearbox stops clicking at the boundary.&lt;/p&gt;

&lt;h2 id=&quot;where-the-three-boxes-are-in-the-code&quot;&gt;Where the three boxes are in the code&lt;/h2&gt;

&lt;p&gt;Sense is the function you already trust from Chapter 04, wrapped so that a timeout comes back as a negative number or an explicit invalid flag. Compute is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; below. Act is the pair of direction pins plus one PWM write. Put the pin numbers in named constants at the top so the lab write-up can say which GPIO moved.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_IN1&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// edit to your forward pair&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_IN2&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_PWM&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_TRIG&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// ultrasonic; ignore for ToF&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN_ECHO&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_FORWARD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STOP_MM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// 20 cm&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GO_MM&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// 25 cm, hysteresis release&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_IN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_IN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_IN1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// forward, from YOUR truth table&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_IN2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;analogWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;// safe act, before any sensor read&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;readRangeMm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;// Chapter 04; negative means timeout / invalid&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STOP_MM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;analogWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GO_MM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;analogWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN_PWM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_FORWARD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// between 200 and 250 mm, hold the previous PWM&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readRangeMm&lt;/code&gt; is the function you adapt from your ultrasonic or ToF sketch. On a timeout it returns a negative value. A literal 0 also stops, because \(0 \le 200\). Direction is set once, from the truth table, so this first loop only has to decide speed. When you later mix in a reverse escape, you will change IN1 and IN2 on purpose, in the act block, with the same “invalid means stop” rule still above them.&lt;/p&gt;

&lt;p&gt;Wheels-up procedure: hold your hand in front of the sensor. Far hand, the wheel spins forward slowly. Hand inside about 20 cm, the wheel stops. Cover the sensor or unplug the echo wire for a moment and confirm the wheel stops, because the read failed. Only after that do you put the robot on the floor, point it at cardboard, and keep your hand on a switch that removes motor power. The floor run is short. You are checking the stop, not racing the hallway.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Thresholds: stop at 200 mm, go at 250 mm and above.&lt;/p&gt;

&lt;p&gt;A reading of 180 mm is below 200 mm, so PWM becomes 0. A reading of 900 mm is a clear hallway, so PWM becomes 80 forward. A reading of 0, or a timeout returned as −1, takes the stop branch even though 0 is “less distance” and a timeout is “no distance.” The compute block refuses to treat either as permission to move.&lt;/p&gt;

&lt;p&gt;Walk the hysteresis. The hand approaches: 400 mm (forward), 260 mm (still forward, because 260 is above 250), 220 mm (hold forward, inside the band), 180 mm (stop). The hand backs away: 220 mm (stay stopped, inside the band), 260 mm (forward again). Without the band, 190 mm and 210 mm of sensor noise would click the gearbox on every loop.&lt;/p&gt;

&lt;h2 id=&quot;figures&quot;&gt;Figures&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sense_compute_act.png&quot; alt=&quot;Sense, compute, act: the range reading enters, a decision is made, a motor command leaves&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Label the boxes with your own nouns: ultrasonic or ToF, the bang-bang &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;, and the H-bridge PWM. Draw a second arrow into compute and write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; on it, then draw the stop still sitting in firmware after that arrow.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;First session: chassis on a stand, wheels in the air, current limit or fuse in place, USB on the microcontroller only. Second session: short floor run toward cardboard, one person, hand on a switch that cuts motor power. Keep PWM at 80, not 255. If the sensor stream stops, the wheel must already be stopped by your timeout branch. Do not “fix” a timeout by commenting that branch out.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Microcontroller plus the range sensor from Chapter 04&lt;/td&gt;
      &lt;td&gt;Sense&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;One motor and the identified driver&lt;/td&gt;
      &lt;td&gt;Act&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Battery or limited supply for VM, common ground&lt;/td&gt;
      &lt;td&gt;Motor power&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cardboard target&lt;/td&gt;
      &lt;td&gt;A surface the sensor can see&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Power switch you can reach&lt;/td&gt;
      &lt;td&gt;The human fuse&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Notebook&lt;/td&gt;
      &lt;td&gt;The three test readings and the timeout test&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Confirm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readRangeMm&lt;/code&gt; against a ruler at about 20 cm and about 50 cm before the motor is enabled. Fix the sensor first.&lt;/li&gt;
  &lt;li&gt;Enter the constants and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt; above. Set direction from your truth table. PWM stays 0 until the first valid far reading.&lt;/li&gt;
  &lt;li&gt;Wheels up. Present a hand at roughly 40 cm, then 15 cm, then cover the sensor. Record PWM behavior for each.&lt;/li&gt;
  &lt;li&gt;Optional floor run: cardboard at the end of a short lane, hand on the switch, PWM 80. The robot should creep and stop near 20 cm. If it does not stop, cut power. Do not raise PWM.&lt;/li&gt;
  &lt;li&gt;Write the hysteresis story in two lines: the distance where it stopped, and the distance where it started again.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A note with three cases (far → forward, near → stop, timeout or 0 → stop) and, if you drove on the floor, the approximate stopping distance. A sentence that names &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; as a future compute input that does not get to override this stop.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheel runs when the sensor is unplugged&lt;/td&gt;
      &lt;td&gt;Timeout is falling through to the forward branch. Return a negative code and test &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mm &amp;lt; 0&lt;/code&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheel chatters at the boundary&lt;/td&gt;
      &lt;td&gt;The go and stop thresholds are equal. Separate them (200 mm and 250 mm).&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheel runs backward&lt;/td&gt;
      &lt;td&gt;IN1 and IN2 do not match the forward row of your truth table.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot never starts&lt;/td&gt;
      &lt;td&gt;Every read is inside the stop band, the target is too close, or STBY / enable is low.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Floor run does not stop&lt;/td&gt;
      &lt;td&gt;Wrong units (cm compared with 200), or the sensor is aimed over the cardboard. Cut power.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-hand-stepping-3-ticks-of-a-discrete-pi-wheel-velocity-controller&quot;&gt;Explanation: Hand-Stepping 3 Ticks of a Discrete PI Wheel Velocity Controller&lt;/h2&gt;

&lt;p&gt;Before tuning gains on your robot, trace three consecutive 100 Hz control ticks (\(\Delta t = 0.01\text{ s}\)) of a discrete &lt;strong&gt;PI velocity controller&lt;/strong&gt; by hand:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Write the discrete PI update equations:&lt;/strong&gt;
\(e_k = r_k - y_k, \qquad I_k = \operatorname{clamp}\bigl(I_{k-1} + K_i e_k \Delta t,\; -u_{\max}, +u_{\max}\bigr), \qquad u_k = \operatorname{clamp}\bigl(K_p e_k + I_k,\; -u_{\max}, +u_{\max}\bigr).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Initialize state cleanly:&lt;/strong&gt; \(I_0 = 0\), command range \(u \in [-1.0, +1.0]\) (normalized PWM duty).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Update error, integral, and PWM at each tick&lt;/strong&gt; using actual encoder feedback \(y_k\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Watch the integral term carry the friction load&lt;/strong&gt; as error shrinks toward zero!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-3-control-ticks-with-k_p--12text-sm-k_i--80text-m-1-target-r--040text-ms&quot;&gt;Concrete Worked Example: 3 Control Ticks with \(K_p = 1.2\text{ s/m}\), \(K_i = 8.0\text{ m}^{-1}\), Target \(r = 0.40\text{ m/s}\)&lt;/h3&gt;

&lt;p&gt;Suppose the robot commands \(r = 0.40\text{ m/s}\) from rest (\(\Delta t = 0.01\text{ s}\), so \(K_i \Delta t = 0.08\), \(I_0 = 0\)), and the encoder measures \(y_1 = 0.00\), \(y_2 = 0.15\), \(y_3 = 0.30\text{ m/s}\):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tick \(k\)&lt;/th&gt;
      &lt;th&gt;Meas \(y_k\) (m/s)&lt;/th&gt;
      &lt;th&gt;Error \(e_k = 0.40 - y_k\)&lt;/th&gt;
      &lt;th&gt;P-term \(1.2 e_k\)&lt;/th&gt;
      &lt;th&gt;Integrator \(I_k = I_{k-1} + 0.08 e_k\)&lt;/th&gt;
      &lt;th&gt;Output \(u_k = P + I_k\) (PWM %)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;\(1\)&lt;/td&gt;
      &lt;td&gt;\(0.00\)&lt;/td&gt;
      &lt;td&gt;\(+0.40\)&lt;/td&gt;
      &lt;td&gt;\(+0.480\)&lt;/td&gt;
      &lt;td&gt;\(0 + 0.032 = 0.032\)&lt;/td&gt;
      &lt;td&gt;\(0.512\) (51.2%)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;\(2\)&lt;/td&gt;
      &lt;td&gt;\(0.15\)&lt;/td&gt;
      &lt;td&gt;\(+0.25\)&lt;/td&gt;
      &lt;td&gt;\(+0.300\)&lt;/td&gt;
      &lt;td&gt;\(0.032 + 0.020 = 0.052\)&lt;/td&gt;
      &lt;td&gt;\(0.352\) (35.2%)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;\(3\)&lt;/td&gt;
      &lt;td&gt;\(0.30\)&lt;/td&gt;
      &lt;td&gt;\(+0.10\)&lt;/td&gt;
      &lt;td&gt;\(+0.120\)&lt;/td&gt;
      &lt;td&gt;\(0.052 + 0.008 = 0.060\)&lt;/td&gt;
      &lt;td&gt;\(0.180\) (18.0%)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Look at what happens as \(y_k \to 0.40\text{ m/s}\): the proportional term smoothly tapers to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, while the integral term \(I_k\) climbs and settles at the exact steady-state PWM needed to overcome motor friction and battery level — locking the wheel onto \(0.40\text{ m/s}\) automatically!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Using &lt;strong&gt;positive feedback&lt;/strong&gt; by accident (wiring the encoder backward or writing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e = y - r&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e = r - y&lt;/code&gt;) — instead of braking when too fast, the controller slams PWM to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100%&lt;/code&gt; and runs away uncontrollably! Always test with wheels lifted off the desk first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If steady-state cruise at \(0.40\text{ m/s}\) requires \(u = 0.36\) PWM and you run &lt;em&gt;pure P-control&lt;/em&gt; (\(K_i = 0, K_p = 1.2\)), the wheel can never reach \(0.40\text{ m/s}\) — it stalls at \(e_{\text{ss}} = 0.36 / 1.2 = 0.30\text{ m/s}\) error (only \(y = 0.10\text{ m/s}\)!). That single division proves why integral action or feedforward is essential.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;This lab uses the sensor and the driver you already have. Buy only the gap: a TB6612 or L298N if lesson 05-02 is still short a board, a TT motor if you have no shaft to watch, an HC-SR04 or a small ToF if Chapter 04 was done on a classmate’s bench. Prices move. Rough bands: HC-SR04 about 15.000–35.000 VND, TT motor about 25.000–45.000 VND, L298N module about 35.000–70.000 VND.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified L298N page, about 45.000 VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=HC-SR04&quot;&gt;HC-SR04&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=HC-SR04&quot;&gt;HC-SR04&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=HC-SR04&quot;&gt;HC-SR04&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=VL53L0X&quot;&gt;VL53L0X&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=HC-SR04&quot;&gt;HC-SR04&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Range reads 180 mm, then 900 mm, then a timeout. What PWM do you command in each case if stop is 200 mm and go is 250 mm?&lt;/li&gt;
  &lt;li&gt;Why does a reading of 0 take the stop branch?&lt;/li&gt;
  &lt;li&gt;The wheel clicks on and off with the hand held still near 20 cm. Which two constants do you separate?&lt;/li&gt;
  &lt;li&gt;A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; message says forward at the same moment the range is 12 cm. What does firmware do?&lt;/li&gt;
  &lt;li&gt;PID is Chapter 08. What is the compute block you ship today, in one sentence?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;p&gt;180 mm stops, 900 mm commands PWM 80 forward, and a timeout stops. A 0 reading is invalid or inside the stop threshold, so it must not count as a clear path. Separate the stop and go thresholds, for example 200 mm and 250 mm, and hold the previous command inside the band. Firmware stops on the 12 cm reading even if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; says forward. Today’s compute block is bang-bang: PWM 0 when the range is under 20 cm or the sensor has failed, and PWM 80 forward when the range is valid and beyond the hysteresis release.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Control Guru, on/off control and how the output responds when the measurement crosses a setpoint: &lt;a href=&quot;https://controlguru.com/the-on-off-controller-output-response-to-set-point-changes/&quot;&gt;https://controlguru.com/the-on-off-controller-output-response-to-set-point-changes/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;ROS 2 topics, the pipe a later &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cmd_vel&lt;/code&gt; subscriber will listen on. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt; itself is introduced in Chapter 09: &lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html&quot;&gt;https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Current limits, stalls, and thermal care</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter05/05_05_current_thermal/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter05/05_05_current_thermal</id>
   <content type="html">&lt;p&gt;A DC motor does not fail because it spun too fast — it fails because its copper windings got too hot and melted their thin enamel insulation. When a robot wheel jams against a chair leg, speed drops to zero, back-EMF vanishes, and electrical input power turns &lt;strong&gt;100% into heat&lt;/strong&gt; inside both the motor coil and the H-bridge MOSFETs. Within ten seconds, a tiny plastic gearmotor can smell like burnt varnish.&lt;/p&gt;

&lt;p&gt;This lesson turns thermal safety into concrete engineering numbers: stall-current budgets, software stall detection via encoder velocity, and thermal time constants.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;A motor datasheet has two currents that matter on a small robot, and they are not close to each other. No-load current is what you measure with the wheels in the air. Stall current is what the copper will draw when the shaft stops and the back-EMF collapses. This lesson turns that stall number into watts, into a driver heatsink, into a fuse in the battery lead, and into the brownout that makes a robot run away.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Distinguish no-load current from stall current, and use stall current to size the driver and the fuse.&lt;/li&gt;
  &lt;li&gt;From a rated voltage and a stall current, estimate winding resistance and the watts dissipated in a stalled motor.&lt;/li&gt;
  &lt;li&gt;Estimate heat in an L298N from its voltage drop, and say why a TB6612 stays cooler at the same current.&lt;/li&gt;
  &lt;li&gt;Place a fuse in the battery positive lead, and treat a supply sag that resets the microcontroller as a control bug.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Ohm’s law and \(P = I^2 R\) are the whole mathematical toolkit. Lesson 05-01’s back-EMF story is the reason stall current is large. Lesson 05-02’s driver identification tells you whether your heat is landing in a Darlington with a bolted heatsink or in a MOSFET bridge. You need a bench supply with a current knob, or a series ammeter, if you will measure a real motor. The arithmetic path is available if you cannot stall safely.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-stall-current-physics-thermal-time-constants-and-software-currentstall-guards&quot;&gt;Key ideas: Stall-Current Physics, Thermal Time Constants, and Software Current/Stall Guards&lt;/h2&gt;

&lt;p&gt;Three physical and firmware safeguards protect actuators from burning out:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Joule Heating Scales as \(I^2 R\):&lt;/strong&gt; At stall, current jumps 4×–8× above cruise current, which means copper heating rate jumps &lt;strong&gt;16× to 64×&lt;/strong&gt;! Even if a motor runs cool forever at 0.3 A cruise, 1.5 A stall raises winding temperature rapidly.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;First-Order Thermal RC Model:&lt;/strong&gt; A motor or driver heats exponentially toward steady-state temperature \(T_{\infty} = T_{\text{amb}} + P_{\text{heat}} R_{\theta}\) with thermal time constant \(\tau_{\text{th}} = R_{\theta} C_{\text{th}}\):
\(T(t) = T_{\text{amb}} + P_{\text{heat}} R_{\theta}\bigl(1 - e^{-t/\tau_{\text{th}}}\bigr).\)
Brief 0.3-second acceleration surges are harmless because thermal mass \(C_{\text{th}}\) absorbs them; sustained 10-second stalls are lethal.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Firmware Stall Detection (Free Safety Sensor!):&lt;/strong&gt; Even without a hardware INA219 current sensor, if your firmware commands &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|PWM| &amp;gt; 60%&lt;/code&gt; for more than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;500 ms&lt;/code&gt; while encoder velocity stays &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|v| &amp;lt; 0.02 m/s&lt;/code&gt;, the wheel is unmistakably stalled — immediately cut PWM and enter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;!r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Chapter 07 puts a battery and a fuse on the chassis because a USB port cannot feed two stalled TT motors. A weak 18650 pair also droops when both wheels start together. If the microcontroller resets, the GPIO pins go back through boot, and a half-initialized H-bridge can drive a wheel before your code reaches the line that sets PWM to zero. Brownout is a control bug, not only a power bug. ROS 2 will later stream &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; as if the base were awake. Firmware still has to fail safe when the supply sags or the sensor task stops: motors off, and stay off, until a fresh setup has run. The numbers in this lesson are how you choose a fuse that survives normal driving and still opens when a wire shorts.&lt;/p&gt;

&lt;h2 id=&quot;two-currents-on-one-curve&quot;&gt;Two currents on one curve&lt;/h2&gt;

&lt;p&gt;The figure for this lesson sketches current against speed. At no load the shaft spins fast, back-EMF cancels most of the supply, and current is small: often a few hundred milliamps or less for a yellow TT motor, sometimes only tens of milliamps for a bare can. At stall, speed is zero, back-EMF is zero, and&lt;/p&gt;

\[I_{stall} \approx \frac{V}{R}\]

&lt;p&gt;The datasheet stall current, at the rated voltage, is the number you show the driver and the fuse. Running current on a smooth floor sits between the two, closer to no-load until you climb, push, or hit a carpet edge. A gearbox binding against a chair leg is electrically a stall even though your PWM still says “forward.”&lt;/p&gt;

&lt;h2 id=&quot;watts-in-a-thumb-sized-can&quot;&gt;Watts in a thumb-sized can&lt;/h2&gt;

&lt;p&gt;Those watts leave as heat in the winding. A stalled motor has almost no mechanical output, so nearly all of \(I \times V\) heats the copper and the brushes. A second or two of measurement is a data point. Holding the shaft “to feel the torque” for half a minute cooks the enamel. Two motors stalled together draw about twice one motor’s stall current, before wiring losses. A USB port limited near 0.5 A cannot supply that. The port folds, the microcontroller resets, and you learn the brownout with the wheels still on the desk if you are lucky.&lt;/p&gt;

&lt;h2 id=&quot;heat-in-the-driver-and-sag-in-the-pack&quot;&gt;Heat in the driver and sag in the pack&lt;/h2&gt;

&lt;p&gt;An L298N drops about 2 V at 1 A. The chip, not the motor, dissipates about&lt;/p&gt;

\[P \approx 2~\mathrm{V} \times 1~\mathrm{A} = 2~\mathrm{W}\]

&lt;p&gt;in that channel. Two channels working hard can double it. The bolted heatsink is required equipment, and the module still needs air. A TB6612 drops far less at the same current, so the board stays cooler, and it still has a thermal limit and a current limit. Cooler is not infinite. If the TB6612 package becomes painful, the motor is stalled or the continuous rating (about 1.2 A per channel) has been ignored.&lt;/p&gt;

&lt;p&gt;A tired 18650 pair has internal resistance. When both motors start, VM sags. The buck that feeds the microcontroller sags with it if they share the pack. The ESP32 brownout detector resets the chip. During reset the motor pins are not yet your safe values. Firmware that sets PWM to 0 as the first act in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt;, and that refuses to re-enable the bridge until a known delay and a known input state, turns a brownout into a stop. Firmware that leaves enable floating turns a brownout into a short drive across the room.&lt;/p&gt;

&lt;h2 id=&quot;the-fuse-is-a-conversation-with-the-short&quot;&gt;The fuse is a conversation with the short&lt;/h2&gt;

&lt;p&gt;Put the fuse in the battery positive lead, close to the pack, so a short anywhere downstream has to pass through it. A 2–3 A fuse on a small 2S pack is a starting conversation, not a law. It has to sit above the normal running current of both motors plus logic, and it has to open on a dead short. If each TT motor runs near 0.3 A and logic is near 0.2 A, a 2 A fuse has margin for driving and will still discuss a hard stall or a pinched wire. A polyfuse is acceptable: it warms, opens, and recovers after it cools. Give it that cooling time. Do not wrap it in tape inside a hot enclosure and then expect the hold current printed on the bag.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A seller rates a motor at 6 V with a stall current of 1.2 A. The winding resistance is about&lt;/p&gt;

\[R \approx \frac{6}{1.2} = 5~\Omega\]

&lt;p&gt;At stall on that rated voltage the copper dissipates&lt;/p&gt;

\[P = I^2 R = (1.2)^2 \times 5 = 1.44 \times 5 = 7.2~\mathrm{W}\]

&lt;p&gt;You can check the same result as \(P = I \times V = 1.2 \times 6 = 7.2~\mathrm{W}\). Seven watts in a motor the size of your thumb is why the stall test lasts about one second and then you release. Two such motors stalled together are about 2.4 A before wiring losses. A USB port at 0.5 A will brown out or reset the board. That is why Chapter 07 uses a battery and a fuse.&lt;/p&gt;

&lt;p&gt;If your meter later shows 0.15 A no-load at a modest PWM, keep both numbers. The gap between 0.15 A and 1.2 A is the headroom the fuse and the driver must survive when the wheel stops against a wall.&lt;/p&gt;

&lt;h2 id=&quot;figures&quot;&gt;Figures&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/stall_current.png&quot; alt=&quot;Stall current versus speed: small current while spinning, large current when the shaft is stopped&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Read the left side of the curve as the datasheet stall number and the right side as the no-load current you are allowed to measure for more than a second.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Digital_Multimeter_Aka.jpg&quot; alt=&quot;Multimeter in series or on the supply&apos;s current display, used for one motor with the wheel in the air&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you insert the meter, use the high-current jack, start at PWM 0, and do not stall the motor through a meter fuse you cannot afford to replace. A bench supply’s own current readout is the calmer instrument.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Wheels up, off the chassis, one motor only. A one-second stall is allowed only when the supply current limit is set near 2 A so a wiring mistake cannot become a fire. If you do not have a limit you trust, do not stall: use the seller’s stall current and compute the watts. Do not hold a spinning pinion. Do not power the motor from USB. Common ground if a microcontroller is producing the PWM.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;One brushed motor&lt;/td&gt;
      &lt;td&gt;The device under test&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bench supply with a current limit, or a battery plus a series ammeter&lt;/td&gt;
      &lt;td&gt;The source and the reading&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;H-bridge or direct supply leads&lt;/td&gt;
      &lt;td&gt;Apply a known voltage&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Multimeter&lt;/td&gt;
      &lt;td&gt;Volts, and amps if that is your method&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Notebook&lt;/td&gt;
      &lt;td&gt;No-load amps, and stall amps or computed watts&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Set the supply current limit near 2 A if you have one. Set voltage to the motor’s rated voltage or to a modest value you record.&lt;/li&gt;
  &lt;li&gt;Run the motor unloaded at a modest PWM (or straight from the supply). Record voltage and current after one second. That is no-load at that voltage.&lt;/li&gt;
  &lt;li&gt;Optional: stall the shaft for about one second only, read current, release. If the limit folds immediately, write “supply limited” and stop. Do not repeat.&lt;/li&gt;
  &lt;li&gt;If you skipped the stall, copy the seller’s stall current, compute \(R \approx V/I_{stall}\) and \(P = I^2 R\), and label them as datasheet estimates.&lt;/li&gt;
  &lt;li&gt;Write one sentence on the fuse you would put in the battery positive lead for two of these motors, and why it is above running current.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;Two numbers in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;: a measured no-load current, and either a measured one-second stall current or a computed stall power from the seller’s figure. A fuse sentence with a current (2 A or 3 A is a reasonable start for a small 2S TT robot) and the running-current estimate that sits under it.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Current reads almost 0 and the shaft spins&lt;/td&gt;
      &lt;td&gt;The meter is in parallel with the motor, or it is on the voltage jack while you think you are measuring amps.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Supply voltage collapses and the MCU resets&lt;/td&gt;
      &lt;td&gt;USB or a tiny pack is feeding a stall. Move to a battery and a limit, wheels up.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;L298N heatsink is too hot to touch after a short run&lt;/td&gt;
      &lt;td&gt;Drop times current is a couple of watts. Shorter stall, more air, or a TB6612.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Polyfuse opens and stays open for minutes&lt;/td&gt;
      &lt;td&gt;It is doing its job. Let it cool. Do not raise the trip point until the short is gone.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot lurches after a reset&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt; is not forcing PWM to 0 and a known direction before anything else.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-computing-thermal-rise-during-a-jammed-wheel--sizing-a-stall-watchdog&quot;&gt;Explanation: Computing Thermal Rise During a Jammed Wheel &amp;amp; Sizing a Stall Watchdog&lt;/h2&gt;

&lt;p&gt;Let’s model how fast a stalled motor heats up and verify how quickly a firmware stall guard saves it:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compute stall power:&lt;/strong&gt; \(P_{\text{stall}} = I_{\text{stall}}^2 R_a = V \cdot I_{\text{stall}}\) (at zero speed, 100% of electrical power becomes heat!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check steady-state stalled temperature:&lt;/strong&gt; \(T_\infty = T_{\text{amb}} + P_{\text{stall}} R_\theta\). If \(T_\infty &amp;gt; 110^\circ\text{C}\), the motor &lt;em&gt;cannot&lt;/em&gt; survive continuous stall.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Evaluate temperature after \(t\) seconds:&lt;/strong&gt; \(T(t) = T_{\text{amb}} + (T_\infty - T_{\text{amb}})(1 - e^{-t/\tau_{\text{th}}})\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Set a software stall cutoff \(t_{\text{cut}} \ll \tau_{\text{th}}\)&lt;/strong&gt; (typically &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.5–1.0 s&lt;/code&gt;) so temperature barely budges before power is cut.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-small-6-v-gearmotor-stalled-against-a-wall&quot;&gt;Concrete Worked Example: Small 6 V Gearmotor Stalled Against a Wall&lt;/h3&gt;

&lt;p&gt;Specs: \(V = 6.0\text{ V}\), \(R_a = 3.0\,\Omega\) (so \(I_{\text{stall}} = 2.0\text{ A}\)), thermal resistance \(R_\theta = 15^\circ\text{C/W}\), thermal time constant \(\tau_{\text{th}} = 20\text{ s}\), ambient \(T_{\text{amb}} = 30^\circ\text{C}\), insulation limit \(T_{\max} = 110^\circ\text{C}\):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Stall heat power and steady-state temperature:&lt;/strong&gt;
\(P_{\text{stall}} = 6.0 \times 2.0 = 12.0\text{ W}.\)
\(T_\infty = 30 + 12.0 \times 15 = 30 + 180 = 210^\circ\text{C} \gg 110^\circ\text{C}\;\; (\text{guaranteed burnout if left stalled!}).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Temperature after 12 seconds without protection:&lt;/strong&gt;
\(T(12) = 30 + 180(1 - e^{-12/20}) = 30 + 180(1 - 0.549) = 30 + 81.2 = 111.2^\circ\text{C}\) — insulation limit breached in just 12 seconds!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Temperature with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.0 s&lt;/code&gt; firmware stall watchdog:&lt;/strong&gt;
\(T(1) = 30 + 180(1 - e^{-1/20}) = 30 + 180(0.0488) = 38.8^\circ\text{C}\) — rises less than \(9^\circ\text{C}\) before the robot safely shuts down!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Setting the software stall timeout too short (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50 ms&lt;/code&gt;) — a heavy robot starting from dead stop takes ~100–200 ms to overcome static friction and register its first encoder counts, causing false stall trips on every launch. Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;400–800 ms&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If you measure &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.25 V&lt;/code&gt; across a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.1 Ω&lt;/code&gt; current-sense shunt resistor, the motor is drawing \(I = 0.25 / 0.1 = 2.5\text{ A}\) and the shunt itself dissipates \(P = 2.5^2 \times 0.1 = 0.625\text{ W}\) (so use a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 W&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2 W&lt;/code&gt; shunt resistor, never a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.25 W&lt;/code&gt; part!).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The gap to fill is a fuse and, if you still lack them, a driver and a TT motor. A blade fuse, a glass fuse, or a polyfuse near 2–3 A, plus a holder you can actually solder or plug, is enough. Prices move. Rough bands: a small fuse and holder about 5.000–25.000 VND, a TT motor about 25.000–45.000 VND, an L298N module about 35.000–70.000 VND.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified L298N page, about 45.000 VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=polyfuse&quot;&gt;polyfuse&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=cau%20chi&quot;&gt;cầu chì&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A 6 V motor stalls at 1.2 A. What are \(R\) and the stall watts?&lt;/li&gt;
  &lt;li&gt;Two of those motors stall together. What current do you plan for, and why is a 0.5 A USB port the wrong source?&lt;/li&gt;
  &lt;li&gt;An L298N drops 2 V while carrying 0.8 A in one channel. How many watts heat that channel?&lt;/li&gt;
  &lt;li&gt;Both wheels run at 0.35 A and the logic draws 0.15 A. Is a 2 A fuse in the battery positive lead above that running total?&lt;/li&gt;
  &lt;li&gt;The ESP32 resets when the motors start, then the robot creeps. What do you change in hardware and in the first lines of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt;?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;p&gt;\(R \approx 5~\Omega\) and \(P = 7.2~\mathrm{W}\). Two motors are about 2.4 A at stall, well above a 0.5 A USB port, so Chapter 07 uses a battery and a fuse. An L298N channel at 2 V and 0.8 A heats about 1.6 W. Running total is \(0.35 + 0.35 + 0.15 = 0.85~\mathrm{A}\), so a 2 A fuse sits above normal running. Hardware wants a stiffer pack and a fuse; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt; must drive PWM to 0 and a safe direction before the bridge can move.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;SparkFun, motors and the stall-current number: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&quot;&gt;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;ST L298 datasheet, for the drop that becomes heat: &lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;https://www.st.com/resource/en/datasheet/l298.pdf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Pololu TB6612FNG carrier, for a cooler bridge at TT currents: &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;https://www.pololu.com/product/713&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Steppers and microstepping intuition</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter05/05_04_steppers_intro/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter05/05_04_steppers_intro</id>
   <content type="html">&lt;p&gt;Whenever a machine needs open-loop precision down to hundredths of a millimeter — 3D printers, CNC routers, laser cutters, and lab pipetting arms — you will almost always find &lt;strong&gt;stepper motors&lt;/strong&gt;. Instead of spinning freely when voltage is applied, a stepper rotates in discrete magnetic detents (typically 200 full steps per revolution, or \(1.8^\circ\) per step), allowing a microcontroller to command exact position simply by counting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STEP&lt;/code&gt; pulses.&lt;/p&gt;

&lt;p&gt;This lesson explains bipolar stepper physics, &lt;strong&gt;microstepping&lt;/strong&gt; (sinusoidal current shaping), and why steppers trade low-speed precision for lower efficiency and missed-step risk compared to closed-loop DC encoder motors.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;A stepper motor moves in counted angles. You tell it how many steps to take, and you trust that it took them. This lesson covers the 1.8 degree hybrid motor, what microstepping actually smooths, how to set the coil current on an A4988-style board without guessing at the potentiometer, and why Capstone A’s wheels stay with DC motors and encoders.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Convert 1.8 degrees per full step into 200 full steps per revolution, and say what an output shaft does after a gearbox.&lt;/li&gt;
  &lt;li&gt;Explain microstepping as a smoothness setting (1/2, 1/4, 1/16) whose holding torque is still the motor’s holding torque.&lt;/li&gt;
  &lt;li&gt;Compute the A4988 reference voltage for a chosen current when the sense resistors are \(0.068~\Omega\), and know to remeasure if a clone uses \(0.1~\Omega\).&lt;/li&gt;
  &lt;li&gt;Name the open-loop failure: missed steps under load look quiet and leave you at the wrong position.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can measure a small DC voltage, down near half a volt, with the meter black lead on ground. Ohm’s law and the stall-current idea from the DC motor lesson transfer directly to “current in a coil.” You do not need a stepper on the desk. A photo of an A4988 or DRV8825, plus the calculation, completes the lab. If you do have a bare NEMA17, you can count one revolution.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-electromagnetic-detents-sinusoidal-microstepping-and-pull-out-torque-curves&quot;&gt;Key ideas: Electromagnetic Detents, Sinusoidal Microstepping, and Pull-Out Torque Curves&lt;/h2&gt;

&lt;p&gt;Three physical ideas explain both the magic and the limits of stepper motors:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Full Steps &amp;amp; Microstepping Geometry:&lt;/strong&gt; A standard NEMA-17 motor has 50 rotor teeth and 2 stator phases (A and B), giving \(200\text{ full steps/rev}\) (\(1.8^\circ/\text{step}\)). A current-controlled driver (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A4988&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TMC2209&lt;/code&gt;) energizes phases sinusoidally — \(I_A = I_{\max}\cos\phi\), \(I_B = I_{\max}\sin\phi\) — subdividing each full step into \(M = 8, 16, \text{ or } 32\) &lt;strong&gt;microsteps&lt;/strong&gt; for silky-quiet motion.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Holding Torque vs. Incremental Microstep Torque:&lt;/strong&gt; Microstepping smooths vibration dramatically, yet incremental torque per microstep shrinks approximately as \(\sin(\pi / (2M))\) — meaning 1/16 microstepping delivers smooth motion but cannot overcome heavy static friction with a single microstep.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Open-Loop Slip &amp;amp; High-Speed Torque Roll-Off:&lt;/strong&gt; Because coil inductance \(L\) opposes rapid current reversals at high step rates, stepper torque drops steeply with RPM. If load torque exceeds pull-out torque, the rotor silently &lt;strong&gt;misses steps&lt;/strong&gt; with no encoder to warn the firmware.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Capstone A drives with DC gearmotors and encoders. The encoder closes the loop that a stepper pretends not to need. Steppers come back when you add a small arm, a pen lift, or a camera slide: a joint that should hold position with the power on and move a known angle. ROS joint trajectories assume you know steps per revolution of the output shaft after gearing. If you advertise 200 steps per revolution on a shaft that is behind a 50:1 gearbox, the arm will move a tiny fraction of the angle the planner drew. Getting the current limit right is what keeps that joint from missing steps while the trajectory still “succeeds” in software.&lt;/p&gt;

&lt;h2 id=&quot;full-steps-and-the-quiet-way-to-lose-them&quot;&gt;Full steps and the quiet way to lose them&lt;/h2&gt;

&lt;p&gt;A common hybrid stepper is marked 1.8 degrees per full step.&lt;/p&gt;

\[\frac{360^\circ}{1.8^\circ} = 200\]

&lt;p&gt;full steps per revolution of the motor shaft. Drivers such as the Allegro A4988 and the TI DRV8825 are chopper bridges: they switch the coil voltage so the current in each winding stays near a limit you set. The STEP pin advances the translator. The DIR pin chooses the sign. The motor holds by keeping current in the coils even when the shaft is standing still, which is why a powered stepper is warm at rest.&lt;/p&gt;

&lt;p&gt;Microstepping (1/2, 1/4, 1/8, 1/16 on a typical A4988, and finer modes on a DRV8825) divides each full step into smaller current patterns so the motion sounds smoother and the resonance is kinder. It does not multiply holding torque by 16. The torque you can fight with your fingers is still set by the motor, the current limit, and the supply. Under a heavy hand the rotor can fall behind the electrical step. Nothing in the driver raises an error flag. The mechanism is now at the wrong angle, and the program’s step counter still believes the trajectory. That is open loop. An encoder, if you add one, is what turns the lie into a number you can see.&lt;/p&gt;

&lt;p&gt;On the A4988 the MS1, MS2, and MS3 pins select the divisor. The silkscreen table is the authority for your board; the usual pattern runs from all low (full step) up to all high (1/16). Leave those pins in a known state. A floating MS pin is a random microstep mode.&lt;/p&gt;

&lt;h2 id=&quot;the-current-limit-potentiometer&quot;&gt;The current-limit potentiometer&lt;/h2&gt;

&lt;p&gt;Pololu-style A4988 boards use \(0.068~\Omega\) sense resistors under the chip. The current limit and the voltage on the potentiometer wiper are related by&lt;/p&gt;

\[I_{max} = \frac{V_{ref}}{8 \times 0.068} = \frac{V_{ref}}{0.544}\]

&lt;p&gt;You set \(V_{ref}\) with the small pot, measured from the wiper to ground, with the driver powered and the motor coils connected, unless your vendor’s note tells you a different safe procedure. Clone boards sometimes fit \(0.1~\Omega\) sense resistors. In that case the 0.544 in the denominator is wrong for your PCB, and you recompute \(8 \times R_{sense}\) from the product note before you turn the pot. A pot turned to the stop “to be strong” can set a current the motor cannot shed as heat.&lt;/p&gt;

&lt;p&gt;Never unplug a coil while the driver is enabled. The winding is an inductor, and opening it under current throws an inductive kick into the chip. Disable the driver, or remove VM, before you rearrange motor wires.&lt;/p&gt;

&lt;h2 id=&quot;a-geared-unipolar-demo-is-a-different-animal&quot;&gt;A geared unipolar demo is a different animal&lt;/h2&gt;

&lt;p&gt;The 28BYJ-48 sold with a ULN2003 board is a small 5 V unipolar motor with a plastic gearbox. It is a friendly desk demo: five wires, a darlington array, and slow motion you can count by eye. It is a poor drive wheel for Capstone. The gearbox is built for a clockwork load, the current pattern is unipolar stepping through the ULN2003, and the output speed and torque do not belong under a robot chassis. Enjoy it as a “steps are real” experiment. Put the TT motors back on the H-bridge for the robot that has to roll.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;You want a 1.0 A current limit on a Pololu-style A4988 with \(0.068~\Omega\) sense resistors.&lt;/p&gt;

\[V_{ref} = I_{max} \times 0.544 = 1.0 \times 0.544 = 0.544~\mathrm{V} \approx 0.54~\mathrm{V}\]

&lt;p&gt;Meter black on GND, red on the pot wiper, driver powered, coils connected (or follow the vendor if they insist on a specific order). Turn the pot until you read about 0.54 V. If the board’s note says the sense resistors are \(0.1~\Omega\), recompute:&lt;/p&gt;

\[V_{ref} = I_{max} \times (8 \times 0.1) = 0.80~\mathrm{V}\]

&lt;p&gt;for the same 1.0 A. Using 0.54 V on that clone would set the current lower than you think; using a pot at maximum could set it much higher. Write the resistor value you assumed next to the voltage you measured.&lt;/p&gt;

&lt;p&gt;For a later ROS joint, also write steps per output revolution. A 200-step motor at 1/16 microstep takes \(200 \times 16 = 3200\) microsteps per motor revolution. Behind a 50:1 gearbox that is \(3200 \times 50 = 160000\) microsteps per revolution of the output shaft. The trajectory converter needs the output number, not the bare 200.&lt;/p&gt;

&lt;h2 id=&quot;figures&quot;&gt;Figures&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Digital_Multimeter_Aka.jpg&quot; alt=&quot;Digital multimeter, the instrument you use on the current-limit potentiometer&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Black probe on the driver ground. Red probe on the potentiometer wiper, not on VM. You are looking for a fraction of a volt. If you read battery voltage, you are on the wrong pin.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;If a driver is powered, do not disconnect a motor coil. Set the current limit before you command a long move. Keep VM within the driver range (A4988 modules are often used near 8–12 V; read your board). A 28BYJ-48 stays on 5 V. Wheels of the Capstone robot are not involved.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Multimeter&lt;/td&gt;
      &lt;td&gt;\(V_{ref}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A4988 or DRV8825 module, if you have one&lt;/td&gt;
      &lt;td&gt;Pot and MS pins&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Stepper with four coil wires, optional&lt;/td&gt;
      &lt;td&gt;One counted revolution&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Or a clear photo of such a module&lt;/td&gt;
      &lt;td&gt;Identification path&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Notebook&lt;/td&gt;
      &lt;td&gt;Resistor value, \(V_{ref}\), step count&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;If you have no hardware, draw the pot and the MS pins from a product photo. Compute \(V_{ref}\) for 1.0 A assuming \(0.068~\Omega\), and write the alternate 0.80 V figure for a \(0.1~\Omega\) clone. Stop here.&lt;/li&gt;
  &lt;li&gt;If you have hardware, find the sense-resistor marking or the product note. Record \(R_{sense}\).&lt;/li&gt;
  &lt;li&gt;Connect the coils in the pairs the datasheet calls one winding (resistance is a few ohms between the two wires of a pair, and open between windings). Power the driver the way the vendor recommends and measure \(V_{ref}\). Adjust only if you understand the target.&lt;/li&gt;
  &lt;li&gt;Set full-step mode. Step 200 times at a slow pace and watch a flag on the shaft. A bare 1.8 degree NEMA17 should return to the flag after about 200 steps. A geared 28BYJ-48 will not.&lt;/li&gt;
  &lt;li&gt;Pinch the shaft gently during a slow move and see whether the flag ends in the wrong place while the code still finishes. That is a missed step. Release before the driver or the motor is hot.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A written \(V_{ref}\) target for 1.0 A with the resistor value you assumed, and either a photo with the pot and MS pins labeled or a counted revolution (about 200 full steps on a bare NEMA17). A sentence that says missed steps do not raise a fault by themselves.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Shaft buzzes and does not turn&lt;/td&gt;
      &lt;td&gt;Coil pairs swapped, current limit near zero, or sleep/enable held in reset.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Motor and chip get hot at idle&lt;/td&gt;
      &lt;td&gt;\(V_{ref}\) is far above the motor’s rated current. Reduce it.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Motion is smoother than full step but weak under a finger&lt;/td&gt;
      &lt;td&gt;Microstepping is on. Torque was not multiplied.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Position drifts only when loaded&lt;/td&gt;
      &lt;td&gt;Missed steps. Lower the speed, raise current only up to the motor rating, or add an encoder.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Meter shows VM on the pot&lt;/td&gt;
      &lt;td&gt;The red probe is on the wrong contact. Move to the wiper and measure to GND.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-computing-step-rate-linear-resolution-and-microstepping-phase-currents&quot;&gt;Explanation: Computing Step Rate, Linear Resolution, and Microstepping Phase Currents&lt;/h2&gt;

&lt;p&gt;Let’s calculate the exact step frequency and phase currents for a 1/16 microstepping NEMA-17 actuator in four steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compute effective microsteps per revolution:&lt;/strong&gt; \(N_{\mu} = N_{\text{full}} \times M\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Convert target RPM to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STEP&lt;/code&gt; pulse frequency:&lt;/strong&gt;
\(f_{\text{step}} = \frac{\text{RPM}}{60} \times N_{\mu}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute linear resolution&lt;/strong&gt; (for a lead screw of pitch \(P\) or pulley of diameter \(D\)):
\(\Delta x = \frac{\text{travel/rev}}{N_{\mu}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify sinusoidal coil currents at microstep \(k\):&lt;/strong&gt; \(I_A = I_{\max}\cos\!\left(\frac{k\pi}{2M}\right)\), \(I_B = I_{\max}\sin\!\left(\frac{k\pi}{2M}\right)\), keeping total power \(I_A^2 + I_B^2 = I_{\max}^2\) constant!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-nema-17-with-116-microstepping-driving-a-60-mm-wheel-at-120-rpm&quot;&gt;Concrete Worked Example: NEMA-17 with 1/16 Microstepping Driving a 60 mm Wheel at 120 RPM&lt;/h3&gt;

&lt;p&gt;Parameters: \(N_{\text{full}} = 200\) steps/rev, microstepping \(M = 16\), phase peak current \(I_{\max} = 1.00\text{ A}\), wheel diameter \(D = 60\text{ mm}\) (circumference \(\pi \times 60 = 188.5\text{ mm}\)):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Microsteps per revolution and linear step size:&lt;/strong&gt;
\(N_{\mu} = 200 \times 16 = 3\,200\text{ microsteps/rev}.\)
\(\Delta x = \frac{188.5\text{ mm}}{3\,200} \approx 0.0589\text{ mm/microstep}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Required &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STEP&lt;/code&gt; pulse rate for 120 RPM (2 rev/s):&lt;/strong&gt;
\(f_{\text{step}} = 2\text{ rev/s} \times 3\,200 = 6\,400\text{ Hz}\;\; (\text{one pulse every } 156.25\,\mu\text{s}).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Coil currents at microstep \(k = 4\) (one quarter of a full step, \(\phi = 4\pi/32 = 22.5^\circ\)):&lt;/strong&gt;
\(I_A = 1.00 \cos(22.5^\circ) \approx 0.924\text{ A}, \qquad I_B = 1.00 \sin(22.5^\circ) \approx 0.383\text{ A}.\)
Check constant-power identity: \(0.924^2 + 0.383^2 = 0.854 + 0.147 \approx 1.00 = I_{\max}^2\)!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Unplugging a stepper motor’s 4-pin cable &lt;em&gt;while the driver is powered ON&lt;/em&gt; — breaking an energized inductive coil generates an instant high-voltage flyback spike that destroys &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A4988&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TMC2209&lt;/code&gt; output MOSFETs. Always power off before touching stepper cables!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; Why do mobile battery robots usually prefer DC gearmotors with encoders over NEMA-17 steppers? Because steppers draw near-full holding current &lt;em&gt;even while standing completely still&lt;/em&gt;, draining a 2S battery 3×–5× faster than a DC motor idling at 0 A!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;You can finish this lesson with no purchase. If you want a desk demo, a 28BYJ-48 with ULN2003 is the cheap unipolar kit. A NEMA17 plus an A4988 is the hybrid setup that matches the 200-step count. Prices move. Rough bands: 28BYJ-48 kit about 25.000–50.000 VND, A4988 module about 20.000–55.000 VND, NEMA17 often 150.000–400.000 VND depending on body length.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=A4988&quot;&gt;A4988&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=28BYJ-48&quot;&gt;28BYJ-48&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=NEMA17&quot;&gt;NEMA17&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=A4988&quot;&gt;A4988&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=28BYJ-48&quot;&gt;28BYJ-48&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=A4988&quot;&gt;A4988&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=DRV8825&quot;&gt;DRV8825&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=A4988&quot;&gt;A4988&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=ULN2003&quot;&gt;ULN2003&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;How many full steps does a 1.8 degree motor take per revolution? How many 1/16 microsteps is that?&lt;/li&gt;
  &lt;li&gt;Sense resistors are \(0.068~\Omega\) and you measure \(V_{ref} = 0.30~\mathrm{V}\). What current limit did you set?&lt;/li&gt;
  &lt;li&gt;A clone note says \(R_{sense} = 0.1~\Omega\). What \(V_{ref}\) gives 0.8 A?&lt;/li&gt;
  &lt;li&gt;The arm finishes a 400-step program and the tip is short of the mark, with no error printed. What physical event fits those facts?&lt;/li&gt;
  &lt;li&gt;You plan a ROS joint on a 200-step motor, full step, 30:1 gearbox. How many steps is one output revolution?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;p&gt;A 1.8 degree motor takes 200 full steps and \(200 \times 16 = 3200\) microsteps of 1/16. With \(0.068~\Omega\), \(I_{max} = 0.30 / 0.544 \approx 0.55~\mathrm{A}\). For \(0.1~\Omega\), \(V_{ref} = 0.8 \times 0.8 = 0.64~\mathrm{V}\). A quiet shortfall is missed steps in open loop. One output revolution at full step behind 30:1 gearing is \(200 \times 30 = 6000\) steps.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Pololu A4988 carrier, including the current-limit section and the \(V_{ref}\) formula: &lt;a href=&quot;https://www.pololu.com/product/1182&quot;&gt;https://www.pololu.com/product/1182&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;TI DRV8825 datasheet (chopper stepper drive and microstep modes): &lt;a href=&quot;https://www.ti.com/lit/ds/symlink/drv8825.pdf&quot;&gt;https://www.ti.com/lit/ds/symlink/drv8825.pdf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;SparkFun motor selection, for torque and current language you can compare with the DC lessons: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&quot;&gt;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Hobby servos, PWM, and torque limits</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter05/05_03_servos_pwm/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter05/05_03_servos_pwm</id>
   <content type="html">&lt;p&gt;Unlike a bare DC motor that spins endlessly until told to stop, a &lt;strong&gt;hobby RC servo&lt;/strong&gt; packs a DC motor, a high-reduction gear train, a position potentiometer, and an internal closed-loop PID controller inside a 20-gram plastic box. Hand it a 1.5-millisecond pulse every 20 milliseconds, and it drives its horn straight to \(90^\circ\) and fights back against external torque to hold that angle.&lt;/p&gt;

&lt;p&gt;This lesson explains standard 50 Hz servo pulse encoding, static torque leverage math, and why powering even one micro-servo off an ESP32’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; or USB pin triggers immediate brownouts.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;A hobby servo hides a small DC motor, a gearbox, a potentiometer, and a control loop inside one plastic case. You command it with a pulse width, not with the duty-cycle PWM you just used on a wheel. This lesson separates those two signals, treats the kg·cm number on the packet as a stall figure, and shows you how to sweep a horn without cooking the motor against a hard stop.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;State the 50 Hz frame and the meaning of about 1.0 ms, 1.5 ms, and 2.0 ms pulses, including that some servos travel over 0.5–2.5 ms.&lt;/li&gt;
  &lt;li&gt;Compute the 7.5% duty of a 1.5 ms pulse in a 20 ms frame, and explain why a motor-style &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; produces a different signal.&lt;/li&gt;
  &lt;li&gt;Read a kg·cm torque rating as a stall figure at a stated voltage, and distinguish a positional servo from a continuous-rotation unit.&lt;/li&gt;
  &lt;li&gt;Power the servo from a 5 V rail that can supply stall current, share ground with the microcontroller, and stop a sweep that meets a mechanical stop.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can measure 5 V with a meter and you know the H-bridge PWM from lesson 05-01 well enough to notice that this pulse is a different shape. You can use a servo library, or you can generate a microsecond pulse on your board. A paper protractor, tape, and a 5 V supply that can deliver about an ampere are enough hardware. If you have no servo, the calculation half of the lab still stands.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-pulse-width-position-encoding-static-torque-leverage-and-actuator-current-spikes&quot;&gt;Key ideas: Pulse-Width Position Encoding, Static Torque Leverage, and Actuator Current Spikes&lt;/h2&gt;

&lt;p&gt;Three mechanical and electrical principles govern hobby servos on robots (pan-tilt heads, grippers, steering):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;50 Hz Servo PWM Protocol:&lt;/strong&gt; Unlike DC motor PWM (where &lt;em&gt;duty percentage&lt;/em&gt; at 20 kHz sets speed), standard hobby servos decode &lt;strong&gt;absolute pulse width&lt;/strong&gt; at \(50\text{ Hz}\) (\(T = 20\text{ ms}\)): \(1.0\text{ ms} \to 0^\circ\), \(1.5\text{ ms} \to 90^\circ\), \(2.0\text{ ms} \to 180^\circ\) (some clones span &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.5–2.5 ms&lt;/code&gt;). Notice duty cycle only spans \(5\%\text{–}10\%\) of the 20 ms frame!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Static Torque &amp;amp; Lever Arm (\(\tau = r \times F\)):&lt;/strong&gt; Servo datasheets quote stall torque in \(\text{kg}\cdot\text{cm}\) (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SG90&lt;/code&gt;: \(1.8\text{ kg}\cdot\text{cm}\); &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MG996R&lt;/code&gt;: \(10\text{ kg}\cdot\text{cm}\) at 6 V). Doubling the length of a gripper arm &lt;em&gt;halves&lt;/em&gt; the mass it can lift!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Stall Current &amp;amp; Dedicated 5–6 V Rail:&lt;/strong&gt; Even a tiny &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SG90&lt;/code&gt; spikes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;500–700 mA&lt;/code&gt; when starting or hitting a mechanical end-stop; an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MG996R&lt;/code&gt; spikes above &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2.0–2.5 A&lt;/code&gt;. Servos must always run from a dedicated 5–6 V buck rail with a bulk capacitor.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Capstone A’s drive does not need a servo. Two brushed motors and an H-bridge do the rolling. A small pan servo is optional later, for a distance sensor or a camera that must look left and right. When you eventually describe that joint to ROS, the controller speaks in position or trajectory of a joint, and a driver node turns that position into the hobby pulse. The pulse itself is a firmware detail. Getting the pulse, the supply, and the horn spline right now means a later joint does not jitter, brown out the robot, or strip a plastic horn on the first demo. The wheel PWM you sign in Chapter 07 stays a wheel PWM. You will not “servo-write” a drive motor.&lt;/p&gt;

&lt;h2 id=&quot;the-pulse-inside-a-20-ms-frame&quot;&gt;The pulse inside a 20 ms frame&lt;/h2&gt;

&lt;p&gt;Hobby servos expect a frame about every 20 ms, which is 50 Hz. Inside that frame the high time is the command. About 1.0 ms sits near one end of travel, 1.5 ms is the mechanical center on a typical positional servo, and 2.0 ms sits near the other end. Some servos, and some extended-range settings, travel from about 0.5 ms to 2.5 ms. The diagram in this lesson draws that high time inside the longer frame. The library call you want is a microsecond write: 1000, 1500, and 2000.&lt;/p&gt;

&lt;p&gt;The duty-cycle trap is numerical. A 1.5 ms pulse divided by a 20 ms frame is 7.5 percent. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; on a typical motor pin emits a much faster PWM whose duty you set as a fraction of a short period. A 7.5 percent duty at 1 kHz is a high time of 75 µs, which a servo does not read as “center.” Use the servo library for your board, or a timer that really holds the line high for the microsecond count you asked for, and repeats that pulse about every 20 ms.&lt;/p&gt;

&lt;h2 id=&quot;torque-gears-and-the-servo-that-is-actually-a-motor&quot;&gt;Torque, gears, and the servo that is actually a motor&lt;/h2&gt;

&lt;p&gt;The number printed in kg·cm is a stall torque at a stated voltage, often 4.8 V or 6 V. It is the twist the horn can hold when the shaft is already stopped and the motor is pulling full current. It is not a license to stall the servo as a clamp. An SG90 is a small 9 g positional servo: useful for a light sensor pan, easy to strip if the horn hits a bracket and you keep commanding the far pulse. A metal-gear MG90S is the usual substitute when the plastic gears skip. It still stalls and still heats; the gears survive longer.&lt;/p&gt;

&lt;p&gt;A continuous-rotation servo is a different part that uses the same three-wire plug. Its potentiometer is replaced by a trim, and the pulse commands speed and direction, not an angle. If your horn spins freely for many turns at 1000 µs and sits still near 1500 µs, you have a continuous unit. Park it for a wheel experiment someday. It will not hold a camera angle.&lt;/p&gt;

&lt;h2 id=&quot;power-ground-and-heat&quot;&gt;Power, ground, and heat&lt;/h2&gt;

&lt;p&gt;Feed the red wire from a 5 V rail that can supply the stall current. For an SG90 that is on the order of 0.5–0.7 A at stall, more than the 3.3 V pin of an ESP32 can provide, and more than a USB port enjoys if the horn is loaded. The signal wire may be driven at 3.3 V; most hobby servos read that as a valid high. The black or brown wire shares ground with the microcontroller and with the 5 V supply. If that ground is missing, the pulses are measured against the wrong reference and the horn twitches or walks to a stop.&lt;/p&gt;

&lt;p&gt;A stalled servo gets hot in seconds because the motor inside is a small DC motor at nearly stall current, and the loop keeps driving it. Your sweep must not command a pulse past a horn that has already hit a screw or the table. If the case becomes hot, command the last free angle, remove power, and move the mechanical stop.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;You command 1500 µs, the center pulse.&lt;/p&gt;

\[D = \frac{1.5~\mathrm{ms}}{20~\mathrm{ms}} = 0.075 = 7.5\%\]

&lt;p&gt;If a pin is instead producing a 1 kHz motor PWM at that same 7.5 percent duty, the high time is&lt;/p&gt;

\[t_{high} = \frac{0.075}{1000~\mathrm{Hz}} = 75~\mu\mathrm{s}\]

&lt;p&gt;Seventy-five microseconds is not a center command. The servo library’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeMicroseconds(1500)&lt;/code&gt; is the call that matches the diagram.&lt;/p&gt;

&lt;p&gt;Now suppose the code really is sending 1500 µs and the arm still sits about 40 degrees off the angle you wanted. The electronics are doing what you asked. The horn is a press fit on a splined shaft, and it was pushed on at whatever angle it happened to have. Power the servo off, lift the horn, set the servo to 1500 µs briefly to confirm center, power off again, and reseat the horn so the arm matches the protractor’s zero. Forcing the horn around with the spline engaged strips the teeth.&lt;/p&gt;

&lt;h2 id=&quot;figures&quot;&gt;Figures&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/servo_pwm.png&quot; alt=&quot;Hobby-servo pulse: a short high time inside a 20 ms frame&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Find the 20 ms repeat and the high time that moves from about 1 ms to about 2 ms. That high time is the whole command. The rest of the frame is the gap the servo uses to finish its own control cycle.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;External 5 V only for the servo power pin. Do not use the ESP32 3.3 V pin as the servo supply. Share ground. Keep the horn clear of fingers and of hard stops. If the case heats up, stop the sweep and remove power. One servo at a time.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;SG90 or MG90S positional servo&lt;/td&gt;
      &lt;td&gt;The actuator under test&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5 V supply capable of about 1 A&lt;/td&gt;
      &lt;td&gt;Servo power, separate from 3.3 V&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Microcontroller&lt;/td&gt;
      &lt;td&gt;Signal pin only&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Paper protractor and tape&lt;/td&gt;
      &lt;td&gt;Rough angle log&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Jumper wires&lt;/td&gt;
      &lt;td&gt;Signal, 5 V, and common ground&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Wire 5 V and ground to the servo from the external rail. Wire ground to the microcontroller. Wire the signal pin to a GPIO. Leave power off while you seat the horn loosely.&lt;/li&gt;
  &lt;li&gt;Command 1500 µs. Power on. Note where the horn rests. Power off and reseat the horn so that rest matches a sensible zero on the protractor.&lt;/li&gt;
  &lt;li&gt;Sweep from 1000 µs to 2000 µs in steps of about 50–100 µs, pausing about 200 ms at each step. At three points (1000, 1500, 2000) write the protractor angle.&lt;/li&gt;
  &lt;li&gt;Stop at once if the horn buzzes against a stop or the case becomes hot. Narrow the sweep to the free range.&lt;/li&gt;
  &lt;li&gt;If you have no servo, write the 7.5% calculation, sketch the 20 ms frame with a 1.5 ms pulse, and state the supply rule in your own sentence.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;Three angle notes, a statement of the free pulse range you actually used, and a one-line supply note (“5 V external, common ground, signal from GPIO”). A no-hardware path still contains the duty calculation and the sketch.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Horn twitches or chatters&lt;/td&gt;
      &lt;td&gt;Missing common ground, or the pin is still a fast &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; PWM.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Horn drives to a stop and heats&lt;/td&gt;
      &lt;td&gt;The pulse is outside the mechanical range. Shorten the sweep.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 resets when the servo moves&lt;/td&gt;
      &lt;td&gt;Servo current is coming from USB or from 3.3 V. Move the red wire to an external 5 V rail.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Angle is offset by a fixed amount&lt;/td&gt;
      &lt;td&gt;Horn spline seating. Power off and reseat.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Horn spins continuously&lt;/td&gt;
      &lt;td&gt;The part is a continuous-rotation servo. Its pulse sets speed, not angle.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-computing-servo-pulse-widths-angular-resolution-and-lifting-torque&quot;&gt;Explanation: Computing Servo Pulse Widths, Angular Resolution, and Lifting Torque&lt;/h2&gt;

&lt;p&gt;Let’s verify both the electrical PWM math and the mechanical lifting limit of a servo arm in four steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Map target angle \(\theta \in [0^\circ, 180^\circ]\) to pulse width \(t_{\text{pulse}}\):&lt;/strong&gt;
\(t_{\text{pulse}}(\theta) = 1.0 + \frac{\theta}{180^\circ} \times 1.0\text{ ms}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Convert to timer counts for an \(N\)-bit 50 Hz timer (\(T = 20\text{ ms}\)):&lt;/strong&gt;
\(\text{counts} = \frac{t_{\text{pulse}}}{20\text{ ms}} \times (2^N - 1).\)
Notice why 8-bit PWM at 50 Hz is terrible for servos: only \(5\%\) of 255 \(\approx 13\) steps span the entire \(180^\circ\) (coarse \(14^\circ\) jumps!). Use at least &lt;strong&gt;12–16 bits&lt;/strong&gt;!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Convert datasheet torque from \(\text{kg}\cdot\text{cm}\) to SI \(\text{N}\cdot\text{m}\):&lt;/strong&gt;
\(\tau\;[\text{N}\cdot\text{m}] = \tau\;[\text{kg}\cdot\text{cm}] \times 0.0981.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute maximum liftable payload at arm length \(L\) with a 50% dynamic safety factor:&lt;/strong&gt; \(m_{\max} = 0.5 \times \frac{\tau_{\text{stall}}\;[\text{kg}\cdot\text{cm}]}{L\;[\text{cm}]}\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-16-bit-pulse-count-for-135circ--sizing-an-sg90-pan-tilt--gripper-arm&quot;&gt;Concrete Worked Example: 16-Bit Pulse Count for \(135^\circ\) &amp;amp; Sizing an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SG90&lt;/code&gt; Pan-Tilt / Gripper Arm&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Part A — Commanding \(\theta = 135^\circ\) on a 16-bit 50 Hz timer (65,535 max):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Pulse width: \(t_{\text{pulse}} = 1.0 + \frac{135}{180}(1.0) = 1.75\text{ ms}\).&lt;/li&gt;
      &lt;li&gt;Duty ratio: \(D = \frac{1.75}{20.0} = 0.0875\;\; (8.75\%)\).&lt;/li&gt;
      &lt;li&gt;16-bit timer count: \(0.0875 \times 65\,535 \approx 5\,734\).&lt;/li&gt;
      &lt;li&gt;Angular resolution across the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.0–2.0 ms&lt;/code&gt; window (\(3\,277\) counts for \(180^\circ\)): \(180 / 3277 \approx 0.055^\circ/\text{count}\) — ultra-smooth!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Part B — Lifting a sensor/object at \(L = 6\text{ cm}\) with an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SG90&lt;/code&gt; (\(\tau_{\text{stall}} = 1.8\text{ kg}\cdot\text{cm}\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Theoretical stall mass at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6 cm&lt;/code&gt;: \(m_{\text{stall}} = \frac{1.8}{6} = 0.30\text{ kg} = 300\text{ g}\).&lt;/li&gt;
      &lt;li&gt;Safe working payload (50% margin for acceleration + arm weight): \(m_{\text{safe}} = 150\text{ g}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Commanding a servo to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0°&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;180°&lt;/code&gt; when a mechanical bracket physically blocks the horn at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;165°&lt;/code&gt; — the internal controller fights the hard stop at full stall current until the plastic gears strip or the coil burns out. Always enforce software angle clamps inside the physical range!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If your gripper arm is shortened from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8 cm&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4 cm&lt;/code&gt;, the gripping force at the fingertips &lt;strong&gt;doubles&lt;/strong&gt; for the exact same servo motor.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;An SG90 is enough for a sensor pan. Buy an MG90S if you already stripped a plastic horn or you know the load is a bit rude. Search “SG90” and “MG90S”. Prices move. Rough bands: SG90 about 20.000–45.000 VND, MG90S about 35.000–80.000 VND. A 5 V buck module, if you do not already have one from the driver lesson, is a separate search (“buck 5V”).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=SG90&quot;&gt;SG90&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=MG90S&quot;&gt;MG90S&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=SG90&quot;&gt;SG90&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=MG90S&quot;&gt;MG90S&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=SG90&quot;&gt;SG90&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=MG90S&quot;&gt;MG90S&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=SG90&quot;&gt;SG90&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=servo&quot;&gt;servo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;What high time, in microseconds, is the usual center, and what duty is that inside a 20 ms frame?&lt;/li&gt;
  &lt;li&gt;A 1 kHz pin runs at 10% duty. What is the high time, and is it inside 1.0–2.0 ms?&lt;/li&gt;
  &lt;li&gt;The packet says 1.8 kg·cm at 4.8 V. What kind of number is that, and what happens electrically at that load?&lt;/li&gt;
  &lt;li&gt;Your 1500 µs command is correct and the arm is 40 degrees off. What do you loosen, and in what power state?&lt;/li&gt;
  &lt;li&gt;Why does Capstone A’s drive chassis ignore this lesson’s pulse, and where might the pulse return later?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;p&gt;Center is 1500 µs, which is 7.5% of 20 ms. A 10% duty at 1 kHz is 100 µs, far below a 1 ms servo pulse, so the servo library is the right tool. The 1.8 kg·cm figure is a stall torque at 4.8 V, and at that load the internal motor is near stall current and heating. Power off, then reseat the horn on the spline. Capstone A drives with DC motors and an H-bridge; a pan servo can return later as a joint whose hobby pulse stays inside firmware.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;SparkFun hobby servo tutorial (pulse, wiring, and power): &lt;a href=&quot;https://learn.sparkfun.com/tutorials/hobby-servo-tutorial/all&quot;&gt;https://learn.sparkfun.com/tutorials/hobby-servo-tutorial/all&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;SparkFun motor guide, for the stall current hiding inside the servo case: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&quot;&gt;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Motor driver board ID: L298N, TB6612, DRV8833</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter05/05_02_driver_board_id/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter05/05_02_driver_board_id</id>
   <content type="html">&lt;p&gt;Three motor driver boards dominate student robotics kits — the bulky red &lt;strong&gt;L298N&lt;/strong&gt;, the tiny blue &lt;strong&gt;TB6612FNG&lt;/strong&gt;, and the postage-stamp &lt;strong&gt;DRV8833&lt;/strong&gt; — and picking the wrong one is the fastest way to turn half your battery into waste heat or melt a breakout board under stall load. Knowing how to read their topology (BJT Darlington vs. MOSFET), pin semantics (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1/AIN2/PWMA&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1/IN2&lt;/code&gt;), and standby pins turns driver selection into an exact science.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;The silkscreen on a red module is part of the circuit. Before you trust a tutorial pinout, you identify which chip is actually soldered on your board, because the L298N, the TB6612, and the DRV8833 disagree about voltage drop, logic supply, standby, and how much current a TT gearmotor may ask for at stall. This lesson teaches you to name the board in your hand and to measure its rails before a microcontroller pin ever touches it.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Identify an L298N module, a TB6612 module, and a DRV8833 breakout from the chip body, the heatsink, and the pin names.&lt;/li&gt;
  &lt;li&gt;State what the L298N 5 V jumper does, when it must come off, and why that regulator is a poor supply for an ESP32 radio.&lt;/li&gt;
  &lt;li&gt;Explain why STBY on a TB6612 has to be driven high, and why a DRV8833 is a small-motor part on a 1S–2S pack under 10 V.&lt;/li&gt;
  &lt;li&gt;Measure VM and the logic rail with a meter before connecting a GPIO, and record every screw terminal in your lab notes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can use the DC volts range of a multimeter and you know which probe is black. Lesson 05-01’s H-bridge vocabulary (forward path, reverse path, shoot-through, PWM on enable) is the map you are now matching to a physical board. You also know that motor current does not come from USB.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-bjt-vs-mosfet-h-bridges-voltage-dropout-and-control-pin-topologies&quot;&gt;Key ideas: BJT vs. MOSFET H-Bridges, Voltage Dropout, and Control Pin Topologies&lt;/h2&gt;

&lt;p&gt;Three hardware-selection criteria distinguish modern motor drivers:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;BJT Darlington (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L298N&lt;/code&gt;) vs. MOSFET (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt;):&lt;/strong&gt; The 1980s-era &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L298N&lt;/code&gt; drops a fixed \(V_{\text{CE(sat)}} \approx 1.8\text{–}3.2\text{ V}\) across its bipolar transistors regardless of low current, wasting huge power and requiring a metal heatsink. Modern MOSFET drivers (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt;: \(R_{\text{on}} \approx 0.5\,\Omega\); &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt;: \(R_{\text{on}} \approx 0.36\,\Omega\)) drop only \(I \cdot R_{\text{on}}\), staying tiny and cool.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Control Pin Semantics:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt; uses 2 direction pins (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIN2&lt;/code&gt;) + 1 dedicated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWMA&lt;/code&gt; pin + a mandatory &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; pin (must be pulled &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; to enable!). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt; uses just 2 PWM-capable pins (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN2&lt;/code&gt;) per channel + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nSLEEP&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; to enable).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Absolute Voltage &amp;amp; Current Windows:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt; tops out at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.8 V&lt;/code&gt; (ideal for 1S/2S packs, &lt;em&gt;destroyed&lt;/em&gt; by 3S 12.6 V packs!), whereas &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt; handles up to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;13.5 V&lt;/code&gt; continuous (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.2 A&lt;/code&gt; cont / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.2 A&lt;/code&gt; peak per channel).r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Capstone A rolls on two yellow TT gearmotors from a 2S pack. The driver under those motors sets how much of the battery reaches the brushes and whether a forgotten pin leaves both wheels asleep. Chapter 07’s signed left and right PWM assumes a live bridge with a known truth table. ROS 2 does not repair a regulator that sags when Wi-Fi transmits: the microcontroller resets, the last PWM value or a floating input remains, and the chassis can creep. Choosing the TB6612 as the default, and knowing how to live safely with an L298N if that is what the shop sold you, is part of making &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd_vel&lt;/code&gt; mean motion you intended.&lt;/p&gt;

&lt;h2 id=&quot;what-you-see-on-the-desk&quot;&gt;What you see on the desk&lt;/h2&gt;

&lt;p&gt;An L298N module is the chunky red or green board with a bolted metal heatsink on a large DIP package and a small jumper near a 5 V regulator. Screw terminals take VM, GND, and the two motors. The logic header is usually IN1, IN2, ENA, IN3, IN4, ENB, plus a 5 V pin.&lt;/p&gt;

&lt;p&gt;A TB6612 module is smaller. The Toshiba TB6612FNG is a MOSFET bridge. The pins you should be able to point to are VM, VCC, GND, AIN1, AIN2, PWMA, BIN1, BIN2, PWMB, and STBY. Two motors share the chip. There is often no large bolted heatsink, because the drop is much smaller than a Darlington’s.&lt;/p&gt;

&lt;p&gt;A DRV8833 breakout is tiny. The TI DRV8833 is a dual bridge whose motor supply is about 2.7–10.6 V. Many hobby boards run it in IN/IN mode, where the two inputs of a channel encode direction and PWM. You rarely see a big heatsink. The part is comfortable with small motors and with a 1S or 2S pack that stays under 10 V. A pair of TT motors at stall asks for more current than this small bridge is happy to hold.&lt;/p&gt;

&lt;h2 id=&quot;the-l298n-and-its-5-v-jumper&quot;&gt;The L298N and its 5 V jumper&lt;/h2&gt;

&lt;p&gt;The L298N output stage is bipolar Darlington. While current flows, the bridge drops roughly 2 V, and that product of drop and current becomes heat in the chip. The bolted heatsink is doing real work. Logic on this module is a 5 V world: the chip’s logic supply pin wants about 5 V.&lt;/p&gt;

&lt;p&gt;The onboard 7805, when the jumper is fitted, steals its input from the motor supply and produces that 5 V. If the motor supply is above 12 V, the regulator dissipates too much and cooks. Pull the jumper off in that case and feed 5 V from a separate regulator. Even at 12 V and below, do not power an ESP32 Wi-Fi radio from that 7805. The radio’s current bursts sag the little regulator, the ESP32 browns out, and the robot reboots mid-command. Use a separate buck for logic, and keep a common ground with VM.&lt;/p&gt;

&lt;p&gt;A 3.3 V GPIO is usually enough to be seen as a high on the L298 inputs, whose input-high threshold is about 2.3 V, provided the logic supply pin itself is a solid 5 V and the grounds are common. The jumper and the 5 V pin are about powering the chip’s logic, not about “borrowing 5 V for the whole robot.”&lt;/p&gt;

&lt;h2 id=&quot;tb6612-the-capstone-default&quot;&gt;TB6612, the Capstone default&lt;/h2&gt;

&lt;p&gt;The TB6612FNG uses MOSFETs, so the voltage left for the motor is much closer to VM than it was on the L298N. VCC, the logic supply, accepts about 2.7–5.5 V, which means a 3.3 V ESP32 can power the logic pin directly. VM may run up to about 15 V; stay inside the range your particular board lists, and a 2S pack near 7.4–8.4 V is a comfortable match. Continuous current is about 1.2 A per channel, which is a fair budget for one TT motor if you respect stall time.&lt;/p&gt;

&lt;p&gt;STBY must be driven high or the bridges stay asleep. A floating STBY pin is the classic “my code is perfect and the wheels are dead” bug. Tie it to VCC if you want the chip awake whenever logic power is present, or drive it from a GPIO you set high in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt; and low when you want a hardware coast. PWMA and PWMB take the speed chops. AIN1 and AIN2, and the B pair, take direction. Read the Pololu or Toshiba table, then confirm coast versus brake on the bench as you did in lesson 05-01.&lt;/p&gt;

&lt;h2 id=&quot;drv8833-and-the-boards-that-are-too-small&quot;&gt;DRV8833 and the boards that are too small&lt;/h2&gt;

&lt;p&gt;The DRV8833 is a good dual bridge for a small mechanism, a tiny robot on 1S, or a 2S experiment whose motors stall well below what a TT pair demands. Many breakouts expose IN1/IN2 and IN3/IN4 and expect you to PWM one input while holding the other. There is often a sleep pin that must be held awake, in the same spirit as STBY. If your only motors are the Capstone TT pair, pick the TB6612.&lt;/p&gt;

&lt;p&gt;MX1508 and L9110 modules show up in the same search results. They are fine for a very small motor on a desk demo. They are the wrong bridge for a 2S TT robot: the current and the thermal mass are both too small, and a stall becomes a melted package. If that is the board in the drawer, use it to learn pin names on a tiny motor, then buy a TB6612 before the chassis goes together.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Suppose the motor supply is 12 V and the L298N jumper is still on, so the 7805 drops \(12 - 5 = 7~\mathrm{V}\). Take \(0.20~\mathrm{A}\) as a round figure for a busy ESP32 radio (a real Wi-Fi burst can be higher).&lt;/p&gt;

\[P_{7805} \approx (12 - 5) \times 0.20 = 1.4~\mathrm{W}\]

&lt;p&gt;That heat sits in a small regulator with almost no copper. The 5 V rail sags, the microcontroller resets, and any PWM that happens to be on becomes a surprise. Above 12 V the product is worse, which is why the jumper comes off. The same 1 A through an L298N channel that drops about 2 V puts about \(2~\mathrm{W}\) in the Darlington chip, which is why the bolted heatsink is not decoration. A TB6612 on 2S avoids both of these heaters for the Capstone motors.&lt;/p&gt;

&lt;h2 id=&quot;figures&quot;&gt;Figures&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Dosmotorsl298n.jpg&quot; alt=&quot;L298N dual H-bridge module with a bolted heatsink&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Look for the large package under the metal, the screw terminals for the motors and VM, and the jumper that feeds the onboard regulator. If your board matches this shape, treat it as an L298N until the chip marking says otherwise.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Digital_Multimeter_Aka.jpg&quot; alt=&quot;A digital multimeter used to measure VM and the logic rail before any GPIO is connected&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Black on the driver ground, red on VM, then red on the logic 5 V or 3.3 V pin. Write the numbers down. A pin that is already at battery voltage is not a GPIO destination.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Motor supply off while you probe with the continuity function. When you measure VM, use the volts range, start with one hand, and do not let the probe slip from VM onto a logic pin. Do not connect an ESP32 pin until both rails are known. Wheels stay off the ground if a motor is attached.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;The driver module you actually own&lt;/td&gt;
      &lt;td&gt;The object of the identification&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Multimeter&lt;/td&gt;
      &lt;td&gt;DC volts, then continuity if you wish&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Your phone&lt;/td&gt;
      &lt;td&gt;A labeled photo&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Optional: 2S pack or bench supply&lt;/td&gt;
      &lt;td&gt;VM source, current-limited&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Notebook&lt;/td&gt;
      &lt;td&gt;Pin list and the two measured voltages&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Photograph the board top and bottom. Circle the chip marking if you can see it.&lt;/li&gt;
  &lt;li&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, label every screw terminal and every logic pin with the name on the silk (VM, VCC, GND, ENA, STBY, and so on).&lt;/li&gt;
  &lt;li&gt;If a 5 V jumper exists, write what it connects. State whether it stays on for your pack voltage.&lt;/li&gt;
  &lt;li&gt;Apply VM from the battery or bench supply with the microcontroller still disconnected. Measure VM and measure the logic rail. Record both.&lt;/li&gt;
  &lt;li&gt;Point at STBY or at the L298 jumper and say aloud what that part does. If you have a DRV8833, point at the sleep or fault pin and say what “awake” means on that board.&lt;/li&gt;
  &lt;li&gt;Only then wire VCC or 5 V logic, common ground, and the input pins.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;You can name the chip, you have a pin-labeled photo, and you have two meter readings written down (VM and logic). You can say what STBY or the 5 V jumper does without reading the silkscreen again. A DRV8833 owner can state the 2.7–10.6 V motor range and whether their pack fits inside it.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Logic 5 V reads 0 with the jumper on&lt;/td&gt;
      &lt;td&gt;VM is missing, the jumper is seated on the wrong pins, or the 7805 has already failed.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 resets when Wi-Fi starts&lt;/td&gt;
      &lt;td&gt;The radio is fed from the module 7805. Move logic power to a separate buck.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TB6612 outputs stay dead&lt;/td&gt;
      &lt;td&gt;STBY is low or floating. Drive it high and remeasure.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;DRV8833 shuts down on a TT motor&lt;/td&gt;
      &lt;td&gt;Stall current is above what the small bridge can hold. Change the motor or the driver.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MX1508 gets too hot to touch&lt;/td&gt;
      &lt;td&gt;The motor is too large for that board. Remove power.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-selecting-the-optimal-motor-driver-via-quantitative-loss--margin-analysis&quot;&gt;Explanation: Selecting the Optimal Motor Driver via Quantitative Loss &amp;amp; Margin Analysis&lt;/h2&gt;

&lt;p&gt;Before wiring a motor driver, run this 4-step selection matrix against your battery pack and motor stall current:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Check maximum battery voltage (fully charged!):&lt;/strong&gt; A 2S Li-ion pack is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8.4 V&lt;/code&gt; fresh (safe for all three); a 3S pack is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12.6 V&lt;/code&gt; fresh (instantly kills a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.8 V&lt;/code&gt; max &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt;!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check continuous vs. peak stall current per channel:&lt;/strong&gt; Verify driver continuous current \(\ge\) cruise load and peak current \(\ge I_{\text{stall}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute driver heat dissipation at cruise (two channels):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;BJT: \(P = 2 \cdot V_{\text{drop}} \cdot I\).&lt;/li&gt;
      &lt;li&gt;MOSFET: \(P = 2 \cdot I^2 \cdot R_{\text{DS(on)}}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify effective voltage delivered to the motors:&lt;/strong&gt; \(V_{\text{motor}} = V_{\text{bat}} - V_{\text{drop}}\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-head-to-head-comparison-at-v_textbat--74text-v-i--08text-achannel-2-motors&quot;&gt;Concrete Worked Example: Head-to-Head Comparison at \(V_{\text{bat}} = 7.4\text{ V}\), \(I = 0.8\text{ A/channel}\) (2 Motors)&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Driver&lt;/th&gt;
      &lt;th&gt;Max \(V_{\text{M}}\)&lt;/th&gt;
      &lt;th&gt;Drop per Channel at 0.8 A&lt;/th&gt;
      &lt;th&gt;Voltage at Motor&lt;/th&gt;
      &lt;th&gt;Total Heat (2 Ch)&lt;/th&gt;
      &lt;th&gt;Verdict on 2S Robot&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;L298N&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(46\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(\approx 2.20\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(5.20\text{ V}\) (70%)&lt;/td&gt;
      &lt;td&gt;\(2(2.20)(0.8) = 3.52\text{ W}\)&lt;/td&gt;
      &lt;td&gt;Heavy, hot, poor 2S efficiency&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;TB6612FNG&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(13.5\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(0.8 \times 0.50 = 0.40\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(7.00\text{ V}\) (95%)&lt;/td&gt;
      &lt;td&gt;\(2(0.8^2)(0.50) = 0.64\text{ W}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Best all-around (2S &amp;amp; 3S)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;DRV8833&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(10.8\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(0.8 \times 0.36 = 0.29\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(7.11\text{ V}\) (96%)&lt;/td&gt;
      &lt;td&gt;\(2(0.8^2)(0.36) = 0.46\text{ W}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Best for low-voltage 1S–2S&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Notice that at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.8 A/channel&lt;/code&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt; waste &lt;strong&gt;5.5× to 7.6× less heat&lt;/strong&gt; than an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L298N&lt;/code&gt; and deliver almost 2 extra volts straight to the wheels!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Wiring a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt; completely correctly except leaving &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; floating (or wiring a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nSLEEP&lt;/code&gt; floating) — the driver stays asleep and outputs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0 V&lt;/code&gt; forever. Tie &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STBY&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nSLEEP&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.3V&lt;/code&gt; (or a software safety-enable GPIO).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If you upgrade later to a 3S LiPo battery (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12.6 V&lt;/code&gt; fully charged), which of the two MOSFET drivers survives? Only the &lt;strong&gt;TB6612FNG&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;13.5 V&lt;/code&gt; max, with caution) — the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10.8 V&lt;/code&gt; max) will burn instantly.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy the chip you can identify, not the first red board in the photo. For Capstone TT motors on 2S, search TB6612 first. An L298N is a valid lab board if you accept the drop and the heatsink. A DRV8833 fits smaller motors. Prices move. Rough bands: L298N module about 35.000–70.000 VND, TB6612 module about 40.000–120.000 VND, DRV8833 breakout about 30.000–80.000 VND.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified L298N page, about 45.000 VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;HShop: &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=DRV8833&quot;&gt;DRV8833&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=L298N&quot;&gt;L298N&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=DRV8833&quot;&gt;DRV8833&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=DRV8833&quot;&gt;DRV8833&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=DRV8833&quot;&gt;DRV8833&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Your module has a bolted heatsink and a 5 V jumper. Motor supply is a 3S pack at 12.6 V. What do you do with the jumper, and where does ESP32 power come from?&lt;/li&gt;
  &lt;li&gt;A TB6612 program sets AIN1, AIN2, and PWMA correctly, and the shaft never moves. Which pin is the first one you check?&lt;/li&gt;
  &lt;li&gt;VM on a DRV8833 board measures 11.1 V from a 3S pack. Is that inside about 2.7–10.6 V?&lt;/li&gt;
  &lt;li&gt;At 1 A, an L298N channel drops 2 V. How much power is the chip turning into heat in that channel?&lt;/li&gt;
  &lt;li&gt;A seller photo shows an MX1508 for a two-TT robot on 2S. What board do you buy for the chassis?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;p&gt;Pull the L298N jumper off above 12 V and power the ESP32 from a separate buck with a common ground. On a silent TB6612, check STBY and drive it high. An 11.1 V pack is above the DRV8833’s about 10.6 V motor range, so that pack does not belong on that chip. One L298N channel at 2 V and 1 A dissipates about 2 W. A two-TT robot on 2S wants a TB6612, not an MX1508 or L9110.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;TI DRV8833 datasheet (VM range and IN/IN interface): &lt;a href=&quot;https://www.ti.com/lit/ds/symlink/drv8833.pdf&quot;&gt;https://www.ti.com/lit/ds/symlink/drv8833.pdf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Pololu TB6612FNG carrier (STBY, VM, and current): &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;https://www.pololu.com/product/713&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Pololu DRV8833 carrier: &lt;a href=&quot;https://www.pololu.com/product/2130&quot;&gt;https://www.pololu.com/product/2130&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;ST L298 datasheet: &lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;https://www.st.com/resource/en/datasheet/l298.pdf&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>DC motors and H-bridges</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter05/05_01_dc_hbridge/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter05/05_01_dc_hbridge</id>
   <content type="html">&lt;p&gt;A brushed DC motor is one of the most elegant electromechanical transducers ever invented: apply voltage and it turns electrical power into mechanical torque; spin its shaft by hand and it turns right around into a generator pushing back-EMF against your battery. To reverse its direction and brake cleanly under software control, robotics relies on a four-switch topology called the &lt;strong&gt;H-Bridge&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This lesson connects the physical motor equation \(V = I R + K_e \omega\) to the four switching modes of an H-bridge (forward, reverse, coast, brake) — including the one forbidden switch combination that vaporizes transistors.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;A brushed DC motor is the actuator you will bolt onto Capstone A. This lesson explains what the commutator is doing inside the can, why the wire gets hottest when the shaft is stuck, and how four switches let a small logic pin reverse a motor that runs from a battery. You will leave with a truth table for the driver on your own bench, written while the wheels are off the ground.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Explain how the commutator keeps shaft torque in one direction as the rotor turns, and why armature current is highest at stall.&lt;/li&gt;
  &lt;li&gt;Name the forward switch pair and the reverse switch pair in an H-bridge, and state which switch combinations short the supply.&lt;/li&gt;
  &lt;li&gt;Compute average voltage from an 8-bit PWM duty on a 7.4 V pack, then account for the roughly 1.5–2 V drop of an L298N.&lt;/li&gt;
  &lt;li&gt;Record a one-motor truth table (IN1, IN2, PWM, observed direction) and connect a swapped motor lead to a later wrong-way yaw command.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can measure DC voltage with a multimeter, you can use Ohm’s law, and you have already driven a GPIO high and low. From the sensor work, you already know that every module on the robot shares one ground. A battery or a current-limited bench supply is the motor source in this lab. The USB cable stays on the programming port of the microcontroller.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-electromechanical-motor-equations-h-bridge-switching-and-shoot-through-protection&quot;&gt;Key ideas: Electromechanical Motor Equations, H-Bridge Switching, and Shoot-Through Protection&lt;/h2&gt;

&lt;p&gt;Three electromechanical fundamentals govern every wheeled DC-motor robot:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Steady-State DC Motor Model:&lt;/strong&gt; Armature voltage balances resistive drop plus speed-proportional back-EMF:
\(V = I R_a + K_e \omega, \qquad \tau = K_t I \quad (\text{in SI units, } K_t = K_e).\)
At stall (\(\omega = 0\)), current peaks at \(I_{\text{stall}} = V / R_a\) and torque peaks at \(\tau_{\text{stall}}\); at no-load speed (\(I \approx 0\)), speed peaks at \(\omega_{\text{nl}} = V / K_e\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;H-Bridge Four-Quadrant Switching:&lt;/strong&gt; Closing diagonal switches &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Q1, Q4)&lt;/code&gt; drives forward; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Q2, Q3)&lt;/code&gt; drives reverse; opening all switches lets the motor &lt;strong&gt;coast&lt;/strong&gt; (slow decay); closing both bottom switches &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Q3, Q4)&lt;/code&gt; shorts the motor terminals for &lt;strong&gt;active dynamic braking&lt;/strong&gt; (fast stop!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Shoot-Through Hazard &amp;amp; Dead-Time:&lt;/strong&gt; Closing both switches on the same vertical leg (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Q1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Q3&lt;/code&gt; simultaneously) shorts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBAT&lt;/code&gt; straight to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;. Integrated drivers (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt;) enforce internal hardware dead-time so software bugs can never cause shoot-through.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Chapter 07 signs two PWM commands, one for the left wheel and one for the right wheel. Positive has to mean “this wheel forward” after you have defined forward as the direction of the robot’s nose. Much later, a ROS 2 node will receive a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;geometry_msgs/Twist&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;linear.x&lt;/code&gt; is the forward speed you asked for, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; is the yaw rate. Your firmware, or a small driver node, splits that twist into the two voltages you practice here. A reversed plug on the right motor turns a positive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; into a yaw the opposite way, and every later controller looks broken while the wiring is simply mirrored. The truth table you write today is the document that keeps that sign honest.&lt;/p&gt;

&lt;h2 id=&quot;the-commutator-keeps-the-torque-facing-one-way&quot;&gt;The commutator keeps the torque facing one way&lt;/h2&gt;

&lt;p&gt;Inside a brushed DC motor the coils sit on the rotor. Two carbon brushes press on a segmented copper commutator. As the shaft turns, those segments swap which side of each coil touches which brush, so the current in the coil that is sitting under a magnet reverses at the right angle. The torque on the shaft therefore keeps pushing the same way. The photograph in this lesson is that copper barrel and the brushes. When you look at the segments, the reversal is a sliding contact you could point at with a pencil.&lt;/p&gt;

&lt;p&gt;The same machine is a generator while it spins. The voltage it produces is the back-EMF, written \(E\), and it grows with speed. It opposes the supply. The current that remains is&lt;/p&gt;

\[I \approx \frac{V_s - E}{R}\]

&lt;p&gt;where \(V_s\) is the voltage actually across the brushes and \(R\) is the resistance of the winding. At stall the shaft is still, so \(E\) is about zero and the supply sees only the copper:&lt;/p&gt;

\[I_{stall} \approx \frac{V}{R}\]

&lt;p&gt;That stall current is the number you hand to the driver choice, the wire gauge, and the fuse. A wheel spinning in the air draws a small no-load current because \(E\) has risen and cancelled most of \(V_s\). The next time a gearbox locks against a curb, the motor electrically returns to the stall case even though your PWM command has not changed.&lt;/p&gt;

&lt;h2 id=&quot;four-switches-and-the-states-that-short-the-battery&quot;&gt;Four switches, and the states that short the battery&lt;/h2&gt;

&lt;p&gt;An H-bridge is four switches drawn like the letter H, with the motor as the crossbar. Call them high-side left, low-side left, high-side right, and low-side right. Forward is high-side left on together with low-side right on. Current enters the left brush and returns to ground through the right brush. Reverse swaps the pair: high-side right and low-side left. The current through the copper flips, and the shaft torque flips with it.&lt;/p&gt;

&lt;p&gt;Both switches in one vertical leg, high and low on the same side, tie battery positive straight to ground. That fault is called shoot-through. It is a short, and it can destroy the driver in milliseconds. Turning both high sides on is in the same family of mistakes: you have left the single path through the motor, and on a real chip that overlap is a short. You do not invent extra states to “see what happens.” Coasting and braking exist, and they are printed in the truth table of the chip you actually bought.&lt;/p&gt;

&lt;p&gt;On some drivers, both inputs low leave the winding open and the wheel freewheels. That is coast. On others, the two low sides (or the two inputs held equal) short the winding through the switches so the motor brakes electrically. The L298N, the TB6612, and the DRV8833 do not share one universal sentence here. You read the table for your board, then you confirm it with the shaft and a piece of tape, because a brake you did not ask for will fight the next PWM command and heat the motor while the robot looks stopped.&lt;/p&gt;

&lt;h2 id=&quot;pwm-on-the-enable-pin&quot;&gt;PWM on the enable pin&lt;/h2&gt;

&lt;p&gt;Direction pins choose the path. A PWM signal on the enable, or on a dedicated PWM pin, chops that path on and off. Ignoring the voltage the silicon itself eats,&lt;/p&gt;

\[V_{avg} \approx D \cdot V_s\]

&lt;p&gt;with duty \(D\) between 0 and 1. An 8-bit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; of 180 means \(D = 180/255\). The motor inductance smooths the chops into a smaller average voltage, which is why the wheel turns slower as the duty falls, until friction eats the last of the torque and the shaft stops even though the duty is not yet zero.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A 2S pack sits near \(V_s = 7.4~\mathrm{V}\). You command 180 counts out of 255 on the enable pin.&lt;/p&gt;

\[D = \frac{180}{255} \approx 0.706\]

\[V_{avg} \approx 0.706 \times 7.4~\mathrm{V} \approx 5.22~\mathrm{V}\]

&lt;p&gt;That 5.22 V is the average before the bridge drop. An L298N is a bipolar Darlington bridge and gives up roughly 1.5–2 V while it conducts, so the brushes may see only about 3.2–3.7 V. A TB6612 is a MOSFET bridge and keeps much more of the 5.22 V for the motor. That is why the TB6612 is the preferred driver for Capstone TT gearmotors on 2S. When a robot on an L298N feels weak at “70 percent throttle,” compute this number before you blame the gearbox.&lt;/p&gt;

&lt;h2 id=&quot;figures&quot;&gt;Figures&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Commutator_and_brushes_DC_motor.jpg&quot; alt=&quot;Segmented commutator and brushes of a brushed DC motor&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The copper segments are the reversing switch. Each brush stays put; the barrel turns under it and hands the coil a new polarity.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;H-bridge concept: one path through the motor, and the leg you must not close&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Trace forward as high-side left plus low-side right. Then find the vertical leg where both switches closed would short the supply. That second picture is the one you refuse to build in firmware.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Lift the wheels off the desk, or run a bare motor in a clamp. Motor current comes from a battery or a bench supply with a current limit, never from the USB port that is talking to the microcontroller. Tie the driver ground to the microcontroller ground with a real wire. Keep fingers off the pinion. Start with PWM at 0.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 or Arduino-class board&lt;/td&gt;
      &lt;td&gt;Two direction pins and one PWM pin&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;One H-bridge module&lt;/td&gt;
      &lt;td&gt;The board you will name in the next lesson&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;One brushed DC motor (TT gearmotor is ideal)&lt;/td&gt;
      &lt;td&gt;The shaft you watch&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2S pack or a 6–9 V current-limited supply&lt;/td&gt;
      &lt;td&gt;VM only&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Multimeter&lt;/td&gt;
      &lt;td&gt;Confirm VM and the common ground&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Tape and a notebook&lt;/td&gt;
      &lt;td&gt;Direction flag and the truth table&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Photograph the wiring. Measure VM and confirm the microcontroller is still on its own USB supply.&lt;/li&gt;
  &lt;li&gt;Set PWM to 0. Walk IN1 and IN2 through 00, 01, 10, and 11. For each row, record whether the shaft is still, turning one way, turning the other way, or braking (hard to turn by hand) versus coasting (easy to turn).&lt;/li&gt;
  &lt;li&gt;Choose the combination you will call forward. Set PWM to 0, 64, 128, 180, and 255. Speed should rise. Write down the triple (IN1, IN2, PWM) that Chapter 07 will treat as positive.&lt;/li&gt;
  &lt;li&gt;With PWM back at a modest value, swap the two motor wires once. Watch the shaft reverse. Restore the wires. Write one sentence: a swapped right-hand plug will reverse the sign of yaw.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; contains a four-row table with columns IN1, IN2, PWM, and observed behavior, plus the forward triple and the 5.22 V calculation for a 180/255 command on 7.4 V if that was your supply. If you used another voltage, recompute \(D \cdot V_s\) for the supply you measured.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What to check&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Shaft dead, driver hot&lt;/td&gt;
      &lt;td&gt;A shoot-through state or a shorted lead. PWM to 0 and remove VM.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Shaft dead, driver cool&lt;/td&gt;
      &lt;td&gt;Enable or STBY held inactive, VM missing, or the PWM GPIO is not the pin you think.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Board resets when the motor starts&lt;/td&gt;
      &lt;td&gt;Motor current is coming through USB. Move VM to the battery and keep grounds common.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Direction opposite your notes&lt;/td&gt;
      &lt;td&gt;Motor plug swapped, or IN1 and IN2 mirrored relative to the table.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Much slower than the duty suggests&lt;/td&gt;
      &lt;td&gt;Bridge drop (especially an L298N) or a battery sagging under load.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-solving-the-linear-torquespeed-curve-of-a-dc-motor-by-hand&quot;&gt;Explanation: Solving the Linear Torque–Speed Curve of a DC Motor by Hand&lt;/h2&gt;

&lt;p&gt;From just two datasheet numbers — rated voltage \(V\), stall current \(I_{\text{stall}}\), and no-load speed \(\omega_{\text{nl}}\) — you can predict a DC motor’s exact current, torque, mechanical power, and efficiency at &lt;em&gt;any&lt;/em&gt; operating speed:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Extract armature resistance and motor constant:&lt;/strong&gt;
\(R_a = \frac{V}{I_{\text{stall}}}, \qquad K_e = K_t = \frac{V}{\omega_{\text{nl}}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute current and torque at operating speed \(\omega\):&lt;/strong&gt;
\(I(\omega) = \frac{V - K_e \omega}{R_a} = I_{\text{stall}}\!\left(1 - \frac{\omega}{\omega_{\text{nl}}}\right), \qquad \tau(\omega) = K_t I(\omega).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute mechanical output power vs. electrical input power:&lt;/strong&gt;
\(P_{\text{mech}} = \tau \omega, \qquad P_{\text{elec}} = V I, \qquad \eta = \frac{P_{\text{mech}}}{P_{\text{elec}}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Recognize the peak-power point:&lt;/strong&gt; Maximum mechanical power always occurs at &lt;strong&gt;half no-load speed&lt;/strong&gt; (\(\omega = \omega_{\text{nl}}/2\)), where efficiency is \(50\%\) — while peak efficiency occurs closer to \(75\text{–}85\%\) of no-load speed!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-operating-point-of-a-6-v-robot-gearmotor-at-half-speed&quot;&gt;Concrete Worked Example: Operating Point of a 6 V Robot Gearmotor at Half Speed&lt;/h3&gt;

&lt;p&gt;Suppose a 6.0 V motor has \(I_{\text{stall}} = 1.50\text{ A}\) and no-load wheel speed \(\omega_{\text{nl}} = 20.0\text{ rad/s}\) (\(\approx 191\text{ RPM}\)):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Motor constants:&lt;/strong&gt;
\(R_a = \frac{6.0}{1.50} = 4.0\,\Omega, \qquad K_e = K_t = \frac{6.0}{20.0} = 0.30\text{ N}\cdot\text{m/A}.\)
Stall torque is \(\tau_{\text{stall}} = 0.30 \times 1.50 = 0.45\text{ N}\cdot\text{m}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Running under load at \(\omega = 12.0\text{ rad/s}\) (60% of no-load speed):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Back-EMF: \(E = 0.30 \times 12.0 = 3.60\text{ V}\).&lt;/li&gt;
      &lt;li&gt;Current: \(I = \frac{6.0 - 3.60}{4.0} = \frac{2.40}{4.0} = 0.60\text{ A}\).&lt;/li&gt;
      &lt;li&gt;Torque: \(\tau = 0.30 \times 0.60 = 0.18\text{ N}\cdot\text{m}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Power and electrical efficiency:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Electrical input: \(P_{\text{elec}} = 6.0 \times 0.60 = 3.60\text{ W}\).&lt;/li&gt;
      &lt;li&gt;Mechanical output: \(P_{\text{mech}} = 0.18 \times 12.0 = 2.16\text{ W}\).&lt;/li&gt;
      &lt;li&gt;Efficiency: \(\eta = \frac{2.16}{3.60} = 60.0\%\) (the remaining \(I^2 R_a = 0.36 \times 4.0 = 1.44\text{ W}\) warms the windings!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Reversing a spinning motor instantly from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+100%&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-100%&lt;/code&gt; PWM — back-EMF now &lt;em&gt;adds&lt;/em&gt; to supply voltage (\(V + E \approx 2V\)!), causing a brief current spike nearly &lt;strong&gt;twice&lt;/strong&gt; normal stall current that can trip fuses or reset the MCU. Ramp through zero or brake briefly first!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If a wheel with radius \(R = 0.0325\text{ m}\) delivers \(\tau = 0.18\text{ N}\cdot\text{m}\), the forward pushing force at the tire contact patch is \(F = \tau / R = 0.18 / 0.0325 \approx 5.54\text{ N}\) (two wheels: \(11.1\text{ N}\) — easily accelerating a 1 kg robot!).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;For this lesson you want one brushed gearmotor and one H-bridge module. A TB6612 module is the better Capstone default; an L298N module is acceptable for the truth-table lab and is easy to find. Prices move. As a rough band in 2026, a yellow TT motor often sits around 25.000–45.000 VND, and a common L298N module around 35.000–70.000 VND.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;HShop search: &lt;a href=&quot;https://hshop.vn/search?q=L298N&quot;&gt;L298N&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/search?q=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Verified L298N module page, about 45.000 VND: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;hshop.vn/mach-dieu-khien-dong-co-dc-l298&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=L298N&quot;&gt;L298N&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=TB6612&quot;&gt;TB6612&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=motor%20TT&quot;&gt;motor TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Lazada: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=L298N&quot;&gt;L298N&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/search?q=L298N&quot;&gt;L298N&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/search?q=TB6612&quot;&gt;TB6612&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A motor on a 7.4 V pack has winding resistance \(5~\Omega\). Estimate the stall current.&lt;/li&gt;
  &lt;li&gt;PWM is \(100/255\) and \(V_s = 6.0~\mathrm{V}\). Compute \(V_{avg}\) before the bridge drop. If the bridge then drops 1.8 V, what is left for the motor?&lt;/li&gt;
  &lt;li&gt;In words, which two switches are on for forward, and which two are on for reverse?&lt;/li&gt;
  &lt;li&gt;Your table says IN1 = 1, IN2 = 0, PWM &amp;gt; 0 is forward. The right-hand motor plug is reversed on the finished robot. What does a positive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;angular.z&lt;/code&gt; do?&lt;/li&gt;
  &lt;li&gt;Why do you write your own coast-versus-brake row instead of copying a classmate’s L298N table onto a TB6612?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;p&gt;Stall current is \(7.4 / 5 \approx 1.48~\mathrm{A}\). Duty \(100/255 \approx 0.392\) gives about \(2.35~\mathrm{V}\) before drop, and about \(0.55~\mathrm{V}\) after 1.8 V, so the shaft may not turn. Forward is high-side left with low-side right; reverse is high-side right with low-side left. A reversed right plug makes positive yaw turn the wrong way. Coast and brake are properties of that chip’s truth table, so a table from a different driver will lie about the stopped state.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;ST L298 datasheet (truth table, drop, and logic levels): &lt;a href=&quot;https://www.st.com/resource/en/datasheet/l298.pdf&quot;&gt;https://www.st.com/resource/en/datasheet/l298.pdf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Pololu’s TB6612FNG carrier, the MOSFET bridge used as the Capstone default: &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;https://www.pololu.com/product/713&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;SparkFun, on choosing a motor and reading stall versus no-load: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&quot;&gt;https://learn.sparkfun.com/tutorials/motors-and-selecting-the-right-one/all&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 04 lab checklist</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter04/04_07_ch04_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter04/04_07_ch04_lab_checklist</id>
   <content type="html">&lt;p&gt;Sensors are only trustworthy after they have been &lt;strong&gt;calibrated, sign-checked, and logged&lt;/strong&gt;. In Chapter 04 you wired buses, encoders, an IMU, and range sensors. This sign-off checklist validates every sensor stream quantitatively — ensuring right-handed coordinate frames, accurate scale factors, and healthy sample rates before Chapter 05 closes the feedback loop onto motors.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Estimated time: &lt;strong&gt;50–70 minutes&lt;/strong&gt;. This is a sign-off, not a new sensor. You pass when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; holds the evidence below. A memory of having “basically done the lab” does not open Chapter 05.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will judge your own Chapter 04 notes against pass/fail measurements: an I2C address with a photo, an encoder \(N\) from one wheel revolution and the matching meters per count, an at-rest IMU reading or a dated waiver, a ranging table or the 5.8 ms dry-run, and a camera sentence that contains 147 Mbit/s. You will paste a block a classmate could check without asking you what you meant. You will name two failure modes you personally hit or deliberately simulated, and you will recognize a filled log that still fails.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 04-01 through 04-06 are the work being signed. You need your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, the ID sheet from Lesson 04-02, and the numbers those lessons already computed: \(N\) and \(r = 0.0325~\mathrm{m}\) if you used the teaching wheel, the complementary step that lands on 9.889 degrees, the echo of 5.8 ms that is 99.5 cm, the divider at 3.33 V, and the 147 Mbit/s camera budget. Chapter 05 starts motors and H-bridges. It assumes this page is already honest, because a 5 V echo on a GPIO will still be a 5 V echo after a motor driver is wired beside it.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-right-handed-coordinate-frames-rep-103-calibration-and-multi-sensor-synchronization&quot;&gt;Key ideas: Right-Handed Coordinate Frames (REP-103), Calibration, and Multi-Sensor Synchronization&lt;/h2&gt;

&lt;p&gt;Three robotics standards make Chapter 04 sensor data seamlessly compatible with both Capstone A and ROS 2:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;ROS REP-103 Right-Handed Frame Convention:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+X&lt;/code&gt; forward, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+Y&lt;/code&gt; left, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+Z&lt;/code&gt; up; positive yaw &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ω_z&lt;/code&gt; rotates &lt;strong&gt;counter-clockwise&lt;/strong&gt; (left turn). Checking sign conventions now prevents inverted steering bugs in Chapter 05 and SLAM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Scale &amp;amp; Bias Calibration:&lt;/strong&gt; Rolling the robot straight along a 1.00 m tape measure calibrates effective wheel circumference &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m_per_count&lt;/code&gt;; rotating 360° in place calibrates effective track width &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L_eff&lt;/code&gt; (accounting for tire scrub!); holding still calibrates IMU gyro bias.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Health &amp;amp; Rate Verification:&lt;/strong&gt; Confirming actual achieved publish/log rates (e.g., encoders at 100 Hz, IMU at 100 Hz, ToF at 20–30 Hz) with zero &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NaN&lt;/code&gt; or stuck readings completes sensor readiness.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Chapter 07’s teleop firmware commands two wheels and stops on a timeout or a range reading. That range is only as good as the table you sign here, and the optional wheel count in the acceptance log is only as good as \(N\). ROS 2 later publishes the same quantities as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Range&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt;, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Imu&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Image&lt;/code&gt;. A doubled \(N\) makes odometry report half the hallway. A timeout stored as 0 cm says the robot is inside the wall. The camera sentence keeps Chapter 11 from arriving as a surprise 147 Mbit/s USB load.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sensor_package_id.png&quot; alt=&quot;The ID sheet this gate checks: bus, voltage, and which module can wait&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;what-you-paste-into-lab-notesmd&quot;&gt;What you paste into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Copy this block, replace the blanks with your evidence, and delete any row you cannot support. A blank you leave in place is a fail.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Chapter 04 sign-off
Date:
I2C: addresses seen = ____ ; photo file = ____
Encoder: N = ____ counts per output rev (three trials: __, __, __)
         r = ____ m ; meters per count = ____
         forward lead: channel ____  (or &quot;single channel, no direction&quot;)
IMU: |a| = ____ g ; at-rest axis = ____ sign ____
     gyro bias deg/s = (__, __, __)
     OR waiver date ____ and complementary result 9.889 deg checked
Range: 20 cm -&amp;gt; __ cm (err __) ; 40 -&amp;gt; __ (err __) ; 80 -&amp;gt; __ (err __)
       OR dry-run: 5.8 ms -&amp;gt; 99.5 cm ; divider 5*(2/3) = 3.33 V
Camera: deferred to ch.11 ; 640x480 YUYV 30 fps ≈ 147 Mbit/s
Failure 1:
Failure 2:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;pass-and-fail&quot;&gt;Pass and fail&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;I2C.&lt;/strong&gt; At least one address is written (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt; are the usual ones) and a photo shows the module and the wires. “It scanned fine,” with no address and no photo, fails.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Encoder.&lt;/strong&gt; \(N\) is one hand revolution of the output shaft, three trials within one count. Meters per count is \(2\pi r / N\). Teaching wheel: \(r = 0.0325~\mathrm{m}\), circumference \(\approx 0.2042~\mathrm{m}\), so \(N = 20\) means \(0.01021~\mathrm{m}\) per count. The seller’s PPR fails when the counter printed a different integer. A single channel passes only if the notes say direction is unknown.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMU.&lt;/strong&gt; Resting magnitude near 1 g, with axis, sign, and gyro bias in deg/s after the datasheet scale, or a dated waiver. The waiver still shows the complementary step (next \(\theta = 9.889~\mathrm{deg}\)) and \(0.5~\mathrm{deg/s} \times 60~\mathrm{s} = 30~\mathrm{deg}\). Raw LSB printed as degrees fails both paths.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ranging.&lt;/strong&gt; A 20 / 40 / 80 cm table with signed errors. At 40 cm, within 5 cm on flat cardboard is a clean pass; a larger error passes only if the notes name the cause. A missing station fails. A mail-delay dry-run passes only with \(5.8~\mathrm{ms} \rightarrow 99.5~\mathrm{cm}\) and \(5 \times 2/3 \approx 3.33~\mathrm{V}\). “Works to 4 m” is not that page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Camera.&lt;/strong&gt; One sentence: the camera waits until Chapter 11, and a 640×480 YUYV stream at 30 fps is about 147 Mbit/s. Omitting the number fails the row. Buying an ESP32-CAM this week does not pass it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Two failures.&lt;/strong&gt; Each names what you saw and what you changed or will change. “None, everything worked” fails the gate even if the other rows are perfect. Simulating a swapped A/B in the notes, or a cloth target with no echo, counts if you write the numbers.&lt;/p&gt;

&lt;h2 id=&quot;a-log-that-feels-finished-and-still-fails&quot;&gt;A log that feels finished and still fails&lt;/h2&gt;

&lt;p&gt;This is the kind of page that gets returned. The student was not lazy. The student measured the wrong thing, and Chapter 05 would inherit it.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# lab-notes.md — example that does NOT pass
Date: 12 Sep

I2C: scanned fine, no errors. (no address, no photo)

Encoder: seller says 40 PPR so N = 40.
One hand turn looked like about 20 blinks but I kept 40.
r = 0.0325 m
meters per count = 0.2042/40 = 0.0051 m

IMU: flat on the desk, az = 16384 degrees. Gyro 70.

Range: ECHO to GPIO18, no resistors, VCC 5 V.
Readings jumped. I will average them in software.
20 cm station not done. 40 cm sometimes 80.

Camera: ordering ESP32-CAM so Chapter 07 can see the hallway.

Failures: none.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The I2C row has no address and no photo. The encoder saw about 20 counts and wrote \(N = 40\), so meters per count is half of \(0.01021~\mathrm{m}\): 140 counts become \(0.71~\mathrm{m}\) in the log instead of \(1.43~\mathrm{m}\). The IMU prints LSB as degrees. At \(\pm 2~\mathrm{g}\), 16384 is \(+1~\mathrm{g}\); a gyro raw 70 at \(\pm 250~\mathrm{deg/s}\) is \(70/131 \approx 0.53~\mathrm{deg/s}\). ECHO at 5 V with no divider sits outside the GPIO rating; the fix is 1 kΩ / 2 kΩ, about 3.33 V, then a real 20/40/80 table. The camera line never says 147 Mbit/s, and “no failures” hides the doubled \(N\) and the bare echo pin that are already on the page.&lt;/p&gt;

&lt;h2 id=&quot;what-to-repair-before-chapter-05&quot;&gt;What to repair before Chapter 05&lt;/h2&gt;

&lt;p&gt;Chapter 05 adds motor current and a driver that can reset the microcontroller. Do not begin it on a GPIO that has already seen 5 V, or with an \(N\) you know is doubled. Before you sign: put the divider on ECHO (1 kΩ / 2 kΩ, about 3.33 V) or disconnect ECHO; rewrite \(N\) from the hand count and recompute meters per count; rewrite the IMU line in g and deg/s, or file the dated waiver with 9.889 deg and 30 deg of drift; finish the ranging table or the dry-run; replace the camera sentence and leave any ESP32-CAM in the bag; fill two failure lines from what you actually undid. Chapter 05 will not re-ask the echo voltage.&lt;/p&gt;

&lt;h2 id=&quot;lab-the-sign-off-itself&quot;&gt;Lab: the sign-off itself&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;You are editing notes. If you power the board for a new photo, USB only, wheels off the floor, ECHO through the divider. Describe the 5 V fault in writing. Do not reproduce it on a live GPIO.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;The document you sign&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ID photo from Lesson 04-02&lt;/td&gt;
      &lt;td&gt;Address row&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Lesson 04-05 notes&lt;/td&gt;
      &lt;td&gt;Error table or dry-run&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Calculator&lt;/td&gt;
      &lt;td&gt;\(2\pi r/N\) and the 5.8 ms echo, recomputed on this page&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Paste the sign-off block into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; and fill it from the lesson where you measured each row. A late module uses the dated waiver or the dry-run, not a blank.&lt;/li&gt;
  &lt;li&gt;On the same page, recompute meters per count and \(343 \times 0.0058 / 2 = 0.995~\mathrm{m}\).&lt;/li&gt;
  &lt;li&gt;Hold your page next to the failing sample. Any sentence that matches it gets rewritten before you tick the row.&lt;/li&gt;
  &lt;li&gt;Write two failures in the first person, each with a number. Stop there. Motor wiring waits until every row passes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A classmate finds an address, an \(N\), a g-vector or a waiver, a range table or 99.5 cm, the 147 Mbit/s sentence, and two failures, without asking you a question.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually is&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Every cell says “yes”&lt;/td&gt;
      &lt;td&gt;The gate wants a number, an address, or a file name&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;\(N\) copied from the product title&lt;/td&gt;
      &lt;td&gt;The hand count was not allowed to win&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Waiver with no 9.889 and no 30 deg&lt;/td&gt;
      &lt;td&gt;The IMU row is empty&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dry-run with no 3.33 V&lt;/td&gt;
      &lt;td&gt;The divider was skipped on paper too&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Camera sentence with no bit rate&lt;/td&gt;
      &lt;td&gt;The row fails&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-calibrating-wheel-radius-and-track-width-with-a-1-meter-tape-measure&quot;&gt;Explanation: Calibrating Wheel Radius and Track Width with a 1-Meter Tape Measure&lt;/h2&gt;

&lt;p&gt;Even caliper-measured wheel diameters differ slightly from effective rolling diameters under chassis weight and tire compression. Calibrate your robot’s true kinematic constants in four exact steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Linear calibration (1.000 m straight push):&lt;/strong&gt; Push the robot straight along a ruler for exact distance \(D_{\text{true}} = 1.000\text{ m}\) and record average encoder counts \(\bar{c} = (c_L + c_R)/2\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute calibrated meters-per-count:&lt;/strong&gt; \(m_{\text{cal}} = D_{\text{true}} / \bar{c}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Angular calibration (exact \(360^\circ = 2\pi\text{ rad}\) spin):&lt;/strong&gt; Rotate the robot in place one full turn (\(2\pi\) rad) and record \(\Delta s_R - \Delta s_L = (c_R - c_L)\,m_{\text{cal}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute effective track width:&lt;/strong&gt; \(L_{\text{eff}} = \frac{\Delta s_R - \Delta s_L}{2\pi}\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-empirical-odometry-calibration-on-a-desk-robot&quot;&gt;Concrete Worked Example: Empirical Odometry Calibration on a Desk Robot&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Straight 1.000 m roll:&lt;/strong&gt; Encoders record \(c_L = 4\,910\) and \(c_R = 4\,890\) counts (\(\bar{c} = 4\,900\)):
\(m_{\text{cal}} = \frac{1.000\text{ m}}{4\,900\text{ counts}} = 0.00020408\text{ m/count}.\)
(If caliper prediction had been \(0.0002000\text{ m/count}\), this single 10-second calibration eliminates a &lt;strong&gt;2.0%&lt;/strong&gt; distance error!)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Exact \(360^\circ\) counter-clockwise spin in place:&lt;/strong&gt; Encoders record \(c_R = +2\,540\) and \(c_L = -2\,520\):
\(\Delta s_R - \Delta s_L = (2540 - (-2520)) \times 0.00020408 = 5\,060 \times 0.00020408 = 1.0326\text{ m}.\)
\(L_{\text{eff}} = \frac{1.0326}{2\pi} = \frac{1.0326}{6.2832} \approx 0.1643\text{ m} \;\; (164.3\text{ mm}).\)
Notice \(L_{\text{eff}} = 164.3\text{ mm}\) is slightly wider than the caliper-measured &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;160 mm&lt;/code&gt; because wide rubber tires scrub during pivots — using \(L_{\text{eff}}\) makes turning accuracy dramatically sharper!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Calibrating only one wheel and assuming left and right motors/tires are identical — a 1% tire-diameter mismatch causes a robot driving “straight” to drift into a slow curve unless you compare &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c_L&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c_R&lt;/code&gt; separately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; Turn the robot \(90^\circ\) left (counter-clockwise) by hand: right encoder should increase (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;), left encoder should decrease (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;), and IMU &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gyro_z&lt;/code&gt; should spike &lt;strong&gt;positive&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;). If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gyro_z&lt;/code&gt; goes negative, negate it in software so all sensors share one right-handed frame!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Close the chapter with the parts the sign-off still lacks. Skip the camera. Prices move.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Part&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Substitute&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HC-SR04&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-sieu-am-srf04&quot;&gt;Hshop listing&lt;/a&gt;, often about 20.000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;VL53L0X, optional&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VL53L0X&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-tof-laser-radar-vl53l0x&quot;&gt;Hshop VL53L0X&lt;/a&gt;. Ultrasonic alone can pass the gate.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MPU-6050 GY-521&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;Hshop GY-521&lt;/a&gt;, often about 85.000. MPU6500 if the library matches.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TT motor with encoder, a pair&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ TT encoder&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;40.000–90.000 each&lt;/td&gt;
      &lt;td&gt;Measure \(N\); do not trust the title&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=HC-SR04&quot;&gt;Hshop: HC-SR04&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=MPU6050%20GY-521&quot;&gt;Shopee: MPU6050&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=dong%20co%20TT%20encoder&quot;&gt;Lazada: động cơ TT encoder&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=VL53L0X&quot;&gt;Thế Giới IC: VL53L0X&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;p&gt;Use the failing log and the lesson numbers. Short answers.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The failing log sets \(N = 40\) with \(r = 0.0325~\mathrm{m}\) after the hand turn showed about 20 counts. What meters per count did they write, what should they write, and how far would 140 counts be in each story?&lt;/li&gt;
  &lt;li&gt;ECHO is on a GPIO with no divider. What voltage hits the pin while the echo is high, what two resistors fix it, and what is \(V_{\mathrm{pin}}\)?&lt;/li&gt;
  &lt;li&gt;The IMU waiver says only “module later.” Does it pass? What two results must be written on that dated line?&lt;/li&gt;
  &lt;li&gt;A dry-run ranging page shows “5.8 ms → 99.5 cm” and does not mention the divider. Pass or fail, and what voltage is missing?&lt;/li&gt;
  &lt;li&gt;Which single camera sentence passes, and which sentence in the sample fails it?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;They wrote \(0.0051~\mathrm{m}\) per count; the hand count \(N = 20\) gives \(0.01021~\mathrm{m}\). One hundred forty counts are \(0.71~\mathrm{m}\) in their log and \(1.43~\mathrm{m}\) in the corrected one. 2. 5 V; 1 kΩ from ECHO to the pin and 2 kΩ to ground; \(\approx 3.33~\mathrm{V}\). 3. It fails. The waiver needs the complementary result \(9.889~\mathrm{deg}\) and the drift \(0.5 \times 60 = 30~\mathrm{deg}\). 4. Fail. The missing number is \(3.33~\mathrm{V}\) from \(5 \times 2/3\). 5. A pass defers the camera to Chapter 11 and includes about 147 Mbit/s. “Ordering ESP32-CAM so Chapter 07 can see” fails.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf&quot;&gt;HC-SR04 datasheet (SparkFun mirror)&lt;/a&gt; — why ECHO is a 5 V pulse.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf&quot;&gt;MPU-6050 datasheet&lt;/a&gt; — the scale that turns 16384 into 1 g.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/humble/p/sensor_msgs/msg/Range.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Range&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://docs.ros.org/en/humble/p/nav_msgs/msg/Odometry.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt;&lt;/a&gt; — the two messages this sign-off is protecting.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/i2c.html&quot;&gt;Espressif I2C&lt;/a&gt; — the scan the photo is supposed to match.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Cameras for robots: USB, CSI, bandwidth</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter04/04_06_cameras_robots/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter04/04_06_cameras_robots</id>
   <content type="html">&lt;p&gt;A single camera frame carries more raw information than a thousand ultrasonic pings — color, lanes, AprilTags, doorways, and obstacles — yet that richness comes with a steep engineering tax: &lt;strong&gt;data bandwidth, latency, and projection geometry&lt;/strong&gt;. Plug a 1080p USB webcam into a robot without doing the throughput math, and your CPU spends 95% of its cycles just copying pixels before a single vision algorithm runs.&lt;/p&gt;

&lt;p&gt;This lesson bridges hardware camera interfaces (USB vs. CSI), bandwidth budgeting, and the pinhole camera projection matrix that turns 2D pixels \((u, v)\) into 3D robot geometry.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Estimated time: &lt;strong&gt;60–75 minutes&lt;/strong&gt;. The required work is a bandwidth budget. A camera on the desk is optional, and Chapter 07 does not wait for one.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will compute uncompressed and MJPEG bandwidth for a stated width, height, and frame rate, and you will compare that number with what USB 2.0 actually delivers once it is shared. You will distinguish a UVC webcam (USB, the laptop is the host), a CSI camera (a ribbon that never enters the USB controller), and an ESP32-CAM (JPEG on a board that is a poor Capstone brain). You will write one sentence that defers the camera until perception work, and that sentence will contain the 147 Mbit/s figure so the checklist can see you did the arithmetic.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 04-02’s identification sheet already has a row for “USB plug versus flex cable versus two metal cans.” Chapter 02 is the ESP32 and the Pico, so you know which board is allowed to be the robot’s computer. You can multiply. You do not need OpenCV, a GPU, or a ROS image pipeline. If those words are new, leave them new until Chapter 11.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-image-bandwidth-pinhole-projection-geometry-and-vision-latency&quot;&gt;Key ideas: Image Bandwidth, Pinhole Projection Geometry, and Vision Latency&lt;/h2&gt;

&lt;p&gt;Three computer-vision and systems pillars govern cameras on mobile robots:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Interface Bandwidth (USB 2.0 vs. MIPI CSI-2):&lt;/strong&gt; Raw uncompressed video scales as \(\text{Width} \times \text{Height} \times \text{Bytes/pixel} \times \text{FPS}\). USB 2.0 tops out around ~35 MB/s real payload (requiring MJPEG compression at high resolution), whereas MIPI CSI-2 streams directly into an SBC’s Image Signal Processor (ISP) via DMA with lower CPU load and latency.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pinhole Camera Intrinsic Matrix \(\mathbf{K}\):&lt;/strong&gt; Projects a 3D point \((X, Y, Z)\) in the camera frame onto pixel coordinates \((u, v)\) via focal lengths \((f_x, f_y)\) and optical center \((c_x, c_y)\):
\(u = f_x \frac{X}{Z} + c_x, \qquad v = f_y \frac{Y}{Z} + c_y.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Exposure, Rolling Shutter, and Motion Blur:&lt;/strong&gt; Fast robot turns smear frames unless shutter exposure time \(t_{\text{exp}}\) is short enough that scene features move \(&amp;lt; 1\text{ pixel}\) during exposure.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Capstone A does not need a camera. Chapter 07’s firmware teleops two wheels from a serial command and stops on a timeout or a range reading. A USB video stream on that same laptop is a different project: it competes for USB bandwidth, for CPU, and for your week. Students who block the motor lab on a camera order finish neither.&lt;/p&gt;

&lt;p&gt;When perception does start, the usual ROS 2 path is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam&lt;/code&gt; or another V4L2 node publishing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Image&lt;/code&gt; for raw pixels and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/CompressedImage&lt;/code&gt; for JPEG. The encoding field (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yuyv&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rgb8&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jpeg&lt;/code&gt;) is the same choice as the byte-per-pixel term in the formula below. A CSI camera on a Pi never crosses that USB link, so it does not change the 147 Mbit/s sum, but it also does not plug into an ESP32. Write the deferral sentence now, with the number in it, and keep shopping for the rangefinder and the encoder motors.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sensor_package_id.png&quot; alt=&quot;Which package is a camera: USB plug or flex, not the two cans&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The figure is the identification sheet from Lesson 04-02. The camera row is the wide connector: a USB plug, or a flat flex into a large module. The two aluminum cans are the ultrasonic sensor from the previous lesson. The tiny window is ToF or an IMU, not a scene camera. If your sheet still calls every black rectangle a camera, fix the row before you read a bandwidth number as if it were a distance.&lt;/p&gt;

&lt;h2 id=&quot;bytes-per-second-then-megabits&quot;&gt;Bytes per second, then megabits&lt;/h2&gt;

&lt;p&gt;Uncompressed image bandwidth is&lt;/p&gt;

\[B \approx w \times h \times \mathrm{fps} \times (\text{bytes per pixel}).\]

&lt;p&gt;YUYV is a 4:2:2 format: two bytes per pixel, no JPEG. A 640×480 stream at 30 frames per second is&lt;/p&gt;

\[B = 640 \times 480 \times 2 \times 30 = 18{,}432{,}000~\mathrm{bytes/s}.\]

&lt;p&gt;Times 8,&lt;/p&gt;

\[18{,}432{,}000 \times 8 = 147{,}456{,}000~\mathrm{bit/s} \approx 147~\mathrm{Mbit/s}.\]

&lt;p&gt;USB 2.0’s theoretical ceiling is 480 Mbit/s. Shared with a serial adapter, a hub, and protocol overhead, practical video throughput is often closer to 280–300 Mbit/s. One raw 640×480 stream is already about half of that practical budget. A second raw stream does not fit. Video on USB is isochronous: a late packet is dropped, and you see a torn frame rather than a clean retry.&lt;/p&gt;

&lt;p&gt;MJPEG at about 10:1 on that same frame is&lt;/p&gt;

\[147 / 10 \approx 15~\mathrm{Mbit/s}.\]

&lt;p&gt;That sits comfortably beside a serial console. You pay the cost in decoder CPU on the host. RGB at three bytes per pixel is heavier than YUYV: \(640 \times 480 \times 3 \times 30 = 27{,}648{,}000\) bytes/s, about 221 Mbit/s, which is a single stream already arguing with the rest of the bus.&lt;/p&gt;

&lt;h2 id=&quot;three-ways-a-robot-has-a-camera&quot;&gt;Three ways a robot “has a camera”&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;USB UVC.&lt;/strong&gt; A webcam speaks USB Video Class. On a laptop, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsusb&lt;/code&gt; lists it, and a later V4L2 node opens &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video0&lt;/code&gt;. The host is the computer, not a GPIO. This is the sensible camera for Chapter 11 if the robot’s brain is a Pi or a PC and the ESP32 is only the motor board. It is the stream whose 147 Mbit/s you just computed, unless you ask the camera for MJPEG.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSI.&lt;/strong&gt; A Raspberry Pi camera rides a ribbon into the Pi’s CSI connector. Those pixels do not pass through the USB host controller, so they do not add to the 480 Mbit/s account. They also do not exist on an ESP32 DevKit or a Pico. A Pi camera bought “for the robot” while the robot’s only computer is an ESP32 is a camera with nowhere to plug in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ESP32-CAM, OV2640.&lt;/strong&gt; The module produces JPEG, which is why the wire-level bandwidth looks solved, and then the board itself becomes the problem. The camera claims a large set of pins, the onboard regulator browns out when the radio and the sensor peak together, and you still owe the chassis a motor driver. It can be a side experiment. It is an awkward brain for Capstone A. Do not replace the ESP32 devkit you already blink with this module in order to “be ready for vision.”&lt;/p&gt;

&lt;p&gt;The ESP32 in the course photos is a devkit with USB for serial and free GPIO for TRIG, ECHO, and later PWM. It is not a CSI host. Treat it that way until a later chapter gives you a different computer.&lt;/p&gt;

&lt;h2 id=&quot;lab-the-budget-is-the-assignment&quot;&gt;Lab: the budget is the assignment&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;No motors. If you plug in a USB camera, use the laptop’s USB port, not a GPIO. Do not power an ESP32-CAM from the ESP32 devkit’s 3.3 V pin; the camera board wants its own 5 V and still browns out easily. Do not look for a laser in a webcam. This lab does not include the VL53L0X.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Paper or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;The required bandwidth worksheet&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A USB webcam, if you already own one&lt;/td&gt;
      &lt;td&gt;Optional &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsusb&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Laptop&lt;/td&gt;
      &lt;td&gt;Host for that optional camera&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32-CAM&lt;/td&gt;
      &lt;td&gt;Not required, and not the Capstone brain&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Compute \(640 \times 480 \times 2 \times 30\) in bytes per second and in Mbit/s. You should land on 18,432,000 bytes/s and about 147 Mbit/s. Show the multiplication in the notes.&lt;/li&gt;
  &lt;li&gt;Divide by 10 for a rough MJPEG stream and write about 15 Mbit/s. State that USB 2.0 practical budget is about 280–300 Mbit/s, shared.&lt;/li&gt;
  &lt;li&gt;Decide, in one sentence, that the camera waits until Chapter 11. The sentence must include 147 Mbit/s. An example that passes: “Camera deferred until Chapter 11; one 640×480 YUYV stream is already about 147 Mbit/s.”&lt;/li&gt;
  &lt;li&gt;Optional, only if a camera is plugged in: run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lsusb&lt;/code&gt; and write the line that appeared. If you can see the connector, say USB-A, micro-USB, or a CSI ribbon. If no camera is present, stop at step 3. That is a complete lab.&lt;/li&gt;
  &lt;li&gt;On the Lesson 04-02 ID sheet, point at the camera row and write the connector type you would actually buy later, or “none yet.”&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;The three numbers (18,432,000 bytes/s, 147 Mbit/s, ~15 Mbit/s MJPEG) and the deferral sentence are in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. A missing camera is not a missing lab. A purchased ESP32-CAM is not extra credit toward Chapter 07.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually is&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Bandwidth written as 18 million bits&lt;/td&gt;
      &lt;td&gt;You forgot the ×8 from bytes to bits&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;“USB 2.0 is 480, so two raw streams fit”&lt;/td&gt;
      &lt;td&gt;You used the theoretical ceiling, not ~300 Mbit/s shared&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Torn frames once the serial adapter is on the same hub&lt;/td&gt;
      &lt;td&gt;The practical budget was already tight&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pi camera and an ESP32, nothing on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/video&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;CSI does not plug into that devkit&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chapter 07 blocked on a camera shipment&lt;/td&gt;
      &lt;td&gt;This lesson’s decision was ignored&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-computing-raw-video-bandwidth-and-estimating-target-depth-from-pixels&quot;&gt;Explanation: Computing Raw Video Bandwidth and Estimating Target Depth from Pixels&lt;/h2&gt;

&lt;p&gt;Before choosing a resolution and frame rate for your ROS 2 robot camera, run this 4-step bandwidth and geometry check:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Calculate raw RGB24 bandwidth (3 bytes/pixel):&lt;/strong&gt; \(\text{BW} = W \times H \times 3 \times \text{FPS}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify against bus and Wi-Fi limits:&lt;/strong&gt; Never stream uncompressed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1280×720&lt;/code&gt; video over Wi-Fi; process on-board at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;320×240&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;640×480&lt;/code&gt; (often grayscale = 1 byte/pixel!) and send only compressed overlays or compact detections.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Apply pinhole projection to estimate range \(Z\) from a known-size landmark&lt;/strong&gt; (e.g., an AprilTag of width \(W_{\text{real}}\) spanning \(\Delta u\) pixels):
\(Z = \frac{f_x \cdot W_{\text{real}}}{\Delta u}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check vision-to-wheel latency:&lt;/strong&gt; If detection takes \(50\text{ ms}\) on a robot moving at \(0.6\text{ m/s}\), the target is already \(3\text{ cm}\) closer by the time the command arrives!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-bandwidth-comparison--apriltag-range-estimation-by-hand&quot;&gt;Concrete Worked Example: Bandwidth Comparison &amp;amp; AprilTag Range Estimation by Hand&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Part A — Raw RGB24 bandwidth at 30 FPS:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;640×480&lt;/code&gt; RGB24 @ 30 FPS:&lt;/strong&gt; \(640 \times 480 \times 3 \times 30 = 27\,648\,000\text{ B/s} \approx 26.4\text{ MB/s}\) (near USB 2.0’s ceiling!).&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;320×240&lt;/code&gt; Grayscale (1 B/px) @ 30 FPS:&lt;/strong&gt; \(320 \times 240 \times 1 \times 30 = 2\,304\,000\text{ B/s} \approx 2.20\text{ MB/s}\) — &lt;strong&gt;12× lighter&lt;/strong&gt;, leaving ample CPU for OpenCV and AprilTag detection!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Part B — Estimating distance to a \(10\text{ cm}\) (\(0.10\text{ m}\)) AprilTag:&lt;/strong&gt;
Your calibrated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;640×480&lt;/code&gt; camera has horizontal focal length \(f_x = 500\text{ px}\). The tag spans \(\Delta u = 80\text{ px}\) horizontally in the image:
\(Z = \frac{500\text{ px} \times 0.10\text{ m}}{80\text{ px}} = \frac{50}{80} = 0.625\text{ m} = 62.5\text{ cm}.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Using a 1080p camera on a robot when line-following or tag detection works faster and cleaner at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;320×240&lt;/code&gt; — 4× more pixels along each axis means &lt;strong&gt;16×&lt;/strong&gt; more compute per frame with zero navigation benefit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If the same \(10\text{ cm}\) AprilTag moves twice as far away (\(Z = 1.25\text{ m}\)), its apparent width halves to \(\Delta u = 40\text{ px}\) — proving that stereo/monocular depth precision degrades quadratically with distance!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Do not buy a camera to finish Chapter 04 or to start Chapter 07. If you already know you want one for Chapter 11, a plain UVC webcam is the boring correct purchase. Prices move.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Part&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Substitute&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;USB webcam&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;camera usb robot&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;80.000–200.000&lt;/td&gt;
      &lt;td&gt;Any UVC camera the laptop already sees. Resolution claims above 720p do not change the Capstone plan.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32-CAM&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32-CAM&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;70.000–140.000&lt;/td&gt;
      &lt;td&gt;Optional side board only. Not a substitute for the devkit that drives the motors.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=camera%20usb&quot;&gt;Hshop: camera USB&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=camera%20usb%20robot&quot;&gt;Shopee: camera usb robot&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=ESP32-CAM&quot;&gt;Lazada: ESP32-CAM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=ESP32-CAM&quot;&gt;Thế Giới IC: ESP32-CAM&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Compute the byte rate and the bit rate of 640×480 YUYV at 30 fps.&lt;/li&gt;
  &lt;li&gt;The same frames as MJPEG at about 10:1. Does ~15 Mbit/s fit on a practical 300 Mbit/s USB 2.0 bus that is also carrying a 12 Mbit/s serial link? Does a second raw 147 Mbit/s stream fit beside the first?&lt;/li&gt;
  &lt;li&gt;A smaller mode is 320×240, RGB (3 bytes/pixel), 15 fps. What is \(B\) in Mbit/s, and how does it compare with the 147 Mbit/s stream?&lt;/li&gt;
  &lt;li&gt;Why does a Pi CSI camera not reduce the USB budget, and why is an ESP32-CAM still a poor Chapter 07 brain even though its JPEG is small?&lt;/li&gt;
  &lt;li&gt;Write the one-sentence camera decision the Chapter 04 checklist will look for. Include the 147 Mbit/s figure.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(18{,}432{,}000\) bytes/s, which is \(147.5~\mathrm{Mbit/s}\) (about 147). 2. \(15 + 12\) fits easily. Two raw streams are about 295 Mbit/s before the serial link, which does not fit in a 280–300 Mbit/s practical budget. 3. \(320 \times 240 \times 3 \times 15 = 3{,}456{,}000\) bytes/s \(\approx 27.6~\mathrm{Mbit/s}\), about one fifth of the 640×480 YUYV stream. 4. CSI never enters the USB host, so the USB sum is unchanged, and a Pi ribbon has no socket on an ESP32. ESP32-CAM JPEG is small and the board still spends pins and regulator current the motor brain needs. 5. Any sentence that defers the camera until Chapter 11 and states that 640×480 YUYV at 30 fps is about 147 Mbit/s.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/pixfmt-yuyv.html&quot;&gt;V4L2 YUYV pixel format&lt;/a&gt; — two bytes per pixel, the 2 in the formula.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://index.ros.org/p/usb_cam/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;usb_cam&lt;/code&gt; on ROS 2&lt;/a&gt; — the usual node behind a UVC webcam, publishing images rather than distances.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/humble/p/sensor_msgs/msg/Image.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Image&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://docs.ros.org/en/humble/p/sensor_msgs/msg/CompressedImage.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/CompressedImage&lt;/code&gt;&lt;/a&gt; — raw versus JPEG, which is the 147 versus 15 choice.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/accessories/camera.html&quot;&gt;Raspberry Pi camera documentation&lt;/a&gt; — CSI ribbons, and why they are not an ESP32 peripheral.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://components.espressif.com/components/espressif/esp32-camera&quot;&gt;Espressif &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32-camera&lt;/code&gt; component&lt;/a&gt; — what the OV2640 path actually involves if you ever do use an ESP32-CAM on purpose.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab: ultrasonic and Time-of-Flight ranging</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter04/04_05_ultrasonic_tof/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter04/04_05_ultrasonic_tof</id>
   <content type="html">&lt;p&gt;Bats navigate pitch-black caves using 40 kHz acoustic chirps; autonomous cars map highways using nanosecond laser pulses. Your desk robot gets to compare both physical worlds side by side: an &lt;strong&gt;HC-SR04 ultrasonic module&lt;/strong&gt; measuring sound travel time in microseconds, and a &lt;strong&gt;VL53L0X Time-of-Flight (ToF)&lt;/strong&gt; sensor timing infrared photons at the speed of light.&lt;/p&gt;

&lt;p&gt;This lab teaches the physics, temperature dependence, and failure modes of active ranging sensors — plus how a simple 3-sample median filter eliminates phantom obstacle spikes.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Estimated time: &lt;strong&gt;80–100 minutes&lt;/strong&gt;. This is the ranging lab: a meterstick, a cardboard target, and a table of errors. Wheels stay off the bench.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will compute distance from an echo pulse width with \(d = vt/2\) at \(343~\mathrm{m/s}\), and with the microsecond shortcut \(d_{\mathrm{cm}} \approx t_{\mu\mathrm{s}} \times 0.01715\). You will bring a 5 V HC-SR04 echo down to about 3.33 V before it touches an ESP32 pin, and you will say what a missing echo means (unknown, not zero centimeters). You will explain why two unmodified VL53L0X boards both at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt; cannot share a bus, and you will fill a 20 / 40 / 80 cm error table against tape, or a dry-run sheet that still contains the 5.8 ms example and the divider.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 04-02 identified the two cans and the tiny black window, including the voltage and the bus. Lesson 04-01 is the I2C scan you will need if a VL53L0X is on the bench. Chapter 03 can time a pulse or at least print a number from a library you have read. Chapter 01 is the multimeter on the echo pin. You do not need the motor chapter, and you should not be holding a wheel.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-wave-propagation-physics-acoustic-vs-optical-constraints-and-robust-filtering&quot;&gt;Key ideas: Wave Propagation Physics, Acoustic vs. Optical Constraints, and Robust Filtering&lt;/h2&gt;

&lt;p&gt;Three physical principles separate reliable range sensing from jittery obstacle avoidance:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Round-Trip Time-of-Flight Equation:&lt;/strong&gt; Whether using sound (\(c_{\text{sound}} \approx 343\text{ m/s}\) at \(20^\circ\text{C}\)) or light (\(c_{\text{light}} \approx 3.0 \times 10^8\text{ m/s}\)), the pulse travels to the target and back, so distance is always &lt;strong&gt;half&lt;/strong&gt; the total path:
\(d = \frac{c \cdot t_{\text{echo}}}{2}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Temperature &amp;amp; Surface Dependence:&lt;/strong&gt; Sound speed shifts with air temperature (\(c(T) \approx 331.3 + 0.606\,T\;[^{\circ}\text{C}]\)), and sound bounces away from smooth walls angled \(&amp;gt; 15^\circ\text{–}20^\circ\). Infrared ToF ignores air temperature and handles angled matte walls well, but loses range on black light-absorbing fabric or bright outdoor sunlight.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Non-Linear Spike Filtering (Median vs. Mean):&lt;/strong&gt; Range sensors suffer occasional multi-path outliers (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;40, 41, 320, 40 cm&lt;/code&gt;). A &lt;strong&gt;3- or 5-sample median filter&lt;/strong&gt; rejects single-sample outliers completely without smearing real wall edges, unlike a moving average.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Chapter 07’s teleop firmware is allowed to drive only because something can tell it to stop. On Capstone A that something is a range check in the same loop as the motor command: too close, or no reading, and the PWM goes to zero. The number in that comparison is born here, at a meterstick, not inside a ROS node.&lt;/p&gt;

&lt;p&gt;Later the same reading is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Range&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;radiation_type&lt;/code&gt; ultrasonic or infrared, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;min_range&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_range&lt;/code&gt; set from what you actually trust, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;range&lt;/code&gt; in meters. A 0 in that field must not mean “the wall is touching the bumper” if your HC-SR04 code uses 0 for a timeout. Write the sentinel down now. Two VL53L0X units become two range topics only after XSHUT has given them two addresses. A camera does not replace this lab. The robot has to stop before it can see.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/range_echo_tof.png&quot; alt=&quot;Round-trip echo time, and a ToF reading already in millimeters&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;sound-goes-out-and-comes-back&quot;&gt;Sound goes out and comes back&lt;/h2&gt;

&lt;p&gt;An HC-SR04 burst travels to the target and back, so the distance is half the path:&lt;/p&gt;

\[d = \frac{v t}{2}.\]

&lt;p&gt;At room temperature take \(v \approx 343~\mathrm{m/s}\). An echo that stays high for \(5.8~\mathrm{ms} = 0.0058~\mathrm{s}\) gives&lt;/p&gt;

\[d = \frac{343 \times 0.0058}{2} = 343 \times 0.0029 = 0.995~\mathrm{m} \approx 99.5~\mathrm{cm}.\]

&lt;p&gt;Divide \(343 / 2\) by \(10^{6}\) to work in microseconds and centimeters:&lt;/p&gt;

\[d_{\mathrm{cm}} \approx t_{\mu\mathrm{s}} \times 0.01715.\]

&lt;p&gt;Check the same pulse: \(5800 \times 0.01715 = 99.5~\mathrm{cm}\). One centimeter of range is about \(58~\mu\mathrm{s}\) of echo. A timer that jitters by \(100~\mu\mathrm{s}\) already moves the reading by roughly \(1.7~\mathrm{cm}\). That is a useful size when your 40 cm station is off by a centimeter and you are tempted to blame the meterstick.&lt;/p&gt;

&lt;p&gt;Speed of sound rises about \(0.6~\mathrm{m/s}\) per degree Celsius. The 343 figure is a 20 °C number. At 30 °C, \(v \approx 349~\mathrm{m/s}\), and a formula that still multiplies by 0.01715 reads a true 80 cm target a little short. The exercise at the end makes you compute how short. For this lab, stay with 343 and write the room temperature next to the table if you know it.&lt;/p&gt;

&lt;h2 id=&quot;hc-sr04-the-pulse-and-the-pin-it-must-not-touch&quot;&gt;HC-SR04, the pulse, and the pin it must not touch&lt;/h2&gt;

&lt;p&gt;VCC is 5 V. You raise TRIG for about \(10~\mu\mathrm{s}\). The module emits a short 40 kHz burst and raises ECHO until the echo returns. On the common board, ECHO high is 5 V. An ESP32 GPIO is not a 5 V input. A divider with \(1~\mathrm{k}\Omega\) from ECHO to the pin and \(2~\mathrm{k}\Omega\) from the pin to ground sets&lt;/p&gt;

\[V_{\mathrm{pin}} = 5 \times \frac{2}{3} \approx 3.33~\mathrm{V}.\]

&lt;p&gt;The acoustic cone is roughly 15 degrees. At 80 cm the spot radius is about \(80 \tan 7.5^\circ \approx 10~\mathrm{cm}\), so a door frame inside that spot returns instead of the cardboard you aimed at. Soft cloth swallows 40 kHz and the echo never comes. A corner multipaths. The blanking distance is about 2 cm. Practical readings you will trust on this robot sit under about 1 m, whatever the listing says about 4 m. Four meters is a hard wall, a quiet room, and a fresh 5 V supply.&lt;/p&gt;

&lt;p&gt;If ECHO never rises, the library often returns 0. That 0 means “I do not know,” and Chapter 05’s stop behavior will treat “I do not know” as motors off. Do not clamp a timeout to 400 cm so the number looks friendly.&lt;/p&gt;

&lt;h2 id=&quot;vl53l0x-millimeters-already-computed&quot;&gt;VL53L0X: millimeters, already computed&lt;/h2&gt;

&lt;p&gt;The VL53L0X times a 940 nm laser on the die and hands you millimeters over I2C, default address &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. The field of view is about 25 degrees. A white card is readable out to something like 1.2–2 m; black fabric and sunlit glass are worse. Sunlight adds photons the chip did not send. Glass returns the pane, not the hallway behind it. The figure’s second path is this sensor: there is no echo pin for you to time.&lt;/p&gt;

&lt;p&gt;Two unmodified boards both answer at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. They will ACK on top of each other and the scan shows one address or a dead bus. XSHUT exists so you can hold one chip in reset, assign the other a new address, then release the first. Buying a second module without a spare GPIO for XSHUT does not buy you a second range.&lt;/p&gt;

&lt;h2 id=&quot;lab-three-distances-one-target&quot;&gt;Lab: three distances, one target&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Cardboard, not a person and not a window. Do not stare into the VL53L0X aperture and do not put a phone lens or a magnifier in front of it. The laser is low-power 940 nm, which is a reason to be boring about it, not a reason to look. HC-SR04 VCC is 5 V from a supply that can provide it; do not feed that 5 V into the ESP32’s 3.3 V pin. Wheels are not part of this setup. If a motor driver is already wired, leave it unpowered.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td&gt;The echo you time&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1 kΩ and 2 kΩ&lt;/td&gt;
      &lt;td&gt;Divider on ECHO&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 or Pico&lt;/td&gt;
      &lt;td&gt;TRIG out, ECHO in through the divider&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Meterstick and cardboard about A4 or larger&lt;/td&gt;
      &lt;td&gt;Truth and target&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;VL53L0X breakout&lt;/td&gt;
      &lt;td&gt;Optional second column&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;The error table&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Build the divider before ECHO touches a GPIO. Measure the high level at the pin if you can catch a pulse, or at least measure that the bottom resistor really goes to GND.&lt;/li&gt;
  &lt;li&gt;TRIG gets a 10 µs pulse. Print echo time in microseconds and distance in centimeters using 0.01715. Print them both, so a bad scale is obvious.&lt;/li&gt;
  &lt;li&gt;Tape the meterstick to the bench. Stand the cardboard at 20 cm, then 40 cm, then 80 cm, measured to the face of the cans, not to the breadboard edge. Hold still. Record five readings and write the middle one.&lt;/li&gt;
  &lt;li&gt;Subtract tape from sensor. A column of signed errors is the result. A single “pretty close” is not.&lt;/li&gt;
  &lt;li&gt;If you own a VL53L0X, power it at the logic voltage the breakout allows, confirm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;, and add a column at the same three stations. If you do not own one, the ultrasonic table is the lab. A dry-run, if the module is still in the mail, still computes the 5.8 ms echo (99.5 cm) and the divider (3.33 V) on the same page as the empty table.&lt;/li&gt;
  &lt;li&gt;One extra target: a folded cloth at 40 cm. Write what the ultrasonic reading does. That row is allowed to be “no echo.”&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;A table shaped like this, with your numbers in it:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Tape (cm)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Ultrasonic (cm)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Error (cm)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;ToF (cm), if any&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;20&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;40&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;80&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;On flat cardboard, a healthy HC-SR04 is often within a few centimeters at 40 cm and a bit worse at 80 cm. An error you can explain (aim, cone, cloth, 4.6 V sagging VCC) passes. A missing row does not. Timeout printed as 0 is labeled “timeout,” not “0.0 cm to the wall.”&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually is&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;GPIO warm, readings stuck&lt;/td&gt;
      &lt;td&gt;ECHO wired at 5 V with no divider&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;20 cm station reads ~40 cm&lt;/td&gt;
      &lt;td&gt;You forgot the divide-by-two&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Random spikes near a chair leg&lt;/td&gt;
      &lt;td&gt;The 15-degree cone found a closer object&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cloth reads nothing or doubles&lt;/td&gt;
      &lt;td&gt;Absorption, or a weak multipath&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Two VL53 boards, one address&lt;/td&gt;
      &lt;td&gt;Both still at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;; use XSHUT&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Always 0 indoors, open space&lt;/td&gt;
      &lt;td&gt;Timeout, not a crash. Do not drive on that 0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-computing-ultrasonic-pulse-timing-timeout-caps-and-median-filtering&quot;&gt;Explanation: Computing Ultrasonic Pulse Timing, Timeout Caps, and Median Filtering&lt;/h2&gt;

&lt;p&gt;Why does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pulseIn(ECHO, HIGH)&lt;/code&gt; without a timeout freeze your robot’s entire control loop when facing an open room? Trace the exact 4-step timing calculation:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Convert target distance \(d\) to echo pulse duration:&lt;/strong&gt; At \(T = 30^\circ\text{C}\) (typical Vietnam lab!), \(c = 331.3 + 0.606(30) = 349.5\text{ m/s}\), so:
\(t_{\text{echo}} = \frac{2d}{c}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute the blocking penalty of a missing timeout:&lt;/strong&gt; Arduino’s default &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pulseIn&lt;/code&gt; timeout is &lt;strong&gt;1,000,000 µs (1.0 full second!)&lt;/strong&gt; — if no echo returns, your 100 Hz control loop freezes for 100 consecutive ticks!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cap timeout to your robot’s maximum useful range (e.g., 2.0 m):&lt;/strong&gt; \(t_{\text{timeout}} = \frac{2 \times 2.0}{343} \approx 11\,660\,\mu\text{s} \approx 12\text{ ms}\) (and trigger ultrasonic at 15–20 Hz asynchronously via interrupts, never blocking the 100 Hz PID loop!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Apply a 3-point median filter&lt;/strong&gt; before feeding distance into your FSM obstacle guard.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-temperature-effect-at-30-c--median-vs-mean-outlier-rejection&quot;&gt;Concrete Worked Example: Temperature Effect at 30 °C &amp;amp; Median vs. Mean Outlier Rejection&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Part A — Ultrasonic echo pulse of \(t_{\text{echo}} = 2\,915\,\mu\text{s}\):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Using standard \(20^\circ\text{C}\) speed (\(343\text{ m/s}\)): \(d = \frac{343 \times 0.002915}{2} = 0.4999\text{ m} = 50.0\text{ cm}\).&lt;/li&gt;
      &lt;li&gt;At a warm \(30^\circ\text{C}\) room (\(349.5\text{ m/s}\)): \(d = \frac{349.5 \times 0.002915}{2} = 0.5094\text{ m} = 50.9\text{ cm}\) (nearly 1 cm systematic shift — 1.9%!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Part B — Comparing Mean vs. Median on a 5-sample ToF burst &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[38, 39, 250, 39, 40] cm&lt;/code&gt;:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Mean:&lt;/strong&gt; \((38 + 39 + 250 + 39 + 40)/5 = 406 / 5 = 81.2\text{ cm}\) — completely fools an obstacle detector set at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50 cm&lt;/code&gt;!&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Median (sorted &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[38, 39, 39, 40, 250]&lt;/code&gt;):&lt;/strong&gt; middle element \(= 39.0\text{ cm}\) — 100% immune to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;250 cm&lt;/code&gt; glitch!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Wiring the HC-SR04’s 5 V &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; pin directly into a 3.3 V ESP32 or Pico GPIO without a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 kΩ / 1.8 kΩ&lt;/code&gt; voltage divider — risking permanent pin damage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; How long does light take to travel to a wall &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.5 m&lt;/code&gt; away and back (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.0 m&lt;/code&gt; round-trip) inside a VL53L0X? \(t = 3.0 / (3.0 \times 10^8) = 10\text{ nanoseconds}\) — resolved by on-chip SPAD photon-timing electronics!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The ultrasonic module is the one Capstone A should not start without. The ToF board is the better second sensor if the budget allows, not a replacement you must wait for. Prices move.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Part&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Substitute&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cảm biến siêu âm HC-SR04&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-sieu-am-srf04&quot;&gt;Hshop HC-SR04&lt;/a&gt; has listed about 20.000. A US-100 is a substitute only after you read whether the pin is GPIO or UART.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;VL53L0X&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VL53L0X ToF&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-tof-laser-radar-vl53l0x&quot;&gt;Hshop VL53L0X&lt;/a&gt;. VL53L1X is a different library and a longer range.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1 kΩ, 2 kΩ&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;điện trở 1k 2k&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;10.000–20.000 a strip&lt;/td&gt;
      &lt;td&gt;Do not skip these because the module “worked once”&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=HC-SR04&quot;&gt;Hshop: HC-SR04&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=VL53L0X&quot;&gt;Shopee: VL53L0X&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=cam%20bien%20sieu%20am%20HC-SR04&quot;&gt;Lazada: cảm biến siêu âm&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=HC-SR04&quot;&gt;Thế Giới IC: HC-SR04&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;ECHO is high for 5.8 ms. Find \(d\) in meters and in centimeters, using \(343~\mathrm{m/s}\).&lt;/li&gt;
  &lt;li&gt;A 40 cm station produces an echo of \(2320~\mu\mathrm{s}\). What does \(t \times 0.01715\) report, in centimeters?&lt;/li&gt;
  &lt;li&gt;ECHO swings to 5 V and the pin is an ESP32 GPIO. What voltage reaches the pin with no divider, and what voltage reaches it with 1 kΩ on top and 2 kΩ to ground?&lt;/li&gt;
  &lt;li&gt;Two VL53L0X boards are wired in parallel, neither XSHUT used. Why does the scan fail to show two sensors, and which pin fixes it?&lt;/li&gt;
  &lt;li&gt;Room temperature is 30 °C, so \(v \approx 349~\mathrm{m/s}\). The true distance is 80 cm. How long is the echo, and what distance does a firmware constant of 0.01715 cm/µs report?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(d = 0.995~\mathrm{m} \approx 99.5~\mathrm{cm}\); \(5800 \times 0.01715 = 99.5\). 2. \(2320 \times 0.01715 \approx 39.8~\mathrm{cm}\). 3. 5 V with no divider; \(5 \times 2/3 \approx 3.33~\mathrm{V}\) with the divider. 4. Both ship at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt; and answer together; hold one in reset with XSHUT, readdress the other, then release. 5. \(t = 2 \times 0.80 / 349 \approx 4.58~\mathrm{ms} = 4585~\mu\mathrm{s}\), and \(4585 \times 0.01715 \approx 78.6~\mathrm{cm}\), about 1.4 cm short.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;try-it-in-the-browser-roverkit-assembly-and-obstacle-avoidance-sim&quot;&gt;Try it in the browser: RoverKit assembly and obstacle-avoidance sim&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://roverkit.vercel.app/#parts&quot;&gt;RoverKit&lt;/a&gt; (Dilum Sanjaya) is a browser app: a ten-step 3D assembly guide for a 4WD robot car, plus a 3×3 m driving simulator. The UI has a Vietnamese setting. Treat it as a supplementary lab, not a substitute for the Capstone A kit.&lt;/p&gt;

&lt;h3 id=&quot;parts-mapped-to-the-course&quot;&gt;Parts, mapped to the course&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Brain:&lt;/strong&gt; Arduino UNO R3 or ESP32-WROOM-32 with an expansion board. The course default brain is the ESP32 (lesson 02-01). The 5 V bench board on hand is an Arduino Mega 2560; UNO sketches are largely portable because they share the Arduino API, differing mainly in pin count and memory.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Driver:&lt;/strong&gt; L9110S. The course prefers the TB6612 (Chapter 05). L9110S is weaker and runs hotter when four TT motors pull together.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Motors and wheels:&lt;/strong&gt; four TT gear motors and 65 mm wheels, driven as left/right pairs (skid-steer). Capstone A is two driven wheels plus a caster, not four driven wheels.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sensor:&lt;/strong&gt; an HC-SR04 on an SG90 servo that looks right, then left. That matches this lesson: the module is a 5 V part, and on an ESP32 the Echo line still needs the \(1~\mathrm{k}\Omega\) / \(2~\mathrm{k}\Omega\) divider.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Power and frame:&lt;/strong&gt; two 18650 cells, an LED with a 220 Ω resistor, a 5 V buzzer through an NPN transistor, and three decks on M3 standoffs (the multi-deck layout in lesson 06-01).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;how-auto-mode-decides&quot;&gt;How Auto mode decides&lt;/h3&gt;

&lt;p&gt;In the simulator, drive with WASD, then switch on Auto. The car goes forward and measures the distance ahead. If an obstacle is closer than about 22 cm, or if the reading barely changes for about 1.5 s (the car is stuck), it stops, turns the LED on, and beeps. The servo looks right (about 20°), measures, looks left (about 160°), measures, then returns to centre. Firmware compares the two readings and turns toward the side with more room. If both sides are tight (under about 18 cm), it backs up and scans again. This is an open-loop sense–decide–act cycle: with no encoder or IMU, the car does not know whether it actually turned far enough or whether a wheel slipped.&lt;/p&gt;

&lt;p&gt;The page also shows a program flow chart; click a box to see the matching code.&lt;/p&gt;

&lt;h3 id=&quot;how-to-use-it-with-this-lesson&quot;&gt;How to use it with this lesson&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Open &lt;a href=&quot;https://roverkit.vercel.app/#parts&quot;&gt;RoverKit&lt;/a&gt; and switch the language if you want Vietnamese.&lt;/li&gt;
  &lt;li&gt;Walk through the ten assembly steps and find the HC-SR04, the servo, the Echo divider, and the driver.&lt;/li&gt;
  &lt;li&gt;In the simulator, place a few obstacles, drive manually, then turn on Auto. Compare the roughly 22 cm stop threshold with the bench readings you just took.&lt;/li&gt;
  &lt;li&gt;Watch the beam width: a target off to one side can be seen early, which is the same \(15^\circ\) opening angle described above.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The real ESP32 car is driven over a Wi-Fi WebSocket (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ws://192.168.4.1:81/&lt;/code&gt;). The public HTTPS site blocks &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ws://&lt;/code&gt; connections, so a real-car session has to run the app locally (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm run dev&lt;/code&gt;). As of 2026-11-26 the project had no public source repository.&lt;/p&gt;

&lt;h3 id=&quot;limits-to-keep-in-view&quot;&gt;Limits to keep in view&lt;/h3&gt;

&lt;p&gt;RoverKit is an intro tool, not the Capstone A control model. L9110S is weaker than the driver the course uses. The four-wheel skid-steer chassis has no encoders, so the closed-loop odometry in lessons 04-03 and 05-06 cannot be done on this kit. There is no IMU either. Use it to learn the wiring, the assembly order, and the avoidance logic; on the course robot, measure the encoders, check wheel direction, and keep the power tree as in Chapter 07.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf&quot;&gt;HC-SR04 datasheet (SparkFun mirror)&lt;/a&gt; — TRIG width, the echo, and the 5 V supply.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/vl53l0x.pdf&quot;&gt;ST VL53L0X datasheet&lt;/a&gt; — ranging in millimeters, field of view, and the address.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/2490&quot;&gt;Pololu VL53L0X carrier&lt;/a&gt; — XSHUT and level behavior, useful even on a different breakout.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/humble/p/sensor_msgs/msg/Range.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Range&lt;/code&gt;&lt;/a&gt; — the message Chapter 09 will publish from this table.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>IMU: accelerometer, gyro, fusion intro</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter04/04_04_imu_basics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter04/04_04_imu_basics</id>
   <content type="html">&lt;p&gt;Inside the human inner ear sit two complementary biological instruments: semicircular canals that sense angular rotation rate, and otolith organs that sense gravity’s downward pull. A 6-axis &lt;strong&gt;Inertial Measurement Unit (IMU)&lt;/strong&gt; — packing a 3-axis MEMS gyroscope and 3-axis accelerometer onto a 3 mm chip — mimics that exact biological design. Neither sensor is perfect alone: the gyroscope is silky-smooth short-term but drifts relentlessly over minutes, while the accelerometer never drifts in tilt yet shakes violently with every motor vibration. Fusing them yields rock-solid orientation.&lt;/p&gt;

&lt;p&gt;This lesson introduces IMU physics, bias calibration, and the &lt;strong&gt;complementary filter&lt;/strong&gt; — your first taste of real-time sensor fusion.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Estimated time: &lt;strong&gt;75–90 minutes&lt;/strong&gt;, including one at-rest log and one 90-degree tilt. No motors.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will state what an accelerometer actually reports (specific force, not an angle) and check that a resting board has magnitude about 1 g. You will treat a gyroscope as a rate in degrees per second, and you will compute how many degrees a constant bias becomes after a minute of integration. You will run one step of a complementary filter with the numbers in this lesson, and you will convert an MPU-6050 raw count with the datasheet scale instead of printing the integer as degrees. You will record the at-rest sign of each axis before any tilt formula is allowed near your notes.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 04-01 is the I2C scan: you should already be able to see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt; before you ask a library for acceleration. Lesson 04-02 is how you recognized a GY-521 and did not confuse it with a rangefinder. Chapter 03 is enough firmware to print three numbers on a timer. Trigonometry for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atan2&lt;/code&gt; is assumed. ROS is not.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-mems-inertial-physics-integration-drift-and-complementary-filtering&quot;&gt;Key ideas: MEMS Inertial Physics, Integration Drift, and Complementary Filtering&lt;/h2&gt;

&lt;p&gt;Three core estimation principles explain how IMUs work on mobile robots:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Accelerometer (Specific Force = Gravity − Linear Acceleration):&lt;/strong&gt; At rest, an accelerometer measures the upward reaction to gravity (\(a_z \approx +9.81\text{ m/s}^2\)), giving absolute pitch and roll via \(\text{pitch} = \operatorname{atan2}(-a_x, \sqrt{a_y^2 + a_z^2})\). It is immune to long-term drift, but corrupted by chassis acceleration and motor vibration — and it &lt;em&gt;cannot&lt;/em&gt; measure yaw around vertical gravity!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Gyroscope (Angular Rate Integration):&lt;/strong&gt; Measures \(\omega_x, \omega_y, \omega_z\) (rad/s or deg/s). Integrating \(\theta_{k+1} = \theta_k + (\omega_k - b_g)\,\Delta t\) tracks fast turns with minimal vibration noise, but any uncalibrated bias \(b_g\) integrates linearly into unbounded heading drift: \(\theta_{\text{err}}(t) = b_g \cdot t\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Complementary Filter (Frequency-Domain Fusion):&lt;/strong&gt; Combines high-pass-filtered gyro integration (trusts gyro for fast changes) with low-pass-filtered accelerometer angle (trusts accelerometer for slow gravity reference):
\(\hat{\theta}_k = \alpha\bigl(\hat{\theta}_{k-1} + \omega_k \Delta t\bigr) + (1 - \alpha)\,\theta_{\text{acc},k}, \qquad \alpha \in [0.95, 0.99].\)r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Capstone A can teleop in Chapter 07 with no IMU. The robot’s job there is to start on a command and stop on a timeout or a range reading. A gyro that has drifted is not part of that safety story, and bolting one on does not make the timeout optional.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Imu&lt;/code&gt; comes after that stop already works. The message carries angular velocity in rad/s, linear acceleration in m/s², and an orientation quaternion you should leave unset (covariance −1 on the diagonal) until a filter you trust has earned it. The complementary filter in this lesson is a pitch estimate for your notebook, not that quaternion. Madgwick and Kalman wait until the raw axes, the bias, and the scale are boringly correct. A ROS plot of “degrees” that are still LSB counts is a firmware bug with a nicer font.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/imu_axes.png&quot; alt=&quot;IMU axes: specific force at rest, and a rate about each axis&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;specific-force-is-not-a-tilt-angle&quot;&gt;Specific force is not a tilt angle&lt;/h2&gt;

&lt;p&gt;An accelerometer measures specific force: acceleration relative to free fall. On the table the board is not accelerating in the lab, but the table pushes up on it, so the reading is about 1 g along the axis that points up. The magnitude&lt;/p&gt;

\[\sqrt{a_x^2 + a_y^2 + a_z^2} \approx 1~\mathrm{g}\]

&lt;p&gt;is the check that the scale is sane. It is not yet a pitch angle. Pitch appears only after you assume the long-term acceleration is gravity and you know which way the chip thinks is up.&lt;/p&gt;

&lt;p&gt;State this frame out loud and then measure it: \(x\) forward, \(y\) left, \(z\) up. If the board is flat and \(a_z\) reads \(+g\), a nose-up pitch is&lt;/p&gt;

\[\theta_{\mathrm{acc}} \approx \operatorname{atan2}(-a_x, a_z).\]

&lt;p&gt;Nose up rotates the forward axis toward the sky, the upward push from the table gains a component along \(-x\), and \(-a_x\) becomes positive. Students who paste the formula before they have written the at-rest signs will “calibrate” a level robot to a large angle. If your flat board reads \(a_z \approx -g\), the chip or the breakout is upside down relative to this sentence, and the formula is not yours yet. Record the sign, then flip the axis in the notes, then filter.&lt;/p&gt;

&lt;h2 id=&quot;a-gyro-is-a-rate&quot;&gt;A gyro is a rate&lt;/h2&gt;

&lt;p&gt;The gyroscope reports angular rate, in deg/s or rad/s, about those same axes. It does not report an angle. An angle is something you build by summing rate times time, and every constant error in the rate is a ramp in the angle. A bias of \(0.5~\mathrm{deg/s}\) left in the sum for 60 s becomes&lt;/p&gt;

\[0.5 \times 60 = 30~\mathrm{deg}\]

&lt;p&gt;of fake rotation while the robot never yawed. That is why an at-rest log of the gyro, in deg/s after the scale, is part of the lab. Subtract that bias before you integrate. It will still wander with temperature; subtracting the first reading only removes the bias you had at that minute.&lt;/p&gt;

&lt;h2 id=&quot;one-complementary-step&quot;&gt;One complementary step&lt;/h2&gt;

&lt;p&gt;Over a short time the integrated gyro is the better angle. Over a long time the accelerometer’s tilt is the better angle, because the bias ramp would otherwise win. A complementary filter blends them:&lt;/p&gt;

\[\theta \leftarrow \alpha\,(\theta + \omega \Delta t) + (1 - \alpha)\,\theta_{\mathrm{acc}}.\]

&lt;p&gt;With \(\alpha = 0.98\) and \(\Delta t = 0.01~\mathrm{s}\), the accelerometer is allowed a time constant on the order of \(\Delta t / (1 - \alpha) = 0.5~\mathrm{s}\). Fast motion stays with the gyro. The steady tilt leaks in from \(\theta_{\mathrm{acc}}\).&lt;/p&gt;

&lt;p&gt;Work one step by hand. Previous \(\theta = 10~\mathrm{deg}\), gyro \(\omega = 5~\mathrm{deg/s}\), accelerometer tilt \(\theta_{\mathrm{acc}} = 2~\mathrm{deg}\):&lt;/p&gt;

\[\theta_{\mathrm{gyro}} = 10 + 5 \times 0.01 = 10.05~\mathrm{deg},\]

\[\theta = 0.98 \times 10.05 + 0.02 \times 2 = 9.849 + 0.04 = 9.889~\mathrm{deg}.\]

&lt;p&gt;The estimate moved a little toward the gyro’s new angle and a little toward 2 degrees. It did not jump to 2 degrees, and it did not ignore the accelerometer. Units have to match: if \(\omega\) is in deg/s, \(\theta\) is in degrees. Mixing radians on one side is a silent factor of \(\pi/180\).&lt;/p&gt;

&lt;p&gt;This is the whole fusion story for the course until much later. Do not import a nine-axis library to avoid writing these three lines.&lt;/p&gt;

&lt;h2 id=&quot;mpu-6050-counts-are-not-degrees&quot;&gt;MPU-6050 counts are not degrees&lt;/h2&gt;

&lt;p&gt;The usual breakout is a GY-521, I2C address &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; with AD0 low and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt; with AD0 high. Full-scale ranges are \(\pm 2, 4, 8, 16~\mathrm{g}\) and \(\pm 250\) to \(2000~\mathrm{deg/s}\). The chip returns 16-bit integers. At \(\pm 2~\mathrm{g}\) the accelerometer scale is \(16384~\mathrm{LSB/g}\). A resting \(a_z\) near \(+16384\) is \(+1~\mathrm{g}\), not “16384 degrees.” At \(\pm 250~\mathrm{deg/s}\) the gyro scale is \(131~\mathrm{LSB/(deg/s)}\). A raw gyro reading of 70 is&lt;/p&gt;

\[\omega = \frac{70}{131} \approx 0.53~\mathrm{deg/s},\]

&lt;p&gt;and an hour of trusting the raw 70 as deg/s would be fiction. Set the range in the configuration registers to the range whose sensitivity you divide by. A library that hides the divide is fine only after you have printed both the raw integer and the scaled value once and seen the factor with your own eyes.&lt;/p&gt;

&lt;h2 id=&quot;lab-rest-then-a-right-angle&quot;&gt;Lab: rest, then a right angle&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;USB power only. Do not spin a wheel in this lab. The module stays on the bench. If the chip becomes hot, AD0, VCC, and GND are probably not what you think; unplug before you reflow anything with your fingers.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;GY-521 or other MPU-6050 breakout&lt;/td&gt;
      &lt;td&gt;The IMU&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 or Pico&lt;/td&gt;
      &lt;td&gt;I2C master&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Breadboard, short wires&lt;/td&gt;
      &lt;td&gt;SDA, SCL, 3.3 V, GND&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A book or the bench edge&lt;/td&gt;
      &lt;td&gt;A repeatable 90-degree tilt&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Signs, 1 g check, bias in deg/s&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Scan I2C. You want &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt;, and you want it stable across two scans. No address, no scaling.&lt;/li&gt;
  &lt;li&gt;Configure \(\pm 2~\mathrm{g}\) and \(\pm 250~\mathrm{deg/s}\), or read back whatever the library set, and write the LSB-per-unit numbers next to it.&lt;/li&gt;
  &lt;li&gt;Leave the board flat and still for a few seconds. Log \(a_x, a_y, a_z\) in g and the three gyro axes in deg/s. Magnitude should sit near 1 g. Gyro channels should sit near 0 and not on 0. The average of a second of gyro data is your bias.&lt;/li&gt;
  &lt;li&gt;Tilt the board 90 degrees so the axis you called forward points up. The ~1 g reading should leave \(z\) and appear on \(x\) (or on \(-x\), if your forward axis pointed down). Write which axis swapped. If none swap, you are tilting the wrong way or scaling a register you never configured.&lt;/li&gt;
  &lt;li&gt;If the module has not arrived, do not invent a table. Write a dated waiver, compute the complementary step to 9.889 degrees, and compute the 30 degree drift from 0.5 deg/s. The checklist accepts that waiver. It does not accept a blank line.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;A sentence of the form “flat, \(a_z = +0.98~\mathrm{g}\), $$&lt;/td&gt;
      &lt;td&gt;a&lt;/td&gt;
      &lt;td&gt;= 1.02~\mathrm{g}\(, gyro bias\)(0.4, -0.2, 0.6)~\mathrm{deg/s}\(” using your numbers, plus “90-degree tilt moved the 1 g reading from\)z\(onto\)x$$.” Raw LSB values may sit beside the scaled ones. They may not replace them.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually is&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Magnitude 2 g or 0.5 g at rest&lt;/td&gt;
      &lt;td&gt;Wrong full-scale, so the wrong sensitivity&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Gyro “bias” of tens of deg/s&lt;/td&gt;
      &lt;td&gt;You forgot to divide by 131, or the board is still in your hand&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pitch formula says 180 degrees on a level desk&lt;/td&gt;
      &lt;td&gt;\(a_z\) is negative and you applied \(\operatorname{atan2}(-a_x, a_z)\) anyway&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Scan shows nothing&lt;/td&gt;
      &lt;td&gt;5 V into a 3.3 V module, SDA/SCL swapped, or no common ground&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Two devices and neither ACKs&lt;/td&gt;
      &lt;td&gt;A second MPU at the same address; move AD0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-quantifying-gyro-drift-and-hand-stepping-a-complementary-filter&quot;&gt;Explanation: Quantifying Gyro Drift and Hand-Stepping a Complementary Filter&lt;/h2&gt;

&lt;p&gt;Let’s verify both why stationary gyro bias calibration is mandatory and how a complementary filter behaves over concrete time steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Measure stationary bias \(b_g\) at boot:&lt;/strong&gt; Keep the robot completely still for 1–2 seconds and average 200 gyro samples: \(\hat{b}_g = \frac{1}{N}\sum \omega_i\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute uncalibrated vs. calibrated yaw drift over 60 seconds:&lt;/strong&gt; \(\Delta\theta_{\text{drift}} = b_g \times 60\text{ s}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Apply the complementary filter for tilt (pitch/roll):&lt;/strong&gt; Choose time constant \(\tau = \frac{\alpha\,\Delta t}{1 - \alpha}\) (typically \(0.5\text{–}1.0\text{ s}\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Understand what happens to yaw:&lt;/strong&gt; Because gravity has no horizontal component, a 6-axis IMU alone cannot anchor yaw with an accelerometer — on a wheeled robot, yaw fuses gyro \(\omega_z\) with wheel-encoder \(\Delta\theta\) (or later LiDAR/magnetometer)!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-bias-drift-calculation--2-steps-of-complementary-filtering&quot;&gt;Concrete Worked Example: Bias Drift Calculation &amp;amp; 2 Steps of Complementary Filtering&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Part A — Why 1-second boot calibration matters:&lt;/strong&gt;
Suppose an uncalibrated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050&lt;/code&gt; z-axis gyro has a resting bias of \(b_z = +0.40^\circ/\text{s}\).
    &lt;ul&gt;
      &lt;li&gt;Without calibration, after just 1 minute (60 s) sitting still on the desk, integrated yaw drifts by \(0.40 \times 60 = 24.0^\circ\)!&lt;/li&gt;
      &lt;li&gt;After averaging 200 boot samples and subtracting \(\hat{b}_z\), residual bias drops to \(\approx 0.02^\circ/\text{s}\) — only \(1.2^\circ\) over an entire minute!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Part B — Stepping a complementary filter (\(\alpha = 0.96\), \(\Delta t = 0.01\text{ s}\)):&lt;/strong&gt;
Initial tilt estimate \(\hat{\theta}_0 = 0.00^\circ\). At step 1, gyro reads \(\omega_1 = +50.0^\circ/\text{s}\) (so \(\omega_1\Delta t = +0.50^\circ\)), while accelerometer reads a slightly noisy \(\theta_{\text{acc},1} = +0.80^\circ\):
    &lt;ul&gt;
      &lt;li&gt;Gyro prediction: \(0.00 + 0.50 = 0.50^\circ\).&lt;/li&gt;
      &lt;li&gt;Fused estimate:
\(\hat{\theta}_1 = 0.96(0.50) + 0.04(0.80) = 0.480 + 0.032 = 0.512^\circ.\)&lt;/li&gt;
      &lt;li&gt;Time constant of this filter: \(\tau = \frac{0.96 \times 0.01}{1 - 0.96} = \frac{0.0096}{0.04} = 0.24\text{ s}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Converting degrees/s to radians/s inconsistently — mixing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deg/s&lt;/code&gt; from the gyro datasheet (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;131 LSB/(°/s)&lt;/code&gt; at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;±250 °/s&lt;/code&gt;) directly into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cos(theta)&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sin(theta)&lt;/code&gt; which expect &lt;strong&gt;radians&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 rad = 57.2958°&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050&lt;/code&gt; sits flat on a level table at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;±2g&lt;/code&gt; range (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;16384 LSB/g&lt;/code&gt;), raw readings should sit near &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(ax, ay, az) ≈ (0, 0, +16384)&lt;/code&gt; — giving total magnitude \(\sqrt{a_x^2 + a_y^2 + a_z^2} \approx 1.00\,g = 9.81\text{ m/s}^2\).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;One IMU is enough for the whole course. You do not need a nine-axis compass to finish this lab. Prices move.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Part&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Substitute&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;GY-521 MPU-6050&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;Hshop GY-521&lt;/a&gt; has listed about 85.000. MPU6500 if the example code matches the registers.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dupont leads&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây dupont cái cái&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–30.000&lt;/td&gt;
      &lt;td&gt;The module is unhappy on 20 cm of tangled jumper&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=MPU6050&quot;&gt;Hshop: MPU6050&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=GY-521%20MPU6050&quot;&gt;Shopee: GY-521&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=MPU6050&quot;&gt;Lazada: MPU6050&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=MPU6050&quot;&gt;Thế Giới IC: MPU6050&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Run the complementary step: \(\alpha = 0.98\), \(\Delta t = 0.01~\mathrm{s}\), \(\theta = 10~\mathrm{deg}\), \(\omega = 5~\mathrm{deg/s}\), \(\theta_{\mathrm{acc}} = 2~\mathrm{deg}\). What is the next \(\theta\)?&lt;/li&gt;
  &lt;li&gt;A gyro bias of \(0.5~\mathrm{deg/s}\) is integrated for 60 s. How many degrees of drift is that? How many degrees is a bias of \(0.8~\mathrm{deg/s}\) integrated for 2 minutes?&lt;/li&gt;
  &lt;li&gt;Full scale is \(\pm 2~\mathrm{g}\) (\(16384~\mathrm{LSB/g}\)) and \(\pm 250~\mathrm{deg/s}\) (\(131~\mathrm{LSB/(deg/s)}\)). Raw \(a_z = 16384\) and raw \(\omega_z = 70\). Give both in physical units. Why is printing 16384 as degrees wrong twice?&lt;/li&gt;
  &lt;li&gt;The flat board reads \(a_z = +1~\mathrm{g}\). After a nose-up tilt, \(a_x = -0.50~\mathrm{g}\) and \(a_z = 0.866~\mathrm{g}\). Estimate pitch with \(\operatorname{atan2}(-a_x, a_z)\).&lt;/li&gt;
  &lt;li&gt;The same board, mounted upside down, reads \(a_z = -1~\mathrm{g}\) and \(a_x = 0\) while it is level. What does the unmodified formula return, and what do you write in the notes before you filter?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(\theta_{\mathrm{gyro}} = 10.05\), then \(0.98 \times 10.05 + 0.02 \times 2 = 9.889~\mathrm{deg}\). 2. \(30~\mathrm{deg}\), and \(0.8 \times 120 = 96~\mathrm{deg}\). 3. \(+1.00~\mathrm{g}\) and \(70/131 \approx 0.53~\mathrm{deg/s}\). The integer is LSB, not degrees, and it is an acceleration channel besides. 4. \(\operatorname{atan2}(0.50, 0.866) = 30^\circ\). 5. \(\operatorname{atan2}(0, -1) = 180^\circ\) on a level board. Write the measured at-rest signs and change the axis map before using the formula.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf&quot;&gt;MPU-6000/MPU-6050 datasheet (TDK InvenSense)&lt;/a&gt; — the sensitivity table, 16384 LSB/g and 131 LSB/(deg/s), lives here.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/mpu-6050-hookup-guide/all&quot;&gt;SparkFun MPU-6050 hookup guide&lt;/a&gt; — AD0, the address, and a first raw read.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/humble/p/sensor_msgs/msg/Imu.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Imu&lt;/code&gt;&lt;/a&gt; — rad/s, m/s², and what a covariance of −1 means for orientation.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/i2c.html&quot;&gt;Espressif I2C master&lt;/a&gt; — the bus the GY-521 is actually on.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Encoders and raw odometry counts</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter04/04_03_encoders_counts/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter04/04_03_encoders_counts</id>
   <content type="html">&lt;p&gt;If you blindfolded yourself and walked ten steps forward, turned 90 degrees left, and walked ten more steps, you could estimate where you stood in the room purely by counting footsteps — that is &lt;strong&gt;dead reckoning (odometry)&lt;/strong&gt;. On a differential-drive robot, &lt;strong&gt;quadrature wheel encoders&lt;/strong&gt; are those footsteps, turning every fraction of a millimeter of tire travel into signed integer counts that tell the robot both &lt;em&gt;how fast&lt;/em&gt; each wheel spins and &lt;em&gt;which way&lt;/em&gt; it is turning.&lt;/p&gt;

&lt;p&gt;This lesson unpacks quadrature phase decoding (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A/B&lt;/code&gt; channels), differential-drive forward kinematics, and why encoder quantization sets a hard floor on slow-speed velocity estimation.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Estimated time: &lt;strong&gt;75–90 minutes&lt;/strong&gt;, most of it one careful hand revolution and the arithmetic that turns the count into meters.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can look at the two square waves from a quadrature encoder and say which channel leads, which is how you tell forward from reverse. You can refuse a seller’s “PPR” until you have measured counts per output-shaft revolution with the same decoding your firmware will use. You can turn a count into arc length for a 65 mm wheel, and you can turn a pair of wheel distances into a dead-reckoning step: how far the axle midpoint moved, and by how many radians the heading changed. Those two numbers are what a later &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt; message is built from.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can count edges on a GPIO and print them (Chapter 03), and you can tell a Hall puck on a yellow TT gearbox from a slotted disk (Lesson 04-02). A raw encoder is not the I2C bus from Lesson 04-01; UART is only how the count will leave the board. Use the Chapter 01 multimeter on the encoder’s high level before that wire touches a GPIO. You do not need ROS, and you do not need the wheels powered.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-quadrature-phase-decoding-differential-drive-kinematics-and-velocity-quantization&quot;&gt;Key ideas: Quadrature Phase Decoding, Differential-Drive Kinematics, and Velocity Quantization&lt;/h2&gt;

&lt;p&gt;Three kinematic and signal-processing pillars underlie encoder odometry:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Quadrature Decoding (1×, 2×, 4×):&lt;/strong&gt; Two Hall or optical channels &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; sit \(90^\circ\) out of phase. When &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; leads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt;, the wheel spins forward (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+1&lt;/code&gt;); when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; leads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;, it spins backward (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt;). Counting every rising and falling edge on both channels (4× decoding) quadruples spatial resolution to \(N_{\text{wheel}} = 4 \times \text{PPR} \times G_{\text{gear}}\) counts per wheel revolution.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Differential-Drive Odometry Update in \(\mathrm{SE}(2)\):&lt;/strong&gt; Given left/right arc displacements \(\Delta s_L, \Delta s_R\) over \(\Delta t\), chassis center travel is \(\Delta s = \frac{\Delta s_R + \Delta s_L}{2}\) and heading change is \(\Delta \theta = \frac{\Delta s_R - \Delta s_L}{L}\), updating pose via midpoint integration:
\(x_{k+1} = x_k + \Delta s \cos\!\left(\theta_k + \frac{\Delta\theta}{2}\right), \qquad y_{k+1} = y_k + \Delta s \sin\!\left(\theta_k + \frac{\Delta\theta}{2}\right), \qquad \theta_{k+1} = \theta_k + \Delta\theta.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Velocity Quantization Floor:&lt;/strong&gt; Estimating speed as \(v = \Delta c \cdot (\text{m/count}) / \Delta t\) has a minimum non-zero step of 1 count per \(\Delta t\). If \(\Delta t\) is too short for a low-CPR encoder, velocity looks like a noisy staircase of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0, 1, 0, 1&lt;/code&gt; counts.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Capstone A in Chapter 07 is differential-drive teleop over a serial line, with a timeout that stops the motors. That demo can roll with no encoders at all. The acceptance log is stronger if the same run also records a wheel count, because “it moved” becomes a number you can compare with a tape. The conversion you practice today is the one that log needs.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt; does not invent a new geometry. Firmware, or a ros2_control hardware interface, turns counts into a wheel angle using \(N\), the counts per revolution after decoding. The controller turns that angle into meters with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt; (this lesson’s \(r\)) and into a yaw with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_separation&lt;/code&gt; (the track \(b\)). It publishes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt;. A doubled \(N\) makes that odometry report half the hallway even when the radius is right. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/JointState&lt;/code&gt; is the same counts as a wheel position in radians. Get \(N\) from the wheel in your hand, not from a product title.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/encoder_quadrature.png&quot; alt=&quot;Quadrature channels A and B: the leading edge is the direction&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;two-square-waves&quot;&gt;Two square waves&lt;/h2&gt;

&lt;p&gt;A quadrature encoder puts out two square waves, A and B, about a quarter cycle apart. Direction is which edge leads. A rule you still have to check on your own hardware: if A rises while B is low, call the step forward; if B rises while A is low, call it reverse. Swapping the two wires swaps the sign. That is wiring, not a filter problem.&lt;/p&gt;

&lt;p&gt;A single channel has no partner. Speed from that pin is trustworthy only while the motor command already supplies the sign, and it lies when the wheel coasts or the shaft kicks back during braking.&lt;/p&gt;

&lt;h2 id=&quot;what-ppr-might-mean&quot;&gt;What “PPR” might mean&lt;/h2&gt;

&lt;p&gt;Sometimes PPR means cycles per revolution of one channel: one high and one low, one slot. Sometimes it means counts after decoding. Four-times (4×) decoding counts every edge of both channels, so&lt;/p&gt;

\[N \approx 4 \times (\text{cycles per revolution of one channel}).\]

&lt;p&gt;Many yellow TT motors put the magnetic disk on the motor shaft, behind the gearbox. Listings often say 11 pulses times a gear ratio, or a Hall count “per shaft revolution,” without naming which shaft. Eleven rising edges per motor turn, counted 1×, through a 1:48 gearbox, produce&lt;/p&gt;

\[N = 11 \times 48 = 528\]

&lt;p&gt;counts per output-shaft revolution. Use 11 in the distance formula and every published meter is 48 times too long. The honest \(N\) is a marked tire, turned by hand, through the decoder your code actually runs.&lt;/p&gt;

&lt;h2 id=&quot;distance-per-count&quot;&gt;Distance per count&lt;/h2&gt;

&lt;p&gt;Without slip, one revolution moves the contact patch by \(2\pi r\):&lt;/p&gt;

\[\Delta s = \frac{2\pi r}{N}\Delta c.\]

&lt;p&gt;A wheel of diameter 65 mm has \(r = 0.0325~\mathrm{m}\) and circumference&lt;/p&gt;

\[2\pi r \approx 2 \times 3.1416 \times 0.0325 = 0.2042~\mathrm{m}.\]

&lt;p&gt;If this wheel is a simple slot disk and the firmware counts \(N = 20\) edges per output revolution, one count is&lt;/p&gt;

\[\frac{0.2042}{20} = 0.01021~\mathrm{m} \approx 1.021~\mathrm{cm}.\]

&lt;p&gt;One hundred forty counts have moved&lt;/p&gt;

\[\Delta s = 140 \times 0.01021 \approx 1.43~\mathrm{m}.\]

&lt;p&gt;That \(N = 20\) is a teaching disk, not a promise about a TT listing. If your hand revolution prints 360, or 528, or 1440, that integer replaces 20.&lt;/p&gt;

&lt;h2 id=&quot;one-dead-reckoning-step&quot;&gt;One dead-reckoning step&lt;/h2&gt;

&lt;p&gt;The same pair shows up in Chapter 06 and inside the controller:&lt;/p&gt;

\[\Delta s = \frac{\Delta s_r + \Delta s_l}{2}, \qquad \Delta \theta = \frac{\Delta s_r - \Delta s_l}{b}.\]

&lt;p&gt;\(b\) is the track between contact patches, not a wheel diameter. With \(\Delta s_r = 0.10~\mathrm{m}\), \(\Delta s_l = 0.06~\mathrm{m}\), and \(b = 0.15~\mathrm{m}\),&lt;/p&gt;

\[\Delta s = \frac{0.10 + 0.06}{2} = 0.08~\mathrm{m}, \qquad \Delta \theta = \frac{0.04}{0.15} = 0.267~\mathrm{rad} \approx 15.3^\circ.\]

&lt;p&gt;The axle crept 8 cm and yawed about 15 degrees toward the slower wheel. Slip is absent from the formula. Swap A and B on the right encoder only, and the log shows \(\Delta s_r = -0.10~\mathrm{m}\) for a tire that went forward. The exercises compute how bad that sign error gets.&lt;/p&gt;

&lt;h2 id=&quot;lab-one-revolution-in-the-hand&quot;&gt;Lab: one revolution, in the hand&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Do not power the motors for the counting pass. A TT wheel on a bench will walk. If you already own a driver and you want a powered direction check, put both tires in the air, use a short pulse, and keep fingers off the spokes. If the Hall high level measures 5 V, it needs the same 1 kΩ / 2 kΩ divider Lesson 04-02 used for an echo pin before it may touch an ESP32 GPIO. Measure the high level first.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;One geared motor with an encoder, or a loose slot disk&lt;/td&gt;
      &lt;td&gt;The shaft you turn&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 or Pico&lt;/td&gt;
      &lt;td&gt;Edge counter&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Marker&lt;/td&gt;
      &lt;td&gt;The index mark on the tire&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Notebook&lt;/td&gt;
      &lt;td&gt;\(N\), radius, meters per count&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Driver and battery&lt;/td&gt;
      &lt;td&gt;Optional in-air direction check only&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Mark the tire and a matching point on the gearbox. That pair is one revolution.&lt;/li&gt;
  &lt;li&gt;Leave the motor unpowered. Reset the counter.&lt;/li&gt;
  &lt;li&gt;Turn the output shaft by hand through one revolution, back to the marks. Write the count. Repeat twice. The three integers should agree within one count.&lt;/li&gt;
  &lt;li&gt;That integer is \(N\) for the decoding you just ran. Compute \(2\pi r / N\) with the tire you marked. The 1.021 cm figure appears only when \(N = 20\) and the diameter is 65 mm.&lt;/li&gt;
  &lt;li&gt;Still unpowered, watch which channel rises first while you turn the tire the way the robot would roll forward. A logic analyzer is ideal. Printing “A rise” and “B rise” is enough. One channel means you write “sign comes only from the motor command.”&lt;/li&gt;
  &lt;li&gt;Optional, wheels off the ground: a brief powered spin should make the same channel lead. If it does not, swap A and B in the notes and repeat once.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;Three hand revolutions, one agreed \(N\), a distance-per-count in meters, and one sentence on which channel leads for forward. Paste that block into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. A Chapter 07 line such as “140 counts, about 1.43 m” is allowed only when your measured \(N\) really is 20.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually is&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Count doubles after a decode-mode change&lt;/td&gt;
      &lt;td&gt;You moved from 1× to 2× or 4× and kept the old \(N\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Listing says 40 PPR, hand turn says 20&lt;/td&gt;
      &lt;td&gt;The listing counted edges your code does not count&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Forward command decreases the count&lt;/td&gt;
      &lt;td&gt;A and B are swapped, or the motor wires are backwards; change one&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Count climbs while the wheel is still&lt;/td&gt;
      &lt;td&gt;Bounce, a floating pin, or no common ground&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Distance looks tens of times too long&lt;/td&gt;
      &lt;td&gt;\(N\) is pulses per motor turn; the gearbox was left out&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;same-pwm-why-does-the-car-curve&quot;&gt;Same PWM, why does the car curve?&lt;/h2&gt;

&lt;p&gt;The same sketch and the same PWM on both wheels still leave a differential-drive chassis curving. No two yellow TT gearmotors are identical: tooth backlash, friction, and ordinary tolerance make one shaft a few percent faster. Open-loop control cannot see that; it just keeps sending the same command.&lt;/p&gt;

&lt;p&gt;An encoder gives the robot eyes. A 20-slot disk and a photo-interrupter, the \(N = 20\) teaching disk already used above, are enough to learn the count. On this course’s robot, use the Hall encoder that comes on a TT motor, count on an ESP32 or a Pico, and drive with a TB6612. The short video credited below uses an Arduino and an L298N; that is not the default kit.&lt;/p&gt;

&lt;p&gt;Once you count both wheels at once, the mismatch is visible. After one run the log might show 519 counts on the left and 552 on the right. The right wheel is ahead by \(552 - 519 = 33\) counts, or&lt;/p&gt;

\[\frac{33}{519} \approx 6.4\%\]

&lt;p&gt;faster than the left. Equal PWM with one wheel already pulling away is how a “drive straight” command becomes an arc.&lt;/p&gt;

&lt;p&gt;A quick check, wheels in the air: the same modest duty on both channels, run for three seconds, and compare the two counts. Paste the pair into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. Keep the tires off the bench; a TT wheel will walk.&lt;/p&gt;

&lt;p&gt;Fixing it properly is not a one-time PWM fudge. That is closed-loop speed control on each wheel, the PID in &lt;a href=&quot;/contents/en/chapter08/08_03_pid_tuning/&quot;&gt;lesson 08-03&lt;/a&gt;, and the note in &lt;a href=&quot;/contents/en/chapter08/08_04_diff_kinematics/&quot;&gt;lesson 08-04&lt;/a&gt; on why a straight command becomes a curve.&lt;/p&gt;

&lt;p&gt;Further watching: a short video by LPPS - 1 Robotics Team, &lt;a href=&quot;https://www.facebook.com/reel/1851960802892543&quot;&gt;same power to both wheels, why the car will not drive straight&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;explanation-hand-computing-one-complete-differential-drive-odometry-step&quot;&gt;Explanation: Hand-Computing One Complete Differential-Drive Odometry Step&lt;/h2&gt;

&lt;p&gt;Let’s execute one exact 2D pose update from raw encoder counts so you can verify your firmware’s odometry math line by line:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compute meters per count:&lt;/strong&gt; \(m_{\text{cnt}} = \frac{\pi D}{N_{\text{wheel}}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Convert tick deltas to wheel arc lengths:&lt;/strong&gt; \(\Delta s_L = \Delta c_L \cdot m_{\text{cnt}}\), \(\Delta s_R = \Delta c_R \cdot m_{\text{cnt}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute chassis displacement \(\Delta s\) and rotation \(\Delta \theta\):&lt;/strong&gt; \(\Delta s = (\Delta s_R + \Delta s_L)/2\), \(\Delta\theta = (\Delta s_R - \Delta s_L)/L\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Update pose \((x, y, \theta)\) using midpoint heading \(\theta_{\text{mid}} = \theta_k + \Delta\theta/2\)&lt;/strong&gt; (far more accurate on curves than Euler integration using \(\theta_k\)!).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-exact-pose-update-on-a-left-curving-arc&quot;&gt;Concrete Worked Example: Exact Pose Update on a Left-Curving Arc&lt;/h3&gt;

&lt;p&gt;Robot specs: wheel diameter \(D = 0.065\text{ m}\) (circumference \(\pi D = 0.20420\text{ m}\)), track width \(L = 0.160\text{ m}\), \(N_{\text{wheel}} = 1\,021\) counts/rev (so \(m_{\text{cnt}} = 0.20420 / 1021 = 0.000200\text{ m/count} = 0.2\text{ mm/count}\)).
Initial pose: \((x_0, y_0, \theta_0) = (0.000\text{ m},\, 0.000\text{ m},\, 0.000\text{ rad})\).
Over \(\Delta t = 0.10\text{ s}\), encoders report \(\Delta c_L = 100\) counts and \(\Delta c_R = 140\) counts:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Wheel displacements:&lt;/strong&gt;
\(\Delta s_L = 100 \times 0.0002 = 0.0200\text{ m}, \qquad \Delta s_R = 140 \times 0.0002 = 0.0280\text{ m}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Center travel and heading change:&lt;/strong&gt;
\(\Delta s = \frac{0.0280 + 0.0200}{2} = 0.0240\text{ m}, \qquad \Delta\theta = \frac{0.0280 - 0.0200}{0.160} = \frac{0.0080}{0.160} = +0.0500\text{ rad}\;\; (2.86^\circ\text{ left}).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Midpoint pose update (\(\theta_{\text{mid}} = 0 + 0.025 = 0.025\text{ rad}\), \(\cos 0.025 \approx 0.99969\), \(\sin 0.025 \approx 0.02500\)):&lt;/strong&gt;
\(x_1 = 0 + 0.0240 \times 0.99969 = 0.02399\text{ m},\)
\(y_1 = 0 + 0.0240 \times 0.02500 = 0.00060\text{ m},\)
\(\theta_1 = 0.0500\text{ rad}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 4 — Instantaneous linear and angular velocities:&lt;/strong&gt;
\(v = \frac{0.0240}{0.10} = 0.240\text{ m/s}, \qquad \omega = \frac{0.0500}{0.10} = 0.500\text{ rad/s}.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Reversed sign on one encoder channel (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; swapped on the right wheel) — when driving straight, \(\Delta c_L = +100\) and \(\Delta c_R = -100\), so odometry thinks the robot is spinning furiously in place (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Δs = 0&lt;/code&gt;)! Always spin each wheel forward by hand first and verify both counters increment positively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; At 1 count quantization per \(\Delta t = 0.01\text{ s}\) (100 Hz) with \(m_{\text{cnt}} = 0.0002\text{ m}\), the velocity step size is \(0.0002 / 0.01 = 0.02\text{ m/s}\) — crisp enough for smooth PID velocity control!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy a pair of TT gearmotors that already include a Hall encoder, so both wheels share a gearbox. Prices move.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Part&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Substitute&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;TT motor with Hall encoder&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ TT encoder Hall&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;40.000–90.000 each&lt;/td&gt;
      &lt;td&gt;N20 with encoder if the hole and the shaft (3 mm or 4 mm) match the chassis&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Slot disk and optical pair&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đĩa encoder quang&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;Fine for learning \(N\), awkward as the Capstone drive&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1 kΩ and 2 kΩ&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;điện trở 1k 2k&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;10.000–20.000 a strip&lt;/td&gt;
      &lt;td&gt;Only if the Hall high level measures 5 V&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=dong%20co%20encoder&quot;&gt;Hshop: động cơ encoder&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=dong%20co%20TT%20encoder&quot;&gt;Shopee: động cơ TT encoder&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=encoder%20Hall%20TT&quot;&gt;Lazada: encoder Hall TT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=encoder&quot;&gt;Thế Giới IC: encoder&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Read which shaft the listing’s pulse count belongs to. Then check it with the hand revolution.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;p&gt;Use \(r = 0.0325~\mathrm{m}\) and circumference \(\approx 0.2042~\mathrm{m}\) unless a problem says otherwise.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;\(N = 20\) counts per output revolution. How far does the wheel roll in 140 counts?&lt;/li&gt;
  &lt;li&gt;A hand revolution of that same firmware produced 20 counts, but the student enters the seller’s “40 PPR” as \(N\). What distance do they report for 140 counts, and what is the true distance?&lt;/li&gt;
  &lt;li&gt;The tires really moved \(\Delta s_r = 0.10~\mathrm{m}\) and \(\Delta s_l = 0.06~\mathrm{m}\), with \(b = 0.15~\mathrm{m}\). A and B on the right encoder are swapped, so the log shows \(-0.10~\mathrm{m}\) and \(+0.06~\mathrm{m}\). Compute the reported \(\Delta s\) and \(\Delta \theta\), against the true \(0.08~\mathrm{m}\) and \(0.267~\mathrm{rad}\).&lt;/li&gt;
  &lt;li&gt;One channel counts 80 rising edges forward and 80 more as the wheel rolls back to the start. For that channel, \(N = 20\). What net distance does quadrature report, and what distance do you get if you treat all 160 edges as forward?&lt;/li&gt;
  &lt;li&gt;A TT motor gives 11 rising edges per motor-shaft turn through a 1:48 gearbox, and your code counts those edges (1×) on the wheel shaft’s output. What is \(N\)? If someone uses 11, by what factor is every distance too large?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;\(0.01021~\mathrm{m}\) per count, times 140, is \(1.43~\mathrm{m}\). 2. \(N = 40\) reports \(0.715~\mathrm{m}\), half of the true \(1.43~\mathrm{m}\). 3. Reported \(\Delta s = -0.02~\mathrm{m}\) and \(\Delta \theta = -1.067~\mathrm{rad}\) (about \(-61^\circ\)), instead of \(+0.08~\mathrm{m}\) and \(+15.3^\circ\). 4. Quadrature net count is 0, so net distance is 0. Treating 160 edges as forward gives \(1.63~\mathrm{m}\). 5. \(N = 528\). Using 11 stretches every distance by 48.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/rotary-encoder-breakout-hookup-guide/all&quot;&gt;SparkFun rotary encoder hookup&lt;/a&gt; — which edge leads, and why one channel is only a speed hint.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/3081&quot;&gt;Pololu magnetic encoder pair&lt;/a&gt; — a datasheet that names counts per motor-shaft revolution, which you still multiply by the gearbox.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/humble/p/nav_msgs/msg/Odometry.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nav_msgs/Odometry&lt;/code&gt;&lt;/a&gt; — the pose is the sum of dead-reckoning steps like the one above.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://control.ros.org/humble/doc/ros2_controllers/diff_drive_controller/doc/userdoc.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff_drive_controller&lt;/code&gt;&lt;/a&gt; — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_radius&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wheel_separation&lt;/code&gt; are \(r\) and \(b\); \(N\) belongs in the hardware interface that feeds wheel position.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Sensor package ID: HC-SR04, ToF, IMU, IR, encoders, cameras</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter04/04_02_sensor_package_id/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter04/04_02_sensor_package_id</id>
   <content type="html">&lt;p&gt;Robots do not perceive “walls” or “speed” — they perceive physical transduction phenomena: ultrasonic pressure echoes, infrared photons timed to picoseconds, Coriolis forces on microscopic silicon springs, and magnetic flux reversals across spinning poles. Every sensor has a sweet spot and a blind spot: ultrasonic fails on soft curtains, optical ToF struggles in direct sunlight, and wheel encoders lie the instant a tire slips on tile.&lt;/p&gt;

&lt;p&gt;This lesson builds your mental catalog of robot sensors so you choose complementary modalities (sensor fusion) rather than expecting one cheap module to do everything.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Estimated time: &lt;strong&gt;60–80 minutes&lt;/strong&gt;. This is a bench identification lesson. You can finish it with photos of modules you already own, plus one datasheet tab per module you still need to buy.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will name a module from its shape and pin labels before you wire it, state its supply voltage and its bus (trigger/echo, I2C, analog, or USB), and refuse to power a 5 V echo pin straight into an ESP32. You will fill an ID sheet that Capstone A can reuse, so the robot’s ranging and wheel-count parts are chosen on purpose. You will also say which of these parts must exist before Chapter 07 and which can wait until perception in Chapter 11.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 04-01 (I2C, SPI, UART) and the habit of reading a pinout from Chapter 01. A multimeter. You do not need every module on the desk at once.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-proprioception-vs-exteroception-noise-models-and-complementary-fusion&quot;&gt;Key ideas: Proprioception vs. Exteroception, Noise Models, and Complementary Fusion&lt;/h2&gt;

&lt;p&gt;Three estimation-theory concepts organize every sensor on Capstone A and ROS 2 robots:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Proprioceptive vs. Exteroceptive Sensors:&lt;/strong&gt; &lt;em&gt;Proprioceptive&lt;/em&gt; sensors measure the robot’s internal state (wheel encoders, IMU, battery voltage); &lt;em&gt;exteroceptive&lt;/em&gt; sensors measure the external environment (ultrasonic, ToF laser, IR line sensors, cameras, LiDAR). Robust navigation always fuses both.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Systematic Bias vs. Zero-Mean Noise:&lt;/strong&gt; Every sensor reading follows \(z = h(x) + b + \epsilon\), where \(b\) is calibratable bias (e.g., gyro drift, wheel diameter error) and \(\epsilon \sim \mathcal{N}(0, \sigma^2)\) is random noise. Averaging reduces \(\epsilon\) by \(1/\sqrt{N}\), but &lt;strong&gt;never&lt;/strong&gt; removes bias \(b\)!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Field of View (FoV) &amp;amp; Specular Failure Modes:&lt;/strong&gt; An HC-SR04 emits a wide \(15^\circ\text{–}30^\circ\) acoustic cone (prone to glancing specular reflections off angled walls), whereas a VL53L0X laser projects a tighter infrared cone (~\(25^\circ\)) with faster update rates and millimeter repeatability.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Capstone A needs a way to see that the robot is about to hit a wall, and a way to count wheel rotation if you want the acceptance test to be more than “it moved.” An HC-SR04 or a VL53L0X covers the wall. A geared motor with a magnetic encoder covers the counts. An IMU is optional for the first teleop and becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Imu&lt;/code&gt; later. A camera is a Chapter 11 problem: it consumes USB bandwidth and CPU that a bare ESP32 does not have. Buying a camera now, before the robot stops on a timeout, is how budgets disappear.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/sensor_package_id.png&quot; alt=&quot;Sensor package identification sheet&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;what-your-eyes-should-catch&quot;&gt;What your eyes should catch&lt;/h2&gt;

&lt;p&gt;Look at the metal, the window, and the number of pins. Then read the silkscreen. Then look up the voltage. In that order. A module that “looks like” an HC-SR04 but has five pins may be an RCWL-1601 or a US-100 with a UART mode. The library you paste will not save you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HC-SR04.&lt;/strong&gt; Two aluminum cans on a small PCB, four pins: VCC, TRIG, ECHO, GND. It wants 5 V. TRIG is an input pulse of about 10 µs. ECHO is an output pulse whose width is the round-trip time, and on the common module that pulse is 5 V. An ESP32 GPIO is not a 5 V pin. A divider (1 kΩ from ECHO to the GPIO, 2 kΩ from the GPIO to ground) brings 5 V down to about 3.3 V. The cans are a wide acoustic cone, roughly 15 degrees, so a door frame and a curtain do not read the same.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VL53L0X and cousins.&lt;/strong&gt; A tiny black optical window, often on a purple breakout, pins VIN, GND, SCL, SDA, sometimes XSHUT and GPIO1. The conversation is I2C, default address &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. The number you read is millimeters, already computed inside the chip. VIN on many breakouts accepts 3.3–5 V because a regulator is on the board; the I2C lines still have to match the MCU. Two unmodified VL53L0X boards on one bus share an address and will not both answer. XSHUT exists so you can boot them one at a time and assign new addresses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MPU-6050 / GY-521.&lt;/strong&gt; A small flat PCB, often green, with a metal-can chip and the pins VCC, GND, SCL, SDA, XDA, XCL, AD0, INT. I2C address &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt; depending on AD0. It is an accelerometer plus a gyroscope. It does not measure distance. Students buy it when they meant to buy a rangefinder because both are “robot sensors.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IR reflectance and obstacle boards.&lt;/strong&gt; A clear LED and a black phototransistor side by side, three pins: VCC, GND, and OUT or AO. Digital boards snap high or low when a reflection crosses a potentiometer threshold. Analog boards (AO) need an ADC pin. TCRT5000 line sensors are this family. They are not laser time-of-flight, and they saturate in direct sun.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wheel encoders.&lt;/strong&gt; Either a slotted disk between an optical pair on the motor shaft, or a tiny magnetic puck on the back of a yellow TT gearbox with a Hall sensor. Two signal pins (A and B) mean quadrature and direction. One signal pin means speed only if you already know the sign from the motor driver. Count the pins before you promise odometry.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cameras.&lt;/strong&gt; A wide flex cable (FPC) into a large board (ESP32-CAM, OV2640) or a USB cable (UVC webcam). If you see a USB plug, the host is a computer, not an analog pin. ESP32-CAM boards are awkward Capstone brains: the camera uses many pins, the regulator is easy to brown out, and you still owe the robot a motor driver. Leave them for later.&lt;/p&gt;

&lt;h2 id=&quot;worked-example-the-5-v-echo&quot;&gt;Worked example: the 5 V echo&lt;/h2&gt;

&lt;p&gt;An HC-SR04 echo pin sits at 5 V while the pulse is high. You build a divider with \(R_1 = 1~\mathrm{k}\Omega\) from ECHO to the ESP32 pin and \(R_2 = 2~\mathrm{k}\Omega\) from that pin to ground. The pin voltage is&lt;/p&gt;

\[V_{pin} = 5 \times \frac{R_2}{R_1 + R_2} = 5 \times \frac{2}{3} \approx 3.33~\mathrm{V}.\]

&lt;p&gt;That is inside what an ESP32 input accepts. Skipping the divider “because it worked once” is how a GPIO becomes a heater. A VL53L0X on I2C does not need this divider if the breakout already speaks 3.3 V logic. Different module, different rule. Write the rule on the ID sheet next to the photo.&lt;/p&gt;

&lt;h2 id=&quot;lab-one-page-id-sheet&quot;&gt;Lab: one-page ID sheet&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Identification is done unpowered first. When you do apply power, USB to the MCU only. Do not tie a 5 V sensor VCC to the ESP32 3.3 V pin, and do not tie ECHO to a GPIO until the divider exists.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Any two of: HC-SR04, VL53 module, GY-521, IR board, encoder motor, webcam&lt;/td&gt;
      &lt;td&gt;The parts you will actually buy&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Phone camera&lt;/td&gt;
      &lt;td&gt;The ID sheet&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Multimeter&lt;/td&gt;
      &lt;td&gt;VCC check after power&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Datasheet tab&lt;/td&gt;
      &lt;td&gt;Voltage and bus&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Unpowered, photograph each module so the pins are readable.&lt;/li&gt;
  &lt;li&gt;Fill a row: name, pin labels you can see, guessed bus, guessed voltage, “Capstone now / later”.&lt;/li&gt;
  &lt;li&gt;Open the datasheet or the seller’s schematic and correct the row. Mark what you guessed wrong.&lt;/li&gt;
  &lt;li&gt;Power only a 3.3 V I2C module as in lesson 04-01, or stop at the paper sheet if the part is still in the mail.&lt;/li&gt;
  &lt;li&gt;Store the sheet in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. Chapter 07 will ask for it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;At least two modules with corrected voltage and bus. A written note that HC-SR04 ECHO is 5 V and VL53L0X is I2C at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. A decision: ranging sensor chosen, camera deferred.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Mistake&lt;/th&gt;
      &lt;th&gt;What breaks&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Calling every black window an IMU&lt;/td&gt;
      &lt;td&gt;You I2C-scan a rangefinder, or you try to read distance from a gyro&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5 V into ESP32&lt;/td&gt;
      &lt;td&gt;Hot pin, stuck input, or a dead GPIO&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Two VL53 boards, no XSHUT plan&lt;/td&gt;
      &lt;td&gt;Scan shows one address or none&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Encoder with one channel&lt;/td&gt;
      &lt;td&gt;You cannot tell forward from back&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Buying ESP32-CAM as the Capstone brain&lt;/td&gt;
      &lt;td&gt;Motor pins and brownouts fight the camera&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-computing-optimal-inverse-variance-fusion-of-two-range-sensors&quot;&gt;Explanation: Computing Optimal Inverse-Variance Fusion of Two Range Sensors&lt;/h2&gt;

&lt;p&gt;Suppose your robot has both an ultrasonic sensor (standard deviation \(\sigma_1 = 2.0\text{ cm}\)) and a Time-of-Flight laser sensor (\(\sigma_2 = 0.5\text{ cm}\)) measuring the same wall. How should you combine their readings \(z_1\) and \(z_2\) instead of taking a naive 50/50 average? Follow the 4-step &lt;strong&gt;Minimum-Variance (Kalman/Bayesian) Fusion&lt;/strong&gt; rule:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compute each sensor’s precision (inverse variance):&lt;/strong&gt; \(w_1 = 1/\sigma_1^2\) and \(w_2 = 1/\sigma_2^2\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Form the precision-weighted average:&lt;/strong&gt;
\(\hat{z} = \frac{w_1 z_1 + w_2 z_2}{w_1 + w_2} = \frac{\sigma_2^2 z_1 + \sigma_1^2 z_2}{\sigma_1^2 + \sigma_2^2}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute the fused variance:&lt;/strong&gt; \(\sigma_{\text{fused}}^2 = \frac{1}{w_1 + w_2} = \frac{\sigma_1^2 \sigma_2^2}{\sigma_1^2 + \sigma_2^2}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify improvement:&lt;/strong&gt; \(\sigma_{\text{fused}}\) is &lt;em&gt;strictly smaller&lt;/em&gt; than even the best individual sensor!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-fusing-ultrasonic-z_1--420text-cm-and-tof-z_2--403text-cm&quot;&gt;Concrete Worked Example: Fusing Ultrasonic (\(z_1 = 42.0\text{ cm}\)) and ToF (\(z_2 = 40.3\text{ cm}\))&lt;/h3&gt;

&lt;p&gt;Given \(\sigma_1 = 2.0\text{ cm}\) (\(\sigma_1^2 = 4.0\)) and \(\sigma_2 = 0.5\text{ cm}\) (\(\sigma_2^2 = 0.25\)):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Sensor precisions:&lt;/strong&gt;
\(w_1 = \frac{1}{4.0} = 0.25\text{ cm}^{-2}, \qquad w_2 = \frac{1}{0.25} = 4.00\text{ cm}^{-2}.\)
Notice the ToF sensor earns &lt;strong&gt;16×&lt;/strong&gt; more weight than the ultrasonic sensor!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Fused distance estimate:&lt;/strong&gt;
\(\hat{z} = \frac{0.25(42.0) + 4.00(40.3)}{0.25 + 4.00} = \frac{10.50 + 161.20}{4.25} = \frac{171.70}{4.25} = 40.40\text{ cm}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Fused uncertainty:&lt;/strong&gt;
\(\sigma_{\text{fused}} = \sqrt{\frac{1}{4.25}} \approx 0.485\text{ cm} &amp;lt; 0.50\text{ cm}.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Blindly fusing an outlier (e.g., ultrasonic glancing off an angled wall and returning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;350 cm&lt;/code&gt; while ToF reads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;40 cm&lt;/code&gt;). Always run a &lt;strong&gt;gating check&lt;/strong&gt; (reject a reading if \(\lvert z_1 - z_2 \rvert &amp;gt; 3\sqrt{\sigma_1^2 + \sigma_2^2}\)) before fusing!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If both sensors have equal noise (\(\sigma_1 = \sigma_2 = \sigma\)), the fused formula reduces to the plain average \(\hat{z} = (z_1 + z_2)/2\) with reduced noise \(\sigma_{\text{fused}} = \sigma/\sqrt{2} \approx 0.707\,\sigma\).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy the ranging sensor and, if the budget allows, TT motors that already include encoders. Skip the camera until Chapter 11.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Part&lt;/th&gt;
      &lt;th&gt;Keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Substitute&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cảm biến siêu âm HC-SR04&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-sieu-am-srf04&quot;&gt;Hshop HC-SR04&lt;/a&gt; has listed about 20.000. US-100 is a substitute only if you read its UART/GPIO mode.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;VL53L0X module&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VL53L0X ToF&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-tof-laser-radar-vl53l0x&quot;&gt;Hshop VL53L0X&lt;/a&gt;. VL53L1X is longer range and a different library.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GY-521&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;MPU6500 if the example code matches.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;IR obstacle or TCRT5000&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cảm biến hồng ngoại vật cản&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;8.000–25.000&lt;/td&gt;
      &lt;td&gt;Fine for a line, poor as the only bumper&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TT motor + encoder&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ TT encoder Hall&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;40.000–90.000 each&lt;/td&gt;
      &lt;td&gt;N20 metal-gear with encoder if the chassis holes match; check 3 mm vs 4 mm shaft&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Logic divider resistors&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;điện trở 1k 2k&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;10.000–20.000 a strip&lt;/td&gt;
      &lt;td&gt;Thế Giới IC for loose resistors&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Searches:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=HC-SR04&quot;&gt;Hshop HC-SR04 search&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=dong%20co%20TT%20encoder&quot;&gt;Shopee encoder motor&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=VL53L0X&quot;&gt;Lazada VL53L0X&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=HC-SR04&quot;&gt;Thế Giới IC modules&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A board has two metal cans and the pins VCC, TRIG, ECHO, GND. What voltage does ECHO reach, and what two resistors do you add for an ESP32?&lt;/li&gt;
  &lt;li&gt;A purple board has a black window and SCL/SDA. Which family is it, and what address do you expect the first time you scan?&lt;/li&gt;
  &lt;li&gt;You need to know whether the left wheel rolled forward or backward. Will a single Hall sensor be enough? What do you buy instead?&lt;/li&gt;
  &lt;li&gt;Sort these into “before Capstone A” and “after the robot already teleops”: HC-SR04, USB camera, encoder motor, ESP32-CAM, VL53L0X.&lt;/li&gt;
  &lt;li&gt;Two VL53L0X modules both stay at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. What pin exists to untangle that, and what does a scan look like before you use it?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;About 5 V; 1 kΩ and 2 kΩ as the divider above, about 3.3 V at the pin. 2. Time-of-flight, usually &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. 3. No; buy a quadrature pair (A and B) or accept that direction comes only from the motor command, which misses coasting. 4. Before: HC-SR04 or VL53L0X, and encoders if you want counts. After: USB camera and ESP32-CAM. 5. XSHUT; the scan shows one address or a messy bus until you power them separately and reassign.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf&quot;&gt;HC-SR04 datasheet mirror (SparkFun)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.st.com/resource/en/datasheet/vl53l0x.pdf&quot;&gt;ST VL53L0X datasheet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf&quot;&gt;TDK MPU-6000/6050 datasheet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/2490&quot;&gt;Pololu VL53L0X carrier notes&lt;/a&gt; — address and level behavior, useful even if you buy a different breakout.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Sensor interfaces: I2C, SPI, UART</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter04/04_01_bus_interfaces/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter04/04_01_bus_interfaces</id>
   <content type="html">&lt;p&gt;A modern mobile robot is a miniature city of specialized silicon chips: an IMU measuring rotation at 400 Hz, a laser Time-of-Flight sensor reporting millimeter range, a LiDAR streaming 360-degree scans, and an MCU orchestrating them all. Yet every one of those chips speaks one of just &lt;strong&gt;three serial languages&lt;/strong&gt;: &lt;strong&gt;I2C&lt;/strong&gt;, &lt;strong&gt;SPI&lt;/strong&gt;, or &lt;strong&gt;UART&lt;/strong&gt;. Understanding their electrical physics, addressing schemes, and timing budgets turns “my sensor returns all zeros” from a mystery into a 60-second diagnosis.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Estimated time: &lt;strong&gt;70–90 minutes&lt;/strong&gt;, including one I2C scan on a real board or a careful dry-run if the module has not arrived.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can choose I2C, SPI, or UART for a sensor from its datasheet rather than from habit, wire the shared ground and the pull-ups an open-drain bus actually needs, and run an address scan that prints something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; before you trust a library. You can also explain why Capstone A teleop is a UART conversation, while the IMU and time-of-flight sensor that show up later on the same robot want a shared I2C bus, and why a ROS 2 topic does not replace either of those wires.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can use a multimeter for continuity and DC voltage (Chapter 01), you know which pins on your ESP32 or Pico are 3.3 V GPIO (Chapter 02), and you can blink an LED from firmware (Chapter 03). You do not need ROS installed for this lesson.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-synchronous-vs-asynchronous-buses-open-drain-pull-ups-and-bus-bandwidth&quot;&gt;Key ideas: Synchronous vs. Asynchronous Buses, Open-Drain Pull-Ups, and Bus Bandwidth&lt;/h2&gt;

&lt;p&gt;Three digital-communications pillars govern sensor integration in robotics:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;I2C (2-Wire, Addressed, Open-Drain):&lt;/strong&gt; Uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCL&lt;/code&gt; with pull-up resistors (typically \(2.2\text{–}4.7\text{ k}\Omega\)). Multiple sensors share two pins via 7-bit addresses (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; for MPU6050, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt; for VL53L0X), running at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100 kHz&lt;/code&gt; (standard) or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;400 kHz&lt;/code&gt; (fast mode). Sensitive to long cable capacitance and address collisions.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;SPI (4-Wire, Push-Pull, High-Speed):&lt;/strong&gt; Uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MOSI&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MISO&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SCLK&lt;/code&gt;, plus one dedicated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CS&lt;/code&gt; (chip select) per device. Because outputs are actively driven push-pull, SPI runs cleanly at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1–20 MHz&lt;/code&gt; — ideal for high-rate IMUs, SD logging, and displays.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;UART (2-Wire, Asynchronous, Point-to-Point):&lt;/strong&gt; Uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RX&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RX&lt;/code&gt; ← &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; with no shared clock; both sides must agree on baud rate (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;230400&lt;/code&gt;, etc.). Standard for GPS receivers, 2D LiDARs, and MCU-to-ROS 2 serial bridges.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-for-capstone-a-and-the-ros-path&quot;&gt;Why this matters for Capstone A and the ROS path&lt;/h2&gt;

&lt;p&gt;Capstone A, in Chapter 07, drives a differential robot from a laptop serial line. That line is UART: a baud rate, a crossed TX/RX pair inside the USB chip, and a rule that silence means stop. The same robot, once it is trustworthy, grows an I2C IMU and often an I2C VL53L0X. Chapter 09 will publish those readings as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Imu&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sensor_msgs/Range&lt;/code&gt;, and Chapter 10 may carry them through micro-ROS. If the bus is missing a pull-up or the address is wrong, the ROS graph looks empty for a reason that lives on the breadboard, not in the launch file. Learn the wire first.&lt;/p&gt;

&lt;h2 id=&quot;three-buses-three-contracts&quot;&gt;Three buses, three contracts&lt;/h2&gt;

&lt;p&gt;A digital sensor is a small computer with a published conversation. The datasheet names the conversation. Your job is to match voltage, wires, and speed, then prove the part answers before you ask it for meters or degrees.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I2C&lt;/strong&gt; uses two signals, SDA and SCL, plus power and ground. Both lines are open-drain: a device may pull a line down, and a resistor pulls it up when nobody is talking. Many breakout boards already include 4.7 kΩ or 10 kΩ pull-ups to their own logic voltage. Two modules in parallel make the pull stronger (the resistances combine in parallel). The ESP32’s internal pull-ups are weak, on the order of tens of kilohms, and are a poor substitute at 400 kHz. A practical starting point on a short 3.3 V bus is external 4.7 kΩ to 3.3 V if the modules do not already provide them.&lt;/p&gt;

&lt;p&gt;Each device has a 7-bit address. The MPU-6050 answers at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; when AD0 is low and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt; when AD0 is high. A VL53L0X usually answers at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. The master speaks an address, the slave ACKs by pulling SDA low, and only then does a register read mean anything. Standard mode is 100 kHz; fast mode is 400 kHz. Do not start at 1 MHz because an example sketch did.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SPI&lt;/strong&gt; adds a clock and a chip-select per device: SCLK, MOSI, MISO, and CS, plus power and ground. It is full duplex and much faster, which is why some IMUs offer SPI as well as I2C. The mode is a pair of bits, CPOL and CPHA. Mode 0 means the clock idles low and the slave is sampled on the rising edge. A wrong mode looks like a sensor that is present but returns nonsense. Unlike I2C, SPI has no address byte; the CS pin &lt;em&gt;is&lt;/em&gt; the address. Leave CS high for devices you are not talking to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UART&lt;/strong&gt; has no clock. You agree a baud rate (115200 is the Capstone A default), a frame (8 data bits, no parity, 1 stop bit), and you cross the wires: the host’s TX goes to the MCU’s RX. USB adapters hide that cross inside the cable, which is why a serial monitor “just works” until you wire two boards and connect TX to TX. Ground is still shared. Only one driver should sit on a given TX wire.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/bus_i2c_spi_uart.png&quot; alt=&quot;I2C, SPI, and UART wiring contracts&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;worked-example-one-bus-two-sensors&quot;&gt;Worked example: one bus, two sensors&lt;/h2&gt;

&lt;p&gt;You want a GY-521 (MPU-6050) and a VL53L0X on an ESP32 DevKit. Both are I2C. Both can run from 3.3 V logic. Wire both SDA pins to GPIO 21, both SCL pins to GPIO 22 (confirm the silkscreen; clones differ), both grounds to ESP32 GND, and both VCC pins to 3.3 V. Do not power the VL53 module from 5 V “just in case” if the breakout’s I2C lines are not level-shifted; many small purple modules are 3.3 V parts with a regulator, and the datasheet of &lt;em&gt;that board&lt;/em&gt; decides.&lt;/p&gt;

&lt;p&gt;A scan should print two addresses. If you see only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt;, the time-of-flight board is unpowered, its SDA/SCL are swapped, or its address was changed and you are scanning the wrong range. If you see nothing, check that both modules share ground with the ESP32 before you change a library.&lt;/p&gt;

&lt;p&gt;The same ESP32’s USB port is a separate UART used later for teleop. It does not occupy SDA/SCL. You can scan I2C and still print the result to the serial monitor. That split — I2C for chips on the robot, UART for the human — is the architecture Capstone A keeps.&lt;/p&gt;

&lt;h2 id=&quot;lab-scan-before-you-decode&quot;&gt;Lab: scan before you decode&lt;/h2&gt;

&lt;h3 id=&quot;safety&quot;&gt;Safety&lt;/h3&gt;

&lt;p&gt;Modules in this lab draw tens of milliamps. Do not connect a motor driver to the same breadboard rails yet. If a module becomes hot in a few seconds, unplug USB; a swapped VCC and GND is the usual cause.&lt;/p&gt;

&lt;h3 id=&quot;bom&quot;&gt;BOM&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item&lt;/th&gt;
      &lt;th&gt;Role&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 devkit or Raspberry Pi Pico&lt;/td&gt;
      &lt;td&gt;I2C master&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GY-521 MPU-6050, or any I2C module you already own&lt;/td&gt;
      &lt;td&gt;Known address&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4.7 kΩ resistors ×2&lt;/td&gt;
      &lt;td&gt;Pull-ups, if the module has none&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Breadboard and dupont wires&lt;/td&gt;
      &lt;td&gt;Short bus&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Multimeter&lt;/td&gt;
      &lt;td&gt;Continuity on GND, voltage on VCC&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;steps&quot;&gt;Steps&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Continuity-check that module GND and MCU GND are the same node before you apply power.&lt;/li&gt;
  &lt;li&gt;Measure VCC at the module with USB plugged in. You want about 3.3 V, not 5 V, on a 3.3 V-only breakout.&lt;/li&gt;
  &lt;li&gt;Flash the scan. On ESP32 Arduino:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;Wire.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Wire&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// SDA, SCL — match your board&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;scan&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint8_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;127&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Wire&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;beginTransmission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Wire&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endTransmission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ACK 0x%02X&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;On Pico MicroPython the same idea is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I2C(0, sda=Pin(0), scl=Pin(1), freq=100_000)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i2c.scan()&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Write the addresses you found into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; next to a photo of the wiring.&lt;/li&gt;
  &lt;li&gt;If you have a second I2C device, add it and confirm both ACKs still appear. Two devices with the same address will not both answer cleanly; that is a datasheet problem, not a ROS problem.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;expected-results&quot;&gt;Expected results&lt;/h3&gt;

&lt;p&gt;One module: a single stable address (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; for a typical GY-521 with AD0 grounded). Two different modules: two addresses. The scan still works after you unplug and replug USB.&lt;/p&gt;

&lt;h3 id=&quot;faults&quot;&gt;Faults&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;No ACK&lt;/td&gt;
      &lt;td&gt;GND not shared, SDA/SCL swapped, or VCC dead&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Address appears, then vanishes&lt;/td&gt;
      &lt;td&gt;Loose dupont, or a pull-up to the wrong voltage&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Every address ACKs&lt;/td&gt;
      &lt;td&gt;SDA shorted to GND&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Works at 100 kHz, fails at 400 kHz&lt;/td&gt;
      &lt;td&gt;Long wires or missing pull-ups&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Library says “device not found” but scan works&lt;/td&gt;
      &lt;td&gt;Library uses a different address or different pins&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;explanation-computing-i2c-bus-read-time-and-pull-up-rc-rise-time&quot;&gt;Explanation: Computing I2C Bus Read Time and Pull-Up RC Rise Time&lt;/h2&gt;

&lt;p&gt;Can your 100 Hz control loop read a 14-byte IMU burst over I2C without eating half your CPU budget? Calculate the exact bus time in four steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Count total bit-clocks per I2C byte:&lt;/strong&gt; Each byte requires &lt;strong&gt;9 clock cycles&lt;/strong&gt; (8 data bits + 1 ACK/NACK bit).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Count transaction overhead:&lt;/strong&gt; A standard register read sends &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[START + Addr(W)]&lt;/code&gt; (1 byte) + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[RegAddr]&lt;/code&gt; (1 byte) + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Re-START + Addr(R)]&lt;/code&gt; (1 byte) + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[N data bytes]&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;STOP&lt;/code&gt; \(\approx N + 3\) byte-frames.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Divide by SCL frequency \(f_{\text{SCL}}\):&lt;/strong&gt; \(t_{\text{read}} \approx \frac{9(N + 3)}{f_{\text{SCL}}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check RC rise time on pull-ups:&lt;/strong&gt; With pull-up \(R_p\) and total bus capacitance \(C_b\), rise time is \(t_r \approx 0.847\,R_p C_b\) (must stay \(&amp;lt; 300\text{ ns}\) for 400 kHz fast-mode I2C!).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-reading-14-bytes-from-an-mpu6050-at-100-khz-vs-400-khz&quot;&gt;Concrete Worked Example: Reading 14 Bytes from an MPU6050 at 100 kHz vs. 400 kHz&lt;/h3&gt;

&lt;p&gt;An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050&lt;/code&gt; burst read fetches 6 bytes accelerometer + 2 bytes temperature + 6 bytes gyro \(= 14\text{ bytes}\) (\(N + 3 = 17\) frames \(= 153\text{ clock cycles}\)):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;At 100 kHz standard I2C:&lt;/strong&gt;
\(t_{\text{read}} = \frac{153}{100\,000\text{ Hz}} = 1.53\text{ ms}\) (over 15% of a 10 ms loop spent blocking on one sensor!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;At 400 kHz fast-mode I2C:&lt;/strong&gt;
\(t_{\text{read}} = \frac{153}{400\,000\text{ Hz}} = 0.383\text{ ms}\) — 4× faster, freeing &amp;gt; 1.1 ms every tick!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pull-up RC check with \(R_p = 4.7\text{ k}\Omega\) and wire capacitance \(C_b = 50\text{ pF}\):&lt;/strong&gt;
\(t_r \approx 0.847 \times 4700 \times 50\times 10^{-12} \approx 199\text{ ns} &amp;lt; 300\text{ ns}\) — crisp edges at 400 kHz!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Wiring &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TX&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RX&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RX&lt;/code&gt; on UART (nothing arrives!), or putting two identical &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VL53L0X&lt;/code&gt; sensors on one I2C bus without toggling their &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XSHUT&lt;/code&gt; pins to reassign unique addresses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; Running an I2C scanner sketch that prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt; confirms four things simultaneously: power is good, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; is shared, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDA/SCL&lt;/code&gt; are not swapped, and pull-ups are working.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;mua-ở-việt-nam--where-to-buy-in-vietnam&quot;&gt;Mua ở Việt Nam / Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;You need one I2C module to finish the lab, plus a handful of 4.7 kΩ resistors if your junk box is empty. A logic-level shifter (BSS138 style, four or eight channels) is worth having before you mix a 5 V peripheral onto a 3.3 V ESP32, but you do not need it for a 3.3 V GY-521.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What&lt;/th&gt;
      &lt;th&gt;Search keywords&lt;/th&gt;
      &lt;th&gt;Rough band (VND)&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;GY-521 MPU-6050&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;35.000–90.000&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;Hshop listing&lt;/a&gt; has been around 85.000. MPU6500 is a fine substitute if the library matches.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4.7 kΩ resistors&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;điện trở 4.7k 1/4W&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;10.000–25.000 for a strip&lt;/td&gt;
      &lt;td&gt;Thế Giới IC is the better counter for bare resistors.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Level shifter&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;logic level shifter 3.3 5&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;8.000–25.000&lt;/td&gt;
      &lt;td&gt;Use on 5 V I2C, not as a power supply.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dupont wires&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây dupont đực cái&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;15.000–40.000&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Search, do not trust a random product photo:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://hshop.vn/search?q=MPU6050&quot;&gt;Hshop search&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://shopee.vn/search?keyword=MPU6050%20GY-521&quot;&gt;Shopee search&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.lazada.vn/catalog/?q=MPU6050&quot;&gt;Lazada search&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.thegioiic.com/search?q=tr%E1%BB%9F%20k%C3%A9o&quot;&gt;Thế Giới IC search&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Street prices move. Compare two sellers and prefer a module with pin labels printed on the PCB.&lt;/p&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A board shows SDA, SCL, GND, VCC, and the text “I2C 0x29”. Which bus is it, which single extra part might the bus need, and what should a scan print?&lt;/li&gt;
  &lt;li&gt;Your scan is empty. List the three electrical checks you do before you change a line of code, in order.&lt;/li&gt;
  &lt;li&gt;Two GY-521 boards both have AD0 tied to GND. What happens on one I2C bus, and what is the cheap hardware fix?&lt;/li&gt;
  &lt;li&gt;Capstone teleop uses 115200 baud, 8N1. A friend connects the USB serial adapter’s TX to the ESP32’s TX. What does the ESP32 receive, and how do you fix a &lt;em&gt;discrete&lt;/em&gt; UART between two boards?&lt;/li&gt;
  &lt;li&gt;Why is “the ROS topic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/imu/data&lt;/code&gt; is silent” not the first debugging step on a robot you have not scanned?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;answer-guidance&quot;&gt;Answer guidance&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;I2C, pull-ups to the logic rail if the module lacks them, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x29&lt;/code&gt;. 2. Shared GND, VCC actually present and at the right voltage, SDA/SCL not swapped. 3. They collide at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x68&lt;/code&gt;; lift one AD0 to VCC so it becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x69&lt;/code&gt;, and tell the library. 4. It receives nothing useful; cross TX to RX and share GND. The USB cable already crosses, so do not “fix” a working USB serial by swapping pins in software first. 5. Because the driver may never have ACKed. Scan, then publish.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.nxp.com/docs/en/user-guide/UM10204.pdf&quot;&gt;NXP UM10204, the I2C-bus specification&lt;/a&gt; — open-drain, addressing, and timing in the primary source.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/i2c/all&quot;&gt;SparkFun I2C tutorial&lt;/a&gt; — pull-ups and addresses with wiring photos.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/all&quot;&gt;SparkFun SPI tutorial&lt;/a&gt; — modes and chip-select.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/serial-communication/all&quot;&gt;SparkFun serial communication&lt;/a&gt; — UART frames and baud.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/i2c.html&quot;&gt;Espressif I2C driver notes&lt;/a&gt; — what the ESP32 peripheral actually implements.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 03 lab checklist</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter03/03_06_ch03_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter03/03_06_ch03_lab_checklist</id>
   <content type="html">&lt;p&gt;Chapter 03 bridges bare electronics and intelligent autonomy: your microcontroller can now read buttons cleanly, generate silent high-frequency PWM, capture microsecond-fast edges with ISRs, and orchestrate behavior through a finite state machine. Before connecting real sensors (encoders, IMUs, ToF, cameras) in Chapter 04, this sign-off checklist verifies that your firmware architecture is deterministic, leak-free, and safe.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can close Chapter 03 with evidence rather than with a column of hopeful ticks. You will point at a button serial log, at PWM duty lines or a photograph of the LED at two duties, at a debounce count that matches your presses with error zero, and at a state diagram whose silence arrow is labeled 300 ms. You will write the sentence “PWM is zero in FAULT” only if the code makes that sentence true, and you will reject a checklist that marks PWM done when the only duty write sits in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 1 through 5 are the work this page gates. You need the non-blocking heartbeat that replaced the &lt;a href=&quot;/contents/en/chapter02/02_04_blink_serial_hello/&quot;&gt;Chapter 02 blink&lt;/a&gt;, the pull-up button, the LEDC or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt; breathe, the short ISR, and the IDLE / TELEOP / FAULT machine. Hardware that has not arrived yet does not earn a pass: a dry reading of your own source is allowed only if the files exist and you can quote the timeout and the duty-zero lines from them. “I will add the timeout when the chassis shows up” is a fail.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-firmware-verification-real-time-profiling-and-fault-injection&quot;&gt;Key ideas: Firmware Verification, Real-Time Profiling, and Fault Injection&lt;/h2&gt;

&lt;p&gt;Three verification practices close out embedded firmware bring-up:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Timing &amp;amp; Jitter Profiling:&lt;/strong&gt; Measuring actual loop execution time (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_end - t_start&lt;/code&gt; in microseconds) proves your firmware has ample CPU headroom before adding I2C sensors and filtering math in Chapter 04.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Fault Injection Testing:&lt;/strong&gt; Deliberately triggering every safety path — pressing E-stop, unplugging a command source (watchdog timeout), or simulating low battery — confirms the FSM always enters &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; and zeroes PWM.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Clean State Logging:&lt;/strong&gt; Printing state transitions (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms,OLD_STATE-&amp;gt;NEW_STATE,reason&lt;/code&gt;) creates an auditable trace for diagnosing field behavior in seconds.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Later chapters will talk as if the differential-drive firmware already accepts a teleop command and already drops both PWM channels when that command goes silent for 300 ms. A ROS graph will publish twists on top of that assumption. It will not notice that your button log is a flood of samples, that the duty was written once at boot, or that FAULT still leaves the last &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; in place. The wheels stay up until those facts are in the notes. This checklist is the contract those chapters are allowed to assume, and a tick without an artifact is not a line in the contract.&lt;/p&gt;

&lt;h2 id=&quot;core-teaching&quot;&gt;Core teaching&lt;/h2&gt;

&lt;p&gt;Each gate is an observation plus the artifact that proves it. A feeling that the button “basically works” is not a gate. The serial monitor is the gate. Work in the language you will keep for Capstone A. You do not have to repeat every listing in the other language today, but the file you submit has to be the non-blocking style from Lesson 1. A checklist that passes on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; has missed the chapter even if an LED happens to change.&lt;/p&gt;

&lt;p&gt;The button gate is a paste of the GPIO lab log: one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt; and one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt; per gesture, not a line every millisecond. Pin numbers and the words &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.PULL_UP&lt;/code&gt; sit next to the paste. If the released pin was floating in your first attempt, the notes can say so, and the log you submit is the one from after the pull-up returned.&lt;/p&gt;

&lt;p&gt;The PWM gate is either a photograph of the same LED visibly dim and visibly bright, or three serial lines whose duties are about 25%, 50%, and 90% of the scale you actually configured. Because ESP32 Arduino 3.x does not promise that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; is 8-bit, the note names &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach&lt;/code&gt; with its resolution, or it names &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;. A line that says “PWM works” and a sketch that never prints the count are not this gate. The load in the photo is the LED and its series resistor. A motor in the photo fails the gate, even if the motor spun, because this chapter does not attach a motor to a GPIO and does not require VM wired.&lt;/p&gt;

&lt;p&gt;The debounce gate is a pair of numbers. You pressed $N$ times. The counter printed $N$, so the error is&lt;/p&gt;

\[e = N_{\mathrm{printed}} - N_{\mathrm{pressed}} = 0.\]

&lt;p&gt;The burst log from the interrupt lesson, the one that jumped by something in the 5–30 range when the stability window was removed, can sit underneath as a contrast. It is not a substitute for $e = 0$. If your best debounced run still gains two counts per press, the checklist item stays open and you return to the 20 ms test.&lt;/p&gt;

&lt;p&gt;The state-diagram gate is a drawing in the notes, not a screenshot of the lesson figure alone. Copying the figure is fine as a template. The arrow from TELEOP to FAULT has to be labeled with the timeout you compiled, 300 ms, and IDLE and FAULT both have to be marked duty 0. The sentence in the notes is exactly the claim the code makes: “PWM is zero in FAULT.” If your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; function lights the LED whenever &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty&lt;/code&gt; is nonzero, regardless of state, you are not allowed to write that sentence yet.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Bring-up order: logic first, motor supply later, wheels up&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The power-order figure is the boundary of this chapter. USB powers the microcontroller. The 3.3 V rail powers the button pull-up and the LED. The motor supply, VM on a TB6612, is a later box in that figure, and it stays empty today. Wheels up is the posture for the first day VM is connected, which is not a Chapter 03 signature. Signing the checklist does not authorize a motor wire.&lt;/p&gt;

&lt;h2 id=&quot;worked-example-a-checklist-that-lies&quot;&gt;Worked example: a checklist that lies&lt;/h2&gt;

&lt;p&gt;A submitted note ticks “button,” “PWM,” “debounce,” and “state machine.” The only source attached is this:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcAttach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PWM done&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;still on&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Read it against the gates. There is no button, so the press log cannot exist. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; runs once, in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt;, at a fixed 180, which is about 70% and never 0. Nothing in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt; reads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;, so a 300 ms silence cannot change the pin. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; is the Lesson 1 failure mode: the first time the loop could even look at a timestamp, the deadline has been missed three times over. The print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM done&lt;/code&gt; is a caption, not a measurement. Ticking the PWM box because the letters &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; appear is how this chapter gets a robot that boots already driving.&lt;/p&gt;

&lt;p&gt;The audit rewrites the tick into four failures. PWM is not demonstrated across duties, and it is not demonstrated at 0. The timeout is absent, so FAULT cannot be entered and the sentence “PWM is zero in FAULT” would be false: there is no FAULT, and the pin is not zero. The loop is blocked for a full second, which is longer than the cutoff&lt;/p&gt;

\[1000\,\mathrm{ms} &amp;gt; 300\,\mathrm{ms}.\]

&lt;p&gt;A repaired sketch writes the duty from the state machine every pass, logs the duty when it changes, and uses the Lesson 1 clock. The checklist item flips only after those lines are in the file you just flashed, and after you have watched the LED go dark when typing stops.&lt;/p&gt;

&lt;p&gt;The same audit catches a subtler cousin: the state machine is real, but &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt; ends with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite(PIN, 180)&lt;/code&gt; and FAULT forgets to write 0 because the author believed “we are not calling ledcWrite, so the motor is off.” PWM peripherals hold the last duty. Not calling the function leaves the old pulses running. FAULT has to write zero explicitly, every pass, for both channels you will later connect to the TB6612.&lt;/p&gt;

&lt;h2 id=&quot;what-you-turn-in&quot;&gt;What you turn in&lt;/h2&gt;

&lt;p&gt;Work down this list in your lab notes. An item without its artifact stays open.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Button log. Paste a press and a release, name the GPIO, and name the pull-up. One pair per gesture.&lt;/li&gt;
  &lt;li&gt;PWM evidence. Photograph the LED at two brightnesses or paste duties near 25%, 50%, and 90%, and write the API (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach&lt;/code&gt; resolution, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;). Include a line that is duty 0.&lt;/li&gt;
  &lt;li&gt;Debounce count. Write $N$ pressed and $N$ printed. Error zero. Mention the burst you saw when the filter was off, as a contrast, if you ran that step.&lt;/li&gt;
  &lt;li&gt;State diagram. Three states, the 300 ms arrow into FAULT, duty 0 marked on IDLE and on FAULT. The sentence “PWM is zero in FAULT” appears in the notes and matches &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Language and loop. Name C++ or MicroPython, and confirm the control path does not call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleep&lt;/code&gt; longer than a few milliseconds. Quote the timeout constant from the file.&lt;/li&gt;
  &lt;li&gt;Motor boundary. Write “no motor on a GPIO, VM not connected, wheels up when a driver is added later.” If you do not own a driver yet, that sentence is still required. Buying one is the TB6612 module at 45000₫, VM about 4.5 V to 10 V, about 1.2 A continuous, from the &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-tb6612fng-dc-motor-driver&quot;&gt;hshop TB6612 listing&lt;/a&gt;, and it is not part of today’s wiring.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A missing board is not item 6. If the hardware is late, items 1–3 stay open. Items 4 and 5 can be drafted from source you have actually compiled or syntax-checked, and they stay marked “source only” until the LED has performed them.&lt;/p&gt;

&lt;h2 id=&quot;explanation-profiling-loop-headroom-and-verifying-watchdog-cutoff-time&quot;&gt;Explanation: Profiling Loop Headroom and Verifying Watchdog Cutoff Time&lt;/h2&gt;

&lt;p&gt;Before signing off Chapter 03, run a quick 4-step quantitative firmware audit:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Measure worst-case loop duration \(t_{\max}\):&lt;/strong&gt; Track &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint32_t dt = micros() - t0; if (dt &amp;gt; t_max) t_max = dt;&lt;/code&gt; over 10 seconds of operation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute CPU headroom:&lt;/strong&gt; \(\text{Headroom} = 1 - \frac{t_{\max}}{T_s}\). Aim for \(\ge 70\%\) headroom at the end of Chapter 03 so Chapters 04–05 (sensors + PID) fit effortlessly.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Measure E-stop / Watchdog latency:&lt;/strong&gt; From the instant a stop event fires to PWM duty reaching &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, latency should be \(\le 1\text{ control tick}\) (e.g., \(\le 10\text{ ms}\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute stopping distance during reaction latency:&lt;/strong&gt; \(d_{\text{react}} = v_{\max} \cdot t_{\text{latency}}\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-checking-reaction-distance-and-cpu-headroom-at-100-hz&quot;&gt;Concrete Worked Example: Checking Reaction Distance and CPU Headroom at 100 Hz&lt;/h3&gt;

&lt;p&gt;Suppose your Chapter 03 firmware runs at \(100\text{ Hz}\) (\(T_s = 10\,000\,\mu\text{s}\)) with measured worst-case loop execution \(t_{\max} = 620\,\mu\text{s}\), and your robot’s top speed will be \(v_{\max} = 0.50\text{ m/s}\):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — CPU headroom:&lt;/strong&gt;
\(\text{Headroom} = 1 - \frac{620}{10\,000} = 1 - 0.062 = 93.8\%\) — huge margin for Chapter 04 sensors!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Worst-case E-stop reaction distance (1 full loop tick = 10 ms):&lt;/strong&gt;
\(d_{\text{react}} = 0.50\text{ m/s} \times 0.010\text{ s} = 0.005\text{ m} = 5\text{ mm}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Watchdog timeout test:&lt;/strong&gt; If serial velocity commands arrive at 20 Hz (every 50 ms) and your watchdog timeout is set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;300 ms&lt;/code&gt;, unplugging the USB cable halts the motors within \(0.30\text{ s}\) (at most \(0.50 \times 0.30 = 15\text{ cm}\) of travel instead of driving off the desk!).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Leaving &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_max&lt;/code&gt; unmeasured and assuming the loop is fast — one stray blocking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.readString()&lt;/code&gt; (default timeout &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1000 ms&lt;/code&gt;!) can silently add a 1-second freeze to your robot.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If your firmware logs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE -&amp;gt; DRIVE -&amp;gt; FAULT&lt;/code&gt; cleanly, zeroes PWM within 10 ms of E-stop, and holds &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_max &amp;lt; 2000 µs&lt;/code&gt; at 100 Hz, you are ready to wire sensors in Chapter 04.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Audit the worked-example sketch as if you were the reviewer. List the checklist items it fails, and quote the line that makes “PWM is zero in FAULT” false.&lt;/li&gt;
  &lt;li&gt;A partner’s diagram shows IDLE, TELEOP, and FAULT, but the silence arrow is drawn from IDLE to FAULT and labeled “1 s.” What does the arrow get wrong about who can time out, and about the constant in Lesson 5?&lt;/li&gt;
  &lt;li&gt;The debounce log says you pressed 10 times and the last line is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;presses=12&lt;/code&gt;. Is the gate closed? What single code change from Lesson 4 is the first place you look?&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt; writes duty 200, and the FAULT branch only sets a variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty = 0&lt;/code&gt; without calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt;. After 300 ms of silence, what is on the pin, and what line has to move into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt;?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Hints&lt;/summary&gt;

1. It fails the button log, the multi-duty PWM evidence, the zero duty, the debounce count, and the state diagram. `ledcWrite(PIN, 180)` in `setup`, with no later write of 0, is the false sentence. `delay(1000)` is the blocked loop.
2. Silence is tested in TELEOP, not in IDLE. IDLE is already duty 0. The constant is 300 ms, not 1 s.
3. The gate stays open because $e = 2$, not 0. Look first at the 20 ms stability test around the increment, and at whether a CHANGE interrupt is also counting the release.
4. The pin keeps pulsing at duty 200. `apply` has to call `ledcWrite` with 0 whenever the mode is FAULT or IDLE, on every pass.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/time/millis/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;&lt;/a&gt; is the function you expect to find in any sketch that claims the 300 ms arrow is real.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html&quot;&gt;ESP32 Arduino LEDC&lt;/a&gt; is how you check that a duty write in the submitted file matches the resolution the author attached.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; will sit on top of a firmware image that has already passed this list. It does not replace the FAULT write of duty 0.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>State machines for robot firmware</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter03/03_05_state_machines_fw/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter03/03_05_state_machines_fw</id>
   <content type="html">&lt;p&gt;Ask a student to add “stop if an obstacle is closer than 20 cm, back up for 0.5 s, turn 45°, resume, and halt immediately if the battery drops below 6.4 V” to a tangle of nested &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if / else&lt;/code&gt; flags, and watch the bugs multiply overnight: the robot backs up &lt;em&gt;while&lt;/em&gt; trying to turn, or ignores the low-battery fault during the backup &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt;. A &lt;strong&gt;Finite State Machine (FSM)&lt;/strong&gt; collapses that combinatorial mess into a handful of explicit states and crystal-clear transition rules.&lt;/p&gt;

&lt;p&gt;From Mars rovers to surgical manipulators, FSMs are the backbone of safe, auditable robot behavior.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can implement three firmware states, IDLE, TELEOP, and FAULT, so that a command, a 300 ms silence, and a button each do one defined thing. In IDLE and in FAULT both PWM channels are commanded to 0. You will write the machine as an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt; and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;switch&lt;/code&gt; in C++, and as an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elif&lt;/code&gt; chain or a dictionary of functions in MicroPython, using the non-blocking clock from Lesson 1 rather than a sleep that stands in for a state. You will also reject the pattern where a blink flag and a motor flag can be true at the same time.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can measure command age with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff()&lt;/code&gt;, which is the skill Lesson 1 put in place of the blocking heartbeat in the &lt;a href=&quot;/contents/en/chapter02/02_04_blink_serial_hello/&quot;&gt;Chapter 02 blink and serial lab&lt;/a&gt;. You can print a button edge, and you can write a duty with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt; without believing that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; is always 0–255 on an ESP32. The LED from the GPIO lab will stand in for “motors enabled.” The TB6612 is still not wired. The timeout math you are encoding is the same 300 ms rule whose 200 ms sleep failure you already calculated.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-formal-automata-guard-conditions-and-fail-safe-transitions&quot;&gt;Key ideas: Formal Automata, Guard Conditions, and Fail-Safe Transitions&lt;/h2&gt;

&lt;p&gt;Three software-architecture ideas make FSMs indispensable in robotics firmware:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;States, Events, and Guards:&lt;/strong&gt; An FSM is a tuple \((S, s_0, \Sigma, \delta)\) where the robot occupies &lt;strong&gt;exactly one state&lt;/strong&gt; \(s \in S\) at any moment (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRIVE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVOID_BACKUP&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVOID_TURN&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;). Transitions occur only when an event or guard condition (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dist_cm &amp;lt; 20&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elapsed_ms &amp;gt;= 500&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v_bat &amp;lt; 6.4&lt;/code&gt;) evaluates true.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Entry / Do / Exit Actions:&lt;/strong&gt; Separating one-time &lt;em&gt;entry actions&lt;/em&gt; (e.g., recording &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state_enter_ms = now&lt;/code&gt;, resetting PID integrators) from continuous &lt;em&gt;do actions&lt;/em&gt; (updating PWM) guarantees clean handoffs with zero lingering state.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Global Safety Transitions (Highest Priority):&lt;/strong&gt; Checking safety guards (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;E_STOP&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW_BATT&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CMD_TIMEOUT&lt;/code&gt;) &lt;em&gt;before&lt;/em&gt; the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;switch (state)&lt;/code&gt; block ensures that every state immediately yields to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; with motors commanded to zero.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Capstone A teleop is not a pile of independent &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; statements. A differential-drive base is either waiting, driving from a fresh command, or stopped because commands died. Those are states, and the PWM pins are outputs of the state, not a side effect that some other block might overwrite. When ROS later delivers velocity commands, the firmware you want already knows what to do with “a command arrived” and with “nothing arrived for 300 ms”: both TB6612 channels go to duty 0 in FAULT, and they stay 0 in IDLE. The wheels stay up while you prove that on an LED. A sketch that blinks a lamp and also writes a motor duty, using two booleans that can both be true, will drive and blink during the state you thought was a fault.&lt;/p&gt;

&lt;h2 id=&quot;core-teaching&quot;&gt;Core teaching&lt;/h2&gt;

&lt;p&gt;A state machine is a variable that names exactly one situation, plus a rule for how events change that name. This firmware has three names.&lt;/p&gt;

&lt;p&gt;IDLE means no drive is allowed. Both PWM channels are 0. The LED that stands in for “motors enabled” is off. A valid command is the event that leaves IDLE.&lt;/p&gt;

&lt;p&gt;TELEOP means a command has been seen recently. The duties follow that command. Every new command refreshes a timestamp. If the age of that timestamp reaches 300 ms, the event is silence, and the next state is FAULT.&lt;/p&gt;

&lt;p&gt;FAULT means the link looks dead, or you have decided the drive is not allowed. Both PWM channels are 0 again. The LED is off. The event that leaves FAULT is a button press, and it returns to IDLE, not directly to TELEOP. A command that arrives during FAULT does not start the wheels. The operator has to acknowledge with the button, then send a new command from IDLE. That extra step is deliberate: a reconnecting joystick must not resume the last motion by itself.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/firmware_state_machine.png&quot; alt=&quot;IDLE, TELEOP, and FAULT, with the 300 ms arrow&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The illegal structure looks shorter and fails in combination:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blinkMode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;motorsOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blinkMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* toggle the LED */&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;motorsOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nothing keeps &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;motorsOn&lt;/code&gt; and a fault in opposite states, because they are not one variable. Both conditions can be true on the same pass, so the lamp blinks while a duty of 200 is still being written. After you add a third flag for the timeout, the truth table has eight rows and you will not test them. One &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enum&lt;/code&gt; has three rows. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;switch&lt;/code&gt; writes PWM in one place.&lt;/p&gt;

&lt;p&gt;C++ for the ESP32 Arduino core. The LED on GPIO 18 is bright only in TELEOP. A second channel is computed and printed so the “both channels” rule is visible, but only one physical LED is required. Serial commands are a single letter: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; means forward at duty 40, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; means an operator stop that returns to IDLE with duty 0. Silence is not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;. Silence is the absence of either letter.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;300&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint8_t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Fault&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastCmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 0..255 stand-in for both TB6612 channels&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INPUT_PULLUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;state IDLE duty 0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Later: ledcWrite(PWMA, enabled ? duty : 0); ledcWrite(PWMB, enabled ? duty : 0);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prevBtn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digitalRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btnPress&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prevBtn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;prevBtn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;available&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;F&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;S&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lastCmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;F&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;102&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// about 40% of 255, or commanded stop&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;S&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// In FAULT the character is ignored on purpose.&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Teleop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastCmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Fault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Fault&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btnPress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; before you read the transitions. IDLE and FAULT cannot light the LED, because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enabled&lt;/code&gt; requires TELEOP. A duty value left over from the last &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; is forced out of the pin by that condition, and the timeout also stores 0 so a later bug cannot revive it. The button edge here is the simple poll from the GPIO lesson; you already know how to move it behind a 20 ms stability test. Keep that test if the button double-fires. Do not add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; to the FAULT branch.&lt;/p&gt;

&lt;p&gt;MicroPython can store the same transitions as functions in a dictionary. Each function updates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty&lt;/code&gt; itself. The loop calls only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HANDLERS[state]&lt;/code&gt;, so two states cannot run on the same pass.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PULL_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;300&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;IDLE&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;prev_btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;TELEOP&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;TELEOP&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;IDLE&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMEOUT_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;FAULT&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_fault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;IDLE&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;HANDLERS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;IDLE&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_idle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;TELEOP&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_teleop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;FAULT&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_fault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# A teaching serial port can be sys.stdin; the lab section shows one pattern.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;level&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;level&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prev_btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;prev_btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;HANDLERS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The dictionary is the point of the Python version: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HANDLERS[state]&lt;/code&gt; is one function. An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elif&lt;/code&gt; on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; is equally acceptable and sometimes easier to read in a first draft. What is not acceptable is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SLEEP&lt;/code&gt; state whose body calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt;. Sleep is not a state of the robot. It is a hole in the clock. The exercise at the bottom asks you to notice that trap before it lands in the driving sketch.&lt;/p&gt;

&lt;p&gt;Command letters stay small on purpose. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; is “both wheels forward at the practice duty,” which on a differential-drive base is a straight line. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; is “I asked you to stop,” which is IDLE, not FAULT. FAULT is reserved for the case where the operator’s stream vanished. Mixing those up makes a deliberate stop look like a fault that then needs a button, or it makes a dead radio look like a clean park. The trace in the next section uses the longer phrase &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F 40&lt;/code&gt; so the duty is visible in the sentence; the lab’s single letter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; is that same command with the 40% baked in, because typing on a serial monitor is clumsy enough.&lt;/p&gt;

&lt;p&gt;Later, a micro-ROS client can deliver the same events this machine already understands. A velocity topic is just a source of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt;-like commands with a richer payload. The firmware does not need a new timeout story when that client arrives; it needs the client to refresh &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lastCmd&lt;/code&gt; and the states you wrote here to keep forcing PWM to 0. The project site &lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro.ros.org&lt;/a&gt; is where that client is documented. One sentence of foreshadow is enough until the ROS chapter.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Start in IDLE. The LED is off, and both conceptual channels are duty 0. At $t = 0$ the monitor receives &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F 40&lt;/code&gt;. The command event fires, the state becomes TELEOP, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lastCmd&lt;/code&gt; is 0, and the duty becomes 40% of full scale. On the 8-bit LEDC range that is $0.40 \times 255 = 102$. The LED turns on. Channel A and channel B would both be 102 if the driver were attached. It is not.&lt;/p&gt;

&lt;p&gt;No further characters arrive. The age of the command is $t - 0$. The timeout comparison uses the same threshold as Lesson 1:&lt;/p&gt;

\[t - t_{\text{cmd}} \ge 300\,\mathrm{ms}.\]

&lt;p&gt;On the first loop pass where that predicate is true, the state becomes FAULT and the duty is stored as 0. The LED turns off. A late &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; typed during FAULT does not leave FAULT and does not restore 102. The operator presses the button. The button event moves the machine to IDLE. Duty is still 0, so the LED stays off until a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; arrives while the machine is in IDLE.&lt;/p&gt;

&lt;p&gt;If the loop had called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(200)&lt;/code&gt; inside TELEOP, the worked example of Lesson 1 applies unchanged: the first chance to notice 300 ms is at 400 ms, and the LED (and later the TB6612) would stay at duty 102 for that extra 100 ms. The state diagram’s arrow is labeled 300 ms. The code has to be able to see that arrow, which means the loop keeps running.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;p&gt;No motor, no VM, no battery. The LED is “motors enabled.” The serial monitor is the joystick.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Flash the C++ listing, or extend the Python handlers with the USB serial read you already used in Chapter 02 so a typed letter fills &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd&lt;/code&gt;. Confirm the boot line says IDLE and the LED is dark.&lt;/li&gt;
  &lt;li&gt;Type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt;. The LED lights, and a print you add should say TELEOP and show duty 102, or 40 in the Python version. Type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;. The LED goes dark and the state returns to IDLE. That is an operator stop, not a fault.&lt;/li&gt;
  &lt;li&gt;Type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; again and then type nothing. Within about 300 ms the LED must go dark and the state must read FAULT. If it stays lit, the timestamp is not being read, or a sleep is longer than the timeout. Type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; during FAULT and confirm the LED stays dark.&lt;/li&gt;
  &lt;li&gt;Press the button. The state returns to IDLE and the duty remains 0. Only a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; after that may light the LED.&lt;/li&gt;
  &lt;li&gt;In your notes, sketch the three bubbles and the silence arrow, and write the sentence “PWM is zero in FAULT.” The checklist asks for both.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the LED turns on the moment the sketch boots, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; is not gated on TELEOP, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt; wrote a duty and the loop never rewrote it. That bug is the subject of the chapter checklist.&lt;/p&gt;

&lt;h2 id=&quot;explanation-designing-a-5-state-diff-drive-fsm-and-tracing-every-transition&quot;&gt;Explanation: Designing a 5-State Diff-Drive FSM and Tracing Every Transition&lt;/h2&gt;

&lt;p&gt;Instead of juggling 4 boolean flags (\(2^4 = 16\) combinations, most of them illegal!), trace how a clean 5-state FSM handles obstacle avoidance and safety:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;List mutually exclusive states:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRIVE&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BACKUP&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TURN&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Assign a strict priority order to transitions:&lt;/strong&gt; Safety faults (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v_bat &amp;lt; 6.4 V&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bumper_stuck&lt;/code&gt;) override everything; obstacle guards fire only in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRIVE&lt;/code&gt;; timer guards govern &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BACKUP&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TURN&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Define exact actuator outputs per state:&lt;/strong&gt; Every state explicitly sets &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(v_cmd, omega_cmd)&lt;/code&gt; so motors can &lt;em&gt;never&lt;/em&gt; inherit a stale speed from a previous state.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trace a timeline with concrete numbers&lt;/strong&gt; to prove no deadlock or rapid oscillation (chattering) can occur.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-hand-tracing-a-4-second-obstacle--low-battery-scenario&quot;&gt;Concrete Worked Example: Hand-Tracing a 4-Second Obstacle + Low-Battery Scenario&lt;/h3&gt;

&lt;p&gt;Parameters: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BACKUP&lt;/code&gt; duration \(= 500\text{ ms}\) at \((-0.15\text{ m/s}, 0)\); &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TURN&lt;/code&gt; duration \(= 600\text{ ms}\) at \((0, +1.5\text{ rad/s})\); obstacle trigger \(d &amp;lt; 20\text{ cm}\); low-battery cutoff \(V_{\text{bat}} &amp;lt; 6.40\text{ V}\).&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Time &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms&lt;/code&gt;&lt;/th&gt;
      &lt;th&gt;Sensor Inputs&lt;/th&gt;
      &lt;th&gt;Active State&lt;/th&gt;
      &lt;th&gt;Transition / Action&lt;/th&gt;
      &lt;th&gt;Command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(v, ω)&lt;/code&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;start_btn=1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d=80 cm&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V=7.2 V&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDLE → DRIVE&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Entry: reset PID&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0.25, 0.0)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1200&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d=18 cm&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V=7.1 V&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRIVE → BACKUP&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Guard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d &amp;lt; 20&lt;/code&gt;: set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_enter=1200&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(-0.15, 0.0)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1700&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d=26 cm&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V=7.1 V&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BACKUP → TURN&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Guard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1700 - 1200 &amp;gt;= 500&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_enter=1700&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0.0, +1.5)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2300&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d=95 cm&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V=7.0 V&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TURN → DRIVE&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Guard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2300 - 1700 &amp;gt;= 600&lt;/code&gt; (turned \(0.9\text{ rad} \approx 52^\circ\))&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0.25, 0.0)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3500&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d=90 cm&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V=6.35 V&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRIVE → FAULT&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Global safety guard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V &amp;lt; 6.40&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0.0, 0.0)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Notice how at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t = 2300 ms&lt;/code&gt;, the robot has backed up \(0.15 \times 0.5 = 7.5\text{ cm}\) and rotated \(1.5 \times 0.6 = 0.90\text{ rad}\) (\(51.6^\circ\)) — cleanly, deterministically, and without a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Transitioning from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVOID&lt;/code&gt; back to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRIVE&lt;/code&gt; using the exact same threshold (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d &amp;lt; 20 cm&lt;/code&gt; to enter, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d &amp;gt;= 20 cm&lt;/code&gt; to exit) — sensor noise around 20 cm causes violent chattering between states. Always add &lt;strong&gt;hysteresis&lt;/strong&gt; (enter at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d &amp;lt; 20 cm&lt;/code&gt;, exit only when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d &amp;gt; 28 cm&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; Why must &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FAULT&lt;/code&gt; require an explicit manual reset (button press or power cycle) rather than auto-recovering the instant &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V_bat&lt;/code&gt; rebounds above &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6.40 V&lt;/code&gt;? Because once motors stop, battery voltage immediately bounces back up by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.3–0.5 V&lt;/code&gt; — auto-recovery would cause an endless start-stop stutter loop!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Walk this trace and name the state and the duty after each event: boot; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt;; 300 ms of silence; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; again with no button; button; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;A classmate adds &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mode::Sleep&lt;/code&gt; and calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; inside that branch “so the robot rests.” Explain which event the machine can no longer see, and what the TB6612 does during the delay if the last duty was nonzero.&lt;/li&gt;
  &lt;li&gt;Why must a command received in FAULT be ignored, instead of treated as proof that the link is healthy?&lt;/li&gt;
  &lt;li&gt;Rewrite the two-boolean loop so a single variable makes “blink while driving” impossible. You only need to describe the variable and which state, if any, owns the LED.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Hints&lt;/summary&gt;

1. IDLE duty 0; TELEOP duty 102 (or 40%); FAULT duty 0; still FAULT duty 0 after the second `F`; IDLE duty 0 after the button; TELEOP duty 102 after the last `F`.
2. During `delay(1000)` the 300 ms silence arrow is not evaluated. If SLEEP does not force duty 0 before sleeping, both channels keep the previous command for a full second.
3. FAULT means a human must acknowledge. A burst of bytes after a dropout would otherwise resume motion with nobody holding the joystick.
4. One `Mode` value, with the LED written only in the TELEOP branch of `apply`. Delete `blinkMode` and `motorsOn`.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/time/millis/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;&lt;/a&gt; is the clock inside the C++ timeout comparison. Unsigned subtraction is what makes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - lastCmd&lt;/code&gt; honest across wrap.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; is the client that will eventually deliver the commands this machine already treats as events. The timeout stays on the microcontroller.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Interrupts, debouncing, and short ISRs</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter03/03_04_interrupts_debounce/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter03/03_04_interrupts_debounce</id>
   <content type="html">&lt;p&gt;If your main loop checks an encoder pin only once every 10 milliseconds, a spinning wheel will fire 50 pulses while the CPU is looking the other way — and your robot’s odometry will think it barely moved. &lt;strong&gt;Hardware interrupts&lt;/strong&gt; solve this by letting an electrical edge tap the CPU on the shoulder the microsecond it happens. Yet interrupts come with a famous trap: put a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print()&lt;/code&gt; or a floating-point calculation inside an Interrupt Service Routine (ISR), and your microcontroller crashes with a watchdog panic.&lt;/p&gt;

&lt;p&gt;This lesson teaches the physics of mechanical contact bounce, the discipline of ultra-short ISRs, and thread-safe data sharing via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; and critical sections.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can separate a contact’s mechanical bounce from a real press, and you can put only a flag update inside an interrupt service routine. You will attach a button on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt;, let the main loop time a 20 ms stability window, and show that one physical press increments a counter by one. You will also explain why &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; inside that ISR freezes every other interrupt, and what a future encoder ISR is allowed to do: increment a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile uint32_t&lt;/code&gt; and return.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;The GPIO lab already wired a button from the pin to ground with the pull-up enabled, and it already rejected a change that had not been stable for 20 ms. That version polled. This lesson moves the “something happened” event into an interrupt and leaves the stability test in the loop you built in Lesson 1, the same non-blocking clock that replaced the &lt;a href=&quot;/contents/en/chapter02/02_04_blink_serial_hello/&quot;&gt;Chapter 02 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; heartbeat&lt;/a&gt;. You need the button you already have. You do not need a new part, and you still do not connect a motor.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-hardware-interrupts-contact-bounce-physics-and-atomic-concurrency&quot;&gt;Key ideas: Hardware Interrupts, Contact Bounce Physics, and Atomic Concurrency&lt;/h2&gt;

&lt;p&gt;Three low-level systems concepts keep interrupt-driven robot firmware rock-solid:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Polling vs. Edge-Triggered Interrupts:&lt;/strong&gt; Polling checks a pin on the CPU’s schedule (fine for slow human buttons); hardware interrupts (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RISING&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt;) jump immediately into an ISR within microseconds (essential for high-speed encoders and safety limits).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mechanical Switch Bounce:&lt;/strong&gt; Inside a pushbutton or limit switch, spring metal contacts physically rebound for \(2\text{–}15\text{ ms}\) after impact, generating 5–20 spurious digital edges from a single press. Software debouncing (ignoring edges within a lockout window \(T_{\text{db}}\)) or an RC hardware filter turns that chatter into one clean event.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Atomicity, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt;, and Critical Sections:&lt;/strong&gt; Because an ISR can preempt the main loop between two assembly instructions, any shared variable must be declared &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; (preventing compiler caching in a register), and multi-byte reads/writes (like a 32-bit encoder count on an 8-bit CPU, or reading two encoder counts together) must happen inside a brief interrupt-disabled critical section.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Capstone A clears FAULT with a button and, later, counts wheel encoders while teleop duties are being updated. If the button ISR prints, allocates, or calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt;, it holds off the loop that is supposed to notice 300 ms of command silence and force both TB6612 channels to 0. A bouncing contact makes the opposite bug: one finger tap looks like five to thirty edges, so a “clear fault” gesture retriggers, or an encoder count runs away while the robot is standing still with its wheels up. The ISR has to be short enough that the timeout loop still gets the CPU, and the debounce has to live where a millisecond clock is legal.&lt;/p&gt;

&lt;h2 id=&quot;core-teaching&quot;&gt;Core teaching&lt;/h2&gt;

&lt;p&gt;A mechanical switch does not close once. The contacts strike, rebound, and strike again. For the buttons in this course that chatter usually lasts somewhere in a 10 ms to 50 ms window, which is why the GPIO lab picked 20 ms as a default inside that range. During the window the pin can produce a burst of edges. An interrupt on each edge will run the ISR once per edge. A human still believes they pressed once.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/debounce_timeline.png&quot; alt=&quot;Contact bounce, then a stable level&quot; /&gt;&lt;/p&gt;

&lt;p&gt;An interrupt service routine, the ISR, is the function the hardware calls when the pin matches the trigger you armed. It preempts the main loop. That is the feature: a short pulse cannot hide between two polls if the poll period is longer than the pulse. It is also the constraint. While the ISR runs, other interrupt work waits, and on a single core your loop does not. The routine therefore stays tiny. Set a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; flag or increment a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; counter, and return. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt; tells the compiler that the main loop must actually reload the variable, because the ISR writes it behind the compiler’s back. Do not call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print&lt;/code&gt; in the ISR. Do not allocate, do not build a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;, and do not touch Python objects if you are on MicroPython. The &lt;a href=&quot;https://docs.micropython.org/en/latest/reference/isr_rules.html&quot;&gt;MicroPython ISR rules&lt;/a&gt; are blunt about heap allocation inside a handler, and the same discipline saves the Arduino sketch from a print that needs the UART interrupt you are currently blocking.&lt;/p&gt;

&lt;p&gt;The main loop is where time is allowed to pass. It clears the flag, then applies the same 20 ms stability rule as the GPIO lab: the raw reading must hold still before a press is counted. Arm the pin on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt; if you only care about the press of a pull-up button, or on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt; if you also want the release. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt; matches pressed-means-LOW. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt; will also see the bounce on the way up, which the stability test then has to reject.&lt;/p&gt;

&lt;p&gt;A future encoder can count in the ISR, and that is a good use of the mechanism, provided the ISR only does&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;volatile&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IRAM_ATTR&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onEnc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;encCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;No floating-point speed estimate, no serial, no debounce-by-delay. The loop reads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;encCount&lt;/code&gt; into a local, computes speed, and updates PWM. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IRAM_ATTR&lt;/code&gt; on ESP32 places the ISR in internal RAM so a flash operation cannot stall it. Use it on the Arduino ESP32 core.&lt;/p&gt;

&lt;p&gt;The bug to refuse is debounce performed inside the ISR with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IRAM_ATTR&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;badIsr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;// blocks the core; other ISRs wait&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digitalRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pressFlag&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; itself depends on the timekeeping interrupt. Calling it from an ISR stops the clock that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; is waiting on, or at best freezes every handler that would have noticed a command pin, a UART byte, or the other encoder channel. Even a “working” busy-wait of 20 ms inside the ISR means the 300 ms silence check cannot run during the bounce, and two encoders will lose edges for the whole wait. The flag-and-loop split exists so the ISR returns in a microsecond and the loop does the waiting without disabling the rest of the chip.&lt;/p&gt;

&lt;p&gt;Arduino C++. The counter increases only after the line has been LOW, stably, for 20 ms following a falling interrupt. GPIO 4 is the button from the GPIO lab.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;volatile&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IRAM_ATTR&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onFall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// nothing else&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INPUT_PULLUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;attachInterrupt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digitalPinToInterrupt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;onFall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FALLING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;presses=0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digitalRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// consumed; the stability test still decides&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;presses=%lu&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The interrupt’s job is to record that a falling edge was seen. The count is printed from the loop, one line per accepted press. If you want the ugly comparison, comment out the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DEBOUNCE_MS&lt;/code&gt; test and increment on every consumed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fell&lt;/code&gt; flag. One click then prints several times. Students on this hardware commonly see a burst of about 5 to 30 counts for a single press when the stability window is removed. Your button may land anywhere in that band. Write the number you actually see.&lt;/p&gt;

&lt;p&gt;MicroPython’s handler has the same shape. The callback sets a global flag and does not allocate. Debounce stays in the loop. Prefer a preallocated integer and a boolean; do not build a list or a formatted string inside the handler.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_fall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PULL_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;irq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trigger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IRQ_FALLING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;on_fall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;presses=0&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;presses&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;presses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.IRQ_FALLING&lt;/code&gt; is the MicroPython spelling of the same trigger. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.IRQ_RISING | Pin.IRQ_FALLING&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt;. Printing inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;on_fall&lt;/code&gt; looks convenient and will eventually throw or corrupt the heap. If a lab partner says the interrupt “randomly dies,” look for an allocation in the handler before you look for a bad solder joint.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A press produces edges at $t = 0, 3, 7, 12$ ms and is then solidly LOW until the release. Without a stability test the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt; ISR runs four times, and a chatty switch in the 5–30 band simply has more edges in that same 10–50 ms window. The counter prints 4, or 17, or 30, for one finger motion. With the 20 ms rule the raw level is not accepted until it has been constant since the last edge. The last falling edge is at 12 ms, so the level is trusted at 32 ms, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;presses&lt;/code&gt; increases by 1. The three earlier edges restarted the timer and never became their own counts.&lt;/p&gt;

&lt;p&gt;Release chatter is ignored for the count because the listing only increments on a stable LOW. It still must not block. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; inside the ISR at $t = 0$ would sit in the handler until 20 ms, missing any other interrupt in that window and postponing the loop’s 300 ms command check by the same 20 ms plus whatever else was queued. Twenty milliseconds is not the full timeout, but it is a hole you punched in the only loop allowed to cut PWM. The encoder version of this arithmetic is stricter: edges 200 µs apart cannot wait behind a 20 ms debounce delay, which is why the encoder ISR only increments.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;p&gt;Use the button wiring from the GPIO lesson. USB only. No motor terminal on the pin.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Flash the debounced listing. Press ten times at about one press a second. The serial log should show &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;presses=1&lt;/code&gt; through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;presses=10&lt;/code&gt;, and the final number should match your finger with error zero. If you are off by one, you double-counted a release or the contact never went stable.&lt;/li&gt;
  &lt;li&gt;Bypass the 20 ms test so each consumed falling flag increments the counter. Press once, firmly. Expect a jump of several counts, typically somewhere from 5 to 30. Paste both logs into your notes: the matched count, and the burst.&lt;/li&gt;
  &lt;li&gt;Do not “fix” the burst by putting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onFall&lt;/code&gt;. Read the bad listing in the core section again and write one sentence in your notes: the wait belongs next to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_diff&lt;/code&gt;, not in the ISR.&lt;/li&gt;
  &lt;li&gt;Optional: move the trigger to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt; and confirm that a sloppy stability test now also reacts to the release burst. The count should still grow by one per press if you only increment on stable LOW.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;explanation-designing-a-crash-proof-encoder-isr-and-software-debouncer&quot;&gt;Explanation: Designing a Crash-Proof Encoder ISR and Software Debouncer&lt;/h2&gt;

&lt;p&gt;Follow these four ironclad rules whenever you write an ISR in robotics firmware:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Keep the ISR under ~2–5 microseconds:&lt;/strong&gt; Increment/decrement a counter, record a timestamp, or set a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile bool&lt;/code&gt; flag — &lt;em&gt;never&lt;/em&gt; call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;malloc()&lt;/code&gt;, or I2C reads inside an ISR.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Place ESP32 ISRs in RAM (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IRAM_ATTR&lt;/code&gt;):&lt;/strong&gt; Flash cache can be disabled during flash writes; marking the ISR &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IRAM_ATTR&lt;/code&gt; keeps it executable from SRAM at all times.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Copy and zero/snapshot atomically in the main loop:&lt;/strong&gt; Briefly disable interrupts (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;noInterrupts()&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;portENTER_CRITICAL&lt;/code&gt;), copy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int32_t c = enc_count;&lt;/code&gt;, and re-enable interrupts immediately.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Separate button debouncing from encoders:&lt;/strong&gt; Optical/Hall encoders do &lt;em&gt;not&lt;/em&gt; bounce mechanically (never add a 20 ms debounce to an encoder, or you will cap wheel speed below 50 pulses/s!).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-why-a-20-ms-debounce-destroys-an-encoder--and-how-to-snapshot-safely&quot;&gt;Concrete Worked Example: Why a 20 ms Debounce Destroys an Encoder — and How to Snapshot Safely&lt;/h3&gt;

&lt;p&gt;Suppose your wheel encoder outputs \(2\,000\text{ pulses/s}\) at cruising speed, while a front bumper microswitch bounces for up to \(8\text{ ms}\):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Time between consecutive encoder edges:&lt;/strong&gt;
\(\Delta t_{\text{enc}} = \frac{1}{2\,000\text{ Hz}} = 0.5\text{ ms} = 500\,\mu\text{s}.\)
If you accidentally applied a \(20\text{ ms}\) software debounce to that encoder pin, it would discard 39 out of every 40 real edges (capturing at most \(1/0.02 = 50\text{ edges/s}\) — losing &lt;strong&gt;97.5%&lt;/strong&gt; of your robot’s distance!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Correct bumper switch debounce window:&lt;/strong&gt;
Choosing \(T_{\text{db}} = 20\text{ ms} &amp;gt; 8\text{ ms}\) (2.5× the worst-case bounce duration) filters 100% of switch chatter, while a robot cruising at \(0.30\text{ m/s}\) moves only \(0.30 \times 0.02 = 6\text{ mm}\) during confirmation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Atomic delta calculation in a 100 Hz control loop (\(\Delta t = 0.01\text{ s}\)):&lt;/strong&gt;
At tick \(k-1\), snapshot &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c_prev = 10420&lt;/code&gt;. At tick \(k\), snapshot &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c_now = 10440&lt;/code&gt;.
\(\Delta c = 10\,440 - 10\,420 = 20\text{ counts} \implies \text{rate} = \frac{20}{0.01} = 2\,000\text{ counts/s}.\)
Notice that taking differences (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c_now - c_prev&lt;/code&gt;) &lt;em&gt;without&lt;/em&gt; resetting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enc_count = 0&lt;/code&gt; avoids losing an edge that arrives between reading and zeroing!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Writing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count = enc_count; enc_count = 0;&lt;/code&gt; outside a critical section — if an encoder edge fires right between those two lines, that tick is silently erased forever. Always snapshot without zeroing and subtract &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c_now - c_prev&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int32_t&lt;/code&gt; encoder counter holds up to \(\pm 2.147 \times 10^9\) counts. At \(10\,000\text{ counts/s}\), it would take \(2.147 \times 10^9 / 10\,000 \approx 2.48\text{ days}\) of nonstop full-speed spinning in one direction to overflow!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;One press produces falling edges at 0, 2, 5, 9, 14, and 18 ms, then stays LOW. How many counts does the bare ISR produce, and at what time does the 20 ms rule accept the press?&lt;/li&gt;
  &lt;li&gt;Why does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.println&lt;/code&gt; inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onFall&lt;/code&gt; break the “short ISR” rule even when the sketch appears to work for a few clicks?&lt;/li&gt;
  &lt;li&gt;A quadrature encoder will later need a count. Which of these belongs in the ISR: adding one to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile uint32_t&lt;/code&gt;, computing revolutions per minute with floating point, or calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt;?&lt;/li&gt;
  &lt;li&gt;Two buttons are armed, and button A’s ISR calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt;. A falling edge arrives on button B 4 ms later. When does B’s ISR run, and what does that imply for a 300 ms PWM cutoff that lives in the main loop?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Hints&lt;/summary&gt;

1. Six falling edges become six counts if nothing filters them. The last edge is at 18 ms, so the stable LOW is accepted at 38 ms and the filtered count is 1.
2. `Serial` needs buffers and often the UART interrupt. Doing that work inside the button ISR lengthens the handler and can deadlock against the interrupt it needs. Set a flag; print in `loop`.
3. Only the increment belongs in the ISR. Speed math and PWM updates read the counter from the loop.
4. B waits until A&apos;s `delay(20)` returns, so B runs about 16 ms late. The main loop, including the silence check, is also stalled for those 20 ms.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;attachInterrupt&lt;/code&gt;&lt;/a&gt; documents &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FALLING&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RISING&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHANGE&lt;/code&gt;, and the requirement that the service routine be short.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.micropython.org/en/latest/reference/isr_rules.html&quot;&gt;MicroPython ISR rules&lt;/a&gt; are the matching constraint for the callback version: no heap, no long work, schedule the real handling in the main loop.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>PWM, timers, and motor-speed signals</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter03/03_03_pwm_timers/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter03/03_03_pwm_timers</id>
   <content type="html">&lt;p&gt;A microcontroller’s digital pin knows only two voltages: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0 V&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.3 V&lt;/code&gt;. Yet somehow it smoothly accelerates a robot wheel from a whisper-quiet crawl to full cruising speed. The trick is &lt;strong&gt;Pulse-Width Modulation (PWM)&lt;/strong&gt;: flipping the switch ON and OFF thousands of times per second so the motor’s own inductance and mechanical inertia act as a natural low-pass filter, responding only to the &lt;em&gt;average&lt;/em&gt; voltage.&lt;/p&gt;

&lt;p&gt;This lesson unpacks hardware timers, duty-cycle resolution, and PWM carrier frequency — explaining why a 500 Hz motor whines loudly while a 20 kHz motor purrs silently.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can state what a duty cycle does to the average voltage of a resistive load, and you can say what it only &lt;em&gt;approximately&lt;/em&gt; does to a small DC motor. You will generate a visible LED breathe on a safe GPIO using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; in ESP32 Arduino 3.x, recognize why a bare &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; is not a promise of 8-bit 0–255 on that core, and set the same idea in MicroPython with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PWM&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;. You will also explain why a 20% duty on a 7.4 V motor rail is often below the start of a TT gearmotor, and why this lab still refuses to attach that motor.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can blink a pin from the &lt;a href=&quot;/contents/en/chapter02/02_04_blink_serial_hello/&quot;&gt;Chapter 02 serial heartbeat&lt;/a&gt; and you can run the non-blocking loop from Lesson 1, so a brightness ramp does not become &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; calls that last a whole fade. The LED and series resistor from the GPIO lab are the load. A TB6612 is discussed as the chip that will later receive these signals. It stays in the bag. If you have not bought the LED yet, the parts list in the GPIO lesson is the shopping stop; this lesson adds no parts.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-hardware-timers-duty-cycle-quantization-and-motor-inductance-filtering&quot;&gt;Key ideas: Hardware Timers, Duty-Cycle Quantization, and Motor Inductance Filtering&lt;/h2&gt;

&lt;p&gt;Three physical and digital principles govern PWM motor control:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Time-Averaged Voltage (Duty Cycle \(D\)):&lt;/strong&gt; Switching between \(V_{\text{sup}}\) for duration \(t_{\text{on}}\) and \(0\text{ V}\) for \(T - t_{\text{on}}\) produces an average voltage \(V_{\text{avg}} = D \cdot V_{\text{sup}}\), where \(D = t_{\text{on}} / T \in [0, 1]\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Timer Clock vs. Resolution Trade-off:&lt;/strong&gt; A hardware timer running at clock \(f_{\text{clk}}\) with \(N\)-bit counter resolution (\(2^N\) steps) overflows at PWM frequency:
\(f_{\text{PWM}} = \frac{f_{\text{clk}}}{2^N}.\)
Raising PWM frequency reduces maximum bit resolution, and vice versa!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Electrical Time Constant &amp;amp; Acoustic Noise:&lt;/strong&gt; A DC motor’s armature has resistance \(R\) and inductance \(L\), forming a low-pass filter with cutoff \(f_L = R / (2\pi L)\). Choosing \(f_{\text{PWM}} \ge 20\text{ kHz}\) both smooths current ripple and pushes switching vibrations above the upper limit of human hearing (20 kHz), eliminating annoying motor whine.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Capstone A’s differential-drive teleop does not send a torque. It sends two duties, one toward PWMA and one toward PWMB on a TB6612, and the driver switches the motor supply at that pattern. When commands go silent for 300 ms, both duties become 0, which is how the wheels are told to stop commanding torque. Practicing that number on an LED means you can see 0%, 25%, 50%, and 90% before a gearbox is involved. A motor on a GPIO would skip the driver and dump coil current into a pin that was designed for an LED. Wheels stay up even later, when the driver is finally connected, until a low duty has been watched with the chassis in the air.&lt;/p&gt;

&lt;h2 id=&quot;core-teaching&quot;&gt;Core teaching&lt;/h2&gt;

&lt;p&gt;A timer toggles a pin high for part of a fixed period and low for the rest. The duty cycle $D$ is the fraction of the period spent high, so $0 \le D \le 1$. For a resistive load on a stiff supply the average voltage is close to&lt;/p&gt;

\[V_{\mathrm{avg}} \approx D \cdot V_{\mathrm{source}}.\]

&lt;p&gt;An LED plus its series resistor is close enough to that picture that brightness tracks $D$, as long as $D \cdot V_{\mathrm{source}}$ still clears the LED’s forward drop for part of the cycle. The eye integrates the pulses. You do not need the LED to switch at a rate you can count.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/pwm_duty_cycle.png&quot; alt=&quot;Duty at 25%, 50%, and 90%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A DC motor is not a resistor. The winding has resistance and inductance, and the rotor produces a back voltage once it is spinning. Duty still sets the average voltage the winding sees, and speed follows that average only roughly, with a dead zone at low duty where friction and the brush drop eat the whole average. Raising $D$ from 0.05 to 0.15 may do nothing visible. Raising it through the band where the motor actually starts produces a jump, not a straight line. Frequency matters as much as duty. Around 100 Hz many small gearmotors chatter and whine: the torque pulses once per period and the gearbox speaks. LED PWM can sit near 1 kHz, which is already hard to see as flicker. Motor PWM is usually placed between 1 kHz and 20 kHz so the current ripple is smaller and the whine moves up, sometimes above hearing. Twenty kilohertz is a normal choice for the TB6612 inputs later. One kilohertz is the right choice for this LED, because you are debugging duty, not inductance.&lt;/p&gt;

&lt;p&gt;On a classic AVR Arduino, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite(pin, 128)&lt;/code&gt; means “about half of an 8-bit range,” and the range really is 0 to 255. That sentence does not travel to the ESP32 Arduino core. In ESP32 Arduino 3.x the supported API is the LEDC peripheral: attach a pin with a frequency and a resolution, then write a duty that fits that resolution. An 8-bit resolution still gives the familiar 0 to 255, but only because you asked for 8 bits. A 10-bit attach makes 255 a quarter-scale command. Read the call. Do not copy an AVR snippet and assume the wheel will creep.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_PIN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;// safe on a typical ESP32 DevKit; not a strap pin&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_HZ&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// LED lab. A later motor channel wants 20000.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint8_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_BITS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// duty 0..255 only because resolution is 8&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ledcAttach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PWM_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_HZ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM_BITS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;constrain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ledcWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PWM_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;duty=%d frac=%.2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;255.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The 15 ms step is a brightness update, not a nap that replaces the loop. There is no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(15)&lt;/code&gt;. When this breathe later shares a loop with a 300 ms timeout, the same &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; test is what keeps the timeout awake. For awareness, the AVR-style call looks like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite(9, 128)&lt;/code&gt;. On ESP32 Arduino 3.x, prefer &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt;. If a core happens to provide &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;analogWrite&lt;/code&gt; as a wrapper, the resolution and default frequency are still whatever that wrapper chose, so the explicit LEDC calls stay in your file.&lt;/p&gt;

&lt;p&gt;MicroPython exposes the same idea with a 16-bit duty even when you are thinking in percent:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PWM&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pwm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PWM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty_u16&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# LED. Use freq=20000 for a motor input later.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1200&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;65535&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;65535&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pwm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;duty_u16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;duty_u16&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt; runs from 0 to 65535. A fraction $D$ is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int(D * 65535)&lt;/code&gt;. Frequency is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;freq&lt;/code&gt; argument in hertz. Changing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;freq&lt;/code&gt; to 20000 does not make the LED a motor, and it may make a cheap LED driver ring; keep 1000 Hz until the load is the TB6612 input.&lt;/p&gt;

&lt;p&gt;ESP32 strapping pins are sampled at reset to decide boot mode and flash voltage. The ones students hit with a PWM wire are GPIO 0, GPIO 2, GPIO 12, and GPIO 15. A pulldown or a peripheral on those pins can stop the chip from booting, or can make the next upload fail in a way that looks like a bad cable. GPIO 2 is also a common onboard LED, which is why Chapter 02 blinked it and why this lesson does not also use it as a motor-shaped experiment. Pick GPIO 18 or GPIO 19 for PWM labs on a DevKit module. Confirm the silk. If a pin is tied to flash or to USB-serial on your particular board, believe the schematic over this paragraph.&lt;/p&gt;

&lt;p&gt;The TB6612, when you buy it, takes a logic-level PWM on its input pins and switches a separate motor supply VM. VM wants about 4.5 V to 10 V, and the chip’s continuous current rating on the usual module is about 1.2 A. Those numbers are why a 7.4 V battery belongs on VM later and never on a GPIO. This chapter does not wire that module. The link is for the week you are actually ready to leave the LED: the module at &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-tb6612fng-dc-motor-driver&quot;&gt;hshop, TB6612FNG&lt;/a&gt; is listed at 45000₫. Until the state machine in Lesson 5 can force duty 0, VM stays disconnected and the wheels stay up.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A two-cell pack sits near 7.4 V. A duty of 20% would present an average near&lt;/p&gt;

\[V_{\mathrm{avg}} \approx 0.20 \times 7.4 = 1.48\,\mathrm{V},\]

&lt;p&gt;about 1.5 V. A yellow TT gearmotor often does not start there. The brushes and the gearbox want something closer to a few volts before the shaft moves, so the first motion test you will run in a later chapter starts nearer 30% to 40% with the wheels up:&lt;/p&gt;

\[0.30 \times 7.4 \approx 2.2\,\mathrm{V}, \qquad 0.40 \times 7.4 \approx 3.0\,\mathrm{V}.\]

&lt;p&gt;Those are still averages, not a promise of speed, and they assume the driver is in the circuit. If you skip the driver and put 7.4 V on a GPIO, you are no longer in the dead-zone discussion. You are past the absolute maximum of the pin.&lt;/p&gt;

&lt;p&gt;On the LED bench the same 20% is an 8-bit duty of $0.20 \times 255 \approx 51$, or a MicroPython value of $0.20 \times 65535 \approx 13107$. Fifty percent is 128 in 8 bits and 32768 in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;. Ninety percent is about 230, or about 58982. Log those three numbers once so the checklist has a table, not a vibe. The figure at the top of the lesson is those three pulses drawn on one period: a short high, a square half, and a high that only dips briefly.&lt;/p&gt;

&lt;p&gt;A 100 Hz motor PWM has period $1/100 = 10$ ms. The torque dips every 10 ms, which is audible and visible as chatter. Moving to 20 kHz shortens the period to $50$ µs. The winding inductance averages the current, and the duty you computed above is what remains. The LED lab does not need that rate. Do not “upgrade” the breathe to 20 kHz and then conclude PWM is broken because your eye cannot see a difference between frequencies that are both flicker-free.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;p&gt;The load is the LED and 180 Ω resistor from the GPIO lesson, on GPIO 18, unless your silk says that pin is taken. USB power only.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Attach PWM at 1 kHz, 8-bit, and run the breathe. You should see a smooth brighten and dim, and the serial log should climb and fall rather than stick at one number. Photograph the LED at a dim step and a bright step, or paste three log lines whose duties match about 25%, 50%, and 90%.&lt;/li&gt;
  &lt;li&gt;Hold the duty at 0 and confirm the LED is dark. Hold it at 255, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt; at 65535, and confirm it matches a plain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;digitalWrite(HIGH)&lt;/code&gt; in brightness. Those two endpoints are what “PWM is zero” and “PWM is full” mean before a driver exists.&lt;/li&gt;
  &lt;li&gt;Move the frequency to 100 Hz for a few seconds. On an LED you may see flicker. That flicker is the motor-chatter lesson without a motor. Put the frequency back to 1 kHz.&lt;/li&gt;
  &lt;li&gt;Do not clip the LED leads onto a battery, and do not jumper GPIO 18 to a motor terminal “just to see.” If the chip fails to reboot after you experiment, check whether the wire is on GPIO 0, 2, 12, or 15, then move it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;explanation-solving-the-esp32-ledc-frequencyresolution-trade-off-by-hand&quot;&gt;Explanation: Solving the ESP32 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEDC&lt;/code&gt; Frequency–Resolution Trade-Off by Hand&lt;/h2&gt;

&lt;p&gt;Why can’t you ask an ESP32 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEDC&lt;/code&gt; timer for both 16-bit resolution &lt;em&gt;and&lt;/em&gt; 20 kHz PWM frequency simultaneously? Check the exact timer equation in four steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Identify the timer base clock:&lt;/strong&gt; ESP32 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LEDC&lt;/code&gt; runs on the APB clock \(f_{\text{clk}} = 80\text{ MHz} = 80\,000\,000\text{ Hz}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute maximum integer bit depth \(N_{\max}\) for a target \(f_{\text{PWM}}\):&lt;/strong&gt;
\(2^N \le \frac{f_{\text{clk}}}{f_{\text{PWM}}} \implies N_{\max} = \left\lfloor \log_2\!\left(\frac{f_{\text{clk}}}{f_{\text{PWM}}}\right) \right\rfloor.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Calculate voltage step size (quantization):&lt;/strong&gt; \(\Delta V = \frac{V_{\text{sup}}}{2^N}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify driver switching-speed ceiling:&lt;/strong&gt; Check the motor driver datasheet (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt; supports up to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100 kHz&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L298N&lt;/code&gt; degrades above ~&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;25 kHz&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-sizing-silent-20-khz-motor-pwm-on-an-80-mhz-esp32&quot;&gt;Concrete Worked Example: Sizing Silent 20 kHz Motor PWM on an 80 MHz ESP32&lt;/h3&gt;

&lt;p&gt;Suppose you want silent \(f_{\text{PWM}} = 20\text{ kHz}\) PWM driving a 7.4 V motor rail:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Maximum supported bit resolution:&lt;/strong&gt;
\(\frac{f_{\text{clk}}}{f_{\text{PWM}}} = \frac{80\,000\,000}{20\,000} = 4\,000.\)
Since \(2^{11} = 2\,048 \le 4\,000 &amp;lt; 4\,096 = 2^{12}\), the maximum bit depth at 20 kHz is &lt;strong&gt;\(N = 11\text{ bits}\)&lt;/strong&gt; (or \(N = 10\text{ bits}\) with 1,024 clean steps)! Asking for 12-bit at 20 kHz fails because \(4096 \times 20\,000 = 81.92\text{ MHz} &amp;gt; 80\text{ MHz}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Voltage resolution with 10-bit PWM (0–1023) on a 7.4 V rail:&lt;/strong&gt;
\(\Delta V = \frac{7.4\text{ V}}{1023} \approx 7.23\text{ mV per step}\) — far finer than motor static friction!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Duty value for 35% average voltage (\(2.59\text{ V}\)):&lt;/strong&gt;
\(\text{duty} = \operatorname{round}(0.35 \times 1023) = 358.\)
Checking pulse width: period \(T = 1/20\,000 = 50\,\mu\text{s}\), so \(t_{\text{on}} = 0.35 \times 50\,\mu\text{s} = 17.5\,\mu\text{s}\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Setting a DC motor’s PWM frequency to 50 Hz (hobby servo frequency!) — at 50 Hz the motor jerks violently at 50 pulses/second and draws huge peak currents. Use 50 Hz &lt;em&gt;only&lt;/em&gt; for hobby servos, and 15–25 kHz for brushed DC motors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If you drop PWM resolution to 8 bits (0–255) on a 7.4 V rail, each step is \(7.4 / 255 \approx 29\text{ mV}\) — still fine for many diff-drive robots, and works cleanly at 20 kHz.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A 7.4 V pack and a duty of 0.35 are the first spin you plan with wheels up. Compute the average voltage. Convert 0.35 into an 8-bit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; argument and into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Someone calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach(18, 1000, 10)&lt;/code&gt; and then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite(18, 255)&lt;/code&gt;, expecting half brightness. What fraction did they actually command, and what count is true half-scale at 10 bits?&lt;/li&gt;
  &lt;li&gt;Why is 100 Hz a poor motor PWM frequency, and why is the same 100 Hz tolerable as a short LED experiment in step 3 of the lab?&lt;/li&gt;
  &lt;li&gt;Name two ESP32 pins you will not use for this breathe, and the pin this lesson does use. What boot-time decision are the avoided pins involved in?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Hints&lt;/summary&gt;

1. $0.35 \times 7.4 \approx 2.6$ V. Eight-bit duty is `round(0.35 * 255) = 89`. `duty_u16` is `round(0.35 * 65535) = 22937`.
2. Ten-bit full scale is 1023, so 255 is about a quarter, not a half. Half-scale is 512.
3. A motor at 100 Hz gets a torque pulse every 10 ms and chatters. An LED at 100 Hz only flickers, and the lab puts the frequency back.
4. Avoid GPIO 0, 2, 12, and 15; they are strap pins read at reset. The listing uses GPIO 18.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html&quot;&gt;ESP32 Arduino LEDC&lt;/a&gt; is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcAttach(pin, freq, resolution)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ledcWrite&lt;/code&gt; reference for core 3.x. Match the resolution to the integer you write.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.micropython.org/en/latest/library/machine.PWM.html&quot;&gt;MicroPython &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;machine.PWM&lt;/code&gt;&lt;/a&gt; documents &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;freq&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;, including the 0 to 65535 range used above.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu item 713&lt;/a&gt; is the TB6612FNG carrier whose input pins expect the PWM you practiced here and whose VM pin is a different supply. Read it before the driver comes out of the bag.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab — GPIO patterns for buttons and LEDs</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter03/03_02_gpio_patterns/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter03/03_02_gpio_patterns</id>
   <content type="html">&lt;p&gt;A single floating input pin can make a robot behave as if it is haunted: waving your hand five centimeters away from an unwired button pin causes its voltage to swing randomly between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; due to ambient 50/60 Hz AC hum. Mastering &lt;strong&gt;GPIO electrical patterns&lt;/strong&gt; — pull-up vs. pull-down resistors, active-low logic, and push-pull vs. open-drain outputs — banishes phantom triggers forever.&lt;/p&gt;

&lt;p&gt;This lab teaches the hardware–software contract behind every button, limit switch, bumper sensor, and status LED on your robot.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lab a button wired from a GPIO to ground reads HIGH when released and LOW when pressed, because the pin mode is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; or the MicroPython equivalent. You will size the LED’s series resistor from the 3.3 V rail, build the same edge-printing sketch in Arduino C++ and in MicroPython, and accept a level change only after the raw reading has been stable for 20 ms. You will recognize three classic faults: a floating input when the pull-up was forgotten, an LED with no resistor, and a button hung between 3.3 V and the pin with no pull-down so the reading never leaves HIGH. Nothing in the sketch drives a motor.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;The &lt;a href=&quot;/contents/en/chapter02/02_04_blink_serial_hello/&quot;&gt;Chapter 02 heartbeat&lt;/a&gt; already proved that your serial monitor matches the board at 115200 and that you can change an LED pin when GPIO 2 is not the lamp on your silk. Lesson 1 of this chapter replaced &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; with a non-blocking clock; the sketches below keep that habit. You need a breadboard, a momentary button, one LED, a resistor near 180 Ω, and jumpers. You do not need the TB6612, a battery, or the chassis.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-high-impedance-cmos-inputs-active-low-logic-and-open-drain-outputs&quot;&gt;Key ideas: High-Impedance CMOS Inputs, Active-Low Logic, and Open-Drain Outputs&lt;/h2&gt;

&lt;p&gt;Three digital-interface patterns appear everywhere in robotics firmware:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Pull-Up &amp;amp; Pull-Down Resistors (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt;):&lt;/strong&gt; An unconnected CMOS input has megaohms of impedance and acts like a radio antenna. An internal pull-up resistor (typically \(30\text{–}50\text{ k}\Omega\)) holds the pin cleanly at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.3 V&lt;/code&gt;) until a button closes to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, pulling it sharply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0 V&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Active-Low Safety Convention:&lt;/strong&gt; Wiring emergency-stop or bumper switches as &lt;strong&gt;active-low&lt;/strong&gt; (or even better, normally-closed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;) means &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW == pressed&lt;/code&gt;, and you only need to route &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; (not a live &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.3 V&lt;/code&gt; wire) out to the exposed metal bumper of the chassis.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Push-Pull vs. Open-Drain Outputs:&lt;/strong&gt; Standard GPIO outputs are &lt;em&gt;push-pull&lt;/em&gt; (actively driving both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt;). Shared buses like &lt;strong&gt;I2C&lt;/strong&gt; use &lt;em&gt;open-drain&lt;/em&gt; outputs that can only pull the line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; and rely on external pull-up resistors to return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;, preventing two chips from ever shorting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.3 V&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Capstone A will treat a button as a human event: clear a fault, or confirm that you meant to leave the stopped state. Teleop commands will arrive later as bytes and then as ROS messages, but the electrical pattern under the button does not change when those bytes show up. If the pin floats, the firmware invents presses and can clear a fault you never acknowledged. If the LED is the stand-in for “I saw the edge,” a missing series resistor makes that stand-in a short. The wheels stay up, and the motor stays off the GPIO, because a wheel driver is a separate chip. This lab is the input and the lamp only.&lt;/p&gt;

&lt;h2 id=&quot;core-teaching&quot;&gt;Core teaching&lt;/h2&gt;

&lt;p&gt;A GPIO is a digital pin that is either an input or an output. As an input with the internal pull-up enabled, the pin is weakly tied to 3.3 V. The button’s other side goes to ground, not to 3.3 V. While the button is released the switch is open, the pull-up wins, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;digitalRead&lt;/code&gt; returns HIGH. While the button is pressed the switch is a short to ground, which is stronger than the pull-up, and the read returns LOW. That inversion surprises people who expect “pressed means true means HIGH.” In this wiring, pressed means LOW. Write the comment in the sketch so future you does not “fix” it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/gpio_pullup_button.png&quot; alt=&quot;Button to ground with the GPIO pull-up&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If the chip has no internal pull-up, copy the same idea with a 10 kΩ resistor from the GPIO to 3.3 V, and still wire the button from the GPIO to ground. Ten kilohms is stiff enough that a finger on the wire does not define the level, and gentle enough that a pressed button only has to sink $3.3 / 10000 = 0.33$ mA.&lt;/p&gt;

&lt;p&gt;The LED needs its own series resistor. A small indicator LED drops about 2.0 V when it is lit, and 8 mA is a bright, safe target from a 3.3 V GPIO. Ohm’s law on the resistor, which sees the leftover voltage, is&lt;/p&gt;

\[R = \frac{V_{CC} - V_F}{I_F} = \frac{3.3 - 2.0}{0.008} = 162.5\,\Omega.\]

&lt;p&gt;The common part in a 1/4 W kit is 180 Ω, which runs the LED a little under the target:&lt;/p&gt;

\[I = \frac{3.3 - 2.0}{180} \approx 7.2\,\mathrm{mA}, \qquad P = I^2 R \approx 9\,\mathrm{mW}.\]

&lt;p&gt;A 1/4 W resistor is nowhere near warm. A 220 Ω part is also fine and only slightly dimmer. Leaving the resistor out asks the GPIO to limit the current by itself. It will not do that politely: the LED or the pin pad is what fails. The cathode of the LED goes toward ground, the anode toward the resistor, and the resistor toward the GPIO. The GPIO sources the current. Stay on the 3.3 V rail. ESP32 pins are not a place to attach the 5 V pin, and they are not a place to attach a motor.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/led_series_resistor.png&quot; alt=&quot;LED, series resistor, and ground&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Contacts do not close in one clean edge. The next lesson photographs that chatter. Today you only need a loop rule that refuses to believe a change until the raw bit has held still for 20 ms. Keep the last raw sample and the time it changed. If the raw value is new, restart the timer. If the raw value has matched itself for 20 ms and it differs from the accepted level, accept it and print one line. Printing every sample floods the monitor and hides the edge you care about. The serial log should say &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt; on the falling accepted edge and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt; on the rising accepted edge, once each.&lt;/p&gt;

&lt;p&gt;The same rule is the debounce you will reuse when an interrupt merely sets a flag. Doing it in the loop now means you can single-step it with a print. Do not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; to implement it. A blocking wait freezes the heartbeat from Lesson 1 for the whole bounce window, and once a motor command exists that wait is the 200 ms problem again, only shorter. Twenty milliseconds of blind time is still the wrong shape. The timestamp test leaves the loop free.&lt;/p&gt;

&lt;p&gt;Three faults show up every term.&lt;/p&gt;

&lt;p&gt;A floating pin is what you get when the button goes to ground and you forget &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.PULL_UP&lt;/code&gt;. Released, the wire is an antenna. The monitor prints press and release with nobody touching it. Fix the mode before you rewrite the debounce.&lt;/p&gt;

&lt;p&gt;An LED with no series resistor may light once, very brightly, and then never again. If it survives, the pin is still being asked for more current than the pad is rated for. Put the 180 Ω part in series even if the lamp “looks fine.”&lt;/p&gt;

&lt;p&gt;A button wired from 3.3 V to the GPIO, with the pull-up left on and no pull-down to ground, reads HIGH when released because of the pull-up and HIGH when pressed because the switch connects 3.3 V. There is no edge. Students describe this as “the button does nothing.” The pin is not broken. Both states are the same voltage. Move the switch to ground, or add a pull-down and stop using the internal pull-up. This course uses the ground wiring.&lt;/p&gt;

&lt;p&gt;Do not drive a motor, a relay coil, or the TB6612’s VM pin from a GPIO. A GPIO can signal a driver’s PWM input later. It cannot be the motor current.&lt;/p&gt;

&lt;p&gt;Arduino C++ for an ESP32. Button on GPIO 4, LED on GPIO 18. Both pins avoid the strapping set discussed in the PWM lesson.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INPUT_PULLUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// released HIGH, pressed LOW&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;gpio edges&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digitalRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BTN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastRaw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastChange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// LED on while held&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;press&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;release&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The LED in this listing follows the held state so you can see the button. The print happens only on the accepted edge.&lt;/p&gt;

&lt;p&gt;MicroPython, same pins if you are on an ESP32 board. On a Pico, move the numbers to free GP pins and keep the pull-up.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PULL_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;gpio edges&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;btn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_change&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEBOUNCE_MS&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;press&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;release&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;You press the button at $t = 1000$ ms and release it at $t = 1600$ ms. The raw pin chatters for 8 ms after each mechanical event and is then steady. The 20 ms rule waits until $t = 1020$ ms before it accepts LOW, prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt;, and turns the LED on. It waits until $t = 1620$ ms before it accepts HIGH, prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt;, and turns the LED off. Samples at 1 ms resolution never appear in the log. The monitor shows&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gpio edges
press
release
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and nothing else for that gesture. If your log instead shows dozens of lines for one click, the stability test is not wrapped around the print. If it shows nothing, measure the pin: released near 3.3 V, pressed near 0 V. A released pin that wanders between 0.8 V and 2 V is floating.&lt;/p&gt;

&lt;p&gt;Suppose the forward voltage is 2.1 V and you want 5 mA from the same 3.3 V rail. The resistor is&lt;/p&gt;

\[R = \frac{3.3 - 2.1}{0.005} = 240\,\Omega,\]

&lt;p&gt;so a 220 Ω or 270 Ω part from the kit is the honest choice. Reusing 180 Ω would run nearer $(3.3-2.1)/180 \approx 6.7$ mA, which is still safe and a bit brighter than you asked. The calculation is the point, not a single catalog number.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;p&gt;Power the board from USB only. Do not connect a battery pack, and do not let any jumper touch a 5 V header.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Unplug USB. Place the button so one leg is GPIO 4 and the opposite leg is GND. Place the LED and the 180 Ω resistor in series between GPIO 18 and GND, anode toward the GPIO. Re-read the two figures before you plug USB back in.&lt;/li&gt;
  &lt;li&gt;Flash the C++ sketch or save the MicroPython script. Open the serial monitor at 115200. You want one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gpio edges&lt;/code&gt; line, then silence.&lt;/li&gt;
  &lt;li&gt;Press and release once, slowly. Expected log: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt; then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt;. The LED lights only while the button is down. Press five times. You want five pairs, not a stream.&lt;/li&gt;
  &lt;li&gt;Delete the pull-up on purpose, run again, and watch the log invent edges. Put the pull-up back. That ten-second experiment is the floating-pin fault, and it belongs in your notes.&lt;/li&gt;
  &lt;li&gt;Record the pin numbers, the resistor value, and a paste of one clean press/release pair. Photograph the breadboard if you can. The checklist at the end of the chapter asks for this log.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the LED is dark while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt; prints, the LED is backwards or on the wrong pin. Trust the serial line first, then turn the LED around. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;press&lt;/code&gt; never prints and the pin measures 3.3 V in both positions, you have built the always-high fault: the switch is on the 3.3 V side.&lt;/p&gt;

&lt;h2 id=&quot;explanation-calculating-pull-up-current-active-low-logic-and-led-sinking-vs-sourcing&quot;&gt;Explanation: Calculating Pull-Up Current, Active-Low Logic, and LED Sinking vs. Sourcing&lt;/h2&gt;

&lt;p&gt;Let’s verify the exact electrical behavior of a standard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; bumper switch and an indicator LED in four steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Button released (open switch):&lt;/strong&gt; Zero current flows through the internal pull-up resistor \(R_{\text{pu}} \approx 45\text{ k}\Omega\), so \(V_{\text{pin}} = 3.3 - 0 = 3.3\text{ V}\) (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;, digital &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Button pressed (closed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;):&lt;/strong&gt; The pin connects directly to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; (\(V_{\text{pin}} = 0\text{ V}\), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt;, digital &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;), and a tiny harmless current flows through \(R_{\text{pu}}\): \(I = V_{\text{DD}} / R_{\text{pu}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Avoid noisy long bumper wires:&lt;/strong&gt; On a 40 cm cable next to DC motors, a weak \(45\text{ k}\Omega\) internal pull-up can pick up motor EMI; adding a stronger external \(4.7\text{ k}\Omega\) or \(10\text{ k}\Omega\) pull-up boosts noise immunity by 5×–10×.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Always abstract active-low in code:&lt;/strong&gt; Write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bool bumper_hit = (digitalRead(PIN_BUMPER) == LOW);&lt;/code&gt; once so the rest of your state machine reads clean English logic.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-comparing-internal-45-kω-vs-external-47-kω-pull-up-on-a-robot-bumper&quot;&gt;Concrete Worked Example: Comparing Internal (45 kΩ) vs. External (4.7 kΩ) Pull-Up on a Robot Bumper&lt;/h3&gt;

&lt;p&gt;Suppose your 3.3 V ESP32 reads a front bumper switch wired to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Case 1 — Internal pull-up (\(R_{\text{pu}} = 45\text{ k}\Omega\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Current while bumper is pressed: \(I = \frac{3.3\text{ V}}{45\,000\,\Omega} \approx 0.073\text{ mA}\) (\(73\,\mu\text{A}\)).&lt;/li&gt;
      &lt;li&gt;Noise resistance: a tiny \(50\,\mu\text{A}\) EMI spike induced by motor brushes creates a voltage glitch of \(\Delta V = 50\,\mu\text{A} \times 45\text{ k}\Omega = 2.25\text{ V}\) — enough to cause a &lt;strong&gt;false bumper trigger&lt;/strong&gt;!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Case 2 — External pull-up (\(R_{\text{pu}} = 4.7\text{ k}\Omega\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Current while bumper is pressed: \(I = \frac{3.3\text{ V}}{4\,700\,\Omega} \approx 0.70\text{ mA}\) (still tiny!).&lt;/li&gt;
      &lt;li&gt;Same \(50\,\mu\text{A}\) EMI spike creates only \(\Delta V = 50\,\mu\text{A} \times 4.7\text{ k}\Omega = 0.235\text{ V}\) — 10× smaller, never crossing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOW&lt;/code&gt; threshold!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Wiring a button between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.3V&lt;/code&gt; and a GPIO pin configured as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt; — pressing the button connects &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt;, so the pin &lt;em&gt;never&lt;/em&gt; changes state! With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt;, the switch must always go between the GPIO pin and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If a 3.3 V GPIO pin drives a green LED (\(V_f = 2.2\text{ V}\)) through a \(220\,\Omega\) resistor, the LED current is \(I = (3.3 - 2.2) / 220 = 5.0\text{ mA}\) — bright, crisp, and well below the 12 mA pin limit.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A red LED is specified at $V_F = 1.8$ V and you budget $I_F = 6$ mA from 3.3 V. Compute $R$, then name the nearest common value you would actually solder, choosing the one that does not exceed 6 mA.&lt;/li&gt;
  &lt;li&gt;The stable level changes from HIGH to LOW at $t = 250$ ms and back to HIGH at $t = 900$ ms, with the 20 ms test already satisfied. Write the serial lines and the LED state in each interval. What must not appear between them?&lt;/li&gt;
  &lt;li&gt;A classmate wires the button from 3.3 V to GPIO 4, calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pinMode(4, INPUT_PULLUP)&lt;/code&gt;, and reports that the lamp never changes. State the voltage in both switch positions and the one wire you move.&lt;/li&gt;
  &lt;li&gt;The internal pull-up is unavailable. You have a 10 kΩ resistor and the same button. Describe the two nodes the resistor joins, and the current it carries while the button is held.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Hints&lt;/summary&gt;

1. $R = (3.3 - 1.8) / 0.006 = 250\,\Omega$. A 270 Ω part stays under 6 mA. A 220 Ω part runs a little over the budget.
2. The log gains `press` at the first accepted edge and `release` at the second. The LED is on only while the accepted level is LOW. Samples between 250 and 900 ms do not print.
3. Both positions sit near 3.3 V, so the accepted level never changes. Move the button&apos;s far side from 3.3 V to GND and leave the pull-up enabled.
4. The resistor joins GPIO to 3.3 V. Held current is about $3.3/10\,\mathrm{k}\Omega = 0.33$ mA. The button&apos;s other side is still GND.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/&quot;&gt;Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pinMode&lt;/code&gt;&lt;/a&gt; lists &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INPUT_PULLUP&lt;/code&gt;, which is the whole reason a released button in this lab reads HIGH.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.micropython.org/en/latest/library/machine.Pin.html&quot;&gt;MicroPython &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;machine.Pin&lt;/code&gt;&lt;/a&gt; documents &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin.PULL_UP&lt;/code&gt; and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;value()&lt;/code&gt; levels used in the second listing.&lt;/li&gt;
  &lt;li&gt;The full course kit, if you have not ordered a board yet, is still the &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Chapter 00 bill of materials&lt;/a&gt;. This lesson only adds the small parts below.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;parts-for-this-lab&quot;&gt;Parts for this lab&lt;/h2&gt;

&lt;p&gt;Prices are the hshop shelf tags checked for this course, in đồng, and they move. The button is a 12 mm round PBS-11B with leads, 10000₫, at &lt;a href=&quot;https://hshop.vn/nut-nhan-nha-tron-pbs-11b-12mm-kem-cap&quot;&gt;hshop.vn/nut-nhan-nha-tron-pbs-11b-12mm-kem-cap&lt;/a&gt;. Any momentary normally-open switch with two legs is the same electrically. The LED pack is 20000₫ at &lt;a href=&quot;https://hshop.vn/bo-5-loai-led-sieu-sang-3mm-thong-dung-5-kind-3mm-transparent-color-led&quot;&gt;hshop.vn/bo-5-loai-led-sieu-sang-3mm-thong-dung-5-kind-3mm-transparent-color-led&lt;/a&gt;. A 3 mm or 5 mm LED both work with the 180 Ω calculation; only the forward voltage changes, and you recompute.&lt;/p&gt;

&lt;p&gt;The breadboard is &lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;hshop.vn/test-board-cammb-102&lt;/a&gt;. Male-to-male jumpers, 40 wires, 20 cm, are &lt;a href=&quot;https://hshop.vn/day-cam-breadboard-duc-duc-20cm-cap-det-40-soi-m-m-jumper-wire&quot;&gt;hshop.vn/day-cam-breadboard-duc-duc-20cm-cap-det-40-soi-m-m-jumper-wire&lt;/a&gt;. The 1/4 W resistor assortment, which is where the 180 Ω part lives, is a Shopee search rather than a single stable product page: &lt;a href=&quot;https://shopee.vn/search?keyword=b%E1%BB%99%20%C4%91i%E1%BB%87n%20tr%E1%BB%9F%201%2F4W&quot;&gt;bộ điện trở 1/4W&lt;/a&gt;. Pick 180 Ω or 220 Ω out of that kit. Do not buy a motor for this chapter, and do not connect any motor you already own to a GPIO.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>C++ and MicroPython on a robot</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter03/03_01_cpp_vs_micropython/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter03/03_01_cpp_vs_micropython</id>
   <content type="html">&lt;p&gt;In real robotics teams, the “C++ vs. Python” debate is a false dichotomy — almost every serious robot uses &lt;strong&gt;both&lt;/strong&gt;, just at different layers of the stack. C++ owns the inner millisecond-critical loops (encoder interrupts, PWM updates, PID control, safety watchdogs) where predictable execution time and zero garbage-collection pauses are sacred. Python owns the outer layers (offline calibration, step-response plotting, rapid prototyping, and high-level mission scripting) where human iteration speed dominates.&lt;/p&gt;

&lt;p&gt;This lesson gives you a quantitative criterion for choosing between C++ and MicroPython on your robot so you write the right code in the right language.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can keep one heartbeat alive in both C++ and MicroPython without parking the CPU inside a long sleep, and you can point at the exact line that would make a 300 ms command timeout late. You will use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; on the Arduino core and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; together with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff()&lt;/code&gt; in MicroPython, which replaces the blocking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; heartbeat from the &lt;a href=&quot;/contents/en/chapter02/02_04_blink_serial_hello/&quot;&gt;Chapter 02 blink and serial lesson&lt;/a&gt;. You will compute how a 200 ms sleep hides a 300 ms silence limit on a differential-drive base, and you will decide, for a new sensor versus the driving robot, which language you want while the timeout is still present in either one.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You already have a board that prints a climbing millisecond stamp and toggles an LED, from the Chapter 02 heartbeat. ESP32 students did that from PlatformIO; Pico students did it from the MicroPython REPL. You can change a pin number when the listing does not match the silk. You do not need PWM, interrupts, or a motor driver yet. What you are retiring is the habit at the bottom of that heartbeat: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep&lt;/code&gt; for half a second was acceptable only while the sketch had nothing else to notice.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-compiled-native-code-deterministic-latency-and-fixed-point--float-performance&quot;&gt;Key ideas: Compiled Native Code, Deterministic Latency, and Fixed-Point / Float Performance&lt;/h2&gt;

&lt;p&gt;Three computer-systems principles explain the real-world gap between C++ and MicroPython on microcontrollers:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Ahead-of-Time Compilation vs. Bytecode Interpretation:&lt;/strong&gt; C++ compiles directly to native CPU machine instructions with aggressive compiler optimizations (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-O2&lt;/code&gt;), often running &lt;strong&gt;20× to 100× faster&lt;/strong&gt; than interpreted MicroPython loops on arithmetic-heavy control code.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deterministic Worst-Case Execution Time (WCET):&lt;/strong&gt; Control theory assumes a constant sampling period \(\Delta t\). C++ with static memory allocation has tightly bounded WCET (microseconds of jitter), whereas MicroPython’s dynamic heap and garbage collector introduce occasional multi-millisecond spikes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Type Safety &amp;amp; Hardware Register Access:&lt;/strong&gt; C++ exposes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;volatile&lt;/code&gt;, fixed-width integers (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int32_t&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint16_t&lt;/code&gt;), hardware timer registers, and FreeRTOS tasks directly — essential when synchronizing an Interrupt Service Routine (ISR) with a main control loop.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Capstone A is a two-wheel differential-drive base. A teleop command eventually becomes two PWM signals on a TB6612, one for each wheel, and the firmware is the only place that can force both of those signals to zero when the commands stop. The rule you will implement later is short: if nothing valid arrives for 300 ms, both channels are commanded to 0, and the wheels stay up until that behavior has been watched. A language that cannot see the clock during that window will keep the last duty after the joystick has gone quiet. MicroPython and C++ can both finish Capstone A. The failure this lesson is about is a sleep that is “only” 200 ms, which is already long enough to step over the 300 ms mark.&lt;/p&gt;

&lt;h2 id=&quot;core-teaching&quot;&gt;Core teaching&lt;/h2&gt;

&lt;p&gt;Both languages run on the microcontroller you flashed in Chapter 02. They share GPIO numbers, the 3.3 V logic rail, and the USB serial port. Capstone A does not require you to pick a personality. It requires a loop that keeps asking how old the last command is.&lt;/p&gt;

&lt;p&gt;MicroPython keeps a read-eval-print loop on the chip. You paste a function, call it, and read the print before you commit to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt;. That edit cycle is the right tool while a sensor’s wiring is still a guess. The cost sits in the runtime. A garbage collector sometimes pauses the program to reclaim memory. Those pauses are often a few milliseconds and sometimes a few tens of milliseconds. They are not a schedule you wrote. Joystick teleop in this course sits around 20 to 50 Hz. The periods are&lt;/p&gt;

\[T_{20} = \frac{1}{20} = 50\,\mathrm{ms}, \qquad T_{50} = \frac{1}{50} = 20\,\mathrm{ms}.\]

&lt;p&gt;A short collector pause jitters one of those periods. It does not, by itself, pretend that 300 ms of silence has passed, provided the loop checks the timestamp again as soon as it runs. The same pause is a poor fit for bit-banged protocols at a high rate: a software-timed serial link or a tight sensor clock will drop edges while the collector runs. Do not use MicroPython for that job. Do use it for human-rate teleop if you refuse long sleeps.&lt;/p&gt;

&lt;p&gt;C++ on the Arduino core, built in PlatformIO the way Chapter 02 did, compiles on your laptop and uploads a binary. There is no garbage collector in the language you are writing, and an interrupt is a function with a trigger you can name. That is why a later encoder is easier to reason about in C++. The cost is the edit cycle: a one-line change waits on a compile and an upload. That wait is acceptable for the driving robot. It is a bad reason to keep &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; in the control loop “because the upload already took long enough.”&lt;/p&gt;

&lt;p&gt;The heartbeat below is the same behavior in both languages. An LED toggles every 500 ms, and every other pass falls through so a future command check still runs. Arduino stores timestamps in an unsigned 32-bit count from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;. Subtracting two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint32_t&lt;/code&gt; values is wrap-safe across the rollover at about 49.7 days. Do not put those timestamps in a signed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt;. MicroPython’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; also wraps, on a modulus you must not guess, so the only legal elapsed-time question is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff(now, last)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On many ESP32 MicroPython ports a completely tight &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while True&lt;/code&gt; starves the runtime and can trip the watchdog. A one-millisecond &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep_ms(1)&lt;/code&gt; at the bottom of the loop is a yield. It is not the bug. The bug is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(0.2)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(1)&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(200)&lt;/code&gt;. Once motors exist, the control loop may block for only a few milliseconds. A 1 ms yield still evaluates a 300 ms timeout hundreds of times inside the window. A 200 ms sleep evaluates it on a grid that skips the deadline.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LED_PIN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// the pin you already blinked in Chapter 02&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BLINK_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastBlink&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastBlink&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;heartbeat, non-blocking&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastBlink&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BLINK_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastBlink&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;t=%lu led=%d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Command-age check belongs on this pass, including passes that do not toggle.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# change to the Chapter 02 LED you already trust
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BLINK_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;heartbeat, non-blocking&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BLINK_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# yield only; never sleep(0.2) once a timeout exists
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Read the two listings until the match is boring. Each stores a timestamp, asks a wrap-safe question, toggles only when the answer is yes, and otherwise falls through. That fall-through is where the 300 ms test will live. A practical split that has held up on this bench: prototype a new sensor in MicroPython while the wiring is still moving, and ship the driving robot in C++ if the timing gets flaky. Either language is acceptable for Capstone A when the timeout is actually in the loop.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;The safety predicate for Capstone A is&lt;/p&gt;

\[\text{cut both PWM channels if } t - t_{\text{cmd}} \ge 300\,\mathrm{ms}.\]

&lt;p&gt;Put a 200 ms sleep at the bottom of the loop and the predicate is only evaluated on a 200 ms grid. A command arrives at $t = 0$, which refreshes $t_{\text{cmd}}$. The loop then sleeps. The sample instants are 200 ms, 400 ms, 600 ms. At 200 ms the age is still under the limit, so the TB6612 keeps the previous duty. The next look is at 400 ms:&lt;/p&gt;

\[t_{\text{stop}} = \left\lceil \frac{300}{200} \right\rceil \times 200 = 400\,\mathrm{ms}.\]

&lt;p&gt;The code does not observe the 300 ms mark. It wakes up already late by&lt;/p&gt;

\[400 - 300 = 100\,\mathrm{ms}.\]

&lt;p&gt;During those extra 100 ms both wheels still have the last speed command, which is exactly the case the timeout exists to prevent. Comparing 200 with 300 and concluding “the sleep is shorter, so it is safe” compares the wrong pair. The sleep has to be a few milliseconds, short next to the 20 ms period of a 50 Hz stream, not merely shorter than the timeout. Ten missed command periods fit inside one 200 ms nap:&lt;/p&gt;

\[\frac{200\,\mathrm{ms}}{20\,\mathrm{ms}} = 10.\]

&lt;p&gt;A garbage-collection pause of about 15 ms on an otherwise cooperative MicroPython loop is a different story. It can stretch one teleop period. It does not hide the 300 ms boundary, because the check runs again as soon as the pause ends. That is why a collector pause is acceptable at 20–50 Hz and a 200 ms sleep is not.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;p&gt;Stay on the LED from Chapter 02. Do not add a motor, and do not wire a TB6612 in this lesson. You are measuring whether the loop can see a clock.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Load the listing that matches your board. On MicroPython, run it from the REPL before you save it as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt;. Confirm one boot line, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t=...&lt;/code&gt; lines about every 500 ms, with the LED following the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;led&lt;/code&gt; field. A second boot line means a reset, which Chapter 02 already taught you to treat as a fault rather than a blink.&lt;/li&gt;
  &lt;li&gt;Add a pass counter that prints once per second using the same non-blocking test, then clears. Write the count in your notes. That number is this sketch’s iteration rate, not a universal constant. A MicroPython loop that yields 1 ms will land near 1000, and that is the yield you asked for.&lt;/li&gt;
  &lt;li&gt;Insert &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(200)&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(0.2)&lt;/code&gt; once per pass. Watch the LED and the counter. The cooperative rate collapses, and a 300 ms silence can only be noticed on the 200 ms grid from the worked example.&lt;/li&gt;
  &lt;li&gt;Remove the 200 ms call before you leave the bench. The file you keep is the non-blocking heartbeat. In your notes, record language, pin, iterations per second, and the sentence “a 200 ms sleep stops the robot at 400 ms.”&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;explanation-quantifying-phase-lag-caused-by-execution-latency--jitter&quot;&gt;Explanation: Quantifying Phase Lag Caused by Execution Latency &amp;amp; Jitter&lt;/h2&gt;

&lt;p&gt;Why do robotics engineers obsess over 2 ms vs. 0.05 ms of loop latency? Because in feedback control, &lt;strong&gt;pure time delay \(T_d\) adds phase lag&lt;/strong&gt; that erodes stability margin! Trace the exact 4-step calculation:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Relate time delay \(T_d\) to phase lag at frequency \(f\):&lt;/strong&gt;
\(\Delta \phi = -360^\circ \cdot f \cdot T_d.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute delay from sampling + computation:&lt;/strong&gt; A digital controller running at period \(T_s\) with computation/GC delay \(T_c\) introduces an effective delay \(T_d \approx \frac{T_s}{2} + T_c\) (zero-order hold half-period plus compute lag).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compare phase lag at the robot’s control bandwidth \(f_c\)&lt;/strong&gt; (typically \(5\text{–}15\text{ Hz}\) for a small wheeled robot, higher for balancing robots).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Apply the rule of thumb:&lt;/strong&gt; Keep loop-induced phase lag at crossover frequency below \(10^\circ\text{–}15^\circ\) so your PID loop stays crisp and non-oscillatory.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-comparing-phase-lag-of-100-hz-c-vs-25-hz-micropython-at-f_c--10text-hz&quot;&gt;Concrete Worked Example: Comparing Phase Lag of 100 Hz C++ vs. 25 Hz MicroPython at \(f_c = 10\text{ Hz}\)&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Controller Setup&lt;/th&gt;
      &lt;th&gt;Period \(T_s\)&lt;/th&gt;
      &lt;th&gt;Compute/GC \(T_c\)&lt;/th&gt;
      &lt;th&gt;Effective Delay \(T_d = T_s/2 + T_c\)&lt;/th&gt;
      &lt;th&gt;Phase Lag at \(f_c = 10\text{ Hz}\)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;C++ at 100 Hz&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(10\text{ ms}\)&lt;/td&gt;
      &lt;td&gt;\(0.1\text{ ms}\)&lt;/td&gt;
      &lt;td&gt;\(5.1\text{ ms}\)&lt;/td&gt;
      &lt;td&gt;\(-360^\circ(10)(0.0051) = -18.4^\circ\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;MicroPython at 25 Hz (+3 ms GC)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;\(40\text{ ms}\)&lt;/td&gt;
      &lt;td&gt;\(3.0\text{ ms}\)&lt;/td&gt;
      &lt;td&gt;\(23.0\text{ ms}\)&lt;/td&gt;
      &lt;td&gt;\(-360^\circ(10)(0.0230) = -82.8^\circ!\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;At \(10\text{ Hz}\) bandwidth, a 25 Hz MicroPython loop with a 3 ms GC pause eats &lt;strong&gt;\(82.8^\circ\) of phase margin&lt;/strong&gt; — instantly destabilizing a fast-reacting controller (like a self-balancing robot), whereas C++ at 100 Hz (or 200 Hz: only \(-9.4^\circ\)!) stays rock-solid.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Writing everything in C++ too early when you just want to verify a brand-new I2C sensor’s register map — prototyping sensor reads in 10 lines of MicroPython or Python takes 2 minutes, then porting the verified math to C++ takes 10 minutes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If your C++ loop runs at \(200\text{ Hz}\) (\(T_s = 5\text{ ms}\)) with \(T_c = 0.1\text{ ms}\), effective delay is \(2.6\text{ ms}\) and phase lag at \(10\text{ Hz}\) is only \(-9.36^\circ\).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Your cooperative loop completes 25000 passes in one second. How many of those passes disappear during a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(200)&lt;/code&gt;, and what is the first time at which a 300 ms silence test can succeed if it is only evaluated after each such delay?&lt;/li&gt;
  &lt;li&gt;A classmate’s MicroPython blink is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(0.5)&lt;/code&gt; with a serial read after the sleep. Name the two clock calls that let the LED still toggle every 500 ms while the serial byte is inspected on every pass.&lt;/li&gt;
  &lt;li&gt;Commands are sent at 40 Hz and a collector pause of about 12 ms occurs once. Does that pause by itself force the stop rule? Would you accept the same pause while bit-banging a protocol whose bits are 8 µs wide?&lt;/li&gt;
  &lt;li&gt;You are bringing up a new distance sensor this afternoon, and the driving robot next week has started to miss its 300 ms cutoff under load. Which language do you reach for in each sitting, and what must already be true in both?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Hints&lt;/summary&gt;

1. Pass time is $1/25000$ seconds, so a 0.2 s block erases $0.2 \times 25000$ passes. The silence test on a 200 ms grid first succeeds at 400 ms, the same ceiling used in the worked example.
2. Store `last = time.ticks_ms()`. Toggle when `time.ticks_diff(time.ticks_ms(), last) &amp;gt;= 500`, and read the byte on the passes that do not toggle. A `sleep_ms(1)` yield is optional. `sleep(0.5)` is the line you delete.
3. Twelve milliseconds is far under 300 ms, so one pause does not mean the link died. An 8 µs bit cannot survive a 12 ms stall. That is the bit-bang case MicroPython is the wrong tool for.
4. Prototype the sensor in MicroPython. Move the driving loop to C++ if the cutoff is already flaky. In both sittings the timeout has to be a real comparison against a timestamp, not a comment.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.micropython.org/en/latest/reference/isr_rules.html&quot;&gt;MicroPython ISR rules&lt;/a&gt; are the constraint you will hit the moment a callback replaces the REPL. The same page is why a later interrupt lesson forbids allocating inside the handler.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.arduino.cc/reference/en/language/functions/time/millis/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;&lt;/a&gt; documents the unsigned millisecond clock used in the C++ heartbeat, including the wrap at about 49.7 days.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.platformio.org/&quot;&gt;PlatformIO&lt;/a&gt; is the build path Chapter 02 already used for the ESP32 sketch. The longer compile is the trade you accept when you leave the REPL.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 02 lab checklist</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter02/02_06_ch02_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter02/02_06_ch02_lab_checklist</id>
   <content type="html">&lt;p&gt;You now have a working microcontroller toolchain, a non-blocking heartbeat, reliable serial telemetry, and a verified pin/power map. This Chapter 02 sign-off checklist locks down your MCU foundation before Chapter 03 introduces timers, PWM, interrupts, and firmware state machines.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;objectives&quot;&gt;Objectives&lt;/h2&gt;

&lt;p&gt;You will close Chapter 02 only with evidence you can point at. You will record the toolchain and its version string. You will show that the onboard LED blinks at a period you chose, and you will paste the serial heartbeat rather than describe it. You will finish a pin card for the board on the desk: which GPIO is the LED, and which pins are safe outputs. You will write the two rail measurements, 3.3 V and 5 V or VBUS. You will add one sentence that names the motor pins you will not use yet, VM and the battery. If a tick fails, you will reopen the lesson that taught it instead of buying a second board.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 1 through 5 are the evidence this page consumes. You need the decision sentence (ESP32 dev board or a named Pico), the toolchain that matches it, the heartbeat log or an honest blank, and the pin card. A meter is required for the rail tick. The full cart remains Chapter 00 (&lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM and shopping&lt;/a&gt;). Re-read the wheels-up sentence in the heartbeat sketch so this checklist does not “pass” a robot that can already roll.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-reproducible-firmware-baselines-and-interface-contracts&quot;&gt;Key ideas: Reproducible Firmware Baselines and Interface Contracts&lt;/h2&gt;

&lt;p&gt;Three engineering disciplines turn a Chapter 02 bring-up into a reusable platform:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Pinned Pin-Map Header (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pins.h&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pins.py&lt;/code&gt;):&lt;/strong&gt; Centralizing every GPIO number in one file eliminates “magic numbers” scattered across code and lets you verify zero pin collisions in seconds.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deterministic Telemetry Schema:&lt;/strong&gt; Fixing a versioned CSV header (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms,state,v_bat&lt;/code&gt;) lets your host Python scripts plot data across chapters without rewriting parsers.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cold-Boot Recovery Test:&lt;/strong&gt; Power-cycling the robot on battery alone (no USB cable!) proves that your firmware boots cleanly without waiting forever on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while (!Serial)&lt;/code&gt; and without strapping-pin conflicts.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters-for-capstone-a&quot;&gt;Why it matters for Capstone A&lt;/h2&gt;

&lt;p&gt;Chapter 07 asks this microcontroller to teleoperate a differential-drive base: two PWM channels toward a TB6612, a battery on VM, and wheels that stay off the ground until you are ready. If commands stop for about 300 ms, both duties go to zero. That chapter assumes the ticks on this page were passed with notes. A student who arrives still unsure which pin is ground will debug a timeout while the chip is resetting. Chapter 08 can carry the same teleop over Wi-Fi on an ESP32, and it will happily transport a stream from a board that browns out. The graph then shows a link that “flakes.” The flake was a skipped meter reading or a VM wire that was never supposed to be attached. This checklist is the pause that keeps later software from hiding an electrical problem.&lt;/p&gt;

&lt;h2 id=&quot;six-ticks-each-with-a-token&quot;&gt;Six ticks, each with a token&lt;/h2&gt;

&lt;p&gt;A tick is a condition with evidence, not a topic you remember reading. Chapter 02 has six. Each one is either passed with a pointer into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; or left open. A half-tick (“it mostly worked”) is an open tick.&lt;/p&gt;

&lt;p&gt;The toolchain tick is a version you wrote down. Arduino IDE: the version from the About box, plus the installed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32&lt;/code&gt; package version from Boards Manager. PlatformIO: the string from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pio --version&lt;/code&gt;, and the line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board = esp32dev&lt;/code&gt; still present in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt;. Pico: the Thonny version or the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt; version, and the UF2 filename, including whether it was &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI_PICO&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI_PICO2&lt;/code&gt;. “I installed something” is not a version.&lt;/p&gt;

&lt;p&gt;The blink tick is an onboard LED you watched, at a period you chose and wrote as a number. The heartbeat lab’s period is 500 ms, a toggle every 250 ms, 2 Hz. If you chose a different period on purpose, write that number. The power LED does not count. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; does not count until you have checked it against the silk. The note records the GPIO or the Pico alias (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt; or 25).&lt;/p&gt;

&lt;p&gt;The serial tick is the heartbeat paste. Ten lines, millisecond stamps climbing by about the period you claimed, one banner, no second boot line inside the block. The fields are time and state. A sentence that says “it counted fine” fails, because the later brownout comparison needs the actual staircase. The baud in the notes is 115200 if you are on the ESP32 sketch from this chapter. A monitor left at 9600 is a failed tick even when the LED is flashing.&lt;/p&gt;

&lt;p&gt;The pin-card tick is a drawing or a markdown table for the board you have, not for a random image. It names the LED GPIO, at least one safe output you have checked (not a strap you intend to pull hard, not the UART pins the USB chip is using, not a power pin), and the grounds. Labels copied from a different clone fail when they disagree with your header. On a Pico W the safe-output list must not call GPIO 25 the onboard LED.&lt;/p&gt;

&lt;p&gt;The meter tick is two numbers, USB only, battery absent. About 4.7 V to 5.1 V on the 5 V pin or on VBUS, and about 3.25 V to 3.35 V on 3V3. Write the numbers. A diode-dropped 4.82 V is a pass with the digits visible. “5 V ok” is a fail. A 3V3 pin at 4.9 V is an unplug, not a pass.&lt;/p&gt;

&lt;p&gt;The motor-pin tick is one sentence that names what you will not use yet. A passing sentence: “VM on the TB6612 and the 2S battery stay disconnected; the pack must not touch GPIO, 3V3, 5V/VIN, VSYS, or VBUS.” Wheels-up belongs in the same note. A tick that says “motors later” without naming VM and the battery is still open.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;The checklist stops before the motor supply&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The drawing is the sequence the ticks enforce. USB, a blink, and a serial heartbeat are the proof the logic rail is alive. The driver and the battery are to the right of a line this chapter does not cross.&lt;/p&gt;

&lt;p&gt;Order matters. The landscape lesson’s bring-up was USB, onboard LED, serial at 115200, a button, PWM on an unloaded pin, and only then a driver and a battery. This chapter’s six ticks stop before that driver. Unloaded PWM can be a later exercise. It is not permission to clip a motor onto a GPIO because the checklist is done.&lt;/p&gt;

&lt;p&gt;If the parcel has not arrived, you may record the toolchain version and a pin card traced from the exact listing you paid for. Leave blink, serial, and the two voltages blank and labeled pending. Do not paste imagined serial output. A dry page is a way to use the waiting time. It is not a passed chapter.&lt;/p&gt;

&lt;h2 id=&quot;remediation&quot;&gt;Remediation&lt;/h2&gt;

&lt;p&gt;When a tick fails, reopen the lesson that taught the missing evidence.&lt;/p&gt;

&lt;p&gt;No version string: open About, or run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pio --version&lt;/code&gt;, or print the MicroPython implementation from the REPL, and write the text. That is the tooling lesson, ESP32 or Pico, depending on the board.&lt;/p&gt;

&lt;p&gt;No blink, or a blink on a pin you have not identified: return to the ESP32 upload lesson or the Pico UF2 lesson, then to the heartbeat lesson. Read the silk. GPIO 2 is common on DevKit V1 boards and wrong on others. Pico W uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt;, not GPIO 25.&lt;/p&gt;

&lt;p&gt;Serial missing, garbage, or a repeating banner: heartbeat lesson. Set 115200. If the banner repeats and no motor is connected, swap the cable and remove jumpers before you rewrite the print. If VM is still wired, unplug VM. That brownout is not a software bug.&lt;/p&gt;

&lt;p&gt;Pin card missing or copied from the other family’s board: pinout lesson. Draw 5 V or VBUS, 3V3, GND, the LED pin, and one safe output.&lt;/p&gt;

&lt;p&gt;Voltages missing or written as “ok”: pinout lesson, USB only. If 3V3 is far from 3.3 V, stop. Do not attach the pack to learn more.&lt;/p&gt;

&lt;p&gt;The motor sentence missing: write it now, from the pinout lesson’s worked example. The 2S pack may touch VM. It must not touch the microcontroller header.&lt;/p&gt;

&lt;p&gt;Buying a second ESP32 because a tick is open usually duplicates the same cable mistake. Replace a charge-only lead first.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Two notebooks, both honest.&lt;/p&gt;

&lt;p&gt;Hà’s board arrived last week. The notes read: Arduino IDE 2.3.2 with esp32 package 3.0.7; NodeMCU-32S; CH340 as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COM5&lt;/code&gt;; onboard LED on GPIO 2, confirmed on the silk, toggling every 250 ms; ten heartbeat lines stepping by 498 ms to 505 ms under one banner, monitor at 115200; pin card with 5V, 3V3, GND, LED GPIO 2, safe output GPIO 4; meter 4.91 V and 3.31 V, USB only; sentence “VM and the 2S battery stay off; pack positive never touches 5V, 3V3, or any GPIO.” All six ticks pass. The TT motors stay in the box. Wheels, when the chassis exists, stay up.&lt;/p&gt;

&lt;p&gt;Khoa’s Pico 2 is on a truck. The notes read: Thonny 4.1.4 installed, UF2 name copied from the RPI_PICO2 page so the RP2040 image is not the one that gets flashed, pin card traced from the listing photo with VBUS, VSYS, 3V3, GND, LED GP25, safe output GP16, and the sentence “no 5 V on GP pins; VM and the battery are not in this parcel.” Blink, serial, and the two voltages are the word &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pending&lt;/code&gt;. Khoa does not tick the chapter complete.&lt;/p&gt;

&lt;p&gt;A third note, rejected, is the one the exercises use.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Open &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. Create a section titled with the date and “Chapter 02 gates.” For each of the six ticks, write PASS plus the evidence, or OPEN plus the lesson you will repeat. If the board is absent, mark blink, serial, and the meter as pending in those words. Read the section once. Any tick you cannot point at with a finger — a version string, a line of serial, a voltage, a pin number, the words VM and battery — becomes OPEN before you save.&lt;/p&gt;

&lt;p&gt;A chapter pass is all six ticks PASS, wheels-up sentence present, no motor connected. A dry run is toolchain, decision, and a photo-traced pin card, with the three measured ticks explicitly pending. There is no third state called “close enough.”&lt;/p&gt;

&lt;h2 id=&quot;explanation-verifying-battery-only-cold-boot-and-telemetry-integrity&quot;&gt;Explanation: Verifying Battery-Only Cold Boot and Telemetry Integrity&lt;/h2&gt;

&lt;p&gt;Many students pass every bench test while tethered to USB, then watch their robot sit lifeless the first time they unplug USB and flip the battery switch. Audit these four failure modes:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Check for blocking USB waits:&lt;/strong&gt; Code like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while (!Serial) {}&lt;/code&gt; (common on native-USB boards like Pico/Leonardo) freezes forever when no laptop is attached. Always add a timeout (e.g., 2 seconds max).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify strapping-pin states at reset:&lt;/strong&gt; Measure &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO0&lt;/code&gt; (must be HIGH) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO12&lt;/code&gt; (must be LOW on 3.3 V flash ESP32) before pressing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EN/RST&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Measure steady heartbeat period:&lt;/strong&gt; Time 10 full LED cycles with a stopwatch; if expected period is \(1.00\text{ s}\), 10 cycles should take \(10.0 \pm 0.2\text{ s}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check zero dropped serial lines:&lt;/strong&gt; Over 100 consecutive telemetry lines, verify monotonic timestamps &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms[k] - t_ms[k-1] == period_ms&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-computing-loop-jitter-from-a-5-line-serial-telemetry-log&quot;&gt;Concrete Worked Example: Computing Loop Jitter from a 5-Line Serial Telemetry Log&lt;/h3&gt;

&lt;p&gt;Suppose your firmware targets a 20 Hz telemetry stream (expected \(\Delta t = 50\text{ ms}\)) and logs five consecutive timestamps (in ms): &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1000, 1050, 1101, 1150, 1200&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Compute inter-sample intervals \(\Delta t_k\):&lt;/strong&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50 ms, 51 ms, 49 ms, 50 ms&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Mean period and peak jitter:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Mean: \(\overline{\Delta t} = \frac{50 + 51 + 49 + 50}{4} = 50.00\text{ ms}\) (zero drift — proving &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last += period&lt;/code&gt; works!).&lt;/li&gt;
      &lt;li&gt;Peak jitter: \(\max \lvert \Delta t_k - 50 \rvert = 1\text{ ms}\) (2% of period — completely normal for 1 ms &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; tick quantization).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Contrast with a hidden &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(20)&lt;/code&gt; bug:&lt;/strong&gt; If intervals read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;70, 70, 70, 70 ms&lt;/code&gt;, your actual frequency collapsed to \(1000/70 = 14.3\text{ Hz}\) — caught immediately by checking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Δt&lt;/code&gt;!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Testing only with USB plugged in and forgetting that the battery-powered 5 V buck rail has slightly different startup ramp timing than USB 5 V.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; Disconnect USB, switch ON battery power, and confirm the heartbeat LED begins blinking within 2 seconds — that is your official green light for Chapter 03.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;p&gt;Use this fake note. It is not a pass.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Date: 23 Sep 2026
Board: ESP32 NodeMCU
Toolchain: Arduino IDE installed
LED: GPIO 2, it blinked I think
Monitor baud: 9600
Serial: saw some characters, did not copy them
Rails: 5V ok, 3.3 ok
Next: PWM the motor from GPIO 2, battery soon
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;List every tick this note fails, and the evidence token that is missing for each.&lt;/li&gt;
  &lt;li&gt;The sketch from the heartbeat lesson calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.begin(115200)&lt;/code&gt;. What does a monitor at 9600 show, and what single change repairs the serial tick?&lt;/li&gt;
  &lt;li&gt;“GPIO 2, it blinked I think” is unverified. What must be true on the silk before GPIO 2 can be the blink tick, and what do you write if the lamp is on another pin?&lt;/li&gt;
  &lt;li&gt;Rewrite the rails line and the motor line so those two ticks could pass. Invent no voltages you did not measure; use the form the tick requires and mark numbers pending if the meter has not been used.&lt;/li&gt;
  &lt;li&gt;Which lesson do you reopen first, and why is a second dev board the wrong purchase?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Answers&lt;/summary&gt;

1. Toolchain: no version string. Blink: “I think” is not a period you chose and not a silk check. Serial: no pasted lines, and the baud is 9600. Meter: “ok” is not two numbers. Motor pins: the note plans to PWM a motor from GPIO 2 and attach a battery, which is the opposite of naming VM and the battery as unused. The pin card is missing entirely, so the safe-output tick is open too.
2. The pane fills with garbage or with characters that are almost letters, while the LED can still blink. Set the monitor to 115200. Do not change the sketch to 9600 to match a habit from an Uno tutorial.
3. The silk or the vendor diagram has to show the onboard LED on GPIO 2. Many DevKit V1 boards do; some boards do not. If the lamp is elsewhere, write that GPIO, watch it blink at the period you chose, and leave GPIO 2 off the card until you know what it is. `LED_BUILTIN` still needs the same check.
4. A passing rails line has digits, for example “USB only, 5V pin ____ V, 3V3 pin ____ V, battery not connected,” with the blanks filled from the meter. A passing motor line: “VM and the 2S battery stay disconnected; the pack must not touch GPIO, 3V3, or 5V/VIN.” GPIO 2 is not a motor pin.
5. Reopen the heartbeat lesson first if the baud and the missing paste are the blockers, and the pinout lesson for the rails and the VM sentence. A second board repeats the same missing notes. A charge-only cable, if the port itself is absent, is the part to replace.

&lt;/details&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy only what an open tick needs. A missing data cable: &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;. A missing brain: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;ESP32 NodeMCU-32S CH340, 190 000 ₫&lt;/a&gt; or &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32, 265 000 ₫&lt;/a&gt;. The &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;ESP32-S3-WROOM-1 at 135 000 ₫&lt;/a&gt; is a bare module and does not close the toolchain tick. Pico track, prices read on 23 September 2026 from &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;the Pico search&lt;/a&gt;: Pico 2 at 195 000 ₫ (&lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-rp2350&quot;&gt;product page&lt;/a&gt;) and Pico 2 W at 275 000 ₫ (&lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-w-rp2350&quot;&gt;product page&lt;/a&gt;). Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=esp32%20devkit%20ch340&quot;&gt;ESP32 DevKit CH340&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=raspberry%20pi%20pico%202&quot;&gt;Pico 2&lt;/a&gt;. &lt;a href=&quot;https://www.thegioiic.com/esp32-devkitc-32u-module-wifi-bluetooth-2-4ghz&quot;&gt;DevKitC-32U at Thế Giới IC&lt;/a&gt; is a board-style page; confirm USB. &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; is a component counter. Do not buy a motor or a TB6612 to finish Chapter 02. Those lines live in the &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Chapter 00 cart&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.platformio.org/en/latest/boards/espressif32/esp32dev.html&quot;&gt;PlatformIO &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;&lt;/a&gt; — the board id the ESP32 toolchain tick expects by default.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html&quot;&gt;Installing arduino-esp32&lt;/a&gt; — where the package version in your notes comes from.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micropython.org/download/&quot;&gt;MicroPython downloads&lt;/a&gt; — the UF2 filename the Pico tick must name, RP2040 and RP2350 separated.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;ESP32 datasheet&lt;/a&gt; — check a “safe output” against the electrical table before you believe a blog pinout.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Pinouts, logic levels, and power rails</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter02/02_05_pinouts_power_rails/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter02/02_05_pinouts_power_rails</id>
   <content type="html">&lt;p&gt;On a microcontroller dev-board, two pins sitting 2.54 mm apart can have completely opposite destinies: one is a clean 3.3 V GPIO, the next is connected straight to the 5 V USB bus, and a third controls whether the chip boots from Flash or hangs silently. Mapping your board’s &lt;strong&gt;pinout and power-rail tree&lt;/strong&gt; on a single page before wiring is the cheapest insurance policy in robotics.&lt;/p&gt;

&lt;p&gt;This lesson turns raw pinout diagrams into an engineering pin-allocation sheet for Capstone A.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;objectives&quot;&gt;Objectives&lt;/h2&gt;

&lt;p&gt;You will name, on your own board, the USB 5 V pin and the regulated 3.3 V pin, and you will measure both with USB only. You will treat every ESP32 and Pico GPIO as a 3.3 V pin that 5 V can kill. You will place a 2S pack on the TB6612 VM pin in a drawing, and you will list the header pins that pack must never touch. You will level-shift an HC-SR04 echo on paper, with a common ground, before any sensor is wired. You will leave the battery disconnected for the whole lab.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;The heartbeat lab has already proved the board enumerates and that you know which LED pin you own. You can set a meter to DC volts and put the black lead on a ground you can name. Chapter 01’s resistor divider is the only circuit math required. No motor supply is connected. The full cart, including the driver and the pack, is the Chapter 00 shopping lesson (&lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM and shopping&lt;/a&gt;). You are not buying the pack in order to finish this page.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-power-rail-domains-pin-multiplexing-and-current-sourcing-limits&quot;&gt;Key ideas: Power-Rail Domains, Pin Multiplexing, and Current Sourcing Limits&lt;/h2&gt;

&lt;p&gt;Three architectural facts govern safe MCU pin and power-rail design:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Multiple Power Domains (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBAT&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V/VIN&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;):&lt;/strong&gt; Never draw motor or servo current from an MCU board’s onboard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; pin (typically limited to \(\approx 300\text{–}500\text{ mA}\) total, &lt;em&gt;including&lt;/em&gt; the MCU itself). Route &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBAT&lt;/code&gt; to the motor driver, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; buck to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIN&lt;/code&gt;/5 V sensors, and keep &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; strictly for low-current logic and I2C pull-ups.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pin Multiplexing &amp;amp; Strapping Constraints:&lt;/strong&gt; Modern MCUs map SPI, I2C, UART, ADC, and PWM onto shared GPIO pads. On ESP32, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ADC2&lt;/code&gt; cannot be used while Wi-Fi is active (always put battery sensing on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ADC1&lt;/code&gt;!), and strapping pins (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO12&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO15&lt;/code&gt;) must not be forced into invalid boot states by external hardware.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Per-Pin vs. Package Current Limits:&lt;/strong&gt; Even if one GPIO can source 12–20 mA, the entire chip has a total I/O current ceiling (typically 100–200 mA). Always use transistors or driver ICs for anything heavier than an indicator LED.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters-for-capstone-a&quot;&gt;Why it matters for Capstone A&lt;/h2&gt;

&lt;p&gt;Chapter 07’s firmware teleop fails in ways that look nothing like a bad &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;. A 5 V ultrasonic echo lands on a 3.3 V GPIO. A motor return never shares ground with the logic, so a “PWM” pin floats relative to the driver and the TB6612 reads nonsense. A 7.4 V pack is clipped to the pin marked 5V or 3V3 because the label looked generous. The chip resets, or a pin dies, the first time the robot is asked to move. The timeout of about 300 ms and the wheels-up rule still matter, and they cannot save a pin that has already seen the battery. The card you draw here is the map you will hold when Chapter 05’s driver and Chapter 07’s PWM are finally legal. Chapter 08’s Wi-Fi does not add a tougher GPIO.&lt;/p&gt;

&lt;h2 id=&quot;two-rails-and-a-third-that-is-not-on-this-header&quot;&gt;Two rails, and a third that is not on this header&lt;/h2&gt;

&lt;p&gt;ESP32 and Pico GPIO pins are 3.3 V logic. Five volts on a GPIO can kill the pin. On the ESP32 the absolute maximum is only a diode drop above the rail:&lt;/p&gt;

\[V_{DD} + 0.3\,\mathrm{V} = 3.3\,\mathrm{V} + 0.3\,\mathrm{V} = 3.6\,\mathrm{V}.\]

&lt;p&gt;The Pico’s limit is the same kind of number, a few tenths above 3.3 V. Five volts is past either limit.&lt;/p&gt;

&lt;p&gt;A hobby reading rule, separate from that damage line, says a high should be at least about \(0.7 \times 3.3\,\mathrm{V} \approx 2.3\,\mathrm{V}\) before you trust it. Real \(V_{IL}\) and \(V_{IH}\) are in the datasheet. The operating rule is simpler than either formula: do not feed a GPIO from a 5 V pin.&lt;/p&gt;

&lt;p&gt;On a classic ESP32 dev board the pin marked 5V or VIN is USB 5 V, often through a Schottky diode, so the header sits a few tenths below the laptop’s 5 V. It can power a small sensor while you are still on the bench. It cannot power TT motors. A single TT motor’s no-load current on the course listing is already about 110 mA to 150 mA, and stall is many times that. Two motors plus a Wi-Fi peak collapse a USB port. The refusal arrives as a disconnect or as a brownout reset, the same repeating banner you learned in the heartbeat lab. The pin marked 3V3 is the regulator output. It feeds the module. A motor does not belong on it.&lt;/p&gt;

&lt;p&gt;The Pico names the same ideas more honestly. VBUS is USB 5 V and is present when the cable is plugged in. VSYS is the system input. USB reaches VSYS through a diode, and an external supply on VSYS itself is limited to about 1.8 V to 5.5 V. 3V3 is the regulated rail and it is an output. Do not feed 7.4 V into 3V3. Do not feed 7.4 V into VSYS. Both are outside what those pins are for, and 7.4 V on 3V3 is a direct hit on the regulator output.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_rails_3v3.png&quot; alt=&quot;USB 5 V, regulated 3.3 V, and a motor rail that stays off the header&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Read the drawing as a promise about current as well as voltage. The 5 V rail comes from USB and may be borrowed lightly. The 3.3 V rail is for logic. Ground is common. The motor rail is not one of those two pins.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/pcb_mcu_anatomy.png&quot; alt=&quot;Board anatomy: probe the header, not the module&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The USB connector explains VBUS or the dev-board 5V pin. The regulator explains 3V3. The meter lands on the header. The metal can is not a test point.&lt;/p&gt;

&lt;p&gt;Common ground with the driver is mandatory. The TB6612 measures your PWM against its own logic ground. If that ground is not the microcontroller’s ground, the driver does not see a reliable high, even when the GPIO voltage looks correct on a meter referenced to the wrong place. When the driver arrives, its GND pin ties to a GND pin on the ESP32 or the Pico. The motor supply does not.&lt;/p&gt;

&lt;h2 id=&quot;the-echo-pin&quot;&gt;The echo pin&lt;/h2&gt;

&lt;p&gt;The HC-SR04’s echo output is a 5 V pulse. That pulse must be level-shifted before it reaches an ESP32 or Pico input. A shifter module is the robust tool: the low-voltage side is the board’s 3V3, the high-voltage side is the sensor’s 5 V, and the grounds are tied together. Echo travels from the high side to the low side and into the GPIO. Many modules accept a 3.3 V high on the trigger pin; that is a property of the sensor’s input, and it is not permission to skip the echo translation. You will confirm trigger behavior in the sensor chapter. You will not discover it by sacrificing a GPIO today.&lt;/p&gt;

&lt;p&gt;A resistor divider is the calculation that shows why a raw 5 V echo is illegal. With \(1\,\mathrm{k}\Omega\) from echo to the GPIO node and \(2\,\mathrm{k}\Omega\) from that node to GND:&lt;/p&gt;

\[V_{\mathrm{GPIO}} = 5\,\mathrm{V} \times \frac{2}{1 + 2} \approx 3.33\,\mathrm{V}.\]

&lt;p&gt;That sits near the top of the allowed range, which is why a shifter is the kinder part once the robot has to be reliable. Tolerances and a long cable move the node. Build nothing with the sensor in this lab. Write the fraction on the pin card so the later wiring has a number attached.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A 2S pack of 18650 cells sits near 7.4 V nominal, higher when full, lower when empty. Hà draws the only legal landing for the red lead: the TB6612 VM terminal. The Hshop-style module in the Chapter 00 notes accepts a motor supply in the range that includes 7.4 V. The black lead lands on the driver GND, and a separate wire joins that GND to the microcontroller GND.&lt;/p&gt;

&lt;p&gt;The pack’s positive lead has a list of pins it must never touch. On the ESP32 header: any GPIO, the 3V3 pin, and the 5V/VIN pin. That 5V pin is USB 5 V; a 2S pack there can back-feed the laptop. On the Pico: any GP pin, 3V3, VSYS, and VBUS. VSYS’s allowed window is about 1.8 V to 5.5 V, and 7.4 V is outside it. 3V3 is an output on both boards. The driver’s logic VCC, the small pin that expects 3.3 V or 5 V, is not VM. Hà writes “VM only” next to the pack and “logic VCC from 3V3, not from the pack” next to the driver.&lt;/p&gt;

&lt;p&gt;No wire is connected. The drawing is the worked example. The meter work is a different, smaller act: USB only.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;USB cable only. Battery unplugged, driver unplugged, sensor unplugged.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Red lead in the voltage jack, black lead on a GND pin you can name, meter on DC volts.&lt;/li&gt;
  &lt;li&gt;Measure the 5 V rail: the dev-board pin marked 5V or VIN, or Pico VBUS. Write the number.&lt;/li&gt;
  &lt;li&gt;Measure 3.3 V: the pin marked 3V3, or Pico 3V3(OUT). Write the number.&lt;/li&gt;
  &lt;li&gt;Draw a pin card: those two rails, GND, the heartbeat LED pin, one free GPIO you have checked against the silk, and a forbidden list. The forbidden list includes “5 V never on a GPIO” and the sentence “the 2S pack touches VM and driver GND only.”&lt;/li&gt;
  &lt;li&gt;On the same card, sketch the HC-SR04 echo through a level shifter or the 1 kΩ / 2 kΩ divider. Do not connect the sensor.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should read about 4.7 V to 5.1 V on the USB rail and about 3.25 V to 3.35 V on 3.3 V. A diode-dropped ESP32 5V pin near 4.7 V to 4.9 V is still a healthy USB rail; write the number rather than rounding it to “5 V.” A 3.3 V reading outside that window is a stop. A reading above 3.6 V on 3V3 means something is leaking 5 V in: unplug and find it. Do not connect the battery in this lab, not to “see what VM would be.”&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Order of connections: common ground, logic, signals, motor supply last, wheels up&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The figure is the order the card is enforcing. You are still on the USB step. Motor supply is the last block, and the wheels are up when it finally becomes legal.&lt;/p&gt;

&lt;p&gt;Faults. Black lead on the 5V pin and red lead on GND produces a negative number or a confusing zero; swap the leads and name GND again. Probing the metal can, or a capacitor, is not a rail measurement. A 0 V reading on 3V3 with a live USB port is a wrong pin, a missing ground, or a dead regulator. Stop. A classmate who powers a TT motor from the 5V pin will brown the board out the moment the motor stalls. That pin is USB 5 V.&lt;/p&gt;

&lt;p&gt;Safety: the pack stays in the bag. Do not series the meter in current mode across 5V and GND. That is a short through the meter.&lt;/p&gt;

&lt;h2 id=&quot;explanation-auditing-your-3v3-onboard-regulator-headroom-by-hand&quot;&gt;Explanation: Auditing Your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; Onboard Regulator Headroom by Hand&lt;/h2&gt;

&lt;p&gt;Before hanging an IMU, an OLED screen, two encoders, and a ToF sensor off your dev-board’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; pin, add up the current budget in four steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Find the onboard 3.3 V regulator limit \(I_{\text{reg,max}}\):&lt;/strong&gt; Common ESP32 dev-boards use an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMS1117-3.3&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ME6211&lt;/code&gt; rated around &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;500–800 mA&lt;/code&gt; nominal (thermally safe continuous \(\approx 400\text{–}500\text{ mA}\) from 5 V input).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Subtract MCU peak current:&lt;/strong&gt; ESP32 peaks around &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;240–300 mA&lt;/code&gt; during Wi-Fi transmit bursts (or ~80 mA with Wi-Fi off); RP2040 peaks around &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50–100 mA&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sum all external 3.3 V sensors:&lt;/strong&gt; Add datasheet maximums (not “typical sleep” currents!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check thermal margin:&lt;/strong&gt; Compute \(P_{\text{LDO}} = (5.0 - 3.3) \times I_{\text{total}}\) and ensure \(P_{\text{LDO}} &amp;lt; 0.8\text{ W}\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-complete-33-v-rail-budget-for-an-esp32-capstone-a-robot&quot;&gt;Concrete Worked Example: Complete 3.3 V Rail Budget for an ESP32 Capstone A Robot&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Load on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; Rail&lt;/th&gt;
      &lt;th&gt;Typical Current&lt;/th&gt;
      &lt;th&gt;Peak Current&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 MCU (Wi-Fi active for telemetry)&lt;/td&gt;
      &lt;td&gt;\(120\text{ mA}\)&lt;/td&gt;
      &lt;td&gt;\(260\text{ mA}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;IMU (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BNO055&lt;/code&gt;)&lt;/td&gt;
      &lt;td&gt;\(4\text{ mA}\)&lt;/td&gt;
      &lt;td&gt;\(13\text{ mA}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;VL53L0X ToF laser sensor&lt;/td&gt;
      &lt;td&gt;\(19\text{ mA}\)&lt;/td&gt;
      &lt;td&gt;\(40\text{ mA}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2× Magnetic Hall encoders (at 3.3 V)&lt;/td&gt;
      &lt;td&gt;\(10\text{ mA}\)&lt;/td&gt;
      &lt;td&gt;\(16\text{ mA}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0.96” I2C OLED display&lt;/td&gt;
      &lt;td&gt;\(15\text{ mA}\)&lt;/td&gt;
      &lt;td&gt;\(25\text{ mA}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Total &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; Rail Load&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(168\text{ mA}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(354\text{ mA}\)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Headroom check:&lt;/strong&gt; Peak \(354\text{ mA} &amp;lt; 500\text{ mA}\) limit — &lt;strong&gt;PASS&lt;/strong&gt; (146 mA safety margin).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;LDO thermal check (fed from 5.0 V buck):&lt;/strong&gt;
\(P_{\text{LDO,peak}} = (5.0 - 3.3) \times 0.354 = 1.7 \times 0.354 \approx 0.60\text{ W}\) — stays cool and stable!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;What if someone added a 500 mA hobby servo onto &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;?&lt;/strong&gt; Peak jumps to \(854\text{ mA}\) and instantly browns out the board. Servos always belong on a dedicated 5 V–6 V buck rail!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Plugging USB into your laptop &lt;em&gt;while&lt;/em&gt; simultaneously feeding 5 V into the dev-board’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; pin without verifying whether the board has a back-power protection diode — on some boards, that can back-feed power into your laptop’s USB port.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If you measure &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.28 V&lt;/code&gt; on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; pin under load, the deviation from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.30 V&lt;/code&gt; is only \(-0.6\%\) — well inside the \(\pm 3\%\) tolerance of digital sensors.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Compute the divider output for 5 V with \(1\,\mathrm{k}\Omega\) on top and \(2\,\mathrm{k}\Omega\) on the bottom. If both resistors are 5 percent high, does the ratio change?&lt;/li&gt;
  &lt;li&gt;Which terminal of a 2S 7.4 V pack may touch the TB6612, and which ESP32 header pins must it never touch?&lt;/li&gt;
  &lt;li&gt;A Pico is on the bench. Which label is USB 5 V, which label can accept about 1.8 V to 5.5 V, and which label is the regulated output? Where does 7.4 V do the most immediate damage?&lt;/li&gt;
  &lt;li&gt;Your meter reads 4.86 V and 3.29 V with USB only. Is that inside the window? What extra sentence does the pin card still need about VM?&lt;/li&gt;
  &lt;li&gt;Echo of an HC-SR04 is wired straight to GPIO 18 “just to see.” What voltage hits the pin, and what part should have been in series with that wire?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Answers&lt;/summary&gt;

1. $$5 \times 2 / 3 \approx 3.33\,\mathrm{V}$$. If both resistors scale by 1.05 the ratio is unchanged, so the ideal node stays about 3.33 V. Unequal errors do not cancel. That is one reason a shifter is kinder than a divider built from leftover parts.
2. Pack positive may touch VM. Pack negative touches driver GND, and that GND must be common with the microcontroller. The pack must not touch any ESP32 GPIO, 3V3, or the 5V/VIN pin.
3. VBUS is USB 5 V. VSYS is the input that can take about 1.8 V to 5.5 V, with USB arriving through a diode. 3V3 is the regulated output. 7.4 V on 3V3 hits the regulator output directly; 7.4 V on VSYS is also outside the allowed window.
4. 4.86 V is inside about 4.7 V to 5.1 V, and 3.29 V is inside about 3.25 V to 3.35 V. The card still needs an explicit sentence that the 2S pack touches VM and driver ground only, and that the battery was not connected for the measurement.
5. The echo pin drives about 5 V into a 3.3 V GPIO, above the roughly 3.6 V absolute-maximum region. A level shifter (or, as a calculated compromise, the divider) belongs on that wire, with grounds tied. Unplug the echo before any further experiments.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;SparkFun logic levels&lt;/a&gt; — why a 5 V high and a 3.3 V input need a translator.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://datasheets.raspberrypi.com/pico/pico-datasheet.pdf&quot;&gt;Pico datasheet&lt;/a&gt; — VBUS, VSYS, 3V3, and the allowed VSYS window.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;ESP32 datasheet (PDF)&lt;/a&gt; — GPIO electrical characteristics and absolute maximum ratings.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab — blink, serial, and a robot heartbeat</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter02/02_04_blink_serial_hello/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter02/02_04_blink_serial_hello</id>
   <content type="html">&lt;p&gt;To an outsider, blinking an LED and printing “Hello” over USB looks like a toy exercise. To a robotics engineer, a &lt;strong&gt;non-blocking heartbeat LED plus structured serial telemetry&lt;/strong&gt; is a life-support monitor: when your robot freezes mid-corridor three weeks from now, the heartbeat LED instantly tells you whether the CPU is deadlocked, reset-looping, or alive and waiting for a command.&lt;/p&gt;

&lt;p&gt;This lab replaces beginner &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay()&lt;/code&gt; loops with non-blocking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_ms()&lt;/code&gt; scheduling — the exact timing pattern that will drive every sensor read and PID update for the rest of the course.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;objectives&quot;&gt;Objectives&lt;/h2&gt;

&lt;p&gt;You will show that the toolchain from the previous lab is still alive, and you will replace a blocking blink with a loop that can do more than one job. You will toggle an LED every 250 ms and print a heartbeat line every 500 ms containing the millisecond time and the LED state. You will write that loop with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; on an ESP32 or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; on a Pico, and you will run only the listing that matches your board. You will explain why &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep&lt;/code&gt; cannot sit in the way of a later 300 ms stop. You will recognize a reset loop, a wrong LED pin, and a monitor left at 9600.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;ESP32 track: a finished upload from the PlatformIO or Arduino IDE lesson, a port you can name, and 115200 agreed on both sides. Pico track: MicroPython on the board, Thonny or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt; aimed at the Pico, and a lamp that already answered to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; or GPIO 25. You need one of those two setups. No driver, no battery, no chassis. If your cable was marginal, this is the lab that will show it.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-non-blocking-scheduling-watchdogs-and-monotonic-clocks&quot;&gt;Key ideas: Non-Blocking Scheduling, Watchdogs, and Monotonic Clocks&lt;/h2&gt;

&lt;p&gt;Three embedded-firmware concepts transform a simple blink into production robotics infrastructure:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Blocking (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt;) vs. Non-Blocking (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;micros&lt;/code&gt;) Timing:&lt;/strong&gt; Calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(500)&lt;/code&gt; halts the entire CPU for half a second — during which an emergency stop button, encoder check, or serial command is completely ignored. Comparing elapsed time &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - last_tick &amp;gt;= period&lt;/code&gt; lets multiple tasks (LED heartbeat at 2 Hz, telemetry at 20 Hz, PID at 100 Hz) run cooperatively on one core.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Unsigned Rollover-Safe Arithmetic:&lt;/strong&gt; A 32-bit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; counter wraps from \(2^{32}-1\) back to \(0\) after \(\approx 49.7\) days. Writing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(uint32_t)(now - last) &amp;gt;= period&lt;/code&gt; handles wrap-around mathematically without glitches, whereas &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now &amp;gt;= last + period&lt;/code&gt; breaks at rollover!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Heartbeat &amp;amp; Watchdog Diagnostics:&lt;/strong&gt; A steady 1 Hz blink means the main loop is healthy; a rapid 5 Hz blink signals a fault state (e.g., low battery or lost IMU); a frozen solid LED means the loop hung.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters-for-capstone-a&quot;&gt;Why it matters for Capstone A&lt;/h2&gt;

&lt;p&gt;Chapter 07’s teleop loop is this heartbeat with the LED replaced by a left-wheel duty and a right-wheel duty on a TB6612. The timestamp is how you prove the loop is running at the rate you think. The same loop has to notice that no fresh command arrived for about 300 ms and write both duties to zero. Wheels stay up the first time those duties are allowed to be non-zero. A sketch that spends its time inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; cannot see the silence until the delay returns, so the wheels keep the last PWM for up to a second after the operator has already let go. Chapter 08 can move the same commands onto Wi-Fi later. The timeout still has to be a comparison in this loop, not a pause.&lt;/p&gt;

&lt;h2 id=&quot;two-timers-no-blocking-wait&quot;&gt;Two timers, no blocking wait&lt;/h2&gt;

&lt;p&gt;A heartbeat here is a line the firmware emits on a schedule whether or not you are typing. The LED changes every 250 ms. A full on-off cycle is therefore 500 ms, which is 2 Hz:&lt;/p&gt;

\[T_{\text{blink}} = 2 \times 250\,\mathrm{ms} = 500\,\mathrm{ms}, \qquad f = \frac{1}{0.5\,\mathrm{s}} = 2\,\mathrm{Hz}.\]

&lt;p&gt;The serial line is printed every 500 ms and carries two fields: the millisecond clock, and the LED state as 0 or 1. You should see about two lines per second. Watch the lamp for five seconds and count complete flashes. About ten flashes is the 2 Hz blink. About ten lines in those five seconds is the matching serial rate, because the print period is also 500 ms.&lt;/p&gt;

&lt;p&gt;There is a sampling catch worth seeing once. The print period is exactly two toggles. If both timers start together and you print the state just after a toggle, the printed column can repeat the same value even while the lamp is flashing. Judge the blink with your eyes. Judge the scheduler with the millisecond column: it should climb by about 500 each line. The sketch below prints on the 500 ms timer after the 250 ms toggle has been applied, and the worked log shows what that looks like on a real run. If your state column sits still, the lamp is still the evidence of the 250 ms edge. You can move the print onto every toggle later; do not “fix” it by inserting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The clock source differs by board and the meaning does not. On the ESP32 Arduino core, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt; counts milliseconds since boot. It wraps after&lt;/p&gt;

\[2^{32}\,\mathrm{ms} \approx 49.7\,\text{days},\]

&lt;p&gt;which you will not hit this week. Unsigned subtraction &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - last&lt;/code&gt; still works across that wrap if both values are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint32_t&lt;/code&gt;. On the Pico, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms()&lt;/code&gt; also counts milliseconds, and the legal subtraction is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_diff(now, last)&lt;/code&gt;. A raw Python minus will lie when the counter wraps. Either number, climbing by roughly 500 each printed line, is the evidence you want. A number that jumps back to a small value, especially if a boot banner is printed again, means the chip reset.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; is the pattern this lab retires. While &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; runs, the CPU does not evaluate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - lastCmdMs &amp;gt; 300&lt;/code&gt;. A stop that became due 50 ms into the delay is applied 950 ms late. The same is true of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep(1)&lt;/code&gt; on the Pico. The non-blocking loop checks the inequality every pass. The extra motion after a timeout is one pass through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt;, far under 300 ms, instead of the length of the sleep. The motor line is still a comment. There is no driver on the bench.&lt;/p&gt;

&lt;p&gt;Run the listing for your board. Reading the other listing tells you what the other bench is seeing. Flashing both onto two boards in one sitting is how notes get crossed.&lt;/p&gt;

&lt;p&gt;ESP32, Arduino core. Change the pin if your silk disagrees with GPIO 2. If the board package defines &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; and that macro is the real lamp, you may use it; the constant below is the explicit form.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kLedPin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// DevKit V1 often uses GPIO 2; confirm the silk&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TOGGLE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BEAT_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastToggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastBeat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kLedPin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastToggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lastBeat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;heartbeat&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastToggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TOGGLE_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastToggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kLedPin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastBeat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BEAT_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lastBeat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// ms, state. Later, both wheel duties follow this same clock.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// A 300 ms command timeout is another comparison here, not a delay.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%lu,%d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ledOn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Capstone A, not today:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// if (now - lastCmdMs &amp;gt; 300) { both TB6612 duties = 0; }&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Pico, MicroPython. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt; covers Pico W and Pico 2 W. GPIO 25 covers an original Pico and a non-W Pico 2 when the alias is missing.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# Pico W, Pico 2 W
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# original Pico, Pico 2
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;TOGGLE_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;BEAT_MS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_toggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_beat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;heartbeat&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_toggle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TOGGLE_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_toggle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ticks_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_beat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BEAT_MS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;last_beat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# ms, state. A 300 ms timeout belongs beside this test.
&lt;/span&gt;        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;led_on&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# later: if time.ticks_diff(now, last_cmd) &amp;gt; 300: both duties = 0
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Wheels-up is a sentence you write in the notes now, while no wheel is attached. It means the tires are not touching the desk when a command that could spin them is present. A robot that “just twitches” on the table walks off the table. The TB6612 and the battery stay in the parts box.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Power order: logic first, motor supply last, wheels up&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The figure is the sequence this heartbeat is rehearsing. USB and a quiet 3.3 V rail come first. A blink and a serial line are the proof. A driver and a battery are to the right of a line you have not crossed.&lt;/p&gt;

&lt;h2 id=&quot;a-log-you-can-audit&quot;&gt;A log you can audit&lt;/h2&gt;

&lt;p&gt;After the single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; banner, copy ten lines. An ESP32 log from a healthy run, times in milliseconds:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;heartbeat
1840,0
2342,0
2843,0
3344,0
3846,0
4347,0
4848,0
5349,0
5851,0
6352,0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The absolute value of the first stamp depends on how long &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup&lt;/code&gt; and the USB adapter delayed. The difference between lines should sit near 500 ms. A few milliseconds of jitter is normal. Both timers in the sketch start together, and 500 ms is exactly two toggles, so this print keeps landing on the same edge and the state column repeats. The lamp must still flash twice a second. That repeated column is a pass when the lamp is right. It is not a stuck pin. A return to a small timestamp, or a second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; banner in the middle of the ten lines, means you recorded a reset. The lab is not passed until a later stretch of ten lines stays monotonic.&lt;/p&gt;

&lt;p&gt;If the board resets and no motor is connected, suspect the cable or a short, not a driver you have not wired. A thin charge-only lead can make the port appear and then sag. A stray jumper from 3V3 to GND is a short and does the same thing. Unplug every jumper, swap the cable, and capture a clean block. Baud 9600 against a sketch at 115200 leaves the LED blinking and the pane full of garbage. Fix the monitor. A dark LED with a healthy millisecond column is the pin constant, not the toolchain: read the silk, and on a Pico W stop writing GPIO 25.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Dũng’s primary board is a classic ESP32 DevKit, silk LED on GPIO 2, CH340, monitor at 115200. The sketch is the C++ listing. The first session prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; twice in six seconds and the timestamps restart near 200 ms. No motor is attached. The cable is a charge-only lead that only sometimes enumerates. A data cable produces one banner and a staircase of about 501 ms. The lamp flashes twice a second. Ten lines go into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;, plus the sentence “wheels up before any TB6612 command; VM and the battery are not connected.”&lt;/p&gt;

&lt;p&gt;On the Pico bench the MicroPython listing is the one that runs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; works on a Pico W. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ticks_ms&lt;/code&gt; climbs by about 500. The note says “Pico W, alias LED, GPIO 25 not used, wheels up, no motor.” Both benches have satisfied the gate. Neither bench flashes the other bench’s firmware.&lt;/p&gt;

&lt;p&gt;The arithmetic Dũng writes in the margin is the reason &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; left the file. A command that goes silent at \(t = 0\) must force PWM to zero when \(t &amp;gt; 300\,\mathrm{ms}\). Inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt; the comparison cannot run, so the last duty can survive until \(t = 1000\,\mathrm{ms}\). The non-blocking loop evaluates the comparison on the next pass after 300 ms.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Open the project that already uploads, or the MicroPython session that already blinks. Replace the blocking blink with the listing for your board. Do not paste the other language “as well.”&lt;/li&gt;
  &lt;li&gt;Confirm the LED pin: GPIO 2 or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; only if the silk agrees; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; or GPIO 25 as in the Pico lesson.&lt;/li&gt;
  &lt;li&gt;Start the monitor or the REPL at 115200 where a baud exists. You should see one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; banner, then lines about every 500 ms, and a lamp flashing twice a second.&lt;/li&gt;
  &lt;li&gt;Copy ten lines into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;. On the same page write the LED pin, the board name, and the wheels-up sentence. Name VM and the battery as pins and parts you are not using.&lt;/li&gt;
  &lt;li&gt;If the banner repeats, stop and fix the cable or a short before you edit the print.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Stop there. Do not add Wi-Fi, a driver, or a battery in order to make the log “more real.”&lt;/p&gt;

&lt;h2 id=&quot;explanation-proving-unsigned-wrap-around-safety--drift-free-scheduling&quot;&gt;Explanation: Proving Unsigned Wrap-Around Safety &amp;amp; Drift-Free Scheduling&lt;/h2&gt;

&lt;p&gt;Two subtle timing bugs haunt robot firmware: &lt;strong&gt;clock rollover&lt;/strong&gt; and &lt;strong&gt;cumulative schedule drift&lt;/strong&gt;. Trace both with exact numbers:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Rollover-safe subtraction (demonstrated on an 8-bit counter \(0\text{–}255\)):&lt;/strong&gt; Suppose &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last = 250&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;period = 10&lt;/code&gt;, and the counter wraps to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now = 4&lt;/code&gt;.
    &lt;ul&gt;
      &lt;li&gt;Modulo-256 unsigned subtraction: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(4 - 250) mod 256 = (-246) mod 256 = 10 &amp;gt;= 10&lt;/code&gt; — triggers at the &lt;em&gt;exact&lt;/em&gt; right tick!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Preventing cumulative period drift:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Drifting update: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last = now;&lt;/code&gt; — if one iteration starts 1 ms late, every future tick shifts 1 ms permanently.&lt;/li&gt;
      &lt;li&gt;Drift-free update: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last += period;&lt;/code&gt; — keeps average frequency locked to exact \(1/\text{period}\) over hours.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute multi-rate loop utilization:&lt;/strong&gt; Sum \(t_i / T_i\) across all cooperative tasks to verify total CPU load \(U = \sum (t_i / T_i) &amp;lt; 1\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-multi-task-cooperative-timing-budget-on-one-core&quot;&gt;Concrete Worked Example: Multi-Task Cooperative Timing Budget on One Core&lt;/h3&gt;

&lt;p&gt;Suppose your firmware runs three non-blocking tasks using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis()&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;micros()&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Task 1 (PID control):&lt;/strong&gt; period \(T_1 = 10\text{ ms}\) (100 Hz), execution time \(t_1 = 0.8\text{ ms}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Task 2 (Serial telemetry):&lt;/strong&gt; period \(T_2 = 50\text{ ms}\) (20 Hz), execution time \(t_2 = 0.5\text{ ms}\).&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Task 3 (Heartbeat LED toggle):&lt;/strong&gt; period \(T_3 = 500\text{ ms}\) (2 Hz), execution time \(t_3 = 0.01\text{ ms}\).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — CPU utilization sum:&lt;/strong&gt;
\(U = \frac{0.8}{10} + \frac{0.5}{50} + \frac{0.01}{500} = 0.08 + 0.01 + 0.00002 = 0.09002 \quad (9.0\%).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Worst-case simultaneous tick (every 500 ms when all three fire together):&lt;/strong&gt;
\(t_{\text{worst}} = 0.8 + 0.5 + 0.01 = 1.31\text{ ms} \ll 10\text{ ms}.\)
Even on the busiest millisecond of the day, the CPU finishes in 1.31 ms with 8.69 ms to spare!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last += period&lt;/code&gt; without a catch-up guard after a long pause — if a task was paused for 2 seconds, it would fire 200 times back-to-back. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;now - last &amp;gt; 2 * period&lt;/code&gt;, resync &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last = now&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; How long before a 32-bit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;micros()&lt;/code&gt; counter overflows? \(2^{32}\text{ }\mu\text{s} \approx 4.295 \times 10^9\text{ }\mu\text{s} \approx 71.58\text{ minutes}\) — shorter than a single afternoon lab, which is why rollover-safe unsigned subtraction matters immediately!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A full blink is 500 ms and the LED toggles every 250 ms. How many complete flashes do you expect in 4 seconds, and how many heartbeat lines?&lt;/li&gt;
  &lt;li&gt;Why can a state column that stays at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; still be a passing blink? What do you watch instead?&lt;/li&gt;
  &lt;li&gt;A stop command is due 300 ms after the last packet. The loop is inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(1000)&lt;/code&gt;. What is the latest time at which the duties can actually reach zero, measured from the moment the timeout became true?&lt;/li&gt;
  &lt;li&gt;A log shows a second &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt; between line 4 and line 5, and the millisecond field drops from 2600 to 180. No motor is wired. Name two causes that fit, and one cause that does not.&lt;/li&gt;
  &lt;li&gt;The monitor was left at 9600. The LED still flashes at 2 Hz. What do you change, and why is reinstalling the core the wrong first move?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Answers&lt;/summary&gt;

1. Four seconds contain $$4 / 0.5 = 8$$ complete flashes, and $$4 / 0.5 = 8$$ heartbeat lines. The first line can be slightly late because of `setup` or the REPL. The spacing is the measurement.
2. The print period is an integer multiple of the toggle period, so a phase-locked sample can repeat one state. The lamp has to flash twice a second. The millisecond column has to step by about 500.
3. The comparison cannot run during the delay, so the write of zero can be as late as 1000 ms after the timeout became true. The non-blocking loop does that write on the first pass after 300 ms of silence.
4. Reset loop. On an unloaded board, suspect the cable or a short from a jumper, or an EN button that is bouncing. A motor on VM is not a cause you have earned yet, because VM is not connected.
5. Set the monitor to 115200 to match `Serial.begin`. The LED proves the sketch is running. Garbage at the wrong baud is not a failed chip.

&lt;/details&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;This lab buys nothing if the board and a data cable already work. If the heartbeat resets and the lead has never copied a file, replace it with the &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;. The dev board, if you still lack one, is the &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;NodeMCU-32S at 190 000 ₫&lt;/a&gt; or the &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32 at 265 000 ₫&lt;/a&gt;, not the &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;bare ESP32-S3-WROOM-1 at 135 000 ₫&lt;/a&gt;. Pico track: the search &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;raspberry pi pico&lt;/a&gt; showed Pico 2 at 195 000 ₫ and Pico 2 W at 275 000 ₫ on 23 September 2026 (&lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-rp2350&quot;&gt;Pico 2&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-w-rp2350&quot;&gt;Pico 2 W&lt;/a&gt;). Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=esp32%20devkit%20ch340&quot;&gt;ESP32 DevKit CH340&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=raspberry%20pi%20pico%202&quot;&gt;Pico 2&lt;/a&gt;. The full cart is &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Chapter 00&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.platformio.org/en/latest/boards/espressif32/esp32dev.html&quot;&gt;PlatformIO &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;&lt;/a&gt; — the board id behind the Arduino listing, if that is your track.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html&quot;&gt;arduino-esp32 installation&lt;/a&gt; — the core that provides &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;millis&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/micropython.html&quot;&gt;MicroPython on Pico&lt;/a&gt; — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;machine.Pin&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.ticks_ms&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://datasheets.raspberrypi.com/pico/pico-datasheet.pdf&quot;&gt;Pico datasheet&lt;/a&gt; — confirm GPIO 25 before you trust a fallback on a non-W board.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Raspberry Pi Pico and MicroPython</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter02/02_03_pico_micropython/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter02/02_03_pico_micropython</id>
   <content type="html">&lt;p&gt;If C++ on an ESP32 is a finely tuned race car, &lt;strong&gt;MicroPython on a Raspberry Pi Pico (RP2040)&lt;/strong&gt; is an interactive robotics laboratory where you can type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;motor.duty_u16(32768)&lt;/code&gt; into a live REPL prompt and watch a wheel spin half a second later — zero compile-flash cycle required. For rapid sensor probing, I2C bus discovery, and verifying motor polarity on a brand-new chassis, that interactive feedback loop is unbeatable.&lt;/p&gt;

&lt;p&gt;This lesson introduces the RP2040 architecture and MicroPython runtime — along with the exact performance boundaries where interpreted Python shines and where C++ or PIO state machines take over.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;objectives&quot;&gt;Objectives&lt;/h2&gt;

&lt;p&gt;You will put a Pico or Pico 2 into BOOTSEL mode so a drive named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt; appears, and you will copy the MicroPython UF2 that matches that chip. You will point Thonny, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt;, at the board and get a REPL that answers &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 + 1&lt;/code&gt;. You will blink the onboard LED at 2 Hz and print a counter, using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; on a Pico W or Pico 2 W and GPIO 25 on an original Pico. You will refuse an RP2040 UF2 on a Pico 2. You will keep every GPIO at 3.3 V and leave motors unwired.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You either chose a Pico-family board in the landscape lesson or you are learning the other bench’s toolchain once. You can copy a file onto a USB drive. You do not need PlatformIO for this page; that file belongs to the ESP32 track. A data-capable micro-USB cable is part of the setup. The shopping list for the whole robot is still Chapter 00 (&lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM and shopping&lt;/a&gt;).&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-interpreted-bytecode-garbage-collection-jitter-and-rp2040-hardware&quot;&gt;Key ideas: Interpreted Bytecode, Garbage Collection Jitter, and RP2040 Hardware&lt;/h2&gt;

&lt;p&gt;Three systems concepts clarify how MicroPython runs on a bare-metal microcontroller:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Bytecode VM &amp;amp; Interactive REPL:&lt;/strong&gt; MicroPython compiles &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.py&lt;/code&gt; scripts on-chip into compact bytecode executed by a small virtual machine in Flash/RAM. The serial REPL lets you inspect registers, scan I2C addresses, and test equations interactively.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Garbage Collection (GC) &amp;amp; Timing Jitter:&lt;/strong&gt; Unlike C++ static allocation, creating temporary lists, floats, or formatted strings in Python allocates heap objects. Periodically, the garbage collector pauses execution for \(1\text{–}5\text{ ms}\) to reclaim memory — harmless at 20 Hz, noticeable in a 500 Hz control loop unless you pre-allocate buffers or call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gc.collect()&lt;/code&gt; deterministically.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;RP2040 Programmable I/O (PIO):&lt;/strong&gt; Unique to the Pico, 8 tiny deterministic state machines run independently of the ARM Cortex-M0+ cores at full 125 MHz clock speed — capable of decoding quadrature encoders or driving WS2812 LEDs with zero Python jitter.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters-for-capstone-a&quot;&gt;Why it matters for Capstone A&lt;/h2&gt;

&lt;p&gt;If the Pico is your brain, this UF2 and this REPL are the language of the Chapter 07 teleop firmware: two wheel commands, PWM that will later land on a TB6612, and a loop that must notice about 300 ms of silence and force both duties to zero. Wheels stay up the first time that command exists. MicroPython is the short path into that loop, because you can ask the chip a question before you trust a file. A non-W Pico has no Wi-Fi, so Chapter 08’s radio lab is a Pico W, a Pico 2 W, or a switch to the ESP32. Choosing a friendly interpreter does not close that door, and it does not mean you install ROS today.&lt;/p&gt;

&lt;h2 id=&quot;bootsel-and-the-right-uf2&quot;&gt;BOOTSEL and the right UF2&lt;/h2&gt;

&lt;p&gt;The RP2040 and the RP2350 both boot from an on-chip boot ROM when you ask. The ask is mechanical. Unplug the board completely. Hold the BOOTSEL button. Plug the USB cable in while the button is still down. Release BOOTSEL. A mass-storage drive named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt; appears, the way a small USB stick would. That name is the bootloader, not your notebook’s disk. Both the original Pico and the Pico 2 use the same volume name, so the label does not tell you which chip you are holding. The filename of the UF2 does.&lt;/p&gt;

&lt;p&gt;Take the original Pico (RP2040) image from the official board page &lt;a href=&quot;https://micropython.org/download/RPI_PICO/&quot;&gt;MicroPython for RPI_PICO&lt;/a&gt;. Pico 2 is an RP2350 and uses a different file, from &lt;a href=&quot;https://micropython.org/download/RPI_PICO2/&quot;&gt;MicroPython for RPI_PICO2&lt;/a&gt;. The index at &lt;a href=&quot;https://micropython.org/download/&quot;&gt;micropython.org/download&lt;/a&gt; lists Pico W and Pico 2 W separately as well. Copy the UF2 onto &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt; and only onto that volume. The drive ejects itself and the chip reboots into MicroPython. It will not come back as a drive unless you hold BOOTSEL again. That disappearance is success.&lt;/p&gt;

&lt;p&gt;Do not flash the RP2040 image onto a Pico 2. The bootloader may still accept the copy, because it is a file drop, and the board then sits there with no working REPL. The fix is the same gesture: unplug, hold BOOTSEL, plug in, and copy the RP2350 UF2. A Pico W image on an original Pico, or the reverse, is the same class of mistake. Match the silk printed on the board, not the file a classmate just used.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Raspberry_Pi_Pico.jpg&quot; alt=&quot;Original Raspberry Pi Pico: BOOTSEL, USB, GPIO 25 LED&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Find three landmarks on the photograph and on your board before you copy firmware: the micro-USB plug, the BOOTSEL button, and the onboard LED near the USB connector. This picture is an original Pico. GPIO 25 is the lamp. A Pico W does not use that pin for the lamp.&lt;/p&gt;

&lt;h2 id=&quot;repl-then-the-lamp&quot;&gt;REPL, then the lamp&lt;/h2&gt;

&lt;p&gt;Thonny is the editor whose interpreter menu tells the truth. Install it, open it, and choose &lt;strong&gt;MicroPython (Raspberry Pi Pico)&lt;/strong&gt;. The port underneath should be the board that just rebooted. Leave that setting for tomorrow. The usual “it worked yesterday” failure is a menu still aimed at the Python on the laptop. The laptop’s Python accepts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print&lt;/code&gt; and then fails on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import machine&lt;/code&gt;, which feels like a broken Pico.&lt;/p&gt;

&lt;p&gt;If you prefer a terminal, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt; (install it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; in a local environment) opens the same REPL. Either tool is enough. Do not run both against the port at once.&lt;/p&gt;

&lt;p&gt;Click the shell and type:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You should see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; and a new prompt. That is this track’s serial hello. There is no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.begin&lt;/code&gt; inside Thonny’s Pico backend. If you later use a raw terminal, MicroPython’s USB REPL is still a serial device and 115200 is the usual speed.&lt;/p&gt;

&lt;p&gt;The onboard LED is the part students copy wrong.&lt;/p&gt;

&lt;p&gt;On the original Pico the lamp is GPIO 25. Recent MicroPython also accepts the alias &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt; for that pin. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; raises &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ValueError&lt;/code&gt;, use GPIO 25.&lt;/p&gt;

&lt;p&gt;On the Pico W the lamp is wired to the CYW43439 wireless chip, not to GPIO 25. GPIO 25 is an ordinary I/O pin. Lighting the onboard LED means &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt;. Writing GPIO 25 will not light it, and it may later be a pin you meant to keep free.&lt;/p&gt;

&lt;p&gt;Pico 2, the RP2350 board without W, follows the original wiring: the user LED is GPIO 25. Pico 2 W follows the Pico W idea: the alias &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt; is the lamp, and GPIO 25 is not that lamp. The practical rule is to read the silk, then pick the constructor. Do not “fix” a dark LED by moving a wire onto a motor driver.&lt;/p&gt;

&lt;p&gt;The lab blink is 2 Hz: on for 250 ms, off for 250 ms, so the full period is 500 ms.&lt;/p&gt;

\[f = \frac{1}{0.250\,\mathrm{s} + 0.250\,\mathrm{s}} = 2\,\mathrm{Hz}.\]

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;machine&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Pico W / Pico 2 W: the lamp is Pin(&quot;LED&quot;), not GPIO 25.
# Original Pico and Pico 2 (no W): the lamp is GPIO 25.
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;LED&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;led&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Run it from the editor and watch the lamp before you save anything as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt;. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt; with a syntax error runs at every power-up and feels like a dead board. If that happens, hold BOOTSEL, get back to a prompt, and remove or edit the file. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;time.sleep&lt;/code&gt; is acceptable in this first blink because the only job is to see 2 Hz and a counter. The next lesson drops the sleep so the loop can also watch a 300 ms command timeout.&lt;/p&gt;

&lt;h2 id=&quot;power-in-one-paragraph&quot;&gt;Power, in one paragraph&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt; is 5 V from USB when the cable is plugged in. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; is the board input and can take roughly 1.8 V to 5.5 V; USB reaches it through a diode. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; is the regulated output. GP pins are 3.3 V. Five volts on a GP pin is how Picos die, and 7.4 V from a 2S pack does not belong on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; or on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt;. You will measure the rails in the pinout lesson. You will not feed them from a battery today.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Chi has an original Pico and a phone cable that has never copied a file. The first plug, BOOTSEL held correctly, produces no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt; drive. A Ugreen data cable produces the drive. Chi almost drops a Pico 2 UF2 on it because that was the file open in the downloads folder. The silk says Pico, not Pico 2, so the file comes from the RPI_PICO page instead. The drive vanishes. Thonny still says the local Python, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import machine&lt;/code&gt; fails. Switching the interpreter to MicroPython (Raspberry Pi Pico) yields a prompt. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 + 1&lt;/code&gt; prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; works on this firmware and the green lamp flashes twice a second. The counter in the shell climbs once per flash. The note says: “original Pico, RP2040 UF2, LED alias worked, GPIO 25 is the same lamp, no Wi-Fi, no motors.”&lt;/p&gt;

&lt;p&gt;On the next bench a Pico 2 W shows the same drive name. The RP2040 file would have been the wrong family. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(&quot;LED&quot;)&lt;/code&gt; lights the lamp, and GPIO 25 is written down as a free pin, not as the LED.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Unplug the board. Hold BOOTSEL. Plug the data cable in. Release BOOTSEL. Confirm the volume is named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Download the matching UF2: &lt;a href=&quot;https://micropython.org/download/RPI_PICO/&quot;&gt;RPI_PICO&lt;/a&gt; for an original Pico, &lt;a href=&quot;https://micropython.org/download/RPI_PICO2/&quot;&gt;RPI_PICO2&lt;/a&gt; for a Pico 2, or the matching W file from the &lt;a href=&quot;https://micropython.org/download/&quot;&gt;download index&lt;/a&gt;. Copy it onto &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt; only. Wait until the drive disappears.&lt;/li&gt;
  &lt;li&gt;Open Thonny on MicroPython (Raspberry Pi Pico), or run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt;. Type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 + 1&lt;/code&gt;. You should see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Run the blink above. The LED should flash twice a second and the counter should climb: 1, 2, 3, without the prompt vanishing into a reboot.&lt;/li&gt;
  &lt;li&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; record the silk (Pico, Pico W, Pico 2, or Pico 2 W), the UF2 page you used, and whether &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;LED&quot;&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;25&lt;/code&gt; lit the lamp.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A pass is a REPL that is the Pico, not the laptop, and a 2 Hz blink you watched. A copy onto a different removable disk does not pass, even if the file “succeeded.”&lt;/p&gt;

&lt;p&gt;Faults. BOOTSEL was released before the plug went in, so no drive appears and the board jumps straight to whatever was already installed. The cable is charge-only, so neither the drive nor a serial port appears. The UF2 family is wrong: an RP2040 image on a Pico 2, or a W image on a non-W board. Thonny is still on local Python, so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;machine&lt;/code&gt; does not exist.&lt;/p&gt;

&lt;p&gt;Safety: 3.3 V only on every GP pin. Do not connect a battery, a motor, or a 5 V sensor in this lab. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt; is an output. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt; is not a place for a 2S pack.&lt;/p&gt;

&lt;h2 id=&quot;explanation-mapping-16-bit-pico-pwm-duty-cycles-and-bounding-loop-jitter&quot;&gt;Explanation: Mapping 16-Bit Pico PWM Duty Cycles and Bounding Loop Jitter&lt;/h2&gt;

&lt;p&gt;On Raspberry Pi Pico MicroPython, PWM duty cycle uses an unsigned 16-bit integer &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt; spanning \(0\) to \(65\,535\) (\(2^{16} - 1\)). Let’s trace both the PWM scaling math and the timing budget of a MicroPython control loop:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Convert normalized command \(u \in [0, 1]\) to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;:&lt;/strong&gt;
\(\text{duty\_u16} = \operatorname{round}(u \times 65\,535).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute effective average motor voltage:&lt;/strong&gt; With supply \(V_{\text{sup}}\) and driver drop \(V_{\text{drop}}\), \(V_{\text{eff}} = u \cdot (V_{\text{sup}} - V_{\text{drop}})\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Budget loop execution + GC pause:&lt;/strong&gt; For a loop period \(T_s = 1/f_s\), total worst-case iteration time \(t_{\text{code}} + t_{\text{GC}}\) must remain strictly below \(T_s\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Minimize heap churn:&lt;/strong&gt; Pre-allocate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bytearray&lt;/code&gt; objects outside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while True:&lt;/code&gt; loop so GC triggers rarely and finishes quickly.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-16-bit-pwm-scaling--50-hz-control-loop-timing-on-pico&quot;&gt;Concrete Worked Example: 16-Bit PWM Scaling &amp;amp; 50 Hz Control Loop Timing on Pico&lt;/h3&gt;

&lt;p&gt;Suppose your Pico drives a TB6612FNG powered by \(V_{\text{sup}} = 7.4\text{ V}\) (\(V_{\text{drop}} = 0.4\text{ V}\)) and you want an effective motor voltage of \(V_{\text{eff}} = 4.2\text{ V}\) inside a \(50\text{ Hz}\) (\(T_s = 20\text{ ms}\)) MicroPython loop:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Required duty cycle \(u\) and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;duty_u16&lt;/code&gt;:&lt;/strong&gt;
\(u = \frac{V_{\text{eff}}}{V_{\text{sup}} - V_{\text{drop}}} = \frac{4.2}{7.4 - 0.4} = \frac{4.2}{7.0} = 0.60 \quad (60\%).\)
\(\text{duty\_u16} = \operatorname{round}(0.60 \times 65\,535) = 39\,321.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Timing margin check at 50 Hz (\(T_s = 20.0\text{ ms}\)):&lt;/strong&gt;
Suppose reading encoders + IMU over I2C takes \(1.8\text{ ms}\), PID math takes \(0.4\text{ ms}\), and an occasional GC sweep takes \(2.5\text{ ms}\).
    &lt;ul&gt;
      &lt;li&gt;Worst-case execution time: \(1.8 + 0.4 + 2.5 = 4.7\text{ ms} \ll 20.0\text{ ms}\) (76.5% idle slack — super stable!).&lt;/li&gt;
      &lt;li&gt;Now check what happens if you try to push the same Python loop to \(500\text{ Hz}\) (\(T_s = 2.0\text{ ms}\)): a single \(2.5\text{ ms}\) GC pause exceeds the entire period, causing missed ticks!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Naming your script &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.py&lt;/code&gt; with an infinite &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while True:&lt;/code&gt; loop that prints continuously without a small startup delay — locking you out of the REPL until you send &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl+C&lt;/code&gt; or flash a nuke UF2 image. Always add a 1–2 second grace delay or physical run-enable switch at startup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; On RP2040, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ADC.read_u16()&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;65535&lt;/code&gt; (scaling the native 12-bit ADC left-aligned to 16 bits), so a reading of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;32768&lt;/code&gt; corresponds to \(3.3 \times (32768 / 65535) \approx 1.65\text{ V}\).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Write the BOOTSEL sequence as four physical actions, and name the drive that must appear. Why does that name fail to tell a Pico from a Pico 2?&lt;/li&gt;
  &lt;li&gt;Your blink uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pin(25)&lt;/code&gt; on a Pico W and the onboard lamp stays off. Give the replacement and say what GPIO 25 actually is on that board.&lt;/li&gt;
  &lt;li&gt;You copied &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI_PICO-....uf2&lt;/code&gt; onto a Pico 2. The drive disappeared and the REPL never comes back. What file replaces it, and what chip does the wrong file belong to?&lt;/li&gt;
  &lt;li&gt;Thonny says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;machine&lt;/code&gt; does not exist. What is the interpreter set to?&lt;/li&gt;
  &lt;li&gt;Why does this lesson forbid 5 V on a GP pin even for “just an echo wire”?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Answers&lt;/summary&gt;

1. Unplug fully. Hold BOOTSEL. Plug USB in while holding. Release BOOTSEL. The drive is named `RPI-RP2`. Original Pico and Pico 2 both use that label. The UF2 filename is what selects RP2040 or RP2350.
2. `led = Pin(&quot;LED&quot;, Pin.OUT)`. On a Pico W and a Pico 2 W the lamp is on the wireless chip. GPIO 25 is a normal GPIO and will not light the onboard LED.
3. Re-enter BOOTSEL and copy the UF2 from the RPI_PICO2 page. The file you used is the RP2040 image for the original Pico. Pico 2 is an RP2350.
4. It is aimed at the laptop’s local Python, or at the wrong port. Set MicroPython (Raspberry Pi Pico), or use `mpremote` on the Pico’s port.
5. GP pins are 3.3 V. A 5 V echo can destroy the pin. Motors and the 2S pack wait for a driver and for the rail lesson; they are not a way to test a dark LED.

&lt;/details&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Buy a Pico-family board and a data micro-USB cable if you do not already have them. Prices below are the public Hshop readings from 23 September 2026. The verified search &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;raspberry pi pico&lt;/a&gt; showed a Pico 2 at 195 000 ₫ and a Pico 2 W at 275 000 ₫ that day. Product pages: &lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-rp2350&quot;&gt;Pico 2, RP2350&lt;/a&gt; and &lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-w-rp2350&quot;&gt;Pico 2 W&lt;/a&gt;. The Pico 2 has no built-in Wi-Fi. The Pico 2 W does. Cable: &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Shopee, if you compare shops: &lt;a href=&quot;https://shopee.vn/search?keyword=raspberry%20pi%20pico%202&quot;&gt;Raspberry Pi Pico 2&lt;/a&gt;. If you are still deciding against the ESP32 default, the finished boards are the &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;NodeMCU-32S at 190 000 ₫&lt;/a&gt; and the &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32 at 265 000 ₫&lt;/a&gt;, not the &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;bare ESP32-S3-WROOM-1 at 135 000 ₫&lt;/a&gt;. &lt;a href=&quot;https://www.thegioiic.com/esp32-devkitc-32u-module-wifi-bluetooth-2-4ghz&quot;&gt;Thế Giới IC’s DevKitC-32U page&lt;/a&gt; is the other track. &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; is a component counter. The full cart stays in &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Chapter 00&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/micropython.html&quot;&gt;MicroPython on Raspberry Pi Pico&lt;/a&gt; — Raspberry Pi’s own setup notes for this firmware.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://datasheets.raspberrypi.com/pico/pico-datasheet.pdf&quot;&gt;Pico datasheet&lt;/a&gt; — GPIO 25, VBUS, VSYS, 3V3, and absolute maximum pin voltage.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micropython.org/download/&quot;&gt;MicroPython downloads&lt;/a&gt; — the index that separates RP2040 and RP2350 UF2 files. Use the board page, not a forum attachment.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Arduino IDE and PlatformIO on ESP32</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter02/02_02_esp32_platformio/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter02/02_02_esp32_platformio</id>
   <content type="html">&lt;p&gt;The Arduino IDE lowered the barrier to embedded electronics for millions of makers, yet as soon as a robot firmware grows beyond one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ino&lt;/code&gt; file into separate modules for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;encoders.cpp&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pid.cpp&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;comms.cpp&lt;/code&gt;, a single-file sketch editor starts fighting you. &lt;strong&gt;PlatformIO&lt;/strong&gt; gives you the best of both worlds: full compatibility with Arduino and ESP-IDF libraries, paired with real C++ project structure, reproducible library pinning, and fast incremental builds.&lt;/p&gt;

&lt;p&gt;This lesson guides you through configuring PlatformIO for the ESP32 and understanding what happens under the hood between clicking &lt;em&gt;Build&lt;/em&gt; and seeing bytes stream back over USB.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;objectives&quot;&gt;Objectives&lt;/h2&gt;

&lt;p&gt;You will install one ESP32 toolchain and ignore the other until you have a blink you trust. Along the Arduino IDE 2 path you will add Espressif’s board index, install the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32&lt;/code&gt; package, and select a Dev Module or NodeMCU-32S entry that matches the silk. Along the PlatformIO path you will write a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; whose board is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt; and whose monitor is 115200. You will blink &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; when the core defines it, and GPIO 2 only after you have checked that your board actually wires the lamp there. You will recognize a missing port, a charge-only cable, a CH340 driver, and a brownout caused by a motor supply that was still connected during upload.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;The landscape lesson should already name an ESP32 dev board as your primary microcontroller. If you committed to a Pico, read this page as a map and do the Pico lesson next; installing both toolchains in one afternoon is how the wrong board id gets saved. You need a laptop and a USB cable that has copied files before. The full parts list remains the Chapter 00 shopping lesson (&lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM and shopping&lt;/a&gt;).&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-build-systems-memory-partitioning-and-flash-upload-mechanics&quot;&gt;Key ideas: Build Systems, Memory Partitioning, and Flash Upload Mechanics&lt;/h2&gt;

&lt;p&gt;Three embedded-systems concepts explain everything PlatformIO automates for you:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Compilation, Linking, and Memory Maps:&lt;/strong&gt; The compiler translates each &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.cpp&lt;/code&gt; file into an object file (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.o&lt;/code&gt;); the linker resolves symbols across libraries and packs code into &lt;strong&gt;Flash&lt;/strong&gt; (non-volatile, e.g., 4 MB on ESP32) and variables into &lt;strong&gt;SRAM&lt;/strong&gt; (volatile, ~320 KB usable), reporting exact &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&lt;/code&gt; usage after every build.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Declarative Environment Management (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt;):&lt;/strong&gt; Specifying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platform&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;framework&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;monitor_speed&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lib_deps&lt;/code&gt; with exact version tags makes builds deterministic across teammates and operating systems.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bootloader &amp;amp; Serial Flashing Protocol:&lt;/strong&gt; Holding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BOOT&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO0&lt;/code&gt; low) during reset puts the ESP32 into its ROM serial bootloader, which receives the compiled &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bin&lt;/code&gt; image over UART (via a CP2102/CH340 USB-to-UART bridge) and writes it into Flash.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters-for-capstone-a&quot;&gt;Why it matters for Capstone A&lt;/h2&gt;

&lt;p&gt;Chapter 07’s diff-drive teleop is a firmware image on this same chip: left and right PWM toward a TB6612, a silence test of about 300 ms, wheels off the table. None of that image can be believed until this board accepts a binary, toggles a pin you chose, and prints a line at a baud rate you wrote down. Chapter 08’s Wi-Fi teleop uses the same upload path. A board that only blinks in someone else’s video has not joined your robot. The serial monitor, one port and 115200 baud, is the console you will trust before any radio is allowed to complicate the log.&lt;/p&gt;

&lt;h2 id=&quot;pick-one-install&quot;&gt;Pick one install&lt;/h2&gt;

&lt;p&gt;Two roads share Espressif’s Arduino core. Pick the one your lab machine already has. You are not asked to start on ESP-IDF this week.&lt;/p&gt;

&lt;p&gt;Arduino IDE 2 is the click-through path. Install the IDE. Open Preferences (or Settings) and paste this Additional Boards Manager URL:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;https://espressif.github.io/arduino-esp32/package_esp32_index.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Open the Boards Manager, search &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32&lt;/code&gt;, and install &lt;strong&gt;esp32 by Espressif Systems&lt;/strong&gt;. Then open the board menu and select the entry that matches the silk. A generic 30-pin or 38-pin DevKit is &lt;strong&gt;ESP32 Dev Module&lt;/strong&gt;. A board whose silk says NodeMCU-32S should use the &lt;strong&gt;NodeMCU-32S&lt;/strong&gt; entry when that entry exists. Set the port to the new serial device. If the first upload fails with a sync or timeout error, set Upload Speed to 115200. The IDE often starts at 921600, and a long cable or a CH340 clone drops bytes at that rate.&lt;/p&gt;

&lt;p&gt;PlatformIO is the path with a file you can diff. Install Visual Studio Code, then the PlatformIO IDE extension, and let the first-run download finish. Create a project and put this in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-ini highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;[env:esp32dev]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;platform&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;espressif32&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;board&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;esp32dev&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;framework&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;arduino&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;monitor_speed&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;115200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt; is the generic classic-ESP32 target: a WROOM-class module and a USB-serial adapter. It matches the NodeMCU-32S and the common DevKit V1 boards in this course. It is the wrong id for an ESP32-S3 native-USB board. That is another reason the landscape lesson told you to buy a classic dev board unless you are ready to change this file and the pin map together.&lt;/p&gt;

&lt;h2 id=&quot;the-lamp-and-the-port&quot;&gt;The lamp and the port&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; is a macro some board variants define. If your selected board defines it, use it for the first blink. If the header does not define it, use GPIO 2. GPIO 2 is the onboard LED on many DevKit V1 boards, and it is a common wiring on NodeMCU-32S clones. It is not a promise. Some boards put the lamp on another GPIO, and some have only a power LED that firmware cannot blink. Read the silk or the vendor diagram, write the number in your notes, and change the constant if GPIO 2 stays dark. Do not reinstall the IDE because a macro was optimistic.&lt;/p&gt;

&lt;p&gt;A short sketch that matches this lab:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#ifndef LED_BUILTIN
#define LED_BUILTIN 2  // common on DevKit V1; confirm your pinout
#endif
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HIGH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;high&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;low&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.begin(115200)&lt;/code&gt; must match the monitor. A mismatch does not look like silence. It looks like characters that are almost letters. This sketch uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; so the first success is obvious. The heartbeat lesson replaces that structure, because a later stop command cannot wait inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay&lt;/code&gt; for a 300 ms timeout.&lt;/p&gt;

&lt;p&gt;The chip near the USB plug is not the ESP32. On classic dev boards it is a CH340 or a CP2102. CP2102 ports usually appear with the operating system’s own driver. CH340 boards on Windows often stay invisible until you install the WCH CH340 driver; Device Manager then shows a new COM port. On Linux the same chips appear as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; (then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ttyUSB1&lt;/code&gt; if a second adapter is plugged in). On Windows the name is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COM3&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COM4&lt;/code&gt;, or another &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COMx&lt;/code&gt; that was not there before you plugged the cable in. Your account on Linux needs to be in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; group, and the group membership applies only after you log out and back in.&lt;/p&gt;

&lt;p&gt;A charge-only USB cable enumerates nothing. The power LED can still glow, because power wires are present and data wires are not. Swap in a cable you have used to copy a file. The &lt;a href=&quot;#where-to-buy-in-vietnam&quot;&gt;data cable in the shopping section&lt;/a&gt; is the known-good shape for these micro-USB boards.&lt;/p&gt;

&lt;p&gt;Download mode is a button story. GPIO 0 sampled low at reset puts a classic ESP32 into the serial bootloader. The BOOT button (sometimes IO0) holds that pin. EN, RST, or RESET restarts the chip. Many dev boards enter the bootloader automatically through the USB-serial chip’s DTR and RTS lines. When that circuit is missing, the upload times out. Hold BOOT, start the upload, and release BOOT when the log says it is connecting. If that is awkward, hold BOOT, tap EN, release BOOT, and upload while the chip stays in that mode.&lt;/p&gt;

&lt;h2 id=&quot;what-a-failed-upload-usually-is&quot;&gt;What a failed upload usually is&lt;/h2&gt;

&lt;p&gt;Three faults cover almost every “the board is dead” report in this lab.&lt;/p&gt;

&lt;p&gt;The board menu is wrong. An S3 or C3 entry, or a random “ESP32 Pico Kit” entry, does not match a NodeMCU-32S. The upload complains, or it appears to succeed and the pin you blink is not the pin you think. Set Dev Module or NodeMCU-32S, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board = esp32dev&lt;/code&gt;, to match the silk.&lt;/p&gt;

&lt;p&gt;No port appears. Start with the cable. Then the driver: CH340 on Windows, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dialout&lt;/code&gt; on Linux. A port left over from yesterday’s Pico (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyACM0&lt;/code&gt; while the ESP32 is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt;) is the same class of mistake.&lt;/p&gt;

&lt;p&gt;The chip resets while the bootloader is talking. If a motor, or a driver whose VM pin is still tied to the board’s 5 V pin, is attached, the current spike browns the rail out and the download dies mid-way. Unplug VM. Unplug every motor wire. Upload again with only the USB cable. The heartbeat lesson will show you the same reset as a repeating &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt; once the sketch is running; during upload it looks like a timeout instead.&lt;/p&gt;

&lt;p&gt;GPIO 1 and GPIO 3 are the UART pins the USB-serial chip is already using. A blink on those pins turns the console into noise. Leave them alone today.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Bình’s board is the Hshop NodeMCU-32S, CH340, on a Windows laptop. The first plug lights the power LED and Device Manager gains nothing. A CH340 driver install produces &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COM5&lt;/code&gt;. The sketch above is pasted into Arduino IDE 2 with the NodeMCU-32S board entry and upload speed 115200. The first upload still fails. A TB6612 from a previous bench session has VM jumpered to the dev board’s 5 V pin. Bình unplugs that VM wire, holds BOOT, and uploads again. The monitor at 115200 prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt;, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;high&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;low&lt;/code&gt; in time with the lamp. GPIO 2 matches the silk on this board, so the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#ifndef&lt;/code&gt; fallback and the real LED are the same pin. Changing only the monitor to 9600 fills the pane with garbage while the LED keeps blinking, which separates a baud mistake from a dead chip.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Choose Arduino IDE 2 or PlatformIO. Install that one path using the URL or the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; above.&lt;/li&gt;
  &lt;li&gt;Plug in the data cable. Confirm a new port: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; on Linux or a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COMx&lt;/code&gt; on Windows. Write the name down.&lt;/li&gt;
  &lt;li&gt;Select ESP32 Dev Module or NodeMCU-32S, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board = esp32dev&lt;/code&gt;. Set the monitor to 115200.&lt;/li&gt;
  &lt;li&gt;Paste the sketch. If your silk’s LED is not GPIO 2 and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; is dark, change the pin and write the new number in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Upload. If the log times out, hold BOOT and retry once. If a motor or a VM wire is attached, unplug VM and retry.&lt;/li&gt;
  &lt;li&gt;Open the monitor at 115200. You should see one &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt; and then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;high&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;low&lt;/code&gt; lines while the onboard LED blinks. Copy eight lines into your notes with the port name and the USB chip (CH340 or CP2102).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A pass is a lamp you watched and a log you pasted. A power LED that is on whenever USB is plugged in does not count.&lt;/p&gt;

&lt;p&gt;Safety: USB only. No battery, no motor, no VM connection. The 5 V pin is not a motor supply, and a stall during upload is exactly the brownout this lab is teaching you to remove.&lt;/p&gt;

&lt;h2 id=&quot;explanation-reading-a-platformio-memory-summary-and-sizing-flash-time&quot;&gt;Explanation: Reading a PlatformIO Memory Summary and Sizing Flash Time&lt;/h2&gt;

&lt;p&gt;After every PlatformIO build, you see two percentages: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RAM&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flash&lt;/code&gt;. Don’t ignore them — learn to read them like a firmware engineer in four steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Inspect static RAM usage:&lt;/strong&gt; Global arrays, ring buffers, and RTOS stacks live in SRAM. Keep static RAM below \(60\text{–}70\%\) so dynamic heap/stack allocations (especially if Wi-Fi is enabled) never collide and cause a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Guru Meditation Error&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Inspect Flash usage:&lt;/strong&gt; Firmware code, constant lookup tables (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const&lt;/code&gt;), and string literals live in Flash.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Estimate upload time:&lt;/strong&gt; Flashing a binary of size \(S\) bytes at upload baud rate \(B\) (8N1, ~10 bits/byte plus ~20% protocol overhead) takes \(t_{\text{flash}} \approx 1.2 \times \frac{10 S}{B}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Match &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;monitor_speed&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.begin(...)&lt;/code&gt;:&lt;/strong&gt; Any mismatch turns clean CSV telemetry into unreadable garbled characters.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-sizing-a-flightdrive-telemetry-ring-buffer-in-esp32-sram&quot;&gt;Concrete Worked Example: Sizing a Flight/Drive Telemetry Ring Buffer in ESP32 SRAM&lt;/h3&gt;

&lt;p&gt;Suppose your ESP32 build reports &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RAM: 14.0% (used 45,875 bytes from 327,680 bytes)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Flash: 24.0% (used 314,572 bytes from 1,310,720 bytes)&lt;/code&gt;. You want to add an in-RAM ring buffer storing 10 seconds of 100 Hz telemetry structs, where each struct contains:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uint32_t t_ms&lt;/code&gt; (4 bytes)&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float v_ref, v_meas, pwm, theta&lt;/code&gt; (\(4 \times 4 = 16\) bytes)
Total struct size \(= 20\text{ bytes}\).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Buffer memory requirement:&lt;/strong&gt;
\(\text{Samples} = 10\text{ s} \times 100\text{ Hz} = 1\,000\text{ samples}.\)
\(\text{Buffer RAM} = 1\,000 \times 20\text{ bytes} = 20\,000\text{ bytes} \approx 19.5\text{ KB}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — New RAM percentage:&lt;/strong&gt;
\(\frac{45\,875 + 20\,000}{327\,680} = \frac{65\,875}{327\,680} \approx 20.1\%\) — plenty of headroom!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Expected Flash upload time at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;upload_speed = 921600&lt;/code&gt;:&lt;/strong&gt;
\(t_{\text{flash}} \approx 1.2 \times \frac{10 \times 314\,572}{921\,600} \approx 4.1\text{ seconds}\) (versus \(\approx 32.8\text{ s}\) at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;115200&lt;/code&gt; baud!).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; concatenation (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;msg += String(x) + &quot;,&quot;&lt;/code&gt;) inside a 100 Hz Arduino loop on an MCU — repeated dynamic heap allocation fragments SRAM over hours and eventually crashes the robot. Use fixed-size &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;char buf[64]&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snprintf&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; sets &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;monitor_speed = 115200&lt;/code&gt;, your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setup()&lt;/code&gt; must call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.begin(115200)&lt;/code&gt; — and unplugging jumper wires from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO0&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO2&lt;/code&gt; resolves 90% of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Failed to connect to ESP32&lt;/code&gt; flashing errors.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Write the four &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt; keys this lesson requires, and write the Arduino IDE boards URL from memory.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LED_BUILTIN&lt;/code&gt; leaves the board dark, and the monitor still prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;high&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;low&lt;/code&gt;. What do you check on the silk, and why is “GPIO 2” a hypothesis rather than a fact?&lt;/li&gt;
  &lt;li&gt;Upload works only while BOOT is held. Which pin is that button, and what do you do with EN?&lt;/li&gt;
  &lt;li&gt;The log prints &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt; over and over and never stays in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt;. A motor driver is still on the desk, VM tied to the 5 V pin. What do you unplug, and what electrical event was resetting the chip?&lt;/li&gt;
  &lt;li&gt;The LED blinks and the monitor shows garbage. Name the two baud numbers that disagree. Why is the chip itself still fine?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Answers&lt;/summary&gt;

1. `platform = espressif32`, `board = esp32dev`, `framework = arduino`, `monitor_speed = 115200`. The boards URL is `https://espressif.github.io/arduino-esp32/package_esp32_index.json`, then the package **esp32 by Espressif Systems**.
2. Find the onboard LED’s GPIO on your board’s pinout. GPIO 2 is the lamp on many DevKit V1 boards and on many NodeMCU-32S clones, and other boards differ. Change the constant to the pin the silk names, and record it.
3. BOOT pulls GPIO 0 low so reset enters the serial bootloader. Hold BOOT, tap EN (reset), release BOOT, and upload while the chip remains in that mode. Boards with a working auto-reset circuit hide this.
4. Unplug VM, and unplug any motor. The extra current sagged the rail (a brownout) and the bootloader reset before the download finished.
5. The sketch is at 115200 and the monitor is at something else, often 9600. The LED is toggling, so the program is running. Fix the monitor speed; do not reinstall the toolchain.

&lt;/details&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;If the landscape lesson’s dev board and data cable already enumerate, buy nothing. If the port never appears, replace the cable before you replace the board. Prices were read from public Hshop listings on 23 September 2026.&lt;/p&gt;

&lt;p&gt;Default board: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;ESP32 NodeMCU-32S CH340, 190 000 ₫&lt;/a&gt;. Alternate finished board: &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32, 265 000 ₫&lt;/a&gt;. The &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;ESP32-S3-WROOM-1 at 135 000 ₫&lt;/a&gt; is a bare module and will not appear as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/ttyUSB0&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;COMx&lt;/code&gt; by itself. Cable: &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Shop searches if you compare: &lt;a href=&quot;https://shopee.vn/search?keyword=esp32%20devkit%20ch340&quot;&gt;Shopee ESP32 DevKit CH340&lt;/a&gt;. &lt;a href=&quot;https://www.thegioiic.com/esp32-devkitc-32u-module-wifi-bluetooth-2-4ghz&quot;&gt;Thế Giới IC’s DevKitC-32U page&lt;/a&gt; is a board-style listing; confirm it is a USB dev board and note whether the antenna is the U.FL version. &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; is a component counter, not the place to grab a bare module and call it this lab’s starter. The rest of the robot cart is still &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Chapter 00&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.platformio.org/en/latest/boards/espressif32/esp32dev.html&quot;&gt;PlatformIO &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt; board page&lt;/a&gt; — the board id in this lesson’s ini file.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html&quot;&gt;Installing the Arduino ESP32 core&lt;/a&gt; — the boards URL and the IDE steps, from Espressif.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/en/support/download/documents&quot;&gt;Espressif document downloads&lt;/a&gt; — datasheets and hardware design guides when a clone’s LED pin disagrees with GPIO 2.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Choosing a microcontroller for a small robot</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter02/02_01_mcu_landscape/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter02/02_01_mcu_landscape</id>
   <content type="html">&lt;p&gt;Choosing a microcontroller for a robot is like choosing an engine for a vehicle: peak clock speed tells only half the story. An Arduino Uno runs at 16 MHz with 2 KB of RAM and only two external interrupt pins — enough for a simple line follower, cramped the moment you add two quadrature encoders, an IMU, a PID loop, and telemetry. An ESP32 or Raspberry Pi Pico (RP2040), by contrast, costs roughly the same yet brings dual cores, hardware PWM/PIO timers, and hundreds of kilobytes of RAM.&lt;/p&gt;

&lt;p&gt;This lesson compares the modern MCU landscape through the lens of &lt;strong&gt;real-time robotics peripherals&lt;/strong&gt; so you select hardware for the right engineering reasons.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;objectives&quot;&gt;Objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can compare three honest brains for a small diff-drive robot: a classic ESP32 dev board, a Raspberry Pi Pico or Pico 2, and an Arduino Uno-class board. You can say what each one does with logic voltage, radio, and the first language you will actually type. You can choose the ESP32 NodeMCU-32S when phone teleop is coming, and you can say why a bare module and a Pico without Wi-Fi are different purchases. You can write the bring-up order that stops a brownout from looking like a random bug in the sketch. You can also compute a classroom threshold for a 3.3 V logic high and use it to refuse a 5 V signal on a GPIO.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can set a meter to DC volts, and you know a GPIO is a pin the chip can drive or read. A series resistor in front of an LED, from Chapter 01, is enough. You do not need to have flashed firmware. The full cart stays in the Chapter 00 shopping lesson (&lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;BOM and shopping&lt;/a&gt;). This page only chooses the brain and the cable.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-cpu-architecture-hardware-peripherals-and-real-time-determinism&quot;&gt;Key ideas: CPU Architecture, Hardware Peripherals, and Real-Time Determinism&lt;/h2&gt;

&lt;p&gt;Evaluating an MCU for Capstone A rests on three technical pillars:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Core Architecture &amp;amp; Floating-Point Support:&lt;/strong&gt; 8-bit AVR chips (ATmega328P) emulate 32-bit floats slowly in software, whereas 32-bit Xtensa (ESP32) and ARM Cortex-M0+/M4 (RP2040, STM32F4) execute 32-bit math natively — with hardware FPU on ESP32 and STM32F4 cutting PID and trigonometry cycle counts by 10×.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Dedicated Robotics Peripherals:&lt;/strong&gt; Hardware timers for glitch-free PWM, hardware quadrature decoder / PIO state machines for zero-CPU-overhead encoder counting, and DMA-backed I2C/SPI/UART buses free the CPU for control and estimation.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Connectivity vs. Determinism:&lt;/strong&gt; ESP32 integrates Wi-Fi and Bluetooth (great for wireless telemetry and micro-ROS), though Wi-Fi interrupts run on Core 0 — which is why pinning your real-time control loop to Core 1 is a standard robotics pattern.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters-for-capstone-a&quot;&gt;Why it matters for Capstone A&lt;/h2&gt;

&lt;p&gt;Capstone A, in Chapter 07, is a differential-drive base: two driven wheels, a caster, and firmware that turns a command into PWM for a TB6612 later. The command stream is not trusted forever. If fresh commands stop for about 300 ms, both wheel duties go to zero while the chassis is still wheels-up on the bench. That loop has to live on one microcontroller you already trust. Chapter 08 then has a Wi-Fi and MQTT lab, which is how the same teleop can leave the USB cable and reach a phone or a laptop. An ESP32 already has 2.4 GHz Wi-Fi and BLE on the module, so it is the course default. A Pico without a W suffix can still run the Chapter 07 firmware over USB, and it is a fine fast-iteration board, but it cannot join the Chapter 08 radio lab until you buy a different board. The choice you write down today is the pin map, the toolchain, and the radio story for the rest of the semester.&lt;/p&gt;

&lt;h2 id=&quot;three-boards-three-different-jobs&quot;&gt;Three boards, three different jobs&lt;/h2&gt;

&lt;p&gt;An ESP32 in this course means a classic dual-core Xtensa module on a dev board, with Wi-Fi and BLE in the metal can and GPIO at 3.3 V. Buy a board that already has a USB connector, a 3.3 V regulator, a USB-serial chip, a BOOT button, and headers. The serial chip is usually a CH340, very common on boards sold in Vietnam, or a CP2102, which the operating system often recognizes with no extra driver. A bare module has none of that. The NodeMCU-32S with CH340 is the shape of the default. The Vietduino ESP32 listing is another finished dev board. The ESP32-S3-WROOM-1 line is a bare module and stays off the starter list even though the price is lower.&lt;/p&gt;

&lt;p&gt;A Raspberry Pi Pico or Pico 2 is the fast-iteration alternate. The original Pico uses an RP2040. Pico 2 uses an RP2350. Both run GPIO at 3.3 V and both answer you in a MicroPython REPL. The original Pico and the Pico 2 without a W have no built-in Wi-Fi. Pico W and Pico 2 W add a radio. Write the exact silk in your notes, because the LED pin and the firmware file change with that suffix. Choose this family when the REPL is what you want to learn first, and when Chapter 08’s Wi-Fi lab can wait for a W board or a switch to the ESP32.&lt;/p&gt;

&lt;p&gt;An Arduino Uno-class board, the ATmega328P and its clones, is 5 V logic. It is worth recognizing so you do not confuse it with the robot brain. It has no Wi-Fi, and a 5 V high does not belong on a 3.3 V input. An HC-SR04 echo or an Uno pin marked “just a signal” can destroy an ESP32 or Pico pin. Keep an Uno as a known 5 V reference if you already own one. Leave it out of the Capstone A socket.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/pcb_mcu_anatomy.png&quot; alt=&quot;Anatomy of a dev board: USB, regulator, headers, and the module&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The USB connector is how the laptop enumerates the board. The regulator makes 3.3 V. The headers are the only pins you wire. A bare module in a tray has the can and none of the rest.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/ESP32_on_Lolin32_Lite_clone_board_cropped.jpg&quot; alt=&quot;ESP32 module on a small dev-board clone&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Match your own board to the metal can, the USB socket, and the headers before you trust a pinout drawn for a different clone.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Raspberry_Pi_Pico.jpg&quot; alt=&quot;Raspberry Pi Pico, the MicroPython alternate&quot; /&gt;&lt;/p&gt;

&lt;p&gt;BOOTSEL, the micro-USB plug, and the labels &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3V3&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSYS&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBUS&lt;/code&gt; are the landmarks. This photograph is an original Pico: no radio, onboard LED on GPIO 25.&lt;/p&gt;

&lt;h2 id=&quot;logic-voltage-is-a-number&quot;&gt;Logic voltage is a number&lt;/h2&gt;

&lt;p&gt;Inside a CMOS input, “high” is a voltage relative to that chip’s own supply. A classroom reading rule, not a datasheet \(V_{IH}\), is that you treat a pin as high only when the voltage is at least about \(0.7\) times the supply:&lt;/p&gt;

\[0.7 \times 3.3\,\mathrm{V} \approx 2.3\,\mathrm{V}.\]

&lt;p&gt;On the ESP32 and the Pico that teaching threshold is about 2.3 V. The real \(V_{IL}\) and \(V_{IH}\) live in the electrical-characteristics table and they move with \(V_{DD}\). You will look them up when a divider is close to the edge. Until then the operating rule is stricter than the formula: GPIO in this course is 3.3 V only.&lt;/p&gt;

&lt;p&gt;The damage threshold is why the Uno is awkward next to those pins. A 3.3 V GPIO’s absolute maximum is only a little above the rail, on the order of&lt;/p&gt;

\[3.3\,\mathrm{V} + 0.3\,\mathrm{V} = 3.6\,\mathrm{V}.\]

&lt;p&gt;Five volts is above that. The protection diode conducts, the pin heats, and the chip can die on the first connection. A 5 V HC-SR04 echo is the same problem in a smaller package. Level shifting waits for the pinout lesson; the decision to keep 5 V off the GPIO header is made now.&lt;/p&gt;

&lt;h2 id=&quot;bring-up-order&quot;&gt;Bring-up order&lt;/h2&gt;

&lt;p&gt;Most “random” firmware bugs in the first week are a rail that sagged. The chip resets, the counter returns to zero, and the sketch you just edited looks guilty. Each step below adds one new thing, so the signature stays obvious.&lt;/p&gt;

&lt;p&gt;The laptop must see the board first. On an ESP32 that is a serial port. On a Pico, when you ask for the bootloader, it is a drive named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPI-RP2&lt;/code&gt;. Then the onboard LED blinks under your program, so a clock and a pin you chose are alive. Then a line of serial at 115200 baud shows that the cable carries data. Then you read a button or a jumper to ground, so an input is real. Then you produce PWM on a pin that has nothing attached except perhaps an LED and a resistor. Only after those five observations do you connect a motor driver and a battery. A TT motor can pull the USB supply down when it stalls. Students who clip the driver on day one spend a week rewriting a loop that was never wrong.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/tracks_mcu_ros.png&quot; alt=&quot;From a microcontroller sketch toward later ROS work&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The left side of the figure is this chapter: one microcontroller, a blink, a serial line. Capstone A’s teleop sits further along, still on this chip, with PWM aimed at a TB6612 only after the driver wiring is legal. Later ROS work assumes this bring-up already happened.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;An wants a phone to drive a two-wheel robot later in the semester. Chapter 07 is the firmware teleop: PWM toward a TB6612, a timeout near 300 ms, wheels up the first time they may turn. Chapter 08 is where that teleop can use Wi-Fi. An writes: “Primary MCU is an ESP32 NodeMCU-32S with CH340, because I need Wi-Fi in Chapter 08 and a 3.3 V board I can flash this week.”&lt;/p&gt;

&lt;p&gt;The bare ESP32-S3-WROOM-1 is cheaper and it does not plug into the laptop. A Pico 2 without W has excellent MicroPython and no radio, so the phone waits. An buys one brain. The line under the sentence is the bring-up order: USB port, onboard LED, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt; at 115200, a button that reads low when pressed, PWM on an unloaded pin, and only then a TB6612 and a battery, chassis lifted. The Uno stays a 5 V reference.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;You can do this lab on paper before the parcel arrives.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Draw a three-row table. Rows: Uno-class, ESP32 dev board, Pico or Pico 2. Columns: GPIO voltage, built-in wireless, first language you will type, and whether it is the Capstone A brain.&lt;/li&gt;
  &lt;li&gt;Circle one primary row. Under the table write the six bring-up steps in order: USB enumerates, onboard LED, serial at 115200, button read, PWM on an unloaded pin, driver and battery.&lt;/li&gt;
  &lt;li&gt;Add one sentence that names brownout: a sagging rail resets the chip and imitates a software bug.&lt;/li&gt;
  &lt;li&gt;If you are buying this week, copy the exact product name and the price you saw into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; with the date. If the board is already on the desk, write its silk and its USB-serial chip if one is printed, and do not flash it yet.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should observe a note that a classmate can audit. A pass names one primary MCU, says what the other two families are for, and lists bring-up in the order above. “I will try both and see” does not pass.&lt;/p&gt;

&lt;p&gt;A listing photo of a metal can with no USB socket is a module. A charge-only phone lead lights a power LED and never creates a port. Fix the listing before you fix a driver.&lt;/p&gt;

&lt;p&gt;Safety: do not connect a battery, a motor, or a 5 V sensor while you are still choosing the board. The only electrical act today is reading a label.&lt;/p&gt;

&lt;h2 id=&quot;explanation-budgeting-cpu-cycles-and-encoder-interrupt-load-across-mcus&quot;&gt;Explanation: Budgeting CPU Cycles and Encoder Interrupt Load Across MCUs&lt;/h2&gt;

&lt;p&gt;Why do two high-resolution wheel encoders overwhelm a slow 8-bit MCU while barely registering on an ESP32 or RP2040? Let’s calculate the exact interrupt load step by step:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compute encoder pulse rate per wheel:&lt;/strong&gt; If a motor spins at \(\text{RPM}_{\text{motor}}\) (before gearbox!) with \(P_{\text{rev}}\) pulses per revolution in 4× quadrature decoding, edge rate is:
\(f_{\text{edge}} = \frac{\text{RPM}_{\text{motor}}}{60} \times 4 P_{\text{rev}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Multiply by 2 wheels&lt;/strong&gt; for a differential-drive robot: \(f_{\text{total}} = 2 f_{\text{edge}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Multiply by ISR execution time \(t_{\text{ISR}}\)&lt;/strong&gt; to find CPU utilization: \(U = f_{\text{total}} \cdot t_{\text{ISR}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify margin:&lt;/strong&gt; Keep interrupt CPU load \(U &amp;lt; 10\%\) so PID control and I2C IMU reads never starve.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-comparing-16-mhz-avr-vs-240-mhz-esp32-on-dual-encoders&quot;&gt;Concrete Worked Example: Comparing 16 MHz AVR vs. 240 MHz ESP32 on Dual Encoders&lt;/h3&gt;

&lt;p&gt;Suppose each gearmotor has a 1:48 gearbox and a magnetic encoder on the motor shaft with \(P_{\text{rev}} = 11\text{ PPR}\). At wheel speed \(150\text{ RPM}\), the motor shaft spins at \(150 \times 48 = 7\,200\text{ RPM}\):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Quadrature edge rate (4× decoding) for two wheels:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;One wheel: \(f_{\text{edge}} = \frac{7\,200}{60} \times (4 \times 11) = 120 \times 44 = 5\,280\text{ edges/s}\).&lt;/li&gt;
      &lt;li&gt;Two wheels: \(f_{\text{total}} = 2 \times 5\,280 = 10\,560\text{ interrupts/s}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — CPU utilization comparison:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;16 MHz Arduino Uno&lt;/strong&gt; (typical Arduino &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;attachInterrupt&lt;/code&gt; ISR \(\approx 5\,\mu\text{s}\)):
\(U_{\text{Uno}} = 10\,560 \times 5 \times 10^{-6} = 5.28\%\) — and Uno only has 2 hardware &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INT&lt;/code&gt; pins total, so 4× quadrature on 2 wheels requires pin-change interrupts!&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;240 MHz ESP32&lt;/strong&gt; (hardware &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PCNT&lt;/code&gt; pulse counter peripheral):
\(U_{\text{ESP32}} = 0.0\%\text{ CPU!}\) The hardware counter accumulates all 10,560 edges/s autonomously while the CPU sleeps or runs Kalman filtering.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Picking an MCU solely by MHz while ignoring ADC linearity, 3.3 V logic tolerance, or whether it has enough free PWM/encoder pins after reserving SPI/I2C/UART.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; With 11 PPR × 4× × 48 gear ratio, one full wheel revolution produces \(11 \times 4 \times 48 = 2\,112\) counts; on a 65 mm diameter wheel (circumference \(\pi \times 65 \approx 204.2\text{ mm}\)), each count resolves \(204.2 / 2112 \approx 0.097\text{ mm}\)!&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Fill the comparison for Uno-class, ESP32, and Pico, including original Pico versus Pico W and Pico 2 versus Pico 2 W. One cell has to say “5 V GPIO.”&lt;/li&gt;
  &lt;li&gt;A classmate clips a driver and a 2S pack before any serial line works, then says the counter “randomly restarts.” Name the electrical event and the bring-up step they skipped.&lt;/li&gt;
  &lt;li&gt;Compute \(0.7 \times 3.3\) and \(3.3 + 0.3\) in volts. In two sentences, say what each number is for, and why a 5 V echo still may not touch the pin.&lt;/li&gt;
  &lt;li&gt;Write your Capstone decision sentence. If you pick the ESP32, name the dev board and say you are not buying a bare WROOM. If you pick a Pico, say how Wi-Fi arrives later.&lt;/li&gt;
  &lt;li&gt;A friend says a Raspberry Pi 5 should be the brain because “robots use Linux.” What does Capstone A actually run on, this semester?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Answers&lt;/summary&gt;

1. Uno-class: 5 V GPIO, no built-in Wi-Fi, Arduino C++ as the usual first language, poor fit as the Capstone brain next to 3.3 V sensors. ESP32 dev board: 3.3 V GPIO, Wi-Fi and BLE, Arduino via the ESP32 core or MicroPython later, course default when Chapter 08 should use the radio. Original Pico and Pico 2 (no W): 3.3 V GPIO, no built-in Wi-Fi, MicroPython is the fast path. Pico W and Pico 2 W: same 3.3 V family with a radio added.
2. The supply sagged and the chip reset (a brownout). They skipped the chain that ends only after USB, blink, serial, a button, and unloaded PWM have been seen. The restarting counter is the boot path running again.
3. $$0.7 \times 3.3 = 2.31\,\mathrm{V}$$, about 2.3 V, a classroom threshold for “is this high enough to count as high?”. $$3.3 + 0.3 = 3.6\,\mathrm{V}$$, about the absolute maximum region of a 3.3 V pin. A 5 V echo is above 3.6 V, so it can damage the pin even though 5 V is a perfectly normal high on an Uno.
4. A passing ESP32 sentence names a USB dev board (NodeMCU-32S or another DevKit with CH340 or CP2102), Wi-Fi for Chapter 08, and refuses the bare module. A passing Pico sentence names MicroPython, states that the non-W board has no Wi-Fi, and names a later W board or a later switch to the ESP32.
5. Capstone A’s teleop firmware runs on the ESP32 or the Pico. A Pi 5 is a separate Linux computer and does not replace that chip, the data cable, or the bring-up order.

&lt;/details&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The full cart, including the TB6612, motors, and sensors, lives in the Chapter 00 shopping lesson. Buy the brain and a data cable here. Prices below were read from public Hshop listings on 23 September 2026. Read the live page before you pay.&lt;/p&gt;

&lt;p&gt;The default dev board is the &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;ESP32 NodeMCU-32S CH340, 190 000 ₫&lt;/a&gt;. It has USB and a CH340. A second finished board, if you want that listing, is the &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32, 265 000 ₫&lt;/a&gt;. The &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;ESP32-S3-WROOM-1, 135 000 ₫&lt;/a&gt; is a bare module. It is not the starter, even though it costs less.&lt;/p&gt;

&lt;p&gt;For the MicroPython track, the verified search &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;raspberry pi pico&lt;/a&gt; showed a Pico 2 at 195 000 ₫ and a Pico 2 W at 275 000 ₫ on that date. Product pages: &lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-rp2350&quot;&gt;Pico 2 (RP2350)&lt;/a&gt; and &lt;a href=&quot;https://hshop.vn/mach-raspberry-pi-pico-2-w-rp2350&quot;&gt;Pico 2 W&lt;/a&gt;. The non-W Pico 2 has no Wi-Fi.&lt;/p&gt;

&lt;p&gt;The cable that matches these micro-USB boards is the &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;. A charge-only lead enumerates nothing.&lt;/p&gt;

&lt;p&gt;Shopee searches, if you compare shops: &lt;a href=&quot;https://shopee.vn/search?keyword=esp32%20devkit%20ch340&quot;&gt;ESP32 DevKit CH340&lt;/a&gt; and &lt;a href=&quot;https://shopee.vn/search?keyword=raspberry%20pi%20pico%202&quot;&gt;Raspberry Pi Pico 2&lt;/a&gt;. Read whether the photo shows a USB socket. &lt;a href=&quot;https://www.thegioiic.com/esp32-devkitc-32u-module-wifi-bluetooth-2-4ghz&quot;&gt;Thế Giới IC’s ESP32-DevKitC-32U page&lt;/a&gt; is a dev-board style listing; check whether the antenna is the U.FL version and whether a cable is included. &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; is a component counter in Thủ Đức, useful later for passives, and easy to misread as a source of a finished USB board.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;ESP32 datasheet (PDF)&lt;/a&gt; — GPIO voltage, strapping pins, and the electrical table behind the 2.3 V classroom rule.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://datasheets.raspberrypi.com/pico/pico-datasheet.pdf&quot;&gt;Raspberry Pi Pico datasheet&lt;/a&gt; — which board has GPIO 25 as the LED, and what VBUS, VSYS, and 3V3 mean.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/&quot;&gt;Raspberry Pi microcontroller documentation&lt;/a&gt; — Pico, Pico W, Pico 2, and Pico 2 W as a family.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 01 lab checklist</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_11_ch01_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_11_ch01_lab_checklist</id>
   <content type="html">&lt;p&gt;Chapter 01 has taken you from Ohm’s law to soldering iron, from color bands to brownout protection. Before handing power and signals over to a microcontroller in Chapter 02, every professional hardware team runs an &lt;strong&gt;electrical bring-up sign-off&lt;/strong&gt;: verifying rails, continuity, divider voltages, and harness integrity quantitatively so Chapter 02 starts on rock-solid foundations.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Chapter 01 ends when seven pieces of evidence are in the notes, each one able to fail in a way you can point at. A tick beside “I understand pinouts” is not evidence. This lesson is the audit: what a real pass looks like, what a weak tick looks like, and the short path back when a row is thin. You may close the gates in any order. You may not replace a missing photo with a classmate’s.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will score seven gates against artifacts, not against a feeling of being done: an LED resistor calculation at 3.3 V, a breadboard rail-map photo, five resistors that were both decoded and measured, one polarity call confirmed in diode mode, a driver photo labeled TB6612 or DRV8833 rather than “H-bridge,” one solder-joint photo or a dated make-up if you still have no iron, and a brownout paragraph that names the separate motor battery. You will recognize a weak tick, such as a checkmark with no number or a photo of the wrong chip, and you will name the single lesson that repairs it. You will take a sloppy lab note and say which gate it fails.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 07 through 10 produced the artifacts this page inspects, and lesson 01 produced the LED arithmetic. The shopping list, when a row is blocked by a missing object, is Chapter 00 lesson 05. You need your notes and your photos. You still do not need a moving robot. Wheels stay up. No motor is connected to celebrate a pass.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-hardware-bring-up-sequencing-and-fault-tree-isolation&quot;&gt;Key ideas: Hardware Bring-Up Sequencing and Fault-Tree Isolation&lt;/h2&gt;

&lt;p&gt;A disciplined Chapter 01 sign-off rests on three hardware-engineering habits:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Power-Off Before Power-On:&lt;/strong&gt; Never apply battery voltage to a freshly wired circuit until resistance between every power rail and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; confirms no dead short and polarity checks pass.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Subsystem Isolation Testing:&lt;/strong&gt; Verify the battery pack, the 5 V buck converter, and the 3.3 V regulator &lt;em&gt;independently&lt;/em&gt; (disconnected from the MCU and motor driver) before joining rails. If a buck trimmer was shipped set to 12 V, catching it unloaded saves every downstream chip.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Quantitative Pass/Fail Thresholds:&lt;/strong&gt; Replace vague impressions (“looks fine”) with numerical tolerances — e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5.00 V ± 0.15 V&lt;/code&gt;, harness drop &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt; 0.10 V&lt;/code&gt;, and divider output strictly &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt; 3.30 V&lt;/code&gt;.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;The capstone is a differential-drive cart on a 3.3 V ESP32 or Pico, a TB6612 or a DRV8833, two TT motors on their own battery, and an HC-SR04 whose echo pin is 5 V. Chapter 02 will start attaching those pieces. If the rail map is a blank breadboard photo, the first dark LED is an afternoon. If the driver photo says only “H-bridge,” STBY never gets driven and the wheels sit still. If the brownout paragraph was never written, a reset when the motors share USB looks like a haunted sketch. The audit is how you notice those gaps while the only thing powered is the notebook.&lt;/p&gt;

&lt;h2 id=&quot;how-to-audit-a-row&quot;&gt;How to audit a row&lt;/h2&gt;

&lt;p&gt;A gate has an artifact and a test you can apply without asking what the instructor meant. The artifact is a calculation with units, a labeled photo, or a paragraph that contains a number. The test is a comparison: inside a tolerance window, the stripe called negative, the chip name readable, the paragraph containing the words “separate motor battery.” A weak tick has the checkmark and is missing the comparison. “Done” with no ohms, “photo taken” of an unlabeled board, and “understood brownout” with no sentence are the three weak ticks this chapter actually produces. They score as open. They do not score as almost.&lt;/p&gt;

&lt;p&gt;The remediation path is short on purpose. Reopen the one lesson named on the gate, repeat only the step that produces the artifact, and date the new file. Do not rewrite the whole chapter, and do not start Chapter 02 motion while the brownout paragraph or the driver label is still a weak tick. Those two are the ones that destroy parts or masquerade as software. A missing iron is the exception: you may schedule a make-up, and the schedule is the artifact until the joint exists.&lt;/p&gt;

&lt;h2 id=&quot;the-seven-gates&quot;&gt;The seven gates&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. LED calculation at 3.3 V.&lt;/strong&gt; Write the arithmetic for the status LED that will hang on a GPIO. Pick a forward drop and show it. For a red LED, \(V_F = 2.0\,\mathrm{V}\) is fair if lesson 10 did not measure one. With a 220 Ω resistor,&lt;/p&gt;

\[I = \frac{3.3 - 2.0}{220} = \frac{1.3}{220} \approx 5.9\,\mathrm{mA}\]

&lt;p&gt;Pass: the expression, the substitution, the result in milliamperes, and one sentence that this current is in the 5–10 mA band a small LED and a 3.3 V GPIO can live with. Using your measured \(V_F\) from lesson 10 is better, if you show it. Dividing 3.3 by the resistor and forgetting \(V_F\) fails, however neat the handwriting. A result near zero for a blue LED passes only if you then say the LED will be dim and you choose a different color or a smaller resistor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Breadboard rail map.&lt;/strong&gt; A photo or a sketch from lesson 10 that shows a five-hole node, the open trench, and whether each supply rail is continuous or split. Pass: another person could place a jumper using only that sheet and land on the node you meant. An unlabeled photo of an empty board fails.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Five resistors, decoded and measured.&lt;/strong&gt; Five rows, each with colors, nominal, tolerance, and an out-of-circuit measurement, marked pass or fail against the window. A color code with no meter reading fails. A meter reading with no colors fails. A value taken while the resistor was still in a circuit fails. Four good rows and a blank fifth is not “mostly.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. One polarity call in diode mode.&lt;/strong&gt; Either the LED or the diode, with a forward voltage and a sentence of the form “this lead is the anode.” Pass: a stranger could connect that LED from 3.3 V through the resistor in gate 1, anode toward the GPIO, cathode toward ground, without asking you. A photo with no words fails. Calling the stripe on an electrolytic “probably negative” does not satisfy this gate; this gate is the diode-mode call. The electrolytic stripe still belongs in your notes from lesson 10, and lesson 09 will not accept a backwards VM capacitor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Driver photo, named.&lt;/strong&gt; A photo of the motor-driver module on which you have written TB6612 or DRV8833, plus the pin names from that chip’s own page: for a TB6612, at least AIN1, AIN2, PWMA, and STBY. “H-bridge,” “motor driver,” or “the small board” fails even if the picture is sharp. If the module has not arrived, the row is a dated dry-run that names which chip you ordered. A rectangle with no chip name is a weak tick.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Solder joint, or a make-up.&lt;/strong&gt; A photo of your joint from lesson 08, continuity noted, compared with the good fillet in the joint figure. A beep plus a ball fails. A pretty open joint fails. If you have no iron yet, write the date you will do the lab and the stand-and-glasses line you will follow. That schedule is an honest delay. A photo of a factory joint on a bought module, claimed as yours, is not a delay. It is a fail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Brownout paragraph.&lt;/strong&gt; The note from lesson 09, containing your USB-only 5 V and 3.3 V readings if you could take them, the hypothesis that a later reset is the motors sagging a shared rail, and the fix: separate motor battery, common ground only, 100–470 µF on VM, thick wires, wheels up, PWM to 0 if the rail sags or commands stop for about 300 ms. A paragraph that powers the TT motors from the ESP32 5 V pin fails even if the grammar is tidy. The HC-SR04 echo forbid, 5 V into a 3.3 V GPIO, may sit in the same note; its absence does not by itself fail this gate, and its presence does not excuse a missing battery sentence.&lt;/p&gt;

&lt;h2 id=&quot;what-a-weak-tick-looks-like&quot;&gt;What a weak tick looks like&lt;/h2&gt;

&lt;p&gt;Compare these with the gates. “Resistors: yes” fails gate 3, because nothing was decoded and nothing was measured. “Polarity OK” fails gate 4, because no lead was named the anode and no voltage was written. “Driver: H-bridge from the kit” fails gate 5, because Chapter 02 cannot tell STBY from a sleep pin on a DRV8833. “Soldering understood” fails gate 6, because there is neither a photo nor a date. “Brownout: motors use a lot of current” fails gate 7, because the separate battery, the common ground, and the 300 ms PWM cut are missing. The repair is not a longer adjective. The repair is the artifact the gate already named.&lt;/p&gt;

&lt;p&gt;Remediation stays inside one sitting when the part is on the bench. Gate 3 is a remeasure, not a new theory chapter. Gate 6 with no iron is a purchase from the lesson 08 list and a date on the calendar, not a rewritten definition of a fillet. Gate 5 with the wrong name is ten minutes with the Pololu page or the TI page and a new label. Do not open a motion lab to “catch up.”&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;This lab is the audit. Build a new circuit only to replace a failed artifact.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Open the notes and make seven rows, one per gate.&lt;/li&gt;
  &lt;li&gt;For gate 1, write the 3.3 V LED arithmetic on a fresh line, even if a similar line exists in lesson 01.&lt;/li&gt;
  &lt;li&gt;Attach the rail map, the five-resistor table, and the diode-mode polarity line. If a row fails its test, fix it before you score it.&lt;/li&gt;
  &lt;li&gt;Label the driver photo with the chip name. If the board is absent, write the dry-run and the date you expect it.&lt;/li&gt;
  &lt;li&gt;Re-inspect the joint photo in daylight. If the fillet is a ball, rework it before it scores, or write the make-up date if you still have no iron.&lt;/li&gt;
  &lt;li&gt;Read the brownout paragraph once and underline “separate,” “common ground,” and “300 ms.” If any of those are missing, add them from lesson 09 rather than inventing a softer sentence.&lt;/li&gt;
  &lt;li&gt;Mark each gate pass or fail. Date the page &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-11-audit&lt;/code&gt;. A fail names the lesson you will reopen.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No wheel turns during the audit.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A note says: “LED resistor done. Breadboard photo attached, no marks. Three resistors measured, two looked right. Polarity: stripe is negative I think. Driver is an H-bridge. Solder next week maybe. Brownout: don’t stall the motors.”&lt;/p&gt;

&lt;p&gt;Gate 1 fails, or is at best incomplete, because no arithmetic is on the page. “Done” is the weak tick. Gate 2 fails: an unmarked breadboard photo is not a rail map. Gate 3 fails: three measurements are not five, and “looked right” is not a decode plus a meter reading. Gate 4 fails: a guess about an electrolytic stripe is not a diode-mode anode call. Gate 5 fails: “H-bridge” is the forbidden label. Gate 6 is not yet a pass; “next week maybe” becomes a pass of the make-up rule only if a real date replaces “maybe.” Gate 7 fails: there is no separate battery, no common ground, no capacitor, and no 300 ms cut. The note does not scrape a pass by being sincere. The next hour is gates 1, 3, and 4 at the bench, plus a rewritten paragraph for gate 7. Gates 2 and 5 need a pencil on a photo. Gate 6 needs a date.&lt;/p&gt;

&lt;h2 id=&quot;explanation-executing-a-4-stage-power-rail-bring-up-check&quot;&gt;Explanation: Executing a 4-Stage Power-Rail Bring-Up Check&lt;/h2&gt;

&lt;p&gt;Follow this exact sequence before connecting any MCU board in Chapter 02:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Cold continuity &amp;amp; polarity check (Power OFF):&lt;/strong&gt; Measure &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBAT&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; in resistance mode; verify &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt; 1 kΩ&lt;/code&gt; (note: bulk capacitors briefly charge and climb upward — wait 2 seconds for the reading to stabilize).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Unloaded buck converter calibration (Power ON, MCU disconnected):&lt;/strong&gt; Power the buck from 2S battery and adjust the potentiometer until the output reads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5.00 V ± 0.05 V&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Loaded rail regulation test:&lt;/strong&gt; Attach a known dummy load (e.g., a \(22\,\Omega\), 2 W power resistor drawing \(5.0 / 22 \approx 0.227\text{ A}\)) and confirm the 5 V rail droops by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt; 0.05 V&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Signal-interface verification:&lt;/strong&gt; Test your 5 V → 3.3 V voltage divider under DC input to confirm &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V_out ≤ 3.30 V&lt;/code&gt; before attaching any sensor.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-computing-load-regulation-percentage-on-your-5-v-buck&quot;&gt;Concrete Worked Example: Computing Load Regulation Percentage on Your 5 V Buck&lt;/h3&gt;

&lt;p&gt;Suppose your adjustable buck converter measures \(V_{\text{no-load}} = 5.04\text{ V}\) unloaded and \(V_{\text{full-load}} = 4.98\text{ V}\) when supplying \(I = 0.50\text{ A}\) to your logic subsystem.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Load regulation percentage:&lt;/strong&gt;
\(\text{Load Reg (\%)} = \frac{V_{\text{no-load}} - V_{\text{full-load}}}{V_{\text{full-load}}} \times 100\% = \frac{5.04 - 4.98}{4.98} \times 100\% \approx 1.20\%.\)
(Anything under \(2\text{–}3\%\) is excellent for a robot logic rail.)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Effective output impedance of the regulator + wiring:&lt;/strong&gt;
\(R_{\text{out}} = \frac{\Delta V}{\Delta I} = \frac{0.06\text{ V}}{0.50\text{ A}} = 0.12\,\Omega.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Extrapolated voltage at \(1.0\text{ A}\) peak:&lt;/strong&gt;
\(V(1.0\text{ A}) \approx 5.04 - 1.0 \times 0.12 = 4.92\text{ V}\) — still comfortably inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4.75–5.25 V&lt;/code&gt; USB/logic spec!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Adjusting a cheap buck converter while the MCU is already wired to its output — a single slip of the screwdriver can spike the output to full battery voltage (8.4 V) and destroy the board instantly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100 µF&lt;/code&gt; capacitor is connected across &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, your continuity beeper may chirp for 0.1 seconds and then go silent as the meter charges the capacitor — that brief chirp is normal, whereas a continuous beep indicates a real short.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;p&gt;Each item is a fragment from a sloppy note. Say which gate fails, what is missing, and the smallest repair.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;”\(3.3 / 180 = 18\,\mathrm{mA}\), LED will be bright.” Which gate, and what term did the arithmetic drop?&lt;/li&gt;
  &lt;li&gt;“Five resistors: 220, 1k, 10k, 330, 470. All good.” Which gate, and which half of the evidence is absent?&lt;/li&gt;
  &lt;li&gt;“Diode mode: it beeped, so polarity is fine.” Which gate, and why is a beep the wrong function?&lt;/li&gt;
  &lt;li&gt;“Module photo saved as driver.jpg. It is the H-bridge from Shopee, pins wired like the video.” Which gate fails, and what two words would have passed the label?&lt;/li&gt;
  &lt;li&gt;“Joint looks okay, no photo, iron is at the shop until Friday 3 pm. Brownout paragraph says both TT motors share the ESP32 5 V pin so the wiring stays simple, echo goes straight to GPIO 4.” Which gates fail, which gate can be a scheduled make-up, and which sentence is unsafe rather than merely incomplete?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
  &lt;summary&gt;Hints and answers&lt;/summary&gt;

  &lt;ol&gt;
    &lt;li&gt;Gate 1 fails. The LED forward drop was omitted, so 18 mA is not the current in the resistor. Rewrite as \(I = (3.3 - V_F) / 180\) with a stated \(V_F\). At \(V_F = 2.0\,\mathrm{V}\) the current is about \(7.2\,\mathrm{mA}\), which is a believable pass.&lt;/li&gt;
    &lt;li&gt;Gate 3 fails. The nominal values are there and the measurements are not. “All good” does not replace five ohm readings taken out of circuit.&lt;/li&gt;
    &lt;li&gt;Gate 4 fails. Continuity beep is not diode mode. The repair is a forward voltage and a named anode. A beep only says the leads are not open.&lt;/li&gt;
    &lt;li&gt;Gate 5 fails. The passing labels are TB6612 or DRV8833, plus that chip’s own pin names. “H-bridge” and “like the video” do not identify STBY or nSLEEP.&lt;/li&gt;
    &lt;li&gt;Gate 6 may be the make-up if “Friday 3 pm” is written as the date and the joint photo is still required then. Gate 7 fails, and the sentence is unsafe: TT motors do not share the ESP32 5 V pin, and echo does not land on GPIO 4. Repair the paragraph from lesson 09 and the echo forbid from lesson 07. Do not treat that sentence as a near miss.&lt;/li&gt;
  &lt;/ol&gt;

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG carrier&lt;/a&gt; — the label and the pin names gate 5 needs when the chip is a TB6612.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ti.com/product/DRV8833&quot;&gt;TI DRV8833&lt;/a&gt; — the other legal label. Do not mix its pin names with the TB6612 card.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;ESP32 datasheet (PDF)&lt;/a&gt; — the 3.3 V limit behind gates 1 and 7.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.adafruit.com/adafruit-guide-excellent-soldering&quot;&gt;Adafruit soldering guide&lt;/a&gt; — the fillet standard for gate 6, next to this course’s joint figure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Do not build a second kit. Compare the open gates with Chapter 00, lesson 05, &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;, and buy only the gaps.&lt;/p&gt;

&lt;p&gt;Gate 6, if the iron is the gap, uses the lesson 08 pages and the 23 September 2026 prices: the &lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;60 W iron&lt;/a&gt; at 75,000₫, the &lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;stand&lt;/a&gt; at 40,000₫, and the &lt;a href=&quot;https://hshop.vn/thiec-han-sunchi-kp26-0-8mm-sn63-pb37-solder-wire&quot;&gt;0.8 mm Sn63 reel&lt;/a&gt; at 24,000₫. Gate 2, if you have no breadboard, is the &lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;CAMMB-102&lt;/a&gt; at 35,000₫. A missing meter, for gates 3 and 4, is the &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UNI-T UT33D&lt;/a&gt;. Shop fronts for a single resistor or LED, without a new slug invented here: &lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt;, &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đầy Rồi&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Lab — continuity, ohms, and lookalikes</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_10_component_id_lab/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_10_component_id_lab</id>
   <content type="html">&lt;p&gt;Open any unlabeled parts bin and you will meet identical-looking black 3-pin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TO-92&lt;/code&gt; packages that could be an NPN transistor (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2N2222&lt;/code&gt;), a PNP transistor (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2N3906&lt;/code&gt;), a 3.3 V regulator, or a Hall-effect sensor — and identical glass diodes that could be fast signal diodes (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1N4148&lt;/code&gt;) or Zener voltage references. Guessing wastes boards; a 30-second multimeter test identifies them with certainty.&lt;/p&gt;

&lt;p&gt;This hands-on lab turns component identification into a fast, repeatable measurement protocol.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;This hour is a bench session. The earlier lessons handed you a color code, a diode band, and an electrolytic stripe one part at a time. Today those parts share one unpowered breadboard, and the grade is a table of numbers you measured. A photograph of a parts pile is not a measurement. A color code you did not check with the meter is a guess you will later solder into the cart.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will map which breadboard rows beep and which gap stays open, and you will photograph that map. You will decode five resistors and measure each one out of circuit, then keep or reject it against its tolerance. You will use diode mode on one LED and one diode, record the forward reading, and write which lead is the anode. You will separate three lookalike pairs in writing: a color-banded resistor and a color-banded inductor, a ceramic capacitor and a tantalum capacitor, a TO-92 transistor and a TO-92 regulator. You will name three measurement faults before they get into the table: ohms measured in-circuit, diode-mode probes that your particular meter has swapped, and a 5-band resistor read as 4-band.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 02 through 05 are the background: breadboard rows, the meter, resistor codes, capacitors, diodes, and LEDs. You need continuity, ohms, and diode mode. You do not need the iron, and you do not need the TB6612 powered. If a part is missing, write the step and mark the row dry-run. Do not borrow a classmate’s numbers.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-diode-test-mode-continuity-thresholds-and-lookalike-disambiguation&quot;&gt;Key ideas: Diode-Test Mode, Continuity Thresholds, and Lookalike Disambiguation&lt;/h2&gt;

&lt;p&gt;Three measurement principles let you identify unknown parts without guessing:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Diode-Test Mode Physics:&lt;/strong&gt; Unlike ohmmeter mode, diode mode forces a constant ~1 mA test current and displays the actual forward voltage drop \(V_f\) directly in volts: \(\approx 0.6\text{–}0.7\text{ V}\) for silicon PN junctions, \(\approx 0.2\text{–}0.35\text{ V}\) for Schottky diodes, \(1.8\text{–}3.0\text{ V}\) for LEDs, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; (open loop) in reverse bias.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;BJT Dual-Diode Model:&lt;/strong&gt; An NPN transistor behaves in diode mode like two diodes sharing a common anode at the Base (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B→E&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B→C&lt;/code&gt; both read \(\approx 0.65\text{ V}\), while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;E↔C&lt;/code&gt; reads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt;). A PNP transistor has a common cathode at the Base (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;E→B&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C→B&lt;/code&gt; read \(\approx 0.65\text{ V}\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tolerance &amp;amp; Verification Against Printed Markings:&lt;/strong&gt; Measuring passive components before soldering catches mislabeled bags and builds intuition for how real parts sit inside their \(\pm 1\%\) or \(\pm 5\%\) bands.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;The diff-drive cart is still a 3.3 V ESP32 or Pico, a TB6612, two TT motors on their own battery, and an HC-SR04 whose echo is 5 V. None of those modules excuse a wrong part in the harness around them. The status LED on a GPIO needs a resistor you measured, not a band you misread by a decade. The bulk capacitor on VM, 100–470 µF from lesson 09, is an aluminum electrolytic with a negative stripe; a tantalum bead uses a stripe that often means the opposite, and swapping that rule vents a part on the motor rail. A TO-92 regulator dropped into a transistor footprint, or the reverse, is how a 3.3 V rail dies while the motors are still innocent. The table from this lab is the evidence that you can tell those parts apart before any of them is buried in heat-shrink.&lt;/p&gt;

&lt;h2 id=&quot;what-is-on-the-bench&quot;&gt;What is on the bench&lt;/h2&gt;

&lt;p&gt;For this hour: one meter, one breadboard, five resistors, one aluminum electrolytic, one ceramic capacitor, one LED, one diode, and a handful of jumpers. Nothing else is required, and nothing is powered. The ceramic is the disc or the little block with no polarity mark. The electrolytic is the can. The diode is whatever number is printed on the part you actually have; write that number, and do not relabel a 1N4001 as a 1N4007 because the exercise said “diode.”&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/resistor_color_code.png&quot; alt=&quot;Color bands you will check, not trust&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Keep the chart beside the five resistors while you fill the nominal column. The meter is allowed to disagree. When it does, you reread the bands, including the possibility that you treated five bands as four. You do not edit the meter to match the chart.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/component_lookalikes.png&quot; alt=&quot;Bodies that match and insides that do not&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This figure is why the lookalike list exists. Same outline, different part. Your list names the pair, the test, and the result. “They look different to me” is not a test.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/resistors_assortment.jpg&quot; alt=&quot;An assortment, before any of it is in a circuit&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Pick five values you can decode, including at least one that is not 220 Ω and not 10 kΩ, so the hour cannot be passed from memory.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/capacitor_polarity.png&quot; alt=&quot;Which lead of the capacitor is negative&quot; /&gt;&lt;/p&gt;

&lt;p&gt;On the aluminum can in this kit, the stripe is negative. You will point at it. You will not apply that sentence to a tantalum bead.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/LEDs.jpg&quot; alt=&quot;LED polarity is a meter question if the leads have been trimmed&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The long lead is usually the anode, and the flat on the rim is usually the cathode. Trimmed leads and cheap lenses break the habit. Diode mode is the check you record.&lt;/p&gt;

&lt;h2 id=&quot;procedure&quot;&gt;Procedure&lt;/h2&gt;

&lt;p&gt;Power stays off for the whole hour. No USB, no battery, no motor.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Map the rows that beep.&lt;/strong&gt; Continuity mode. Beep a group of five holes in the center and confirm they are one node. Beep across the center trench and confirm it is open. Beep each supply rail from end to end. If the beep stops halfway, the rail is split; mark the break on a sketch. This sketch is part of the grade. A later “the LED is dead” includes the possibility that you were never on one node.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Decode, then measure, five resistors.&lt;/strong&gt; For each, write the colors, the nominal ohms, and the tolerance. Measure out of circuit, legs not pinched in your fingers if the value is large, because skin is a resistor too. A 5% part passes only inside&lt;/li&gt;
&lt;/ol&gt;

\[R_{\mathrm{nom}}(1 - 0.05) \le R_{\mathrm{meas}} \le R_{\mathrm{nom}}(1 + 0.05)\]

&lt;p&gt;Gold as the last band is 5%. If the code and the meter disagree, the meter starts a second look at the bands. A part outside the window is set aside. It does not go back into the known-good bag. Short the meter leads once and write that offset, often a few tenths of an ohm. It matters for a 10 Ω part and it does not matter for a 10 kΩ part.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Diode mode on the LED and on the diode.&lt;/strong&gt; Record the forward reading and which physical lead was on the red probe when you got it. That lead is the anode for this measurement, after you have confirmed how your meter is wired. Also record the open or overload reading with the probes swapped, and the feature you can see: flat, short lead, or cathode band. A red LED often shows roughly 1.6–2.2 V and may glow faintly. A silicon diode often shows roughly 0.5–0.8 V. “OL” in both directions on an LED can mean a dead part or a meter whose diode voltage cannot light that color. Say which you believe, and why.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Lookalike list, three pairs.&lt;/strong&gt; Write them into the notes even if you only have one side of the pair on the bench. Say what you would measure.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A resistor and a small inductor can wear the same color bands. The resistor measures near its code. The inductor, a coil of wire, measures near a short, a few ohms or less, far below a 1 kΩ or 10 kΩ code. Do not install the coil as the LED’s series resistor. You will current-limit nothing and you may cook the LED or the GPIO.&lt;/p&gt;

&lt;p&gt;A ceramic disc has no polarity. A tantalum capacitor is polarized, and the stripe or bar on many tantalum beads marks the positive lead, which is the opposite of the aluminum can in the polarity figure. If you are not sure which family a bead belongs to, it does not go across VM. The aluminum rule and the tantalum rule are not interchangeable.&lt;/p&gt;

&lt;p&gt;A TO-92 transistor and a TO-92 regulator share the half-cylinder body. The marking decides. A code such as 78L05 is a regulator, with a pinout in the family of input, ground, and output. A code such as S8050 or BC547 is a transistor, with some order of emitter, base, and collector that you look up rather than guess. A rubbed-off marking goes to the unknown bag. It does not power the ESP32.&lt;/p&gt;

&lt;h2 id=&quot;faults-that-manufacture-bad-numbers&quot;&gt;Faults that manufacture bad numbers&lt;/h2&gt;

&lt;p&gt;Measuring ohms in-circuit reads the network, not the part. A 220 Ω resistor beside other parts can show 80 Ω and still be a good 220 Ω part. Lift one leg. If you cannot, you are not measuring that resistor.&lt;/p&gt;

&lt;p&gt;Some meters swap the sense of diode mode, or a probe has been parked in the wrong jack since yesterday. The red lead is positive on most digitals and not on all of them. Prove yours once: the lead that produces the forward reading on a known diode is the anode side of that diode. Write “red = anode source” or “red is swapped” at the top of the table. A reversed assumption labels every cathode in the notebook backwards, including the LED you will hang on a 3.3 V pin.&lt;/p&gt;

&lt;p&gt;Reading a 5-band resistor as a 4-band part moves the multiplier. Five bands are three digits, then multiplier, then tolerance. Four bands are two digits, then multiplier, then tolerance. Brown-black-black-red-brown is not the same recipe as brown-black-black-gold. When the meter disagrees by about ten or a hundred, recount the bands before you condemn the part.&lt;/p&gt;

&lt;h2 id=&quot;the-table-the-notes-must-contain&quot;&gt;The table the notes must contain&lt;/h2&gt;

&lt;p&gt;Copy this and fill it. Expected ranges are there so a blank “ok” cannot pass.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;#&lt;/th&gt;
      &lt;th&gt;Part&lt;/th&gt;
      &lt;th&gt;What you read&lt;/th&gt;
      &lt;th&gt;Nominal or polarity&lt;/th&gt;
      &lt;th&gt;Measured&lt;/th&gt;
      &lt;th&gt;Pass?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;five-hole row&lt;/td&gt;
      &lt;td&gt;which holes&lt;/td&gt;
      &lt;td&gt;one node&lt;/td&gt;
      &lt;td&gt;beep or open&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;center trench&lt;/td&gt;
      &lt;td&gt;across the gap&lt;/td&gt;
      &lt;td&gt;open&lt;/td&gt;
      &lt;td&gt;beep or open&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;red rail&lt;/td&gt;
      &lt;td&gt;end to end&lt;/td&gt;
      &lt;td&gt;continuous or split&lt;/td&gt;
      &lt;td&gt;where it stops&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4–8&lt;/td&gt;
      &lt;td&gt;five resistors&lt;/td&gt;
      &lt;td&gt;colors&lt;/td&gt;
      &lt;td&gt;Ω and %&lt;/td&gt;
      &lt;td&gt;Ω&lt;/td&gt;
      &lt;td&gt;yes/no&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt;LED&lt;/td&gt;
      &lt;td&gt;anode lead&lt;/td&gt;
      &lt;td&gt;long lead or not&lt;/td&gt;
      &lt;td&gt;forward V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;diode&lt;/td&gt;
      &lt;td&gt;anode lead&lt;/td&gt;
      &lt;td&gt;band is cathode&lt;/td&gt;
      &lt;td&gt;forward V&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;11&lt;/td&gt;
      &lt;td&gt;electrolytic&lt;/td&gt;
      &lt;td&gt;stripe&lt;/td&gt;
      &lt;td&gt;negative&lt;/td&gt;
      &lt;td&gt;photo name&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A resistor row passes only with both a decoded value and a measured value. An LED row passes only with a number and the word anode attached to a lead. File it as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-10-id-table&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Bands brown, black, red, gold. That is 1, 0, two zeros, 5%, so \(1.0\,\mathrm{k}\Omega \pm 5\%\).&lt;/p&gt;

\[1000 \times 0.95 = 950,\qquad 1000 \times 1.05 = 1050\]

&lt;p&gt;The meter shows 1027 Ω. The row passes. A second part, coded 220 Ω, is still sitting in a breadboard next to an LED circuit and the meter shows 86 Ω. That reading is discarded. One leg is lifted. The new reading is 218 Ω, inside \(220 \times 0.95\) to \(220 \times 1.05\), and the row passes. The 86 Ω was the network.&lt;/p&gt;

&lt;p&gt;A third part has five bands and was first written down as if it had four, producing 1000 Ω on paper and about 100 Ω on the meter. Recounting as three digits plus a multiplier removes the decade error. The part was never bad.&lt;/p&gt;

&lt;p&gt;On the diode, the banded end is the cathode, so the anode is the other lead. Red probe on the anode, black on the band, the display shows 0.62 V. Probes swapped, the display shows OL. On the red LED, red on the long lead shows 1.85 V and a faint glow, so that long lead is the anode today. Those two voltages in one table are how you prove the parts were not swapped: about 0.6 V is the diode, about 1.8 V is the red LED.&lt;/p&gt;

&lt;h2 id=&quot;explanation-identifying-an-unknown-3-pin-transistor-in-6-probe-touches&quot;&gt;Explanation: Identifying an Unknown 3-Pin Transistor in 6 Probe Touches&lt;/h2&gt;

&lt;p&gt;Suppose you pick up an unmarked &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TO-92&lt;/code&gt; transistor with pins labeled &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt; from left to right. Walk through this systematic 4-step diode-mode test:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Find the Base pin:&lt;/strong&gt; Place the red probe on pin 1 and touch black to pins 2 and 3; repeat with red on pin 2, then red on pin 3. If one red-probe position produces \(\approx 0.65\text{ V}\) to &lt;em&gt;both&lt;/em&gt; other pins, that pin is the &lt;strong&gt;Base&lt;/strong&gt; of an &lt;strong&gt;NPN&lt;/strong&gt; transistor! (If no red position works, repeat holding the &lt;em&gt;black&lt;/em&gt; probe fixed to find a &lt;strong&gt;PNP&lt;/strong&gt; Base.)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Distinguish Emitter from Collector:&lt;/strong&gt; Between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B→E&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B→C&lt;/code&gt;, the emitter junction is more heavily doped, so \(V_{\text{BE}}\) is consistently &lt;strong&gt;5–15 mV higher&lt;/strong&gt; than \(V_{\text{BC}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Confirm no Collector–Emitter short:&lt;/strong&gt; Both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C→E&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;E→C&lt;/code&gt; must read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; (if either reads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.00 V&lt;/code&gt;, the transistor is shorted and dead).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify continuity vs. low resistance:&lt;/strong&gt; Remember that a continuity beeper sounds for &lt;em&gt;anything&lt;/em&gt; below \(\approx 30\text{–}50\,\Omega\) — including a healthy \(10\,\Omega\) motor winding! Always read the numeric ohm value.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-interpreting-a-real-bench-diode-mode-table&quot;&gt;Concrete Worked Example: Interpreting a Real Bench Diode-Mode Table&lt;/h3&gt;

&lt;p&gt;You probe an unknown &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TO-92&lt;/code&gt; part and record the following 6 readings in diode mode:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Red Probe (+)&lt;/th&gt;
      &lt;th&gt;Black Probe (–)&lt;/th&gt;
      &lt;th&gt;Meter Reading&lt;/th&gt;
      &lt;th&gt;Interpretation&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin 2&lt;/td&gt;
      &lt;td&gt;Pin 1&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.688 V&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Forward PN junction (higher \(V_f \implies\) Emitter)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin 2&lt;/td&gt;
      &lt;td&gt;Pin 3&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.679 V&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Forward PN junction (lower \(V_f \implies\) Collector)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin 1&lt;/td&gt;
      &lt;td&gt;Pin 2&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Reverse-biased junction&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin 3&lt;/td&gt;
      &lt;td&gt;Pin 2&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Reverse-biased junction&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin 1&lt;/td&gt;
      &lt;td&gt;Pin 3&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Open C–E channel (healthy)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Pin 3&lt;/td&gt;
      &lt;td&gt;Pin 1&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Open E–C channel (healthy)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Conclusion: Pin 2 is &lt;strong&gt;Base&lt;/strong&gt;, the transistor is &lt;strong&gt;NPN&lt;/strong&gt;, Pin 1 is &lt;strong&gt;Emitter&lt;/strong&gt; (\(0.688\text{ V} &amp;gt; 0.679\text{ V}\)), and Pin 3 is &lt;strong&gt;Collector&lt;/strong&gt; — matching the standard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;E-B-C&lt;/code&gt; pinout of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2N2222A&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Testing a MOSFET in diode mode after accidentally touching the gate pin with your fingers — static charge on the gate can latch the MOSFET ON so Drain-to-Source looks shorted (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.00 V&lt;/code&gt;). Short Gate to Source momentarily before retesting!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; A healthy silicon diode reads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.62 V&lt;/code&gt; one way and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; the other; a shorted diode reads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.00 V&lt;/code&gt; both ways; an open (blown) diode reads &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OL&lt;/code&gt; both ways.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A 470 Ω, gold-band resistor measures 502 Ω. Compute the 5% window and give the verdict.&lt;/li&gt;
  &lt;li&gt;Diode mode shows OL with red on the long LED lead and 1.9 V with red on the short lead. Your meter was already proven to put positive on red. Which lead is the anode, and what did OL mean?&lt;/li&gt;
  &lt;li&gt;A 1 kΩ resistor in an unpowered board reads 120 Ω. What is the first correction, and why is “the code is wrong” not that correction?&lt;/li&gt;
  &lt;li&gt;An unmarked banded part coded like 10 kΩ measures 1.2 Ω. What is the more likely identity, and may it be the series resistor for a 3.3 V LED?&lt;/li&gt;
  &lt;li&gt;A TO-92 part’s marking is gone. Write the lookalike entry, and say whether it may feed the ESP32.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
  &lt;summary&gt;Hints and answers&lt;/summary&gt;

  &lt;ol&gt;
    &lt;li&gt;The window is \(470 \times 0.95 = 446.5\,\Omega\) to \(470 \times 1.05 = 493.5\,\Omega\). 502 Ω is outside. Fail, and set it aside.&lt;/li&gt;
    &lt;li&gt;The anode is the short lead, because that is where a positive red probe produced 1.9 V. OL was the reverse direction, not automatically a dead LED. The long-lead habit failed, which is why the meter is in the procedure.&lt;/li&gt;
    &lt;li&gt;Lift one leg and measure again. Parallel paths read low. Judge the color code only after the part is alone. The board was unpowered, which is required and still not enough.&lt;/li&gt;
    &lt;li&gt;A steady reading near a short, against a 10 kΩ code, is the inductor lookalike (or a destroyed part). It must not be the LED resistor. A real 10 kΩ part would measure near 10 kΩ.&lt;/li&gt;
    &lt;li&gt;“TO-92, marking illegible, transistor or regulator unknown, pinout unknown.” It may not feed the ESP32. Unknown function plus unknown pinout is how rails get shorted.&lt;/li&gt;
  &lt;/ol&gt;

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-use-a-multimeter&quot;&gt;SparkFun: how to use a multimeter&lt;/a&gt; — continuity, ohms, and diode mode, which are the only three functions this lab allows.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/resistors/resistor-color-code&quot;&gt;SparkFun: resistor color codes&lt;/a&gt; — 4-band and 5-band on one page, for the recount in step 2.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;ESP32 datasheet (PDF)&lt;/a&gt; — the 3.3 V rail your LED resistor must respect, from lesson 07.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;If the Chapter 00 kit is already on the bench, buy nothing. The list is lesson 05, &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Two pages are enough when something on the BOM above is missing. The breadboard, 35,000₫ on 23 September 2026, is &lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;Hshop test board CAMMB-102&lt;/a&gt;. The meter, live price on the page, is the &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UNI-T UT33D at Hshop&lt;/a&gt;. Loose resistors, one LED, one diode, and one electrolytic can come from the shop fronts &lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt;, and &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đầy Rồi&lt;/a&gt;. This lesson does not invent slugs for them. Write the number printed on the diode you received.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>ESD, polarity, fuses, and brownouts</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_09_esd_polarity_brownout/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_09_esd_polarity_brownout</id>
   <content type="html">&lt;p&gt;A microcontroller rarely dies with a dramatic movie explosion. More often, it suffers one of three silent killers: an invisible 4,000-volt static spark from a carpet (&lt;em&gt;ESD&lt;/em&gt;), a half-second reversed battery plug (&lt;em&gt;reverse polarity&lt;/em&gt;), or a voltage dip when both motors start from rest (&lt;em&gt;brownout&lt;/em&gt;). The first two can permanently cripple silicon; the third causes maddening “random” resets that look like software bugs until you measure the supply rail.&lt;/p&gt;

&lt;p&gt;This lesson equips you with the circuit-level armor — fuses, reverse-polarity protection, bulk decoupling, and ESD discipline — that turns a fragile prototype into a field-ready robot.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Three accidents get blamed on “random” firmware. A static zap wounds a pin before you flash anything. A backwards battery destroys a driver as the connector clicks. A motor stall sags the 5 V rail, the ESP32 reboots, and the log looks haunted. This lesson separates the three and ends with a hypothesis you will test when the motors exist.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will handle a board by the edges after touching grounded metal, without turning ESD into theatre and without shrugging it off. You will state how reverse polarity kills an electrolytic and a TB6612 VM pin, and you will check polarity with a meter before the first power-up rather than trusting wire color. You will size a polyfuse or a 2–3 A fuse so it sits above the light running current of two TT motors and below a current that smokes the battery lead. You will explain brownout as the shared 5 V rail collapsing under stall, and you will write the fix: a separate motor battery, common ground only, a 100–470 µF capacitor on VM with the stripe correct, thick motor wires, wheels up, and firmware that forces PWM to 0 if the supply sags or if commands stop for about 300 ms.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 04’s electrolytic stripe and lesson 07’s split between VM and VCC are assumed. Ohm’s law from lesson 01 is the only arithmetic. You will meter a USB rail today. You will not spin a wheel, and you will not connect a motor battery.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-electrostatic-discharge-reverse-polarity-protection-and-brownout-physics&quot;&gt;Key ideas: Electrostatic Discharge, Reverse-Polarity Protection, and Brownout Physics&lt;/h2&gt;

&lt;p&gt;Three electrical protection mechanisms safeguard every embedded robot controller:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Electrostatic Discharge (ESD):&lt;/strong&gt; A human body acts like a \(C_{\text{HBM}} \approx 100\text{ pF}\) capacitor charged to several kilovolts. Discharging into an exposed CMOS gate oxide (nanometers thick) punctures the insulator even when you feel no shock. Grounding yourself and holding boards by their edges prevents latent failures.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reverse-Polarity Protection:&lt;/strong&gt; A series Schottky diode blocks reverse current cleanly at the cost of a \(0.3\text{–}0.5\text{ V}\) drop; a P-channel MOSFET reverse-polarity circuit achieves the same protection with milliohm loss (\(I^2 R_{\text{DS(on)}}\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Brownout Detection (BOD) &amp;amp; Rail Impedance:&lt;/strong&gt; Every battery plus wire harness has an effective Thévenin resistance \(R_{\text{th}}\). When a motor surge \(\Delta I\) hits, the supply sags by \(\Delta V = \Delta I \cdot R_{\text{th}}\). If the MCU rail dips below its brownout threshold (e.g., \(2.43\text{ V}\) on ESP32), hardware BOD forces an immediate reboot.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;The capstone cart is a 3.3 V ESP32 or Pico, a TB6612, two TT motors, and an HC-SR04. The motors will stall against a curb or a hand. If VM is secretly the same 5 V pin that feeds the board, stall current pulls that rail down, the 3.3 V regulator follows, and the chip resets mid-command. That looks like a logic bug. A second version of the same confusion is the HC-SR04 echo pin, a 5 V output aimed at a GPIO that lesson 07 already forbade. Neither fault is fixed by editing the loop. A separate motor battery, joined to the logic supply by ground and by nothing else, is what makes the next failure interpretable.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_rails_3v3.png&quot; alt=&quot;Logic rail beside the motor rail&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Two supplies meet only at ground. The 3.3 V rail feeds the microcontroller and the driver logic pin. The motor rail feeds VM. USB drawn into the motor rail is the brownout in advance. The HC-SR04 may take 5 V on its own supply; echo still does not enter a GPIO.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Order of power, wheels still off the desk&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Logic may come up on USB. The motor chain stays off in this lab. STBY stays inactive until both rails exist and the program is ready.&lt;/p&gt;

&lt;h2 id=&quot;esd-in-proportion&quot;&gt;ESD, in proportion&lt;/h2&gt;

&lt;p&gt;Electrostatic discharge is a spark you often cannot see. A carpet walk and then a finger on a bare Pico pad or a MOSFET gate is enough to puncture oxide. The habit is dull and enough: touch grounded metal, such as a plugged-in computer chassis, then pick the board up by the edges; keep bare boards bagged until you are seated; do not hand a loose board across the room. Skip the balloon theatrics. Do not shrug because the last board survived. A soldered module is tougher than a loose transistor, and the same sequence still applies. A wrist strap is optional, not the pass condition.&lt;/p&gt;

&lt;h2 id=&quot;polarity-now-and-later&quot;&gt;Polarity, now and later&lt;/h2&gt;

&lt;p&gt;Reverse polarity on an aluminum electrolytic heats the can until it vents, and it can take the copper with it. Reverse polarity on VM does the same class of damage inside the driver. The stripe is the negative lead and it goes to ground. Battery positive goes to VM, never to a GPIO, and never “just to see.”&lt;/p&gt;

&lt;p&gt;A series Schottky in the positive lead, or a keyed XT30, is the right later improvement. The Schottky costs a few tenths of a volt at motor current, so it is a decision, not a reflex, and you do not add it today unless it is already in the kit. Today you meter the battery before it touches the driver: red on the wire you believe is positive, black on the other. A positive reading matches that belief. A minus sign means you stop. Two red wires are not a polarity system.&lt;/p&gt;

&lt;p&gt;The HC-SR04 has a polarity that is not a battery. Its VCC is 5 V, trig is an input, and echo is a 5 V output. Swapping sensor VCC and GND is reverse polarity on a small board. Landing echo on a GPIO is the forbid from lesson 07. Check both on the pin card before the sensor is plugged in.&lt;/p&gt;

&lt;h2 id=&quot;the-numbers-that-make-usb-fail-as-a-motor-supply&quot;&gt;The numbers that make USB fail as a motor supply&lt;/h2&gt;

&lt;p&gt;The TT 1:48 listing this course buys quotes about 110–150 mA no-load. Two motors, wheels in the air, draw about&lt;/p&gt;

\[2 \times 130\,\mathrm{mA} \approx 0.26\,\mathrm{A}\]

&lt;p&gt;Call it \(0.3\,\mathrm{A}\). A USB port is often budgeted at 500 mA, so light running looks as if it fits:&lt;/p&gt;

\[0.50 - 0.30 = 0.20\,\mathrm{A}\]

&lt;p&gt;of apparent headroom. Stall is much higher. Do not invent a precise stall ampere you have not measured. Write that stall is several times no-load, already more than a 500 mA port for one TT motor. USB cannot be the motor supply. The TB6612’s 1.2 A continuous rating is a ceiling for the chip, not a source. The source is the battery on VM.&lt;/p&gt;

&lt;h2 id=&quot;brownout-and-the-fix-you-will-actually-build&quot;&gt;Brownout, and the fix you will actually build&lt;/h2&gt;

&lt;p&gt;A shared 5 V rail has resistance in the cable and the connector. Stall current through that resistance is a sag:&lt;/p&gt;

\[\Delta V = I_{\mathrm{stall}} \times R_{\mathrm{path}}\]

&lt;p&gt;You do not need a measured stall current to see the shape. When the 5 V pin droops, the regulator that makes 3.3 V runs out of headroom, the ESP32 resets, and the sketch starts again mid-command. A Pico can brown out the same way. A boot banner where you expected a speed report is this bug.&lt;/p&gt;

&lt;p&gt;The hardware fix, in the order you should design it:&lt;/p&gt;

&lt;p&gt;The motors get their own battery, inside the 4.5–10 V VM window from lesson 07. Logic stays on USB for now. The supplies share one ground wire and nothing else. Do not force that ground to carry motor current along a thin breadboard rail.&lt;/p&gt;

&lt;p&gt;A bulk aluminum electrolytic, 100–470 µF, sits across VM, close to the driver, rated 16 V or 25 V under that 10 V ceiling. The stripe is negative and goes to ground. Reversing it is the vent from the previous section, now on the motor rail. The capacitor covers the sharp edge of a stall so the leads do not have to.&lt;/p&gt;

&lt;p&gt;Motor wires are thicker than signal jumpers. Wheels stay up whenever a motor is connected and you are not deliberately driving. A wheel on the desk is a stall you caused.&lt;/p&gt;

&lt;p&gt;Firmware, when it exists, forces PWM to 0 if the supply sags, and also if commands stop for about 300 ms. That is long enough to ignore one late packet and short enough that a dead sender does not leave the last speed on the wheels. STBY may drop too. Lesson 07 required STBY high for motion; this lesson requires a way back to off.&lt;/p&gt;

&lt;h2 id=&quot;the-fuse&quot;&gt;The fuse&lt;/h2&gt;

&lt;p&gt;A polyfuse or a 2–3 A fuse in the battery lead is cheap insurance. Size it above the light-running current, about \(0.3\,\mathrm{A}\), so a normal drive does not open it, and below the current that smokes the battery wire. A dead short can be tens of amperes; the fuse is for that, not for a gentle stall. A part under \(0.3\,\mathrm{A}\) is simply wrong. The fuse does not replace the separate battery and it does not know polarity. Put it in the positive lead, close to the pack, and still do the meter check.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;USB only. Motors unplugged. Battery disconnected. If a wheel is already on the shaft, leave it in the air.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Touch grounded metal, then pick the board up by the edges. Write that you did.&lt;/li&gt;
  &lt;li&gt;USB and nothing else. DC volts, black on ground. Record the 5 V pin, then the 3.3 V pin. Write the numbers, not the word “fine.”&lt;/li&gt;
  &lt;li&gt;From the lesson 07 card, or a four-pin sketch if the sensor is absent, circle HC-SR04 Echo and write the forbid: 5 V output, not a GPIO.&lt;/li&gt;
  &lt;li&gt;Write the Chapter 02 hypothesis in this shape, with your readings filled in: “On USB alone the board showed &lt;em&gt;__ V on 5 V and __&lt;/em&gt; V on 3.3 V. If it later resets when the TT motors share that 5 V rail, the hypothesis is stall current sagging the rail into brownout. The fix is a separate motor battery, common ground only, 100–470 µF on VM with the stripe to ground, thick motor wires, wheels up, and PWM forced to 0 if the rail sags or if commands stop for about 300 ms. A 2–3 A fuse goes in the battery lead.”&lt;/li&gt;
  &lt;li&gt;Do not connect the motors to test the paragraph. The pass is the two voltages plus that paragraph, filed as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-09-brownout&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the 5 V reading is far from 5 V with only USB attached, stop and say so. A board that is already sick does not need a motor to explain a reset.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Two TT 1:48 motors at mid-band no-load:&lt;/p&gt;

\[I_{\mathrm{run}} = 2 \times 0.130 = 0.26\,\mathrm{A}\]

&lt;p&gt;USB is \(0.50\,\mathrm{A}\). Headroom with the wheels in the air is about \(0.24\,\mathrm{A}\), which is the whole of the false confidence. Stall \(\gg 0.3\,\mathrm{A}\), and one stalled motor is already an unfair request of a 500 mA port. The redraw: USB for the ESP32 or Pico and for TB6612 logic at 3.3 V; a separate pack inside 4.5–10 V on VM; one ground point; 220 µF or 470 µF, stripe on ground; a 2 A or 3 A fuse in the positive lead. The notes already say PWM returns to 0 if commands are quiet for about 300 ms.&lt;/p&gt;

&lt;p&gt;A 0.5 A fuse “so stall cannot happen” sits on the light-running current and opens during an ordinary drive. People then bypass fuses. Choose 2 A or 3 A, and let the timeout handle stall.&lt;/p&gt;

&lt;h2 id=&quot;explanation-predicting-a-brownout-reset-from-battery--wire-impedance&quot;&gt;Explanation: Predicting a Brownout Reset from Battery + Wire Impedance&lt;/h2&gt;

&lt;p&gt;Why does a robot run fine on USB power yet reboot the instant both wheels command 100% forward on battery? Trace the 4-step Thévenin sag calculation:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Sum all upstream series resistances:&lt;/strong&gt; \(R_{\text{th}} = R_{\text{cells}} + R_{\text{switch}} + R_{\text{fuse}} + R_{\text{wires}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Identify peak inrush current \(I_{\text{inrush}}\):&lt;/strong&gt; At zero RPM, a DC motor has zero back-EMF, so startup current briefly equals full stall current \(I_{\text{stall}} = V / R_{\text{armature}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute the sagged battery voltage:&lt;/strong&gt; \(V_{\text{sag}} = V_{\text{oc}} - I_{\text{inrush}} \cdot R_{\text{th}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compare against regulator dropout:&lt;/strong&gt; For a 5 V buck or LDO requiring \(V_{\text{in}} \ge V_{\text{out}} + V_{\text{dropout}}\), any dip below that floor collapses the 5 V and 3.3 V logic rails.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-why-a-nearly-empty-2s-pack-resets-an-ldo-powered-mcu&quot;&gt;Concrete Worked Example: Why a Nearly Empty 2S Pack Resets an LDO-Powered MCU&lt;/h3&gt;

&lt;p&gt;Suppose a depleted 2S Li-ion pack sits at \(V_{\text{oc}} = 6.80\text{ V}\) with total upstream resistance (cells + thin switch + jumper wires) \(R_{\text{th}} = 0.45\,\Omega\). Both motors start simultaneously, drawing \(I_{\text{inrush}} = 3.6\text{ A}\), while an LDO requires \(V_{\text{in,min}} = 5.0 + 1.2 = 6.2\text{ V}\) to hold 5.0 V:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Voltage sag during startup:&lt;/strong&gt;
\(\Delta V = 3.6\text{ A} \times 0.45\,\Omega = 1.62\text{ V}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Loaded battery voltage at the driver/regulator node:&lt;/strong&gt;
\(V_{\text{sag}} = 6.80 - 1.62 = 5.18\text{ V} &amp;lt; 6.20\text{ V}!\)
The 5 V rail drops below 4.0 V, the 3.3 V LDO drops out, and the ESP32 triggers &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Brownout detector was triggered&lt;/code&gt;!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Effect of upgrading to thick 18 AWG wire + low-resistance switch (\(R_{\text{th}} = 0.15\,\Omega\)) and soft-start PWM ramping:&lt;/strong&gt;
With ramped startup limiting \(I_{\text{inrush}} = 1.8\text{ A}\), \(\Delta V = 1.8 \times 0.15 = 0.27\text{ V} \implies V_{\text{sag}} = 6.53\text{ V} &amp;gt; 6.20\text{ V}\) — rock-solid operation!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Disabling the ESP32’s brownout detector in software (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0)&lt;/code&gt;) instead of fixing the power rail — that risks flash corruption and wild motor behavior when the CPU executes under-voltage instructions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; A 100 pF human body charged to 3,000 V stores \(E = \frac{1}{2} C V^2 = 0.5 \times 10^{-10} \times 9\times 10^6 = 0.45\text{ mJ}\) — tiny to your fingers, yet discharged in nanoseconds it delivers peak powers over kilowatts to a microscopic CMOS gate.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Two motors at 150 mA no-load, and a USB port at 500 mA. Compute the light-running sum and the leftover budget. Why does that leftover not mean USB may feed VM once the wheels touch the ground?&lt;/li&gt;
  &lt;li&gt;The 5 V pin reads 4.95 V and the 3.3 V pin reads 3.28 V on USB alone. In Chapter 02 the serial monitor prints the boot banner every time both motors are plugged into that same 5 V pin. Write the one-sentence hypothesis, and name the supply change that tests it.&lt;/li&gt;
  &lt;li&gt;A 470 µF capacitor is on VM with the stripe away from ground. What fails, and which lead must move?&lt;/li&gt;
  &lt;li&gt;Commands stop because the laptop slept, and the last PWM was “both wheels forward.” What must the firmware do within about 300 ms, and why is a fuse of 3 A not a substitute for that line of code?&lt;/li&gt;
  &lt;li&gt;Echo on the HC-SR04 is wired to a Pico GP pin “because the sensor works from 5 V and the Pico has a 5 V pin nearby.” Which lesson 07 number forbids it, and which of today’s checks would have caught the swap of sensor VCC and GND?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
  &lt;summary&gt;Hints and answers&lt;/summary&gt;

  &lt;ol&gt;
    &lt;li&gt;\(2 \times 0.150 = 0.30\,\mathrm{A}\), so about \(0.20\,\mathrm{A}\) remains on a 500 mA port. Stall is much higher than no-load and does not fit in that leftover. USB stays off VM.&lt;/li&gt;
    &lt;li&gt;The hypothesis is that motor current sagged the shared 5 V rail and the 3.3 V rail followed it into brownout. The test is to move the motors to a separate battery, keep one common ground, and see whether the boot banner stops appearing on the same command.&lt;/li&gt;
    &lt;li&gt;A reversed aluminum electrolytic on a battery can vent. The stripe is negative and belongs on ground. Move that lead, and do not power the rail to “confirm” the vent.&lt;/li&gt;
    &lt;li&gt;Firmware sets PWM to 0, and may release STBY, about 300 ms after commands stop. A 3 A fuse will not open at ordinary motor current, so the wheels keep the last command until something else stops them.&lt;/li&gt;
    &lt;li&gt;The GPIO absolute maximum, near the 3.3 V rail and not 5 V, forbids echo on the GP pin. A meter check of sensor VCC against GND, before plugging in, catches a reversed sensor supply. The nearby 5 V pin is a supply, not a logic input.&lt;/li&gt;
  &lt;/ol&gt;

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;SparkFun logic levels&lt;/a&gt; — why a 5 V echo and a 3.3 V GPIO are not the same “high,” which is the datasheet fence from lesson 07 in plain language.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://batteryuniversity.com/article/bu-409-charging-lithium-ion&quot;&gt;Battery University, BU-409&lt;/a&gt; — charging limits for the lithium pack you may later put on VM. Read it before you invent a charger.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG carrier&lt;/a&gt; — logic supply and motor supply as separate pins, which is the hardware half of the brownout fix.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The motor battery, the TT motors, the driver, and the HC-SR04 are on the kit list in Chapter 00, lesson 05, &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;. Do not buy a second set from this page.&lt;/p&gt;

&lt;p&gt;This lab needs a meter if you do not have one. The UNI-T UT33D listing is &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;on Hshop&lt;/a&gt;; read the live price, which this lesson does not freeze. Shop fronts for a 100–470 µF, 16 V or 25 V electrolytic, when you are ready to build the VM capacitor and not before, are &lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt;, and &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đầy Rồi&lt;/a&gt;. This page does not invent a capacitor slug. Confirm the can’s voltage and which lead the stripe marks before you solder it.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Soldering robot harnesses</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_08_soldering_basics/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_08_soldering_basics</id>
   <content type="html">&lt;p&gt;A good solder joint is not glue — it is a &lt;strong&gt;metallurgical alloy&lt;/strong&gt; formed when molten tin-based solder wets clean copper and dissolves a microscopic intermetallic layer at the interface. A cold joint, by contrast, is a mechanical trap: it may pass a multimeter continuity test on a quiet desk and then crack open the moment your robot hits a bump at full speed.&lt;/p&gt;

&lt;p&gt;This lesson connects the physics of heat transfer, flux chemistry, and wire gauge selection to reliable, vibration-proof robot wiring harnesses.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Soldering, in this course, means a motor lead that still conducts after the chassis has shaken it. You tin stranded wire, join it to a header pin or a scrap pad, and cover the joint so the flex happens in the wire. A shiny joint that cracks at the first test is still a failed joint.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will heat the pad and the lead together, feed solder into that joint, take the solder away, and only then take the iron away. You will accept a joint that is a shiny fillet wetting both surfaces, and you will reject a ball, a dull grainy cold joint, a bridge, a lifted pad, and burned insulation. You will say why 60/40 or Sn63 is a reasonable alloy for a hobby harness when the room is ventilated, and why flux is part of the joint rather than a smell to tolerate. You will add strain relief and heat-shrink on a TT motor lead that must flex, and you will refuse to tin a Dupont crimp and expect it to stay. You will continuity-beep one practice joint and photograph it against the fillet figure.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can beep continuity from lesson 02, and you know which end of a header is the pin. This lab uses scrap, so lesson 07’s card can wait. Read the safety section before you plug an iron in. No chassis and no lithium cell belong on this bench.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-thermal-mass-flux-chemistry-and-strain-relief-in-robot-harnesses&quot;&gt;Key ideas: Thermal Mass, Flux Chemistry, and Strain Relief in Robot Harnesses&lt;/h2&gt;

&lt;p&gt;Three physical principles govern every reliable solder joint on a mobile robot:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Heat Transfer &amp;amp; Thermal Mass:&lt;/strong&gt; Solder melts around \(183^\circ\text{C}\) (leaded 63/37) or \(217^\circ\text{C}\) (lead-free SAC305), so an iron tip set to \(320\text{–}350^\circ\text{C}\) must heat &lt;em&gt;both&lt;/em&gt; the pad and the lead simultaneously. Heavy ground planes and thick 16–18 AWG battery wires have high thermal mass and require a wide chisel tip, not a fine needle tip.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Oxidation &amp;amp; Flux Chemistry:&lt;/strong&gt; Heated copper oxidizes in seconds, preventing solder from wetting. Rosin or no-clean flux chemically strips metal oxides at soldering temperature; if you hold the iron on a joint for 10 seconds, all flux boils off and the solder turns dull and spiky.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Wire Ampacity &amp;amp; Mechanical Strain Relief:&lt;/strong&gt; Solder wicks up stranded wire and turns the transition zone rigid. Without heat-shrink tubing or zip-tie strain relief, chassis vibration flexes the strands right at the solder boundary until they snap.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;The diff-drive capstone runs two TT motors from a TB6612, on a battery that is not the ESP32 or Pico USB cable. Those leads land on AO1, AO2, BO1, and BO2 and they flex at every tie point. A cold joint can beep and still drop voltage only when a wheel stalls, so the cart pulls while the two PWM commands match. The HC-SR04 flexes less, and the same habit still matters: a 5 V echo wire fraying into a 3.3 V neighbor is worse than an open. Dupont jumpers stay on the breadboard. The motor path wants a pad, a fillet, and a loop of slack.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Soldering_iron.jpg&quot; alt=&quot;A plug-in iron needs a stand the moment it is hot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The photograph is the tool, not the technique. A 60 W iron has no temperature knob. You control heat by how long the tip touches copper. The stand is what keeps that tip off the notebook, the lead, and your arm while you reach for the solder.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/solder_joint_compare.png&quot; alt=&quot;Shiny fillet against a ball&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Use this figure as the lab rubric. The acceptable side is concave, shiny for a tin-lead alloy, and wetting both the pad and the lead so the outline of the pin is still visible. The reject side is a ball or a dull grainy mass. A beep does not promote the reject side.&lt;/p&gt;

&lt;h2 id=&quot;heat-then-solder-then-leave&quot;&gt;Heat, then solder, then leave&lt;/h2&gt;

&lt;p&gt;Clean the tip until it is bright, then melt a little solder onto it. That coat is how heat crosses into the joint. A black tip scorches flux and does not transfer heat. The 60 W iron linked below has one setting: plugged in. It is hotter than a header pad needs, so contact time is the control. One to three seconds is the usual window. A tiny pad you lean on for ten seconds lifts.&lt;/p&gt;

&lt;p&gt;Touch the tip to the pad and the lead together so both are hot. Feed solder into the corner where they meet, not as a blob painted from the iron. The wire should melt because the copper is hot. If it melts only on the tip, the pad is still cold. When the fillet has wet both surfaces, remove the solder first, then the iron. Do not blow, and do not move the lead until the fillet has frozen. Movement in that second cracks later.&lt;/p&gt;

&lt;p&gt;Flux is why the solder spreads instead of balling. Rosin-core wire already contains some. A dab of paste on a dull pad finishes the job. The paste linked below is RELIFE RL-426A. The URL slug says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mo-han&lt;/code&gt;, which also names an iron. Read the jar. If the box is an iron and not flux paste, do not dab it on the pad.&lt;/p&gt;

&lt;p&gt;Sn63/Pb37, and the older 60/40 alloy, melt near \(183^\circ\mathrm{C}\). Common lead-free alloys melt nearer \(217^\circ\mathrm{C}\) and wet more stubbornly, which is a poor match for an iron you cannot turn down. For hobby motor harnesses, tin-lead is acceptable if you ventilate and you wash your hands. It is not acceptable as a reason to skip the fan. Flux smoke is an irritant. A small fan across the bench, or an open window, keeps your face out of the plume. Safety glasses stay on, because flux spits.&lt;/p&gt;

&lt;h2 id=&quot;what-the-fillet-is-telling-you&quot;&gt;What the fillet is telling you&lt;/h2&gt;

&lt;p&gt;A good joint is a shiny fillet that has wetted both the pad and the lead. “Wetted” means the solder feathered onto the copper instead of sitting beside it with a ditch. You can still see the shape of the lead inside the solder. The surface, for Sn63 or 60/40, looks smooth rather than sandy.&lt;/p&gt;

&lt;p&gt;A cold joint is dull and grainy: the pad was not hot, or the wire moved as the alloy froze. Insufficient wetting is the ball, often with bare copper still showing around it. A bridge joins two pins that should stay separate, such as PWMA and its neighbor. A lifted pad is foil torn off by too much heat or by pulling the wire early. On scrap, that is the lesson. On the TB6612 module, that is the end of the module. Burned insulation means the iron leaned on the jacket. The copper may still beep, and the strands break at the brown plastic.&lt;/p&gt;

&lt;p&gt;Fix a bad joint by removing solder, not by piling more on. A desoldering pump, used with the joint molten, pulls the excess off. Then flux, then one clean attempt. Do not pry a frozen joint off a pad.&lt;/p&gt;

&lt;p&gt;Under motor current the shape is an electrical fact, not a beauty contest. Continuity only proves some path exists. A half-wet joint of a few tenths of an ohm, at stall current, drops real voltage:&lt;/p&gt;

\[V_{\mathrm{drop}} = I \times R_{\mathrm{joint}}\]

&lt;p&gt;A joint of \(0.4\,\Omega\) at \(1.2\,\mathrm{A}\) drops about half a volt and heats a speck of solder at about half a watt. One TT motor then runs weak, the cart arcs, and the firmware is innocent.&lt;/p&gt;

&lt;h2 id=&quot;strain-relief-and-the-crimp-you-should-not-solder&quot;&gt;Strain relief, and the crimp you should not solder&lt;/h2&gt;

&lt;p&gt;Motor leads flex, so the fillet must not be the hinge. Strip a few millimetres, twist, and tin a rope no longer than the pad. Make the joint. Slide heat-shrink over the fillet and a short way up the jacket, and shrink it with the iron near the tube, not stabbed into it. A loop or a tie takes the pull. Heat-shrink forgotten until after the joint will not crawl past a housing; cut the joint and start again.&lt;/p&gt;

&lt;p&gt;A Dupont crimp is a spring. Tin it and solder wicks up the strands, the spring stops flexing, and the wire breaks beside the solder after a few dozen plug cycles. Crimps want the matching tool. Solder wants a pad or a header pin you mean to leave in place. Breadboard jumpers stay crimped. TT leads on the driver get soldered, sleeved, and strain-relieved.&lt;/p&gt;

&lt;h2 id=&quot;safety&quot;&gt;Safety&lt;/h2&gt;

&lt;p&gt;The iron rests in a stand from the moment it is hot. Wear eye protection. Do not flick solder off the tip; wipe it on the stand’s sponge or brass wool. Unplug the iron when you stop, including for a short errand. Children do not share this bench. Wash your hands after leaded solder. Do not solder a lithium cell or a loose 18650. Use a pack that already has leads.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Practice on scrap. The driver module and any battery stay in the drawer.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Plug the iron in only after it is in the stand, glasses are on, and a fan or a window is moving air across the bench. Write the alloy. The linked reel is 0.8 mm Sn63/Pb37.&lt;/li&gt;
  &lt;li&gt;Tin the tip until it is shiny. If it will not take solder, the tip is oxidized. Clean it before you touch a pad.&lt;/li&gt;
  &lt;li&gt;Take a scrap of stranded wire. Strip about 3 mm, twist, and tin the strands into one short rope.&lt;/li&gt;
  &lt;li&gt;Solder that end to a header pin or a protoboard scrap. Heat pad and lead together, feed solder into the joint, remove the solder, remove the iron, hold still until the fillet freezes.&lt;/li&gt;
  &lt;li&gt;Photograph the joint next to the solder-joint figure, or hold the figure on your phone in the same frame. Say out loud which side yours matches. If it is dull, balled, or sitting on the pad without wetting, pump the solder off and make one new attempt. If the pad lifts, stop, move to a fresh pad, and write “lifted pad” with how many seconds you think you stayed.&lt;/li&gt;
  &lt;li&gt;Beep continuity from the free end of the wire to the free end of the pin. A beep with a bad shape still fails. An open shiny joint fails the other way.&lt;/li&gt;
  &lt;li&gt;If the joint is on a free lead, add heat-shrink. File the photo as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-08-joint&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Observations you write down: alloy, whether the fillet matched the good side, the continuity result, and any fault you actually produced (bridge, lift, poor wetting, browned insulation). Faults are data. A page that says only “ok” does not pass.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A TT lead was tinned and soldered to a male pin that will plug into the driver. The joint is grey and rounded. The meter beeps. At light load, near the 110–150 mA no-load band of one TT 1:48 motor, the drop is small and the cart creeps straight. The wheel then stalls against a book and the current rises to about \(1.2\,\mathrm{A}\). Measuring across the joint gives roughly \(0.4\,\Omega\).&lt;/p&gt;

\[V_{\mathrm{drop}} = 1.2 \times 0.4 = 0.48\,\mathrm{V}\]

\[P = 1.2 \times 0.48 \approx 0.58\,\mathrm{W}\]

&lt;p&gt;Half a watt in a grain-of-rice blob gets hot. That motor sees half a volt less than its partner, so the diff-drive pulls to one side while both PWM commands match. The repair is to pump the joint clear, tin the strands properly, form a concave fillet, sleeve it, and add a strain-relief loop. Beep again only after the metal has cooled and you are no longer holding it.&lt;/p&gt;

&lt;h2 id=&quot;explanation-sizing-wire-gauge-and-checking-voltage-drop-in-a-robot-harness&quot;&gt;Explanation: Sizing Wire Gauge and Checking Voltage Drop in a Robot Harness&lt;/h2&gt;

&lt;p&gt;Before cutting and soldering a battery-to-driver harness, verify the wire gauge using a 4-step resistance calculation:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Measure round-trip wire length \(L_{\text{rt}} = 2 \times L_{\text{one-way}}\):&lt;/strong&gt; Current travels out on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V+&lt;/code&gt; and back on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, so both wires contribute resistance.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Look up resistance per meter by AWG:&lt;/strong&gt; 18 AWG \(\approx 0.021\,\Omega/\text{m}\); 22 AWG \(\approx 0.053\,\Omega/\text{m}\); 26 AWG \(\approx 0.134\,\Omega/\text{m}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute voltage drop at peak stall current:&lt;/strong&gt; \(\Delta V = I_{\text{peak}} \cdot (R_{\text{per m}} \cdot L_{\text{rt}})\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify acceptable drop (\(\le 2\text{–}3\%\) of rail voltage):&lt;/strong&gt; Keep harness losses small so motor torque stays crisp and logic rails never brown out.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-comparing-18-awg-vs-26-awg-for-a-030-m-robot-power-harness&quot;&gt;Concrete Worked Example: Comparing 18 AWG vs. 26 AWG for a 0.30 m Robot Power Harness&lt;/h3&gt;

&lt;p&gt;Suppose your battery pack sits \(0.30\text{ m}\) from the motor driver (round-trip length \(L_{\text{rt}} = 0.60\text{ m}\)), and peak dual-motor stall current is \(I_{\text{peak}} = 4.0\text{ A}\) on a \(7.4\text{ V}\) rail:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Wire Gauge&lt;/th&gt;
      &lt;th&gt;Resistance / m&lt;/th&gt;
      &lt;th&gt;Round-Trip \(R\) (0.60 m)&lt;/th&gt;
      &lt;th&gt;Voltage Drop \(\Delta V\) at 4 A&lt;/th&gt;
      &lt;th&gt;Power Lost \(I^2 R\)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;18 AWG&lt;/strong&gt; (recommended)&lt;/td&gt;
      &lt;td&gt;\(0.021\,\Omega/\text{m}\)&lt;/td&gt;
      &lt;td&gt;\(0.0126\,\Omega\)&lt;/td&gt;
      &lt;td&gt;\(4.0 \times 0.0126 = 0.050\text{ V}\) (0.7%)&lt;/td&gt;
      &lt;td&gt;\(0.20\text{ W}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;22 AWG&lt;/strong&gt; (medium)&lt;/td&gt;
      &lt;td&gt;\(0.053\,\Omega/\text{m}\)&lt;/td&gt;
      &lt;td&gt;\(0.0318\,\Omega\)&lt;/td&gt;
      &lt;td&gt;\(4.0 \times 0.0318 = 0.127\text{ V}\) (1.7%)&lt;/td&gt;
      &lt;td&gt;\(0.51\text{ W}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;26 AWG&lt;/strong&gt; (jumper wire)&lt;/td&gt;
      &lt;td&gt;\(0.134\,\Omega/\text{m}\)&lt;/td&gt;
      &lt;td&gt;\(0.0804\,\Omega\)&lt;/td&gt;
      &lt;td&gt;\(4.0 \times 0.0804 = 0.322\text{ V}\) (4.4%)&lt;/td&gt;
      &lt;td&gt;\(1.29\text{ W}\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Choosing 18 AWG cuts voltage drop and wire heating by more than &lt;strong&gt;6×&lt;/strong&gt; compared to thin 26 AWG jumpers!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Melting solder onto the iron tip and “painting” it onto a cold pad — the flux burns away on the tip before reaching the joint, guaranteeing a brittle cold solder joint. Always heat the pad and lead first, then feed solder into the junction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; A healthy solder joint looks like a smooth, shiny concave volcano fillet (slightly matte for lead-free) covering the entire pad and pin, completed in 2–4 seconds of iron contact.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;List the contact order for one joint: what the iron touches, where the solder wire touches, which item leaves first, and when you may move the lead. One swap is enough to fail the answer.&lt;/li&gt;
  &lt;li&gt;A joint is \(0.5\,\Omega\) and one TT motor stalls at \(1.0\,\mathrm{A}\). Compute the drop and the power in the joint. Would a continuity beep have caught it?&lt;/li&gt;
  &lt;li&gt;You soldered a header and only then remembered the heat-shrink. What do you do, and where must a pull land once the lead is covered?&lt;/li&gt;
  &lt;li&gt;A friend wants the Dupont crimp on a motor lead tinned “so it never falls out.” Refuse in two sentences and name the joint you would make instead.&lt;/li&gt;
  &lt;li&gt;Name the fault for each clue: copper foil peeled off the scrap board; a dull sandy surface; solder joining two header pins; the wire jacket brown next to an otherwise shiny fillet.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
  &lt;summary&gt;Hints and answers&lt;/summary&gt;

  &lt;ol&gt;
    &lt;li&gt;The iron touches pad and lead together. Solder feeds into the joint, not onto the iron as the main supply of metal. Solder leaves first, then the iron. The lead stays still until the fillet has frozen.&lt;/li&gt;
    &lt;li&gt;\(V = 1.0 \times 0.5 = 0.5\,\mathrm{V}\) and \(P = 0.5\,\mathrm{W}\). A beep only says a path exists. It does not see half an ohm.&lt;/li&gt;
    &lt;li&gt;If the housing blocks the tube, cut the joint and remake it with heat-shrink already parked on the wire. The tie or the loop pulls on the insulation, not on the fillet.&lt;/li&gt;
    &lt;li&gt;Solder wicks into a crimp and the strands break beside it. Crimp with the right tool, or solder the motor lead to a header pin or a pad and add strain relief. Do not tin the crimp.&lt;/li&gt;
    &lt;li&gt;Peeled foil is a lifted pad, from heat or from force. A sandy surface is a cold joint. Solder between pins is a bridge. Brown jacket is burned insulation, even when the fillet itself looks right.&lt;/li&gt;
  &lt;/ol&gt;

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.adafruit.com/adafruit-guide-excellent-soldering&quot;&gt;Adafruit guide to excellent soldering&lt;/a&gt; — fillets, cold joints, and tip care in photographs. Match your lab photo to theirs as well as to this lesson’s figure.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-solder-through-hole-soldering/all&quot;&gt;SparkFun through-hole soldering&lt;/a&gt; — the same heat-then-feed order, with the defects named.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG carrier&lt;/a&gt; — the header you are practising toward. Motor pads are still not today’s scrap exercise.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;Prices below were read on Hshop on &lt;strong&gt;23 September 2026&lt;/strong&gt;. They will move. Confirm the live page before you pay. The rest of the robot is Chapter 00, lesson 05, &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;. This list is only the bench for the joint.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;60 W iron, WADFOW WEL3616, 75,000₫: &lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Round stand with sponge, 40,000₫: &lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Sunchi 0.8 mm Sn63/Pb37, 24,000₫: &lt;a href=&quot;https://hshop.vn/thiec-han-sunchi-kp26-0-8mm-sn63-pb37-solder-wire&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Handskit GS-107 desoldering pump, 55,000₫: &lt;a href=&quot;https://hshop.vn/hut-thiec-handskit-gs-107-desoldering-pump&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;170 mm flush cutter, 35,000₫: &lt;a href=&quot;https://hshop.vn/kim-cat-day-dien-nho-170-cat-chan-linh-kien-dien-tu&quot;&gt;Hshop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;RELIFE RL-426A paste, 25,000₫: &lt;a href=&quot;https://hshop.vn/mo-han-relife-rl-426a-30g-chinh-hang&quot;&gt;Hshop&lt;/a&gt;. Confirm the jar is flux paste. The slug says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mo-han&lt;/code&gt;; do not buy it thinking it is the iron.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same two searches, if Hshop is out of stock: &lt;a href=&quot;https://shopee.vn/search?keyword=mo%20han%2060W&quot;&gt;Shopee, mỏ hàn 60W&lt;/a&gt; and &lt;a href=&quot;https://shopee.vn/search?keyword=thiec%20han&quot;&gt;Shopee, thiếc hàn&lt;/a&gt;. On Lazada, open the &lt;a href=&quot;https://www.lazada.vn/catalog/?q=&quot;&gt;catalog search&lt;/a&gt; and type the same keywords yourself. Shop fronts: &lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt;, &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đầy Rồi&lt;/a&gt;. A header strip and a scrap of protoboard can come from those fronts; this page does not invent a product link for them. Eye protection is not optional because it was left off the price list.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Reading datasheets and pinouts</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_07_datasheet_pinouts/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_07_datasheet_pinouts</id>
   <content type="html">&lt;p&gt;A datasheet is the legal contract between the silicon engineer who designed a chip and the robotics engineer who integrates it. Yet a 60-page PDF filled with timing diagrams and acronyms (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VIH&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VOL&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IOL&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RDS(on)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Abs Max&lt;/code&gt;) can intimidate newcomers into copying random wiring diagrams from forum posts — until a pin smokes because an ESP32 strapping pin was pulled low at boot.&lt;/p&gt;

&lt;p&gt;This lesson gives you a 5-minute tactical reading order for any datasheet so you extract the exact electrical limits, logic thresholds, and pin traps that matter on a robot.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;A datasheet is the only page allowed to permit a wire. This lesson fixes one reading order, then uses it on the TB6612FNG module the diff-drive cart buys and on the ESP32 pin that must not meet a 5 V echo pulse.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will open an unfamiliar PDF and walk it in one order every time: absolute maximum ratings, pinout and pin names, recommended operating conditions, the electrical characteristics you will actually use, the typical application schematic, and the package drawing that locates pin 1. You will copy, for a TB6612-class module, the motor-supply window, the logic-supply window, the standby rule, and the continuous and peak current, and you will place AIN1, AIN2, PWMA, BIN1, BIN2, PWMB, AO1, AO2, BO1, and BO2 on a card. You will point at the ESP32 absolute-maximum row and say why an HC-SR04 echo wire is forbidden on a bare GPIO. You will treat a clone module’s silkscreen as a rumor until a meter and the datasheet agree.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You need Ohm’s law from lesson 01, the meter habits from lesson 02, and the polarity habit from lesson 04. You do not power a motor today. Bring a notebook page you will still have in Chapter 02.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-absolute-maximums-recommended-operating-conditions-and-logic-thresholds&quot;&gt;Key ideas: Absolute Maximums, Recommended Operating Conditions, and Logic Thresholds&lt;/h2&gt;

&lt;p&gt;Reading datasheets efficiently relies on three universal specification categories:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Absolute Maximum Ratings vs. Recommended Operating Conditions:&lt;/strong&gt; &lt;em&gt;Absolute Maximum&lt;/em&gt; is the cliff edge where permanent damage begins (never design near it). &lt;em&gt;Recommended Operating Conditions&lt;/em&gt; is the safe valley where the manufacturer guarantees all timing, accuracy, and noise specs.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Digital Logic Thresholds (\(V_{\text{IH}}, V_{\text{IL}}, V_{\text{OH}}, V_{\text{OL}}\)):&lt;/strong&gt; For two chips to talk reliably, the sender’s worst-case high output must exceed the receiver’s minimum high threshold (\(V_{\text{OH,min}} &amp;gt; V_{\text{IH,min}}\)), and its worst-case low output must sit below the receiver’s maximum low threshold (\(V_{\text{OL,max}} &amp;lt; V_{\text{IL,max}}\)). The gap is your &lt;strong&gt;noise margin&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Special-Function &amp;amp; Strapping Pins:&lt;/strong&gt; On microcontrollers like the ESP32, certain pins are sampled at reset to choose boot mode (flash vs. normal run), some are input-only (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO34–39&lt;/code&gt;), and some lack internal pull-ups. Reading the pinout table before wiring saves hours of baffling boot failures.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;The capstone is a differential-drive cart: two TT gearmotors, a TB6612FNG module, and a 3.3 V ESP32 or Pico. VM comes from a separate motor battery. VCC may sit on the 3.3 V rail. The HC-SR04 echo pin is a 5 V output, and those GPIO pins are not 5 V inputs. Sticker-reading shows up later as wheels that never leave standby, or as a GPIO that dies when the ranger answers. The order below finds the one number that forbids the wire before you build it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;What an H-bridge does with the motor output pins&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Each motor sits between a pair of outputs. Direction is which output is high, speed is how often PWM allows that state, and none of it happens while standby holds the bridge off. Use the figure after you have the pin names, not instead of the table.&lt;/p&gt;

&lt;h2 id=&quot;a-reading-order-you-do-not-shuffle&quot;&gt;A reading order you do not shuffle&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Absolute maximum ratings.&lt;/strong&gt; This table is the damage fence. Past it, the maker stops promising the part survives. It is not a recommended operating point. Copy the one cell that matches the wire you want: supply voltage, logic-pin voltage, or output current. If the plan crosses that cell, the plan is forbidden.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Pinout and pin names.&lt;/strong&gt; The pin table is a dictionary. AO1 is not “left wheel” until you choose which plug lands on AO1. On a TB6612FNG module the names you must be able to touch with a pencil are AIN1, AIN2, PWMA, BIN1, BIN2, PWMB, AO1, AO2, BO1, and BO2, plus VM, VCC, GND, and STBY. Inputs are pins a GPIO may drive. Outputs are pins that may touch a motor. Mixing those groups puts motor current into the microcontroller.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Recommended operating conditions.&lt;/strong&gt; The robot lives here, in the region where the other tables were measured. A module listing can be tighter than the bare die, because copper and headers are part of the object. When the listing is tighter, it wins.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Electrical characteristics you will actually use.&lt;/strong&gt; Skip rows you cannot measure yet. For this driver, copy the minimum voltage that still counts as a logic high, the fact that STBY must be high or every other row is meaningless, and the continuous and peak output current.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Typical application schematic.&lt;/strong&gt; Copy the idea: a capacitor near VM, a capacitor near VCC, one common ground, inputs driven by another chip. A breakout may already hold the small capacitors. An extra capacitor does not replace a missing ground.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Package drawing, for pin 1.&lt;/strong&gt; The drawing is a top view: you look down on the plastic, pins pointing into the board. Pin 1 sits by the dot or the bevel. A bottom-view photo mirrors the row. If the dot and the silk disagree about AO1, the meter settles it.&lt;/p&gt;

&lt;p&gt;Clone boards lie in ink. A “5V” label on a 3.3 V logic pin, GND swapped with VM, an ECHO arrow aimed at trigger: all of these ship. Power only that rail from a current-limited supply, black lead on ground, and write the volts on the card. The silkscreen does not vote after that.&lt;/p&gt;

&lt;h2 id=&quot;worked-walkthrough-the-tb6612fng&quot;&gt;Worked walkthrough: the TB6612FNG&lt;/h2&gt;

&lt;p&gt;Use the module window this course buys, and the Toshiba behavior summarized on Pololu’s carrier page in Further reading. If a cell on that page disagrees with this text, the page wins. If your own module listing is tighter than both, the listing wins.&lt;/p&gt;

&lt;p&gt;Absolute maximum ratings come first so a reversed pack or an over-voltage battery is rejected before the connector mates. That table is the fence. The next paragraphs are where you are allowed to live.&lt;/p&gt;

&lt;p&gt;Pin names come second. Motor A is commanded by AIN1 and AIN2, and its speed pin is PWMA. Motor B is BIN1, BIN2, and PWMB. The pads that touch the motor leads are AO1 and AO2, or BO1 and BO2. STBY is not a spare GPIO. On this chip it is the off switch for both bridges. The control table contains no motion until STBY is high. Many modules pull STBY down on the board, so a forgotten wire leaves both wheels stopped while your notes say “forward.”&lt;/p&gt;

&lt;p&gt;Recommended conditions come third. For the module this course treats as the buy, VM is 4.5–10 V. A single lithium cell near 3.7 V is under that floor. A pack that sits above 10 V when charged is over the module ceiling even if a bare-die PDF looks kinder. Logic VCC is 2.7–5.5 V, so a 3.3 V rail is a legal logic supply. USB 5 V is inside that logic window and is still a bad motor supply, which is lesson 09. VM and VCC are different pins on purpose.&lt;/p&gt;

&lt;p&gt;The rows you will use come fourth. Continuous current is about 1.2 A per channel and peak is about 3.2 A. Two TT motors free-running sit well under 1.2 A. Stall does not. Park a stall on that continuous rating and the driver becomes the fuse. Also copy the logic-high rule. Many printings set&lt;/p&gt;

\[V_{IH,\min} = 0.7 \times V_{CC}\]

&lt;p&gt;At \(V_{CC} = 3.3\,\mathrm{V}\) that is \(2.31\,\mathrm{V}\). A GPIO driven to 3.3 V clears it. The same fraction at 5 V is \(3.5\,\mathrm{V}\), and a 3.3 V GPIO does not clear 3.5 V. That single number forbids “power the logic pin from 5 V because the silk says so, then drive the inputs from the ESP32.” Feed VCC from 3.3 V. Do not live in the gap and hope the input “mostly” works.&lt;/p&gt;

&lt;p&gt;The schematic comes fifth. VM to the motor battery, VCC to 3.3 V, grounds joined, motors on AO1/AO2 and BO1/BO2, four GPIOs on AIN1, AIN2, PWMA, and STBY. A bulk capacitor goes across VM, stripe correct; lesson 09 sizes it. The package drawing comes sixth: mark pin 1 even if you only touch headers.&lt;/p&gt;

&lt;p&gt;“Clockwise” in the datasheet follows the motor leads, not the nose of the chassis. With STBY high, opposite levels on AIN1 and AIN2 plus PWMA high runs the motor; swapping AIN1 and AIN2 reverses it; STBY low forces standby anyway. The first later motion test is STBY high, the direction pins different, and a modest PWM.&lt;/p&gt;

&lt;h2 id=&quot;the-esp32-gpio-is-not-a-place-to-live-at-5-v&quot;&gt;The ESP32 GPIO is not a place to live at 5 V&lt;/h2&gt;

&lt;p&gt;Open the Espressif PDF and use the same order. The absolute-maximum voltage on a GPIO sits at the 3.3 V rail, a few hundred millivolts above it, not at 5 V. Recommended operation is that 3.3 V I/O supply. The absolute-maximum row is the fence, not a place to live. A 5 V pulse is outside the fence.&lt;/p&gt;

&lt;p&gt;The HC-SR04 makes the fence concrete. Powered from 5 V, its echo wire swings to about 5 V when the pulse returns. That absolute-maximum cell forbids a direct connection to any ESP32 GPIO. Trigger is an input on the sensor and can usually be driven from 3.3 V. Echo cannot. A divider is later work; today the card says “echo not direct” and quotes the voltage. A Pico matches this limit: the Raspberry Pi documentation describes 3.3 V pins that are not 5 V tolerant, and code uses the GP numbers, not a count of pads from the USB end.&lt;/p&gt;

&lt;p&gt;If a clone board prints “5V” beside a hole you hoped was a GPIO, measure it with USB plugged in and nothing else attached. A hole that sits near 5 V is a supply pin. It is not an echo input, and it is not an LED pin without a resistor.&lt;/p&gt;

&lt;h2 id=&quot;lab&quot;&gt;Lab&lt;/h2&gt;

&lt;p&gt;Nothing spins. You file two cards.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Title two pages, “MCU pinout” and “driver pinout.” Date them. If the board has not arrived, build the MCU card from the ESP32 PDF and write “board not purchased.”&lt;/li&gt;
  &lt;li&gt;On the driver card, show all six steps. Copy VM 4.5–10 V, VCC 2.7–5.5 V, “STBY must be high,” 1.2 A continuous, and 3.2 A peak. Sketch the header and label AIN1, AIN2, PWMA, BIN1, BIN2, PWMB, AO1, AO2, BO1, BO2, VM, VCC, GND, and STBY. If the silk differs, keep two columns.&lt;/li&gt;
  &lt;li&gt;On the MCU card, copy the GPIO absolute-maximum voltage and the table name. Write that HC-SR04 echo is a 5 V output and does not land on a GPIO. Assign legal outputs to AIN1, AIN2, PWMA, and STBY. On a typical ESP32 module, skip flash pins and input-only pins.&lt;/li&gt;
  &lt;li&gt;Leave VM unpowered. If you can feed only logic from a current-limited 3.3 V source, measure silk “5V” and silk “3V3” with black on ground. Otherwise write the meter steps and mark “dry-run.”&lt;/li&gt;
  &lt;li&gt;File both cards. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-07-mcu-card&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ch01-07-driver-card&lt;/code&gt; are enough if Chapter 02 can still read them.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Stop if a pin gets hot, or if a hole you were about to call a GPIO measures near 5 V.&lt;/p&gt;

&lt;h2 id=&quot;explanation-computing-digital-noise-margins-between-33-v-and-5-v-chips&quot;&gt;Explanation: Computing Digital Noise Margins Between 3.3 V and 5 V Chips&lt;/h2&gt;

&lt;p&gt;Can a 3.3 V ESP32 GPIO output reliably drive a 5 V motor driver’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IN1&lt;/code&gt; logic input without a level shifter? Don’t guess — calculate the noise margins in four steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Read the transmitter (ESP32, \(V_{\text{DD}} = 3.3\text{ V}\)) output specs:&lt;/strong&gt; Typically \(V_{\text{OH,min}} = 0.8\,V_{\text{DD}} = 2.64\text{ V}\) and \(V_{\text{OL,max}} = 0.1\,V_{\text{DD}} = 0.33\text{ V}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Read the receiver’s input specs:&lt;/strong&gt; Compare a TTL-compatible driver (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L298N&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;74HCT&lt;/code&gt;: \(V_{\text{IH,min}} = 2.3\text{ V}\), \(V_{\text{IL,max}} = 0.8\text{ V}\)) against a pure 5 V CMOS input (\(V_{\text{IH,min}} = 0.7\,V_{\text{CC}} = 3.5\text{ V}\), \(V_{\text{IL,max}} = 0.3\,V_{\text{CC}} = 1.5\text{ V}\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute high and low noise margins:&lt;/strong&gt;
\(\text{NM}_H = V_{\text{OH,min}} - V_{\text{IH,min}}, \qquad \text{NM}_L = V_{\text{IL,max}} - V_{\text{OL,max}}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Apply the go/no-go rule:&lt;/strong&gt; Both \(\text{NM}_H\) and \(\text{NM}_L\) must be strictly positive (ideally \(\ge 0.3\text{ V}\) on a noisy robot).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-33-v-esp32-driving-two-different-5-v-inputs&quot;&gt;Concrete Worked Example: 3.3 V ESP32 Driving Two Different 5 V Inputs&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Receiver Type&lt;/th&gt;
      &lt;th&gt;\(V_{\text{IH,min}}\)&lt;/th&gt;
      &lt;th&gt;\(\text{NM}_H = 2.64 - V_{\text{IH,min}}\)&lt;/th&gt;
      &lt;th&gt;\(V_{\text{IL,max}}\)&lt;/th&gt;
      &lt;th&gt;\(\text{NM}_L = V_{\text{IL,max}} - 0.33\)&lt;/th&gt;
      &lt;th&gt;Verdict&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;5 V TTL-compatible driver&lt;/td&gt;
      &lt;td&gt;\(2.30\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(+0.34\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(0.80\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(+0.47\text{ V}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Works directly&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5 V pure CMOS (e.g., NeoPixel / 74HC)&lt;/td&gt;
      &lt;td&gt;\(3.50\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(-0.86\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(1.50\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(+1.17\text{ V}\)&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;Fails — needs level shifter!&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;That single subtraction (\(2.64 - 3.50 = -0.86\text{ V}\)) explains why 3.3 V logic reliably controls a TB6612/L298N input, yet fails intermittently on 5 V CMOS chips unless you add a level shifter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Assuming bidirectional compatibility — even when a 3.3 V output drives a 5 V TTL input cleanly, a &lt;strong&gt;5 V output driving a 3.3 V input&lt;/strong&gt; violates the ESP32’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Abs Max = 3.6 V&lt;/code&gt; and can permanently damage the GPIO protection diodes!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; On an ESP32, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO34&lt;/code&gt;–&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GPIO39&lt;/code&gt; are input-only and have no internal pull-up/pull-down resistors — great for analog battery sensing, unusable for motor PWM outputs.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;p&gt;Each answer names one number, the table it came from, and the wire that number forbids.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Someone wires HC-SR04 ECHO straight to ESP32 GPIO 4 and says the absolute-maximum table will cover it. Which number forbids the wire, and what would you write down if you measured the echo pulse with the sensor powered from 5 V?&lt;/li&gt;
  &lt;li&gt;Driver silk says the logic pin wants 5 V. The ESP32 high is 3.3 V. Compute \(0.7 \times 5\) and \(0.7 \times 3.3\). Which result forbids driving those inputs from the ESP32, and which VCC removes the forbid?&lt;/li&gt;
  &lt;li&gt;Both TT motors are about to be tied to the ESP32 5 V pin “just for a test.” USB is budgeted near 500 mA. The driver is rated 1.2 A continuous. Which number fails first, and why is the larger one not permission to use USB as VM?&lt;/li&gt;
  &lt;li&gt;STBY is unconnected and the sketch never drives it. Which chip behavior forbids PWM from turning a wheel, and what one GPIO action removes it?&lt;/li&gt;
  &lt;li&gt;A DRV8833 board shows up instead of a TB6612. What forbids copying AIN1, PWMA, and STBY from today’s card onto that silk, and which further-reading page do you open?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
  &lt;summary&gt;Hints and answers&lt;/summary&gt;

  &lt;ol&gt;
    &lt;li&gt;The forbid is the ESP32 GPIO absolute maximum, a limit sitting at the 3.3 V rail, not at 5 V. Echo pulses to about the sensor supply, near 5 V. Write that measured high next to the cell. The direct wire is already illegal; a divider is later work.&lt;/li&gt;
    &lt;li&gt;\(0.7 \times 5 = 3.5\,\mathrm{V}\), which a 3.3 V GPIO does not reach, so VCC at 5 V is forbidden for those inputs. \(0.7 \times 3.3 = 2.31\,\mathrm{V}\), which the GPIO does reach. Feed VCC from 3.3 V.&lt;/li&gt;
    &lt;li&gt;The 500 mA USB budget fails first. Stall current of even one TT motor is far above that, and two motors are worse. The 1.2 A figure is the driver’s continuous ceiling, not a promise that USB can source it. VM stays on the motor battery.&lt;/li&gt;
    &lt;li&gt;STBY held low, including an undriven pin that the chip or the module pulls down, keeps both bridges in standby. Drive STBY high and hold it there while motion is allowed.&lt;/li&gt;
    &lt;li&gt;Names do not transfer across driver families. Open the TI DRV8833 page and repeat the six steps. Label whatever that datasheet actually prints.&lt;/li&gt;
  &lt;/ol&gt;

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG carrier&lt;/a&gt; — Toshiba behavior in short English: logic range, standby, and the 1.2 A / 3.2 A story. Let this page correct the lesson if a cell differs, and let your module listing win when it is tighter.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;ESP32 datasheet (PDF)&lt;/a&gt; — absolute maximum ratings and the GPIO list for the echo question.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ti.com/product/DRV8833&quot;&gt;TI DRV8833&lt;/a&gt; — the other driver the chapter checklist allows you to label. Start here if the chip in the photo is not a TB6612.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html&quot;&gt;Raspberry Pi Pico documentation&lt;/a&gt; — 3.3 V I/O, pin 1, and the GP numbers the code must use.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The ESP32 or Pico, the TB6612-class module, the TT motors, and the HC-SR04 are already on the kit list in Chapter 00, lesson 05, &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;. Buy them from that lesson. Do not start a second kit here, and do not buy a loose fine-pitch TB6612FNG to hand-solder for the capstone.&lt;/p&gt;

&lt;p&gt;If you still have no meter, the silkscreen check needs one. A known page is the UNI-T UT33D at &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;Hshop&lt;/a&gt;. Read the live price. Comparing shops is fine at &lt;a href=&quot;https://hshop.vn/&quot;&gt;Hshop&lt;/a&gt;, &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt;, and &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đầy Rồi&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Connectors, motor drivers, and MCU boards</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_06_connectors_drivers_boards/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_06_connectors_drivers_boards</id>
   <content type="html">&lt;p&gt;In robotics competitions, more robots lose points to a vibrated-loose Dupont jumper wire than to a bug in their path planner. A mobile robot is a vibrating mechanical platform: every connector, motor driver breakout, and microcontroller dev-board is both an electrical interface and a mechanical joint.&lt;/p&gt;

&lt;p&gt;This lesson maps the physical ecosystem of connectors (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XT60&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JST-XH&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dupont&lt;/code&gt;), motor drivers, and MCU boards — explaining why keying, current ratings, and star-grounding separate a dependable robot from a fragile science-fair sculpture.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you will be able to tell a 2.54 mm Dupont jumper, which is a signal wire, from a connector that is allowed to carry motor current. You will refuse to force a JST-XH housing onto a servo plug or an XT30 onto an XT60. You will compute the voltage a TT motor actually sees from a 7.4 V pack through a TB6612 versus through an L298N, and you will name the DRV8833 as the smaller substitute with a 1.5 A ceiling that gets hot in a stall. You will distinguish an ESP32 dev board from a bare WROOM module, point at VBUS versus 3V3 on a Pico, and explain why an HC-SR04 echo must not land raw on a 3.3 V GPIO.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can measure DC voltage in parallel, you know a GPIO is a few tens of milliamperes, and you know a stalled TT motor is not. You do not need to PWM a motor yet. This lesson stops at chip markings, pin names, and one voltage-drop calculation before the first wheel turns.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-contact-mechanics-connector-ampacity-and-ground-loop-isolation&quot;&gt;Key ideas: Contact Mechanics, Connector Ampacity, and Ground-Loop Isolation&lt;/h2&gt;

&lt;p&gt;Three practical engineering principles govern interconnects on a robot:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Keyed vs. Unkeyed Connectors &amp;amp; Ampacity:&lt;/strong&gt; High-current battery connectors (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XT60&lt;/code&gt; rated 60 A, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XT30&lt;/code&gt; rated 30 A) and medium-current signal/power headers (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JST-XH&lt;/code&gt; 2.54 mm, rated ~3 A) are physically &lt;strong&gt;keyed&lt;/strong&gt; so you cannot plug them in backward. Friction-fit Dupont jumpers are unkeyed and rated only ~1–2 A — fine for breadboard prototypes, dangerous for permanent battery harnesses.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Common Ground &amp;amp; Star-Grounding Topology:&lt;/strong&gt; Logic signals (PWM, encoder pulses) are voltages measured &lt;em&gt;relative to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;&lt;/em&gt;. Your MCU and motor driver must share a common &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, yet high-current motor return paths should meet the logic ground at a single &lt;strong&gt;star point&lt;/strong&gt; near the battery/regulator so motor current spikes do not lift the MCU’s ground reference.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Subsystem Modularity:&lt;/strong&gt; Treating the power board, motor driver, and MCU dev-board as modular blocks joined by keyed connectors lets you swap a blown driver in 60 seconds without desoldering the entire robot.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-on-the-diff-drive-robot&quot;&gt;Why this matters on the diff-drive robot&lt;/h2&gt;

&lt;p&gt;The capstone is two TT gearmotors, a TB6612, an ESP32 or Pico at 3.3 V, and an HC-SR04. Dupont leads are the right wire for AIN1, AIN2, PWMA, and STBY. They are a poor way to carry stall current into VM, and a worse way to pretend a GPIO is VM.&lt;/p&gt;

&lt;p&gt;A 2S pack is about 7.4 V nominal. A MOSFET bridge drops a few tenths of a volt. An L298N, the same price on the day we checked, drops about two volts, so the motors live at the slow end of their range. The HC-SR04 echo pin is a 5 V output, and an ESP32 or Pico GPIO is not a 5 V input. The level shifter is a 10 000 ₫ part. None of this requires firmware. It requires reading the silk before the pack is plugged in.&lt;/p&gt;

&lt;h2 id=&quot;connectors-pitch-is-not-a-suggestion&quot;&gt;Connectors: pitch is not a suggestion&lt;/h2&gt;

&lt;p&gt;Dupont, the loose square-pin jumper, is built on a 2.54 mm pitch. It is perfect for a breadboard and for logic signals into the TB6612. The contact and the thin wire behind it are not a motor connector. A stall near an ampere warms a tired crimp and drops voltage you already calculated carefully at the driver. Use Dupont for signals. Use a heavier pair of wires, properly secured, for motor current and for the pack leads into VM.&lt;/p&gt;

&lt;p&gt;JST-XH is a different family. The pitch is 2.50 mm, not 2.54 mm, which is close enough to tempt a forced fit and different enough to bend the pins. It shows up on balance leads and on small battery pigtails. A hobby servo uses a three-pin JR-style plug, again near 2.54 mm, with a polarity key that is easy to defeat if you shave the shoulder. XT30 and XT60 are bullet connectors for higher current. They do not mate with each other, and they do not mate with JST. If it does not slide home with a firm click and no bent metal, stop. Forcing a housing is how a pack gets reversed onto VM.&lt;/p&gt;

&lt;h2 id=&quot;the-driver-drop-worked-at-74-v&quot;&gt;The driver drop, worked at 7.4 V&lt;/h2&gt;

&lt;p&gt;An H-bridge steers motor current. It does not manufacture that current. The microcontroller tells the bridge which way to close the switches. The pack on VM supplies the amperes.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;The microcontroller steers the bridge; the pack supplies the motor current&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The TB6612FNG module in the cart is a MOSFET bridge. Hshop’s listing, read on 23 September 2026, allows VM from 4.5 V to 10 V, logic from 2.7 V to 5.5 V, 1.2 A continuous and 3.2 A peak. A 3.3 V GPIO is a legal high. A reasonable voltage drop for that style of bridge, at modest current, is a few tenths of a volt. Take 0.3 V as the worked figure:&lt;/p&gt;

\[V_\mathrm{motor} \approx 7.4 - 0.3 = 7.1\ \mathrm{V}\]

&lt;p&gt;The L298N module was the same 45 000 ₫ and is the part we do not make the default. It is a Darlington bridge. Budget about 2 V of drop at motor current:&lt;/p&gt;

\[V_\mathrm{motor} \approx 7.4 - 2.0 = 5.4\ \mathrm{V}\]

&lt;p&gt;The TT motors in this course are the 3 V to 9 V class. Five-point-four volts still spins them. It spins them more slowly and with less torque than 7.1 V, and a pack that is no longer fresh makes the gap worse. You also do not use the L298N module’s onboard 5 V regulator to run the ESP32.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Dosmotorsl298n.jpg&quot; alt=&quot;An L298N-style module: capable, and a large voltage drop&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The DRV8833 is the smaller substitute, listed at 25 000 ₫, with VM from 2.7 V to 10.8 V and 1.5 A maximum per channel on that Hshop listing. One TT motor per channel is a fair use. A prolonged stall heats a small package quickly. Do not parallel both drive motors onto one DRV8833 channel and call it a spare plan. On the TB6612, STBY must be held high or the bridge stays asleep and the motors sit still while the firmware looks perfect.&lt;/p&gt;

&lt;h2 id=&quot;boards-where-5-v-is-allowed-to-exist&quot;&gt;Boards: where 5 V is allowed to exist&lt;/h2&gt;

&lt;p&gt;An ESP32 development board has a USB socket, a USB-serial chip such as a CH340 or a CP2102 near that socket, a 3.3 V regulator, and a metal-can module. A bare ESP32-WROOM module is the can and the pads, with no USB and no regulator you can plug a cable into. If the part in the bag has no USB connector, you did not buy the dev board the cart asked for, even if the can says ESP32.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/ESP32_on_Lolin32_Lite_clone_board_cropped.jpg&quot; alt=&quot;An ESP32 on a small development board, USB and regulator included&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/pcb_mcu_anatomy.png&quot; alt=&quot;What to look for on a microcontroller board: USB, regulator, module, headers&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A Raspberry Pi Pico has a dual-row header and no separate metal-can Wi-Fi module on the original part. VBUS is the USB 5 V rail when the board is plugged into a host. 3V3 is the regulated output. GPIO logic is 3.3 V. Do not power the TT motors from 3V3, and do not treat VBUS as a GPIO.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Raspberry_Pi_Pico.jpg&quot; alt=&quot;Raspberry Pi Pico: dual-row header, 3.3 V logic&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_rails_3v3.png&quot; alt=&quot;Logic at 3.3 V, USB at 5 V, motor supply kept off that rail&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The HC-SR04’s trigger pin will usually accept a 3.3 V high. Its echo pin drives out at about 5 V. That echo must not go straight into an ESP32 or Pico GPIO. A 4-channel level shifter, the 10 000 ₫ module on the cart, is the straightforward fix: 3.3 V side toward the microcontroller, 5 V side toward the sensor, grounds common. A resistor divider on echo only is the other option. Two resistors, 10 kΩ on top and 20 kΩ on the bottom, give&lt;/p&gt;

\[V_\mathrm{out} = 5 \times \frac{20}{10 + 20} \approx 3.3\ \mathrm{V}\]

&lt;p&gt;which a 3.3 V pin can accept. Do not put that divider on a pin the sensor is trying to read as a 5 V input unless you have checked that 3.3 V is a valid high for it. Trig from a GPIO is usually fine without the divider. Echo is the one that bites.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;p&gt;Do this with the pack disconnected and the motors unplugged. Identification first.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Photograph the motor-driver chip, close enough that the marking can be read. Write TB6612FNG, DRV8833, or L298N, whichever is actually printed. If the marking is a module vendor’s sticker and not the silicon, say so.&lt;/li&gt;
  &lt;li&gt;Photograph the microcontroller’s USB-serial chip. CH340 and CP2102 are the common ones on the ESP32 boards in this course. If there is no USB socket and no serial chip, you have a bare module.&lt;/li&gt;
  &lt;li&gt;From the Hshop TB6612 description, label a sketch with AIN1, AIN2, PWMA, STBY, VM, VCC, and GND. Match those names to the silk on your module if you already have it. VM is the motor supply, about 4.5 V to 10 V. VCC is logic, 2.7 V to 5.5 V, so the ESP32 or Pico 3.3 V pin may feed it. GND is the common ground with the microcontroller. STBY must be driven high or the outputs stay off.&lt;/li&gt;
  &lt;li&gt;On the Pico or the ESP32, point at 3V3, at the USB 5 V node (VBUS or the pin marked 5V), and at one GPIO. Say out loud which of those three is allowed to touch an HC-SR04 echo pin. The answer is none of them, until echo has been shifted or divided.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should finish with two photographs and a labeled sketch, and with every motor lead still in the bag. A correct sketch shows VM and VCC as different pins. If your module ties them together, you have not found a shortcut; you have found a module that cannot take a 7.4 V pack without also forcing that voltage onto the logic pin. Do not power it that way.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to change&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Motors silent, firmware convinced it is driving&lt;/td&gt;
      &lt;td&gt;STBY left floating or low&lt;/td&gt;
      &lt;td&gt;Tie STBY high with the logic supply, through the pin the silk names.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Robot crawls on a fresh 7.4 V pack&lt;/td&gt;
      &lt;td&gt;L298N drop, motor seeing about 5.4 V&lt;/td&gt;
      &lt;td&gt;Use the TB6612. Do not raise the pack above the driver’s VM rating to compensate.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Dupont connector hot at VM&lt;/td&gt;
      &lt;td&gt;Signal jumper carrying motor current&lt;/td&gt;
      &lt;td&gt;Move pack and motor current to heavier wire. Leave Dupont on AIN1, AIN2, PWMA, STBY.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;GPIO dead after the HC-SR04 was plugged in&lt;/td&gt;
      &lt;td&gt;Echo at 5 V into a 3.3 V pin&lt;/td&gt;
      &lt;td&gt;Add the level shifter or the echo divider before the sensor returns.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Housing will not mate without force&lt;/td&gt;
      &lt;td&gt;JST-XH, JR, XT30, and XT60 are different families&lt;/td&gt;
      &lt;td&gt;Stop. Match the family. Do not shave a key.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Safety: wheels off the table even though this lab does not spin them. Pack disconnected. Do not probe a pack with your fingers on the metal of the tips. The 7.4 V arithmetic is done on paper until a later lesson measures VM with the meter on a range above 10 V.&lt;/p&gt;

&lt;h2 id=&quot;explanation-why-shared-return-wires-cause-ground-bounce-on-sensors&quot;&gt;Explanation: Why Shared Return Wires Cause “Ground Bounce” on Sensors&lt;/h2&gt;

&lt;p&gt;Have you ever wondered why an analog sensor reading jumps wildly the moment the motors turn on? Trace the 4-step ground-bounce mechanism:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Every return wire has non-zero resistance \(R_{\text{gnd}}\):&lt;/strong&gt; A thin 20 cm Dupont wire plus two loose pin contacts easily totals \(R_{\text{gnd}} \approx 0.15\,\Omega\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Motor current returns through that shared wire:&lt;/strong&gt; If the MCU and sensor share the &lt;em&gt;same&lt;/em&gt; ground wire back to the battery as the motor driver, a \(2.0\text{ A}\) motor pulse flows through \(R_{\text{gnd}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute the ground lift (ground bounce):&lt;/strong&gt; \(\Delta V_{\text{gnd}} = I_{\text{motor}} \cdot R_{\text{gnd}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Fix with star grounding:&lt;/strong&gt; Give the motor driver its own thick (18–20 AWG) ground wire straight to the battery, and run a separate ground wire from the battery/regulator to the MCU and sensors. Now \(I_{\text{motor}}\) never flows through the sensor’s ground wire!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-ground-bounce-error-on-a-12-bit-adc-sensor&quot;&gt;Concrete Worked Example: Ground Bounce Error on a 12-Bit ADC Sensor&lt;/h3&gt;

&lt;p&gt;Suppose an analog IR distance sensor outputs \(1.500\text{ V}\) relative to true battery ground, read by a 3.3 V, 12-bit ADC (4096 counts, \(0.806\text{ mV/count}\)).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Bad wiring (daisy-chained thin ground wire, \(R_{\text{gnd}} = 0.15\,\Omega\), motor surge \(I = 2.0\text{ A}\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Ground lift at sensor/MCU junction: \(\Delta V_{\text{gnd}} = 2.0\text{ A} \times 0.15\,\Omega = 0.300\text{ V}\).&lt;/li&gt;
      &lt;li&gt;ADC reading error: \(\frac{0.300\text{ V}}{0.000806\text{ V/count}} \approx 372\text{ ADC counts}\) (a massive &lt;strong&gt;20% phantom distance jump&lt;/strong&gt; every time the motor accelerates!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Good wiring (star ground with 18 AWG motor return, sensor ground carries only \(I_{\text{sensor}} = 0.02\text{ A}\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Ground lift on sensor wire: \(\Delta V_{\text{gnd}} = 0.02 \times 0.15 = 0.003\text{ V}\) (only ~3.7 counts — 100× cleaner!).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Forgetting to connect the MCU &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; to the motor driver logic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; altogether — leaving the PWM input “floating” so the motor twitches unpredictably or ignores commands.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If your robot uses 2S Li-ion power (peak 5 A), an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XT30&lt;/code&gt; (30 A) or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XT60&lt;/code&gt; (60 A) battery plug provides a 6×–12× current safety factor and physically prevents reverse-polarity accidents.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A pack is 7.4 V. Repeat the drop calculation for a 0.3 V MOSFET bridge and a 2.0 V Darlington bridge. Which voltage does the TT motor prefer, and why is the answer not “buy the L298N because it was also 45 000 ₫”?&lt;/li&gt;
  &lt;li&gt;The DRV8833 listing says 1.5 A maximum per channel. A stall lasts several seconds and the chip is too hot to touch. What limit did you meet, and why is “maximum” not a continuous plan for two motors on one channel?&lt;/li&gt;
  &lt;li&gt;Name three pins on a TB6612 module that a Dupont jumper may carry, and one net it should not.&lt;/li&gt;
  &lt;li&gt;A bag contains a bare ESP32-WROOM and a NodeMCU-32S board. Which one appears as a USB serial port when you plug in a cable, and what chip near the USB socket should you photograph?&lt;/li&gt;
  &lt;li&gt;An HC-SR04 echo wire is landed on a Pico GPIO. What voltage is that wire trying to be, what is VBUS, and what part from the cart goes between them?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. MOSFET bridge: $7.4 - 0.3 = 7.1\ \mathrm{V}$. Darlington: $7.4 - 2.0 = 5.4\ \mathrm{V}$. The motor prefers 7.1 V, inside the 3 V to 9 V class with more speed and torque. Equal price does not equal voltage at the terminals.
2. You met the thermal limit of a small package, and you are at the 1.5 A maximum. Maximum is a ceiling, not a cruise. One motor per channel, and a stall that is allowed to sit there will still get hot. Do not parallel both drive wheels on one channel.
3. Dupont is appropriate for AIN1, AIN2, PWMA, and STBY. It should not be the sustained path for VM motor current.
4. The NodeMCU-32S has USB. Photograph the CH340 or CP2102 beside the socket. The bare WROOM has no USB-serial chip until you add one.
5. Echo wants to be about 5 V. VBUS is the Pico&apos;s USB 5 V input, not a GPIO and not a place to land echo. The 4-channel level shifter goes between echo and the GPIO, with grounds common. A divider on echo only is the other fix.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;p&gt;Pololu’s TB6612FNG carrier page is the clean statement of what the chip is for, separate from any one marketplace listing: &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG carrier&lt;/a&gt;. TI’s product page for the smaller substitute is &lt;a href=&quot;https://www.ti.com/product/DRV8833&quot;&gt;DRV8833&lt;/a&gt;. SparkFun’s logic-level tutorial is the 3.3 V versus 5 V problem the HC-SR04 creates: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;Logic levels&lt;/a&gt;. The ESP32 numbers behind the GPIO warning live in Espressif’s datasheet: &lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;ESP32 datasheet&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The rest of the robot, including the chassis, the cells, and the meter, is in &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;. Prices below are Hshop listings as read on 23 September 2026. Confirm them before you pay.&lt;/p&gt;

&lt;p&gt;The default driver was 45 000 ₫: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-tb6612fng-dc-motor-driver&quot;&gt;TB6612FNG&lt;/a&gt;, VM 4.5–10 V, logic 2.7–5.5 V, 1.2 A continuous and 3.2 A peak. The smaller substitute was 25 000 ₫: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-drv8833-dc-motor-driver&quot;&gt;DRV8833&lt;/a&gt;, VM 2.7–10.8 V, 1.5 A maximum per channel. The L298N was also 45 000 ₫ and is not the default, because of the drop you just calculated: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;L298N module&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The level shifter for the HC-SR04 echo was 10 000 ₫: &lt;a href=&quot;https://hshop.vn/mach-chuyen-muc-tin-hieu-logic-4-kenh&quot;&gt;mạch chuyển mức 4 kênh&lt;/a&gt;. The ESP32 dev board with USB was 190 000 ₫: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;NodeMCU-32S CH340&lt;/a&gt;. A Pico is a search page on Hshop, not a single slug in this list: &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;search for Raspberry Pi Pico&lt;/a&gt;. On 23 September 2026 that search showed a Pico 2 at 195 000 ₫ and a Pico 2 W at 275 000 ₫. Read the current titles before you add one to the cart. Buy a board with USB, not a bare module, unless you already have a programmer.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Transistors, MOSFETs, regulators, and IC packages</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_05_transistor_mosfet_ic_id/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_05_transistor_mosfet_ic_id</id>
   <content type="html">&lt;p&gt;A microcontroller GPIO pin can output a crisp 3.3 V logic signal, yet it can safely source only about 12–40 mA — barely enough to light two LEDs, let alone spin a 1.5 A gearmotor. How does a whisper of milliwatt logic command watts of mechanical muscle? Through &lt;strong&gt;transistors and MOSFETs&lt;/strong&gt; acting as electrically controlled switches, and &lt;strong&gt;voltage regulators&lt;/strong&gt; acting as automated pressure valves for power rails.&lt;/p&gt;

&lt;p&gt;This lesson teaches you to recognize BJTs, logic-level MOSFETs, linear vs. switching regulators, and common IC packages — and to calculate whether a transistor will stay cool or cook itself before you ever power up.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you will be able to refuse a pinout that was guessed from the package shape. A TO-92 or a TO-220 can be a 2N2222, a BC547, a 2N3904, an AMS1117, a 7805, or a small MOSFET, and only the printing knows which. You will number a DIP from the dot or notch, counterclockwise. You will explain why a MOSFET gate is not a current-driven base, and why $V_{GS(th)}$ is not the voltage at which a motor switch is fully on. You will compare a 2N2222 used as a lamp switch with the TB6612 the robot actually needs, and you will say why 12 V must not be poured into an ESP32 5 V pin in the hope that the onboard regulator will cope.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can tell a diode’s cathode by its band, and you can use diode mode on an unpowered part. You know a GPIO is a 12 mA to 40 mA object and a stalled TT motor is an ampere-class object. This lesson is identification, plus the two ratings that stop a smoked part: pin order, and heat while dropping voltage.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-semiconductor-switching-linear-vs-buck-regulation-and-thermal-resistance&quot;&gt;Key ideas: Semiconductor Switching, Linear vs. Buck Regulation, and Thermal Resistance&lt;/h2&gt;

&lt;p&gt;Three pillars tie active semiconductors to reliable robot hardware:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;BJT (Current-Controlled) vs. MOSFET (Voltage-Controlled):&lt;/strong&gt; A Bipolar Junction Transistor (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2N2222&lt;/code&gt;) requires continuous base current \(I_B \approx I_C / \beta_{\text{sat}}\) and drops \(V_{\text{CE(sat)}} \approx 0.2\text{ V}\). A logic-level N-channel MOSFET (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IRLZ44N&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AO3400&lt;/code&gt;) draws essentially zero steady-state gate current and behaves like a tiny resistor \(R_{\text{DS(on)}}\) once \(V_{\text{GS}} &amp;gt; V_{\text{GS(th)}}\) fully enhances the channel.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Linear (LDO) vs. Switching (Buck) Regulators:&lt;/strong&gt; An LDO (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMS1117-3.3&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7805&lt;/code&gt;) regulates voltage by burning excess voltage directly as heat: \(P_{\text{heat}} = (V_{\text{in}} - V_{\text{out}}) I_{\text{load}}\). A buck converter switches rapidly through an inductor to convert voltage at \(85\text{–}95\%\) efficiency.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Thermal Resistance (\(R_{\theta\text{JA}}\)):&lt;/strong&gt; Every package has a junction-to-ambient thermal resistance (in \(^\circ\text{C/W}\)). Junction temperature rise is \(\Delta T = P_{\text{heat}} \cdot R_{\theta\text{JA}}\); exceeding \(T_{J,\max}\) (typically \(125\text{–}150^\circ\text{C}\)) triggers thermal shutdown or permanent silicon failure.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-on-the-diff-drive-robot&quot;&gt;Why this matters on the diff-drive robot&lt;/h2&gt;

&lt;p&gt;The ESP32 board hides an AMS1117-3.3, or something in that family, between USB 5 V and the 3.3 V rail. The Pico has its own regulator between VBUS and 3V3. Those parts are why the board can speak 3.3 V logic to a TB6612, whose logic range is about 2.7 V to 5.5 V. They are not permission to feed a 12 V pack into the pin marked 5V.&lt;/p&gt;

&lt;p&gt;A small transistor looks like a way to switch one TT motor from a GPIO. The motor is inductive, so it needs a flyback path when the switch opens, and stall current sits near or above what a 2N2222 wants to carry. A MOSFET chosen because its threshold is “only 2 V” may still be half-on at 3.3 V and hot. The TB6612 already solves this for two channels, at 1.2 A continuous and 3.2 A peak. This lesson is how you recognize the packages so you do not install a regulator where a transistor was drawn.&lt;/p&gt;

&lt;h2 id=&quot;packages-and-why-the-printing-wins&quot;&gt;Packages, and why the printing wins&lt;/h2&gt;

&lt;p&gt;TO-92 is the little black blob with three leads. TO-220 is the larger tabbed package. Both shapes are shared by different silicon. A 2N2222, a BC547, and a 2N3904 are small bipolars with different pin orders. The P2N2222 and the 2N2222 are a famous pair: same family, different lead order. An AMS1117 and a 7805 are regulators, not switches. A small MOSFET in TO-92 will not survive being wired from a 2N2222 pinout you memorized.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/component_lookalikes.png&quot; alt=&quot;Several packages that share a silhouette and not a pinout&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Read the printing on the flat face, then open that exact datasheet. If the marking is rubbed off, the part is unknown. Do not try pin orders on a live GPIO.&lt;/p&gt;

&lt;p&gt;A dual-inline package is numbered from the notch or the dot. Notch at the top, pin 1 is upper left. Numbers run down the left side, then up the right side: counterclockwise as you look at the top. The same rule matters when a level-shifter chip straddles the breadboard trench.&lt;/p&gt;

&lt;h2 id=&quot;bipolar-switch-mosfet-and-the-number-v_gsth-is-not&quot;&gt;Bipolar switch, MOSFET, and the number $V_{GS(th)}$ is not&lt;/h2&gt;

&lt;p&gt;An NPN used as a switch wants base current, often on the order of a tenth of the collector current, through a resistor from the GPIO. For a 150 mA load that is already about 15 mA, most of a cautious GPIO budget. For an 800 mA stall the same rule wants about 80 mA, which a GPIO does not have, and the 2N2222’s own rating is then the next limit. When the switch opens, motor current does not stop instantly. It needs a flyback diode, band toward the motor supply, or the spike lands on the collector.&lt;/p&gt;

&lt;p&gt;A MOSFET gate is different. In normal switching you do not push a continuous gate current. You charge the gate up to a voltage, and the channel’s resistance is set by how far that voltage sits above the source. $V_{GS(th)}$, the threshold, is the gate voltage where the manufacturer measured a tiny test current, often a quarter of a milliamp. It is the start of conduction, not a promise of low $R_{DS(on)}$. A datasheet might list threshold anywhere from about 2 V to 4 V and then specify $R_{DS(on)}$ only at $V_{GS} = 4.5\ \mathrm{V}$ or $10\ \mathrm{V}$. Drive the gate at “just threshold” and the FET stays in its linear region: it drops volts, it gets hot, and the motor is slow.&lt;/p&gt;

&lt;p&gt;A logic-level MOSFET is one whose low on-resistance is specified at a logic-sized $V_{GS}$, sometimes 4.5 V and sometimes 2.5 V. Even a good one, used as a low-side switch for a single TT motor, still needs a flyback diode. Two motors, two directions, and PWM are why the cart uses a TB6612 instead of a transistor you identified today. The driver is still a set of MOSFETs. You are just not hand-wiring their gates on a breadboard for the capstone.&lt;/p&gt;

&lt;h2 id=&quot;regulators-and-the-watt-you-throw-away&quot;&gt;Regulators, and the watt you throw away&lt;/h2&gt;

&lt;p&gt;A linear regulator such as the 7805 holds its output at 5 V by dropping the excess input voltage as heat. The power in the regulator is the drop times the current:&lt;/p&gt;

\[P = (V_\mathrm{in} - V_\mathrm{out}) \, I\]

&lt;p&gt;Work a mild case. A 9 V supply, a 5 V output, and 200 mA of load:&lt;/p&gt;

\[P = (9 - 5) \times 0.2 = 0.8\ \mathrm{W}\]

&lt;p&gt;A TO-220 with a little copper or a small tab can do that. It will be warm. Now imagine 12 V poured into the 5 V pin of an ESP32 board whose AMS1117-3.3 then makes 3.3 V for a board that draws 150 mA once Wi-Fi is up:&lt;/p&gt;

\[P = (12 - 3.3) \times 0.15 \approx 1.3\ \mathrm{W}\]

&lt;p&gt;That is a lot for a small SOT-223 sitting on a thin module, and it assumes the only victim is the regulator. On many dev boards the pin marked 5V is also the USB 5 V node. Twelve volts there can reach the USB socket and the USB-serial chip. The AMS1117’s own absolute maximum is not a promise about the rest of the board. Power the ESP32 or the Pico from USB while you are on the bench. Put the motor pack on TB6612 VM only, inside about 4.5 V to 10 V. Share ground. Do not hope.&lt;/p&gt;

&lt;h2 id=&quot;worked-example-sort-the-marking-then-the-junction&quot;&gt;Worked example: sort the marking, then the junction&lt;/h2&gt;

&lt;p&gt;Suppose the tray contains four parts whose bodies you must not trust: a TO-92 printed BC547, a TO-92 printed 2N2222, a TO-220 printed 7805, and a small MOSFET whose marking you can actually read as a MOSFET. The families are: BC547 and 2N2222 are NPN bipolars with different pinouts; the 7805 is a 5 V linear regulator; the MOSFET is a voltage-controlled switch. Nothing in that sentence tells you which lead is the emitter. The printing plus the datasheet does.&lt;/p&gt;

&lt;p&gt;Out of circuit and unpowered, diode mode on a bipolar junction is a sanity check, not a full pinout. The base-emitter junction and the base-collector junction each behave like a diode, near 0.6 V one way and open the other. The emitter-collector path should not beep like a wire in both directions. If you find two junctions that share a common lead, that common lead is a candidate for the base. Stop there. Do not declare the emitter versus the collector from one afternoon of probing, and do not install the part on a GPIO until the datasheet agrees. A regulator will not show two tidy diode drops that match a 2N2222, which is useful: if your “transistor” does not diode-test like a transistor, it may be the 7805 you were about to wire as a switch.&lt;/p&gt;

&lt;p&gt;The robot still does not get a 2N2222 on the TT motor after a successful diode test. Direction and braking want an H-bridge. The TB6612 module we use is rated 1.2 A continuous and 3.2 A peak, with logic that accepts 3.3 V. That is the switch.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;If you have loose parts, sort them by the printed marking into bipolar, MOSFET, regulator, or unknown. If you only have the photographs in this lesson and the boards in the kit, sort those pictures the same way and write the family next to each.&lt;/li&gt;
  &lt;li&gt;On any DIP you can see, find the notch or dot and number pin 1. Confirm the count runs down one side and up the other.&lt;/li&gt;
  &lt;li&gt;On the ESP32 board, find the small regulator near the USB connector if the silk or the body shows an AMS1117-class marking. Do not apply 12 V to test it. Note which pin is labeled 5V and which is 3V3.&lt;/li&gt;
  &lt;li&gt;Unpowered, out of circuit, diode-test one known diode and one known bipolar transistor if you have them. Record the two junctions. If you do not have a loose transistor, skip the probe and keep the written sort from step 1. Do not diode-test a part that is still soldered to a powered board.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should end with a labeled list, not with a motor that spins. A successful diode test shows about 0.5 V to 0.7 V on two junctions that share a pin, and no short from emitter to collector. The 7805 does not join that list.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to change&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Two TO-92 parts, one pinout assumed for both&lt;/td&gt;
      &lt;td&gt;Different markings, or 2N2222 versus P2N2222&lt;/td&gt;
      &lt;td&gt;Read the print. Open that datasheet. Do not copy the first pin order.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Diode test shows a short both ways on every pin&lt;/td&gt;
      &lt;td&gt;Part damaged, or probes on a soldered circuit that is not empty&lt;/td&gt;
      &lt;td&gt;Lift the part. If it is still a short, discard it.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Regulator gets hot on 12 V into a 5 V pin&lt;/td&gt;
      &lt;td&gt;Linear drop times board current, and 12 V on a USB-named rail&lt;/td&gt;
      &lt;td&gt;Remove 12 V. Power the board from USB. Motor pack goes to VM only.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MOSFET barely warm at the gate, motor slow and FET hot&lt;/td&gt;
      &lt;td&gt;Gate held near $V_{GS(th)}$, channel not fully on&lt;/td&gt;
      &lt;td&gt;This is why the capstone uses a TB6612, not a threshold-biased FET.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Safety: no motor supply in this lab, no 12 V experiments, diode mode only on loose parts. A TO-220 tab on a regulator can be electrically live and hot on a finished board; do not grab it to see if the robot is working.&lt;/p&gt;

&lt;h2 id=&quot;explanation-computing-junction-temperature-for-an-ldo-vs-a-mosfet-switch&quot;&gt;Explanation: Computing Junction Temperature for an LDO vs. a MOSFET Switch&lt;/h2&gt;

&lt;p&gt;Before choosing between an LDO and a buck converter — or checking if a MOSFET needs a heatsink — run this 4-step thermal check:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Compute dissipated power \(P_{\text{diss}}\):&lt;/strong&gt; For an LDO, \(P = (V_{\text{in}} - V_{\text{out}}) I\); for a saturated MOSFET, \(P = I^2 R_{\text{DS(on)}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Look up \(R_{\theta\text{JA}}\) from the datasheet:&lt;/strong&gt; Typical values are \(\approx 60^\circ\text{C/W}\) for a bare &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TO-220&lt;/code&gt; package and \(\approx 90\text{–}120^\circ\text{C/W}\) for an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SOT-223&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMS1117&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Calculate junction temperature:&lt;/strong&gt; \(T_J = T_{\text{ambient}} + P_{\text{diss}} \cdot R_{\theta\text{JA}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify margin:&lt;/strong&gt; Keep \(T_J &amp;lt; 100^\circ\text{C}\) (and surface touch temperature safe) so the robot does not brown out mid-run.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-powering-an-esp32-from-74-v--ldo-vs-buck-plus-a-2-a-mosfet-switch&quot;&gt;Concrete Worked Example: Powering an ESP32 from 7.4 V — LDO vs. Buck, Plus a 2 A MOSFET Switch&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Part A — &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMS1117-3.3&lt;/code&gt; LDO dropping \(7.4\text{ V} \to 3.3\text{ V}\) at \(I = 0.40\text{ A}\) (Wi-Fi peak):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Heat dissipated: \(P_{\text{LDO}} = (7.4 - 3.3) \times 0.40 = 4.1 \times 0.40 = 1.64\text{ W}\).&lt;/li&gt;
      &lt;li&gt;With \(R_{\theta\text{JA}} = 90^\circ\text{C/W}\) and \(T_{\text{amb}} = 30^\circ\text{C}\):
\(T_J = 30 + 1.64 \times 90 = 30 + 147.6 = 177.6^\circ\text{C} &amp;gt; 125^\circ\text{C}!\)
The LDO immediately hits thermal shutdown and resets your ESP32! Using a 5 V buck converter first drops the LDO input to \(5.0\text{ V}\), cutting LDO heat to \((5.0 - 3.3) \times 0.40 = 0.68\text{ W}\) (\(T_J \approx 91^\circ\text{C}\) — safe).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Part B — Logic-level MOSFET (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R_DS(on) = 0.05 Ω&lt;/code&gt; at \(V_{\text{GS}} = 3.3\text{ V}\)) switching a \(I = 2.0\text{ A}\) motor:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Heat dissipated: \(P_{\text{MOS}} = 2.0^2 \times 0.05 = 4 \times 0.05 = 0.20\text{ W}\).&lt;/li&gt;
      &lt;li&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TO-220&lt;/code&gt; \(R_{\theta\text{JA}} = 62^\circ\text{C/W}\): \(\Delta T = 0.20 \times 62 = 12.4^\circ\text{C}\) — barely warm to the touch, no heatsink needed!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Picking a standard 10 V MOSFET (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IRF540N&lt;/code&gt;) and driving its gate with 3.3 V from an ESP32. At 3.3 V the channel barely opens, \(R_{\text{DS(on)}}\) stays huge, and the MOSFET overheats rapidly. Always check the datasheet’s \(R_{\text{DS(on)}}\) &lt;em&gt;specifically at \(V_{\text{GS}} = 3.3\text{ V}\) or \(4.5\text{ V}\)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; An LDO’s efficiency is simply \(V_{\text{out}} / V_{\text{in}}\). Dropping \(7.4\text{ V}\) to \(3.3\text{ V}\) linearly wastes \(1 - (3.3/7.4) = 55.4\%\) of battery energy as heat.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A DIP has a notch. Looking down, where is pin 1, and which way do the numbers run?&lt;/li&gt;
  &lt;li&gt;A MOSFET datasheet lists $V_{GS(th)}$ from 2 V to 4 V at $250\ \mu\mathrm{A}$, and $R_{DS(on)}$ only at $V_{GS} = 10\ \mathrm{V}$. Why is “the threshold is 2 V, and my GPIO is 3.3 V” not a motor-drive design?&lt;/li&gt;
  &lt;li&gt;Compute the heat in a 7805 that drops 12 V to 5 V at 300 mA. Would you bolt that tab to something?&lt;/li&gt;
  &lt;li&gt;A 2N2222 is proposed as the low-side switch for one TT motor at 0.8 A stall, with base current “a tenth of collector current” from a GPIO. Estimate the base current and say what else the motor still needs when the transistor turns off.&lt;/li&gt;
  &lt;li&gt;Why is an unmarked TO-92 a worse spare than no spare at all for the ESP32’s 3.3 V regulator?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. Pin 1 is at the upper left of the notch. Numbers run down the left side, then up the right side: counterclockwise as you look at the top.
2. Threshold is where a tiny test current begins. $R_{DS(on)}$ at 10 V says nothing reliable about 3.3 V. The FET may be only partly on, hot, and dropping voltage the motor needed. A logic-level part specifies on-resistance at a low $V_{GS}$, and the capstone still uses a driver.
3. $P = (12 - 5) \times 0.3 = 2.1\ \mathrm{W}$. Yes. A bare TO-220 at 2 W is a burn, and it is still the wrong way to power an ESP32.
4. Base current on that rule is about $0.8 / 10 = 80\ \mathrm{mA}$, far above a 12 mA to 40 mA GPIO. The motor also needs a flyback diode. Use the TB6612.
5. An unmarked TO-92 might be a bipolar transistor, a MOSFET, or something else. Installing it in place of an AMS1117 can short 5 V onto the 3.3 V rail. An unknown part is not a regulator until the marking says so.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;p&gt;SparkFun’s transistor tutorial is the bipolar and MOSFET picture without a homemade motor stage: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/transistors&quot;&gt;Transistors&lt;/a&gt;. The 7805 family, including the dropout you just turned into heat, is documented on TI’s product page: &lt;a href=&quot;https://www.ti.com/product/LM7805&quot;&gt;LM7805&lt;/a&gt;. Read the marking on your part before you borrow either pinout.&lt;/p&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;You do not need a loose transistor to finish the diff-drive robot. The switch is the TB6612 already on the cart in &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;. Buy the driver, the ESP32 or Pico, and the motors there, not a grab-bag of TO-92 parts.&lt;/p&gt;

&lt;p&gt;If you want one known diode and one known bipolar transistor for the unpowered junction lab, buy them from a counter that prints the part number on the bag, such as &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; or &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt;. This lesson has no verified single-product slug for a 2N2222, so do not invent one. A search page, if you use a marketplace, should be treated as a search page: &lt;a href=&quot;https://shopee.vn/search?keyword=2N2222&quot;&gt;Shopee search for 2N2222&lt;/a&gt;. Read the marking when it arrives, and do not pay a fantasy price for an unlabeled strip. The ESP32 board you already need was 190 000 ₫ on Hshop on 23 September 2026 and already carries its own 3.3 V regulator: &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;ESP32 NodeMCU-32S&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Capacitors, diodes, and LEDs</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_04_cap_diode_led_id/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_04_cap_diode_led_id</id>
   <content type="html">&lt;p&gt;Why does an ESP32 sometimes reboot spontaneously the exact millisecond a DC motor stops spinning, even when the battery is fully charged? Two humble two-terminal parts — &lt;strong&gt;capacitors&lt;/strong&gt; and &lt;strong&gt;diodes&lt;/strong&gt; — are the silent bodyguards that stop that crash. A capacitor acts like a local water tank that supplies microsecond current spikes right beside an IC’s power pins, while a flyback diode acts like a one-way pressure-relief valve that safely redirects the inductive voltage kick from a collapsing motor magnetic field.&lt;/p&gt;

&lt;p&gt;This lesson pairs physical component identification with the differential equations that make capacitors, diodes, and LEDs indispensable on every robot board.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you will be able to point at the stripe on an aluminum electrolytic and say that it marks the negative lead, and you will treat a ceramic disc or chip capacitor as non-polar unless the body says otherwise. You will read a ceramic code such as 104 as 100 nF. You will name the cathode of a diode by its band, and you will check an LED with the meter’s diode mode instead of trusting the long lead alone. You will also explain why a 100 µF capacitor across a motor driver’s VM pin softens short spikes and still does not replace a separate motor supply for two TT motors.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can use $V = IR$ and you can size the series resistor for a red LED on 3.3 V. You know that diode mode belongs on an unpowered part, with the red lead out of the current jack. If the kit has not arrived, the photographs still carry the polarity rules. The lab is a diode test, not a powered experiment.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-energy-storage-in-electric-fields-semiconductor-junctions-and-inductive-flyback&quot;&gt;Key ideas: Energy Storage in Electric Fields, Semiconductor Junctions, and Inductive Flyback&lt;/h2&gt;

&lt;p&gt;Three physical mechanisms unite capacitors, diodes, and LEDs in robotics:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Capacitor Dynamics (\(i = C\frac{dv}{dt}\)):&lt;/strong&gt; Capacitors oppose abrupt changes in &lt;em&gt;voltage&lt;/em&gt;. Placing a \(0.1\,\mu\text{F}\) ceramic decoupling capacitor beside an IC filters high-frequency switching noise, while a \(100\text{–}470\,\mu\text{F}\) electrolytic bulk capacitor absorbs millisecond motor startup dips.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;PN-Junction Rectification &amp;amp; Forward Drop (\(V_f\)):&lt;/strong&gt; Diodes conduct easily in one direction once forward voltage exceeds \(V_f\) (\(\approx 0.7\text{ V}\) for silicon &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1N4007&lt;/code&gt;, \(\approx 0.3\text{ V}\) for Schottky &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1N5819&lt;/code&gt;, and \(1.8\text{–}3.2\text{ V}\) for LEDs) and block reverse current up to their reverse breakdown rating.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Inductive Flyback Protection (\(v = L\frac{di}{dt}\)):&lt;/strong&gt; A spinning motor coil stores magnetic energy. Switching off its current abruptly makes \(di/dt\) huge and negative, generating a high-voltage spike that destroys MOSFETs unless a &lt;strong&gt;flyback (freewheeling) diode&lt;/strong&gt; gives the inductive current a safe recirculating loop.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-on-the-diff-drive-robot&quot;&gt;Why this matters on the diff-drive robot&lt;/h2&gt;

&lt;p&gt;The ESP32 or Pico runs at 3.3 V. The TB6612 takes motor power on VM, about 4.5 V to 10 V, and logic on VCC that is happy at 3.3 V. A 100 µF electrolytic across VM is a local reservoir for PWM spikes from the TT motors. It is not the battery. The energy in 100 µF is gone in a fraction of a millisecond at stall current, so the pack still has to exist, and it still must not be wired to a GPIO.&lt;/p&gt;

&lt;p&gt;The status lamp is an LED plus a resistor. A flyback diode, later, is a 1N4007-class part with the band toward the positive motor terminal. An electrolytic backwards on a live pack can vent. This lesson is how you tell capacitors, diodes, and LEDs apart before any of those wires are hot.&lt;/p&gt;

&lt;h2 id=&quot;capacitors-stripe-code-and-what-100-µf-can-actually-do&quot;&gt;Capacitors: stripe, code, and what 100 µF can actually do&lt;/h2&gt;

&lt;p&gt;An aluminum electrolytic is polarized. The stripe down one side, usually with minus signs in it, marks the negative lead. On a single-supply rail that lead goes to ground and the other lead goes to the rail you are stiffening. The voltage rating on the can must sit above the highest voltage that rail will see. A 2S pack is about 8.4 V full, so 16 V or 25 V is comfortable and 6.3 V is not.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/capacitor_polarity.png&quot; alt=&quot;Electrolytic polarity: the stripe is the negative lead&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Electrolytic_capacitor.jpg&quot; alt=&quot;An aluminum electrolytic, stripe marking the negative side&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Ceramic capacitors in this course are usually non-polar. A disc with no stripe, or a small brown SMD rectangle with no printed polarity mark, may go in either way. Do not invent a polarity for it because the electrolytic next to it had one. Some tantalum capacitors are polarized and use a different mark; if you did not buy a tantalum, do not assume every beige rectangle is one.&lt;/p&gt;

&lt;p&gt;Through-hole ceramics often use a three-digit code in picofarads. The first two digits are significant figures and the third is a power of ten, the same pattern as an SMD resistor, but the unit is pF. A part marked 104 is&lt;/p&gt;

\[10 \times 10^{4}\ \mathrm{pF} = 100000\ \mathrm{pF} = 100\ \mathrm{nF} = 0.1\ \mu\mathrm{F}\]

&lt;p&gt;A 103 is 10 nF. The code is not the capacitance in nanofarads. Electrolytics usually print the value in plain text, such as 100 µF 16 V, because 100 µF would be an absurd picofarad code.&lt;/p&gt;

&lt;p&gt;Here is the motor-rail number that keeps the capacitor honest. Energy in a capacitor is&lt;/p&gt;

\[E = \frac{1}{2} C V^2\]

&lt;p&gt;For $C = 100\ \mu\mathrm{F} = 100 \times 10^{-6}\ \mathrm{F}$ at a 7.4 V nominal pack,&lt;/p&gt;

\[E = \frac{1}{2} \times 100 \times 10^{-6} \times (7.4)^2 \approx 0.0027\ \mathrm{J}\]

&lt;p&gt;A single stalled TT motor near 0.8 A at 7.4 V is about 6 W. The time that 100 µF can supply that power is roughly $0.0027 / 6 \approx 0.5\ \mathrm{ms}$. Half a millisecond of stall energy is useful against a PWM edge and against the inductance of long battery leads. It is not a power source for wheels. Two motors make the reservoir even smaller relative to the load. Put the 100 µF across VM and GND, stripe to GND, and still give the TB6612 a real pack. Do not hang that pack on the Pico or ESP32 3V3 pin and call the capacitor a solution.&lt;/p&gt;

&lt;h2 id=&quot;diodes-and-leds&quot;&gt;Diodes and LEDs&lt;/h2&gt;

&lt;p&gt;A silicon diode conducts easily one way and blocks the other. The band on a 1N4007 marks the cathode, the end conventional current leaves when the diode is forward biased. The other end is the anode. A red LED plays the same role in a lamp, with a forward drop near 2 V instead of about 0.7 V.&lt;/p&gt;

&lt;p&gt;An LED’s longer lead is often the anode, and the flat on the rim is often the cathode. “Often” is the whole problem. Cheap assortments are cut to length, and a previous student may already have trimmed the leads. Trust the meter. Diode mode, on an unpowered part, shows a forward drop in one direction and an open circuit the other way. For a silicon diode that drop is roughly 0.5 V to 0.7 V. For a red LED it is often near 1.6 V to 2.0 V, and the LED may glow faintly because the meter is pushing a small test current through it. If both directions read like a short, the part is dead. If both read open, it is dead or it is not a diode.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/LEDs.jpg&quot; alt=&quot;LEDs of several colors; lead length is a hint, not a guarantee&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/component_lookalikes.png&quot; alt=&quot;Packages that look alike until you read the marks&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A blank brown SMD rectangle is often a capacitor; a rectangle with a three-digit code is often a resistor. Verify. The status LED is the one in series with about 180 Ω from 3.3 V. A flyback diode beside a bare transistor has its band at the positive end of the motor, so the current that wants to keep flowing when the switch opens does not go through the transistor. The TB6612 already includes that path. You still need to recognize a 1N4007 when it falls out of the kit.&lt;/p&gt;

&lt;h2 id=&quot;worked-example-diode-mode-readings-you-should-expect&quot;&gt;Worked example: diode-mode readings you should expect&lt;/h2&gt;

&lt;p&gt;Take an unpowered 1N4007 and a red LED, out of circuit. Diode mode applies a small current and displays the voltage it needed.&lt;/p&gt;

&lt;p&gt;One way on the 1N4007 you might see about 0.62 V. Swap the probes and the display should show over-range, often “OL” or a blank of the same kind. The band end was the cathode: the reading appeared when the red probe was on the anode (the end without the band) and the black probe was on the band. Conventional current in diode test leaves the red probe.&lt;/p&gt;

&lt;p&gt;One way on the red LED you might see about 1.8 V and a faint glow. The other way is over-range. The lead that was under the red probe during the 1.8 V reading is the anode. If that lead is also the longer one, the manufacturer and the meter agree. If they disagree, believe the meter and mark the anode with a pen before you trim the leads.&lt;/p&gt;

&lt;p&gt;The LED wants a series resistor because its voltage barely moves while current runs away. The capacitor’s voltage does move, which is why $E = \tfrac{1}{2}CV^2$ is the right question for the 100 µF part. Neither part is a motor you may hang on a GPIO.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;p&gt;This lab stays unpowered on purpose. You will not reverse an electrolytic on a live rail.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Collect one electrolytic, one ceramic marked 104 if you have it, one 1N4007 or any banded diode, and one LED. Leave every battery and USB cable unplugged.&lt;/li&gt;
  &lt;li&gt;On the electrolytic, find the stripe. Write down which lead is negative. Read the voltage rating. If it is below 16 V, it is a poor choice for an 8.4 V pack even though this lab will not apply that pack.&lt;/li&gt;
  &lt;li&gt;If the ceramic says 104, write $10 \times 10^4\ \mathrm{pF} = 100\ \mathrm{nF}$ before you look it up.&lt;/li&gt;
  &lt;li&gt;Diode mode, red lead in the voltage/ohms jack, black in COM. Probe the 1N4007 both ways. Record which orientation shows a voltage near 0.5 V to 0.7 V, and confirm that orientation puts black on the band.&lt;/li&gt;
  &lt;li&gt;Probe the LED both ways. Record the forward reading and which lead was under the red probe. Compare with lead length and with the flat, and note any disagreement.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should see one direction of each diode conduct and the other block. The LED may glow faintly in the conducting direction. The electrolytic is identified by eye in this lab; you do not charge it and you do not measure its capacitance unless your meter has a capacitance range and the part is out of circuit.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to change&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Both directions of the diode read OL&lt;/td&gt;
      &lt;td&gt;Probes not making contact, or the part is open&lt;/td&gt;
      &lt;td&gt;Reclean the leads and retry. If it stays open, set it aside.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Both directions read near 0 V&lt;/td&gt;
      &lt;td&gt;The part is shorted, or you are still on continuity across a wire&lt;/td&gt;
      &lt;td&gt;Check the dial. A healthy diode is not a beep both ways.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LED lights in diode mode but the long lead was the cathode&lt;/td&gt;
      &lt;td&gt;The leads were trimmed, or the batch is odd&lt;/td&gt;
      &lt;td&gt;Mark the anode from the meter. Do not argue with a faint glow.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You are tempted to “see what reverse bias does” on the electrolytic&lt;/td&gt;
      &lt;td&gt;That is the failure this lab refuses to demonstrate&lt;/td&gt;
      &lt;td&gt;Stop. Read the note below. Do not power it backwards.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A reversed aluminum electrolytic on a live rail does not sit there politely. The oxide film breaks down, leakage current heats the electrolyte, pressure rises, and the scored vent on the can opens or the can ruptures. The failed part often becomes a short. On a motor pack that short can dump the cells. We describe it so you can recognize a vented can. We do not build it.&lt;/p&gt;

&lt;p&gt;Safety: diode mode only, parts out of circuit, USB unplugged, no VM connected. Do not discharge a large capacitor by shorting it with your fingers or with a probe tip you are holding. The 100 µF parts in this kit are small; the habit still matters when a later board is not.&lt;/p&gt;

&lt;h2 id=&quot;explanation-rc-time-constants-and-sizing-a-bulk-decoupling-capacitor-by-hand&quot;&gt;Explanation: RC Time Constants and Sizing a Bulk Decoupling Capacitor by Hand&lt;/h2&gt;

&lt;p&gt;How large does a bulk capacitor need to be to keep a 5 V logic rail from dipping during a brief motor surge? Walk through the exact 4-step physics calculation:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Use the capacitor charge equation:&lt;/strong&gt; \(\Delta V = \frac{I_{\text{pulse}} \cdot \Delta t}{C}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Solve for minimum capacitance \(C\):&lt;/strong&gt; Given a transient current deficit \(I_{\text{pulse}}\) lasting \(\Delta t\) and maximum tolerable voltage droop \(\Delta V_{\max}\), we need \(C \ge \frac{I_{\text{pulse}} \Delta t}{\Delta V_{\max}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check the RC time constant \(\tau = RC\):&lt;/strong&gt; For a simple RC debounce or low-pass filter, the voltage reaches \(63.2\%\) of its final value after \(1\tau\) and \(99.3\%\) after \(5\tau\), with cutoff frequency \(f_c = \frac{1}{2\pi R C}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify voltage rating and polarity:&lt;/strong&gt; Electrolytic capacitors must have a voltage rating at least \(1.5\times\text{–}2\times\) the rail voltage (e.g., 16 V cap on a 7.4 V rail) and strictly match &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+/-&lt;/code&gt; polarity.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-bulk-capacitor-droop--rc-low-pass-filter-calculation&quot;&gt;Concrete Worked Example: Bulk Capacitor Droop &amp;amp; RC Low-Pass Filter Calculation&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Part A — Hold-up capacitance during a 2 ms, 0.5 A current transient:&lt;/strong&gt;
Suppose your 5 V logic rail can tolerate at most \(\Delta V_{\max} = 0.25\text{ V}\) droop while the buck converter responds over \(\Delta t = 2\text{ ms} = 0.002\text{ s}\) to a \(I_{\text{pulse}} = 0.5\text{ A}\) spike:
\(C \ge \frac{0.5\text{ A} \times 0.002\text{ s}}{0.25\text{ V}} = \frac{0.001}{0.25} = 0.004\text{ F} = 4000\,\mu\text{F}\)
(or for a shorter \(200\,\mu\text{s}\) switching transient, \(C \ge 400\,\mu\text{F}\) — which is why a \(470\,\mu\text{F}\) bulk electrolytic cap sits on motor driver boards!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Part B — RC button-debounce filter with \(R = 10\text{ k}\Omega\) and \(C = 1\,\mu\text{F}\):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Time constant: \(\tau = R C = 10^4\,\Omega \times 10^{-6}\text{ F} = 0.01\text{ s} = 10\text{ ms}\).&lt;/li&gt;
      &lt;li&gt;Cutoff frequency: \(f_c = \frac{1}{2\pi (0.01)} \approx 15.9\text{ Hz}\) — cleanly smooths out 1–5 ms mechanical switch bounce while responding crisply to human button presses.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Wiring a flyback diode &lt;em&gt;forward-biased&lt;/em&gt; across a motor supply (creating a dead short) or wiring an electrolytic capacitor backward (heating and venting electrolyte). Remember: the stripe on a diode marks the &lt;strong&gt;cathode&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;K&lt;/code&gt;), which goes toward &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V+&lt;/code&gt; for a flyback diode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; Decode ceramic capacitor code &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;104&lt;/code&gt;: \(10 \times 10^4\text{ pF} = 100\,000\text{ pF} = 100\text{ nF} = 0.1\,\mu\text{F}\) — the universal decoupling capacitor on almost every digital IC.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A ceramic is marked 104 and another is marked 223. Give both values in nF.&lt;/li&gt;
  &lt;li&gt;A 100 µF capacitor sits at 7.4 V. Compute the stored energy. How long could that energy run a 6 W stall, at least as a rough $E/P$ estimate?&lt;/li&gt;
  &lt;li&gt;Diode mode shows 0.58 V on a 1N4007 with black on the banded end, and OL when the probes swap. Which end is the cathode, and which probe was on the anode during the 0.58 V reading?&lt;/li&gt;
  &lt;li&gt;An LED assortment has had every lead cut to the same length. Describe the unpowered test that still finds the anode, and say what you would refuse to do in order to “make the test more obvious.”&lt;/li&gt;
  &lt;li&gt;A classmate puts the 100 µF electrolytic across the ESP32 3V3 pin and GND, stripe correct, and then connects two TT motors to that same 3V3 pin because “the capacitor will supply the stall.” What is wrong with the energy argument, and what should VM be connected to instead?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. 104 is $10 \times 10^4\ \mathrm{pF} = 100\ \mathrm{nF}$. 223 is $22 \times 10^3\ \mathrm{pF} = 22\ \mathrm{nF}$.
2. $E = \tfrac{1}{2} \times 100 \times 10^{-6} \times 7.4^2 \approx 0.0027\ \mathrm{J}$. Time is about $0.0027 / 6 \approx 0.45\ \mathrm{ms}$. That is a spike, not a cruise.
3. The banded end is the cathode, because black was on it during the forward reading. The red probe was on the anode.
4. Unpowered diode mode. The anode is the lead under the red probe when you see a forward drop and, often, a faint glow. Do not apply 5 V or a GPIO with no series resistor to make the glow brighter.
5. At stall the capacitor holds well under a millisecond of energy, so the 3V3 regulator is still asked for about an ampere and will brown out or fail. VM on the TB6612 should see the motor pack, in the module&apos;s 4.5 V to 10 V range, with ground shared and the GPIO used only as logic.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;p&gt;SparkFun’s capacitor note is the polarity and the code in a shorter page: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/capacitors&quot;&gt;Capacitors&lt;/a&gt;. The diode page covers the band and forward drop: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/diodes&quot;&gt;Diodes&lt;/a&gt;. The LED page is the same one the resistor lesson already earned: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds&quot;&gt;Light-emitting diodes&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The full cart, including the microcontroller, the TB6612, and the TT motors that justify the 100 µF story, is in &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;. Buy those there. This lesson does not add another driver.&lt;/p&gt;

&lt;p&gt;If the LED assortment is not already in the cart, Hshop listed a 3 mm pack at 20 000 ₫ on 23 September 2026: &lt;a href=&quot;https://hshop.vn/bo-5-loai-led-sieu-sang-3mm-thong-dung-5-kind-3mm-transparent-color-led&quot;&gt;bộ 5 loại LED 3 mm&lt;/a&gt;. Confirm the live price.&lt;/p&gt;

&lt;p&gt;A 1N4007 and a 100 µF, 16 V or 25 V electrolytic were not a single verified Hshop product link in this lesson’s source list. Use search pages and read the voltage rating yourself: &lt;a href=&quot;https://shopee.vn/search?keyword=1N4007&quot;&gt;Shopee search for 1N4007&lt;/a&gt; and &lt;a href=&quot;https://shopee.vn/search?keyword=t%E1%BB%A5%20100uF&quot;&gt;Shopee search for tụ 100uF&lt;/a&gt;. Both of those are search pages, not products. &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; and &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; are component counters if you would rather buy two loose parts than a kit. Do not power any of them until the stripe and the band have been written down.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Resistor identification: color bands and SMD codes</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_03_resistor_color_smd/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_03_resistor_color_smd</id>
   <content type="html">&lt;p&gt;In 1920s radio factories, tiny cylindrical resistors had no room for printed text, so engineers invented a color-coded language that could be read from any angle even after a part was soldered onto a chassis. Nearly a century later, through-hole color bands and microscopic 3-digit SMD markings still sit on every robot PCB and pull-up network — and misreading a single multiplier band (picking \(100\text{ k}\Omega\) instead of \(10\text{ k}\Omega\), or \(10\,\Omega\) instead of \(1\text{ k}\Omega\)) can silently break an I2C bus or burn out an LED.&lt;/p&gt;

&lt;p&gt;This lesson makes resistor decoding second nature and connects tolerance bands to real circuit margins.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you will be able to read a four-band resistor as digit, digit, multiplier, and tolerance, and compute the window the tolerance allows. You will decode brown-black-red-gold as 1 kΩ ±5% and brown-blue-orange-gold as 16 kΩ ±5% without looking up the arithmetic a second time. You will recognize a five-band part as three digits plus a multiplier, read a three-digit SMD code such as 103 or 472, and treat an R in a code such as R22 as a decimal point. You will confirm a decoded value with an unpowered meter and know when probe resistance, not the part, is what you are seeing.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You need Ohm’s law from the first lesson, especially $I = V/R$ and the reason a status LED uses about 180 Ω rather than whatever cylinder was nearest the iron. You need the meter’s ohms habit from the previous lesson: unpowered part, red lead in the voltage/ohms jack, not the current jack. No SMD soldering is required. If your kit is all through-hole, the SMD section is still worth reading, because the same codes are printed on the small parts already soldered to an ESP32 board and a TB6612 module.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-scientific-notation-encoding-e-series-values-and-tolerance-propagation&quot;&gt;Key ideas: Scientific Notation Encoding, E-Series Values, and Tolerance Propagation&lt;/h2&gt;

&lt;p&gt;Behind resistor markings lies an elegant engineering numbering system:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Positional Scientific Notation:&lt;/strong&gt; Both 4-band/5-band color codes and 3-digit/4-digit SMD codes encode \(R = (\text{mantissa}) \times 10^{\text{exponent}}\). The final digit or band is always the power of 10 (number of trailing zeros), while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; in SMD codes (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4R7&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0R1&lt;/code&gt;) acts as the decimal point.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Logarithmic E-Series (E12, E24, E96):&lt;/strong&gt; Rather than manufacturing every integer ohm, industry spaces standard values geometrically (approximately \(10^{k/12}\) for E12: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82&lt;/code&gt;) so adjacent values overlap their tolerance windows with minimal inventory.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tolerance &amp;amp; Worst-Case Circuit Analysis:&lt;/strong&gt; A \(\pm 5\%\) (gold) or \(\pm 1\%\) (brown) tolerance means a nominal \(10\text{ k}\Omega\) part can legally sit anywhere in \([9.5\text{ k}\Omega, 10.5\text{ k}\Omega]\) or \([9.9\text{ k}\Omega, 10.1\text{ k}\Omega]\). In precision battery voltage dividers for ADC sensing, 1% resistors keep state-of-charge errors small.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-on-the-diff-drive-robot&quot;&gt;Why this matters on the diff-drive robot&lt;/h2&gt;

&lt;p&gt;The 3.3 V lamp on the ESP32 or Pico only works if the cylinder you just inserted is actually near 180 Ω. Brown-black-orange is 10 kΩ, not 1 kΩ, and the lamp then runs at $I = 1.3 / 10000 = 0.13\ \mathrm{mA}$, which looks like a dead GPIO in a lit room. The opposite mistake, a few ohms where you wanted hundreds, dumps the pin.&lt;/p&gt;

&lt;p&gt;The same skill shows up off the breadboard. Pull-ups on a button, the series resistor you may later use to tame an HC-SR04 echo, and the tiny parts on a motor-driver module are all resistors with either bands or a printed code. The TB6612 does not become safer because the resistor “looks red.” Two TT motors still must not be treated as resistors on a GPIO; identifying a 10 Ω shunt is not an invitation to build one. Read the part, then decide whether it belongs in the circuit at all.&lt;/p&gt;

&lt;h2 id=&quot;four-bands-then-five&quot;&gt;Four bands, then five&lt;/h2&gt;

&lt;p&gt;A standard four-band resistor is read from the end closest to the bands, with the tolerance band — almost always gold or silver on a hobby part — kept on the right. The first two bands are significant digits. The third is a multiplier, a power of ten. The fourth is the tolerance, how far the real resistance may sit from the painted number.&lt;/p&gt;

&lt;p&gt;The digit colors used in this course are: black 0, brown 1, red 2, orange 3, yellow 4, green 5, blue 6, violet 7, grey 8, white 9. The multiplier uses the same colors as powers of ten: black ×1, brown ×10, red ×100, orange ×1 000, yellow ×10 000. Gold as a multiplier means ×0.1, and as a tolerance band means ±5%. Silver as tolerance means ±10%.&lt;/p&gt;

&lt;p&gt;Brown, black, red, gold is the first number you should be able to say out loud. Brown is 1, black is 0, red means ×100:&lt;/p&gt;

\[10 \times 100 = 1000\ \Omega = 1\ \mathrm{k}\Omega\]

&lt;p&gt;Gold says ±5%, so the honest window is 950 Ω to 1050 Ω. A meter reading of 980 Ω is a good part, not a counterfeit.&lt;/p&gt;

&lt;p&gt;Brown, blue, orange, gold is the second. Brown is 1, blue is 6, orange means ×1 000:&lt;/p&gt;

\[16 \times 1000 = 16000\ \Omega = 16\ \mathrm{k}\Omega\]

&lt;p&gt;again ±5%, so about 15.2 kΩ to 16.8 kΩ. If you reverse the resistor and try to start from the gold band, the code becomes nonsense, because gold is not a digit.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/resistor_color_code.png&quot; alt=&quot;Four-band color code, tolerance band on the right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Five-band resistors add a third significant digit before the multiplier, and the last band is still the tolerance. Brown-black-black-red-brown is 1, 0, 0, ×100, ±1%:&lt;/p&gt;

\[100 \times 100 = 10\ \mathrm{k}\Omega\]

&lt;p&gt;within about ±1%. You do not need a drawer full of five-band parts to finish the robot. You do need to notice when a resistor has four color bands of digits and a fifth tolerance band, because reading it as a four-band code shifts the multiplier by a factor of ten. Count the bands before you multiply.&lt;/p&gt;

&lt;h2 id=&quot;smd-codes-including-the-letter-you-do-not-memorize&quot;&gt;SMD codes, including the letter you do not memorize&lt;/h2&gt;

&lt;p&gt;A three-digit SMD resistor code is the same idea printed in ink. The first two digits are the significant figures. The third is the power of ten. So 103 is $10 \times 10^3 = 10\ \mathrm{k}\Omega$, and 472 is $47 \times 10^2 = 4700\ \Omega = 4.7\ \mathrm{k}\Omega$. A code of 100 is $10 \times 10^0 = 10\ \Omega$, not 100 Ω. The trailing zero is an exponent, not another digit to append.&lt;/p&gt;

&lt;p&gt;When the value is under 10 Ω the letter R stands in for the decimal point. R22 means 0.22 Ω. 4R7 means 4.7 Ω. That R is easy to miss on a dark body, and it matters: a 0.22 Ω part used as an LED resistor from 3.3 V is almost a short.&lt;/p&gt;

&lt;p&gt;Four-digit codes extend the same rule to three significant figures: 4701 is $470 \times 10^1 = 4.7\ \mathrm{k}\Omega$. The EIA-96 system is different. It prints two digits plus a letter, and the digits are an index into a table rather than the resistance itself. You will see it on 1% SMD parts. Do not memorize the table. If the code is two digits and a letter, look it up or measure the part. Guessing turns an index of “01” into a one-ohm resistor that is not one ohm.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/resistors_assortment.jpg&quot; alt=&quot;A mixed assortment: bands, bodies, and wattage are not the same thing&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Body size is a hint about power, not about resistance. A 1/4 W through-hole cylinder is the default in this chapter. A physically larger ceramic cylinder may be 1 W or more and may use a different band layout. The LED calculation from the first lesson dissipated about 0.011 W, so 1/4 W is comfortable there. It does not make a 10 Ω, 1/4 W part a safe dummy load for a stalled motor.&lt;/p&gt;

&lt;h2 id=&quot;worked-example-five-codes-then-the-meter-window&quot;&gt;Worked example: five codes, then the meter window&lt;/h2&gt;

&lt;p&gt;Decode these before you touch the dial.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Brown, black, red, gold → $10 \times 100 = 1\ \mathrm{k}\Omega$, ±5%, window 950 Ω to 1050 Ω.&lt;/li&gt;
  &lt;li&gt;Brown, blue, orange, gold → $16 \times 1000 = 16\ \mathrm{k}\Omega$, ±5%, window 15.2 kΩ to 16.8 kΩ.&lt;/li&gt;
  &lt;li&gt;Red, red, brown, gold → $22 \times 10 = 220\ \Omega$, ±5%, window about 209 Ω to 231 Ω. This is the substitute status-lamp resistor when 180 Ω is missing.&lt;/li&gt;
  &lt;li&gt;SMD 103 → $10 \times 10^3 = 10\ \mathrm{k}\Omega$.&lt;/li&gt;
  &lt;li&gt;SMD R22 → 0.22 Ω. The R is the decimal point, not a color.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now put number 3 in the lamp formula from lesson 1, $V_f = 2.0\ \mathrm{V}$ on a 3.3 V rail:&lt;/p&gt;

\[I = \frac{3.3 - 2.0}{220} = \frac{1.3}{220} \approx 5.9\ \mathrm{mA}\]

&lt;p&gt;Power in that resistor is $P = I^2 R \approx (0.0059)^2 \times 220 \approx 0.008\ \mathrm{W}$, still a small fraction of 1/4 W. The same formula with a misread brown-black-orange (10 kΩ) gives 0.13 mA and a lamp you will call dead. The same formula with R22 gives $I = 1.3 / 0.22 \approx 6\ \mathrm{A}$, which neither the LED nor a GPIO can survive. The code is not a suggestion.&lt;/p&gt;

&lt;p&gt;On the meter, expect a 5% part to fall inside its window. A reading 2% low is still a good part. Two effects fool students on the low end. Finger heat on a film resistor shifts the value a little while you pinch the body; hold the leads, not the capsule, or let go and wait a few seconds. On a part under a few ohms, the probes and leads themselves are a few tenths of an ohm. Short the probes, note that offset, and subtract it before you accuse an R22 of being the wrong code. Do not “zero” the meter by measuring a resistor that is still soldered into a live board.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Pick five resistors from the kit, or use the five codes in the worked example if the kit has not arrived. Write the band or SMD reading and the ohms you claim, including the ± window for banded parts.&lt;/li&gt;
  &lt;li&gt;Power is off. Nothing is plugged into the resistor. Red lead in the voltage/ohms jack, black in COM, dial on ohms. If the meter is manual-ranging, start on a range above the value you expect.&lt;/li&gt;
  &lt;li&gt;Measure each part. Write the meter reading next to the decoded value. A 5% part should land inside its window. If it does not, check that you did not start from the gold band, and check that you did not count a five-band part as four.&lt;/li&gt;
  &lt;li&gt;For any part under about 10 Ω, short the probes first, record that resistance, and subtract.&lt;/li&gt;
  &lt;li&gt;Optional: place the 180 Ω or 220 Ω part back into the unpowered LED loop from lesson 1 and measure it again in circuit only with the supply unplugged. Then say whether neighboring parts could be in parallel with it. If you are not sure, lift one lead.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should see meter readings inside the tolerance window for intact, correctly read parts. A 1 kΩ ±5% part at 1.02 kΩ is a pass. Continuity mode will probably stay silent on all five, which is not a failure of the resistors.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to change&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Meter reads about 10× or 0.1× your decode&lt;/td&gt;
      &lt;td&gt;Bands read from the wrong end, or a five-band part read as four&lt;/td&gt;
      &lt;td&gt;Put gold or silver on the right and count the bands again.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A “1 kΩ” part reads near 10 kΩ&lt;/td&gt;
      &lt;td&gt;Multiplier band read as red when it is orange, or the reverse&lt;/td&gt;
      &lt;td&gt;Check the third band under a lamp. Orange and red are the classic mix-up.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Low-ohm part reads 0.4 Ω high, every time&lt;/td&gt;
      &lt;td&gt;Probe and lead resistance&lt;/td&gt;
      &lt;td&gt;Subtract the shorted-probe reading.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ohms reading wanders while the board is powered&lt;/td&gt;
      &lt;td&gt;Supply still connected&lt;/td&gt;
      &lt;td&gt;Unplug. Ohms mode is not for a live rail.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Continuity is silent on a 220 Ω part&lt;/td&gt;
      &lt;td&gt;Continuity is not the ohms range&lt;/td&gt;
      &lt;td&gt;Switch to Ω. Silence here is expected.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Safety: never measure ohms on a powered rail, a battery, or the VM pin of a motor driver. A 2S pack is not a resistor. Keep the red lead out of the current jack so a later voltage check on the 3.3 V pin is not a short.&lt;/p&gt;

&lt;h2 id=&quot;explanation-decoding-both-color-bands-and-smd-codes--plus-worst-case-divider-drift&quot;&gt;Explanation: Decoding Both Color Bands and SMD Codes — Plus Worst-Case Divider Drift&lt;/h2&gt;

&lt;p&gt;Follow this 4-step decoding habit (and always confirm with your multimeter when bands look ambiguous):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Locate the tolerance band first:&lt;/strong&gt; Usually gold (\(\pm 5\%\)) or brown (\(\pm 1\%\)) slightly separated on the right end.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Read significant digits left-to-right:&lt;/strong&gt; 2 digits for 4-band, 3 digits for 5-band.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Apply the multiplier \(10^m\):&lt;/strong&gt; Black=0, Brown=1, Red=2, Orange=3, Yellow=4, Green=5, Blue=6.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute worst-case bounds:&lt;/strong&gt; Multiply by \((1 \pm \text{tol})\) to verify your circuit still meets voltage or current limits at the extremes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-decoding-three-parts-and-computing-worst-case-adc-voltage-error&quot;&gt;Concrete Worked Example: Decoding Three Parts and Computing Worst-Case ADC Voltage Error&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Part 1 (4-band: Brown–Black–Red–Gold):&lt;/strong&gt;
Digits &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;; multiplier \(10^2\); tolerance \(\pm 5\%\):
\(R = 10 \times 10^2 = 1\,000\,\Omega = 1\text{ k}\Omega \;\; (\text{range: } 950\,\Omega \text{ to } 1\,050\,\Omega).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Part 2 (5-band: Yellow–Violet–Black–Brown–Brown):&lt;/strong&gt;
Digits &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;; multiplier \(10^1\); tolerance \(\pm 1\%\):
\(R = 470 \times 10^1 = 4\,700\,\Omega = 4.7\text{ k}\Omega \;\; (\text{range: } 4\,653\,\Omega \text{ to } 4\,747\,\Omega).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Part 3 (SMD codes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;472&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4R7&lt;/code&gt;):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;103&lt;/code&gt; \(= 10 \times 10^3 = 10\text{ k}\Omega\).&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;472&lt;/code&gt; \(= 47 \times 10^2 = 4.7\text{ k}\Omega\).&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4R7&lt;/code&gt; \(= 4.7\,\Omega\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Worst-case check on a 2:1 battery divider (\(R_1 = R_2 = 10\text{ k}\Omega \pm 5\%\), \(V_{\text{bat}} = 8.00\text{ V}\)):&lt;/strong&gt;
Nominal \(V_{\text{adc}} = 4.00\text{ V}\). Worst-case high occurs when \(R_1\) is \(-5\%\) (\(9.5\text{ k}\Omega\)) and \(R_2\) is \(+5\%\) (\(10.5\text{ k}\Omega\)):
\(V_{\text{adc,max}} = 8.00 \times \frac{10.5}{9.5 + 10.5} = 8.00 \times 0.525 = 4.20\text{ V} \quad (+5\% \text{ error!}).\)
Switching to \(\pm 1\%\) resistors narrows that worst-case error to \(\pm 1\%\) (\(4.04\text{ V}\)).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Reading a 5-band resistor backward (e.g., mistaking Brown–Black–Black–Red–Brown &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 kΩ 1%&lt;/code&gt; for Brown–Red–Black–Black–Brown &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;120 Ω 1%&lt;/code&gt;). When in doubt, spend 3 seconds measuring it with an ohmmeter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; What does SMD code &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;000&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; mean? It is a &lt;strong&gt;0 Ω jumper resistor&lt;/strong&gt; used as a bridge or configuration link on PCBs.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Decode yellow-violet-brown-gold, orange-orange-red-gold, and blue-grey-black-gold. Give each nominal value and the ±5% window.&lt;/li&gt;
  &lt;li&gt;Someone reads a resistor as gold-orange-violet-red. What did they do, and what is the legal four-band reading if those colors are red, violet, orange, gold?&lt;/li&gt;
  &lt;li&gt;Decode SMD 222, 510, 4R7, and 1003. Which of them, if any, is 10 kΩ?&lt;/li&gt;
  &lt;li&gt;A 1 kΩ ±5% part measures 980 Ω on an unpowered meter. Keep it, or reject it? What percent error is 980 Ω from 1000 Ω?&lt;/li&gt;
  &lt;li&gt;A button pull-up on a 3.3 V GPIO was supposed to be 10 kΩ. The part fitted is brown-black-red-gold. Estimate the current when the button shorts the pin to ground, and say whether the pin is happier with this part or with a true 10 kΩ pull-up.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. Yellow-violet-brown-gold is $47 \times 10 = 470\ \Omega$, about 447 Ω to 494 Ω. Orange-orange-red-gold is $33 \times 100 = 3.3\ \mathrm{k}\Omega$, about 3.14 kΩ to 3.47 kΩ. Blue-grey-black-gold is $68 \times 1 = 68\ \Omega$, about 65 Ω to 71 Ω.
2. Gold was placed on the left. Gold is tolerance, so it belongs on the right. The legal reading is red-violet-orange-gold: $27 \times 1000 = 27\ \mathrm{k}\Omega$, ±5%.
3. 222 is $22 \times 10^2 = 2.2\ \mathrm{k}\Omega$. 510 is $51 \times 10^0 = 51\ \Omega$. 4R7 is 4.7 Ω. 1003 is $100 \times 10^3 = 100\ \mathrm{k}\Omega$. None of them is 10 kΩ. The three-digit code for 10 kΩ is 103.
4. 980 Ω sits inside 950 Ω to 1050 Ω. The error is $20/1000 = 2\%$, inside the 5% band. Keep it.
5. Brown-black-red-gold is 1 kΩ, so $I = 3.3 / 1000 = 3.3\ \mathrm{mA}$ while the button is held. A 10 kΩ pull-up would draw $3.3 / 10000 = 0.33\ \mathrm{mA}$. Both are inside a GPIO&apos;s rough 12 mA to 40 mA budget, and 10 kΩ is the kinder pull-up. The 1 kΩ part is a lamp-resistor cousin, not a better button resistor.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;p&gt;SparkFun’s resistor tutorial covers bands, power, and the series and parallel cases you already used for the LED: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/resistors&quot;&gt;Resistors&lt;/a&gt;. When a band is ambiguous under bad light, a calculator is faster than a second guess: &lt;a href=&quot;https://www.digikey.com/en/resources/conversion-calculators/conversion-calculator-resistor-color-code&quot;&gt;Digi-Key resistor color-code calculator&lt;/a&gt;. Use it to check yourself, then put the part on the meter anyway.&lt;/p&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The full cart is in &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;. This lesson adds a resistor assortment only.&lt;/p&gt;

&lt;p&gt;Hshop search for điện trở did not return a 1/4 W assortment when this page was written. Use a search page, not a guessed product link: &lt;a href=&quot;https://shopee.vn/search?keyword=b%E1%BB%99%20%C4%91i%E1%BB%87n%20tr%E1%BB%9F%201%2F4W&quot;&gt;Shopee search for bộ điện trở 1/4W&lt;/a&gt;. That URL is a search page. Read the listing for 1/4 W and for 5% or 1%, and prefer a kit that includes 180 Ω or 220 Ω plus 1 kΩ and 10 kΩ. Confirm the price on the day you order. Loose resistors from a component counter such as &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; or &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; are fine if the bag is labeled and you still measure five of them in the lab.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Breadboards, multimeters, and safe probing</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_02_breadboard_meter/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_02_breadboard_meter</id>
   <content type="html">&lt;p&gt;A solderless breadboard looks deceptively simple — a plastic grid of tiny holes — yet hidden inside are phosphor-bronze spring clips with milliohms of contact resistance and picofarads of parasitic capacitance. Knowing &lt;em&gt;how those hidden metal strips run&lt;/em&gt; and &lt;em&gt;how to probe them safely with a multimeter&lt;/em&gt; turns blind wire-wiggling into systematic fault isolation.&lt;/p&gt;

&lt;p&gt;This lesson demystifies breadboard topology and teaches the three core multimeter modes (voltage, continuity, resistance) as a structured diagnostic workflow.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you will be able to point to a five-hole terminal strip, the center trench, and a power rail, and say which holes are actually the same wire. You will predict that many full-size breadboards split each power rail in the middle, and you will prove the split with a continuity beep rather than with a guess. You will put the meter on DC volts in parallel, refuse to measure current through a USB charger in this lab, and measure resistance only on an unpowered part. You will also start every voltage reading on a range above the voltage you expect, with your fingers off the probe tips.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You should know a volt from an ampere, and that a status LED needs a series resistor. This lesson does not derive Ohm’s law again. It teaches which holes are one node, and which meter setting reads voltage rather than shorting the source. A multimeter and one solderless breadboard are enough.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-node-topology-continuity-testing-and-in-circuit-measurement-limits&quot;&gt;Key ideas: Node Topology, Continuity Testing, and In-Circuit Measurement Limits&lt;/h2&gt;

&lt;p&gt;Three electrical principles explain every breadboard mystery and multimeter quirk:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Electrical Nodes &amp;amp; Breadboard Topology:&lt;/strong&gt; Two points connected by an ideal conductor form the same electrical node (identical potential). On a standard breadboard, each 5-hole terminal row is one node, separated by the center DIP ravine, while outer power rails run lengthwise (sometimes split halfway!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Parallel vs. Series Measurement:&lt;/strong&gt; Voltage is a difference &lt;em&gt;between&lt;/em&gt; two nodes, so voltmeters connect &lt;strong&gt;in parallel&lt;/strong&gt; with the component. Current is flow &lt;em&gt;through&lt;/em&gt; a branch, so ammeters must break the branch and connect &lt;strong&gt;in series&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;In-Circuit Resistance Pitfalls:&lt;/strong&gt; An ohmmeter injects a small known test current and measures the resulting voltage. If the circuit is powered or if parallel paths exist around a resistor, the meter reads the parallel equivalent — or gets damaged by external voltage.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-on-the-diff-drive-robot&quot;&gt;Why this matters on the diff-drive robot&lt;/h2&gt;

&lt;p&gt;On the capstone the ESP32 or Pico, the TB6612, and the TT motor leads meet on a breadboard or on Dupont pins that obey one rule: five holes in a column are one node, and the trench is a wall. A rail split in the middle, a jumper that missed the spring, or a meter lead left in the current jack looks like a dead robot when the chip never received power. The TB6612 logic is 3.3 V from the microcontroller, while VM is a separate motor supply. If you “fix” a silent bridge by jumpering 5 V onto a GPIO, a measurement mistake becomes a damaged pin. The HC-SR04 echo is a 5 V output; you will not discover that safely by poking at random. This lesson is the beep and the voltage check, done while the robot is still parts in a tray.&lt;/p&gt;

&lt;h2 id=&quot;how-a-breadboard-is-actually-wired&quot;&gt;How a breadboard is actually wired&lt;/h2&gt;

&lt;p&gt;A solderless breadboard is a grid of spring clips under colored plastic. A lead pressed into a hole is tied to every other hole on the same clip. You do not get a gas-tight joint. A lead that is too short or dirty can look inserted and still be open, so the meter decides whether a node exists.&lt;/p&gt;

&lt;p&gt;The long rows along the edges are the power rails, usually marked red and blue or black. Every hole along one unbroken colored line is the same conductor. The top red rail is not the bottom red rail. On many full-size boards, including a typical 830-point board, the clips also stop at a gap in the middle, and the colored line is printed broken there. Feed 5 V into the left end and the right end stays dark until you jumper across that gap. Some mini boards have no gap. Measure yours.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/breadboard_anatomy.png&quot; alt=&quot;Breadboard anatomy: rails, five-hole strips, and the center trench&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The center field is the terminal strips. Each column has five holes above the trench and five holes below it. Those five holes are shorted vertically. The next column over is a different net. The trench running the long way down the middle is an insulator: a hole on the near side is not connected to the hole with the same column number on the far side. That gap exists so a DIP chip can straddle the trench without its own pins shorting together. If you probe “both sides of the same column” and expect a beep, you are asking the trench to be a wire.&lt;/p&gt;

&lt;p&gt;Landing both LED leads in the same five-hole column shorts the LED. Landing them on opposite sides of the trench, with nothing joining the sides, opens the circuit. Both mistakes look tidy.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/breadboard.jpg&quot; alt=&quot;A solderless breadboard, rails along the edges&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;what-the-meter-dial-is-actually-asking&quot;&gt;What the meter dial is actually asking&lt;/h2&gt;

&lt;p&gt;Three habits prevent almost every blown fuse in a student lab. The black lead lives in COM for every measurement in this course. The red lead lives in the jack marked V, Ω, or similar whenever you measure voltage, resistance, or continuity. The red lead moves to a current jack only for a current measurement, and it moves back when you are done. Leaving it in the current jack and then “measuring voltage” places the meter’s current shunt straight across the supply. That is nearly a short. The mA fuse opens. Voltage readings still work afterwards, which hides the damage until the day you need current.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Digital_Multimeter_Aka.jpg&quot; alt=&quot;A digital multimeter: dial, display, and lead jacks&quot; /&gt;&lt;/p&gt;

&lt;p&gt;DC voltage is measured in parallel. The circuit stays connected. You touch the two points whose difference you want. Swapped leads on a digital meter give a minus sign, not a broken part. The dial sits on a DC range above the voltage you expect: 20 V covers USB 5 V and a 2S pack near 8.4 V. An autoranging meter still needs the DC volts symbol, not ohms. You are looking across a part, not inserting yourself into the current.&lt;/p&gt;

&lt;p&gt;Current is measured in series. You break one wire so the current flows through the meter, and only after the red lead is in the current jack. This lesson does not measure current on a USB charger or on a GPIO. A charger will try to feed an amp or more into a meter placed across its output, and stall current from a TT motor can open a cheap mA fuse in one twitch. Resistance and continuity require an unpowered circuit, because the meter applies its own small test voltage. Continuity beeps on a wire or a closed spring. A 180 Ω resistor is silence on many continuity settings: continuity means “almost a short,” not “any connection.” Use ohms when you want the number.&lt;/p&gt;

&lt;h2 id=&quot;worked-example-the-rail-that-looks-powered-and-is-not&quot;&gt;Worked example: the rail that looks powered and is not&lt;/h2&gt;

&lt;p&gt;A USB charger is jumpered to the left-hand red and blue rails of an 830-point board. Nothing else is plugged in. No ESP32, no GPIO. The meter is on the 20 V DC range, black in COM, red in the voltage jack.&lt;/p&gt;

&lt;p&gt;On the left red rail, relative to the left blue rail, the meter reads 5.08 V. That is a healthy charger, a little above the nameplate 5 V, which is normal. Move the red probe to the right-hand red rail, past the printed break in the colored line, and keep black on the left blue rail. The meter reads 0.02 V. That is not “a bit of loss in the wire.” It is an open circuit. Set the charger aside, power everything down, and switch to continuity. Probing along the red rail across the middle gap is silence. One short jumper across the red gap, and another across the blue gap, and the right-hand rail then reads 5.07 V with the charger restored.&lt;/p&gt;

&lt;p&gt;The current jack is nearly a short, which is why this lab never uses it on that charger. A rough picture of a 10 A shunt, on the order of a hundredth of an ohm, gives&lt;/p&gt;

\[I \approx \frac{5}{0.01} = 500\ \mathrm{A}\]

&lt;p&gt;in a fantasy where the charger could supply it. A real charger folds back to an amp or two, which is already enough to open a 200 mA fuse and is not a measurement of “how much current the robot needs.” You learned in the previous lesson that two stalled TT motors want on the order of an ampere each. You will measure that later, in series, on the motor supply, for a short pulse, with the red lead in a jack rated for it. You will not learn it by shorting a USB port.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Unplug every supply. Seat black in COM and red in the voltage/ohms jack. Turn the dial to continuity.&lt;/li&gt;
  &lt;li&gt;Beep a jumper from metal end to metal end. You should hear a beep. Press one end into the red rail and the other into a five-hole row. Beep from another hole in that same row back to a second hole on the rail: beep. Beep from that row to the neighboring row: silence.&lt;/li&gt;
  &lt;li&gt;Beep across the center trench at the same column number. Expect silence. The trench is not a wire.&lt;/li&gt;
  &lt;li&gt;Find the rail break. Beep from a hole on the left half of the red rail to a hole on the right half. On a typical 830-point board this is silence, and the colored line is printed with a gap. Bridge the gap with one jumper and beep again. Expect a beep. Do the same for the blue rail.&lt;/li&gt;
  &lt;li&gt;Only then measure voltage. Use a USB phone charger or power bank, not a GPIO and not the 3V3 pin. If you have a USB breakout, land 5 V and GND on the rail you just proved. If you are probing a loose cable, use the charger end you understand, red on 5 V and black on GND. Dial on a DC range above 5 V, commonly 20 V. Read the display.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should hear a beep through a good jumper and through one row, silence across the trench, and silence across the middle of the rail until you add the bridge. The charger reading should sit near 4.7 V to 5.3 V. A reading near 0 V after continuity already beeped means you are not on the rail you think, or the charger is not the source you think.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to change&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;No beep through a jumper that looks seated&lt;/td&gt;
      &lt;td&gt;The pin missed the spring, or that clip is bent&lt;/td&gt;
      &lt;td&gt;Move to another hole in the same column. If the whole column is dead, abandon it.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Left rail reads about 5 V, right rail reads about 0 V&lt;/td&gt;
      &lt;td&gt;The middle break is open&lt;/td&gt;
      &lt;td&gt;Add one jumper across the red gap and one across the blue gap, then measure again.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Continuity beeps while the charger is still plugged in&lt;/td&gt;
      &lt;td&gt;You measured ohms or continuity on a live circuit&lt;/td&gt;
      &lt;td&gt;Unplug first. The ohms circuit is not a voltage meter.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Display flashes, then every mA reading is zero while DC volts still works&lt;/td&gt;
      &lt;td&gt;Red lead was in the current jack and got placed across the charger&lt;/td&gt;
      &lt;td&gt;The mA fuse opened. This lab does not replace that fuse by trying the same short again.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Safety: start on a voltage range above what you expect, so a 2S pack near 8 V is not first met on the 200 mV scale. Keep your fingers off the probe tips whenever you measure a pack or a charger; the tip is the only metal that should touch the circuit. Do not measure current on USB in this lesson. Do not land the charger on an ESP32 or Pico GPIO to “see if 5 V is there.”&lt;/p&gt;

&lt;h2 id=&quot;explanation-tracing-a-dead-breadboard-circuit-using-half-split-probing&quot;&gt;Explanation: Tracing a “Dead” Breadboard Circuit Using Half-Split Probing&lt;/h2&gt;

&lt;p&gt;When a breadboard circuit fails to power on, novices randomly pull wires out. Engineers use &lt;strong&gt;binary-search voltage probing&lt;/strong&gt; with the black probe anchored permanently to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Power-OFF continuity check first:&lt;/strong&gt; Verify no short between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V+&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; rails, and verify the split power rail (if any) is bridged.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Power-ON rail verification:&lt;/strong&gt; Measure &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V+&lt;/code&gt; right at the power supply, then at the far end of the breadboard rail. Any drop &amp;gt; 0.1 V signals a loose jumper or overloaded rail.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trace node by node downstream:&lt;/strong&gt; Follow the signal/power path from source to load; the exact boundary between “expected voltage” and “0 V” pinpoints the broken row or mis-seated pin.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Account for meter loading on megaohm circuits:&lt;/strong&gt; On normal \(1\text{–}100\text{ k}\Omega\) robot circuits, a \(10\text{ M}\Omega\) DMM introduces &amp;lt; 1% error; on \(10\text{ M}\Omega\) dividers, it halves the reading!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-quantifying-voltmeter-loading-error-on-two-different-dividers&quot;&gt;Concrete Worked Example: Quantifying Voltmeter Loading Error on Two Different Dividers&lt;/h3&gt;

&lt;p&gt;Suppose a digital multimeter has internal input resistance \(R_m = 10\text{ M}\Omega\) and measures the midpoint of an equal-resistor divider (\(R_1 = R_2 = R\)) powered by \(V_{\text{in}} = 5.00\text{ V}\) (true midpoint \(= 2.50\text{ V}\)). When the meter probes across \(R_2\), the lower leg becomes \(R_2 \parallel R_m = \frac{R \cdot R_m}{R + R_m}\).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Case 1 — Typical robot divider (\(R = 10\text{ k}\Omega = 0.01\text{ M}\Omega\)):&lt;/strong&gt;
\(R_2 \parallel R_m = \frac{0.01 \times 10}{0.01 + 10} = 0.00999\text{ M}\Omega.\)
Measured voltage: \(V_{\text{meas}} = 5.00 \times \frac{0.00999}{0.01 + 0.00999} = 2.4988\text{ V}\) (error only \(-0.05\%\) — negligible!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Case 2 — Ultra-high-impedance divider (\(R = 10\text{ M}\Omega\)):&lt;/strong&gt;
\(R_2 \parallel R_m = \frac{10 \times 10}{10 + 10} = 5.0\text{ M}\Omega.\)
Measured voltage: \(V_{\text{meas}} = 5.00 \times \frac{5.0}{10 + 5.0} = 1.667\text{ V}\) (error \(-33.3\%\)!).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Plugging both leads of a resistor into the &lt;em&gt;same&lt;/em&gt; 5-hole breadboard row — the metal clip underneath shorts the two legs together, turning a \(10\text{ k}\Omega\) resistor into \(0\,\Omega\).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If your continuity beeper sounds (resistance \(&amp;lt; 50\,\Omega\)) when touching &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; with power OFF, never connect the battery — inspect for reversed electrolytic capacitors or bridged IC pins first.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A board has 30 columns on each side of the trench, and both the top and bottom power-rail pairs are split once in the middle. How many separate five-hole nodes are in the terminal field? How many separate rail segments are there?&lt;/li&gt;
  &lt;li&gt;You want the voltage across a 180 Ω resistor that is already lighting an LED from 3.3 V. Which jack, which dial position, and do you disconnect the LED?&lt;/li&gt;
  &lt;li&gt;A jumper beeps from metal end to metal end and reads about 0.3 Ω. Once pressed into a hole, that hole does not beep to the other end of the wire. Where is the open?&lt;/li&gt;
  &lt;li&gt;A classmate tried to “measure USB current” by placing the meter across 5 V and GND, saw a flash, and now the mA range reads zero while the 20 V range still shows the charger. What failed?&lt;/li&gt;
  &lt;li&gt;Why is a beep on the continuity setting not a sufficient check that a 180 Ω LED resistor is the right part?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. Each side of each column is one node, so $30 \times 2 = 60$ five-hole nodes. Top red, top blue, bottom red, and bottom blue, each split in two, is 8 rail segments.
2. Black in COM, red in the voltage/ohms jack, dial on DC volts above 3.3 V. Probes go across the resistor, in parallel, while the LED stays in the circuit. Disconnecting the LED opens the loop and the resistor voltage collapses.
3. The wire is intact. The open is between the pin and the spring. Move the pin to another hole in that column. Do not throw away a jumper that already beeps end to end.
4. The mA fuse opened. The meter was being used as a short across the charger. Voltage readings survive because they do not use that fuse. Do not repeat the measurement to &quot;confirm.&quot;
5. Continuity is a near-short detector. A 180 Ω part is usually silent, and so is a 100 kΩ part. Only the ohms range, on an unpowered resistor, tells those two apart.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;p&gt;SparkFun’s breadboard tutorial matches the rails, strips, and trench in the figure above: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard&quot;&gt;How to use a breadboard&lt;/a&gt;. Their meter tutorial is the jack discipline with a photograph of each function: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-use-a-multimeter&quot;&gt;How to use a multimeter&lt;/a&gt;. Read the current section, then look again at which jack your red lead is in before you close a circuit.&lt;/p&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The full robot cart, including the ESP32 or Pico, the TB6612, the TT motors, and the HC-SR04, is in &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;. This lesson only adds the bench tools if they are not already on that list.&lt;/p&gt;

&lt;p&gt;Prices below were read on Hshop on 23 September 2026. Confirm the live listing before you pay.&lt;/p&gt;

&lt;p&gt;An 830-point breadboard was 35 000 ₫: &lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;test board CAMMB-102&lt;/a&gt;. Male-to-male jumpers, 40 wires, were 30 000 ₫: &lt;a href=&quot;https://hshop.vn/day-cam-breadboard-duc-duc-20cm-cap-det-40-soi-m-m-jumper-wire&quot;&gt;dây cắm breadboard đực-đực&lt;/a&gt;. The UNI-T UT33D+ meter was 285 000 ₫: &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;đồng hồ vạn năng UNI-T UT33D+&lt;/a&gt;. Any substitute meter still needs a continuity beeper and a fused milliamp range you promise not to short across USB.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Voltage, current, power, and Ohm's law</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter01/01_01_voltage_current_ohm/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter01/01_01_voltage_current_ohm</id>
   <content type="html">&lt;p&gt;In 1827, Georg Simon Ohm published a modest pamphlet claiming that the current flowing through a wire is simply proportional to the voltage pushing it and inversely proportional to the wire’s resistance. Critics mocked the equation as too simple to be true. Yet nearly two centuries later, \(V = IR\) and \(P = VI\) remain the single most frequently used calculations on every robotics workbench — governing everything from choosing a 330 \(\Omega\) LED resistor to predicting why a microcontroller resets the instant both robot wheels hit a door threshold.&lt;/p&gt;

&lt;p&gt;This lesson turns Ohm’s law and Kirchhoff’s laws into practical intuition for diagnosing real robot power rails, voltage dividers, and motor loads.r&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you will be able to name voltage, current, resistance, and power with their units, use $V = IR$ to find the missing quantity in one loop, and compute resistor heating with $P = VI = I^2 R = V^2/R$. You will size the series resistor for a red LED on a 3.3 V rail and show that a 1/4 W body is plenty. You will also explain why a TT gearmotor is not a resistor you may hang on an ESP32 or Pico GPIO.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Arithmetic is enough if every number keeps its unit. A bare 0.008 might be 8 mA or a slipped decimal. The numbers can be finished on paper. A meter and a red LED, if you already have them, are for the lab. The next lesson names holes and jacks.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-ohms-law-kirchhoffs-laws-and-power-dissipation-in-robot-circuits&quot;&gt;Key ideas: Ohm’s Law, Kirchhoff’s Laws, and Power Dissipation in Robot Circuits&lt;/h2&gt;

&lt;p&gt;Every DC circuit on your mobile robot obeys three compact physical laws:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Ohm’s Law (\(V = IR\)):&lt;/strong&gt; Relates electrical potential difference \(V\) (volts), charge flow rate \(I\) (amperes), and opposition to flow \(R\) (ohms). In linear resistors, \(R\) is constant; in motors and LEDs, effective resistance changes with speed or voltage, making Ohm’s law a local or series-limiting tool.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Kirchhoff’s Voltage &amp;amp; Current Laws (KVL &amp;amp; KCL):&lt;/strong&gt; KVL states that voltage rises and drops around any closed loop sum to zero (energy conservation); KCL states that current entering any node equals current leaving it (charge conservation). Together they yield series/parallel resistor networks and voltage dividers.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Joule Power &amp;amp; Thermal Limits (\(P = VI = I^2 R = V^2/R\)):&lt;/strong&gt; Every resistor has a maximum wattage rating (commonly \(1/4\text{ W} = 0.25\text{ W}\) for through-hole parts). Exceeding that rating turns a resistor into a smoke generator even if the circuit logic is “correct.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-this-matters-on-the-diff-drive-robot&quot;&gt;Why this matters on the diff-drive robot&lt;/h2&gt;

&lt;p&gt;The capstone is a two-wheel differential-drive robot. An ESP32 or a Pico speaks 3.3 V logic. A TB6612FNG steers two TT gearmotors from a separate battery, and an HC-SR04 later reports distance. The alive-lamp is a red LED and one resistor on 3.3 V. Too much resistance and the lamp looks dead. No resistor and the LED, or the pin, dies.&lt;/p&gt;

&lt;p&gt;The motors are the other budget. A stalled TT motor draws on the order of an ampere. A GPIO is comfortable nearer 12 mA and is in trouble somewhere around 40 mA. If that motor current is borrowed from the USB lead that feeds the chip, the 5 V rail sags, the onboard regulator cannot hold 3.3 V, and the board resets at the instant the wheels were commanded to turn. Ohm’s law separates those two failures before you blame the firmware.&lt;/p&gt;

&lt;h2 id=&quot;voltage-current-and-resistance&quot;&gt;Voltage, current, and resistance&lt;/h2&gt;

&lt;p&gt;Voltage is energy per unit charge, always measured between two points. One volt means one joule per coulomb. A pin marked 3.3 V says a charge falling from that pin to ground gives up 3.3 J per coulomb. One meter lead, with nowhere for the other to land, has not measured a voltage.&lt;/p&gt;

&lt;p&gt;Current is how much charge passes a point each second. One ampere is one coulomb per second. The symbol is $I$. On a GPIO and on an LED we speak in milliamperes:&lt;/p&gt;

\[1\ \mathrm{mA} = 0.001\ \mathrm{A}\]

&lt;p&gt;so 8 mA is $0.008\ \mathrm{A}$. A TT motor lives elsewhere: a few hundred milliamperes while rolling, and an ampere-class spike when the rotor is locked. Diagrams in this course use conventional current, drawn from positive toward negative.&lt;/p&gt;

&lt;p&gt;Resistance, $R$, in ohms ($\Omega$), is how hard a part pushes back. A larger resistance allows less current for the same voltage. A jumper is a fraction of an ohm. A 180 Ω resistor is a deliberate bottleneck. An open switch is so large a resistance that the current is effectively zero.&lt;/p&gt;

&lt;p&gt;For a resistor at a steady temperature the three quantities are tied by Ohm’s law:&lt;/p&gt;

\[V = IR\]

&lt;p&gt;which rearranges to $I = V/R$ and $R = V/I$. The law is a model of ohmic parts. A resistor is close enough for every calculation in this chapter. An LED is not ohmic. It conducts very little until the voltage across it reaches a forward drop, then the current rises steeply. That is why an LED gets a resistor as a partner rather than being treated as a resistor itself.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/ohms_law_triangle.png&quot; alt=&quot;Ohm&apos;s law triangle: cover the quantity you want&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The triangle is only a memory aid for $V = IR$. Cover the letter you want. Cover $V$ and you multiply $I$ by $R$. Cover $I$ and you divide $V$ by $R$. Power is not on this triangle.&lt;/p&gt;

&lt;h2 id=&quot;power-and-the-series-led-loop&quot;&gt;Power, and the series LED loop&lt;/h2&gt;

&lt;p&gt;Power is energy per second, in watts. For any two-terminal part it is the product of the voltage across that part and the current through it:&lt;/p&gt;

\[P = VI\]

&lt;p&gt;On a resistor, substitute Ohm’s law. With $I = V/R$,&lt;/p&gt;

\[P = \frac{V^2}{R}\]

&lt;p&gt;With $V = IR$,&lt;/p&gt;

\[P = I^2 R\]

&lt;p&gt;The three expressions agree on a resistor. On an LED the voltage stays near the forward drop while the resistor sets the current, so use $P = V_f I$ for the LED and $I^2 R$ for the resistor. In $V^2/R$, $V$ is the voltage across the resistor, not the full supply.&lt;/p&gt;

&lt;p&gt;A 1/4 W resistor may dissipate 0.25 W in free air. The LED resistors here dissipate tens of milliwatts. A low-value resistor asked to pass an ampere will brown, and $I^2 R$ tells you first.&lt;/p&gt;

&lt;p&gt;In one series loop the current is the same everywhere and the voltages add to the supply:&lt;/p&gt;

\[V_\mathrm{s} = V_R + V_f\]

&lt;p&gt;so $V_R = V_\mathrm{s} - V_f$ and $I = V_R / R$. In parallel, each branch sees the same voltage and the currents add. Two motors on one driver are that second case. The battery supplies the sum. The microcontroller’s USB lead is not that battery.&lt;/p&gt;

&lt;h2 id=&quot;worked-example-the-33-v-status-lamp&quot;&gt;Worked example: the 3.3 V status lamp&lt;/h2&gt;

&lt;p&gt;The rail is 3.3 V, the LED is red with $V_f \approx 2.0\ \mathrm{V}$, and the target is 8 mA so the lamp is obvious indoors without stressing a pin.&lt;/p&gt;

\[R = \frac{V_\mathrm{s} - V_f}{I} = \frac{3.3 - 2.0}{0.008} = \frac{1.3}{0.008} = 162.5\ \Omega\]

&lt;p&gt;Choose the next common value up, 180 Ω, so the current lands under the target:&lt;/p&gt;

\[I = \frac{3.3 - 2.0}{180} = \frac{1.3}{180} \approx 7.2\ \mathrm{mA}\]

&lt;p&gt;Check the heat with the target current and the body you will actually fit. That estimate is slightly pessimistic, which is what you want before you trust a power rating:&lt;/p&gt;

\[P = I^2 R = (0.008)^2 \times 180 = 0.0115\ \mathrm{W} \approx 0.011\ \mathrm{W}\]

&lt;p&gt;A 1/4 W part is rated 0.25 W, so the resistor runs at about 4% of its rating. Using the 7.2 mA that 180 Ω really sets gives a little less, about 0.009 W. The same number comes from the 1.3 V the resistor drops: $P = VI \approx 0.0072 \times 1.3 \approx 0.009\ \mathrm{W}$ and $P = V^2/R = 1.69/180 \approx 0.009\ \mathrm{W}$. The LED itself dissipates $P_\mathrm{LED} \approx 2.0 \times 0.0072 \approx 0.014\ \mathrm{W}$.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/led_series_resistor.png&quot; alt=&quot;One loop: supply, series resistor, LED, ground&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Current leaves the positive rail, passes through the resistor, enters the LED at the anode, leaves at the cathode, and returns to ground. The current in the resistor is the current in the LED. If the LED is backwards the loop stays dark. That is not a reason to omit the resistor when you turn it around. With no resistor, nothing sets the current. The LED is asked to sit near 3.3 V with a forward drop near 2 V, and the current becomes whatever the pin and the steep LED curve will allow, often many times 20 mA.&lt;/p&gt;

&lt;h2 id=&quot;worked-example-a-motor-is-not-a-gpio-resistor&quot;&gt;Worked example: a motor is not a GPIO resistor&lt;/h2&gt;

&lt;p&gt;Suppose a stalled TT motor draws about 0.8 A at 6 V. Ohm’s law, misapplied, “finds a resistance”:&lt;/p&gt;

\[R_\mathrm{stall} \approx \frac{6}{0.8} = 7.5\ \Omega\]

&lt;p&gt;Hang that across a 3.3 V GPIO and the same formula predicts&lt;/p&gt;

\[I \approx \frac{3.3}{7.5} \approx 0.44\ \mathrm{A} = 440\ \mathrm{mA}\]

&lt;p&gt;A cautious GPIO budget is about 12 mA. A figure often quoted as an absolute ceiling is about 40 mA. Four hundred milliamperes is an order of magnitude past both. The pin fails, or the regulator sags and the chip resets. The arithmetic is also too kind: a stalled motor is a coil plus a locked gearbox, not a stable 7.5 Ω film part. While it spins, back-emf makes the current smaller than stall, so a free-running bench test lies about a robot that has nosed into a wall.&lt;/p&gt;

&lt;p&gt;Motor current belongs on the TB6612 VM pin, from a pack in the module’s range of about 4.5 V to 10 V. The GPIO only carries direction and PWM into logic inputs specified from 2.7 V to 5.5 V, so 3.3 V is a legal high. Two stalled motors near 0.8 A each ask for about 1.6 A. A laptop USB port is often budgeted near 0.5 A, so the wheels are not powered from USB or from the 3.3 V pin.&lt;/p&gt;

&lt;h2 id=&quot;hands-on-lab&quot;&gt;Hands-on lab&lt;/h2&gt;

&lt;p&gt;Build the lamp only after the arithmetic.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Write $R = (3.3 - 2.0) / 0.008 = 162.5\ \Omega$ and, beside it, the 180 Ω part you will fit, before any lead goes into a hole.&lt;/li&gt;
  &lt;li&gt;Place the resistor and a red LED in series between the 3.3 V rail and GND. On a dev board that rail is the pin marked 3V3, not a GPIO and not the pin marked 5V. The longer LED lead, the anode, points toward the positive side. The flat on the rim points toward GND.&lt;/li&gt;
  &lt;li&gt;Apply power only after the series resistor is in the loop. Look at the LED from the side, not straight into the lens.&lt;/li&gt;
  &lt;li&gt;Set the meter to DC volts, red lead in the voltage jack, and measure across the resistor only. Divide that voltage by 180 Ω. You are inferring current. Do not break the loop to measure amperes; that skill is the next lesson.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should see a clearly visible red glow, not a dazzling one. The voltage across the 180 Ω resistor should sit near 1.1 V to 1.5 V, so the current is near 6 mA to 8 mA. The resistor will not feel hot. A 220 Ω substitute gives $I = 1.3 / 220 \approx 5.9\ \mathrm{mA}$, dimmer and still a success.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see&lt;/th&gt;
      &lt;th&gt;Likely cause&lt;/th&gt;
      &lt;th&gt;What to change&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Dark LED, resistor cool&lt;/td&gt;
      &lt;td&gt;LED reversed&lt;/td&gt;
      &lt;td&gt;Swap the LED. Leave the resistor in the loop.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A flash, then darkness&lt;/td&gt;
      &lt;td&gt;Series resistor missing, or shorted by the way it was inserted&lt;/td&gt;
      &lt;td&gt;Retire that LED. Rebuild with 180 Ω before you trust the pin.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Board resets, or a GPIO no longer toggles&lt;/td&gt;
      &lt;td&gt;5 V landed on an ESP32 or Pico pin, or the LED returned through a GPIO from the 5 V rail&lt;/td&gt;
      &lt;td&gt;Stop. Power the lamp only from 3V3 to GND. Retest the pin with a known-good LED and 180 Ω.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Glow that vanishes in room light&lt;/td&gt;
      &lt;td&gt;The part is tens of kilohms, not 180 Ω&lt;/td&gt;
      &lt;td&gt;Measure it. $I = 1.3 / 10000 = 0.13\ \mathrm{mA}$ looks black.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Safety: keep any chassis wheels off the table, do not connect a motor supply, and do not land VM or USB 5 V on a GPIO. One LED at a few milliamps is the whole experiment.&lt;/p&gt;

&lt;h2 id=&quot;explanation-designing-a-5-v--33-v-sensor-divider-and-checking-resistor-wattage&quot;&gt;Explanation: Designing a 5 V → 3.3 V Sensor Divider and Checking Resistor Wattage&lt;/h2&gt;

&lt;p&gt;Suppose you need to connect a 5 V sensor output (such as an HC-SR04 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; pin) to a 3.3 V ESP32 GPIO input without frying the pin. Walk through the four-step KVL/Ohm’s law design:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Apply the voltage divider equation:&lt;/strong&gt; With top resistor \(R_1\) and bottom resistor \(R_2\) to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt;, the midpoint voltage is:
\(V_{\text{out}} = V_{\text{in}} \cdot \frac{R_2}{R_1 + R_2}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Choose the ratio \(R_2 / (R_1 + R_2) \le 3.3 / 5.0 = 0.66\):&lt;/strong&gt; Using standard E12 values \(R_1 = 1\text{ k}\Omega\) and \(R_2 = 2\text{ k}\Omega\) (or \(1.8\text{ k}\Omega\)) gives a clean ratio.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check quiescent current:&lt;/strong&gt; \(I = V_{\text{in}} / (R_1 + R_2)\) should be small (a few mA) so it does not overload the sensor’s output pin, yet large enough to overcome stray capacitance.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify power dissipation:&lt;/strong&gt; Check \(P_1 = I^2 R_1\) and \(P_2 = I^2 R_2\) against the \(0.25\text{ W}\) resistor limit.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-1-kω--18-kω-divider-and-led-current-limiting-resistor&quot;&gt;Concrete Worked Example: 1 kΩ / 1.8 kΩ Divider and LED Current-Limiting Resistor&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Part A — Level-shifting a 5.0 V &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; signal with \(R_1 = 1.0\text{ k}\Omega\) and \(R_2 = 1.8\text{ k}\Omega\):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Output voltage: \(V_{\text{out}} = 5.0 \times \frac{1.8}{1.0 + 1.8} = 5.0 \times \frac{1.8}{2.8} \approx 3.214\text{ V}\) (safely below 3.3 V, comfortably above the ESP32 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HIGH&lt;/code&gt; threshold of \(0.75 \times 3.3 = 2.475\text{ V}\)!).&lt;/li&gt;
      &lt;li&gt;Current through divider: \(I = \frac{5.0\text{ V}}{2800\,\Omega} \approx 1.79\text{ mA}\).&lt;/li&gt;
      &lt;li&gt;Power in \(R_2\): \(P_2 = (0.00179)^2 \times 1800 \approx 0.0058\text{ W} = 5.8\text{ mW} \ll 250\text{ mW}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Part B — Sizing a red LED resistor on a 3.3 V GPIO pin (\(V_f = 2.0\text{ V}\), target \(I = 10\text{ mA}\)):&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Voltage across resistor (KVL): \(V_R = 3.3 - 2.0 = 1.3\text{ V}\).&lt;/li&gt;
      &lt;li&gt;Required resistance: \(R = \frac{1.3\text{ V}}{0.010\text{ A}} = 130\,\Omega\) (pick standard \(150\,\Omega \implies I = 1.3/150 \approx 8.7\text{ mA}\)).&lt;/li&gt;
      &lt;li&gt;Power in resistor: \(P_R = 1.3 \times 0.0087 \approx 11.3\text{ mW}\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Trying to power a 3.3 V sensor &lt;em&gt;supply rail&lt;/em&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VCC&lt;/code&gt;) from a resistive voltage divider! As soon as the sensor draws current \(I_{\text{load}}\), that extra current flows through \(R_1\) and collapses \(V_{\text{out}}\). Dividers are for high-impedance &lt;strong&gt;signals&lt;/strong&gt;, never for power rails (use a voltage regulator for power).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If a motor draws \(1.5\text{ A}\) through a thin wire with total resistance \(0.4\,\Omega\), the voltage lost in the wire is \(V = 1.5 \times 0.4 = 0.6\text{ V}\) and the wire heats at \(P = 1.5^2 \times 0.4 = 0.9\text{ W}\).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A green LED with $V_f = 2.2\ \mathrm{V}$ is lit from 5 V at 10 mA, between 5 V and GND, not into a GPIO. Compute $R$, pick the next value up from 220 Ω, 270 Ω, and 330 Ω, and compute $P$ in that resistor.&lt;/li&gt;
  &lt;li&gt;The kit has no 180 Ω part, so you use 220 Ω for the 3.3 V red lamp with $V_f = 2.0\ \mathrm{V}$. Find the current. Is the lamp still usable indoors?&lt;/li&gt;
  &lt;li&gt;One TT motor draws 0.15 A while rolling and 0.8 A when stalled. Two run together. Which total, if either, fits on a USB port limited to 0.5 A?&lt;/li&gt;
  &lt;li&gt;A friend measures 3.3 V across a dark LED with no series resistor and says the voltage is correct, so the LED is fine. Using $V_\mathrm{s} = V_R + V_f$, what is missing?&lt;/li&gt;
  &lt;li&gt;For a 180 Ω resistor with 1.3 V across it, show that $P = VI$, $P = I^2 R$, and $P = V^2/R$ agree. Why is it wrong to plug $V = 3.3\ \mathrm{V}$ into $V^2/R$ for that resistor?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. $R = (5 - 2.2) / 0.010 = 280\ \Omega$. The next value up is 330 Ω. Then $I = 2.8 / 330 \approx 8.5\ \mathrm{mA}$ and $P \approx 0.024\ \mathrm{W}$, well under 0.25 W. This lamp is not on a GPIO.
2. $I = 1.3 / 220 \approx 5.9\ \mathrm{mA}$. Yes. Dimmer than 8 mA, still an obvious indoor indicator. Do not remove the resistor to &quot;fix&quot; the dimness.
3. Rolling: $0.30\ \mathrm{A}$, under 0.5 A. Stall: $1.6\ \mathrm{A}$, which does not fit. A rolling test can look safe and still reset the board when a wheel stalls. Motor current does not come from USB.
4. With no resistor, $V_R = 0$, so the entire 3.3 V is forced onto the LED. A healthy red LED sits near 2 V only when a resistor sets a modest current. Full supply across a dark LED means it is open, backwards, or already dead.
5. $I = 1.3 / 180 \approx 7.2\ \mathrm{mA}$, and all three formulas give about 0.0094 W. Using 3.3 V pretends the resistor drops the whole supply and ignores the LED&apos;s 2 V.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;p&gt;All About Circuits states the law in a DC chapter: &lt;a href=&quot;https://www.allaboutcircuits.com/textbook/direct-current/chpt-2/voltage-current-resistance-ohms-law/&quot;&gt;Voltage, current, resistance, and Ohm’s law&lt;/a&gt;. SparkFun’s LED page is the companion once the resistor is in the loop: &lt;a href=&quot;https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds&quot;&gt;Light-emitting diodes&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam&quot;&gt;Where to buy in Vietnam&lt;/h2&gt;

&lt;p&gt;The chassis, driver, microcontroller, ultrasonic sensor, and the rest of the cart are listed in &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;Bill of materials and how to buy the kit in Vietnam&lt;/a&gt;. This lesson adds only the lamp parts, and only if that cart does not already include them.&lt;/p&gt;

&lt;p&gt;A 3 mm LED assortment on Hshop was 20 000 ₫ on 23 September 2026: &lt;a href=&quot;https://hshop.vn/bo-5-loai-led-sieu-sang-3mm-thong-dung-5-kind-3mm-transparent-color-led&quot;&gt;bộ 5 loại LED 3 mm&lt;/a&gt;. Use a red LED for the calculation above. Confirm the live price before you pay.&lt;/p&gt;

&lt;p&gt;A 1/4 W assortment that includes 180 Ω or 220 Ω is the resistor buy. Hshop search for điện trở did not return a 1/4 W kit when this lesson was written, so use a search page and read the shop: &lt;a href=&quot;https://shopee.vn/search?keyword=b%E1%BB%99%20%C4%91i%E1%BB%87n%20tr%E1%BB%9F%201%2F4W&quot;&gt;Shopee search for bộ điện trở 1/4W&lt;/a&gt;. That link is a search page, not one product. Look for 1/4 W and 5% or 1%. Do not trust a loose unlabeled “180 Ω” without measuring it.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Chapter 00 lab checklist: the bench is ready</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter00/00_07_ch00_lab_checklist/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter00/00_07_ch00_lab_checklist</id>
   <content type="html">&lt;p&gt;Before launching a rocket or rolling a mobile robot onto the floor, engineers run a &lt;strong&gt;readiness review&lt;/strong&gt;: a concrete, pass/fail verification that tools, power, safety gear, and workspace files exist and work as specified. Skipping readiness checks is how a five-minute missing driver turns into a three-hour troubleshooting spiral in Chapter 01.&lt;/p&gt;

&lt;p&gt;This capstone checklist for Chapter 00 turns abstract preparation into measurable bench acceptance criteria.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;This is the exit gate for Chapter 00. Plan 60–90 minutes, mostly checking evidence from lessons 00-01 through 00-06. A tick without a file, a photo, or a pasted line does not count. Chapter 01 will ask you to measure real parts. Do not start it on a vibe.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will &lt;strong&gt;audit&lt;/strong&gt; the bench with a written procedure, one item at a time. You will &lt;strong&gt;attach&lt;/strong&gt; evidence to each item: a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; sum, a meter beep on a known short, a serial port or an explicit plan if the board has not arrived, a Track A or Track B sentence, a charger photo or a note that cells are not purchased yet, and the power-up card from lesson 00-03. You will &lt;strong&gt;separate&lt;/strong&gt; a missing tool that blocks Chapter 01 from a missing chassis that does not. You will &lt;strong&gt;predict&lt;/strong&gt; which missing line would make the first electronics lab unsafe. You will &lt;strong&gt;list&lt;/strong&gt; only the gaps you will buy, using lesson 00-05 as the cart rather than a new one.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You need the journal, the track sentence, the power-up card, the Wave 1 inventory photo, and whatever &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; lesson 00-05 asked you to build. If one of those was skipped, the audit records the skip. You do not need a moving robot, ROS 2, or a soldered joint. You do need to be willing to leave a box unticked.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-verification-validation-and-acceptance-testing-in-robotics&quot;&gt;Key ideas: Verification, Validation, and Acceptance Testing in Robotics&lt;/h2&gt;

&lt;p&gt;Even at the bench-setup stage, you are practicing the same systems-engineering habits used in aerospace and industrial robotics:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Verification (“Did we build it right?”):&lt;/strong&gt; Objective, quantitative checks — does the multimeter measure a known AA cell within \(\pm 0.05\text{ V}\)? Does the USB cable carry data (not charge-only)? Does the toolchain compile a minimal binary without errors?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Validation (“Will it support the mission?”):&lt;/strong&gt; Confirming that your BOM parts, battery capacity, and workspace layout match the requirements of Capstone A (closed-loop differential-drive MCU robot) and the later ROS 2 track.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Baseline Logging:&lt;/strong&gt; Recording initial measurements (battery resting voltage, wheel diameter, track width) in a versioned bench log creates a ground-truth reference whenever behavior drifts weeks later.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Capstone A stops the wheels when commands go silent for about 300 ms. That behaviour needs a bench that can tell a ground from a wish. Later ROS 2 topics will describe the same robot. They will not repair a Chapter 01 session that measured ohms on a live rail, or a lithium cell that arrived with no charger decision. This gate finds, while the parts are still in bags, which missing evidence would make the next chapter unsafe.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;The order on the power-up card is one of the items this audit must see&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you cannot find the card, you do not redraw it from memory during the audit and call it old. You redo lesson 00-03’s page and then you photograph it. The figure is a reminder of the sequence: common ground, logic, signals, motor supply last, wheels up.&lt;/p&gt;

&lt;h2 id=&quot;what-counts-as-evidence&quot;&gt;What counts as evidence&lt;/h2&gt;

&lt;p&gt;A tick is a claim that a specific artifact exists. The artifact has to be something you could hand to another student.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;bill of materials&lt;/strong&gt; counts when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; (or the same table in the journal) has a sum you wrote yourself, with a date. A screenshot of a shop homepage with no total is not a sum. If lesson 00-05 is unfinished, the line is open, and you finish that lesson before you pretend Chapter 00 is closed.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;meter&lt;/strong&gt; counts when the journal says the probes were touched together and the meter beeped, or when it records the failure (“no beep; fuse or battery”). A photo of a meter in a shopping cart does not beep. If the meter has not arrived, the line stays open. Chapter 01’s continuity work is blocked until it closes. That is the correct outcome, not a negotiated pass.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;USB cable&lt;/strong&gt; counts in one of two ways. A serial port appeared when the board was plugged in, and the name is pasted in the journal, and it disappeared when you unplugged. Or the board has not arrived, and the journal contains a written plan: USB only, no motor supply, record the port, unplug. A cable that only lights an LED has not proved data. Write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unproven&lt;/code&gt; rather than ticking the line.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;track sentence&lt;/strong&gt; counts when it names Track A or Track B, the MCU (ESP32 or Pico 2), and the refusal to buy a LiDAR before Capstone A. “I like robots” is not a track.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;charger&lt;/strong&gt; counts when a photo shows a label you can read (Li-ion, 4.2 V, one cell), or when the journal says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cells not purchased yet&lt;/code&gt;. A bare cell with no note is an open safety item, not a neutral. A TP4056 pictured while clipped across a 2S holder is evidence of a problem, not a tick.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;power-up card&lt;/strong&gt; counts when the photo or the page itself shows common ground, USB logic, signals, VM last, wheels up, the 300 ms PWM stop, and the ban on motor voltage and 5 V echo into a 3.3 V GPIO. A card that starts with the battery fails the item even if the paper exists.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;workspace&lt;/strong&gt; from lesson 00-06 counts when the directories exist and the install log has versions or the words &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not installed&lt;/code&gt;. An empty &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; with that honesty is a pass. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; full of clones you did not mean to keep is a mess you clean before you tick.&lt;/p&gt;

&lt;h2 id=&quot;the-audit-procedure&quot;&gt;The audit procedure&lt;/h2&gt;

&lt;p&gt;Do this in order. Do not skip to the items you know you passed.&lt;/p&gt;

&lt;p&gt;Open the journal and add a heading &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Chapter 00 exit&lt;/code&gt;. Copy the seven evidence items above as lines you will mark &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pass&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; and write the total in đồng on the exit heading, plus the date the prices were copied. If you cannot find the file, mark &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open&lt;/code&gt; and stop claiming the shopping lesson is done. Open the Wave 1 photo from lesson 00-04 and check it against the lines: meter, iron, stand, solder, cutters, breadboard, jumpers, data cable, MCU, LED, button. Mark each &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;here&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gap&lt;/code&gt;. The chassis and the driver are Wave 2. Their absence does not fail Chapter 00. Their presence does not excuse a missing meter.&lt;/p&gt;

&lt;p&gt;Perform the beep test again, even if you did it last week. Probes together, meter in continuity, circuit unpowered. Write “beep” or what happened instead. If you are about to measure a powered USB rail in ohms because the beep failed, stop. That is the fault the rule exists to prevent.&lt;/p&gt;

&lt;p&gt;Check the USB evidence. If the board is on the desk, plug it in with no motor battery connected, paste the port name, and unplug. If the board is not on the desk, read the written plan and confirm it forbids VM on that first plug-in. If there is no plan and no port, the item is open.&lt;/p&gt;

&lt;p&gt;Read the track sentence aloud. Confirm it mentions the 300 ms stop and names TB6612FNG rather than an L298N if a driver is already in the spreadsheet. If the spreadsheet says L298N, the shopping lesson’s voltage-drop arithmetic has not been applied. Mark the driver line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open&lt;/code&gt; even if the rest of the track sentence sounds confident.&lt;/p&gt;

&lt;p&gt;Look at the charger evidence. Either the photo’s label matches one cell at 4.2 V, or the note says cells are not purchased yet. If a 2S holder and a TP4056 are in the same photo and connected, disconnect them before you continue the audit, and do not tick the line.&lt;/p&gt;

&lt;p&gt;Find the power-up card. Compare it to the figure at the top of this lesson. If VM is not last, rewrite the card. Photograph the version you would actually follow.&lt;/p&gt;

&lt;p&gt;Open the firmware folder and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/README.md&lt;/code&gt;. Confirm the install log matches the computer in front of you. Delete nothing you still need, but remove the claim that Jazzy is installed if the machine is Windows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you should see when the gate is honest.&lt;/strong&gt; The exit heading lists pass or open for every item. The sum, the beep, the port or the plan, the track sentence, the cell decision, and the card are each a real artifact. Open items name the lesson that closes them: 00-04 for a missing stand, 00-05 for a missing sum, 00-03 for a missing card, 00-06 for a missing folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you should see when someone is rushing.&lt;/strong&gt; Every line says pass, and the only attachment is “looks good.” Put &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open&lt;/code&gt; back on any line you cannot point at in a minute.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;My’s exit note, written after she actually looked, reads as follows. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; sum 1,620,000 ₫, prices dated 23 Sep 2026, file in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/&lt;/code&gt;. Meter: beep on a known short, repeated today. USB: board not arrived; plan is “USB only, no VM, paste the port, unplug; cable in the drawer is unproven until that port appears.” Track: “Track A, ESP32, TB6612FNG, no LiDAR before wheels-up teleop with a 300 ms stop. Track B on the lab Ubuntu PC after Chapter 07.” Cells: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not purchased yet&lt;/code&gt;. Power-up card: photographed, VM last, wheels-up line present. Install log: Arduino IDE not installed on the laptop, Jazzy not installed, Gazebo Harmonic named only.&lt;/p&gt;

&lt;p&gt;She ticks the written plan, not a proved cable, and leaves &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cable unproven&lt;/code&gt; visible. Gaps: a stand (40000 ₫ on the 23 Sep 2026 snapshot) and jumpers. The missing chassis is Wave 2 and does not block Chapter 01. The missing stand blocks soldering, so a hot iron stays off the desk. Metering and identification may start.&lt;/p&gt;

&lt;p&gt;A second student, Khoa, has a beep, a card, and a track sentence, and also a photograph of a TP4056 wired across two cells because “the connector fit.” His charger line is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open&lt;/code&gt;, and the photo is evidence of a fault, not of readiness. He disconnects the module, writes that each cell will be charged alone to 4.2 V or not at all, and only then may he reconsider the tick. His &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; has no sum. He goes back to lesson 00-05 before he calls the gate passed. A messaging stack on Ubuntu would not have caught either miss.&lt;/p&gt;

&lt;h2 id=&quot;explanation-quantifying-battery-state-of-charge--bench-readiness&quot;&gt;Explanation: Quantifying Battery State-of-Charge &amp;amp; Bench Readiness&lt;/h2&gt;

&lt;p&gt;How do you verify a fresh 2S Li-ion pack is healthy before it ever powers your electronics? Walk through these four checks:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Measure open-circuit voltage (OCV) of the full pack and each cell:&lt;/strong&gt; A healthy Li-ion cell sits between \(3.0\text{ V}\) (empty cutoff) and \(4.2\text{ V}\) (full charge), with storage voltage near \(3.7\text{–}3.85\text{ V}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check cell balance \(\Delta V = \lvert V_1 - V_2 \rvert\):&lt;/strong&gt; If two series cells differ by more than \(0.10\text{ V}\), one cell will over-discharge while the pack total still looks acceptable.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify polarity before mating connectors:&lt;/strong&gt; Red probe on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V+&lt;/code&gt;, black on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; must read &lt;strong&gt;positive&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+7.4 V&lt;/code&gt;, never &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-7.4 V&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Estimate remaining energy conservatively:&lt;/strong&gt; Never extrapolate linearly below \(3.3\text{ V/cell}\) because Li-ion discharge curves drop sharply near the knee.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-checking-a-2s-18650-pack-at-bench-bring-up&quot;&gt;Concrete Worked Example: Checking a 2S 18650 Pack at Bench Bring-Up&lt;/h3&gt;

&lt;p&gt;Suppose your multimeter measures total pack voltage \(V_{\text{pack}} = 7.62\text{ V}\), cell 1 \(V_1 = 3.82\text{ V}\), and cell 2 \(V_2 = 3.80\text{ V}\).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Series sum consistency check:&lt;/strong&gt;
\(V_1 + V_2 = 3.82 + 3.80 = 7.62\text{ V} = V_{\text{pack}}\) (confirms clean contact and no hidden series drop).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Cell imbalance check:&lt;/strong&gt;
\(\Delta V = \lvert 3.82 - 3.80 \rvert = 0.02\text{ V} \le 0.05\text{ V}\) (well-balanced!).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Linearized state-of-charge estimate (3.2 V empty to 4.2 V full):&lt;/strong&gt;
\(\text{SoC} \approx \frac{3.81 - 3.20}{4.20 - 3.20} = \frac{0.61}{1.00} = 61\%\) — right in the ideal storage band for first bench tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Assuming a pack reading &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7.0 V&lt;/code&gt; is safe when cell 1 is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4.0 V&lt;/code&gt; and cell 2 is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.0 V&lt;/code&gt; — always check individual cell voltages if your pack has a balance tap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If a 2S pack drops to \(6.4\text{ V}\) (\(3.2\text{ V/cell}\)) under load, stop the lab immediately and recharge; discharging below \(2.5\text{–}3.0\text{ V/cell}\) permanently damages lithium-ion chemistry.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;p&gt;These ask you to find the unsafe gap. They are about your audit, or about the audits below, not about restating the chapter.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Chapter 01’s first measurement lab asks for continuity and DC volts. Which single open item in your exit list makes that lab dishonest or unsafe, and what evidence would close it?&lt;/li&gt;
  &lt;li&gt;A student ticks “USB cable proved” because the board’s LED lights. No port name is written. Why is the tick false, and what could go wrong in Chapter 02 if they keep it?&lt;/li&gt;
  &lt;li&gt;The power-up card is missing, and the student says Chapter 01 has no motors so the card can wait. Name one Chapter 01 action (probing, soldering, or a lithium cell already on the shelf) that the card was supposed to constrain anyway.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; lists an L298N and a 2S holder, the sum is blank, and the track sentence says “ROS first.” Which of those three facts would make a later motor test unsafe even if Chapter 01’s resistor lab is fine? Explain with the roughly 2 V drop or with the charger rule.&lt;/li&gt;
  &lt;li&gt;Cells are not purchased, the meter beeps, the card exists, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; is empty on Windows with that fact written down. May this student start Chapter 01’s unpowered identification labs? What are they still forbidden to do?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. An open meter line: no beep on a known short means every later &quot;0 Ω&quot; or &quot;3.3 V&quot; is untrustworthy, and a student who then tries current or ohms at random can short a port. Close it with a beep, probes touching, written in the journal. A missing chassis does not do this damage.
2. The LED only shows that power arrived. A charge-only cable never enumerates a serial port, so Chapter 02&apos;s upload fails in a way that looks like a bad board. The tick needs a port name that appears and disappears with the plug, or an explicit unproven plan.
3. Soldering still needs the stand, glasses, and ventilation. A cell on the shelf still needs &quot;one cell, 4.2 V, never across 2S.&quot; Probing USB 5 V with the red lead in the current jack is the same family of mistake.
4. The L298N will drop about 2 V and make a weak motor look like a software bug, and a 2S holder next to an unnamed charger is how a TP4056 gets clipped across 8.4 V. &quot;ROS first&quot; skips the timeout that should zero PWM. Any one of the three is enough to call the motor test unsafe. The blank sum means they also do not know what they think they bought.
5. Yes, for unpowered identification and for continuity on parts that are not connected to a supply, provided the meter beep is real. They are still forbidden to solder without a stand, to connect a motor supply, to charge lithium, and to treat the empty `ros_ws/` as a reason to skip the bench later.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;p&gt;The gate does not add a new theory reading. Open these again only for the line that is still open.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-use-a-multimeter&quot;&gt;How to use a multimeter&lt;/a&gt; — if the beep test failed and you need the dial and the jacks explained again.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;Logic levels&lt;/a&gt; — if the power-up card’s 5 V ban is the line you rewrote.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation.html&quot;&gt;ROS 2 Jazzy install&lt;/a&gt; — only to confirm you are not installing it on the wrong operating system. An empty &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; on Windows is the correct Chapter 00 result.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam--mua-ở-việt-nam&quot;&gt;Where to buy in Vietnam / Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Buy the gaps the audit wrote down, then stop. The master cart is &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;lesson 00-05&lt;/a&gt;. Tool pages below are Hshop snapshots from &lt;strong&gt;23 September 2026&lt;/strong&gt;. Search URLs are searches.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;If the audit says this gap&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Snapshot&lt;/th&gt;
      &lt;th&gt;Link&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;No meter, or the meter will not beep and you are replacing it&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;285000 ₫ or 185000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UT33D+&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-ky-thuat-so-vom-wadfow-wdm1501-digital-multimeter-true-rms&quot;&gt;Wadfow WDM1501&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;No iron&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;75000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;60 W Wadfow&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;No stand&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;40000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;stand&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;No 0.8 mm solder&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;24000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/thiec-han-sunchi-kp26-0-8mm-sn63-pb37-solder-wire&quot;&gt;Sn63 wire&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;No side cutters&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/kim-cat-day-dien-nho-170-cat-chan-linh-kien-dien-tu&quot;&gt;170 cutters&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;No breadboard&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;830-point board&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;One meter, not both. Jumpers and a data cable, if those are the open lines: &lt;a href=&quot;https://shopee.vn/search?keyword=c%C3%A1p%20usb%20data&quot;&gt;Shopee search, cáp USB data&lt;/a&gt; and &lt;a href=&quot;https://www.lazada.vn/catalog/?q=d%C3%A2y%20jumper&quot;&gt;Lazada search, dây jumper&lt;/a&gt;. Eye protection if the card says it is missing: &lt;a href=&quot;https://shopee.vn/search?keyword=k%C3%ADnh%20b%E1%BA%A3o%20h%E1%BB%99&quot;&gt;Shopee search, kính bảo hộ&lt;/a&gt;. &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; and &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; are shop homepages, useful if you are standing at a counter with the gap list in your hand. Do not add a LiDAR, a Pi, or a scope to close Chapter 00.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Workspace folders and a toolchain preview</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter00/00_06_workspace_toolchain_preview/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter00/00_06_workspace_toolchain_preview</id>
   <content type="html">&lt;p&gt;Nothing derails a robotics project faster than “works on my laptop yesterday, refuses to compile today.” Unlike pure web scripts, robot software spans firmware builds, hardware pin maps, calibration logs, and plotting notebooks — and a bug in any one of them can send a physical chassis into a table leg.&lt;/p&gt;

&lt;p&gt;This lesson establishes a disciplined workspace layout and toolchain mental model (PlatformIO/C++ for deterministic MCU firmware, Python for offline analysis and telemetry plotting, and later ROS 2 workspaces) so every experiment is reproducible.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Plan about 70 minutes at the computer you will actually use for firmware. You need the track sentence from lesson 00-02. You do not need the robot, Ubuntu, or a successful compile. The point is a folder layout and an install log that tell the truth about what is installed today.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will &lt;strong&gt;create&lt;/strong&gt; a workspace with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/&lt;/code&gt;, either &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/esp32/&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/pico/&lt;/code&gt;, and an empty &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; that says why it is empty. You will &lt;strong&gt;choose&lt;/strong&gt; an ESP32 path (Arduino IDE 2.x or PlatformIO, board class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;, serial 115200, a data USB cable) or a Pico 2 path (BOOTSEL, a MicroPython UF2, Thonny or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt;). You will &lt;strong&gt;record&lt;/strong&gt; versions you actually see, including the honest line “not installed.” You will &lt;strong&gt;explain&lt;/strong&gt; why ROS 2 Jazzy is an Ubuntu 24.04 install and why Windows Subsystem for Linux is not the robot lab yet. You will &lt;strong&gt;name&lt;/strong&gt; Gazebo Harmonic without installing it, and you will &lt;strong&gt;estimate&lt;/strong&gt; how small a serial byte is next to the 300 ms teleop timeout.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can create directories and you can paste text into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/lab-notes.md&lt;/code&gt;. Lessons 00-01 through 00-04 gave you the journal fields, the track, the power-up card, and the tool inventory. Lesson 00-05, the bill of materials, should already exist as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; or as a dated note; if it does not, write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv missing&lt;/code&gt; in the install log rather than inventing prices. You do not need to have soldered.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-cross-compilation-flashing-pipelines-and-telemetry-analysis&quot;&gt;Key ideas: Cross-Compilation, Flashing Pipelines, and Telemetry Analysis&lt;/h2&gt;

&lt;p&gt;Three software-engineering pillars separate reliable robotics code from fragile hobby sketches:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Cross-Compilation &amp;amp; Toolchains:&lt;/strong&gt; Your laptop (x86_64 or ARM64) compiles C++ source code using a target compiler (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xtensa-esp32-elf-g++&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arm-none-eabi-gcc&lt;/code&gt;) into machine binary instructions for a completely different CPU architecture on the microcontroller, then flashes that binary over UART/USB.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Deterministic Project Configuration:&lt;/strong&gt; Pinning board definitions, library versions, and compiler flags in a manifest (such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platformio.ini&lt;/code&gt;) guarantees that rebuilding a project six months later produces identical timing and behavior.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Offline Data Logging &amp;amp; Telemetry:&lt;/strong&gt; Real-time control loops cannot pause to draw plots. Standard robotics practice streams compact CSV/binary telemetry over serial (e.g., &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ms,v_ref,v_meas,pwm&lt;/code&gt;) to the host computer, where Python computes step-response metrics (rise time, overshoot, steady-state error).r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;The Chapter 07 timeout lives in firmware you can rebuild, on a board whose serial port you have already seen appear. A ROS 2 topic, later, is a messaging layer on that behaviour. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; is a pile of half-installed packages on a Windows laptop, you will debug apt errors while the PWM pin is still untested. An empty workspace with a written reason is a better lab partner than a tutorial you cannot finish. Gazebo Harmonic can wait until there is a robot whose silence behaviour you trust enough to simulate.&lt;/p&gt;

&lt;h2 id=&quot;the-folders-and-what-each-one-is-allowed-to-contain&quot;&gt;The folders, and what each one is allowed to contain&lt;/h2&gt;

&lt;p&gt;Put the course directory somewhere you will find it. The names below are the contract.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/&lt;/code&gt; holds &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lab-notes.md&lt;/code&gt; and, when lesson 00-05 is done, a copy or a pointer to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt;. Photos can live beside the markdown or in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/img/&lt;/code&gt;. This folder is not for libraries you downloaded “in case.”&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/esp32/&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/pico/&lt;/code&gt; holds the programs for the microcontroller you chose. Create the one that matches the track sentence. If you are still deciding, create the one you wrote down in lesson 00-02 and add a line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undecided — revisit before paying&lt;/code&gt;. Do not create both and call that a plan, unless you already own both boards and you have named which one drives the motors.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; is a ROS 2 workspace. Today it contains a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README&lt;/code&gt; and nothing else. The README states the operating system of this computer and the sentence “Jazzy is not installed here” or “Jazzy is installed on this Ubuntu 24.04 machine, version string below.” It does not contain a cloned Nav2 stack, a LiDAR driver, or a half-extracted Gazebo world.&lt;/p&gt;

&lt;p&gt;A short tree for an ESP32 student on Windows looks like this.&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;robot-course/
  notes/lab-notes.md
  notes/bom.csv
  firmware/esp32/README.md
  ros_ws/README.md
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A Pico student uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/pico/&lt;/code&gt; instead. The names are boring on purpose. Future lessons will tell you which file to open.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/pcb_mcu_anatomy.png&quot; alt=&quot;What you are pointing a toolchain at: USB, regulator, 3.3 V pins, ground&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The anatomy drawing is the board as a set of jobs. USB brings power and a serial link. A regulator makes 3.3 V. GPIO pins speak logic. Ground is the common node from the safety lesson. The toolchain’s only job today is to talk to that USB link, or to admit that the board is not here.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/ESP32_on_Lolin32_Lite_clone_board_cropped.jpg&quot; alt=&quot;An ESP32 module on a small dev board, the Wi-Fi-capable brain&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Raspberry_Pi_Pico.jpg&quot; alt=&quot;A Raspberry Pi Pico, the USB-first MicroPython board&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The photographs are the two legal brains, not a third option you have to buy. An ESP32-class board is the one that can do Wi-Fi teleop later. A Pico-class board, including a Pico 2 if that is what lesson 00-05 sold you, is the one that starts as a USB device running MicroPython. Match the photo to your sentence. Do not match it to whichever listing is on sale tonight.&lt;/p&gt;

&lt;h2 id=&quot;the-esp32-path-previewed-and-not-finished&quot;&gt;The ESP32 path, previewed and not finished&lt;/h2&gt;

&lt;p&gt;Two tools are legal. &lt;strong&gt;Arduino IDE 2.x&lt;/strong&gt; is the one with a GUI, a board manager, and a serial monitor. &lt;strong&gt;PlatformIO&lt;/strong&gt; is the one that lives in an editor such as VS Code and pins versions in a file. Pick one for the install log. You may switch later. You may not write “both, whichever” and leave the log blank.&lt;/p&gt;

&lt;p&gt;When the board support is installed, the board entry you want for a common DevKit is in the &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;&lt;/strong&gt; class: in Arduino IDE that is often labelled “ESP32 Dev Module”; in PlatformIO the board id is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;. The exact menu label moves between package versions. The id is the thing to write down. The serial monitor baud for this course’s first sketches is &lt;strong&gt;115200&lt;/strong&gt;. A charge-only USB cable will power a lamp on the board and will never show a port. The proof of a data cable is a new serial device when you plug in, and its disappearance when you unplug.&lt;/p&gt;

&lt;p&gt;You are not required to compile blink today. If the IDE is installed, record the version string from the About dialog or from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pio --version&lt;/code&gt;. If it is not installed, write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Arduino IDE not installed&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlatformIO not installed&lt;/code&gt; and the reason. “I am on a lab PC I do not control” is a reason. “I got bored” is also a reason, and it should be visible so next week is honest.&lt;/p&gt;

&lt;p&gt;A single byte on an 8N1 serial link is about 10 bit times. At 115200 bit/s,&lt;/p&gt;

\[t_{\mathrm{byte}} \approx \frac{10}{115200}\,\mathrm{s} \approx 87\,\mu\mathrm{s}.\]

&lt;p&gt;A short command of four characters is under half a millisecond. The Chapter 07 timeout is about 300 ms, hundreds of times longer. Serial baud is not the dead-man switch. Dropped commands are. Knowing that now stops you from “fixing” a timeout by changing the baud later.&lt;/p&gt;

&lt;h2 id=&quot;the-pico-path-previewed-and-not-finished&quot;&gt;The Pico path, previewed and not finished&lt;/h2&gt;

&lt;p&gt;Hold the &lt;strong&gt;BOOTSEL&lt;/strong&gt; button, plug the board into a data USB cable, and release the button. A drive appears, the way a USB stick would. You copy a &lt;strong&gt;MicroPython UF2&lt;/strong&gt; onto that drive. The board reboots into the MicroPython interpreter. &lt;strong&gt;Thonny&lt;/strong&gt; is the simple editor that can find the board. &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mpremote&lt;/code&gt;&lt;/strong&gt; is the command-line tool that can do the same job from a terminal. Pick one and write it in the log.&lt;/p&gt;

&lt;p&gt;If no drive appears, the cable is the first suspect, then the button, then the port. If the board has not arrived, write the steps as a plan and do not download random UF2 files “to be ready” without noting the date and the official Raspberry Pi source. A UF2 from a forum thread is how people flash the wrong chip.&lt;/p&gt;

&lt;p&gt;The Pico path does not use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt; and does not use 115200 as a requirement of MicroPython’s REPL, though a later UART you wire yourself might. Do not mix the two install logs. A student who chose the Pico still leaves &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; empty for the same reason the ESP32 student does.&lt;/p&gt;

&lt;h2 id=&quot;ros-2-jazzy-and-gazebo-harmonic-named-only&quot;&gt;ROS 2 Jazzy and Gazebo Harmonic, named only&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;ROS 2 Jazzy&lt;/strong&gt; is the distribution this course uses, and the supported desktop install is &lt;strong&gt;Ubuntu 24.04&lt;/strong&gt;. If this computer is Windows, do not pretend that Windows Subsystem for Linux is the robot lab. WSL can be a place to read documentation. It is not where you will plug the motor driver’s ground and watch a wheel. Write the install as &lt;strong&gt;later&lt;/strong&gt;, on an Ubuntu 24.04 machine: your own, if you have one, or a lab PC, as in the track lesson. The install page is &lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation.html&quot;&gt;docs.ros.org/en/jazzy/Installation.html&lt;/a&gt;. Following it today is allowed only when the operating system is actually Ubuntu 24.04, and only as a parallel sitting that does not replace the firmware folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gazebo Harmonic&lt;/strong&gt; is the simulator that pairs with that stack. Its name goes in the README. Its packages do not get installed today. A world file you cannot connect to a robot you have not built is a second project.&lt;/p&gt;

&lt;p&gt;micro-ROS, when it comes, is firmware on the same ESP32 or Pico-class board plus an agent on the Ubuntu machine. It belongs after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/&lt;/code&gt; already contains a program that can zero PWM. The empty &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt; is how you remember that order.&lt;/p&gt;

&lt;h2 id=&quot;lab-folders-readme-install-log&quot;&gt;Lab: folders, README, install log&lt;/h2&gt;

&lt;p&gt;Create the tree in the previous section. In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/esp32/README.md&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/pico/README.md&lt;/code&gt;, write three lines: the track (A, or B-after-A), the chosen MCU in words, and the tool you will use (Arduino IDE 2.x, PlatformIO, Thonny, or mpremote). If the tool is not installed, say so in the same file.&lt;/p&gt;

&lt;p&gt;Create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/README.md&lt;/code&gt; with the operating system name and the sentence that Jazzy and Gazebo Harmonic are not installed today, or the version string if you are on Ubuntu 24.04 and you did install Jazzy. Do not paste a tutorial’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt&lt;/code&gt; transcript you did not run.&lt;/p&gt;

&lt;p&gt;Add an &lt;strong&gt;install log&lt;/strong&gt; section to today’s journal entry. Each line is a tool, a version or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not installed&lt;/code&gt;, and a date. Example lines, which you must edit to match your machine:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;2026-09-23  OS: Windows 11
2026-09-23  Arduino IDE: not installed — will use lab time on Thursday
2026-09-23  PlatformIO: not installed
2026-09-23  serial plan: 115200, board id esp32dev, cable not yet proved
2026-09-23  ROS 2 Jazzy: not installed — Ubuntu 24.04 lab PC later
2026-09-23  Gazebo Harmonic: named only
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A Pico log names BOOTSEL and the UF2 instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt;. Paste the log into the serial-snippet field if you have no board, so the field is not empty. If a port did appear, paste the port name above the log.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you should see.&lt;/strong&gt; The three directories exist. The firmware README names a track and an MCU. The ROS README does not claim Ubuntu you do not have. The install log has dates and the words &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not installed&lt;/code&gt; where that is true.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it goes wrong.&lt;/strong&gt; You created &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/src&lt;/code&gt; full of clones because a video started that way. Delete the clones until the README is the only file; you can re-clone when Chapter 09 says so. You wrote “WSL Ubuntu” as if it were the robot lab. Correct the README: WSL is not the bench. You set the baud to 9600 because an old Arduino sketch did. The course serial monitor for ESP32 sketches is 115200. You copied a UF2 from a random link. Replace the plan with the official Raspberry Pi MicroPython documentation and do not flash it until you can say which chip the file is for.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Trang is on Windows, Track A, ESP32, ceiling already decided in lesson 00-02. She creates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;firmware/esp32/&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/&lt;/code&gt;. She does not install the IDE in this sitting because the lab PC at school already has Arduino IDE 2.3 and she would rather learn one install. Her log says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Arduino IDE: not on this laptop; school lab PC, confirm version Thursday&lt;/code&gt;. The firmware README says “Track A, ESP32, Wi-Fi teleop later, tool is Arduino IDE 2.x, board class esp32dev, serial 115200.” She computes the byte time, about 87 µs, and writes “300 ms timeout is about 3000 byte-times; baud is not the safety layer.” Her &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/README.md&lt;/code&gt; says “This laptop is Windows 11. Jazzy waits for the Ubuntu 24.04 lab PC. Gazebo Harmonic is not installed. WSL will not be used as the robot lab.”&lt;/p&gt;

&lt;p&gt;A classmate on Ubuntu 24.04 may install Jazzy in another hour and paste a real version string. That classmate still owes the same firmware folder. Trang does not copy a version she did not run.&lt;/p&gt;

&lt;h2 id=&quot;explanation-sizing-your-serial-telemetry-bandwidth-by-hand&quot;&gt;Explanation: Sizing Your Serial Telemetry Bandwidth by Hand&lt;/h2&gt;

&lt;p&gt;A classic beginner mistake is printing verbose debug sentences inside a 100 Hz control loop and accidentally breaking real-time control. Let’s calculate the exact serial bandwidth budget step by step:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Understand UART frame overhead:&lt;/strong&gt; Standard &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8N1&lt;/code&gt; serial sends 1 start bit + 8 data bits + 1 stop bit = &lt;strong&gt;10 bits per byte&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute maximum byte throughput:&lt;/strong&gt; At baud rate \(B\) (bits/second), maximum raw throughput is \(B / 10\) bytes/second.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Measure your telemetry line length:&lt;/strong&gt; Count every character (including commas and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\r\n&lt;/code&gt;) in one telemetry packet.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compare packet rate against control rate:&lt;/strong&gt; Required bandwidth \(= (\text{bytes/line}) \times f_s\) must stay comfortably below \(70\%\) of \(B/10\) to prevent TX buffer blocking.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-will-115200-baud-keep-up-with-a-200-hz-control-loop&quot;&gt;Concrete Worked Example: Will 115200 Baud Keep Up with a 200 Hz Control Loop?&lt;/h3&gt;

&lt;p&gt;Suppose your firmware logs one CSV line per tick:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12345,0.350,0.342,148\r\n&lt;/code&gt; — count the characters: \(5+1+5+1+5+1+3+2 = 23\text{ bytes}\).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Maximum link capacity at 115,200 baud (8N1):&lt;/strong&gt;
\(\text{Capacity} = \frac{115\,200\text{ bits/s}}{10\text{ bits/byte}} = 11\,520\text{ bytes/s}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Bandwidth needed at \(f_s = 200\text{ Hz}\):&lt;/strong&gt;
\(\text{Demand} = 23\text{ bytes/tick} \times 200\text{ ticks/s} = 4\,600\text{ bytes/s}.\)
Utilization is \(4600 / 11520 \approx 39.9\%\) — comfortably safe!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — What if you instead print a verbose 120-byte debug sentence at 200 Hz?&lt;/strong&gt;
\(\text{Demand} = 120 \times 200 = 24\,000\text{ bytes/s} &amp;gt; 11\,520\text{ bytes/s}!\)
The serial TX buffer fills completely within milliseconds, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.print()&lt;/code&gt; blocks the CPU, and your 200 Hz PID loop collapses to \(11520 / 120 = 96\text{ Hz}\) with severe jitter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Leaving &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Serial.begin(9600)&lt;/code&gt; (only \(960\text{ bytes/s}\)!) from an introductory blink tutorial while logging PID data at 100 Hz.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; Raising baud rate to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;921600&lt;/code&gt; boosts capacity to \(92\,160\text{ bytes/s}\) — enough for 23-byte telemetry lines at over 4,000 Hz (or decimated 50 Hz telemetry with huge headroom).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Your track sentence says Pico 2 and MicroPython. Which directory do you create, what does BOOTSEL have to do before any file is copied, and which two tools are legal for the REPL?&lt;/li&gt;
  &lt;li&gt;The board LED lights from a USB cable, and no serial port appears. Which object do you suspect, and what evidence would change your mind?&lt;/li&gt;
  &lt;li&gt;You are on Windows and a blog says to install Jazzy inside WSL tonight. What do you write in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ros_ws/README.md&lt;/code&gt;, and which operating system is actually required?&lt;/li&gt;
  &lt;li&gt;At 115200 bit/s, 8N1, estimate the time for one byte and for a 4-byte command. Is that time a meaningful fraction of 300 ms?&lt;/li&gt;
  &lt;li&gt;Your install log says “PlatformIO latest” with no version and no date. What would you add so the line is evidence?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. Create `firmware/pico/`. Hold BOOTSEL while plugging in a data cable so a drive appears, then copy the MicroPython UF2. Thonny or `mpremote` may talk to the board afterwards.
2. Suspect a charge-only cable. A data cable is proved when a serial port or a BOOTSEL drive appears and disappears with the plug. A glowing LED only proves power.
3. Write that Jazzy is not installed, that WSL is not the robot lab, and that the install waits for Ubuntu 24.04. Jazzy&apos;s supported desktop path for this course is Ubuntu 24.04.
4. One byte is about $10/115200 \approx 87\,\mu\mathrm{s}$. Four bytes are about $0.35\,\mathrm{ms}$. That is roughly a thousandth of 300 ms. The timeout is about missing commands, not about baud.
5. Add the date, the output of `pio --version` or the IDE About string, and `not installed` if you have not run it. &quot;Latest&quot; is not a version.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.platformio.org/&quot;&gt;PlatformIO docs&lt;/a&gt; — the editor-based ESP32 path, including the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esp32dev&lt;/code&gt; board id.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.arduino.cc/&quot;&gt;Arduino docs&lt;/a&gt; — Arduino IDE 2.x and the serial monitor.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.espressif.com/projects/esp-idf/en/latest/esp32/&quot;&gt;ESP-IDF for ESP32&lt;/a&gt; — Espressif’s own framework. You are not required to use it for Capstone A. It is the reference when a board-support package disagrees with a blog.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.raspberrypi.com/documentation/microcontrollers/micropython.html&quot;&gt;Raspberry Pi Pico MicroPython&lt;/a&gt; — BOOTSEL and the official UF2, which is the only flash plan this lesson trusts.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Tutorials.html&quot;&gt;ROS 2 Jazzy tutorials&lt;/a&gt; — skim the list so you can see it assumes an install. That install is Ubuntu 24.04, later.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gazebosim.org/docs/harmonic/&quot;&gt;Gazebo Harmonic&lt;/a&gt; — the simulator’s name and docs. Do not install it from this lesson.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Bill of materials and how to buy the kit in Vietnam</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter00/00_05_bom_and_shopping/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter00/00_05_bom_and_shopping</id>
   <content type="html">&lt;p&gt;Ask any hardware team what delayed their robot prototype by three weeks and the answer is rarely a difficult differential equation — it is a missing 2-pin JST connector, an incompatible 5 V vs. 3.3 V logic module, or a motor driver whose current rating was read as “continuous” when the datasheet meant “10-millisecond peak.” A disciplined &lt;strong&gt;Bill of Materials (BOM)&lt;/strong&gt; is your first engineering design document.&lt;/p&gt;

&lt;p&gt;This lesson walks through every component of the Vietnam-sourced robot kit, showing how voltage rails, stall currents, logic levels, and mechanical mounts must agree on paper before you click “Buy.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Estimated time: &lt;strong&gt;90–120 minutes&lt;/strong&gt;, including the spreadsheet lab. Prices below were read from public Hshop product pages on &lt;strong&gt;23 September 2026&lt;/strong&gt;. They will move. Treat every đồng figure as a snapshot, then confirm the live listing before you pay.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lesson you can build a Track A bill of materials for the differential-drive capstone, choose between an ESP32 dev board and a Raspberry Pi Pico using ratings rather than a flash sale, reject an L298N for 3–6 V TT motors and say why in volts, match a lithium cell to a charger that cannot set the pack on fire, and fill &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; with a vendor, a search keyword, a substitute, and a received/not-received flag for every line.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You only need the habit of writing things down. Lesson 00-02 (which track you are on) helps you decide whether Wi-Fi has to be on the microcontroller now. You do not need Ohm’s law yet; the driver and battery sections teach the two numbers you must not get wrong. A phone camera is the inspection tool until the multimeter arrives.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-systems-integration-electrical-compatibility-and-bom-verification&quot;&gt;Key ideas: Systems Integration, Electrical Compatibility, and BOM Verification&lt;/h2&gt;

&lt;p&gt;Building a robot from off-the-shelf modules is an exercise in &lt;strong&gt;interface matching&lt;/strong&gt; across three pillars:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Electrical Rail &amp;amp; Current Matching:&lt;/strong&gt; Every load has a supply voltage window \([V_{\min}, V_{\max}]\) and peak current \(I_{\text{peak}}\). Your battery and buck converters must supply \(\sum I_{\text{peak}}\) with headroom while staying inside every IC’s absolute maximum rating.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Logic-Level Compatibility:&lt;/strong&gt; Modern MCUs (ESP32, RP2040) operate at \(3.3\text{ V}\) logic and are &lt;em&gt;not&lt;/em&gt; 5 V tolerant on GPIO inputs. Mixing a 5 V ultrasonic sensor (HC-SR04 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ECHO&lt;/code&gt; pin) with a 3.3 V MCU requires a resistive voltage divider or logic level shifter.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mechanical &amp;amp; Mounting Compatibility:&lt;/strong&gt; Wheel hub shafts (D-shaft vs. double-flat TT shaft), encoder mounting brackets, and M3 standoff patterns must match the chassis plate before assembly begins.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Chapter 07 teleop dies in boring ways: a motor driver that eats two volts, a USB port asked to spin the wheels, a 5 V echo wired into a 3.3 V pin, or a charger that tries to push 4.2 V into an 8.4 V pack. ROS 2 later does not fix any of those. The bill of materials is the parts list that makes the physics match the firmware. Buy the ratings, then the robot.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/kit_catalog_overview.png&quot; alt=&quot;Kit families you are shopping for&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Power comes up in this order: common ground, logic, signals, motor supply last, wheels off the desk&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;what-you-are-actually-buying&quot;&gt;What you are actually buying&lt;/h2&gt;

&lt;p&gt;A bill of materials is a table with one row per physical thing that must exist on the bench, including the screwdriver you already own. It is not a screenshot of a shopping cart. Each row answers six questions: what the part is, how many, what electrical or mechanical rating makes it acceptable, what it does on this robot, what you will accept as a substitute, and where you will buy it. If a row has no rating, you cannot tell a good substitute from a cheaper lookalike.&lt;/p&gt;

&lt;p&gt;The default robot in this course is a &lt;strong&gt;differential-drive&lt;/strong&gt; platform. Two driven wheels, one idle caster, a 3.3 V microcontroller, a MOSFET motor driver, a ranging sensor, and a motor battery that never shares its current with the microcontroller’s 3.3 V pin. Optional rows (IMU, wheel encoders, time-of-flight) wait until the base cart moves under command. Track B adds an Ubuntu 24.04 computer later for ROS 2 Jazzy. Do not buy a LiDAR to feel ready.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;USB 5 V ---- ESP32 / Pico logic (3.3 V GPIO)
                 |
                GND -------- common with driver GND
                 |
2S 18650 ~7.4 V ---- VM on TB6612 ---- two TT motors
                 |
                 +-- not wired to the 3.3 V pin, ever
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_rails_3v3.png&quot; alt=&quot;Logic rail, USB 5 V, and the separate motor rail&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;the-default-track-a-list&quot;&gt;The default Track A list&lt;/h2&gt;

&lt;p&gt;Buy in three waves so a delayed chassis does not stop Chapter 01.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wave 1, bench (start Chapter 01 this week).&lt;/strong&gt; Multimeter, temperature-aware or at least a 60 W iron with a stand, solder, side cutters, solderless breadboard, jumper wires, a data-capable USB cable, the microcontroller, a few LEDs, and a tactile button.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wave 2, motion (before Chapter 05).&lt;/strong&gt; Chassis or the loose TT motors plus brackets and a caster, TB6612FNG (DRV8833 only as a smaller substitute), HC-SR04, and a 3.3 V / 5 V level shifter for the echo pin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wave 3, energy (after you can measure voltage).&lt;/strong&gt; Two 18650 cells in a plausible capacity band, a holder, and a charger that charges &lt;strong&gt;one cell at a time&lt;/strong&gt; to 4.2 V. The series holder on the robot is not a charger.&lt;/p&gt;

&lt;p&gt;Leave these off the first invoice: RPLiDAR, Raspberry Pi 5, a bench oscilloscope, a 12 V 10 A supply, and any “robot kit” whose driver is an L298N glued to a 4×AA holder with no documentation.&lt;/p&gt;

&lt;h3 id=&quot;consolidated-kit-table&quot;&gt;Consolidated kit table&lt;/h3&gt;

&lt;p&gt;Ratings in this table are from the manufacturer’s usual datasheet or from the Hshop listing cited in the shopping section. Confirm the silk on the part you receive.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Line&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Qty&lt;/th&gt;
      &lt;th&gt;Acceptable rating&lt;/th&gt;
      &lt;th&gt;Role on the capstone&lt;/th&gt;
      &lt;th&gt;Preferred part&lt;/th&gt;
      &lt;th&gt;Substitute&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;MCU&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;3.3 V GPIO, USB-UART, ≥20 usable pins&lt;/td&gt;
      &lt;td&gt;Firmware, later Wi-Fi teleop&lt;/td&gt;
      &lt;td&gt;ESP32 dev board, 30-pin class, CH340 or CP2102&lt;/td&gt;
      &lt;td&gt;Pico 2 if you accept USB teleop first and Wi-Fi later&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Chassis&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;Two driven wheels, deck for boards&lt;/td&gt;
      &lt;td&gt;Diff-drive body&lt;/td&gt;
      &lt;td&gt;2WD acrylic TT kit, or Hshop 4WD kit used as two driven wheels plus caster&lt;/td&gt;
      &lt;td&gt;Loose TT motors, two wheels, two brackets, one caster, any stiff deck&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TT motor&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2 driven&lt;/td&gt;
      &lt;td&gt;3–9 V, gearbox about 1:48&lt;/td&gt;
      &lt;td&gt;Left and right drive&lt;/td&gt;
      &lt;td&gt;Motors included with the chassis&lt;/td&gt;
      &lt;td&gt;Hshop TT 1:48, 20 000 ₫ each on the listing date&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Driver&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;MOSFET H-bridge, VM about 5–10 V, ≥1 A continuous per channel&lt;/td&gt;
      &lt;td&gt;Direction and PWM&lt;/td&gt;
      &lt;td&gt;TB6612FNG module, VM 4.5–10 V, 1.2 A continuous / 3.2 A peak&lt;/td&gt;
      &lt;td&gt;DRV8833, VM 2.7–10.8 V, 1.5 A max per channel on the Hshop listing; watch heat when stalled&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Range&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;HC-SR04 or a small ToF&lt;/td&gt;
      &lt;td&gt;Obstacle distance&lt;/td&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td&gt;VL53L0X module if you want I²C and 3.3 V from the start&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Level shift&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;4-channel 3.3 V ↔ 5 V&lt;/td&gt;
      &lt;td&gt;HC-SR04 echo into ESP32&lt;/td&gt;
      &lt;td&gt;MOSFET shifter module&lt;/td&gt;
      &lt;td&gt;A resistor divider on echo only (not on trig if the module is unreliable at 3.3 V)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Motor pack&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2 cells&lt;/td&gt;
      &lt;td&gt;Li-ion 18650, capacity claim ≤ 3500 mAh&lt;/td&gt;
      &lt;td&gt;VM through the driver&lt;/td&gt;
      &lt;td&gt;Cells you charge &lt;strong&gt;outside&lt;/strong&gt; the robot&lt;/td&gt;
      &lt;td&gt;6×AA (~9 V) if you refuse lithium; do not use a 9 V rectangular battery&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Charger&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;4.2 V per cell, stops when full&lt;/td&gt;
      &lt;td&gt;Charge cells one by one&lt;/td&gt;
      &lt;td&gt;4-bay 18650 USB charger&lt;/td&gt;
      &lt;td&gt;A single TP4056 &lt;strong&gt;only&lt;/strong&gt; for one cell, never across a 2S holder&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Meter&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;DC volts, ohms, continuity beep&lt;/td&gt;
      &lt;td&gt;Prove the wiring&lt;/td&gt;
      &lt;td&gt;UNI-T UT33D+ or a cheaper true-RMS hobby meter&lt;/td&gt;
      &lt;td&gt;Any meter with a continuity beeper; avoid a meter with no current fuse if you will measure amps&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Iron&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;~60 W, stand, 0.8 mm solder&lt;/td&gt;
      &lt;td&gt;Harnesses&lt;/td&gt;
      &lt;td&gt;Fixed 60 W iron is enough for Dupont and headers&lt;/td&gt;
      &lt;td&gt;A station later; not required to start&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Prototype&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td&gt;830-tie breadboard, M-M and M-F jumpers&lt;/td&gt;
      &lt;td&gt;Chapters 01–03&lt;/td&gt;
      &lt;td&gt;—&lt;/td&gt;
      &lt;td&gt;400-tie board if you keep leads short&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Fasteners&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1 set&lt;/td&gt;
      &lt;td&gt;M3 screws, nylon standoffs&lt;/td&gt;
      &lt;td&gt;Hold the MCU off the metal&lt;/td&gt;
      &lt;td&gt;Often included with the chassis&lt;/td&gt;
      &lt;td&gt;A small M3 assortment from any hardware stall&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;why-the-driver-row-is-not-any-h-bridge&quot;&gt;Why the driver row is not “any H-bridge”&lt;/h2&gt;

&lt;p&gt;The L298N module on Hshop is the same &lt;strong&gt;45 000 ₫&lt;/strong&gt; as the TB6612FNG module (SKUs HS0695V and HS2458V on the day this lesson was written). The price is not the difference. The L298N is a Darlington bridge. At motor current it drops roughly 1.8 V to 2.5 V between the battery and the motor. The TB6612FNG is a MOSFET bridge; the motor sees nearly the full motor supply.&lt;/p&gt;

\[V_{motor} \approx V_{bat} - V_{drop}\]

&lt;p&gt;Work the two cases for a 2-cell pack at 7.4 V nominal and a 2.0 V Darlington drop versus a 0.3 V MOSFET drop:&lt;/p&gt;

\[V_{L298} \approx 7.4 - 2.0 = 5.4\,\text{V}\]

\[V_{TB6612} \approx 7.4 - 0.3 = 7.1\,\text{V}\]

&lt;p&gt;The Hshop TT motor listing (ratio 1:48) recommends &lt;strong&gt;3–9 V&lt;/strong&gt;, quotes no-load current &lt;strong&gt;110–150 mA&lt;/strong&gt;, and quotes about &lt;strong&gt;200–208 rpm at 5–6 V&lt;/strong&gt; with &lt;strong&gt;0.8 kg·cm at 6 V&lt;/strong&gt;. Five volts is already the slow, weak end of that listing. A fresh 4×AA pack is only 6 V before the L298N drop, so the motor can be left near 4 V and the robot crawls. The same module also carries a 7805 regulator that you must not use to feed an ESP32 when the motor supply is above 12 V; our pack stays under 10 V, and you still do not power the microcontroller from that regulator. Logic for the TB6612FNG is &lt;strong&gt;2.7–5.5 V&lt;/strong&gt;, so a 3.3 V GPIO is a valid high. Tie STBY high or the bridge stays asleep.&lt;/p&gt;

&lt;p&gt;The DRV8833 module is &lt;strong&gt;25 000 ₫&lt;/strong&gt; and the Hshop listing allows VM &lt;strong&gt;2.7–10.8 V&lt;/strong&gt; with &lt;strong&gt;1.5 A maximum per channel&lt;/strong&gt;. That is a fair substitute for one TT motor per channel. It has less copper and a smaller package than a TB6612 carrier, so a prolonged stall smells like hot plastic sooner. Do not parallel two motors on one DRV8833 channel.&lt;/p&gt;

&lt;p&gt;Stall current is not the no-load number. The listing does not publish stall current. Budget roughly several times 150 mA and &lt;strong&gt;measure&lt;/strong&gt; it later with the wheels in the air and the meter in series for a short pulse. If you only buy a driver because the sticker says 2 A, you have not finished the row.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/hbridge_concept.png&quot; alt=&quot;H-bridge idea: the microcontroller steers current, it does not supply the motor current&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;cells-holders-and-chargers-are-one-decision&quot;&gt;Cells, holders, and chargers are one decision&lt;/h2&gt;

&lt;p&gt;A single 18650 is about 3.6–3.7 V nominal and &lt;strong&gt;4.2 V full&lt;/strong&gt;. Two in series, which is what a “2×18650 nối tiếp” holder does, are about &lt;strong&gt;7.4 V nominal and 8.4 V full&lt;/strong&gt;. That voltage belongs on the driver VM pin. It does not belong on a GPIO, and it does not belong on a TP4056.&lt;/p&gt;

&lt;p&gt;The TP4056 boards on Hshop (the USB-C module at 10 000 ₫ and the mini-USB module at 2 000 ₫) are &lt;strong&gt;single-cell&lt;/strong&gt; chargers. They try to bring one cell to 4.2 V at about 1 A. If you clip one across a series pair, you are asking a 4.2 V charger to operate on an 8.4 V stack, or you are charging only a fraction of the pack while the cells drift apart. Either way the row is wrong. Charge each cell by itself in a bay charger, check that the open-circuit voltage sits between about 3.6 V and 4.20 V, then insert the cells into the holder. The 4-bay USB charger on Hshop was &lt;strong&gt;68 000 ₫&lt;/strong&gt; (listing title: 14500 / 18650 / 21700). Use one bay until you trust it.&lt;/p&gt;

&lt;p&gt;Capacity claims have a physical ceiling. A real 18650 is rarely above about 3500 mAh, and honest mid cells sit nearer 2000–3000 mAh. Hshop’s Sunpower cells were listed at &lt;strong&gt;2000 mAh for 35 000 ₫&lt;/strong&gt; and &lt;strong&gt;2500 mAh for 45 000 ₫&lt;/strong&gt;. A marketplace listing that says 9900 mAh or 12000 mAh for a similar price is fiction. The “10C” marking on those Sunpower titles is a discharge-rate claim, not a capacity. Do not believe a C-rating without a datasheet; two TT motors do not need a fantasy C-rating anyway.&lt;/p&gt;

&lt;p&gt;These hobby cells often arrive &lt;strong&gt;without&lt;/strong&gt; a protection PCB. A screwdriver across the terminals can dump tens of amps. Do not carry loose cells in a pocket with keys. Do not charge them on a bed. Stop if a cell is swollen, below about 2.5 V, or hot. A 9 V rectangular battery is the other classic mistake: its internal resistance collapses as soon as a motor stalls, and the microcontroller resets while the driver is still commanded on.&lt;/p&gt;

&lt;p&gt;During bench work, power the microcontroller from USB. Common the USB ground with the driver ground. Switch the motor battery on last, and only with the wheels off the table.&lt;/p&gt;

&lt;h2 id=&quot;buying-strategy&quot;&gt;Buying strategy&lt;/h2&gt;

&lt;p&gt;Buy the rating, not the thumbnail. A 30 % sticker on an L298N bundle is still the wrong bridge. Read voltage, continuous current, logic level, and connector gender before you read the discount.&lt;/p&gt;

&lt;p&gt;Order Wave 1 from one shop so a missing USB cable does not wait on a second parcel. In Vietnam, Hshop (hshop.vn, shop at 269/20 Lý Thường Kiệt, Phường Phú Thọ, TP.HCM) publishes a technical listing and a SKU. Their own note says a SKU ending in &lt;strong&gt;V&lt;/strong&gt; is a company invoice price that already includes VAT. They also ask you to film the unboxing within seven days if you want their basic warranty. Shopee and Lazada are search markets: sort by shop rating, read whether the listing is a module or a dev board, and prefer shops that show the chip marking. Thế Giới IC (thegioiic.com) and IC Đây Rồi (icdayroi.com, Thủ Đức) are component counters. They are excellent for an ESP32 &lt;strong&gt;module&lt;/strong&gt; or a loose regulator, and easy to misread as a USB dev board.&lt;/p&gt;

&lt;p&gt;Pay the electronics shop after they confirm stock if you are transferring. Marketplace escrow is fine for jumpers and chassis kits. Do not pay a stranger on social media for “tested 18650 cells” with no return path.&lt;/p&gt;

&lt;p&gt;When the parcel arrives, do this before you celebrate:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Film the opening. Count bags against &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Meter: continuity beeps on a shorted probe pair, then about 5 V on a USB charger you already trust.&lt;/li&gt;
  &lt;li&gt;Microcontroller: plug the &lt;strong&gt;data&lt;/strong&gt; cable into your computer. A new serial port must appear. A charge-only cable gives silence and looks like a dead board.&lt;/li&gt;
  &lt;li&gt;With only USB connected, the 3.3 V pin reads about 3.3 V. If it reads 5 V, stop.&lt;/li&gt;
  &lt;li&gt;Driver: the chip text matches TB6612FNG or DRV8833, not L298N.&lt;/li&gt;
  &lt;li&gt;Motors: shafts turn by hand without grit. Gearbox ratio on the bag matches the row you paid for.&lt;/li&gt;
  &lt;li&gt;Cells: not swollen, voltage of each cell between about 3.4 V and 4.2 V. Do not series them until both readings exist.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam--mua-ở-việt-nam&quot;&gt;Where to buy in Vietnam / Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Links below are either a &lt;strong&gt;product page that returned a price on 23 September 2026&lt;/strong&gt; or a &lt;strong&gt;search page&lt;/strong&gt;. Search pages are labeled. Do not treat a search URL as a promise that the first hit is the right part. Shopee keyword links are search pages. Lazada’s public catalog search is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://www.lazada.vn/catalog/?q=&lt;/code&gt; plus the keyword; an automated browser sometimes hits a captcha, a normal browser shows listings.&lt;/p&gt;

&lt;h3 id=&quot;microcontroller&quot;&gt;Microcontroller&lt;/h3&gt;

&lt;p&gt;Search: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 NodeMCU 38 chân CH340&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ESP32 DevKit V1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Raspberry Pi Pico 2&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Hshop product (verified): &lt;a href=&quot;https://hshop.vn/kit-rf-thu-phat-wifi-ble-esp32-nodemcu-32s-ch340-ai-thinker&quot;&gt;ESP32 NodeMCU-32S CH340 Ai-Thinker, 190 000 ₫&lt;/a&gt;. This is a USB dev board. Buy this class of board.&lt;/li&gt;
  &lt;li&gt;Hshop product (verified, more expensive): &lt;a href=&quot;https://hshop.vn/mach-phat-trien-vietduino-esp32&quot;&gt;Vietduino ESP32, 265 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Hshop product (verified, &lt;strong&gt;not&lt;/strong&gt; what you want as the only computer): &lt;a href=&quot;https://hshop.vn/mach-thu-phat-wifi-ble-soc-esp32-s3-esp32-s3-wroom-1-chinh-hang-espressif&quot;&gt;ESP32-S3-WROOM-1 module, 135 000 ₫&lt;/a&gt;. A module has no USB-UART until you add one.&lt;/li&gt;
  &lt;li&gt;Hshop search (verified search page): &lt;a href=&quot;https://hshop.vn/search?q=raspberry+pi+pico&quot;&gt;raspberry pi pico&lt;/a&gt; listed Pico 2 at 195 000 ₫ and Pico 2 W at 275 000 ₫.&lt;/li&gt;
  &lt;li&gt;Thế Giới IC product (verified page exists): &lt;a href=&quot;https://www.thegioiic.com/esp32-devkitc-32u-module-wifi-bluetooth-2-4ghz&quot;&gt;ESP32-DevKitC-32U&lt;/a&gt;. Read whether the antenna is the U.FL version (DevKitC-32U) and whether a cable is included.&lt;/li&gt;
  &lt;li&gt;Shopee search: &lt;a href=&quot;https://shopee.vn/search?keyword=esp32%20devkit%20ch340&quot;&gt;ESP32 DevKit&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Avoid a picture of a bare metal can with no USB socket if the title says “kit phát triển”.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;chassis-motors-caster&quot;&gt;Chassis, motors, caster&lt;/h3&gt;

&lt;p&gt;Search: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;khung xe robot 2 bánh TT&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;động cơ giảm tốc TT 1:48&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bánh đa hướng&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Hshop did &lt;strong&gt;not&lt;/strong&gt; show a cheap 2WD acrylic kit on the searches run for this lesson. It did show the &lt;a href=&quot;https://hshop.vn/khung-xe-mke-r01-4wd-robot-car-chassis-tt-motor&quot;&gt;MKE-R01 4WD TT chassis at 245 000 ₫&lt;/a&gt;. Use two of its motors for the capstone and keep two as spares, and add a caster, &lt;strong&gt;or&lt;/strong&gt; run left wheels together and right wheels together only after you have measured current.&lt;/li&gt;
  &lt;li&gt;Loose parts, verified: &lt;a href=&quot;https://hshop.vn/dong-co-dc-giam-toc-tt-motor-ti-so-1-48&quot;&gt;TT motor 1:48, 20 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/banh-xe-dong-co-dc-giam-toc-v1-plastic-geared-tt-motor-65mm&quot;&gt;65 mm wheel, 8 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/ga-bat-dong-co-dc-giam-toc-tt-motor-mounting-bracket&quot;&gt;bracket, 10 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/banh-xe-nhua-da-huong-3pi-ball-caster-wheel&quot;&gt;caster, 15 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Shopee search (search page, price not fetched): &lt;a href=&quot;https://shopee.vn/search?keyword=khung%20xe%20robot%202%20b%C3%A1nh%20TT&quot;&gt;khung xe 2 bánh TT&lt;/a&gt;. Street listings are often in a &lt;strong&gt;90 000–180 000 ₫&lt;/strong&gt; band; confirm the photo shows two motors, a caster, and a battery box.&lt;/li&gt;
  &lt;li&gt;Lazada search: &lt;a href=&quot;https://www.lazada.vn/catalog/?q=khung%20xe%20robot%202%20banh&quot;&gt;khung xe robot&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Avoid a “robot kit” that only shows an L298N and a 4×AA box and calls itself complete.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;motor-driver&quot;&gt;Motor driver&lt;/h3&gt;

&lt;p&gt;Search: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TB6612FNG&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DRV8833&lt;/code&gt;, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;module L298 giá rẻ&lt;/code&gt; as the default.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-tb6612fng-dc-motor-driver&quot;&gt;TB6612FNG, 45 000 ₫, SKU HS2458V&lt;/a&gt;. VM 4.5–10 V, logic 2.7–5.5 V, 1.2 A continuous, 3.2 A peak, 20×20 mm.&lt;/li&gt;
  &lt;li&gt;Verified substitute: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-drv8833-dc-motor-driver&quot;&gt;DRV8833, 25 000 ₫&lt;/a&gt;. VM 2.7–10.8 V, 1.5 A max per channel on that listing.&lt;/li&gt;
  &lt;li&gt;Verified part to &lt;strong&gt;not&lt;/strong&gt; make the default: &lt;a href=&quot;https://hshop.vn/mach-dieu-khien-dong-co-dc-l298&quot;&gt;L298N module, also 45 000 ₫&lt;/a&gt;. Buy it only if you already understand the drop and your motors are 12 V class.&lt;/li&gt;
  &lt;li&gt;Shopee search: &lt;a href=&quot;https://shopee.vn/search?keyword=tb6612fng&quot;&gt;TB6612FNG&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Pololu’s carrier is a trustworthy reference design if you later import one: &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu 713&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;sensors&quot;&gt;Sensors&lt;/h3&gt;

&lt;p&gt;Search: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HC-SR04&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VL53L0X&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MPU6050 GY-521&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đĩa encoder TT 20 xung&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified: &lt;a href=&quot;https://hshop.vn/cam-bien-sieu-am-srf04&quot;&gt;HC-SR04, 27 000 ₫&lt;/a&gt;. Echo is a 5 V output. Buy the shifter with it.&lt;/li&gt;
  &lt;li&gt;Verified shifter: &lt;a href=&quot;https://hshop.vn/mach-chuyen-muc-tin-hieu-logic-4-kenh&quot;&gt;4-channel logic level, 10 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Verified later row: &lt;a href=&quot;https://hshop.vn/cam-bien-6-dof-bac-tu-do-gy-521-mpu6050&quot;&gt;GY-521 MPU6050, 85 000 ₫&lt;/a&gt;. I²C, 3.3–5 V supply on that listing, 3.3 V logic. Not required to blink an LED.&lt;/li&gt;
  &lt;li&gt;Verified ToF substitute: &lt;a href=&quot;https://hshop.vn/cam-bien-khoang-cach-tof-laser-radar-vl53l0x&quot;&gt;VL53L0X module, 115 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Encoder disc, verified but easy to lose: &lt;a href=&quot;https://hshop.vn/dia-encoder-20-xung-dong-co-dc-giam-toc-v1&quot;&gt;20-count disc, 1 000 ₫&lt;/a&gt;. The sensor module is a separate row; search &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cảm biến tốc độ encoder hồng ngoại&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Shopee: &lt;a href=&quot;https://shopee.vn/search?keyword=hc-sr04&quot;&gt;HC-SR04&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;power&quot;&gt;Power&lt;/h3&gt;

&lt;p&gt;Search: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pin 18650 2500mAh&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sạc 18650 4 ngăn&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TP4056&lt;/code&gt; only if you know it is one cell.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified cells: &lt;a href=&quot;https://hshop.vn/pin-sac-18650-li-ion-rechargeable-battery-3-7v-2500mah-10c-sunpower&quot;&gt;Sunpower 2500 mAh, 45 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/pin-sac-18650-li-ion-rechargeable-battery-3-7v-2000mah-10c-sunpower&quot;&gt;2000 mAh, 35 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Verified holder, solder tabs: &lt;a href=&quot;https://hshop.vn/hop-pin-2-co-18650&quot;&gt;2×18650 holder, 4 000 ₫&lt;/a&gt;. The chassis kit may already include a spring holder. Do not buy both and then series four cells.&lt;/li&gt;
  &lt;li&gt;Verified solderless holder: &lt;a href=&quot;https://hshop.vn/hop-pin-ghep-noi-diy-solderless-18650-battery-holder&quot;&gt;DIY 18650 holder, 10 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Verified bay charger: &lt;a href=&quot;https://hshop.vn/bo-sac-pin-18650-li-ion-usb-battery-charger-yh-18650-4&quot;&gt;4-bay USB charger, 68 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Verified single-cell board, &lt;strong&gt;not&lt;/strong&gt; for the 2S pack: &lt;a href=&quot;https://hshop.vn/mach-sac-pin-tp4056-lithium-battery-charge-controller-usb-c&quot;&gt;TP4056 USB-C, 10 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Shopee search: &lt;a href=&quot;https://shopee.vn/search?keyword=s%E1%BA%A1c%20pin%2018650%204%20ng%C4%83n&quot;&gt;sạc pin 18650&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Avoid “sạc nhanh 2A” boards with no current resistor you can see, and any cell whose label exceeds 3500 mAh.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;bench-tools-and-prototype&quot;&gt;Bench tools and prototype&lt;/h3&gt;

&lt;p&gt;Search: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;đồng hồ vạn năng&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mỏ hàn 60W&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;breadboard 830&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dây breadboard đực cái&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Verified meters: &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UNI-T UT33D+, 285 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-ky-thuat-so-vom-wadfow-wdm1501-digital-multimeter-true-rms&quot;&gt;Wadfow WDM1501, 185 000 ₫&lt;/a&gt;. The UT136C+ at 560 000 ₫ is a later upgrade, not the student default.&lt;/li&gt;
  &lt;li&gt;Verified iron: &lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;Wadfow WEL3616 60 W, 75 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;stand, 40 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/thiec-han-sunchi-kp26-0-8mm-sn63-pb37-solder-wire&quot;&gt;0.8 mm Sn63 solder, 24 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Verified cutters: &lt;a href=&quot;https://hshop.vn/kim-cat-day-dien-nho-170-cat-chan-linh-kien-dien-tu&quot;&gt;170 mm flush cutter, 35 000 ₫&lt;/a&gt;. Stripper, if you want one tool that does not nick the copper: &lt;a href=&quot;https://hshop.vn/kim-tuot-day-dien-da-nang-wadfow-wbq8401-wire-stripper&quot;&gt;Wadfow WBQ8401, 130 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Verified prototype: &lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;830-point breadboard, 35 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/day-cam-breadboard-duc-duc-20cm-cap-det-40-soi-m-m-jumper-wire&quot;&gt;M-M jumpers, 30 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/day-cam-breadboard-duc-cai-20cm-cap-det-40-soi-m-f-jumper-wire&quot;&gt;M-F jumpers, 30 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/mach-cap-nguon-cho-breadboard-400-830-lo-mb-102&quot;&gt;breadboard power module, 25 000 ₫&lt;/a&gt;. Skip the power module until you know which rail is 3.3 V and which is 5 V.&lt;/li&gt;
  &lt;li&gt;Verified cable: &lt;a href=&quot;https://hshop.vn/cap-micro-usb-to-usb-2-0-dai-1m-cao-cap-60136-chinh-hang-ugreen&quot;&gt;Ugreen Micro-USB 1 m, 54 000 ₫&lt;/a&gt;. A 30 cm cable at 27 000 ₫ exists and is annoyingly short on a robot. Match USB-C if your board is USB-C.&lt;/li&gt;
  &lt;li&gt;Verified small parts: &lt;a href=&quot;https://hshop.vn/bo-5-loai-led-sieu-sang-3mm-thong-dung-5-kind-3mm-transparent-color-led&quot;&gt;3 mm LED assortment, 20 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/nut-nhan-nha-tron-pbs-11b-12mm-kem-cap&quot;&gt;round pushbutton, 10 000 ₫&lt;/a&gt;, &lt;a href=&quot;https://hshop.vn/day-dien-do-den&quot;&gt;red/black wire, 1 m, 8 000 ₫&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Resistor assortments were &lt;strong&gt;not&lt;/strong&gt; on the first Hshop page for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;điện trở&lt;/code&gt; (that search returned shunts and LCDs). Use Shopee search &lt;a href=&quot;https://shopee.vn/search?keyword=b%E1%BB%99%20%C4%91i%E1%BB%87n%20tr%E1%BB%9F%201%2F4W&quot;&gt;bộ điện trở 1/4W&lt;/a&gt; and expect roughly &lt;strong&gt;30 000–80 000 ₫&lt;/strong&gt;. You want 220 Ω, 330 Ω, 1 kΩ, and 10 kΩ in the mix.&lt;/li&gt;
  &lt;li&gt;Shopee tool search: &lt;a href=&quot;https://shopee.vn/search?keyword=m%E1%BB%8F%20h%C3%A0n%2060W&quot;&gt;mỏ hàn 60W&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=%C4%91%E1%BB%93ng%20h%E1%BB%93%20v%E1%BA%A1n%20n%C4%83ng&quot;&gt;đồng hồ vạn năng&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;IC Đây Rồi for passives if you are in Thủ Đức: &lt;a href=&quot;https://icdayroi.com/&quot;&gt;icdayroi.com&lt;/a&gt;. Thế Giới IC: &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;thegioiic.com&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dupont jumpers are signal wires. Motor current belongs on the thicker red/black lead, screwed or soldered to the driver terminals, not on a breadboard row.&lt;/p&gt;

&lt;h2 id=&quot;worked-cart-lans-track-a-under-2-000-000-&quot;&gt;Worked cart: Lan’s Track A under 2 000 000 ₫&lt;/h2&gt;

&lt;p&gt;Lan is on Track A. She wants Wi-Fi teleop in Chapter 08, so the microcontroller is the ESP32 NodeMCU-32S, not the Pico. She has no lithium experience, so the cart includes a bay charger and forbids a TP4056 across the holder. She will pull two motors out of the 4WD kit, fit a caster, and keep the other two motors as spares. Prices are the Hshop snapshots above.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Line&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Qty&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Unit (₫)&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Line (₫)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 NodeMCU-32S CH340&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;190 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;190 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MKE-R01 4WD chassis (use 2 motors)&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;245 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;245 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Caster&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;TB6612FNG&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;45 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;45 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;HC-SR04&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;27 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;27 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4-channel level shifter&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;10 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;10 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Breadboard 830&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Jumper M-M and M-F&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;30 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;60 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Ugreen Micro-USB 1 m&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;54 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;54 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;UNI-T UT33D+&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;285 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;285 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Iron 60 W + stand&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1+1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;75 000 + 40 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;115 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Solder 0.8 mm&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;24 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;24 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Flush cutter&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;18650 2500 mAh&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;45 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;90 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4-bay charger&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;68 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;68 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MPU6050 (optional, she keeps it)&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;85 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;85 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;LED assortment + button + 1 m wire&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;20 000 + 10 000 + 8 000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;38 000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Parts&lt;/strong&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;strong&gt;1 421 000&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A resistor assortment at a stated Shopee band of 50 000 ₫, plus a typical inner-city ship of 30 000 ₫, lands near &lt;strong&gt;1 500 000 ₫&lt;/strong&gt;. That is inside a 1 500 000–2 500 000 ₫ student budget with room for M3 standoffs and a second USB cable. Swapping the meter for the Wadfow at 185 000 ₫ saves 100 000 ₫. Swapping the ESP32 for a Pico 2 at 195 000 ₫ does not save money and postpones Wi-Fi. Adding a second TB6612 instead of the caster, and driving all four motors as two pairs, is a different robot; Lan writes that as a rejected alternate, not as a silent edit.&lt;/p&gt;

&lt;p&gt;She does not add an L298N “because it is also 45 000”. At 7.4 V and a 2.0 V drop the motors would see 5.4 V. On the Hshop motor listing, 5–6 V is 200 rpm and 0.8 kg·cm at 6 V. She wants the 7 V side of that curve, so the MOSFET driver stays.&lt;/p&gt;

&lt;h2 id=&quot;lab-build-bomcsv-and-inspect-the-idea-of-the-kit&quot;&gt;Lab: build &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; and inspect the idea of the kit&lt;/h2&gt;

&lt;p&gt;Create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bom.csv&lt;/code&gt; in your notes folder with this header:&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;part_id,name,qty,role,rating,vendor,url_or_search,unit_vnd,line_vnd,substitute,wave,received,photo,notes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Procedure&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Copy every line from Lan’s table, then delete or defer the MPU6050 if it blows your budget. Add a resistor-assortment row whose price is “Shopee band, confirm listing” until you have a real number.&lt;/li&gt;
  &lt;li&gt;Add three columns you will actually use: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;received&lt;/code&gt; (yes/no), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;photo&lt;/code&gt; (filename), and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes&lt;/code&gt; (chip marking you can read).&lt;/li&gt;
  &lt;li&gt;For the driver row, write the VM range and the sentence “not L298N” in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;For the charger row, write “one cell per bay, never across 2S holder”.&lt;/li&gt;
  &lt;li&gt;If the parcel has not arrived, set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;received&lt;/code&gt; to no and still finish the sheet. Photograph the meter and the board the day they do arrive.&lt;/li&gt;
  &lt;li&gt;Compute the column sum in a spreadsheet. If it exceeds 2 500 000 ₫, the first cuts are the IMU, the stripper, and the breadboard power module, in that order. Do not cut the meter or the driver.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;You should see&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A sum you can explain to a classmate in one minute, a driver row whose substitute is DRV8833 and whose rejected part is L298N, and a charger row that cannot be satisfied by a TP4056 on a series holder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Faults&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you notice&lt;/th&gt;
      &lt;th&gt;What it usually means&lt;/th&gt;
      &lt;th&gt;Fix&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Cart looks cheap but the driver is L298N&lt;/td&gt;
      &lt;td&gt;Drop was ignored&lt;/td&gt;
      &lt;td&gt;Replace the row; do not “fix” it in software&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ESP32 row links to a WROOM module&lt;/td&gt;
      &lt;td&gt;No USB&lt;/td&gt;
      &lt;td&gt;Replace with a dev board that has a USB socket&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Two cells plus one TP4056&lt;/td&gt;
      &lt;td&gt;Charger voltage is 4.2 V, pack is 8.4 V&lt;/td&gt;
      &lt;td&gt;Bay charger, cells charged alone&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9900 mAh in the capacity column&lt;/td&gt;
      &lt;td&gt;Counterfeit claim&lt;/td&gt;
      &lt;td&gt;Refuse the listing&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sum uses a 30 cm cable&lt;/td&gt;
      &lt;td&gt;Robot USB tether will not reach&lt;/td&gt;
      &lt;td&gt;Buy the 1 m data cable&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Motors on Dupont wires in the notes&lt;/td&gt;
      &lt;td&gt;Breadboard will brown out or melt&lt;/td&gt;
      &lt;td&gt;Screw terminals and the red/black lead&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;strong&gt;Safety.&lt;/strong&gt; Lithium cells are the hazard in this lesson, not the spreadsheet. Charge where you can see the cell. Do not short the holder. Do not connect VM to the ESP32 5 V or 3.3 V pin to “test the battery”.&lt;/p&gt;

&lt;h2 id=&quot;explanation-auditing-a-robot-bom-with-a-quantitative-compatibility-check&quot;&gt;Explanation: Auditing a Robot BOM with a Quantitative Compatibility Check&lt;/h2&gt;

&lt;p&gt;Before purchasing any kit, run a 4-step paper audit on your BOM:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Check the voltage tree:&lt;/strong&gt; Map every component to its exact rail (raw battery &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VBAT&lt;/code&gt;, regulated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5V&lt;/code&gt;, or MCU &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.3V&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Sum worst-case current per rail:&lt;/strong&gt; Add logic idle currents plus simultaneous motor stall currents to size the buck converter and motor driver.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify driver voltage drop:&lt;/strong&gt; Bipolar drivers like the L298N lose \(V_{\text{drop}} \approx 2.0\text{–}3.0\text{ V}\) internally, whereas MOSFET drivers (TB6612FNG, DRV8833) drop only \(I \cdot R_{\text{DS(on)}} \approx 0.2\text{–}0.5\text{ V}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Audit spare margin:&lt;/strong&gt; Include 10–15% spares on consumables (JST connectors, M3 screws, fuses, resistors) so one bent pin does not stall a weekend lab.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-comparing-l298n-vs-tb6612fng-on-your-2s-battery-bom&quot;&gt;Concrete Worked Example: Comparing L298N vs. TB6612FNG on Your 2S Battery BOM&lt;/h3&gt;

&lt;p&gt;Suppose your BOM uses a 2S Li-ion pack (\(V_{\text{bat}} = 7.4\text{ V}\)) driving two 6 V DC gearmotors drawing \(I = 1.0\text{ A}\) each under heavy load (\(2.0\text{ A}\) total):&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Metric&lt;/th&gt;
      &lt;th&gt;L298N (BJT H-Bridge)&lt;/th&gt;
      &lt;th&gt;TB6612FNG (MOSFET H-Bridge)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Typical voltage drop \(V_{\text{drop}}\) at 1 A/ch&lt;/td&gt;
      &lt;td&gt;\(\approx 2.4\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(I \cdot R_{\text{on}} = 1.0 \times 0.5\,\Omega = 0.5\text{ V}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Voltage actually reaching motor&lt;/td&gt;
      &lt;td&gt;\(7.4 - 2.4 = 5.0\text{ V}\)&lt;/td&gt;
      &lt;td&gt;\(7.4 - 0.5 = 6.9\text{ V}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Power wasted as heat (2 channels)&lt;/td&gt;
      &lt;td&gt;\(2 \times (2.4 \times 1.0) = 4.8\text{ W}\)&lt;/td&gt;
      &lt;td&gt;\(2 \times (0.5 \times 1.0) = 1.0\text{ W}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Electrical efficiency \(\eta\)&lt;/td&gt;
      &lt;td&gt;\(5.0 / 7.4 \approx 67.6\%\)&lt;/td&gt;
      &lt;td&gt;\(6.9 / 7.4 \approx 93.2\%\)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Look at that result: swapping a single line item on the BOM cuts wasted driver heat by nearly &lt;strong&gt;5×&lt;/strong&gt; (from 4.8 W down to 1.0 W) and boosts motor efficiency from 67.6% to 93.2%!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Ordering motors rated for 12 V alongside a 2S (7.4 V) battery pack and wondering why the robot barely creeps across carpet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If your 5 V buck converter supplies 0.35 A to the MCU + sensors, its output power is \(5.0 \times 0.35 = 1.75\text{ W}\); at 85% efficiency it draws \(1.75 / (0.85 \times 7.4) \approx 0.28\text{ A}\) from the 7.4 V battery.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;A 2S pack sits at 8.0 V. The L298N drops 2.2 V and the TB6612 path drops 0.4 V. What voltage reaches one TT motor in each case, and which one is inside the comfortable middle of the 3–9 V motor listing?&lt;/li&gt;
  &lt;li&gt;Your holder is marked nối tiếp. You own one TP4056. Describe the exact sequence that charges both cells without putting the charger across the series pair.&lt;/li&gt;
  &lt;li&gt;A Shopee title says “ESP32 WiFi Bluetooth” and the photo is a metal-covered module with castellated edges and no USB connector. What row of the BOM is this, and what extra part is still missing?&lt;/li&gt;
  &lt;li&gt;HC-SR04 echo is 5 V. Your GPIO is 3.3 V. Name the part you add, and name one measurement that proves you have not connected echo straight to the pin.&lt;/li&gt;
  &lt;li&gt;The parts sum is 1 420 000 ₫ before shipping. Shipping is 35 000 ₫ and a resistor kit is 55 000 ₫. Is Lan still inside a 2 000 000 ₫ cap if she also adds a second TB6612 at 45 000 ₫? Show the arithmetic.&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. L298 path: $$8.0 - 2.2 = 5.8\,\text{V}$$. TB6612 path: $$8.0 - 0.4 = 7.6\,\text{V}$$. Both are inside 3–9 V. 7.6 V is the stronger, faster point on the listing&apos;s 6 V torque figure; 5.8 V is the sluggish end.
2. Remove the cells. Charge cell A in one bay until the charger stops near 4.2 V. Repeat for cell B. Measure both. Only then insert them into the series holder. The TP4056 may charge one loose cell; it must not be clipped to the two holder terminals.
3. It is a bare module, not the MCU dev-board row. You still need a dev board or a USB-UART programmer and a 3.3 V regulator. Do not start the course on a bare module.
4. Add the 4-channel level shifter (or a divider on echo). With the sensor powered and echo not yet on the GPIO, a meter on the echo pin during a ranging pulse must not be wired to the ESP32. The safe check is continuity: echo net and GPIO net are not the same net until the shifter low-voltage side is in between.
5. $$1\,420\,000 + 35\,000 + 55\,000 + 45\,000 = 1\,555\,000$$. Yes, still under 2 000 000 ₫. The second driver is a design change (four motors), not a free spare, so the notes column should say so.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;TB6612FNG product page and ratings, Pololu 713&lt;/a&gt; — MOSFET carrier the Hshop module is copying.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ti.com/product/DRV8833&quot;&gt;TI DRV8833 product folder&lt;/a&gt; — current limits and VM range for the cheap substitute.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/category/11/brushed-dc-motor-drivers&quot;&gt;Pololu brushed DC motor driver comparison&lt;/a&gt; — why “2 A” on a Darlington board is not the whole story.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;SparkFun logic levels&lt;/a&gt; — 5 V echo versus 3.3 V GPIO.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://batteryuniversity.com/article/bu-409-charging-lithium-ion&quot;&gt;Battery University, BU-409 charging lithium-ion&lt;/a&gt; — 4.20 V per cell, why series packs need a charger that understands series.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf&quot;&gt;Espressif ESP32 datasheet&lt;/a&gt; — absolute maximum on a GPIO is not 5 V.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf&quot;&gt;HC-SR04 module note, SparkFun&lt;/a&gt; — trigger and echo timing; treat echo as 5 V.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>The bench catalog: tools you can trust</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter00/00_04_kit_catalog_tooling/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter00/00_04_kit_catalog_tooling</id>
   <content type="html">&lt;p&gt;A seasoned roboticist can diagnose a stubborn motor driver in two minutes not because of superhuman intuition, but because they never guess what a multimeter can measure. Your workbench tools — multimeter, temperature-controlled soldering iron, wire strippers, and calipers — are the &lt;strong&gt;sensory organs&lt;/strong&gt; of an engineer.&lt;/p&gt;

&lt;p&gt;This lesson explains what each bench tool actually measures, how measurement error propagates into your robot’s math, and why investing in a few dependable instruments saves dozens of hours of phantom debugging.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Plan about 90 minutes. If you already have a meter, prove it on a known short and put it away. If you do not, finish the Wave 1 inventory and photograph the gaps. Do not heat an iron in this lesson.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will &lt;strong&gt;say&lt;/strong&gt; what each Wave 1 tool is for, and &lt;strong&gt;state&lt;/strong&gt; the observation that proves it works. You will &lt;strong&gt;select&lt;/strong&gt; DC volts and continuity on a meter, and you will &lt;strong&gt;refuse&lt;/strong&gt; a resistance measurement on a rail that is still powered. You will &lt;strong&gt;describe&lt;/strong&gt; a solder joint as the result of heating the parts, not of melting a blob onto cold metal, using 0.8 mm Sn63 and a stand. You will &lt;strong&gt;distinguish&lt;/strong&gt; a breadboard, which is for signals, from a path that must carry motor current. You will &lt;strong&gt;separate&lt;/strong&gt; side cutters from strippers by the damage each one avoids. You will &lt;strong&gt;inventory&lt;/strong&gt; the bench against Wave 1, photograph it, and write the gaps down so lesson 00-05 is a shopping list rather than a guess.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;The power-up card from lesson 00-03 exists, at least on paper. You know motor VM is not the 3.3 V pin, and you know an iron needs a stand and eye protection before it needs a plug. Chapter 01 is where probing becomes a measured lab. Today you are learning which tool answers which question, so that lab is not a walk to the shop in the middle of an exercise.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-metrology-measurement-loading-and-mechanical-precision&quot;&gt;Key ideas: Metrology, Measurement Loading, and Mechanical Precision&lt;/h2&gt;

&lt;p&gt;Three foundational measurement concepts govern every tool on your bench:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Accuracy, Precision, and Resolution:&lt;/strong&gt; A multimeter with 4000 counts on the 4 V range has resolution \(0.001\text{ V}\), while its accuracy spec (say \(\pm(0.5\% + 2\text{ digits})\)) bounds systematic error. Knowing both prevents you from chasing 5 mV “drifts” that are merely meter tolerance.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Instrument Loading Effect:&lt;/strong&gt; An ideal voltmeter has infinite input impedance (typically \(10\text{ M}\Omega\) in digital multimeters) so it draws negligible current; an ideal ammeter has zero shunt resistance. Probing a high-impedance voltage divider with a low-impedance meter distorts the very voltage you are trying to read.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Mechanical Metrology for Kinematics:&lt;/strong&gt; Your odometry equations depend directly on measured wheel radius \(R\) and track width \(L\). A 1 mm caliper error on a 65 mm wheel creates a systematic 1.5% distance drift on every single meter the robot drives.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Chapter 07 fails in boring ways: a jumper that looks seated and is open inside the crimp, a ground you “measured” while the meter was still in ohms on a live rail, a motor wire asked to live in a breadboard spring. Those failures look like firmware bugs. They will send you into ROS 2 tutorials that cannot beep a wire. The tools in this catalog are how you separate “the PWM timeout is wrong” from “the wire is wrong” before a topic graph exists to confuse you further.&lt;/p&gt;

&lt;h2 id=&quot;what-works-means-for-each-tool&quot;&gt;What “works” means for each tool&lt;/h2&gt;

&lt;p&gt;Wave 1, copied from the bill of materials so this lesson and lesson 00-05 agree, is the bench that lets Chapter 01 start: a multimeter, a 60 W iron with a stand, solder, side cutters, a solderless breadboard, jumper wires, a data-capable USB cable, the microcontroller, a few LEDs, and a tactile button. Wave 2 is the chassis, the TB6612FNG, and the HC-SR04. Wave 3 is the cells and a charger that charges one cell at a time. This lesson is Wave 1’s tools. The microcontroller itself is a line on the inventory, not a tool you learn to swing.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/kit_catalog_overview.png&quot; alt=&quot;A family portrait of the bench, not a branded kit&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The drawing is kinds of objects. The meter, the iron, the breadboard, and the hand tools are the must-have wave. A bench supply and a logic analyzer, if the picture includes them as later instruments, are not today’s purchase. The bottom row, the board and the robot, is bought from lesson 00-05 when you are ready, not duplicated here.&lt;/p&gt;

&lt;h3 id=&quot;the-meter&quot;&gt;The meter&lt;/h3&gt;

&lt;p&gt;A digital multimeter answers three questions you will ask constantly, and one question you must not ask by accident.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DC volts&lt;/strong&gt; measures a difference. The dial is on V with a straight line. Black on ground, red on the point you care about. A healthy USB 5 V pin is near 5 V. A healthy 3.3 V pin is roughly 3.2–3.4 V. A reading of 0.00 V means off, wrong pin, or not actually on ground. It does not mean the meter is broken until continuity has proved the leads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Continuity&lt;/strong&gt; beeps when the resistance between the probes is very small. Touch the probes together. You should hear a beep and see a display near 0 Ω, sometimes a few tenths of an ohm of lead resistance. That beep is the proof the meter works. A good short jumper beeps. &lt;strong&gt;OL&lt;/strong&gt; means the meter cannot see a path.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ohms&lt;/strong&gt; keeps a number when there is no beep. Measure resistance only on an &lt;strong&gt;unpowered&lt;/strong&gt; part. The meter injects a small current. On a powered rail that current fights the supply, the reading is nonsense, and you can damage the meter or the board. Never ohms on a powered rail.&lt;/p&gt;

&lt;p&gt;Current mode is the trap. The red lead moves to a jack marked A or mA, and the meter becomes a near short. Probing a voltage that way shorts the source. Look at the jack before every voltage measurement.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Digital_Multimeter_Aka.jpg&quot; alt=&quot;A handheld meter; the jacks matter as much as the dial&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Yours may be a UNI-T UT33D+ or a Wadfow. Either is enough if it beeps and reads DC volts. The proof of life is that beep, before you trust a number on the robot.&lt;/p&gt;

&lt;h3 id=&quot;the-iron-the-stand-and-the-solder&quot;&gt;The iron, the stand, and the solder&lt;/h3&gt;

&lt;p&gt;A joint is a metallurgical event between the pad and the pin. You heat those two pieces, then you let a little solder flow onto the hot metal. If you melt a blob on the tip and dab it onto a cold pin, you get a ball that looks occupied and falls off next week. That is the whole technique this lesson needs. Chapter 01 will ask you to solder; today you only need to know what “good” will look like: the solder wet both surfaces, the joint is shiny rather than a sphere sitting on top, and the neighbouring pad did not bridge.&lt;/p&gt;

&lt;p&gt;The alloy on the course list is 0.8 mm Sn63/Pb37. The diameter is small enough for a header pin. The stand is not optional: a 60 W tip left on a cable will end the lab. Eye protection stays on when leads are cut, because the offcut is a projectile. Flux fumes are a reason to open a window, not a reason to hover.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/Soldering_iron.jpg&quot; alt=&quot;An iron without a stand is a picture of the hazard, not the setup you want&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Read the photograph as a bare iron, which is the hazard. Your iron goes in the stand. You will know the pair works when the iron stays put while you reach for solder. Chapter 00 does not require a practice joint.&lt;/p&gt;

&lt;h3 id=&quot;the-breadboard-is-not-a-motor-path&quot;&gt;The breadboard is not a motor path&lt;/h3&gt;

&lt;p&gt;A solderless breadboard is a grid of spring clips under plastic. Holes in the same numbered row are connected. A trench down the middle lets a dual-inline chip straddle the board without shorting its two sides. The long strips along the edge are power rails, and on many 830-point boards those rails are &lt;strong&gt;split in the middle&lt;/strong&gt;. A rail that does not beep from one end to the other is not a mystery. It is the gap the plastic already told you about.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/wikimedia/breadboard.jpg&quot; alt=&quot;A solderless breadboard: fine for signals, wrong for motor current&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The breadboard works when a jumper beeps from one end of a row to the other, and stays silent across the centre trench. Motor current does not belong in those springs. A stalled TT motor is a large fraction of an ampere, and the clip’s resistance turns that into heat and a voltage drop. Signals live on the breadboard. Motor leads wait for the TB6612FNG screw terminals.&lt;/p&gt;

&lt;h3 id=&quot;cutters-and-strippers&quot;&gt;Cutters and strippers&lt;/h3&gt;

&lt;p&gt;Side cutters shear a lead. They are the 170-size cutter on the shopping list. They work when a lead comes off cleanly and the offcut is caught. They nick copper if you use them as strippers, and the wire breaks later at the nick. Strippers remove insulation without biting the conductor. A knife is a worse substitute and belongs on the gap list. Jumpers also fail inside the crimp while the plastic looks fine, so a silent motor with a pretty PWM wire is a beep test.&lt;/p&gt;

&lt;h3 id=&quot;later-and-not-now&quot;&gt;Later, and not now&lt;/h3&gt;

&lt;p&gt;A current-limited supply, a logic analyzer, and an oscilloscope make later debugging faster. None of them is required to pass Chapter 00, and none of them repairs a missing common ground. Write them down as later.&lt;/p&gt;

&lt;h2 id=&quot;lab-inventory-against-wave-1&quot;&gt;Lab: inventory against Wave 1&lt;/h2&gt;

&lt;p&gt;Clear a corner of the desk. Lay out every Wave 1 item you actually possess. Do not borrow objects into the photo from another room “so it looks complete.”&lt;/p&gt;

&lt;p&gt;The lines are: multimeter, iron, stand, solder, side cutters, breadboard, jumper wires, data-capable USB cable, microcontroller, at least one LED, at least one tactile button. For each line write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;here&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gap&lt;/code&gt;. Photograph the layout. Put the photo in the journal. In the serial-snippet field, write either the port name if the board enumerates on USB alone, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cable unproven — board not here&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cable unproven — no port appeared&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then prove the meter if you have one. Probes touching: beep. Record “beep on known short.” No beep means the meter is not a tool yet. Do not measure ohms on anything plugged in. No meter means &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;meter missing&lt;/code&gt;, and Chapter 01’s measurement labs wait. If you have a breadboard, beep one row and confirm the centre trench is silent. If you have an iron, confirm the stand and leave the iron unplugged. The inventory does not need a hot tip.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you should see.&lt;/strong&gt; A photo, a gap list, and a beep or an explicit missing-meter line.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it goes wrong.&lt;/strong&gt; A friend’s meter in the photo does not count. A beep on a powered board is not an ohms test; unplug first. Motor wires in the breadboard come out. One meter is the line, not two.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Quân’s photo shows a Wadfow meter, a 60 W iron, no stand, a coil of 0.8 mm solder, cutters, no breadboard, a charge-only USB cable he has not proved, and no microcontroller. He writes gaps: stand, breadboard, jumpers, data cable, MCU, LED, button. He touches the meter probes together and gets a beep, so the meter line is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;here&lt;/code&gt;, with the evidence “beep on known short.” He does not ohm-test the USB port while the cable is plugged into the laptop.&lt;/p&gt;

&lt;p&gt;He prices only tool gaps, using the 23 September 2026 Hshop snapshots, and leaves the MCU to lesson 00-05. He already owns the iron, the solder, and the cutters. His gap today is the stand and the breadboard:&lt;/p&gt;

\[40000 + 35000 = 75000\]

&lt;p&gt;đồng, plus jumpers and a data cable from lesson 00-05, plus the board. A full tool set for someone with an empty drawer, one meter not two, is the UT33D+ at 285000 ₫, or the Wadfow at 185000 ₫, plus iron 75000 ₫, stand 40000 ₫, solder 24000 ₫, cutters 35000 ₫, and an 830-point breadboard at 35000 ₫. With the Wadfow meter that sum is&lt;/p&gt;

\[185000 + 75000 + 40000 + 24000 + 35000 + 35000 = 394000\]

&lt;p&gt;đồng. With the UT33D+ the empty-drawer total is 494000 ₫. Quân writes both figures, dated 23 Sep 2026, and does not order a scope.&lt;/p&gt;

&lt;h2 id=&quot;explanation-how-a-1-mm-caliper-error-propagates-into-odometry-drift&quot;&gt;Explanation: How a 1 mm Caliper Error Propagates into Odometry Drift&lt;/h2&gt;

&lt;p&gt;Why do we bother owning vernier calipers instead of eyeballing a wheel with a plastic ruler? Let’s trace the error propagation step by step:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Write the forward distance equation:&lt;/strong&gt; After \(N\) full wheel revolutions, the firmware estimates distance traveled as \(\hat{s} = 2\pi \hat{R} N\), whereas true distance is \(s = 2\pi R N\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute relative error:&lt;/strong&gt; \(\frac{\hat{s} - s}{s} = \frac{\hat{R} - R}{R} = \frac{\Delta D}{D}\). Relative diameter error maps 1-to-1 into relative distance error.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Trace heading error during a turn:&lt;/strong&gt; For an in-place rotation \(\Delta\theta = \frac{s_R - s_L}{L}\), an error \(\Delta L\) in track width \(L\) causes a proportional heading error \(\frac{\Delta\theta_{\text{err}}}{\Delta\theta} \approx -\frac{\Delta L}{L}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Combine with electrical measurement error:&lt;/strong&gt; Knowing meter accuracy tells you whether a battery reading of 7.35 V is genuinely sagging or within spec.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-caliper-measurement-error--multimeter-accuracy-spec&quot;&gt;Concrete Worked Example: Caliper Measurement Error &amp;amp; Multimeter Accuracy Spec&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Part A — Wheel diameter error over a 10 m hallway run:&lt;/strong&gt;
Suppose the true wheel diameter is \(D = 65.0\text{ mm}\), but a rough ruler estimate records \(\hat{D} = 66.0\text{ mm}\) (\(\Delta D = +1.0\text{ mm}\)).
    &lt;ul&gt;
      &lt;li&gt;Relative error: \(\frac{1.0}{65.0} \approx +1.538\%\).&lt;/li&gt;
      &lt;li&gt;Over a \(10.00\text{ m}\) run, your robot’s odometry reports \(10.00 \times 1.01538 = 10.154\text{ m}\) — an error of &lt;strong&gt;15.4 cm&lt;/strong&gt; purely from a 1 mm ruler mistake!&lt;/li&gt;
      &lt;li&gt;With digital calipers accurate to \(\pm 0.05\text{ mm}\), that systematic error drops to \(1000\text{ cm} \times (0.05 / 65) \approx 0.77\text{ cm}\) — a 20× improvement.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Part B — Multimeter accuracy bound on a 3.300 V rail:&lt;/strong&gt;
Your DMM is set to the 4.000 V range (resolution \(0.001\text{ V}\)) with accuracy \(\pm(0.5\% + 2\text{ digits})\) and displays &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.300 V&lt;/code&gt;.
    &lt;ul&gt;
      &lt;li&gt;Percentage term: \(0.005 \times 3.300 = 0.0165\text{ V}\).&lt;/li&gt;
      &lt;li&gt;Digit term: \(2 \times 0.001 = 0.002\text{ V}\).&lt;/li&gt;
      &lt;li&gt;Total uncertainty: \(\pm(0.0165 + 0.002) = \pm 0.0185\text{ V}\), so the true voltage lies in \([3.2815\text{ V},\, 3.3185\text{ V}]\).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Measuring current by placing the multimeter probes &lt;em&gt;in parallel&lt;/em&gt; across a battery or power rail while the red lead is plugged into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10A&lt;/code&gt; jack — that creates a dead short through the meter’s internal fuse.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If your robot turns \(360^\circ\) (\(2\pi\text{ rad}\)) and your estimated track width \(\hat{L} = 160\text{ mm}\) is off by \(4\text{ mm}\) (\(2.5\%\)), your estimated heading after one full spin will be off by \(0.025 \times 360^\circ = 9^\circ\).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;The probes are touching and the meter is silent. May you measure the 3.3 V pin and trust the number? What do you check first?&lt;/li&gt;
  &lt;li&gt;You want the resistance of a 10 kΩ resistor that is still plugged into a powered ESP32 rail. Why is that measurement refused, and what do you do instead?&lt;/li&gt;
  &lt;li&gt;A solder joint looks like a ball sitting on the pin. Which step was skipped: heating the joint, or adding solder? How does 0.8 mm Sn63 change the amount you need?&lt;/li&gt;
  &lt;li&gt;A stalled motor lead is pushed into a breadboard hole because the screw terminal is in another room. What fails, electrically, and where should that wire wait?&lt;/li&gt;
  &lt;li&gt;Your Wave 1 photo has a meter and an iron and no stand, no solder, and no cutters. Which of those gaps can burn you or the cable before Chapter 01 even asks you to measure a resistor?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. No. A silent meter with the probes touching has not proved continuity. Check the dial, the lead jacks, and the battery before you trust a voltage.
2. Ohms mode injects current into a live rail. Unplug the board, lift one leg of the resistor if it is in circuit, and measure the part unpowered.
3. The pin and the pad were not heated; solder was dropped on as a blob. Thinner 0.8 mm wire lets you feed a small amount onto a hot joint instead of flooding it.
4. The spring clip is a poor, resistive contact for motor current. It heats and drops voltage. The wire waits for the driver screw terminal or a proper harness. Signals may use the breadboard.
5. The missing stand. A hot iron on the table or on its own cable is the immediate hazard. Missing solder and cutters block joints and lead trimming; they do not replace the stand.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/how-to-use-a-multimeter&quot;&gt;How to use a multimeter (SparkFun)&lt;/a&gt; — volts, continuity, and the current-jack trap, with pictures.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.arduino.cc/&quot;&gt;Arduino language reference&lt;/a&gt; — not a tool manual. It is where the LED and the button on your Wave 1 list will be exercised once the board arrives. Do not start a motor tutorial from the homepage.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam--mua-ở-việt-nam&quot;&gt;Where to buy in Vietnam / Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;Buy the gaps, not a second copy of what the photo already shows. The full robot cart remains &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;lesson 00-05&lt;/a&gt;. Prices below are Hshop snapshots from &lt;strong&gt;23 September 2026&lt;/strong&gt;.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tool&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Snapshot&lt;/th&gt;
      &lt;th&gt;Page&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;UNI-T UT33D+ meter&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;285000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UT33D+&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wadfow WDM1501 meter&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;185000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-ky-thuat-so-vom-wadfow-wdm1501-digital-multimeter-true-rms&quot;&gt;WDM1501&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wadfow 60 W iron&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;75000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;WEL3616&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Round iron stand&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;40000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;stand&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sunchi 0.8 mm Sn63/Pb37&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;24000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/thiec-han-sunchi-kp26-0-8mm-sn63-pb37-solder-wire&quot;&gt;solder&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;170 side cutters&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/kim-cat-day-dien-nho-170-cat-chan-linh-kien-dien-tu&quot;&gt;cutters&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;830-point breadboard&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35000 ₫&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://hshop.vn/test-board-cammb-102&quot;&gt;breadboard&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Pick one meter. Jumpers, LEDs, buttons, and a data USB cable are searches, because this lesson does not invent extra Hshop product slugs. &lt;a href=&quot;https://shopee.vn/search?keyword=d%C3%A2y%20c%E1%BA%AFm%20breadboard&quot;&gt;Shopee, dây cắm breadboard&lt;/a&gt; and &lt;a href=&quot;https://www.lazada.vn/catalog/?q=d%C3%A2y%20jumper%20%C4%91%E1%BB%B1c%20%C4%91%E1%BB%B1c&quot;&gt;Lazada, dây jumper đực đực&lt;/a&gt; are search pages. Strippers, if they are the gap the cutters cannot fill: &lt;a href=&quot;https://shopee.vn/search?keyword=k%C3%ACm%20tu%E1%BB%91t%20d%C3%A2y&quot;&gt;Shopee, kìm tuốt dây&lt;/a&gt;. &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; and &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; are shop homepages for a counter visit, not a SKU. A scope, a logic analyzer, and a current-limited supply stay off this invoice.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>A safety practice for the robot lab</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter00/00_03_safety_mindset_lab/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter00/00_03_safety_mindset_lab</id>
   <content type="html">&lt;p&gt;In 1999, a seemingly minor unit mismatch destroyed the $125 million Mars Climate Orbiter; on a student workbench, a single reversed battery lead or dropped screwdriver across a Li-ion pack can vent a cell in under two seconds. Safety in a robotics lab is not bureaucracy — it is &lt;strong&gt;quantitative hazard analysis&lt;/strong&gt; applied to stored electrical, thermal, and mechanical energy.&lt;/p&gt;

&lt;p&gt;This lesson builds a repeatable pre-power ritual grounded in physics: how much energy sits inside a lithium battery, how fast a short circuit heats a wire, and how mechanical pinch points behave when a firmware bug drives a motor at 100% duty cycle.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Give this lesson 80 minutes. You can pass it with paper before any parcel arrives. If an iron or a lithium cell is already on the desk, the rules apply now.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You will &lt;strong&gt;name&lt;/strong&gt; the three hazards that actually show up on this bench: stored energy, spinning wheels, and firmware that forgets to turn PWM off. You will &lt;strong&gt;write&lt;/strong&gt; a one-page power-up card whose electrical order is common ground, then logic from USB, then signals, then motor VM last, with the wheels already up. You will &lt;strong&gt;apply&lt;/strong&gt; the lithium rule: charge an 18650 one cell at a time to 4.2 V, and never clip a TP4056 across a 2S holder that is an 8.4 V pack when full. You will &lt;strong&gt;reject&lt;/strong&gt; a plan that feeds motor voltage, or a 5 V echo, into a 3.3 V pin, knowing that the ESP32 absolute maximum on a GPIO is not 5 V. You will &lt;strong&gt;run&lt;/strong&gt; a USB-only bring-up checklist with no motors connected, and you will &lt;strong&gt;match&lt;/strong&gt; a symptom to a fix in the fault table rather than to a guess.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lessons 00-01 and 00-02: you can say the 300 ms stop, and the journal has a Track A or Track B sentence. You do not need parts. A GPIO pin is a logic pin, not a power supply, and a wheel on the table becomes a winch the moment PWM is not zero.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-electrical-energy-storage-thermal-runaway-and-mechanical-kill-switches&quot;&gt;Key ideas: Electrical Energy Storage, Thermal Runaway, and Mechanical Kill-Switches&lt;/h2&gt;

&lt;p&gt;Three physical principles explain why lab safety rules are non-negotiable:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Internal Resistance &amp;amp; Short-Circuit Current:&lt;/strong&gt; A lithium-ion cell has very low internal resistance \(R_{\text{int}}\) (typically \(30\text{–}80\text{ m}\Omega\)). When shorted, current spikes to \(I_{\text{sc}} = V / R_{\text{int}}\), dumping tens or hundreds of watts directly into thin jumper wires and the cell’s own chemistry.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Joule Heating &amp;amp; Fuse Clearing:&lt;/strong&gt; Power dissipated in a conductor scales with the &lt;em&gt;square&lt;/em&gt; of current: \(P = I^2 R\). Doubling current quadruples heating rate. A properly sized inline fuse melts intentionally in milliseconds before wire insulation ignites.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Fail-Safe Architecture (Hardware vs. Software E-Stop):&lt;/strong&gt; Software can hang in an infinite loop while PWM timers keep driving a motor. True safety requires a physical power cutoff (battery switch / pull-plug) and lifting drive wheels off the bench during every untested firmware flash.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;The Chapter 07 demo moves when you ask and goes quiet when commands stop. It is not trustworthy if the first power-up can walk off the desk or put a motor pack into a 3.3 V pin. The timeout runs only after the chip is alive. ROS 2 topics will not debug a pad you already destroyed.&lt;/p&gt;

&lt;h2 id=&quot;three-hazards-and-the-order-that-keeps-them-boring&quot;&gt;Three hazards, and the order that keeps them boring&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Stored energy&lt;/strong&gt; dumps current without asking: a USB port, AA cells, a pair of 18650s, a capacitor on a driver. The damage is heat in a trace, a pin, or a cell charged the wrong way. &lt;strong&gt;Spinning wheels&lt;/strong&gt; pull a cable or walk off the desk the first time PWM comes up wrong. &lt;strong&gt;Firmware that forgets to turn PWM off&lt;/strong&gt; is the teleop program that crashes after “forward,” the serial window you closed, the laptop that sleeps. The 300 ms rule turns stale software into a stop. It does not protect the second before your program starts, and it does not protect a GPIO from overvoltage.&lt;/p&gt;

&lt;p&gt;The ritual fits on one page. Do it every time, including the test you want to call quick. Common ground comes first: microcontroller ground, driver ground, and battery negative are one node, because motor current returns there. A signal connected before that ground can return through a GPIO and destroy it. Logic power is next, USB into the microcontroller, which makes 3.3 V for its pins. The driver’s logic supply comes from that quiet rail, not from the motor battery. Signals come after: PWM, direction, and standby, already meaning stop if firmware is running. Motor VM is last, through a switch if you have one. The wheels are already up. Lifting them after the twitch is how a caster leaves the desk.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_order.png&quot; alt=&quot;Power comes up as common ground, logic, signals, motor supply last, wheels off the desk&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Copy the boxes in that order. “Last” means the switch closes last. The wheels are already up.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/power_rails_3v3.png&quot; alt=&quot;Logic at 3.3 V, USB 5 V, ground, and a separate motor rail&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Use the colours as a ban list. USB 5 V feeds the board, not two stalled TT motors. GPIO is a 3.3 V world. Ground is the node you share on purpose. The motor rail is a separate battery and never lands on a GPIO or on the 3.3 V pin.&lt;/p&gt;

&lt;h2 id=&quot;lithium-in-the-specific-way-people-get-hurt&quot;&gt;Lithium, in the specific way people get hurt&lt;/h2&gt;

&lt;p&gt;A single 18650 lithium-ion cell is about 3.6–3.7 V nominal and &lt;strong&gt;4.2 V full&lt;/strong&gt;. You charge &lt;strong&gt;one cell at a time&lt;/strong&gt; to 4.2 V, on a charger whose label says Li-ion or Li-ion 18650. Two cells in series, which is what a 2S holder does, are about 7.4 V nominal and &lt;strong&gt;8.4 V full&lt;/strong&gt;. That series pair is a motor pack. It is not a charger input.&lt;/p&gt;

&lt;p&gt;A TP4056 tries to bring one cell to 4.2 V. Clipping it across a 2S holder puts that charger across an 8.4 V pack. Do not. Take one cell out, charge it alone, then put it back.&lt;/p&gt;

&lt;p&gt;A cell at about 2.1 V is far below the usual cutoff near 2.5–3.0 V. Charging it at a normal rate can plate metal inside and set up an internal short. Do not “wake” it with a TP4056. Recycle it. A swollen cell is already damaged: do not charge it, puncture it, or keep it “in case.” A bare 18650 in a pocket with keys can weld a short. Cells live in a holder or a case. The first motor tests may use a AA holder so you learn VM versus logic before you own lithium. No cells yet is a safe state. An unmatched charger is not.&lt;/p&gt;

&lt;h2 id=&quot;pins-iron-and-eyes&quot;&gt;Pins, iron, and eyes&lt;/h2&gt;

&lt;p&gt;The ESP32 absolute maximum on a GPIO is &lt;strong&gt;not&lt;/strong&gt; 5 V. The pad sits a few tenths of a volt above the 3.3 V rail, not at 5 V and not at motor voltage. A Pico pin is the same kind of 3.3 V input. An HC-SR04 echo is about 5 V when high. The pack is often 7–8 V. A level shifter belongs on the echo. The pack belongs on VM only.&lt;/p&gt;

&lt;p&gt;Wear eye protection when you cut leads and when you solder. Leads fly. Rosin fumes need moving air, not a closed room and a face over the joint. The iron lives in a stand, not on the cable. You are not soldering today. You are writing that rule where your hands will see it.&lt;/p&gt;

&lt;p&gt;The software half of the ritual is the timeout you already wrote:&lt;/p&gt;

\[t_{\mathrm{silence}} &amp;gt; 300\,\mathrm{ms} \implies \text{PWM duty} = 0.\]

&lt;p&gt;You cannot flash that loop today. Refuse to call a setup ready if the only stop is pulling the USB plug after the robot has left the mat. A silent teleop link must be a stop, not a hold-last-speed cruise.&lt;/p&gt;

&lt;h2 id=&quot;faults-you-should-recognise-before-you-have-them&quot;&gt;Faults you should recognise before you have them&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;What you see or were about to do&lt;/th&gt;
      &lt;th&gt;What it usually is&lt;/th&gt;
      &lt;th&gt;What you do&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Serial port vanishes when the motors are tied to the board’s 5 V pin&lt;/td&gt;
      &lt;td&gt;The USB port cannot feed two stalled motors; the 3.3 V rail collapses&lt;/td&gt;
      &lt;td&gt;Motors on a separate VM, common ground, logic from USB only&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A GPIO is dead after the ultrasonic echo was connected&lt;/td&gt;
      &lt;td&gt;5 V into a pin whose absolute maximum is not 5 V&lt;/td&gt;
      &lt;td&gt;Stop; that pin may be ruined; shifter on echo before any retry&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A TP4056 is clipped across a 2S holder&lt;/td&gt;
      &lt;td&gt;A 4.2 V charger on an 8.4 V pack&lt;/td&gt;
      &lt;td&gt;Remove it; charge one cell at a time, off the robot&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;A cell measures about 2.1 V, or the jacket is swollen&lt;/td&gt;
      &lt;td&gt;Over-discharged or damaged lithium&lt;/td&gt;
      &lt;td&gt;Do not charge; do not pocket it; recycle&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Wheels on the desk, new firmware, you are “just checking”&lt;/td&gt;
      &lt;td&gt;Unexpected motion, and PWM may be nonzero before your loop runs&lt;/td&gt;
      &lt;td&gt;Wheels up first; VM last; timeout still required once code runs&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;The iron is lying on its cable, or your eyes are unprotected while you cut leads&lt;/td&gt;
      &lt;td&gt;A burn, a melted cord, or a lead in an eye&lt;/td&gt;
      &lt;td&gt;Stand, glasses, ventilation; unplug if the setup is already wrong&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;lab-the-card-then-a-usb-only-bring-up&quot;&gt;Lab: the card, then a USB-only bring-up&lt;/h2&gt;

&lt;p&gt;Perform this with motors &lt;strong&gt;out of the circuit&lt;/strong&gt;. If you have no board yet, write the card anyway and put &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;board not arrived&lt;/code&gt; where a port name would go.&lt;/p&gt;

&lt;p&gt;Cut or tear a page. Title it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;POWER UP&lt;/code&gt;. Number four lines: (1) common GND, (2) logic from USB, (3) signals, PWM and direction, already meaning stop, (4) motor VM last. Under them write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WHEELS UP BEFORE VM&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SILENCE &amp;gt; 300 ms → PWM 0&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NEVER: battery or 5 V echo on a 3.3 V GPIO&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;18650: one cell, 4.2 V, never a TP4056 across a 2S holder&lt;/code&gt;. Tape the page where your hands will work. If you have no tape, put it under the keyboard you use for notes.&lt;/p&gt;

&lt;p&gt;Then run the USB-only checklist, in this order.&lt;/p&gt;

&lt;p&gt;Look at the desk and confirm there is no motor, no motor battery, and no VM wire. Disconnect a battery before USB goes in. Wheels, if you own them, are not on a powered driver and are not part of this test. Plug in only the microcontroller, on a cable you believe can carry data. Look for a serial port or, on a Pico in BOOTSEL, a drive. Paste the port name, or write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;none — board not arrived&lt;/code&gt; and the plan “when it arrives, USB only, no VM, record the port, then unplug.” Unplug. Do not attach motors to celebrate.&lt;/p&gt;

&lt;p&gt;If you already own lithium cells, photograph the charger label and type the chemistry words you can read. A missing label means &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;will not charge&lt;/code&gt;. No lithium means &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cells not purchased yet&lt;/code&gt;. If you own an iron, confirm the stand before you ever turn it on, and note whether eye protection is on the bench.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you should see.&lt;/strong&gt; The card is visible without opening the lesson. The journal has a photo or sketch of the card, a serial field that is a real port name or an explicit plan, and a charger decision: “matches,” “will not charge,” or “cells not purchased yet.” No motor was connected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it goes wrong.&lt;/strong&gt; If VM is listed first, rewrite the card. Wheels stay up for the first test of new firmware, including on the desk. A video that powers motors from the DevKit 5 V pin is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forbidden&lt;/code&gt;. A charger clipped on because the connector fit comes off. One cell at a time.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;A classmate wants the wheels down “just to see the new sketch.” PWM has not been shown to start at 0, and the 300 ms timer cannot run until setup finishes. Keep the wheels up, VM open, USB logic only, then a first run in the air. Wheels down can drag the board off the desk before that line executes.&lt;/p&gt;

&lt;p&gt;The same afternoon an 18650 reads 2.1 V and someone offers a TP4056 “recovery.” The cell is below a sane cutoff. The note says it will not be charged, pocketed, or clipped across the 2S holder. A healthy cell, if they have one, is charged alone to 4.2 V and only then placed in the holder.&lt;/p&gt;

&lt;p&gt;The echo wire in the same pile was about to land on an ESP32 GPIO. Absolute maximum on that pad is not 5 V, so the wire waits for a level shifter. Motor voltage is the same ban, only larger.&lt;/p&gt;

&lt;h2 id=&quot;explanation-calculating-short-circuit-power-and-fuse-sizing-by-hand&quot;&gt;Explanation: Calculating Short-Circuit Power and Fuse Sizing by Hand&lt;/h2&gt;

&lt;p&gt;Why can a harmless-looking 3.7 V lithium cell melt a jumper wire instantly? Trace the four-step calculation:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Model the battery as an ideal source \(V_{\text{oc}}\) in series with \(R_{\text{int}}\):&lt;/strong&gt; For a fresh 2S Li-ion pack, \(V_{\text{oc}} = 8.4\text{ V}\) and total pack internal resistance \(R_{\text{int}} \approx 0.12\,\Omega\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Add the accidental short path resistance \(R_{\text{wire}}\):&lt;/strong&gt; A 20 cm 26 AWG jumper wire has resistance \(R_{\text{wire}} \approx 0.03\,\Omega\) (total loop \(R_{\text{total}} = 0.15\,\Omega\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute short-circuit current and power:&lt;/strong&gt; \(I_{\text{sc}} = V_{\text{oc}} / R_{\text{total}}\), and power dumped into the thin wire is \(P_{\text{wire}} = I_{\text{sc}}^2 R_{\text{wire}}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Size an inline fuse:&lt;/strong&gt; Pick a fuse rating just above peak normal stall current (e.g., 1.25×–1.5× peak load) so normal driving never blows it, while a dead short clears it rapidly.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-short-circuiting-a-2s-pack-vs-protecting-it-with-a-5-a-fuse&quot;&gt;Concrete Worked Example: Short-Circuiting a 2S Pack vs. Protecting It with a 5 A Fuse&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Unfused dead short across a 26 AWG jumper:&lt;/strong&gt;
\(I_{\text{sc}} = \frac{8.4\text{ V}}{0.12\,\Omega + 0.03\,\Omega} = \frac{8.4}{0.15} = 56\text{ A}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Power dissipated inside that tiny jumper wire:&lt;/strong&gt;
\(P_{\text{wire}} = I_{\text{sc}}^2 R_{\text{wire}} = 56^2 \times 0.03 = 3136 \times 0.03 = 94.1\text{ W}!\)
That is nearly the wattage of a hot soldering iron concentrated inside a strand of PVC-coated wire — melting insulation in under a second.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Power dissipated inside the cells themselves:&lt;/strong&gt;
\(P_{\text{cells}} = 56^2 \times 0.12 = 376.3\text{ W},\)
rapidly pushing cell temperature toward thermal runaway.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 4 — Sizing the inline fuse for our diff-drive robot:&lt;/strong&gt;
Suppose two motors stall at \(1.5\text{ A}\) each (\(3.0\text{ A}\) total) and logic draws \(0.5\text{ A}\) (peak \(3.5\text{ A}\)). Choosing a &lt;strong&gt;5 A blade or glass fuse&lt;/strong&gt; allows brief startup surges without nuisance tripping, yet at \(56\text{ A}\) (over 11× rated current) it opens in milliseconds.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Relying on a breadboard’s thin spring clips to carry motor stall currents — breadboards are typically rated for \(\le 1\text{ A}\) and will melt silently before a 5 A fuse ever blows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; Before flipping the main switch on any new circuit, measure resistance between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V+&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GND&lt;/code&gt; with a multimeter (power OFF). A reading near \(0\,\Omega\) catches a dead short &lt;em&gt;before&lt;/em&gt; 56 A ever flows.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;p&gt;These are situations. Write the action, not a definition.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;A classmate has the chassis on the desk, wheels down, and wants to upload firmware “just for a second.” What has to be true before VM is allowed to close, and why is the 300 ms rule not enough on that first upload?&lt;/li&gt;
  &lt;li&gt;A cell on the bench reads 2.1 V. Someone offers a TP4056. What do you do with the cell, and what do you refuse to do with the 2S holder?&lt;/li&gt;
  &lt;li&gt;The echo wire of an HC-SR04 is about to go straight to an ESP32 GPIO. What voltage is that wire when the sensor answers, and why is “the ESP32 is tough” not a rating?&lt;/li&gt;
  &lt;li&gt;You find a TP4056 already clipped across a holder that contains two cells in series. The holder is the one that will later feed VM. What do you disconnect, and how will those cells be charged if they are healthy?&lt;/li&gt;
  &lt;li&gt;Teleop was working, then the laptop slept. The firmware has no silence timer. What do the wheels do, and what single assignment belongs in the control loop?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. Wheels up, ground then logic then signals already done, VM still open until you can watch the wheels in the air. The timeout runs only after firmware is executing. Reset can drive a pin first, and a wheel on the desk can pull the board off.
2. Do not charge the cell, pocket it, or clip a TP4056 to it or to the 2S holder. Recycle it.
3. The echo is about 5 V when high. The ESP32 absolute maximum on a GPIO is not 5 V. The wire needs a level shifter. The pin may already be damaged.
4. Disconnect the TP4056 now. Charge each healthy cell alone to 4.2 V, off the robot. The holder is only a pack for VM after that.
5. The wheels keep the last command. The loop must set PWM duty to 0 when silence exceeds about 300 ms.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://learn.sparkfun.com/tutorials/logic-levels&quot;&gt;SparkFun logic levels&lt;/a&gt; — why 5 V and 3.3 V are not interchangeable, which is the echo-pin problem in miniature.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://batteryuniversity.com/article/bu-409-charging-lithium-ion&quot;&gt;Battery University, BU-409, charging lithium-ion&lt;/a&gt; — the 4.2 V full-charge idea and why a lithium-ion cell is not a generic “rechargeable.”&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;where-to-buy-in-vietnam--mua-ở-việt-nam&quot;&gt;Where to buy in Vietnam / Mua ở Việt Nam&lt;/h2&gt;

&lt;p&gt;The robot cart is &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;lesson 00-05&lt;/a&gt;. Prices here are Hshop snapshots from &lt;strong&gt;23 September 2026&lt;/strong&gt;. Buy one meter: the &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-dien-tu-digital-multimeter-uni-t-ut33d-chinh-hang&quot;&gt;UNI-T UT33D+&lt;/a&gt; at 285000 ₫ or the &lt;a href=&quot;https://hshop.vn/dong-ho-van-nang-ky-thuat-so-vom-wadfow-wdm1501-digital-multimeter-true-rms&quot;&gt;Wadfow WDM1501&lt;/a&gt; at 185000 ₫. The iron and the stand are a pair: &lt;a href=&quot;https://hshop.vn/mo-han-60w-wadfow-wel3616-soldering-iron&quot;&gt;Wadfow 60 W&lt;/a&gt; at 75000 ₫ and a &lt;a href=&quot;https://hshop.vn/de-gac-mo-han-tron-b-2-co-khay-roi&quot;&gt;round stand&lt;/a&gt; at 40000 ₫. An iron without the stand fails the rule you just wrote. Solder, cutters, and the breadboard are in lesson 00-04.&lt;/p&gt;

&lt;p&gt;Eye protection and a one-cell charger are searches, not invented product slugs. &lt;a href=&quot;https://shopee.vn/search?keyword=k%C3%ADnh%20b%E1%BA%A3o%20h%E1%BB%99&quot;&gt;Shopee, kính bảo hộ&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=k%C3%ADnh%20b%E1%BA%A3o%20h%E1%BB%99&quot;&gt;Lazada, kính bảo hộ&lt;/a&gt;, &lt;a href=&quot;https://shopee.vn/search?keyword=s%E1%BA%A1c%20pin%2018650&quot;&gt;Shopee, sạc pin 18650&lt;/a&gt;, &lt;a href=&quot;https://www.lazada.vn/catalog/?q=s%E1%BA%A1c%20pin%2018650&quot;&gt;Lazada, sạc pin 18650&lt;/a&gt;. Those URLs are search pages. Buy a charger only if the label says one Li-ion cell to 4.2 V. &lt;a href=&quot;https://www.thegioiic.com/&quot;&gt;Thế Giới IC&lt;/a&gt; and &lt;a href=&quot;https://icdayroi.com/&quot;&gt;IC Đây Rồi&lt;/a&gt; are shop homepages, not product links. Do not buy a LiDAR from these searches.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Two tracks: a budget MCU robot and ROS 2 later</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter00/00_02_learning_tracks_budget/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter00/00_02_learning_tracks_budget</id>
   <content type="html">&lt;p&gt;Every robotics engineer eventually faces an architectural fork in the road: should intelligence live on a bare-metal microcontroller executing deterministic loops, or on a Linux single-board computer running a full middleware stack like ROS 2? In industry, the answer is almost always &lt;strong&gt;both&lt;/strong&gt; — a low-level real-time controller paired with a high-level autonomy computer.&lt;/p&gt;

&lt;p&gt;This lesson explains why splitting the course into a budget MCU track (Capstone A) and a later ROS 2 track mirrors real production robots from warehouse AMRs to autonomous rovers — and how to budget both compute and power before spending a single đồng.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Plan about an hour. You need the Capstone sentence from the welcome lesson and an honest description of the computer you actually own. Prices in this lesson are shapes of a budget, not a cart. The cart is &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;lesson 00-05&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;You leave this lesson able to &lt;strong&gt;choose&lt;/strong&gt; Track A or Track B in a sentence that names your laptop’s operating system, a money ceiling in VND, and a two-month goal. You can &lt;strong&gt;specify&lt;/strong&gt; a Track A robot as an ESP32 or a Pico 2, TT gear motors, a TB6612FNG rather than an L298N, and an HC-SR04, with no Ubuntu required for Capstone A. You can &lt;strong&gt;compute&lt;/strong&gt; the voltage a Darlington driver throws away, about 2 V, and say what that does to a 7.4 V pack. You can &lt;strong&gt;place&lt;/strong&gt; Ubuntu 24.04, ROS 2 Jazzy, Gazebo Harmonic, and micro-ROS after the same robot already moves, with a Raspberry Pi only as a later option. You can &lt;strong&gt;refuse&lt;/strong&gt; a LiDAR purchase before Capstone A acceptance, in writing.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;Lesson 00-01: you can state the wheels-up teleop test and the 300 ms PWM stop, and you have a journal with the six fields. You do not need Ubuntu, ROS, or a paid order. If you already own a Raspberry Pi, or a laptop that dual-boots Linux, you still start from the same chassis. Ownership is not a reason to skip the firmware robot.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-real-time-embedded-control-vs-high-level-autonomy-middleware&quot;&gt;Key ideas: Real-Time Embedded Control vs. High-Level Autonomy Middleware&lt;/h2&gt;

&lt;p&gt;Understanding the two-track architecture requires connecting three core systems-engineering concepts:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Hard Real-Time vs. Soft Real-Time Computing:&lt;/strong&gt; Microcontrollers (ESP32, RP2040, STM32) run tight control loops with microsecond-level timing predictability, ideal for PWM generation, encoder counting, and PID velocity control. General-purpose Linux hosts (Raspberry Pi, Jetson) excel at high-bandwidth perception, SLAM, and path planning, but OS scheduling introduces millisecond jitter.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Power–Compute–Weight Trade-offs:&lt;/strong&gt; Every watt drawn by an onboard computer reduces battery runtime and increases chassis mass, which in turn demands higher motor torque. Starting with an MCU-only robot lets you master actuation, sensing, and closed-loop control at a fraction of the power and cost.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Interface Boundaries (Serial / micro-ROS):&lt;/strong&gt; Clean robotics systems isolate low-level firmware behind a simple velocity command interface \((v_{\text{cmd}}, \omega_{\text{cmd}})\) and odometry telemetry stream \((x, y, \theta, v, \omega)\), so upgrading from MCU-only to ROS 2 requires swapping the brain without rebuilding the chassis.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Capstone A does not link against ROS. It needs a microcontroller that can time about 300 ms and a driver that can take PWM and then let go. If the first month is spent partitioning a disk, the motor wires are still untested. Track B is real. It is the path to Jazzy topics, Gazebo Harmonic, and micro-ROS. Those topics are a messaging layer on physics you already trust. Choosing the track now is how you stop a shopping tab from choosing it for you, and how you stop a LiDAR from arriving before the timeout exists.&lt;/p&gt;

&lt;h2 id=&quot;two-legal-paths-one-moving-robot&quot;&gt;Two legal paths, one moving robot&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Track A&lt;/strong&gt; is the budget microcontroller path, and it is enough for Capstone A. The brain is either an ESP32 development board or a Raspberry Pi Pico 2. The ESP32 is the board you pick when you want Wi-Fi teleop later: a phone or laptop on the same desk sends commands without a USB cable in the final demo. The Pico 2 is the board you pick when you want MicroPython and a USB cable first. Both speak 3.3 V logic. Both can run the 300 ms stop. Neither requires Ubuntu.&lt;/p&gt;

&lt;p&gt;The mechanics are TT gear motors on a small chassis, two driven wheels and a caster. The driver you want is a &lt;strong&gt;TB6612FNG&lt;/strong&gt; module, a MOSFET H-bridge. The module people buy because a video used it is often an &lt;strong&gt;L298N&lt;/strong&gt;, a Darlington bridge. At motor current the Darlington pair drops on the order of 2 V between the battery and the motor. The MOSFET bridge drops a few tenths of a volt. For a two-cell pack sitting near 7.4 V,&lt;/p&gt;

\[V_{\mathrm{L298}} \approx 7.4\,\mathrm{V} - 2.0\,\mathrm{V} = 5.4\,\mathrm{V},\]

\[V_{\mathrm{TB6612}} \approx 7.4\,\mathrm{V} - 0.3\,\mathrm{V} = 7.1\,\mathrm{V}.\]

&lt;p&gt;A TT motor is already a small, lossy gearbox. Starving it of another 2 V is why “the same battery” feels weak on an L298N and ordinary on a TB6612. Pololu’s TB6612 carrier, &lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;product 713&lt;/a&gt;, is the clearest public explanation of that chip if you want a second schematic style. You are allowed to buy a different module that uses the same chip. You are not required to import that exact carrier. A DRV8833 is a smaller MOSFET substitute when the TB6612 is out of stock. It is still not an L298N.&lt;/p&gt;

&lt;p&gt;The range sensor on the first cart is an HC-SR04 ultrasonic module. Its echo pin swings to 5 V, so it must not land directly on a 3.3 V GPIO. Lesson 00-05 includes a level shifter for that wire. Track A does not install Ubuntu. Serial teleop from Windows, macOS, or whatever Linux you already have is enough for acceptance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Track B&lt;/strong&gt; is Track A plus a later software stack. The robot is the same chassis, the same driver, the same timeout. When teleop is reliable, you use a machine running &lt;strong&gt;Ubuntu 24.04&lt;/strong&gt; and install &lt;strong&gt;ROS 2 Jazzy&lt;/strong&gt; there. &lt;strong&gt;Gazebo Harmonic&lt;/strong&gt; is the simulator that belongs with that release, and it is Chapter 10 work. &lt;strong&gt;micro-ROS&lt;/strong&gt; lets the microcontroller speak ROS 2 to that machine. It is a transport added on top of PWM you have already watched work. A Raspberry Pi can appear later as an optional companion computer. It is not the device that first spins the motors, and it is not a reason to buy a LiDAR so the Pi “has something to do.”&lt;/p&gt;

&lt;p&gt;People who install micro-ROS on a board that has never turned a wheel debug two failures at once. A topic with no messages and a motor with no voltage look identical from the chair. The course therefore puts the message graph after the bench.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/tracks_mcu_ros.png&quot; alt=&quot;Track A on the left, Track B as a later layer on the same robot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Read the left side of the figure as this month’s robot and the right side as software you earn. “No Ubuntu required” is the Track A permission for Capstone A. “Buy LiDAR only after Capstone A is reliable” is the shared spending rule. A spinning LiDAR is a different power problem, a different ROS tutorial, and often a few million đồng. It does not make the 300 ms timeout more correct.&lt;/p&gt;

&lt;h2 id=&quot;the-decision-table&quot;&gt;The decision table&lt;/h2&gt;

&lt;p&gt;Use this as a worksheet. Copy one row into the journal and strike the others, or mark a single row “mine.”&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Your situation this month&lt;/th&gt;
      &lt;th&gt;Track&lt;/th&gt;
      &lt;th&gt;Do now&lt;/th&gt;
      &lt;th&gt;Leave for later&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Any laptop OS; about 1.5–2.0 million VND; goal is “it drives when I type”&lt;/td&gt;
      &lt;td&gt;A&lt;/td&gt;
      &lt;td&gt;ESP32 or Pico 2, TT motors, TB6612FNG, HC-SR04, shifter, tools, battery plan from lesson 00-05&lt;/td&gt;
      &lt;td&gt;Ubuntu, Pi, LiDAR, Nav2, Jazzy install&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Windows or macOS, and you want ROS eventually&lt;/td&gt;
      &lt;td&gt;A now, B after Chapter 07&lt;/td&gt;
      &lt;td&gt;The same robot; write “Ubuntu 24.04 lab PC later”&lt;/td&gt;
      &lt;td&gt;Do not wipe the laptop; do not buy LiDAR to justify a future Pi&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;You already run Ubuntu 24.04 and you want ROS literacy&lt;/td&gt;
      &lt;td&gt;B, but build the Track A robot first&lt;/td&gt;
      &lt;td&gt;Same electronics; you may install Jazzy on that PC in parallel&lt;/td&gt;
      &lt;td&gt;Do not block Chapter 07 on a Gazebo world; do not skip the bench&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;The budget is mostly a Pi plus a LiDAR kit from a video&lt;/td&gt;
      &lt;td&gt;Stop and recompute&lt;/td&gt;
      &lt;td&gt;Rewrite the goal as wheels-up teleop&lt;/td&gt;
      &lt;td&gt;That kit does not implement a 300 ms stop or a MOSFET drop&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A LiDAR at 4,000,000 ₫ next to a Track A core near 800,000 ₫ is&lt;/p&gt;

\[\frac{4{,}000{,}000}{800{,}000} = 5\]

&lt;p&gt;times the core, and it does not flash the first LED. The ratio is the reason for the ban before acceptance.&lt;/p&gt;

&lt;p&gt;Price hygiene belongs in the same note. A number without a date is gossip. Lesson 00-05 holds the dated Hshop snapshots for the parts you will actually order. This lesson does not rebuild that table. If a board moves by a hundred thousand đồng, your track does not change. The track changes only when the operating system, the ceiling, or the goal changes.&lt;/p&gt;

&lt;h2 id=&quot;lab-write-the-track-sentence&quot;&gt;Lab: write the track sentence&lt;/h2&gt;

&lt;p&gt;Open &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/lab-notes.md&lt;/code&gt;. Under today’s date, write three lines: the laptop operating system, the money ceiling in VND for the next 30 days, and a one-sentence goal. If the goal mentions maps, SLAM, or Nav2, add the clause “after Capstone A.”&lt;/p&gt;

&lt;p&gt;Pick the microcontroller in words. Either “ESP32 dev board, Wi-Fi teleop later, USB serial first” or “Pico 2, MicroPython, USB first.” You may change this before you pay. You may not leave it blank.&lt;/p&gt;

&lt;p&gt;Write this sentence and put your name after it: “I will not buy a LiDAR before my Capstone A wheels-up teleop passes, including the 300 ms PWM stop.”&lt;/p&gt;

&lt;p&gt;Add one line that points at lesson 00-05 as the only shopping list. Do not paste a second cart here. If you already know you will use a shared lab PC for Ubuntu, name that machine as “lab Ubuntu, not my laptop.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you should see.&lt;/strong&gt; A later reader can tell which track you chose, which MCU you lean toward, and which expensive objects you refused. Ubuntu is either “not now,” “lab PC later,” or “already installed, bench still first.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it goes wrong.&lt;/strong&gt; You chose Track B and ordered only a Pi. The motors are still driven by the ESP32 or the Pico 2; add those rows by opening lesson 00-05, not by inventing a new kit. You chose Track A and then started a Jazzy install “just to try” on Windows. Bookmark the install page under “after Chapter 07” and close it. Windows Subsystem for Linux is not the robot lab. Your budget is a single undated number. Split “robot core” from “tools” and date the estimate.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Minh has a Windows laptop and a ceiling of about 1.5–2.0 million VND for the month. The goal he first wrote was “learn ROS and SLAM.” He rewrites it to “drive a two-wheel robot from serial, with a 300 ms stop, and only then use a lab Ubuntu PC for Jazzy.” The table puts him on Track A now. He chooses an ESP32 because he wants Wi-Fi teleop as a later Chapter 07 variant, while the first commands will still be USB serial. He does not buy a Pico 2 as well. He does not buy a LiDAR. He writes that Track B will happen on a university lab machine that already has Ubuntu 24.04, not by repartitioning the Windows disk this week. The shopping list he will copy is lesson 00-05, not a forum kit.&lt;/p&gt;

&lt;p&gt;A second student, Lan, already runs Ubuntu 24.04 on her own laptop. She is allowed to follow the &lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation.html&quot;&gt;Jazzy installation page&lt;/a&gt; in parallel, in a separate sitting, and to record the version string in her journal. She is not allowed to skip the bench. Her robot is still TT motors, a TB6612FNG, an HC-SR04, and firmware that zeros PWM when packets stop. If Jazzy installs cleanly and the motors are still in a bag, she is not ahead. She has a messaging layer and no physics. Gazebo Harmonic stays named and unopened until the chassis has passed wheels-up teleop at least once. micro-ROS stays a link, &lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro.ros.org&lt;/a&gt;, until the same board has moved the wheels without it.&lt;/p&gt;

&lt;p&gt;Minh sketches the Darlington loss so the driver choice is not a brand preference. At 7.4 V with about 2 V dropped, the motor sees 5.4 V. A MOSFET drop of about 0.3 V leaves 7.1 V. He writes “TB6612FNG, not L298N” in the track sentence. Both students end the entry with the LiDAR refusal. Neither opens a second spreadsheet. Lesson 00-05 is the cart.&lt;/p&gt;

&lt;h2 id=&quot;explanation-quantifying-the-power-and-runtime-budget-of-both-tracks&quot;&gt;Explanation: Quantifying the Power and Runtime Budget of Both Tracks&lt;/h2&gt;

&lt;p&gt;Why not bolt a Raspberry Pi 4 and LiDAR onto your robot on day one? Let’s compute the power and battery runtime of Track A (MCU-only) versus Track B (MCU + SBC) step by step:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Sum steady-state current draws:&lt;/strong&gt; Separate &lt;em&gt;logic current&lt;/em&gt; (MCU, sensors, SBC) from &lt;em&gt;actuator current&lt;/em&gt; (two DC motors under cruise vs. stall load).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Convert to battery-side power:&lt;/strong&gt; Account for DC–DC buck converter efficiency \(\eta \approx 0.85\) when stepping a 7.4 V (2S Li-ion) pack down to 5 V.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compute usable battery energy:&lt;/strong&gt; A 2S 18650 pack rated at \(2000\text{ mAh}\) and \(7.4\text{ V}\) nominal stores \(E = 7.4 \times 2.0 = 14.8\text{ Wh}\); keeping a 20% safety margin leaves \(E_{\text{usable}} = 11.84\text{ Wh}\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Divide usable energy by average power:&lt;/strong&gt; \(T_{\text{run}} = E_{\text{usable}} / P_{\text{avg}}\).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-comparing-battery-runtime-on-a-2s-2000-mah-pack&quot;&gt;Concrete Worked Example: Comparing Battery Runtime on a 2S 2000 mAh Pack&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Subsystem&lt;/th&gt;
      &lt;th&gt;Track A (ESP32 MCU only)&lt;/th&gt;
      &lt;th&gt;Track B (ESP32 + Raspberry Pi 4 + LiDAR)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Logic rail (at 5 V)&lt;/td&gt;
      &lt;td&gt;\(0.25\text{ A} \implies 1.25\text{ W}\)&lt;/td&gt;
      &lt;td&gt;\(2.00\text{ A} \implies 10.00\text{ W}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Battery power for logic (\(\eta = 0.85\))&lt;/td&gt;
      &lt;td&gt;\(1.25 / 0.85 \approx 1.47\text{ W}\)&lt;/td&gt;
      &lt;td&gt;\(10.00 / 0.85 \approx 11.76\text{ W}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Two DC motors (cruise, 7.4 V, 0.4 A total)&lt;/td&gt;
      &lt;td&gt;\(7.4 \times 0.40 = 2.96\text{ W}\)&lt;/td&gt;
      &lt;td&gt;\(7.4 \times 0.40 = 2.96\text{ W}\)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Total average power \(P_{\text{avg}}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(4.43\text{ W}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(14.72\text{ W}\)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;Usable runtime (\(11.84\text{ Wh}\))&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(11.84 / 4.43 \approx 2.67\text{ hours}\)&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;\(11.84 / 14.72 \approx 0.80\text{ hours (48 min)}\)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Notice the engineering lesson: adding a Linux SBC and LiDAR triples power draw, cuts runtime from nearly 3 hours to 48 minutes, and requires a 5 V / 3 A buck regulator to prevent voltage sags.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Powering a Raspberry Pi and DC motors off a weak 1 A linear regulator or USB power bank that resets the Pi every time the motors accelerate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; In Track A, even if motor cruise current doubles to \(0.8\text{ A}\) (\(5.92\text{ W}\)), total power is \(7.39\text{ W}\) and runtime is still \(11.84 / 7.39 \approx 1.60\text{ hours}\) — plenty for an entire afternoon of PID tuning.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Your laptop is Windows, your ceiling is 1,800,000 ₫, and you want the robot to move this month. Which track, which MCU if you care about Wi-Fi later, and which driver? What do you do with Ubuntu this week?&lt;/li&gt;
  &lt;li&gt;You already have Ubuntu 24.04. Why is the first build still the Track A chassis if you are allowed to install Jazzy in parallel?&lt;/li&gt;
  &lt;li&gt;A 6 V battery of four AA cells feeds an L298N that drops about 2 V. What voltage reaches the motor? Why is that a poor match for a TT motor whose useful range starts near 3 V but is already weak at 4 V?&lt;/li&gt;
  &lt;li&gt;A listing adds a 3,500,000 ₫ LiDAR to a Track A plan “for only a bit more.” Does Capstone A acceptance get easier? Where does the LiDAR sit relative to the acceptance test?&lt;/li&gt;
  &lt;li&gt;Where does micro-ROS sit relative to the first day the wheels respond to a serial command? What would a &lt;a href=&quot;https://github.com/linorobot/linorobot2&quot;&gt;linorobot2&lt;/a&gt; repository tempt you to skip?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. Track A now. Choose an ESP32 if Wi-Fi teleop is the later goal; choose a Pico 2 if you want MicroPython over USB first and accept Wi-Fi as someone else&apos;s problem for now. The driver is a TB6612FNG. Ubuntu waits. Do not install Jazzy on Windows and call it the lab.
2. Jazzy can show topics on a PC. It cannot show that your VM wiring, the stall behaviour, and the 300 ms timeout work on the hardware you will later describe with those topics. Parallel install is allowed. Skipping the bench is not.
3. $6 - 2 = 4$ V at the motor. Four volts is the weak end of a small TT gearbox, before load. The robot crawls or stalls, and the symptom looks like &quot;bad motors.&quot;
4. Acceptance does not get easier. The LiDAR does not implement the timeout or the MOSFET driver. It belongs after Capstone A, if it belongs at all.
5. micro-ROS comes after the wheels already respond. linorobot2 is a picture of a differential-drive robot described for ROS 2. Opening it before you have a driver and a timeout tempts you to debug a URDF while the bench is still empty.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/linorobot/linorobot2&quot;&gt;linorobot2&lt;/a&gt; — one concrete differential-drive stack for ROS 2. Read it as a picture of Track B’s destination, not as this month’s parts list.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://micro.ros.org/&quot;&gt;micro-ROS&lt;/a&gt; — what it is. Bring the tutorials back after the chassis moves without it.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/Installation.html&quot;&gt;Install ROS 2 Jazzy&lt;/a&gt; — Ubuntu 24.04 is the supported path. If your daily machine is Windows, this page is a later lab-PC task.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.pololu.com/product/713&quot;&gt;Pololu TB6612FNG carrier, product 713&lt;/a&gt; — why a MOSFET carrier is the driver to compare against a Darlington module.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Shopping stays in &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;lesson 00-05&lt;/a&gt;. Do not rebuild the cart from these four links.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Welcome, outcomes, and how to study</title>
   <link href="https://robotics-self-learning.pages.dev/contents/en/chapter00/00_01_welcome_and_outcomes/"/>
   <updated>2021-01-01T00:00:00+00:00</updated>
   <id>https://robotics-self-learning.pages.dev/contents/en/chapter00/00_01_welcome_and_outcomes</id>
   <content type="html">&lt;p&gt;In 1969, researchers at Stanford Research Institute built Shakey — the first mobile robot capable of reasoning about its own actions. Shakey stood nearly two meters tall, filled a room with supporting computers, and took minutes just to decide how to push a wooden block across a tiled floor. Today, a microcontroller smaller than a postage stamp executes thousands of control iterations per second on a desk. Yet the intellectual core of robotics has not changed: a robot is a closed loop where &lt;strong&gt;physics, electronics, estimation, and control&lt;/strong&gt; negotiate with an imperfect world in real time.&lt;/p&gt;

&lt;p&gt;This opening lesson sets your compass for the entire course. Rather than treating robotics as a pile of disconnected tutorials, we frame every upcoming chapter as one link in the &lt;em&gt;sense → estimate → plan → act&lt;/em&gt; pipeline.r&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Set aside about ninety minutes. You need a text editor and a calendar. A robot is not required, and neither is Ubuntu. The work is writing a map you will still use when the chassis rolls.&lt;/p&gt;

&lt;h2 id=&quot;learning-objectives&quot;&gt;Learning objectives&lt;/h2&gt;

&lt;p&gt;By the end you can &lt;strong&gt;state&lt;/strong&gt; the course spine, from component literacy through Chapter 07 teleoperation to a later reading of robot-learning papers. You can &lt;strong&gt;compute&lt;/strong&gt; a load of 4–6 hours a week across 12–16 weeks and place the chapter checkpoints. You can &lt;strong&gt;open&lt;/strong&gt; a lab journal with six fields: date, photo, wiring sketch, serial snippet, what broke, and what fixed it. You can &lt;strong&gt;judge&lt;/strong&gt; a note as evidence, using a missing serial snippet or a missing failure as the test. You can &lt;strong&gt;locate&lt;/strong&gt; the Vietnamese twin by chapter and order, and you can &lt;strong&gt;postpone&lt;/strong&gt; LiDAR, Nav2, and a full ROS 2 install until a differential-drive robot already stops when commands go quiet.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;You can read a technical page slowly and you can create a folder. You do not need a meter, an iron, or a prior robotics course. Ohm’s law waits for Chapter 01, where it is measured. What you do need is a note that records the failure. “Started the course” will not help in week nine.&lt;/p&gt;

&lt;h2 id=&quot;key-ideas-how-mechanics-electronics-control-and-software-interlock-in-robotics&quot;&gt;Key ideas: How Mechanics, Electronics, Control, and Software Interlock in Robotics&lt;/h2&gt;

&lt;p&gt;Before touching a soldering iron or writing a single line of C++, it helps to see how four classical engineering disciplines fuse inside even a small differential-drive robot:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Kinematics &amp;amp; Mechanics (Geometry of Motion):&lt;/strong&gt; Describes how wheel rotations \((\omega_L, \omega_R)\) map to chassis linear velocity \(v\) and angular velocity \(\omega\) in the 2D plane \(\mathrm{SE}(2)\). Without a clean kinematic model, a robot cannot predict where a motor command will take it.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Embedded Electronics &amp;amp; Power Systems:&lt;/strong&gt; Governs how energy flows from a battery pack through regulators and H-bridge drivers to DC motors, while keeping noisy inductive loads from browning out the microcontroller.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Feedback Control Theory:&lt;/strong&gt; Measures the error \(e(t) = r(t) - y(t)\) between desired and actual motion and computes corrective commands (PID, state-space) at fixed real-time intervals \(\Delta t\).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;State Estimation &amp;amp; Software Architecture:&lt;/strong&gt; Turns noisy encoder ticks, IMU readings, and range measurements into a coherent belief about pose \((x, y, \theta)\), orchestrated first in deterministic microcontroller firmware and later in ROS 2 nodes.r&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;why-it-matters&quot;&gt;Why it matters&lt;/h2&gt;

&lt;p&gt;Chapter 07 is Capstone A: two driven wheels and a caster, teleoperated with the wheels off the table. Teleoperation means you drive it yourself. If commands stop for about 300 ms, PWM duty goes to 0. ROS 2 Jazzy comes after that test. A topic is a named stream of messages, a messaging layer on physics you already trust. It does not create torque. Gazebo Harmonic and micro-ROS are the same idea in simulation and on the microcontroller. If you cannot say the 300 ms rule now, a later install is a way of avoiding the bench.&lt;/p&gt;

&lt;h2 id=&quot;the-spine-said-in-the-order-you-will-live-it&quot;&gt;The spine, said in the order you will live it&lt;/h2&gt;

&lt;p&gt;The course is one path, with optional rooms at the end.&lt;/p&gt;

&lt;p&gt;You begin with component literacy: a breadboard row, a motor driver, a USB cable that only charges, a cell whose charger does not match the label. Chapter 00 is the bench habit. Chapter 01 turns voltage, current, and resistance into numbers you measured.&lt;/p&gt;

&lt;p&gt;Firmware comes next, on an ESP32 or a Raspberry Pi Pico. Chapter 02 gets a program onto the chip. Chapter 03 makes it behave like robot code: pins, timing, and PWM. PWM, pulse-width modulation, is a pin that is high for a fraction of a short period. The motor driver turns that fraction into how hard a motor is asked to turn.&lt;/p&gt;

&lt;p&gt;Chapters 04 through 06 add sensors, the driver, and the mechanics of two driven wheels. Chapter 07 is the acceptance test. You send forward, spin, or stop, over USB serial or, later, a small Wi-Fi page if you chose an ESP32. The wheels stay in the air. A timer remembers the last valid command. Let $t_{\mathrm{last}}$ be that timestamp and $t_{\mathrm{now}}$ the time in the control loop. Silence is&lt;/p&gt;

\[t_{\mathrm{silence}} = t_{\mathrm{now}} - t_{\mathrm{last}}.\]

&lt;p&gt;The stop rule for this course is&lt;/p&gt;

\[t_{\mathrm{silence}} &amp;gt; 300\,\mathrm{ms} \implies \text{PWM duty} = 0.\]

&lt;p&gt;The inequality is strict, so a command that lands exactly on 300 ms still counts as fresh. You will not flash this loop today. At a slow hobby speed of $0.3\,\mathrm{m/s}$ the robot can creep about 9 cm during that window:&lt;/p&gt;

\[s \approx 0.3\,\mathrm{m/s} \times 0.3\,\mathrm{s} = 0.09\,\mathrm{m}.\]

&lt;p&gt;Nine centimetres will also yank a USB cable, which is why acceptance is wheels-up.&lt;/p&gt;

&lt;p&gt;Only after that demo is repeatable do you add the messaging layer. Chapter 08 is communication and simple loops on hardware you have seen move. Chapter 09 installs ROS 2 Jazzy, and only on Ubuntu 24.04. A velocity topic is useful because you already know what the firmware does when messages stop. Chapter 10 names Gazebo Harmonic, the simulator paired with Jazzy, and micro-ROS, a way for the same microcontroller to speak ROS 2. micro-ROS is interesting after the board can move the chassis without it. A silent topic and a silent motor are otherwise the same symptom.&lt;/p&gt;

&lt;p&gt;Chapters 11 and 12 are optional. They ask for a careful reading. LeRobot is a Hugging Face library of robot datasets and learned policies. ACT, action chunking with transformers, predicts a short chunk of future actions rather than a single twitch. A diffusion policy generates an action by iterative denoising. A vision-language-action model, a VLA, maps camera images and language onto robot actions. None of these replace a driver that can turn PWM off. Read the claim, the data, and the limit. A repository does not make the first chassis a research platform.&lt;/p&gt;

&lt;p&gt;Videos treat several things as step one that this course postpones. You will not buy a LiDAR in this chapter, run Nav2, or install ROS 2 this week unless you are only reading the install page on a machine that already runs Ubuntu 24.04. The bench still comes first. A robot that rolled for a second is not autonomous. Motor-battery voltage does not belong on a 3.3 V pin.&lt;/p&gt;

&lt;h2 id=&quot;cadence-four-to-six-hours-for-twelve-to-sixteen-weeks&quot;&gt;Cadence: four to six hours, for twelve to sixteen weeks&lt;/h2&gt;

&lt;p&gt;The design load is &lt;strong&gt;4–6 hours per week&lt;/strong&gt; for about &lt;strong&gt;12–16 weeks&lt;/strong&gt;. Fourteen weeks at five hours is&lt;/p&gt;

\[5 \times 14 = 70\]

&lt;p&gt;hours. Twelve weeks at six hours is 72. Sixteen weeks at four hours is 64, enough for Chapters 00 through 10 if one sitting each week touches the bench, or the paper rituals before the parcel arrives. A five-hour week is about 90 minutes of reading, 120 minutes on the bench, and 90 minutes cleaning the note. One Sunday of twelve hours, then three quiet weeks, throws the wiring out of memory.&lt;/p&gt;

&lt;p&gt;The chapter checkpoints are the spine with dates on it.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Weeks&lt;/th&gt;
      &lt;th&gt;Chapters&lt;/th&gt;
      &lt;th&gt;You can show this evidence&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1–3&lt;/td&gt;
      &lt;td&gt;00 and 01&lt;/td&gt;
      &lt;td&gt;A power-up card, a meter that beeps on a known short, and a journal entry with a voltage or an honest “none”&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;4–6&lt;/td&gt;
      &lt;td&gt;02 and 03&lt;/td&gt;
      &lt;td&gt;Firmware on an ESP32 or Pico, and a serial snippet pasted into the journal&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;7–11&lt;/td&gt;
      &lt;td&gt;04 through 07&lt;/td&gt;
      &lt;td&gt;Wheels-up teleop; PWM duty 0 if commands are silent for more than about 300 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;12–14&lt;/td&gt;
      &lt;td&gt;08 through 10&lt;/td&gt;
      &lt;td&gt;A ROS 2 topic on Ubuntu 24.04 that describes motion you already trust; Gazebo Harmonic and micro-ROS named from a lab note, not from a thumbnail&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;Later, optional&lt;/td&gt;
      &lt;td&gt;11 and 12&lt;/td&gt;
      &lt;td&gt;A reading note on LeRobot, ACT, a diffusion policy, or a VLA that says what was claimed and what was not measured on your robot&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;If a week collapses, slip a checkpoint. Do not skip the wheels-up test to catch up to Jazzy.&lt;/p&gt;

&lt;h2 id=&quot;the-lab-journal&quot;&gt;The lab journal&lt;/h2&gt;

&lt;p&gt;Keep one running file, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/lab-notes.md&lt;/code&gt;. Every entry has six fields, in this order. The &lt;strong&gt;date&lt;/strong&gt; is the day you touched the work. The &lt;strong&gt;photo&lt;/strong&gt; is a camera-roll name or an image of the bench; write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;none&lt;/code&gt; on purpose when there is nothing to shoot. The &lt;strong&gt;wiring sketch&lt;/strong&gt; can be pencil on paper; before any circuit exists, write “no circuit yet” rather than leaving the field out. The &lt;strong&gt;serial snippet&lt;/strong&gt; is pasted text from a serial monitor, a Thonny shell, or a later ROS 2 echo. Before any board is plugged in, the legal text is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;none — no board yet&lt;/code&gt;. &lt;strong&gt;What broke&lt;/strong&gt; names a symptom you could see or measure, such as “the serial port disappeared when both motors were tied to the USB 5 V pin,” not “motor bad.” &lt;strong&gt;What fixed it&lt;/strong&gt; names the change, including “nothing was powered; I wrote the stop rule before buying parts.” An empty field makes the entry a mood.&lt;/p&gt;

&lt;p&gt;The site is bilingual. The language switch matches &lt;strong&gt;chapter&lt;/strong&gt; and &lt;strong&gt;order&lt;/strong&gt;, not the title. This page is chapter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;00&lt;/code&gt;, order &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;, and so is its Vietnamese twin. If a paragraph is muddy, switch for that section. Keep GPIO, PWM, breadboard, Capstone, and ROS 2 stable in both languages.&lt;/p&gt;

&lt;h2 id=&quot;figures&quot;&gt;Figures&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/kit_catalog_overview.png&quot; alt=&quot;Families of parts and tools the course will ask you to recognise&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The drawing is a family portrait, not a cart. Lesson 00-05 is the bill of materials. Buying from the picture alone is how a LiDAR arrives before a meter.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/generated/tracks_mcu_ros.png&quot; alt=&quot;Two study tracks sharing one robot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Track A finishes Capstone A from firmware, with no Ubuntu. Track B is that same robot plus Ubuntu 24.04, ROS 2 Jazzy, Gazebo Harmonic, and micro-ROS after the chassis moves. Both tracks share one acceptance test. LiDAR stays off the list until that test is reliable.&lt;/p&gt;

&lt;h2 id=&quot;lab-the-journal-and-the-next-fourteen-weeks&quot;&gt;Lab: the journal and the next fourteen weeks&lt;/h2&gt;

&lt;p&gt;Create a course folder and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notes/lab-notes.md&lt;/code&gt;. Fill all six fields under today’s date. In the sketch field, write the Capstone A sentence: two driven wheels, wheels up, serial or simple Wi-Fi, PWM duty 0 if silence exceeds 300 ms. In the serial field, write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;none — no board yet&lt;/code&gt; unless a port is already present. Do not invent a log line.&lt;/p&gt;

&lt;p&gt;Place three calendar blocks in the next seven days that add up to 4–6 hours, named Read, Bench, and Notes. Until parts arrive, Bench may be “write the power-up card,” which is the safety lesson after you choose a track. Copy the checkpoint table into the note and put a real week beside weeks 1–3, 4–6, 7–11, and 12–14. If travel deletes a week, slip the later checkpoint. Do not delete Chapter 07 to catch up. List three things you will not buy this month: a LiDAR, a computer bought only “for ROS,” and a charger whose label you have not read. The cart, when you are ready, is &lt;a href=&quot;/contents/en/chapter00/00_05_bom_and_shopping/&quot;&gt;lesson 00-05&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you should see.&lt;/strong&gt; A stranger finds all six fields in a minute. The sentence contains 300. The calendar shows repeated sittings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When it goes wrong.&lt;/strong&gt; Blank fields get &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;none&lt;/code&gt;. Split a single six-hour Sunday. A Nav2 bookmark is labelled “after Chapter 07.” Loose lithium cells wait until lesson 00-05 matches a charger to a chemistry.&lt;/p&gt;

&lt;h2 id=&quot;worked-example&quot;&gt;Worked example&lt;/h2&gt;

&lt;p&gt;Hà starts on a Wednesday night with a Windows laptop and no parts. She computes two calendars before she picks one. A compressed plan of 6 hours a week for 12 weeks is $6 \times 12 = 72$ hours, but week 8 is a family trip, so Chapter 07 would land on a week she is not home. A 5-hour week for 14 weeks is 70 hours and still finishes Chapters 00–10 if she keeps the bench sitting. She writes the checkpoints as: weeks 1–3 Chapters 00–01, weeks 4–6 Chapters 02–03, weeks 7–11 Chapters 04–07, weeks 12–14 Chapters 08–10, Chapters 11–12 optional after the robot stops on silence.&lt;/p&gt;

&lt;p&gt;She copies the 300 ms stop into the same note, without pretending to flash it. The serial field says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;none — no board yet&lt;/code&gt;. The photo field says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;none&lt;/code&gt;. What broke: nothing powered. What fixed it: she wrote the week numbers before opening a shop tab. Thursday 20:00–21:30 is Bench, labelled “safety card, still no soldering.” That file is a pass. She has not flashed a board.&lt;/p&gt;

&lt;h2 id=&quot;explanation-from-high-level-goal-to-real-time-control-budget&quot;&gt;Explanation: From High-Level Goal to Real-Time Control Budget&lt;/h2&gt;

&lt;p&gt;Beginners often imagine robotics as “write a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;move_forward()&lt;/code&gt; function and you’re done.” In reality, every physical robot runs on a strict &lt;strong&gt;time-and-geometry budget&lt;/strong&gt;. Follow these four steps slowly:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Discretize time:&lt;/strong&gt; A microcontroller does not run continuously; it samples sensors and updates motor PWM every \(\Delta t\) seconds (for example, \(f_s = 100\text{ Hz} \implies \Delta t = 0.01\text{ s}\)).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Map chassis goals to wheel speeds:&lt;/strong&gt; For a differential-drive robot with wheel radius \(R\) and track width \(L\), forward speed \(v\) and yaw rate \(\omega\) relate to left/right wheel angular speeds by:
\(v = \frac{R}{2}(\omega_R + \omega_L), \qquad \omega = \frac{R}{L}(\omega_R - \omega_L).\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Check physical actuation limits:&lt;/strong&gt; Every motor has a maximum no-load speed \(\omega_{\max}\) and stall torque; commanding a speed above \(\omega_{\max}\) saturates the actuator and breaks open-loop predictions.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Verify timing slack:&lt;/strong&gt; If your control loop takes \(t_{\text{exec}}\) to read sensors, compute PID, and write PWM, you must maintain \(t_{\text{exec}} \ll \Delta t\) so jitter does not destabilize the loop.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;concrete-worked-example-sizing-a-100-hz-differential-drive-loop-by-hand&quot;&gt;Concrete Worked Example: Sizing a 100 Hz Differential-Drive Loop by Hand&lt;/h3&gt;

&lt;p&gt;Suppose your desk robot has wheel radius \(R = 0.0325\text{ m}\) (65 mm diameter), track width \(L = 0.16\text{ m}\), and runs a \(100\text{ Hz}\) control loop (\(\Delta t = 10\text{ ms}\)). You want it to cruise at \(v = 0.30\text{ m/s}\) while turning left at \(\omega = 0.50\text{ rad/s}\).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Step 1 — Solve for required wheel angular velocities:&lt;/strong&gt;
\(\omega_R = \frac{v + \frac{L}{2}\omega}{R} = \frac{0.30 + 0.08(0.50)}{0.0325} = \frac{0.34}{0.0325} \approx 10.46\text{ rad/s}.\)
\(\omega_L = \frac{v - \frac{L}{2}\omega}{R} = \frac{0.30 - 0.08(0.50)}{0.0325} = \frac{0.26}{0.0325} \approx 8.00\text{ rad/s}.\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 2 — Convert to RPM to check against your motor datasheet:&lt;/strong&gt;
\(\text{RPM}_R = 10.46 \times \frac{60}{2\pi} \approx 99.9\text{ RPM}, \qquad \text{RPM}_L = 8.00 \times \frac{60}{2\pi} \approx 76.4\text{ RPM}.\)
A standard 150 RPM yellow TT gearmotor has ample headroom.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Step 3 — Check distance traveled per control tick:&lt;/strong&gt;
\(\Delta s = v \cdot \Delta t = 0.30 \times 0.01 = 0.003\text{ m} = 3\text{ mm per tick}.\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Pitfall:&lt;/strong&gt; Assuming that because a 240 MHz microcontroller is “fast,” timing does not matter. Blocking calls like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delay(100)&lt;/code&gt; or slow serial prints inside the loop can stretch \(\Delta t\) unpredictably and turn a stable controller into an oscillator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Check:&lt;/strong&gt; If you halve the control frequency to \(50\text{ Hz}\) (\(\Delta t = 20\text{ ms}\)), the robot travels \(6\text{ mm}\) between updates at \(0.30\text{ m/s}\) — double the blind distance before any course correction occurs.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Write your Capstone A sentence. It must include two driven wheels, wheels up, serial or simple Wi-Fi, and PWM forced off when silence exceeds about 300 ms. Then write the week range in which that sentence becomes a demo you could film.&lt;/li&gt;
  &lt;li&gt;Compare two calendars. Plan P is 4 hours a week for 16 weeks. Plan Q is 6 hours a week for 12 weeks. Compute the total hours of each. Which one still has a honest place for Chapters 11 and 12 without stealing the week of the wheels-up test?&lt;/li&gt;
  &lt;li&gt;A journal entry has a date, a photo of a glowing LED, and the words “it worked.” The serial snippet is missing and “what broke” is missing. Name the two facts a later debugging session cannot recover from this entry.&lt;/li&gt;
  &lt;li&gt;A friend wants to start week 2 with a LeRobot ACT training notebook. Which checkpoint does that skip, and what physical behaviour of your future robot will the notebook not have tested?&lt;/li&gt;
  &lt;li&gt;Commands are sent every $100\,\mathrm{ms}$. How many consecutive missed commands does it take until $t_{\mathrm{silence}} &amp;gt; 300\,\mathrm{ms}$? After exactly three missed periods, are the motors required to be off?&lt;/li&gt;
&lt;/ol&gt;

&lt;details&gt;
&lt;summary&gt;Suggested answers&lt;/summary&gt;

1. One acceptable sentence: &quot;Capstone A passes when my two-wheel differential-drive robot, wheels off the table, follows teleop from serial or a simple Wi-Fi page, and the firmware sets PWM duty to 0 if no new command arrives for more than 300 ms.&quot; That demo sits in weeks 7–11, with Chapter 07 as the gate.
2. Plan P is $4 \times 16 = 64$ hours. Plan Q is $6 \times 12 = 72$ hours. Plan P can hold Chapters 11 and 12 after Chapters 00–10. Plan Q has no spare week inside the 12, so the wheels-up test still owns weeks 7–11.
3. You cannot recover the text the board actually printed, and you cannot recover what failed before the LED glowed. A later &quot;it broke again&quot; has nothing to compare.
4. It skips firmware, the driver, and Chapter 07. The notebook does not test a 300 ms stop, and it does not test that motor voltage stayed off the 3.3 V pins.
5. Three missed periods are $300\,\mathrm{ms}$, which is not yet over the limit. The fourth makes $400\,\mathrm{ms}$, and PWM must be 0. After exactly three missed periods the motors may still run.

&lt;/details&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.ros.org/en/jazzy/&quot;&gt;ROS 2 Jazzy documentation&lt;/a&gt; — the later messaging layer. Read the introduction if you want a picture of topics. Do not start an install from this lesson.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/huggingface/lerobot&quot;&gt;LeRobot&lt;/a&gt; — the library Chapters 11 and 12 will ask you to read carefully. Cloning it this week does not build the chassis.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.duckietown.com/&quot;&gt;Duckietown docs&lt;/a&gt; — a teaching robot with a published stack, useful as a picture of how a small wheeled robot is documented. It is not this course’s bill of materials.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://manipulation.mit.edu/&quot;&gt;MIT manipulation course&lt;/a&gt; — a rigorous look at manipulation and learned policies. Treat it as a destination for careful reading after your own robot can stop.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 

</feed>
