<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Harshit Agarwal</title>
    <description>The latest articles on DEV Community by Harshit Agarwal (@aharshit123456).</description>
    <link>https://dev.to/aharshit123456</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1559884%2Ff9a4ab6e-9ed4-4255-a54c-d16169ec935a.png</url>
      <title>DEV Community: Harshit Agarwal</title>
      <link>https://dev.to/aharshit123456</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aharshit123456"/>
    <language>en</language>
    <item>
      <title>A Google Lens Alternative for a Job Interview, Re-Audited</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:48 +0000</pubDate>
      <link>https://dev.to/aharshit123456/a-google-lens-alternative-for-a-job-interview-re-audited-4gkd</link>
      <guid>https://dev.to/aharshit123456/a-google-lens-alternative-for-a-job-interview-re-audited-4gkd</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/shoppin-lens-research" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In November 2024, shoppin' sent me an assignment before my interview: build an alternative to Google Lens. Propose four or five ways to do image similarity search, fine-tune them, compare precision, recall and retrieval accuracy, think about speed at scale, and write it all up with citations. I had about two weeks around end-semester exams. I built five embedding pipelines, three hashing attempts, a Siamese network, an autoencoder, a FastAPI and React demo, and a side project I cared about more than the brief: a &lt;strong&gt;cosplay lens&lt;/strong&gt; that finds costumes for an anime character.&lt;/p&gt;

&lt;p&gt;I got the job. Two years later I reopened the repo to write this post, re-read every notebook cell and its saved output, and found that several of my headline numbers were measuring something other than what the report claimed. This post explains how every method works, from the image to the vector to the nearest neighbour, and then audits my own evaluation the way I'd audit a teammate's. The bugs are the most useful part.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;8,677&lt;/strong&gt; Caltech-101 images indexed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;12&lt;/strong&gt; approaches attempted&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;5&lt;/strong&gt; evaluated end to end&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;41&lt;/strong&gt; anime characters scraped&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Everything here comes from the public repo, &lt;a href="https://github.com/aharshit123456/lens_research" rel="noopener noreferrer"&gt;aharshit123456/lens_research&lt;/a&gt;: two Colab notebooks (&lt;code&gt;lens.ipynb&lt;/code&gt;, &lt;code&gt;coslens.ipynb&lt;/code&gt;), a FastAPI backend, a React frontend, a scraper and the draft report I submitted, &lt;a href="https://github.com/aharshit123456/lens_research/blob/master/Review_on_Image_Similarity_Search%20(4).pdf" rel="noopener noreferrer"&gt;&lt;em&gt;Current Trends in Image Similarity Search and application in Fashion based Image Search Engine&lt;/em&gt;&lt;/a&gt; (PDF). All commits are mine, from 30 November to 13 December 2024. Numbers quoted as "printed" are verbatim from saved notebook outputs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What "a Google Lens" is, technically
&lt;/h2&gt;

&lt;p&gt;Strip away the product and Google Lens is &lt;strong&gt;content-based image retrieval&lt;/strong&gt; (CBIR): given a query image, return the images in a collection that look most like it. Every approach in this post fits the same three-step shape:&lt;/p&gt;

&lt;p&gt;Query image → &lt;strong&gt;Encoder image → vector&lt;/strong&gt; → Index nearest neighbours → Top-k results&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — every method is an encoder plus an index. The catalogue goes through the same encoder once, offline.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Encode.&lt;/strong&gt; Turn each image into a fixed-length vector (an &lt;em&gt;embedding&lt;/em&gt;) so that images that look alike end up close together.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index.&lt;/strong&gt; Store the catalogue's vectors in a structure that can answer "what's nearest to this vector?" quickly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search.&lt;/strong&gt; Encode the query the same way, look up its k nearest neighbours, return those images.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Almost all of the quality comes from step 1. Almost all of the speed at scale comes from step 2. The assignment asked for different &lt;em&gt;methods&lt;/em&gt;, which really means different encoders and different indexes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The dataset
&lt;/h2&gt;

&lt;p&gt;The main benchmark was &lt;strong&gt;Caltech-101&lt;/strong&gt;: 101 object categories (accordion, airplanes, anchor … wrench), 40 to 800 images each. I removed the &lt;code&gt;BACKGROUND_Google&lt;/code&gt; class, because it is a grab bag of unrelated pictures, some containing faces that overlap with the &lt;code&gt;Faces&lt;/code&gt; class. That left &lt;strong&gt;8,677 images in 101 classes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To report results per kind of object, I grouped the 101 classes into eight parent categories using a zero-shot text classifier on the class names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;classifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"zero-shot-classification"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"facebook/bart-large-mnli"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;parent_categories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Animals"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Appliances"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Clothes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Vehicles"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                     &lt;span class="s2"&gt;"Nature"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Furniture"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Electronics"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Miscellaneous"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;# for each class name: result = classifier(name, parent_categories)&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'labels'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It was a neat trick, and it was wrong surprisingly often. &lt;code&gt;Faces&lt;/code&gt; went to Animals, &lt;code&gt;Faces_easy&lt;/code&gt; and &lt;code&gt;menorah&lt;/code&gt; to Clothes, &lt;code&gt;revolver&lt;/code&gt; and &lt;code&gt;stapler&lt;/code&gt; to Vehicles, &lt;code&gt;watch&lt;/code&gt; to Nature, &lt;code&gt;grand_piano&lt;/code&gt; to Animals. The groups ended up lopsided: Animals had 35 classes, while Clothes and Appliances had 2 each. Scoring still used the fine Caltech labels, so the grouping only affects how averages are bucketed, but "Clothes" in my tables mostly means faces and a menorah. Lesson one: a zero-shot classifier on a one- or two-word label has almost no context. Ten minutes of hand-labelling would have been better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encoders, part 1: pretrained CNNs
&lt;/h2&gt;

&lt;p&gt;A convolutional network trained on ImageNet classification learns a hierarchy of filters: edges, then textures, then parts, then objects. If you cut off the final classification layer, the activations just before it make a strong general-purpose description of the image. This is the oldest trick in deep CBIR and it still works.&lt;/p&gt;

&lt;p&gt;I used four Keras models, all with ImageNet weights, &lt;code&gt;include_top=False&lt;/code&gt; (no classifier) and global max pooling, so each image becomes one vector:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Embedding size&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;ResNet50&lt;/td&gt;
&lt;td&gt;2048&lt;/td&gt;
&lt;td&gt;Residual connections; the classic feature extractor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VGG16&lt;/td&gt;
&lt;td&gt;512&lt;/td&gt;
&lt;td&gt;Old and heavy, but simple features&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EfficientNetV2-B0&lt;/td&gt;
&lt;td&gt;1280&lt;/td&gt;
&lt;td&gt;Compound-scaled; much smaller and faster&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MobileNetV2&lt;/td&gt;
&lt;td&gt;1280&lt;/td&gt;
&lt;td&gt;Built for phones; depthwise-separable convolutions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tensorflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;applications&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resnet50&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;ResNet50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;preprocess_input&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResNet50&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weights&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"imagenet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;include_top&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pooling&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"max"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                 &lt;span class="n"&gt;input_shape&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;224&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;224&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;preprocess_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pil_image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;224&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;224&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# shape (2048,)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The one line that matters most there is &lt;code&gt;preprocess_input&lt;/code&gt;. Every Keras model expects its inputs normalised the way it saw them in training. ResNet50 and VGG16 subtract per-channel ImageNet means in BGR order, MobileNetV2 scales to [-1, 1], and EfficientNetV2 does its own rescaling inside the model. Feed a model the wrong normalisation and its features drift in ways no error message reports. I did exactly that with MobileNet, which comes up again in the results.&lt;/p&gt;

&lt;h3&gt;
  
  
  What CNN features get wrong
&lt;/h3&gt;

&lt;p&gt;CNN features from a classifier are biased toward &lt;strong&gt;shape and texture&lt;/strong&gt;, not meaning. The best illustration in my notebook is a spoon:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwhr4vwzjb9r94p1ygx80.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwhr4vwzjb9r94p1ygx80.webp" alt="A spoon query image on the left; the top five results on the right are three wrenches, a headset and a ceiling fan" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 2 — an early CNN-feature run (probably ResNet50 features, judging by the cell next to it). A spoon returns three wrenches, a headset and a ceiling fan. One wrench is grayscale, drawn with matplotlib's default colour map.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To the network, a spoon is an elongated, shiny, metal object on a white background, and so is a wrench. This is the core weakness of using ImageNet classifier features for shopping: two products can look alike in silhouette and be completely different things, and the same product can look different photographed on a model versus flat on a table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encoders, part 2: vision transformers
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Vision Transformer&lt;/strong&gt; (ViT) cuts the image into 16×16 patches, treats each patch like a word token, and runs a transformer over the sequence. A special &lt;code&gt;[CLS]&lt;/code&gt; token attends to every patch, and its final state summarises the image. That summary vector is the embedding. I tried three transformer encoders through Hugging Face &lt;code&gt;transformers&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Checkpoint&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;th&gt;How it was trained&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ViT&lt;/td&gt;
&lt;td&gt;&lt;code&gt;google/vit-base-patch16-224&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;768&lt;/td&gt;
&lt;td&gt;Supervised classification (ImageNet-21k, then ImageNet-1k)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DINOv2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;facebook/dinov2-base&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;768&lt;/td&gt;
&lt;td&gt;Self-supervised: no labels, the model learns by matching views of the same image&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CLIP&lt;/td&gt;
&lt;td&gt;&lt;code&gt;openai/clip-vit-base-patch32&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;512&lt;/td&gt;
&lt;td&gt;Contrastive image–text: images and their captions are pulled together in one space&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;transformers&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;AutoImageProcessor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AutoModel&lt;/span&gt;

&lt;span class="n"&gt;processor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AutoImageProcessor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_pretrained&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"facebook/dinov2-base"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AutoModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_pretrained&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"facebook/dinov2-base"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;inputs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;processor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;images&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;pil_image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_tensors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"pt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;no_grad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_hidden_state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;   &lt;span class="c1"&gt;# the CLS token, shape (1, 768)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For CLIP the call is &lt;code&gt;model.get_image_features(**inputs)&lt;/code&gt;, which returns the 512-d vector in the shared image–text space.&lt;/p&gt;

&lt;p&gt;Why should these beat CNN features?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DINOv2&lt;/strong&gt; was trained specifically to produce features that work without fine-tuning: its objective makes two crops of the same image agree, across 142 million curated images. That's close to what retrieval needs, "same thing, different photo".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CLIP&lt;/strong&gt; learned from 400 million image–caption pairs, so its space is organised by &lt;em&gt;meaning a human would describe&lt;/em&gt;, not just texture. The bonus for a shopping product is that a text query lands in the same space: "red bodycon knit dress" can search an image index directly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ViT&lt;/strong&gt; is a supervised classifier like the CNNs, but its global attention sees the whole image at once rather than building up from local filters.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Indexes: finding the nearest vectors
&lt;/h2&gt;

&lt;p&gt;Once every catalogue image is a vector, search is "find the k vectors closest to the query vector". I used scikit-learn's &lt;code&gt;NearestNeighbors&lt;/code&gt; with k = 5: a &lt;strong&gt;k-d tree&lt;/strong&gt; with Euclidean (L2) distance for the CNN features, and a &lt;strong&gt;ball tree&lt;/strong&gt; for the transformer features.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sklearn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;neighbors&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;NearestNeighbors&lt;/span&gt;

&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NearestNeighbors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n_neighbors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"kd_tree"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"euclidean"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;feature_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                       &lt;span class="c1"&gt;# (8677, 2048) for ResNet50&lt;/span&gt;
&lt;span class="n"&gt;distances&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;kneighbors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query_vector&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both trees work by splitting space into regions and skipping regions that can't contain a closer point. That works well in 2 or 10 dimensions. In 768 or 2048 dimensions it barely helps: high-dimensional points are all roughly equidistant, so almost no region can be ruled out and the tree ends up visiting nearly everything. At 8,677 images a plain brute-force scan is just as fast. Trees don't make this scale; approximate methods do (more on that below).&lt;/p&gt;

&lt;p&gt;Two smaller choices are worth knowing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cosine vs Euclidean.&lt;/strong&gt; For embeddings, the angle between vectors usually matters more than their length. If you L2-normalise every vector first, Euclidean distance ranks results exactly like cosine similarity, so normalise once at index time and use either. I didn't normalise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PCA.&lt;/strong&gt; I computed a 100-component PCA of the features, which would shrink the index 20× and speed up every distance calculation, but the index was then fit on the raw features. That cell did nothing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Locality-sensitive hashing
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Locality-sensitive hashing&lt;/strong&gt; (LSH) is the classic answer to "trees don't work in high dimensions". The idea for cosine similarity is random hyperplanes: pick a random vector &lt;em&gt;r&lt;/em&gt;; the bit &lt;code&gt;sign(r · x)&lt;/code&gt; says which side of that hyperplane &lt;em&gt;x&lt;/em&gt; falls on. Two vectors with a small angle between them land on the same side of most random hyperplanes. Take 10 such bits and you have a 10-bit hash where similar vectors usually collide. Build 5 independent tables to reduce the chance that a true neighbour lands in a different bucket every time. At query time you only compare against the vectors in the query's buckets, not the whole catalogue.&lt;/p&gt;

&lt;p&gt;I tried it three ways, and none produced a usable result:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Hand-written random projections&lt;/strong&gt;, 10 bits × 5 tables. The cell errors with &lt;code&gt;shapes (10,1280) and (128,) not aligned&lt;/code&gt;: the hyperplanes were built for 1280-d EfficientNet vectors and the query was a 128-d PCA vector.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The &lt;code&gt;sparselsh&lt;/code&gt; library&lt;/strong&gt;, &lt;code&gt;LSH(hash_size=10, input_dim=1280, num_hashtables=5)&lt;/code&gt;. This ran and printed &lt;code&gt;Precision: 0.00&lt;/code&gt;. The bug was in my scoring: I compared each result's full file path to the class name, which never matches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;datasketch&lt;/code&gt;'s &lt;code&gt;MinHashLSH&lt;/code&gt;&lt;/strong&gt; on PCA-compressed ViT vectors, which printed "No similar images found." MinHash estimates &lt;em&gt;Jaccard similarity between sets&lt;/em&gt;. It's the right tool for near-duplicate text or shingles and the wrong tool for dense float vectors.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My draft report had an LSH results table. It was an exact copy of the k-NN table. That was a placeholder I never replaced, and it should never have looked like a result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deep metric learning: a Siamese network with triplet loss
&lt;/h2&gt;

&lt;p&gt;Pretrained features are general. &lt;strong&gt;Metric learning&lt;/strong&gt; trains the embedding directly for "these two are the same thing". A Siamese network runs a shared encoder over an &lt;em&gt;anchor&lt;/em&gt;, a &lt;em&gt;positive&lt;/em&gt; (same class) and a &lt;em&gt;negative&lt;/em&gt; (different class), and the &lt;strong&gt;triplet loss&lt;/strong&gt; pushes the anchor closer to the positive than to the negative by a margin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;L(a, p, n) = max( ‖f(a) − f(p)‖² − ‖f(a) − f(n)‖² + margin, 0 )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My setup: ResNet50 frozen up to &lt;code&gt;conv5_block1_out&lt;/code&gt;, then Dense 512 → 256 → 256 trainable layers, 200×200 inputs, margin 0.5, Adam at 1e-4, and 4,310 anchor–positive pairs from Caltech-101 with random negatives.&lt;/p&gt;

&lt;p&gt;It trained for four epochs before I stopped it partway through the fifth. Loss went 0.448 → 0.207 → 0.194 → 0.152 and validation loss 0.336 → 0.324 → 0.279 → 0.255, so it was learning. But each epoch took 43 to 48 minutes at about 25 seconds a step, which means it was running on CPU, and the sanity check at the end was telling. The cosine similarity of an anchor to its positive was &lt;strong&gt;0.996&lt;/strong&gt; and to its negative &lt;strong&gt;0.994&lt;/strong&gt;. Everything was still crowded into one tiny region of the space. A triplet model needs many more epochs, and above all &lt;strong&gt;hard negatives&lt;/strong&gt; (negatives that currently sit close to the anchor), before it separates classes. I never evaluated it for retrieval.&lt;/p&gt;

&lt;h2&gt;
  
  
  Autoencoders
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;autoencoder&lt;/strong&gt; compresses an image to a small latent vector and reconstructs it, trained only on reconstruction error. The latent vector can serve as an embedding. Mine was a convolutional autoencoder on grayscale images, and it never trained: the input was 377×510, and three rounds of 2× pooling and 2× upsampling produce 384×512, so the loss compared tensors of different shapes. The fix is to resize inputs to a multiple of 2ⁿ (or crop the output). The more important point is that autoencoders are a weak choice here anyway: reconstruction rewards remembering pixels and lighting, not what an object &lt;em&gt;is&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I measured, and what the measurement got wrong
&lt;/h2&gt;

&lt;p&gt;Here is the scoring code, which is identical in every evaluation cell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;true_labels&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;ground_truth_label&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;predicted_labels&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;precision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;precision_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;true_labels&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;true_labels&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zero_division&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;recall&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recall_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;true_labels&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;true_labels&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zero_division&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;ndcg&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ndcg_score&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;true_labels&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;true_labels&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For each query I took the 5 nearest neighbours and marked each as relevant if it shared the query's Caltech class. About three random queries per class were drawn, around 303 queries in total, and the scores were averaged. Reading it again, there are five problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Precision is fine: it's precision@5.&lt;/strong&gt; It's the share of the 5 results that come from the right class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recall is always 1.00, by construction.&lt;/strong&gt; Every result is "predicted positive", so there are never false negatives, and the zero-hit case returns 1 because of &lt;code&gt;zero_division=1&lt;/code&gt;. Real recall@k asks what fraction of &lt;em&gt;all&lt;/em&gt; relevant images in the catalogue appear in the top k. For a class with 80 images and k = 5, the best possible recall@5 is 0.06. Every "Recall: 1.00" in my report is an artefact.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;nDCG ignores ranking.&lt;/strong&gt; Passing the same score for all five results makes every ordering a tie, so this nDCG is just a rescaled hit count. It can't tell "right answer first" from "right answer fifth".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Queries were drawn from the index.&lt;/strong&gt; Each query image is also in the catalogue, at distance 0 from itself, so the first result is always the query. That's one free hit in five: precision can't drop below 0.20.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retrieval time only timed the encoder.&lt;/strong&gt; The timer wrapped &lt;code&gt;model.predict&lt;/code&gt; (or the transformer forward pass). The nearest-neighbour search ran after the timer stopped.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;None of this means the encoders were bad. It means the table measured less than it claimed. The fix is standard: &lt;strong&gt;hold out queries&lt;/strong&gt; (or exclude the query from its own results), report &lt;strong&gt;precision@k, recall@k and mAP&lt;/strong&gt; with real definitions, compute nDCG from the actual similarity scores, and time the whole request.&lt;/p&gt;

&lt;h2&gt;
  
  
  The numbers, re-read
&lt;/h2&gt;

&lt;p&gt;Because exactly one of the five results is always the query itself, the precision &lt;strong&gt;excluding self-matches&lt;/strong&gt; can be recovered from the printed precision@5 &lt;em&gt;p&lt;/em&gt; as (5*p* − 1) / 4. These are the evaluations that actually printed results:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Encoder&lt;/th&gt;
&lt;th&gt;Printed precision@5&lt;/th&gt;
&lt;th&gt;Precision without the self-match&lt;/th&gt;
&lt;th&gt;Printed time (encoder only)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;DINOv2&lt;/td&gt;
&lt;td&gt;0.96&lt;/td&gt;
&lt;td&gt;≈ 0.95&lt;/td&gt;
&lt;td&gt;0.03 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ViT&lt;/td&gt;
&lt;td&gt;0.95&lt;/td&gt;
&lt;td&gt;≈ 0.94&lt;/td&gt;
&lt;td&gt;0.88 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EfficientNetV2-B0&lt;/td&gt;
&lt;td&gt;0.90&lt;/td&gt;
&lt;td&gt;≈ 0.88&lt;/td&gt;
&lt;td&gt;0.07 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CLIP&lt;/td&gt;
&lt;td&gt;0.89&lt;/td&gt;
&lt;td&gt;≈ 0.86&lt;/td&gt;
&lt;td&gt;0.02 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;DINOv2&lt;/td&gt;
&lt;td&gt;0.95&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ViT&lt;/td&gt;
&lt;td&gt;0.94&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EfficientNetV2&lt;/td&gt;
&lt;td&gt;0.88&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CLIP&lt;/td&gt;
&lt;td&gt;0.86&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Fig. 3 — precision of the top results once the query's match with itself is removed, on Caltech-101 (about 3 queries per class).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The ranking survives the correction. &lt;strong&gt;DINOv2 was the best encoder&lt;/strong&gt;, which matches what the field found: self-supervised features trained for invariance are built for retrieval. CLIP came last of the four here, which is also expected. Caltech-101 asks "which exact object category is this?", and CLIP's space is organised around how people &lt;em&gt;describe&lt;/em&gt; images, which blurs fine visual distinctions. The real advantage of CLIP, text queries, never showed up in this benchmark.&lt;/p&gt;

&lt;p&gt;The per-category runs make the gap between encoders vivid:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ViT, Animals&lt;/td&gt;
&lt;td&gt;0.88&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MobileNet, Animals&lt;/td&gt;
&lt;td&gt;0.03&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ViT, Electronics&lt;/td&gt;
&lt;td&gt;0.98&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MobileNet, Electronics&lt;/td&gt;
&lt;td&gt;0.36&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Fig. 4 — precision without self-matches, by parent category. MobileNet's index was built with the wrong preprocessing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;MobileNet's collapse (printed 0.22 for Animals, which is barely above the 0.20 that the self-match guarantees) isn't MobileNet being a bad model. Its index was built from images normalised with &lt;strong&gt;VGG's&lt;/strong&gt; &lt;code&gt;preprocess_input&lt;/code&gt;, and queries were normalised with MobileNet's. Query and catalogue vectors came from two different input distributions. That one line explains most of the "MobileNet is weak" conclusion in my draft.&lt;/p&gt;

&lt;p&gt;Three more corrections to the &lt;a href="https://github.com/aharshit123456/lens_research/blob/master/Review_on_Image_Similarity_Search%20(4).pdf" rel="noopener noreferrer"&gt;draft report&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ResNet50's numbers&lt;/strong&gt; in the report (precision 0.84, and a per-category table) don't appear in any saved notebook output. ResNet50 features were extracted, but I can't find a printed evaluation, so I'm not claiming them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;VGG16&lt;/strong&gt; features were extracted and never evaluated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The timings aren't comparable.&lt;/strong&gt; ViT at 0.88 s and DINOv2 at 0.03 s are models of the same size, so the gap says more about which runtime the Colab session had (one cell printed "GPU not available, running on CPU") than about the models. On a GPU, any of these encoders embeds an image in milliseconds.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The cosplay lens
&lt;/h2&gt;

&lt;p&gt;The assignment was about retrieval in general. What I actually wanted to build was a &lt;strong&gt;cosplay searcher&lt;/strong&gt;: give it an anime character and get matched costume pieces from across the internet. It's a harder retrieval problem than Caltech-101, because the same character is drawn, photographed on many different people, cropped differently, and half the visual signal is a wig.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building the dataset
&lt;/h3&gt;

&lt;p&gt;There was no dataset, so I scraped one. &lt;code&gt;scraper.py&lt;/code&gt; reads a list of 41 character names (Naruto, Levi Ackerman, Nezuko, Gaara, Zoro …), queries the Google Custom Search image API with the name plus "cosplay", downloads up to 10 results per character, verifies each file opens as an image (converting the ones that don't), and writes the URLs to a CSV per character.&lt;/p&gt;

&lt;p&gt;41 character names → Custom Search API "name + cosplay", 10 results → Download + verify with PIL → &lt;strong&gt;One folder per character&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 5 — the cosplay scraper. One API call per character, ten images back.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The final folder had 42 classes and 288 files, of which 282 were readable by Keras. That's between 1 and 10 images per character: Gohan, Piccolo, Ryuk and Tatsumaki ended up with &lt;strong&gt;one image each&lt;/strong&gt;. (One more small bug: the query was &lt;code&gt;keyword + "cosplay"&lt;/code&gt; with no space, so Google received "Narutocosplay".) A web search for "Nezuko cosplay" also returns fan art, merchandise and screenshots, so the labels were noisy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fine-tuning, and watching it overfit
&lt;/h3&gt;

&lt;p&gt;The report said the cosplay models were fine-tuned. Here's what that run did: ResNet50 with ImageNet weights, every layer trainable (24 million parameters), plus Dense 256 → Dropout 0.5 → Dense 42 softmax, split 80/20 for test and then 80/20 again for validation, Adam at 1e-4, 10 epochs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyhcjkno5p81fvse1ohj0.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyhcjkno5p81fvse1ohj0.webp" alt="Loss and accuracy curves: training loss falls to near zero and training accuracy rises to 0.95, while validation loss rises and validation accuracy stays at 0 for all 10 epochs" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 6 — the cosplay fine-tune. Training accuracy climbs from 0.06 to 0.95; validation accuracy is 0.0 at every epoch and validation loss rises from 7.0 to 11.3. Test accuracy: 3.12%.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That is textbook overfitting, and it was predictable from the numbers alone. 24 million free parameters, about 180 training images, 42 classes and ~4 images per class means the network can simply memorise every training photo. Validation accuracy stuck at exactly zero also means many classes had no images in the validation split at all. With one to ten images per class, a random 80/20/20 split can't put every character in every split.&lt;/p&gt;

&lt;p&gt;Then I built the search index on the model's &lt;strong&gt;42-dimensional softmax outputs&lt;/strong&gt;. An overfit softmax is close to one-hot, and for a new image it puts nearly all its mass on the same few classes, so every query had almost the same vector. In the evaluation every one of 27 queries returned the &lt;strong&gt;same five images&lt;/strong&gt;. Twenty-four had zero correct results.&lt;/p&gt;

&lt;h3&gt;
  
  
  The confusion matrix that looked great
&lt;/h3&gt;

&lt;p&gt;An earlier cosplay run, on a smaller 9-character set, produced this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feyxpudonl7gxtfcke55e.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feyxpudonl7gxtfcke55e.webp" alt="A 9 by 9 confusion matrix for cosplay characters, almost perfectly diagonal with one off-diagonal entry" width="800" height="756"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 7 — top-1 retrieval on 9 characters. Near-perfect, because each image's nearest neighbour is itself.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It looks like 98% accuracy. It's computed by &lt;code&gt;neighbors.kneighbors(feature_list)&lt;/code&gt;: querying the index with the same vectors it was built from, so every image's top-1 match is itself at distance zero. The single off-diagonal entry is a Mikasa image whose top match was filed under Levi, most likely the same picture scraped for both characters, who wear the same uniform. This is the same self-match bug as before, in its purest form. A diagonal confusion matrix from a retrieval system should make you suspicious, not happy.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the cosplay lens should have been
&lt;/h3&gt;

&lt;p&gt;With 282 noisy images, training anything was the wrong move. The right design for that data is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;No fine-tuning.&lt;/strong&gt; Use DINOv2 or CLIP embeddings as-is, which already worked best on Caltech-101.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use CLIP's text side.&lt;/strong&gt; "Nezuko Kamado cosplay" can be a query on its own, with no example image needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Crop before embedding.&lt;/strong&gt; A cosplay photo is a person, a background and a costume. Detect the person, segment the garments, and embed each piece, so the haori matches haoris instead of matching the background of a convention hall.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evaluate with held-out queries&lt;/strong&gt; and a recall@k that can fail.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Point 3 is, more or less, what the actual job turned out to be about.&lt;/p&gt;

&lt;h2&gt;
  
  
  The demo app
&lt;/h2&gt;

&lt;p&gt;The repo also has a small working product: a &lt;strong&gt;FastAPI&lt;/strong&gt; backend and a &lt;strong&gt;React&lt;/strong&gt; frontend. You upload an image, pick a model from a dropdown (ResNet, VGG or EfficientNet), and get the five nearest Caltech-101 images, served from the frontend's &lt;code&gt;public/&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;React upload + model dropdown → FastAPI POST /search/ resize 224, extract features → &lt;strong&gt;Pickled k-NN index 5 neighbours&lt;/strong&gt; → Image paths back to React&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 8 — the demo. Encoders and the index are unpickled once at startup.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It worked for the default path, and a code review finds four problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One index for every model.&lt;/strong&gt; The backend loads a ResNet extractor and an EfficientNet extractor but a single &lt;code&gt;nearest_neighbors.pkl&lt;/code&gt; fit on 2048-d ResNet vectors. Picking EfficientNet sends a 1280-d vector to a 2048-d index, which raises an error and returns HTTP 500. Picking VGG raises a &lt;code&gt;KeyError&lt;/code&gt;, because that model was commented out. Each encoder needs its own index.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wrong preprocessing again.&lt;/strong&gt; The server divides pixels by 255, while the index was built with Keras &lt;code&gt;preprocess_input&lt;/code&gt;. It's the MobileNet bug in a second place.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;img.show()&lt;/code&gt;&lt;/strong&gt; opens an image viewer on the server for every request. It was a debugging line I never removed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unused data loaded at startup.&lt;/strong&gt; &lt;code&gt;res.npy&lt;/code&gt; (71 MB) is loaded and never read.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What a production version needs
&lt;/h2&gt;

&lt;p&gt;A two-week interview project and a production visual search engine differ mostly in the parts the assignment didn't ask about:&lt;/p&gt;

&lt;p&gt;Photo → Detect + segment each garment → &lt;strong&gt;Fashion-tuned embedding&lt;/strong&gt; → ANN index HNSW / IVF-PQ → Re-rank + filters&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 9 — the shape of a real fashion lens: crop first, embed per item, approximate search, then re-rank.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Detection and segmentation first.&lt;/strong&gt; A street photo has several products in it. Each one needs to become its own query.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An embedding tuned for the domain&lt;/strong&gt;, trained with metric learning on real product pairs (the same item from different shops, or shown on a model and laid flat), with hard-negative mining. This is where the Siamese idea pays off, given enough data and a GPU.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Approximate nearest-neighbour search.&lt;/strong&gt; HNSW graphs or inverted-file indexes with product quantisation (FAISS, or a vector database) search millions of vectors in milliseconds, with recall you can measure and tune.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A catalogue that stays fresh&lt;/strong&gt;, which means scrapers and pipelines that keep re-embedding products as stores change them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evaluation that can fail&lt;/strong&gt;: held-out queries, recall@k against all relevant items, mAP, and latency measured end to end.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rest of this series covers those parts as I worked on them at shoppin': detection, segmentation, embedding models, the infrastructure, and the scrapers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell myself in 2024
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Be suspicious of metrics that flatter you.&lt;/strong&gt; Recall at 1.00 for every model, and a diagonal confusion matrix, were both telling me the measurement was broken, and I read them as good news.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never query with an image that's in the index&lt;/strong&gt;, or exclude it from its own results. This one bug inflated every number in the project.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Preprocessing is part of the model.&lt;/strong&gt; Two of the worst results in the repo were the right model fed the wrong normalisation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A placeholder table is a liability.&lt;/strong&gt; Copying the k-NN table into the LSH section "for now" produced a results table for an experiment that scored zero.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Small data means no training.&lt;/strong&gt; With 282 images, pretrained embeddings and good cropping beat any fine-tuning I could have done.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The strongest result was the simplest one.&lt;/strong&gt; DINOv2 features plus brute-force nearest neighbours, with no training, beat everything I trained.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The assignment also asked for "4–5 methods". I attempted twelve, and five worked end to end. In hindsight, five done properly, with an evaluation that could fail, would have been a better submission than twelve with tables I hadn't checked. It still got me the job, and the &lt;a href="https://www.aharshit123456.space/blog/shoppin-experience" rel="noopener noreferrer"&gt;story of how that happened&lt;/a&gt; is its own post.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;K. He, X. Zhang, S. Ren, J. Sun. &lt;em&gt;Deep Residual Learning for Image Recognition&lt;/em&gt;. CVPR 2016. &lt;a href="https://doi.org/10.1109/CVPR.2016.90" rel="noopener noreferrer"&gt;doi:10.1109/CVPR.2016.90&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;K. Simonyan, A. Zisserman. &lt;em&gt;Very Deep Convolutional Networks for Large-Scale Image Recognition&lt;/em&gt;. 2015. &lt;a href="https://arxiv.org/abs/1409.1556" rel="noopener noreferrer"&gt;arXiv:1409.1556&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;M. Tan, Q. V. Le. &lt;em&gt;EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks&lt;/em&gt;. 2019. &lt;a href="https://arxiv.org/abs/1905.11946" rel="noopener noreferrer"&gt;arXiv:1905.11946&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;A. Dosovitskiy et al. &lt;em&gt;An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale&lt;/em&gt;. 2021. &lt;a href="https://arxiv.org/abs/2010.11929" rel="noopener noreferrer"&gt;arXiv:2010.11929&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;M. Oquab et al. &lt;em&gt;DINOv2: Learning Robust Visual Features without Supervision&lt;/em&gt;. 2024. &lt;a href="https://arxiv.org/abs/2304.07193" rel="noopener noreferrer"&gt;arXiv:2304.07193&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;A. Radford et al. &lt;em&gt;Learning Transferable Visual Models From Natural Language Supervision&lt;/em&gt; (CLIP). 2021. &lt;a href="https://arxiv.org/abs/2103.00020" rel="noopener noreferrer"&gt;arXiv:2103.00020&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;F. Schroff, D. Kalenichenko, J. Philbin. &lt;em&gt;FaceNet: A Unified Embedding for Face Recognition and Clustering&lt;/em&gt; (triplet loss). CVPR 2015. &lt;a href="https://arxiv.org/abs/1503.03832" rel="noopener noreferrer"&gt;arXiv:1503.03832&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;M. Charikar. &lt;em&gt;Similarity Estimation Techniques from Rounding Algorithms&lt;/em&gt; (random-hyperplane LSH). STOC 2002. &lt;a href="https://doi.org/10.1145/509907.509965" rel="noopener noreferrer"&gt;doi:10.1145/509907.509965&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Y. A. Malkov, D. A. Yashunin. &lt;em&gt;Efficient and Robust Approximate Nearest Neighbor Search Using Hierarchical Navigable Small World Graphs&lt;/em&gt;. 2018. &lt;a href="https://arxiv.org/abs/1603.09320" rel="noopener noreferrer"&gt;arXiv:1603.09320&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;F.-F. Li, M. Andreeto, M. Ranzato, P. Perona. &lt;em&gt;Caltech 101&lt;/em&gt;. &lt;a href="https://doi.org/10.22002/D1.20086" rel="noopener noreferrer"&gt;doi:10.22002/D1.20086&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;S. Lepage, J. Mary, D. Picard. &lt;em&gt;LRVS-Fashion: Extending Visual Search with Referring Instructions&lt;/em&gt;. 2023. &lt;a href="https://arxiv.org/abs/2306.02928" rel="noopener noreferrer"&gt;arXiv:2306.02928&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  More from Building shoppin'
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/shoppin-experience" rel="noopener noreferrer"&gt;shoppin' Experience&lt;/a&gt;: how I found shoppin', the strange application email, and the interview.&lt;/li&gt;
&lt;li&gt;The next posts cover object detection, segmentation, image-search embeddings, infrastructure and Terraform, and the scrapers.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>machinelearning</category>
      <category>computervision</category>
      <category>python</category>
      <category>ai</category>
    </item>
    <item>
      <title>From ORB Features to ORB-SLAM3</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:44 +0000</pubDate>
      <link>https://dev.to/aharshit123456/from-orb-features-to-orb-slam3-31ec</link>
      <guid>https://dev.to/aharshit123456/from-orb-features-to-orb-slam3-31ec</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/orb-slam3" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; &lt;strong&gt;Disclaimer:&lt;/strong&gt; This research and presentation were first presented at &lt;strong&gt;NISER SMLAB TALKS&lt;/strong&gt; on May 31, 2024.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In May 2024 I joined SMLab at NISER as a summer intern on Swayansaasita, the lab's project on sensor data and perception for autonomous two-wheelers. One of my first jobs was to read the SLAM literature and work out what the state of the art looked like. A week in, I had to pick one paper and present it at the lab's talk series. I picked ORB-SLAM3, because so many of the other papers I was reading either compared themselves against it or were built on top of it.&lt;/p&gt;

&lt;p&gt;The talk was a short deck and a live demo. This post is the longer version: it starts at a single pixel (how FAST decides something is a corner), builds up the binary ORB descriptor, compares ORB with the other common detectors, shows my demo, and then walks through ORB-SLAM3: tracking, local mapping, loop closing, the Atlas of multiple maps, DBoW2 place recognition and IMU initialisation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;256 bits&lt;/strong&gt; per ORB descriptor&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;~3,835&lt;/strong&gt; ORB keypoints per photo in my demo&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;~0.02 s&lt;/strong&gt; detect + describe per photo&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;~28%&lt;/strong&gt; keypoints with a mutual match&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why a SLAM system needs features
&lt;/h2&gt;

&lt;p&gt;SLAM (simultaneous localisation and mapping) asks a sensor to work out where it is while building the map it localises against. A camera-based system answers one question thousands of times a second: &lt;em&gt;is this thing I see now the same physical point I saw before?&lt;/em&gt; That is &lt;strong&gt;data association&lt;/strong&gt;, and every later stage (pose estimation, triangulation, bundle adjustment, loop closure) assumes it was answered correctly. A wrong association is much harder to recover from than a missing one.&lt;/p&gt;

&lt;p&gt;Feature-based systems answer it in two steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;keypoint detector&lt;/strong&gt; picks pixel locations that are distinctive and repeatable, such as corners. A flat wall gives you nothing; the corner of a window gives you something you can find again.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;descriptor&lt;/strong&gt; turns the patch around each keypoint into a vector, so that patches showing the same physical point produce close vectors and different points produce distant ones.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Matching is then a nearest-neighbour search in descriptor space. With &lt;em&gt;n&lt;/em&gt; keypoints in one image and &lt;em&gt;m&lt;/em&gt; in the next, brute-force matching costs &lt;em&gt;n × m&lt;/em&gt; distance computations per frame. That cost is why the descriptor matters so much for a real-time system, and it is the reason ORB exists.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAST: a corner test on a ring of pixels
&lt;/h2&gt;

&lt;p&gt;FAST (Features from Accelerated Segment Test, Rosten and Drummond) looks at a circle of 16 pixels around a candidate pixel &lt;em&gt;p&lt;/em&gt; and asks whether a long enough contiguous arc of them is all brighter than &lt;em&gt;p&lt;/em&gt; by some threshold &lt;em&gt;t&lt;/em&gt;, or all darker by &lt;em&gt;t&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p is a corner if there is a contiguous arc of N pixels q on the ring with
    I(q) &amp;gt; I(p) + t   for every q in the arc      (all brighter), or
    I(q) &amp;lt; I(p) - t   for every q in the arc      (all darker)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ORB uses the FAST-9 variant. The test is cheap because most pixels can be rejected after looking at only a few ring positions, and the original FAST work learned a decision tree over those checks so that the common "not a corner" case exits almost immediately.&lt;/p&gt;

&lt;p&gt;FAST has three gaps that ORB has to fill:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;No cornerness score.&lt;/strong&gt; FAST also fires along strong edges. ORB sets the FAST threshold low enough to get more than the target number &lt;em&gt;N&lt;/em&gt; of points, ranks them by the Harris corner measure, and keeps the top &lt;em&gt;N&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No scale.&lt;/strong&gt; ORB builds an image pyramid (the image downscaled several times) and runs FAST plus Harris filtering on every level.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No orientation.&lt;/strong&gt; Rotate the camera and the patch around a corner rotates too, so a descriptor computed on a fixed grid changes. This is the gap the "O" in ORB fills.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Orientation from the intensity centroid
&lt;/h2&gt;

&lt;p&gt;The idea, which the ORB paper takes from Rosin's work on measuring corner properties, is that the brightness inside a corner patch is not centred on the corner. Compute the patch's centre of mass, using intensity as mass, and the vector from the corner to that centroid gives a direction that rotates with the image.&lt;/p&gt;

&lt;p&gt;The moments of a patch are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;m_pq = Σ_(x,y)  x^p · y^q · I(x, y)

centroid     C = ( m_10 / m_00 ,  m_01 / m_00 )

orientation  θ = atan2( m_01 , m_10 )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; are measured from the keypoint, so the keypoint is the origin. &lt;code&gt;atan2&lt;/code&gt; is the quadrant-aware arctangent, and that matters: a plain &lt;code&gt;arctan(m_01 / m_10)&lt;/code&gt;, which is how I first wrote it in my own notes, cannot tell a patch pointing up-right from one pointing down-left. The paper also restricts the sums to a circular region rather than a square, so that rotating the patch does not change which pixels are included.&lt;/p&gt;

&lt;p&gt;On artificially rotated, noisy patches, the ORB authors found the centroid more reliable than two gradient-based estimates, one of them SIFT-like. It is also cheap: three sums over a patch.&lt;/p&gt;

&lt;h2&gt;
  
  
  BRIEF, and why binary descriptors
&lt;/h2&gt;

&lt;p&gt;SIFT describes a patch with a 128-dimensional vector of floats built from gradient histograms. Comparing two SIFT descriptors means a Euclidean distance over 128 floats. Do that for every candidate pair in every frame and it becomes the bottleneck.&lt;/p&gt;

&lt;p&gt;BRIEF (Calonder et al.) takes a different route. Smooth the patch, pick a fixed list of pixel pairs (&lt;em&gt;x_i&lt;/em&gt;, &lt;em&gt;y_i&lt;/em&gt;) inside it, and for each pair write down one bit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;τ(p; x_i, y_i) = 1   if I(p + x_i) &amp;lt; I(p + y_i)
                 0   otherwise

descriptor d(p) = the n bits τ(p; x_1, y_1) ... τ(p; x_n, y_n),   n = 256 in ORB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the whole descriptor: 256 brightness comparisons, packed into 32 bytes. The distance between two such descriptors is the &lt;strong&gt;Hamming distance&lt;/strong&gt;, the number of bit positions where they differ:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hamming(d_a, d_b) = popcount( d_a XOR d_b )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On a modern CPU that is a few XOR and population-count instructions over four 64-bit words. Binary descriptors are cheap to compute, store and compare, and comparison is what dominates matching. My pre-talk notes put it as: every point in image A is compared with all the points in image B, that is heavy, so we use binary descriptors. They also index well: the ORB paper uses multi-probe locality-sensitive hashing, and DBoW2 (below) builds its vocabulary directly on binary descriptors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rotated BRIEF: steering the tests, then relearning them
&lt;/h2&gt;

&lt;p&gt;Plain BRIEF is not rotation invariant, because the pixel pairs are fixed relative to the image axes. The obvious fix is to rotate the pair coordinates by the keypoint's orientation θ before sampling. ORB calls this &lt;strong&gt;steered BRIEF&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;S   = [ x_1 ... x_n ; y_1 ... y_n ]      (2 × n matrix of test locations)
S_θ = R_θ · S                              (rotate every test location by θ)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To keep it fast, ORB discretises θ in steps of 12 degrees (30 steps per turn) and precomputes a lookup table of rotated patterns, so steering costs one table lookup per keypoint.&lt;/p&gt;

&lt;p&gt;The interesting part of the paper is what goes wrong next. A good binary test has high variance (its mean over many keypoints is near 0.5, so the bit carries information) and is uncorrelated with the other tests. BRIEF's random pattern has both properties on unoriented patches. Once every patch is steered to a canonical orientation, patches start to look alike, the test means drift away from 0.5, and the descriptor loses discriminative power.&lt;/p&gt;

&lt;p&gt;ORB's answer, &lt;strong&gt;rBRIEF&lt;/strong&gt;, is to learn 256 tests that keep high variance and low correlation &lt;em&gt;after&lt;/em&gt; steering. The authors enumerated every test inside a 31 × 31 patch that compares two non-overlapping 5 × 5 sub-windows (205,590 candidates), ran them all on about 300,000 training keypoints, sorted them by how close their mean is to 0.5, and then walked that list greedily, keeping a test only if its correlation with every test already kept stayed below a threshold.&lt;/p&gt;

&lt;p&gt;Image pyramid → FAST-9 corners per level → Harris ranking keep top N → Orientation θ intensity centroid → &lt;strong&gt;rBRIEF 256 learned tests, steered by θ&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — the ORB pipeline. Each stage fixes one gap in the previous one: scale, cornerness, orientation, and then the loss of descriptor variance that steering causes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The paper's headline claim is that ORB matches about as well as SIFT on many tasks while being roughly two orders of magnitude faster. For a SLAM system that extracts and matches features on every frame, that speed gap is what made ORB the default.&lt;/p&gt;

&lt;h2&gt;
  
  
  How ORB compares with SIFT, SURF, KAZE, AKAZE and BRISK
&lt;/h2&gt;

&lt;p&gt;While preparing the talk I read a 2018 comparison by Tareen and Saleem. They ran all six detector-descriptors on scaled (5% to 500%), rotated (0 to 360 degrees) and perspective-warped versions of benchmark images, matched with the nearest-neighbour distance ratio, and fitted transforms with RANSAC. They also ran ORB and BRISK capped at 1,000 features, written ORB(1000) and BRISK(1000), next to the uncapped versions. Their conclusions, paraphrased:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Descriptor&lt;/th&gt;
&lt;th&gt;What the comparison found&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SIFT&lt;/td&gt;
&lt;td&gt;128 floats, gradient histograms&lt;/td&gt;
&lt;td&gt;Most accurate overall; among the most scale invariant; expensive to compute&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SURF&lt;/td&gt;
&lt;td&gt;64 or 128 floats, Haar responses&lt;/td&gt;
&lt;td&gt;Among the most scale invariant; the 128-d version was slowest at matching&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;KAZE&lt;/td&gt;
&lt;td&gt;Floats, nonlinear scale space&lt;/td&gt;
&lt;td&gt;Accurate under rotation; slowest to detect and describe&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AKAZE&lt;/td&gt;
&lt;td&gt;Binary, nonlinear scale space&lt;/td&gt;
&lt;td&gt;Among the most rotation invariant; middle of the pack on speed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BRISK&lt;/td&gt;
&lt;td&gt;Binary&lt;/td&gt;
&lt;td&gt;With SIFT, the most accurate overall; also among the most efficient&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ORB&lt;/td&gt;
&lt;td&gt;Binary, 256 bits&lt;/td&gt;
&lt;td&gt;Cheapest to detect and describe per feature; finds the most features; &lt;em&gt;least&lt;/em&gt; scale invariant&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The line I highlighted in the paper was that ORB(1000) has the least feature-matching cost. Their ranking of total image matching time puts ORB(1000) first but &lt;em&gt;uncapped&lt;/em&gt; ORB near the bottom, and that explains why ORB-based systems cap the number of features. Uncapped ORB finds so many keypoints that matching all of them takes longer than matching SIFT's smaller set, even though each comparison is far cheaper. A fixed budget keeps matching bounded, at some cost in accuracy, which the paper also reports.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; "Least scale invariant" is about repeatability across large scale changes in that benchmark. A SLAM system mostly matches neighbouring frames, where scale changes are small, so ORB's weakness matters less there than in one-shot image registration.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The live demo: ORB matching on two phone photos
&lt;/h2&gt;

&lt;p&gt;For the talk I ran a small ORB demo in a Colab notebook with OpenCV. I took two phone photos of the same corner of the lab (a sensor enclosure with a red breakout board, and a laptop showing the notebook itself) from different positions, and measured how long ORB took per image, how many keypoints it found, and what fraction of keypoints found a match.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;orb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ORB_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# effectively no cap on features&lt;/span&gt;
&lt;span class="n"&gt;bf&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;BFMatcher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NORM_HAMMING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;crossCheck&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;kp1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;des1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;detectAndCompute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;kp2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;des2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;detectAndCompute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;knnMatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;des1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;des2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fraction_matched&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kp1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;NORM_HAMMING&lt;/code&gt; is the XOR-and-popcount distance from above. &lt;code&gt;crossCheck=True&lt;/code&gt; keeps a pair only if each descriptor is the other's nearest neighbour, a cheap first filter against wrong matches. The notebook printed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Average time for ORB features:   0.019579744338989256
Average number of ORB features:  3835.2
Percentage of Matched Keypoints: 0.28125
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, on the images in my folder: about 3,835 keypoints per image in about 20 ms each, and about 28% of the first image's keypoints had a mutual nearest neighbour in the second. On the slide I rounded this to "about 30% matched, in very little time". (Despite the label, the last number is a fraction, not a percentage.)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp7osuhiz0vdnxik5zrqi.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp7osuhiz0vdnxik5zrqi.webp" alt="Two photos of the same lab bench side by side, a sensor enclosure with a red circuit board and a laptop screen, joined by hundreds of coloured lines connecting matched ORB keypoints" width="800" height="628"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 2 — the 500 best cross-checked ORB matches from my talk demo. Roughly parallel lines are consistent matches; the long diagonals that cut across the bundle are wrong ones. (Browser bars blurred.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Looking at the figure now, it shows the limits as much as the strengths. Many lines cut across the bundle at steep angles: keypoints on the enclosure's repeated hole pattern matched to the wrong hole, text matched to different text. Cross-checking does not catch those. A real pipeline adds a ratio test (is the best match clearly better than the second best?) and a geometric check such as RANSAC. ORB-SLAM goes further and almost never matches blindly: it predicts where a map point should appear and searches a small window around that prediction.&lt;/p&gt;

&lt;p&gt;The notebook also printed a "drift" metric computed under an assumed transform between the photos. That transform does not describe how these photos were taken, so I have left the number out.&lt;/p&gt;

&lt;h2&gt;
  
  
  From features to a SLAM system
&lt;/h2&gt;

&lt;p&gt;ORB-SLAM3 (Campos et al., IEEE Transactions on Robotics, 2021) builds on ORB-SLAM, ORB-SLAM2 and the visual-inertial ORB-SLAM-VI from the University of Zaragoza. It is one library for monocular, stereo and RGB-D cameras, with or without an IMU, using pinhole or fisheye lens models.&lt;/p&gt;

&lt;p&gt;The paper's two main contributions are a visual-inertial system that is formulated as maximum-a-posteriori (MAP) estimation all the way through, including IMU initialisation, and a multi-map system built on a place recognition method with better recall. The ORB descriptor is used at every time scale: short-term association (frame to frame), mid-term association (frame to local map), and long-term association (place recognition through DBoW2).&lt;/p&gt;

&lt;p&gt;Camera + IMU → &lt;strong&gt;Tracking every frame&lt;/strong&gt; → Local mapping every keyframe → Loop closing &amp;amp; map merging → Full BA separate thread&lt;/p&gt;

&lt;p&gt;Atlas: active map + non-active maps one DBoW2 keyframe database shared by all&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 3 — the main threads of ORB-SLAM3. Tracking runs per frame; local mapping and place recognition run per keyframe; all of them read from and write to the Atlas.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Tracking
&lt;/h3&gt;

&lt;p&gt;The tracking thread runs on every frame and has to be real-time. It extracts ORB features, predicts the current pose, projects the points of the active map into the image at that predicted pose, searches for matching descriptors in small windows around each projection, and then optimises the pose by minimising the reprojection error of those matches. It also decides whether the current frame should become a &lt;strong&gt;keyframe&lt;/strong&gt;, which is what the other threads work on.&lt;/p&gt;

&lt;p&gt;In visual-inertial mode, the IMU readings between frames give a much better pose prediction, and the optimisation also estimates the body velocity and the IMU biases. To keep it cheap, only the states of the last two frames are optimised and the map points are held fixed.&lt;/p&gt;

&lt;p&gt;Tracking loss is handled in two stages. With fewer than 15 tracked map points the system is "short-term lost": it predicts the pose from the IMU and searches a wider window. If that fails for 5 seconds it is "long-term lost" and starts a new map, which becomes the active map in the Atlas.&lt;/p&gt;

&lt;h3&gt;
  
  
  Local mapping
&lt;/h3&gt;

&lt;p&gt;The local mapping thread takes each new keyframe, triangulates new map points, removes redundant keyframes and points, and refines the neighbourhood with &lt;strong&gt;local bundle adjustment&lt;/strong&gt;, the core optimisation of visual SLAM. Given camera poses &lt;em&gt;T_i&lt;/em&gt;, 3D points &lt;em&gt;X_j&lt;/em&gt;, and the observed pixel &lt;em&gt;x_ij&lt;/em&gt; of point &lt;em&gt;j&lt;/em&gt; in keyframe &lt;em&gt;i&lt;/em&gt;, it adjusts all of them together to minimise reprojection error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;min over {T_i}, {X_j}   Σ_(i,j)  ρ( || x_ij − π( T_i · X_j ) ||²_Σ )

π  : projects a 3D point in the camera frame to pixels (pinhole or fisheye model)
ρ  : the Huber robust cost, so a few bad matches do not dominate
Σ  : the measurement covariance of each observation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Solving this for the whole map at every keyframe would be intractable, so ORB-SLAM3 optimises a sliding window of recent keyframes and their points, with the other keyframes that observe those points (the "covisible" keyframes) held fixed. In visual-inertial mode the problem gains terms for the preintegrated IMU measurements between keyframes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Loop closing and map merging
&lt;/h3&gt;

&lt;p&gt;Every new keyframe is checked against the whole Atlas for a place the system has seen before. If the match is in the active map, it is a &lt;strong&gt;loop closure&lt;/strong&gt;: accumulated drift is corrected by a pose-graph optimisation, and then a full bundle adjustment runs in a separate thread so tracking is not blocked. If the match is in a different, non-active map, the two maps are &lt;strong&gt;merged&lt;/strong&gt; into one, which becomes the active map.&lt;/p&gt;

&lt;p&gt;Merging happens in a "welding window": the two matched keyframes, their covisible neighbours and the points they observe. The old map is aligned to the active one, duplicated points are fused, a bundle adjustment runs on the welding window, and a pose-graph optimisation over the merged map's &lt;em&gt;essential graph&lt;/em&gt; (a sparse subset of the covisibility graph) spreads the correction to the rest of the map.&lt;/p&gt;

&lt;p&gt;The paper reports about 10 ms per keyframe for place recognition and under a second for merging and loop closing with pose-graph optimisation only. A full bundle adjustment after a loop can take a few seconds, which is why it has its own thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Atlas: what happens when tracking fails
&lt;/h2&gt;

&lt;p&gt;Earlier visual SLAM systems kept one map. When tracking was lost (a fast turn, a blank wall) the system could only try to relocalise in that map, and until it succeeded, nothing new was recorded.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Atlas&lt;/strong&gt; is a set of disconnected maps. One is active: tracking localises against it and local mapping grows it. When tracking is lost for long enough, the active map is stored and a fresh one starts, so mapping continues. When place recognition later finds an overlap, the merge step stitches the maps together. All maps share one DBoW2 keyframe database, so one query searches every map, and a map saved from an earlier session is just another non-active map.&lt;/p&gt;

&lt;p&gt;For a vehicle this matters a lot: losing visual tracking on a road is routine, every time the camera faces the sun or a truck fills the frame.&lt;/p&gt;

&lt;h2&gt;
  
  
  Place recognition with DBoW2
&lt;/h2&gt;

&lt;p&gt;Tracking always has a pose prediction to search around. Place recognition does not, so it needs a way to find similar keyframes without geometry. ORB-SLAM uses the &lt;strong&gt;bag of words&lt;/strong&gt; idea from text retrieval, which I described in the talk as treating image features as words:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Offline, cluster a large set of ORB descriptors into a hierarchical vocabulary tree. Each leaf is a "visual word".&lt;/li&gt;
&lt;li&gt;For each keyframe, push every descriptor down the tree to find its word, and represent the keyframe as a weighted histogram of words (tf-idf style weights, so common words count less).&lt;/li&gt;
&lt;li&gt;Keep an inverted index from each word to the keyframes that contain it. A query only scores keyframes that share words with it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;DBoW2 (Gálvez-López and Tardós) implements this for binary descriptors. Raw queries are fast but noisy: the ORB-SLAM3 paper cites precision and recall of roughly 50% to 80% for the top candidate. To avoid false loops that would corrupt the map, DBoW2 adds consistency checks, one of which is temporal: the same place must be recognised in three consecutive keyframes. That pushes precision to 100% but delays recognition by at least three keyframes and lowers recall, and the ORB-SLAM3 authors found that this delay too often left duplicated areas in the Atlas.&lt;/p&gt;

&lt;p&gt;ORB-SLAM3 replaces the temporal check with geometric checks that use what is already in the map:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Query DBoW2 for the three most similar keyframes that are not already covisible with the current one, and build a local window around each: the candidate, its best covisible keyframes and their map points.&lt;/li&gt;
&lt;li&gt;Estimate an aligning transform from 3D-3D matches with RANSAC (Horn's method on three-point samples): Sim(3) while scale is unknown, SE(3) once it is known. Refine it with guided matching in both directions.&lt;/li&gt;
&lt;li&gt;Verify it against keyframes already covisible with the current one in the active map, instead of waiting for new keyframes. In visual-inertial mode, also require small pitch and roll, since gravity fixes those two angles.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The result is higher recall without the delay, which is what the Atlas needs to merge maps soon after they overlap.&lt;/p&gt;

&lt;h2&gt;
  
  
  IMU initialisation as MAP estimation
&lt;/h2&gt;

&lt;p&gt;An IMU provides scale and the gravity direction, which a single camera cannot observe, but only once its biases, gravity direction and velocity are known. Some earlier systems solved the full visual-inertial problem from scratch and took tens of seconds to converge; others used closed-form equations that ignore sensor noise.&lt;/p&gt;

&lt;p&gt;ORB-SLAM3 frames initialisation as three MAP estimation steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Vision only.&lt;/strong&gt; Run pure monocular SLAM for about 2 seconds, inserting keyframes at 4 Hz, and refine with visual bundle adjustment. This gives an accurate trajectory up to an unknown scale.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inertial only.&lt;/strong&gt; Hold that trajectory fixed and estimate only the inertial variables from the IMU readings between keyframes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Visual-inertial.&lt;/strong&gt; Scale and rotate the map using the estimates, then run a joint visual-inertial bundle adjustment.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The inertial-only step is the interesting one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Y_k = { s, R_wg, b, v_0:k }

s      : scale of the vision-only map           (kept positive: s_new = s_old · exp(δs))
R_wg   : rotation giving gravity in the world frame
         (updated with two angles only; rotating about gravity does not change it)
b      : accelerometer and gyroscope biases, assumed constant during initialisation
v_0:k  : body velocity at each keyframe

Y_k* = argmax  p(Y_k) · Π_i p( I_(i-1,i) | s, R_wg, b, v_(i-1), v_i )

     = argmin  || b ||²_(Σ_b)  +  Σ_i || r_I(i-1,i) ||²_(Σ_I)     (negative log, Gaussian errors)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The prior &lt;em&gt;p(Y_k)&lt;/em&gt; says the biases should be near zero, with a covariance encoding how large they can realistically be. That prior makes it MAP rather than maximum likelihood, and keeps the problem well posed when two seconds of motion do not excite every axis.&lt;/p&gt;

&lt;p&gt;The paper reports about 5% scale error after 2 seconds of trajectory on EuRoC, and about 1% after two further visual-inertial bundle adjustments at 5 and 15 seconds. For stereo-inertial setups the scale is fixed to one and removed from the problem, which helps convergence.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the results say
&lt;/h2&gt;

&lt;p&gt;In my copy of the paper I highlighted three findings from the evaluation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Of the four sensor configurations (monocular and stereo, each with and without an IMU), stereo-inertial is clearly the most robust and accurate.&lt;/li&gt;
&lt;li&gt;On the long indoor "magistrale" sequences of TUM-VI, up to 900 m, errors stayed around a metre, with one sequence near 5 m.&lt;/li&gt;
&lt;li&gt;On some long outdoor sequences, the lack of nearby visual features let the inertial parameters (scale and accelerometer bias especially) drift, giving errors of tens of metres.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The abstract also gives an average accuracy of 3.5 cm for stereo-inertial SLAM on the EuRoC drone dataset, and 9 mm for quick hand-held motion in the TUM-VI room sequences. For a two-wheeler the outdoor finding is the relevant one: roads are exactly where most features are far away.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would change in the talk
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Show the failure cases from the demo.&lt;/strong&gt; The crossing lines in Fig. 2 argue for guided matching better than any slide I had.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Say what ORB-SLAM assumes about the world.&lt;/strong&gt; It assumes a static scene. My notes from that week list Det-SLAM (ORB-SLAM3 plus Detectron2 segmentation and depth, to discard points on moving objects) as a possible successor, though its authors note it does not run in real time. Traffic is about as dynamic as a scene gets.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;E. Rublee, V. Rabaud, K. Konolige, G. Bradski. &lt;em&gt;ORB: An efficient alternative to SIFT or SURF&lt;/em&gt;. ICCV 2011. &lt;a href="https://doi.org/10.1109/ICCV.2011.6126544" rel="noopener noreferrer"&gt;doi:10.1109/ICCV.2011.6126544&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;C. Campos, R. Elvira, J. J. Gómez Rodríguez, J. M. M. Montiel, J. D. Tardós. &lt;em&gt;ORB-SLAM3: An Accurate Open-Source Library for Visual, Visual-Inertial and Multimap SLAM&lt;/em&gt;. IEEE Transactions on Robotics 37(6), 2021. &lt;a href="https://arxiv.org/abs/2007.11898" rel="noopener noreferrer"&gt;arXiv:2007.11898&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;S. A. K. Tareen, Z. Saleem. &lt;em&gt;A Comparative Analysis of SIFT, SURF, KAZE, AKAZE, ORB, and BRISK&lt;/em&gt;. International Conference on Computing, Mathematics and Engineering Technologies (iCoMET), 2018.&lt;/li&gt;
&lt;li&gt;E. Rosten, T. Drummond. &lt;em&gt;Machine learning for high-speed corner detection&lt;/em&gt;. ECCV 2006.&lt;/li&gt;
&lt;li&gt;M. Calonder, V. Lepetit, C. Strecha, P. Fua. &lt;em&gt;BRIEF: Binary Robust Independent Elementary Features&lt;/em&gt;. ECCV 2010.&lt;/li&gt;
&lt;li&gt;P. L. Rosin. &lt;em&gt;Measuring corner properties&lt;/em&gt;. Computer Vision and Image Understanding 73(2), 1999.&lt;/li&gt;
&lt;li&gt;D. Gálvez-López, J. D. Tardós. &lt;em&gt;Bags of Binary Words for Fast Place Recognition in Image Sequences&lt;/em&gt;. IEEE Transactions on Robotics 28(5), 2012.&lt;/li&gt;
&lt;li&gt;R. Mur-Artal, J. M. M. Montiel, J. D. Tardós. &lt;em&gt;ORB-SLAM: A Versatile and Accurate Monocular SLAM System&lt;/em&gt;. IEEE Transactions on Robotics 31(5), 2015.&lt;/li&gt;
&lt;li&gt;R. Mur-Artal, J. D. Tardós. &lt;em&gt;ORB-SLAM2: An Open-Source SLAM System for Monocular, Stereo, and RGB-D Cameras&lt;/em&gt;. IEEE Transactions on Robotics 33(5), 2017.&lt;/li&gt;
&lt;li&gt;A. Eslamian, M. R. Ahmadzadeh. &lt;em&gt;Det-SLAM: A semantic visual SLAM for highly dynamic scenes using Detectron2&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Additional Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://arxiv.org/pdf/2007.11898" rel="noopener noreferrer"&gt;Arxiv Paper&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://github.com/UZ-SLAMLab/ORB_SLAM3" rel="noopener noreferrer"&gt;GitHub Repository&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Presentation Slides
&lt;/h2&gt;

&lt;h2&gt;
  
  
  More from Research at NISER &amp;amp; IISER Bhopal
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-internship-retrospective" rel="noopener noreferrer"&gt;NISER Internship Retrospective&lt;/a&gt;: going back to the lab, and what the internship gave me.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-slam-bayes-filter" rel="noopener noreferrer"&gt;SLAM from the Bayes Filter Up&lt;/a&gt;: the probabilistic foundations under every SLAM system, from Bayes' rule to pose graphs.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-ros-dashboard" rel="noopener noreferrer"&gt;A Browser Dashboard for a ROS 2 Sensor Rig&lt;/a&gt;: live camera, point cloud, IMU and GPS previews in a web app.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-iphone-sensor-rig" rel="noopener noreferrer"&gt;Turning an iPhone into a LiDAR, IMU and GPS Sensor&lt;/a&gt;: phone sensors streamed to a server for mapping.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-perception-notes" rel="noopener noreferrer"&gt;Field Notes on Perception for Two-Wheeler Autonomy&lt;/a&gt;: computer vision, point clouds and prediction for autonomous two-wheelers, written for the Swayansaasita project site.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/iiser-bhopal-quantum-workshop" rel="noopener noreferrer"&gt;Hands-on Quantum Programming at IISER Bhopal&lt;/a&gt;: qubits, Bell states and Deutsch–Jozsa worked through from scratch at the QuCIS workshop.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>robotics</category>
      <category>computervision</category>
      <category>slam</category>
      <category>research</category>
    </item>
    <item>
      <title>SLAM from the Bayes Filter Up</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:40 +0000</pubDate>
      <link>https://dev.to/aharshit123456/slam-from-the-bayes-filter-up-1n4o</link>
      <guid>https://dev.to/aharshit123456/slam-from-the-bayes-filter-up-1n4o</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/niser-slam-bayes-filter" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After my ORB-SLAM3 talk at SMLab in May 2024, my next task on Swayansaasita, the lab's autonomous two-wheeler project, was to find the state of the art in SLAM on lidar and IMU data. Reading system papers is much easier once you know &lt;em&gt;why&lt;/em&gt; the problem is set up the way it is: why everything is a probability, why there is a prediction step and a correction step, why some systems are "filters" and others are "graphs". So in June and early July I went back to the foundations and worked through Cyrill Stachniss's &lt;em&gt;Robot Mapping&lt;/em&gt; lecture series, keeping notes in Logseq as I went.&lt;/p&gt;

&lt;p&gt;This post is those notes, cleaned up and corrected. It goes from the definition of the SLAM problem, through Bayes' rule and the Markov assumption, to a derivation of the recursive Bayes filter, then to its implementations (Kalman, extended Kalman, particle filters), occupancy grids, pose graphs, the taxonomy of SLAM approaches, and finally what the lidar-plus-IMU corner of the field looked like when I surveyed it. The coordinate-frame material comes first, because every equation after it silently depends on it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The site renders equations as plain text, so they are written in code blocks using a simple notation: &lt;code&gt;x_t&lt;/code&gt; is x at time t, &lt;code&gt;z_1:t&lt;/code&gt; is the sequence z_1 ... z_t, and &lt;code&gt;p(a | b)&lt;/code&gt; is the probability of a given b.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The SLAM problem, stated as a probability
&lt;/h2&gt;

&lt;p&gt;A robot (or a camera on a motorcycle) moves through an unknown environment. It gives itself controls, or measures its own motion with odometry or an IMU, and it observes the world with sensors. Those are the &lt;strong&gt;inputs&lt;/strong&gt;. The &lt;strong&gt;outputs&lt;/strong&gt; we want are a map of the environment and the path the robot took through it.&lt;/p&gt;

&lt;p&gt;Neither the controls nor the observations are exact. Wheels slip, IMUs drift, lidar returns are noisy, a camera mistakes one corner for another. Probabilistic robotics deals with this by representing uncertainty explicitly: instead of computing &lt;em&gt;the&lt;/em&gt; path and &lt;em&gt;the&lt;/em&gt; map, we compute a distribution over all possible paths and maps given the data. In my notes I wrote it as "basically P(out | in)":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p( x_0:t , m | z_1:t , u_1:t )

x_0:t  : the path, the sequence of robot poses from time 0 to t
m      : the map
z_1:t  : the observations (sensor measurements) up to time t
u_1:t  : the controls (or odometry readings) up to time t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Springer Handbook of Robotics, which the lecture series points to, separates two versions of this problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Full SLAM&lt;/strong&gt; estimates the posterior over the &lt;em&gt;entire&lt;/em&gt; path and the map, &lt;code&gt;p(x_0:t, m | z_1:t, u_1:t)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Online SLAM&lt;/strong&gt; estimates only the &lt;em&gt;current&lt;/em&gt; pose and the map, &lt;code&gt;p(x_t, m | z_1:t, u_1:t)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The difference is only how much of the path you keep. Online SLAM is full SLAM with the past poses integrated out, one at a time, as they become past:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p( x_t, m | z_1:t, u_1:t ) = ∫ ∫ ... ∫ p( x_0:t, m | z_1:t, u_1:t ) dx_0 dx_1 ... dx_t-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That integral is why the two families of SLAM algorithms look so different. Filters solve the online problem: they keep a belief over the present and fold each past state into it. Smoothers (pose-graph and bundle-adjustment methods) solve the full problem: they keep the past around so that new information, like a loop closure, can correct it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coordinate frames come first
&lt;/h2&gt;

&lt;p&gt;Before any probability, there is geometry. A pose is a position and an orientation, and every measurement is taken in some sensor's own frame. A lidar point is in the lidar frame, an IMU acceleration is in the IMU frame, a GPS fix is in a global frame. Nothing can be fused until it is all in one frame.&lt;/p&gt;

&lt;p&gt;The transformations involved are &lt;strong&gt;affine&lt;/strong&gt;: maps that preserve straight lines and parallelism. Translation, rotation, scaling, reflection and shear are all affine. The ones that matter for robots are rotations and translations (rigid motions). A rotation about the x-axis by angle θ is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;R_x(θ) = | 1     0        0     |
         | 0   cos θ   -sin θ   |
         | 0   sin θ    cos θ   |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The awkward part is that rotation is a matrix product but translation is an addition, so chaining several transforms (world to body, body to camera, camera to pixel) becomes a mess of mixed operations. &lt;strong&gt;Homogeneous coordinates&lt;/strong&gt; fix this by adding one extra coordinate &lt;em&gt;w&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2D point (x, y)  →  (x, y, 1)  ≡  (w·x, w·y, w)   for any w ≠ 0

rigid transform in 3D, as one 4 × 4 matrix:

T = | R   t |        R : 3 × 3 rotation
    | 0   1 |        t : 3 × 1 translation  (t_x, t_y, t_z)

p_world = T_world_body · T_body_sensor · p_sensor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A 2D transform becomes a 3 × 3 matrix and a 3D transform a 4 × 4 matrix, and chaining frames is plain matrix multiplication. Homogeneous coordinates also let you write a point at infinity, with &lt;em&gt;w&lt;/em&gt; = 0. My notes have a question I asked myself, "why do we need a point at infinity?", and the robotics answer I wrote down: a sensor only knows the world up to some range, and directions beyond that range behave like points at infinity. In a camera, the vanishing point of parallel road edges is exactly such a point.&lt;/p&gt;

&lt;p&gt;Every sensor on a rig needs its own &lt;code&gt;T_body_sensor&lt;/code&gt;, its &lt;strong&gt;extrinsic calibration&lt;/strong&gt;. If that transform is off by a degree, every measurement from that sensor is wrong in a consistent way, and no filter downstream can tell. That is why calibration came up so often on the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bayes' rule and the Markov assumption
&lt;/h2&gt;

&lt;p&gt;Two tools do almost all of the work in SLAM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bayes' rule&lt;/strong&gt; turns a question we cannot answer directly ("where am I, given this measurement?") into one we can model ("what measurement would I expect, if I were here?"):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p(x | z) = p(z | x) · p(x) / p(z)  =  η · p(z | x) · p(x)

p(z | x)  : likelihood, from a sensor model
p(x)      : prior, what we believed before the measurement
η         : 1 / p(z), a normaliser that makes the result sum to 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The normaliser does not depend on &lt;em&gt;x&lt;/em&gt;, so in practice we compute &lt;code&gt;p(z | x) · p(x)&lt;/code&gt; for every candidate &lt;em&gt;x&lt;/em&gt; and rescale at the end.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Markov assumption&lt;/strong&gt; says the state is a complete summary of the past. Two forms are used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;observation:  p( z_t | x_0:t, z_1:t-1, u_1:t ) = p( z_t | x_t )
motion:       p( x_t | x_0:t-1, z_1:t-1, u_1:t ) = p( x_t | x_t-1, u_t )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first says that if you know where the robot is now, older measurements and controls tell you nothing more about what it will measure now. The second says that if you know the previous pose and the latest control, nothing older helps predict the current pose. Both are approximations (unmodelled dynamics, people walking through the scene and correlated sensor errors all break them), but without them the computation grows with the length of the history.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deriving the recursive Bayes filter
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;belief&lt;/strong&gt; is the posterior over the current state given everything so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bel(x_t) = p( x_t | z_1:t, u_1:t )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The goal is to compute &lt;code&gt;bel(x_t)&lt;/code&gt; from &lt;code&gt;bel(x_t-1)&lt;/code&gt;, the latest control and the latest measurement, without touching the rest of the history. Each line below uses exactly one rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bel(x_t) = p( x_t | z_1:t, u_1:t )

  (Bayes' rule on z_t)
         = η · p( z_t | x_t, z_1:t-1, u_1:t ) · p( x_t | z_1:t-1, u_1:t )

  (Markov, observation)
         = η · p( z_t | x_t ) · p( x_t | z_1:t-1, u_1:t )

  (total probability over the previous state x_t-1)
         = η · p( z_t | x_t ) · ∫ p( x_t | x_t-1, z_1:t-1, u_1:t ) · p( x_t-1 | z_1:t-1, u_1:t ) dx_t-1

  (Markov, motion)
         = η · p( z_t | x_t ) · ∫ p( x_t | x_t-1, u_t ) · p( x_t-1 | z_1:t-1, u_1:t ) dx_t-1

  (u_t is chosen after x_t-1, so it says nothing about x_t-1)
         = η · p( z_t | x_t ) · ∫ p( x_t | x_t-1, u_t ) · p( x_t-1 | z_1:t-1, u_1:t-1 ) dx_t-1

  (that last factor is the previous belief)
         = η · p( z_t | x_t ) · ∫ p( x_t | x_t-1, u_t ) · bel(x_t-1) dx_t-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last line is the whole filter. It is recursive: the new belief needs only the old belief, one motion model and one observation model.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; My first written version of this derivation had a mistake in the total-probability step: I wrote the joint &lt;code&gt;p(x_t, x_t-1 | ...)&lt;/code&gt; inside the integral where the conditional &lt;code&gt;p(x_t | x_t-1, ...)&lt;/code&gt; belongs. The joint integrated over &lt;code&gt;x_t-1&lt;/code&gt; already gives the left-hand side, so multiplying it by the previous belief counts that belief twice. Writing one rule per line, as above, makes that kind of slip easy to spot.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prediction and correction
&lt;/h2&gt;

&lt;p&gt;The same equation is usually split into two steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;prediction:   bel_bar(x_t) = ∫ p( x_t | x_t-1, u_t ) · bel(x_t-1) dx_t-1
correction:   bel(x_t)     = η · p( z_t | x_t ) · bel_bar(x_t)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prediction&lt;/strong&gt; pushes the old belief through the &lt;strong&gt;motion model&lt;/strong&gt; &lt;code&gt;p(x_t | x_t-1, u_t)&lt;/code&gt;. Because motion is noisy, the belief spreads out: you are less sure where you are after moving. The lectures distinguish odometry-based motion models (for robots with wheel encoders) from velocity-based ones (when there are none). On a motorcycle the IMU plays this role.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Correction&lt;/strong&gt; multiplies the predicted belief by the &lt;strong&gt;observation model&lt;/strong&gt; &lt;code&gt;p(z_t | x_t)&lt;/code&gt;, which scores how well each candidate state explains what the sensor just saw. Because a measurement is information, the belief sharpens.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;bel(x_t-1) → Predict motion model, control u_t → bel_bar(x_t) wider → Correct observation model, z_t → &lt;strong&gt;bel(x_t) sharper&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — one cycle of the recursive Bayes filter. The output becomes the input for the next time step; nothing older is kept.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The Bayes filter is a framework, not an algorithm. It does not say how to represent a distribution over a continuous state, and the integral has no closed form in general. Every practical filter is a choice about that: which models are allowed (linear or not), and which distributions (Gaussian only, or anything).&lt;/p&gt;

&lt;h2&gt;
  
  
  The Kalman filter: linear models, Gaussian noise
&lt;/h2&gt;

&lt;p&gt;If the motion and observation models are linear and all noise is Gaussian, the belief stays Gaussian forever, and the integral becomes matrix algebra. That special case is the &lt;strong&gt;Kalman filter&lt;/strong&gt;. The models are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x_t = A_t · x_t-1 + B_t · u_t + ε_t       ε_t ~ N(0, R_t)   motion noise
z_t = C_t · x_t + δ_t                     δ_t ~ N(0, Q_t)   measurement noise
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A Gaussian belief is just a mean μ and a covariance Σ, and one step of the filter is seven lines (this is the form in Thrun, Burgard and Fox's &lt;em&gt;Probabilistic Robotics&lt;/em&gt;, which the lecture follows):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Kalman_filter(μ_t-1, Σ_t-1, u_t, z_t):

  μ_bar = A_t · μ_t-1 + B_t · u_t                       # predict the mean
  Σ_bar = A_t · Σ_t-1 · A_tᵀ + R_t                      # predict the covariance (grows by R_t)

  K_t   = Σ_bar · C_tᵀ · ( C_t · Σ_bar · C_tᵀ + Q_t )⁻¹  # Kalman gain
  μ_t   = μ_bar + K_t · ( z_t − C_t · μ_bar )           # correct with the innovation
  Σ_t   = ( I − K_t · C_t ) · Σ_bar                     # covariance shrinks

  return μ_t, Σ_t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;Kalman gain&lt;/strong&gt; &lt;em&gt;K&lt;/em&gt; is the interesting line. It weighs how uncertain the prediction is (Σ_bar) against how noisy the measurement is (Q). If the sensor is very precise, &lt;em&gt;K&lt;/em&gt; is large and the estimate jumps toward what the sensor says. If the prediction is already confident, &lt;em&gt;K&lt;/em&gt; is small and the measurement barely moves it. The term &lt;code&gt;z_t − C_t · μ_bar&lt;/code&gt;, the &lt;strong&gt;innovation&lt;/strong&gt;, is the difference between what you measured and what you expected to measure.&lt;/p&gt;

&lt;p&gt;When I wrote the prediction article for the project website, this algorithm was the figure. Writing it out as code here makes the structure clearer: the first two lines are the prediction step and the last three are the correction step of the Bayes filter.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the world is not linear: EKF, UKF and information filters
&lt;/h2&gt;

&lt;p&gt;Robots turn, and turning involves sines and cosines, so real motion and observation models are non-linear: &lt;code&gt;x_t = g(u_t, x_t-1) + ε_t&lt;/code&gt; and &lt;code&gt;z_t = h(x_t) + δ_t&lt;/code&gt;. A Gaussian pushed through a non-linear function is no longer Gaussian. The common fixes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Extended Kalman filter (EKF).&lt;/strong&gt; Linearise &lt;em&gt;g&lt;/em&gt; and &lt;em&gt;h&lt;/em&gt; around the current mean using their Jacobians, &lt;code&gt;G_t&lt;/code&gt; and &lt;code&gt;H_t&lt;/code&gt;, and use those in place of &lt;em&gt;A&lt;/em&gt; and &lt;em&gt;C&lt;/em&gt;. It works well when the functions are close to linear over the spread of the belief, and fails quietly when they are not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EKF SLAM.&lt;/strong&gt; Put the robot pose &lt;em&gt;and&lt;/em&gt; every landmark position into one big state vector, and run an EKF over it. The covariance matrix holds every pose-landmark and landmark-landmark correlation, so it grows with the square of the number of landmarks, and every update touches all of it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unscented Kalman filter (UKF).&lt;/strong&gt; Instead of linearising, pick a small set of "sigma points" that capture the mean and covariance, push each point through the real non-linear function, and fit a new Gaussian to the results. No Jacobians, and usually a better approximation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extended information filter (EIF) and its sparse version (SEIF).&lt;/strong&gt; Represent the Gaussian by the inverse covariance (the information matrix) instead. In SLAM that matrix is close to sparse, and the sparse extended information filter exploits this to keep updates cheap.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Particle filters: when one Gaussian is not enough
&lt;/h2&gt;

&lt;p&gt;A Gaussian has one peak. If the robot could be in one of two corridors that look identical, the true belief has two peaks, and any Kalman-style filter will average them into one wrong answer. A &lt;strong&gt;particle filter&lt;/strong&gt; gives up on a parametric form and represents the belief by a set of samples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for each particle i:
    x_t[i] ~ p( x_t | x_t-1[i], u_t )      # prediction: sample from the motion model
    w_t[i] = p( z_t | x_t[i] )             # correction: weight by the observation model
resample particles in proportion to w_t   # drop unlikely ones, duplicate likely ones
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It handles arbitrary non-linear models and multi-modal beliefs, at the price of needing enough particles to cover the state space, which grows quickly with dimension. For SLAM, the Rao-Blackwellised version gives each particle its own path and its own map, so the map can be estimated cheaply given the path; the grid-mapping work by Grisetti, Stachniss and Burgard, which I cited on the project site, builds on this.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Filter&lt;/th&gt;
&lt;th&gt;Belief&lt;/th&gt;
&lt;th&gt;Models&lt;/th&gt;
&lt;th&gt;Main cost or weakness&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Kalman&lt;/td&gt;
&lt;td&gt;One Gaussian&lt;/td&gt;
&lt;td&gt;Linear only&lt;/td&gt;
&lt;td&gt;Wrong as soon as models bend&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EKF&lt;/td&gt;
&lt;td&gt;One Gaussian&lt;/td&gt;
&lt;td&gt;Linearised with Jacobians&lt;/td&gt;
&lt;td&gt;Linearisation error; EKF SLAM is quadratic in landmarks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UKF&lt;/td&gt;
&lt;td&gt;One Gaussian&lt;/td&gt;
&lt;td&gt;Non-linear, via sigma points&lt;/td&gt;
&lt;td&gt;Still unimodal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Information filter / SEIF&lt;/td&gt;
&lt;td&gt;Gaussian in information form&lt;/td&gt;
&lt;td&gt;Linearised&lt;/td&gt;
&lt;td&gt;Recovering the mean needs extra work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Particle&lt;/td&gt;
&lt;td&gt;Weighted samples&lt;/td&gt;
&lt;td&gt;Anything you can sample&lt;/td&gt;
&lt;td&gt;Particle count grows with state dimension&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Occupancy grid maps
&lt;/h2&gt;

&lt;p&gt;So far the map has been a set of landmarks. Lidar and depth sensors do not produce landmarks; they produce rays that either hit something or pass through free space. An &lt;strong&gt;occupancy grid&lt;/strong&gt; represents the map as a grid of cells, each with a probability of being occupied, and makes two simplifying assumptions: cells are independent of each other, and the world is static.&lt;/p&gt;

&lt;p&gt;With those assumptions, each cell can be updated on its own with a binary Bayes filter. It is done in log-odds, so the update is a sum instead of a product:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;l(m_i) = log( p(m_i) / (1 − p(m_i)) )

l_t(m_i) = l_t-1(m_i) + log( p(m_i | z_t, x_t) / (1 − p(m_i | z_t, x_t)) ) − l_0

p(m_i | z_t, x_t) : the inverse sensor model. For a lidar beam, cells the beam
                    passed through are likely free; the cell where it ended is
                    likely occupied; cells beyond it are unknown.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Log-odds also avoid the numerical problem of probabilities saturating near 0 or 1. Occupancy grid mapping assumes the poses are known, which is exactly the "mapping" half of SLAM. In a full system it is paired with a localisation method, for example a particle per path as above.&lt;/p&gt;

&lt;h2&gt;
  
  
  From filtering to smoothing: pose graphs
&lt;/h2&gt;

&lt;p&gt;A filter forgets. Once a pose is folded into the belief, it can never be revisited, even when a later observation shows that it was wrong. &lt;strong&gt;Graph-based SLAM&lt;/strong&gt; keeps the past around and turns SLAM into an optimisation problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nodes&lt;/strong&gt; are robot poses (and, in some formulations, landmark positions).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edges&lt;/strong&gt; are constraints between nodes, each a relative transform with an uncertainty: from odometry between consecutive poses, from landmark observations, or from matching two lidar scans.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loop closures&lt;/strong&gt; are edges between nodes that are far apart in time but close in space, found when the robot recognises a place it has been before. They are what correct accumulated drift.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;x_0 → x_1 → x_2 → x_3 → &lt;strong&gt;x_4 sees x_0's place again&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;odometry edges: x_i → x_i+1 → &lt;strong&gt;loop-closure edge: x_4 ↔ x_0&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 2 — a pose graph. Odometry edges chain consecutive poses and accumulate drift; one loop-closure edge ties the end of the path back to its start and lets the optimiser spread the correction over every pose in between.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Each edge says "the measured transform between node &lt;em&gt;i&lt;/em&gt; and node &lt;em&gt;j&lt;/em&gt; is &lt;em&gt;z_ij&lt;/em&gt;, with information matrix Ω_ij". The optimiser finds the poses that disagree least with all edges at once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;e_ij(x)  = z_ij  ⊖  ( x_i⁻¹ ⊕ x_j )            # measured vs predicted relative pose
x*       = argmin_x  Σ_(i,j)  e_ij(x)ᵀ · Ω_ij · e_ij(x)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is non-linear least squares, solved iteratively with Gauss-Newton or Levenberg-Marquardt: linearise the errors around the current guess, solve a sparse linear system, update, repeat. The system stays sparse because each edge touches only two nodes, which is what makes graphs with thousands of poses practical.&lt;/p&gt;

&lt;p&gt;The weak point is &lt;strong&gt;data association&lt;/strong&gt;. A wrong loop-closure edge is a confident, wrong constraint, and least squares will bend the whole map to satisfy it. My talk notes put it as: incorrect associations are hard to recover from, so associations should be made probabilistically and conservatively. That is why ORB-SLAM3 runs several geometric checks before accepting a loop.&lt;/p&gt;

&lt;p&gt;Bundle adjustment, which I covered in the ORB-SLAM3 post, is the same idea with camera poses and 3D points as nodes and reprojection errors as edges.&lt;/p&gt;

&lt;h2&gt;
  
  
  A taxonomy of SLAM approaches
&lt;/h2&gt;

&lt;p&gt;The lecture series opens with a list of the axes along which SLAM systems differ. The first seven rows are from my notes on that lecture; the last two are the distinctions this post has been building up to:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Axis&lt;/th&gt;
&lt;th&gt;Options&lt;/th&gt;
&lt;th&gt;What it decides&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Map representation&lt;/td&gt;
&lt;td&gt;Volumetric vs feature-based&lt;/td&gt;
&lt;td&gt;Grids and voxels vs a sparse set of landmarks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Map type&lt;/td&gt;
&lt;td&gt;Topological vs geometric&lt;/td&gt;
&lt;td&gt;A graph of places vs metric coordinates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Correspondence&lt;/td&gt;
&lt;td&gt;Known vs unknown&lt;/td&gt;
&lt;td&gt;Whether data association is given or must be solved&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Environment&lt;/td&gt;
&lt;td&gt;Static vs dynamic&lt;/td&gt;
&lt;td&gt;Whether moving objects are modelled or ignored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Uncertainty&lt;/td&gt;
&lt;td&gt;Small vs large&lt;/td&gt;
&lt;td&gt;Local tracking vs global relocalisation and big loops&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Control&lt;/td&gt;
&lt;td&gt;Active vs passive&lt;/td&gt;
&lt;td&gt;Whether the robot chooses where to go to map better&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Resources&lt;/td&gt;
&lt;td&gt;Anytime and any-space&lt;/td&gt;
&lt;td&gt;Whether the algorithm adapts to the compute and memory available&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Estimation&lt;/td&gt;
&lt;td&gt;Filtering vs smoothing&lt;/td&gt;
&lt;td&gt;Online belief over now vs optimisation over the whole path&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Problem&lt;/td&gt;
&lt;td&gt;Online vs full SLAM&lt;/td&gt;
&lt;td&gt;Current pose only, or the entire trajectory&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The last two rows are related but not identical. Filters solve online SLAM. Smoothing solves full SLAM, but sliding-window smoothers (like ORB-SLAM3's local bundle adjustment) sit in between: they optimise a recent window of the past and marginalise or freeze the rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  What lidar and IMU SLAM looked like in mid-2024
&lt;/h2&gt;

&lt;p&gt;My other task in June was to plan a survey of the state of the art in SLAM using lidar and IMU data, the sensors the project's rig was built around. The plan classified methods two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;By sensor:&lt;/strong&gt; visual SLAM (monocular, stereo, RGB-D), lidar SLAM, and multi-sensor SLAM that combines them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;By method:&lt;/strong&gt; feature-based (detect and match features), direct (use raw sensor data without explicit features) and graph-based (pose-graph optimisation as the back end).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The comparison was to use the standard benchmarks: KITTI for driving, TUM RGB-D for indoor depth cameras, and EuRoC for drones with stereo cameras and an IMU. I also set up a structured review form so each paper's metadata, sensors and approach could be compared statistically rather than from memory.&lt;/p&gt;

&lt;p&gt;One paper from that reading shows where things were heading. LIV-GaussMap (Hong et al., RA-L 2024) fuses lidar, IMU and camera. A lidar-inertial odometry front end, using adaptive voxels that store plane features such as normals and covariances, gives the poses and the scene structure. That structure initialises a map of 3D Gaussians, which are then refined with photometric gradients from the camera so the map can be rendered photorealistically. The probabilistic foundations in this post are all still there: the lidar-inertial part is a state estimator with motion and observation models, and the map refinement is an optimisation over many measurements at once. What changed is the map representation.&lt;/p&gt;

&lt;p&gt;By the time of my July internship report the survey was still at the stage of drafting sub-sections, so I can't point to a finished paper. What it left me with is the habit of reading any new SLAM system by asking three questions: what is the state, what are the motion and observation models, and is it filtering or smoothing?&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;C. Stachniss. &lt;em&gt;Robot Mapping&lt;/em&gt; lecture course (University of Freiburg), including "A Short Introduction to the Bayes Filter and Related Models".&lt;/li&gt;
&lt;li&gt;S. Thrun, W. Burgard, D. Fox. &lt;em&gt;Probabilistic Robotics&lt;/em&gt;. MIT Press, 2005.&lt;/li&gt;
&lt;li&gt;B. Siciliano, O. Khatib (eds.). &lt;em&gt;Springer Handbook of Robotics&lt;/em&gt;, chapter on Simultaneous Localization and Mapping.&lt;/li&gt;
&lt;li&gt;G. Grisetti, C. Stachniss, W. Burgard. &lt;em&gt;Improved Techniques for Grid Mapping With Rao-Blackwellized Particle Filters&lt;/em&gt;. IEEE Transactions on Robotics 23(1), 2007.&lt;/li&gt;
&lt;li&gt;G. Grisetti, R. Kümmerle, C. Stachniss, W. Burgard. &lt;em&gt;A Tutorial on Graph-Based SLAM&lt;/em&gt;. IEEE Intelligent Transportation Systems Magazine, 2010.&lt;/li&gt;
&lt;li&gt;S. Hong, J. He, X. Zheng, C. Zheng, S. Shen. &lt;em&gt;LIV-GaussMap: LiDAR-Inertial-Visual Fusion for Real-time 3D Radiance Field Map Rendering&lt;/em&gt;. IEEE Robotics and Automation Letters, 2024. &lt;a href="https://arxiv.org/abs/2401.14857" rel="noopener noreferrer"&gt;arXiv:2401.14857&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;A. Geiger, P. Lenz, R. Urtasun. &lt;em&gt;Are we ready for Autonomous Driving? The KITTI Vision Benchmark Suite&lt;/em&gt;. CVPR 2012.&lt;/li&gt;
&lt;li&gt;J. Sturm, N. Engelhard, F. Endres, W. Burgard, D. Cremers. &lt;em&gt;A Benchmark for the Evaluation of RGB-D SLAM Systems&lt;/em&gt;. IROS 2012.&lt;/li&gt;
&lt;li&gt;M. Burri et al. &lt;em&gt;The EuRoC micro aerial vehicle datasets&lt;/em&gt;. International Journal of Robotics Research, 2016.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  More from Research at NISER &amp;amp; IISER Bhopal
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-internship-retrospective" rel="noopener noreferrer"&gt;NISER Internship Retrospective&lt;/a&gt;: going back to the lab, and what the internship gave me.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/orb-slam3" rel="noopener noreferrer"&gt;From ORB Features to ORB-SLAM3&lt;/a&gt;: FAST, rotated BRIEF and binary matching, then every thread of ORB-SLAM3.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-ros-dashboard" rel="noopener noreferrer"&gt;A Browser Dashboard for a ROS 2 Sensor Rig&lt;/a&gt;: live camera, point cloud, IMU and GPS previews in a web app.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-iphone-sensor-rig" rel="noopener noreferrer"&gt;Turning an iPhone into a LiDAR, IMU and GPS Sensor&lt;/a&gt;: phone sensors streamed to a server for mapping.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-perception-notes" rel="noopener noreferrer"&gt;Field Notes on Perception for Two-Wheeler Autonomy&lt;/a&gt;: computer vision, point clouds and prediction for autonomous two-wheelers, written for the Swayansaasita project site.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/iiser-bhopal-quantum-workshop" rel="noopener noreferrer"&gt;Hands-on Quantum Programming at IISER Bhopal&lt;/a&gt;: qubits, Bell states and Deutsch–Jozsa worked through from scratch at the QuCIS workshop.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>robotics</category>
      <category>slam</category>
      <category>probability</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>A Browser Dashboard for a ROS 2 Sensor Rig</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:37 +0000</pubDate>
      <link>https://dev.to/aharshit123456/a-browser-dashboard-for-a-ros-2-sensor-rig-5ggj</link>
      <guid>https://dev.to/aharshit123456/a-browser-dashboard-for-a-ros-2-sensor-rig-5ggj</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/niser-ros-dashboard" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;From May to October 2024 I was a research intern at SMLab, NISER, on &lt;strong&gt;Swayansaasita&lt;/strong&gt;, the lab's project to collect a multimodal driving dataset for autonomous two-wheelers. The sensors ran on Jetson boards under ROS 2, and bringing them up, checking every stream and starting a recording all meant SSH sessions and terminal commands. My job was a web page that did those things: start the sensors, show which topics were publishing and how fast, start and stop a recording, show disk space, list past recordings, and preview a stream. This post explains how it works, from rosbridge and compressed images to the two backends I wrote, and which parts were still placeholders when I stopped.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;12 of 13&lt;/strong&gt; commits in the lab's dashboard repo are mine&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;114&lt;/strong&gt; topics in one &lt;code&gt;ros2 topic list&lt;/code&gt; capture from the rig&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;14.7 MB&lt;/strong&gt; one ZED point cloud message, before base64&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2&lt;/strong&gt; backends: Express over SSH, then FastAPI on the rig&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Sources: the lab's dashboard repo (Vue app, Express server, FastAPI server; commits from 12 June to 25 August 2024), my earlier &lt;code&gt;ros-webinterface&lt;/code&gt; repo (29 May), a small Vue prototype (11 June), and my work notes from the internship. Network addresses, usernames and passwords in the code are left out of every excerpt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The rig and what it publishes
&lt;/h2&gt;

&lt;p&gt;I didn't build the rig, so I'll describe it only as far as the code shows. The launch script started drivers for a &lt;strong&gt;ZED stereo camera&lt;/strong&gt;, a &lt;strong&gt;CS20 time-of-flight depth camera&lt;/strong&gt; (the kill endpoint matches its processes as &lt;code&gt;synexens&lt;/code&gt;), a &lt;strong&gt;u-blox GPS receiver&lt;/strong&gt; (matched as &lt;code&gt;ublox&lt;/code&gt;), plus a topic-rate monitor, a disk monitor and rosbridge. Topic names carry the board and camera, such as &lt;code&gt;/Jetson1/camera1/zed_node/imu/data&lt;/code&gt;, and my June notes describe calling the recording service "in both the nodes (jetsons)".&lt;/p&gt;

&lt;p&gt;On 27 May I saved the output of &lt;code&gt;ros2 topic list&lt;/code&gt; and a few &lt;code&gt;ros2 topic echo&lt;/code&gt; dumps into the first repo so I could design against real messages. That one capture had 114 topics, 95 of them from the ZED wrapper:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What it is&lt;/th&gt;
&lt;th&gt;Example topic&lt;/th&gt;
&lt;th&gt;ROS message type&lt;/th&gt;
&lt;th&gt;What the dashboard did with it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Rectified colour image&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/zed/zed_node/left/image_rect_color&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sensor_msgs/Image&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Too big to send raw (see below)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same image, JPEG&lt;/td&gt;
&lt;td&gt;&lt;code&gt;.../image_rect_color/compressed&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sensor_msgs/CompressedImage&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Shown in an &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Point cloud&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/zed/zed_node/point_cloud/cloud_registered&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sensor_msgs/PointCloud2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Decoded, never rendered&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IMU&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/zed/zed_node/imu/data&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sensor_msgs/Imu&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Live D3 line charts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pose, odometry&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;/zed/zed_node/pose&lt;/code&gt;, &lt;code&gt;/zed/zed_node/odom&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;geometry_msgs/PoseStamped&lt;/code&gt;, &lt;code&gt;nav_msgs/Odometry&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Not used&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Depth and IR (depth camera)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;/camera1/depth_raw&lt;/code&gt;, &lt;code&gt;/camera1/ir_raw&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sensor_msgs/Image&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;IR JPEG used as the image preview&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  ROS 2 in a few paragraphs
&lt;/h2&gt;

&lt;p&gt;If you haven't used ROS, here is the vocabulary the rest of this post needs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;node&lt;/strong&gt; is a process that does one job: a camera driver, a GPS driver, a recorder.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;topic&lt;/strong&gt; is a named channel. Nodes &lt;strong&gt;publish&lt;/strong&gt; messages to a topic and other nodes &lt;strong&gt;subscribe&lt;/strong&gt; to it. Publishers don't know who is listening. &lt;code&gt;/zed/zed_node/imu/data&lt;/code&gt; is a topic.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;message&lt;/strong&gt; is a typed struct defined in a &lt;code&gt;.msg&lt;/code&gt; file. &lt;code&gt;sensor_msgs/Imu&lt;/code&gt; has an orientation quaternion, an angular velocity, a linear acceleration and covariance matrices. Every sensor message starts with a &lt;code&gt;header&lt;/code&gt; holding a timestamp and a coordinate frame name.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;service&lt;/strong&gt; is request and response, like a function call between nodes. &lt;code&gt;std_srvs/Trigger&lt;/code&gt; takes no arguments and returns &lt;code&gt;success&lt;/code&gt; (a bool) and &lt;code&gt;message&lt;/code&gt; (a string). The rig's start and stop recording were Trigger services.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;launch file&lt;/strong&gt; starts a set of nodes with their parameters in one command (&lt;code&gt;ros2 launch &amp;lt;package&amp;gt; &amp;lt;file&amp;gt;&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;bag&lt;/strong&gt; is a recording: &lt;code&gt;ros2 bag record&lt;/code&gt; writes every message on chosen topics, with timestamps, to disk, and &lt;code&gt;ros2 bag play&lt;/code&gt; republishes them later as if the sensors were live.&lt;/li&gt;
&lt;li&gt;The ZED camera also has its own recording format, &lt;strong&gt;SVO&lt;/strong&gt;, which stores the stereo video and the camera's sensor data and can be replayed through the ZED SDK. The rig recorded both bags and SVO files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One more ROS tool matters here: &lt;strong&gt;&lt;code&gt;image_transport&lt;/code&gt;&lt;/strong&gt;. A camera driver that uses it publishes each image topic several times: raw, &lt;code&gt;/compressed&lt;/code&gt; (JPEG or PNG), &lt;code&gt;/compressedDepth&lt;/code&gt; (PNG for depth images) and &lt;code&gt;/theora&lt;/code&gt; (a video codec). In my capture, 17 topics each had those three suffixes. So I could choose an encoding without touching the drivers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a browser
&lt;/h2&gt;

&lt;p&gt;My notes list three goals for the interface: show left and right camera, point cloud, GPS and IMU data; keep latency minimal with a live feed; and give control over the rig. A web page fits that: any laptop on the lab network can open it with nothing installed, and buttons replace commands that otherwise get typed from memory in a terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting ROS into a browser: rosbridge
&lt;/h2&gt;

&lt;p&gt;A browser can't join a ROS 2 network directly. &lt;strong&gt;rosbridge&lt;/strong&gt; is a ROS node that runs a WebSocket server (port 9090 by default) and translates between ROS and JSON. The browser sends &lt;code&gt;{"op": "subscribe", "topic": "/imu", "type": "sensor_msgs/msg/Imu"}&lt;/code&gt;; rosbridge subscribes on its behalf and forwards every message as &lt;code&gt;{"op": "publish", "topic": ..., "msg": {...}}&lt;/code&gt;. Service calls go the same way with &lt;code&gt;call_service&lt;/code&gt;. &lt;strong&gt;roslibjs&lt;/strong&gt; is the JavaScript client that wraps that protocol in &lt;code&gt;Ros&lt;/code&gt;, &lt;code&gt;Topic&lt;/code&gt; and &lt;code&gt;Service&lt;/code&gt; objects.&lt;/p&gt;

&lt;p&gt;Sensor drivers on the Jetsons → ROS 2 topics and services → &lt;strong&gt;rosbridge ROS ↔ JSON over WebSocket&lt;/strong&gt; → roslibjs in the browser&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — the data path for everything the dashboard reads. Binary fields such as image bytes arrive as base64 strings inside the JSON.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The detail that shaped every decision later: rosbridge sends &lt;code&gt;uint8[]&lt;/code&gt; fields, which is where images and point clouds keep their bytes, as &lt;strong&gt;base64 text inside JSON&lt;/strong&gt;. Base64 makes data a third bigger, and the browser then has to parse it on the main thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  Version one: one HTML page per sensor (May)
&lt;/h2&gt;

&lt;p&gt;The first repo, &lt;code&gt;ros-webinterface&lt;/code&gt;, is plain HTML. &lt;code&gt;index.html&lt;/code&gt; stacks three iframes in 16:9 boxes, one per sensor page, and each page opens its own rosbridge connection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Images took three attempts.&lt;/strong&gt; My notes from 27 May record them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Raw &lt;code&gt;sensor_msgs/Image&lt;/code&gt;.&lt;/strong&gt; My notes say each message was about 5 MB, and I dropped it. That fits the arithmetic if the camera ran at the same 1280×720 as its point cloud: a 720p frame in 4-byte BGRA is 3.7 MB, and base64 makes it about 4.9 MB, per frame, per viewer. A web worker that swaps BGR to RGBA for a &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; is still in the repo from that attempt.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Theora video&lt;/strong&gt; through ogv.js, a JavaScript video decoder. I couldn't get its decoder object to initialise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;CompressedImage&lt;/code&gt; (JPEG).&lt;/strong&gt; At first I couldn't find a library to decode it. The next day I realised I didn't need one: the browser already decodes JPEG, and the base64 string rosbridge sends is exactly what a &lt;code&gt;data:&lt;/code&gt; URL wants.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;ROSLIB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Topic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ros&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/zed/zed_node/stereo_raw/image_raw_color/compressed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;messageType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"sensor_msgs/msg/CompressedImage"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// message.data is already base64 JPEG bytes&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"imageDisplay"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"data:image/jpeg;base64,"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That became the image preview for the rest of the project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The IMU page&lt;/strong&gt; draws &lt;code&gt;linear_acceleration&lt;/code&gt; with D3, keeping a sliding one-second window. The first version had a copy-paste bug: the third chart, meant for z, plotted y a second time. The later version fixes it and averages samples into 50 ms bins before drawing, so each redraw plots about 20 averaged points for the one-second window instead of every raw sample.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The point cloud page&lt;/strong&gt; set up a Three.js scene with orbit controls and subscribed to a &lt;code&gt;PointCloud2&lt;/code&gt; topic, but its &lt;code&gt;convertPointCloud2ToVertices&lt;/code&gt; function returned an empty array. The parsing was the hard part, and it wasn't written yet.&lt;/p&gt;

&lt;p&gt;The same day I scaffolded an Angular app around those pages and left it there; my July report says I tried React, Angular and Next before choosing Vue with Vite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Latency and size
&lt;/h2&gt;

&lt;p&gt;The table below is the reasoning I wish I had written down at the start. Sizes come from the message dumps I saved on 27 May.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stream&lt;/th&gt;
&lt;th&gt;One message on the wire&lt;/th&gt;
&lt;th&gt;Decision&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Raw colour image&lt;/td&gt;
&lt;td&gt;About 5 MB (my notes)&lt;/td&gt;
&lt;td&gt;Dropped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JPEG &lt;code&gt;CompressedImage&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Far smaller; varies with the scene&lt;/td&gt;
&lt;td&gt;Used: &lt;code&gt;data:&lt;/code&gt; URL, browser decodes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Theora video&lt;/td&gt;
&lt;td&gt;Not measured&lt;/td&gt;
&lt;td&gt;Dropped: decoder wouldn't initialise&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ZED point cloud, 1280×720, 16 bytes per point&lt;/td&gt;
&lt;td&gt;14.7 MB raw, ~19.7 MB as base64&lt;/td&gt;
&lt;td&gt;Decoded only, never drawn&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IMU&lt;/td&gt;
&lt;td&gt;Small: a header, three vectors, three covariance matrices&lt;/td&gt;
&lt;td&gt;Charted, averaged into 50 ms bins&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The point cloud row explains most of the trouble. The ZED wrapper publishes one point per pixel: 921,600 points with four float32 fields each (x, y, z and colour packed into a float), so a single message is 14.7 MB before rosbridge turns it into text. No browser tab keeps up with that at camera rate. The real fixes are upstream: publish a downsampled cloud, lower the wrapper's point cloud rate, or decode in a worker from a binary transport. My notes mark "configure the PointCloud streaming and make it lighter weight" as done in late June, but that change lived in the rig's configuration, not in either repo, so I can't show what it was.&lt;/p&gt;

&lt;p&gt;Two other ideas never reached these repos: a server-side compression step between rosbridge and the browser (2 June), and streaming ZED frames from the ZED SDK directly, outside ROS. For the second, the README logs my changes to the SDK's SVO recording sample on the rig to turn on its streaming mode; my July report marks it "integration pending".&lt;/p&gt;

&lt;h2&gt;
  
  
  The Vue app (June to August)
&lt;/h2&gt;

&lt;p&gt;The dashboard proper is a Vue 3 app built with Vite, in the lab's repo. I made 12 of its 13 commits; the remaining one is the lab account's initial commit. The final screen has a header row of buttons (Start ROS, Reboot System, Kill System, Start Frequency), a table of topics in the middle, and a black footer with the recording controls, disk usage and the current file.&lt;/p&gt;

&lt;p&gt;Browser Vue app → rosbridge WebSocket topics + record services → &lt;strong&gt;ROS 2 on the rig&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Browser Vue app → Local API server :3000 start, kill, reboot, logs → &lt;strong&gt;SSH, later a child process&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 2 — two paths. ROS data and recording go through rosbridge. Anything rosbridge can't do, such as launching ROS itself, goes through a small HTTP server.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The second path exists because of a chicken-and-egg problem: rosbridge is one of the things the launch file starts, so the browser can't use rosbridge to start ROS. Process control needs something outside ROS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Topic health: the frequency table
&lt;/h3&gt;

&lt;p&gt;The rig ran a monitor node that published every topic's rate as one JSON string on &lt;code&gt;/topic_status&lt;/code&gt;. The browser subscribes once and fills a reactive dictionary that the table renders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;frequencyTopic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;ROSLIB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Topic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ros&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'/topic_status'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;messageType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'std_msgs/msg/String'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;frequencyTopic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// { "/topic": hz, ... }&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;topic&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;topicFrequencyDict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;topicFrequencyDict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;frequency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;topicFrequencyDict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;frequency&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JSON inside a &lt;code&gt;std_msgs/String&lt;/code&gt; is a shortcut, but a useful one: a custom message type would have to be defined, built and installed on every machine that touches it, while a string works everywhere. Each row shows the topic, its rate, Active or Inactive, and a Preview/Debug button. This table is the part I'd keep unchanged: one glance tells you whether every sensor is up before you press record.&lt;/p&gt;

&lt;h3&gt;
  
  
  Start, kill and reboot
&lt;/h3&gt;

&lt;p&gt;The first backend was an Express server using the &lt;code&gt;ssh2&lt;/code&gt; library. &lt;strong&gt;Start ROS&lt;/strong&gt; opened an SSH session to the Jetson and ran &lt;code&gt;bash start_ros.sh&lt;/code&gt;. &lt;strong&gt;Kill System&lt;/strong&gt; ran &lt;code&gt;kill_ros.sh&lt;/code&gt;. &lt;strong&gt;Reboot&lt;/strong&gt; ran &lt;code&gt;sudo reboot&lt;/code&gt;. The README warns users to refresh the page after Start ROS, and the code does &lt;code&gt;window.location.reload()&lt;/code&gt; itself. The reason is that the page opens its rosbridge connection when it loads, which is before rosbridge exists, and roslibjs doesn't reconnect on its own.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recording
&lt;/h3&gt;

&lt;p&gt;The 11 June prototype called the ZED wrapper's own SVO service directly, with a fixed filename and an automatic stop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;ROSLIB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ros&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'/Jetson1/camera1/zed_node/start_svo_rec'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;serviceType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'zed_interfaces/srv/StartSvoRec'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;callService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;ROSLIB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ServiceRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;svo_filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'file5.svo'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* call stop_svo_rec */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By late July the rig had &lt;code&gt;/start_recording&lt;/code&gt; and &lt;code&gt;/stop_recording&lt;/code&gt; services of type &lt;code&gt;std_srvs/Trigger&lt;/code&gt;. The recorder node behind them isn't in this repo. It returned the session's path in the &lt;code&gt;message&lt;/code&gt; field, which the dashboard displays as the file name. On success the button turns red and disabled and a one-second timer starts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;callService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;startTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;timerInterval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;elapsedTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;startTime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;fileName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// e.g. ".../CS2303DATA/CS2303-019"&lt;/span&gt;
    &lt;span class="nf"&gt;getFileSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftbxh2vva0ria5wfxn3aw.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftbxh2vva0ria5wfxn3aw.webp" alt="Browser console during a test recording: the start service returns success with a session path ending in CS2303-019, file size shows Static size, elapsed time counts from 1 to 8, then two stop service results both say recording stopped" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 3 — the console during a test recording on 31 July, with the home directory blacked out. The session path comes back in the Trigger response. File size is a placeholder, and the stop service answered twice.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That screenshot is from the day I wrote "seems to be working fine I guess" in my notes, and it shows what wasn't:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;File size was static.&lt;/strong&gt; &lt;code&gt;getFileSize&lt;/code&gt; exists, but its own comment says "TODO Currently of no use". Size was never polled while recording.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The footer didn't update by itself.&lt;/strong&gt; &lt;code&gt;fileName&lt;/code&gt;, &lt;code&gt;fileSize&lt;/code&gt; and &lt;code&gt;elapsedTime&lt;/code&gt; are plain variables, not Vue refs, so the template only shows new values when something else triggers a re-render, such as the next frequency update. The console shows the timer working; the screen didn't reliably show it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stop was glitchy.&lt;/strong&gt; My notes list it as high priority with "sometimes not working", and the screenshot ends with two stop responses for one session. Making the button wait until the file was actually closed needed the recorder to report it, which was server-side.&lt;/li&gt;
&lt;li&gt;My notes also mark "only enable start recording if all topic frequencies are non-zero" as done, but I can't find that check in the committed code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The playback list
&lt;/h3&gt;

&lt;p&gt;Each session was appended to a &lt;code&gt;log.csv&lt;/code&gt; on the rig: session id, date, start time, stop time, duration and file size. The server reads it and returns JSON. (The header in the data really is spelled &lt;code&gt;Sesion-id&lt;/code&gt;, so the Vue code has to spell it that way too.) &lt;strong&gt;Playback Recordings&lt;/strong&gt; shows the rows in a table with a Play button on each.&lt;/p&gt;

&lt;p&gt;Play is where the browser part stops. It opens a &lt;code&gt;GridLayout&lt;/code&gt; component meant to show the session's streams in movable panes; it renders three hard-coded tiles labelled "Item 1" to "Item 3". My notes say I wrote rosbag and SVO playback scripts for the rig, but they aren't in this repo and the browser was never connected to them.&lt;/p&gt;

&lt;h3&gt;
  
  
  The disk monitor
&lt;/h3&gt;

&lt;p&gt;A disk monitor node published &lt;code&gt;df&lt;/code&gt; output as JSON on &lt;code&gt;/disk_space&lt;/code&gt;. &lt;code&gt;df&lt;/code&gt; reports &lt;code&gt;Used&lt;/code&gt; and &lt;code&gt;1K-blocks&lt;/code&gt; in kibibytes, so dividing by 1024² gives GiB, and the footer shows used over total. Two cosmetic bugs survived: before the first message arrives it shows &lt;code&gt;NaN GB&lt;/code&gt;, and the template appends "GB" twice ("... GB / ... GB GB Used"). Disk space matters on a recording rig: sensor sessions are large, and running out mid-ride loses the session.&lt;/p&gt;

&lt;h3&gt;
  
  
  The preview button
&lt;/h3&gt;

&lt;p&gt;This feature changed the most. On 25 June I tried a &lt;strong&gt;batch preview&lt;/strong&gt;: an inline D3 chart or canvas inside every topic tile, fed by IMU samples pushed into a reactive array that every tile watched. The commit is honestly titled "batch preview feature failed to integrate", and it also checked in a Chrome performance trace from that afternoon. The next two days I switched to a &lt;strong&gt;popup per topic&lt;/strong&gt;: the button opens a window and adds an iframe for the right viewer, with a close button underneath.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;deviceType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deviceType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'point'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="n"&gt;iframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"../src/assets/pointcloud3.html"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deviceType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'raw'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="n"&gt;iframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"../src/assets/home.html"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deviceType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'imu'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="n"&gt;iframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"../src/assets/graph.html"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deviceType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'fix'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="n"&gt;iframe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"../src/assets/map.html"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Invalid Topic`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Moving each viewer into its own iframe gave every stream its own connection and its own page lifecycle: closing the iframe ends its subscription, so a forgotten preview can't keep pulling a heavy topic. That was the point of the "linear lifecycle" commit.&lt;/p&gt;

&lt;p&gt;The honest state of the viewers at the end:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The image viewer works&lt;/strong&gt;, but it always subscribes to one hard-coded IR image topic from the depth camera, whichever image topic you clicked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The IMU charts work&lt;/strong&gt;, with a leak: the sample arrays only ever grow, and every new message filters the whole history, so a preview left open slows down over time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The point cloud viewer&lt;/strong&gt; decodes x, y and z from the base64 buffer (16-byte stride, three float32 offsets) and logs them to the console. It draws nothing. My July report says a Three.js version worked; the committed file isn't that version.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The GPS map&lt;/strong&gt; is a stub page with a heading. It reads &lt;code&gt;topic&lt;/code&gt; and &lt;code&gt;type&lt;/code&gt; from its URL query, which shows the plan: pass the clicked topic into the viewer. The line in the app that would do that is commented out.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  FireAPI: moving the backend onto the rig
&lt;/h2&gt;

&lt;p&gt;In August I wrote a second backend, &lt;code&gt;API_server.py&lt;/code&gt;. The commit calls it "FireAPI"; it is a &lt;strong&gt;FastAPI&lt;/strong&gt; app with the same endpoints as the Express server (&lt;code&gt;/start-ros&lt;/code&gt;, &lt;code&gt;/kill-ros&lt;/code&gt;, &lt;code&gt;/reboot-system&lt;/code&gt;, &lt;code&gt;/log-file&lt;/code&gt;, &lt;code&gt;/get-file-size&lt;/code&gt;) plus &lt;code&gt;/stop-ros&lt;/code&gt;, &lt;code&gt;/ros-status&lt;/code&gt; and a WebSocket at &lt;code&gt;/ws/logs&lt;/code&gt;. Because the paths match, the Vue app didn't need to change. The main difference is where it runs: on the ROS machine itself, starting the launch file as a child process instead of over SSH.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/start-ros"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;start_ros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;ros_process&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"process"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ROS2 launch file is already running"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;process&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_subprocess_exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s1"&gt;'ros2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'launch'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'daemon'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'init.launch.py'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PIPE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PIPE&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;ros_process&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"process"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;process&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Express + ssh2 (June)&lt;/th&gt;
&lt;th&gt;FastAPI (August)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Runs on&lt;/td&gt;
&lt;td&gt;Wherever it's started; reaches the Jetson over SSH&lt;/td&gt;
&lt;td&gt;The ROS machine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Starts ROS&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;bash start_ros.sh&lt;/code&gt; over SSH&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ros2 launch&lt;/code&gt; as a child process it keeps a handle to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Knows if ROS is running&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Stores the process; refuses a second start&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Credentials&lt;/td&gt;
&lt;td&gt;Hard-coded SSH password in source&lt;/td&gt;
&lt;td&gt;None needed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shell commands&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;du -sh ${fileName}&lt;/code&gt; built as a string: command injection&lt;/td&gt;
&lt;td&gt;Argument lists, no shell&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Live logs&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;/ws/logs&lt;/code&gt; streams the launch's output&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It was a clear improvement, and re-reading it now I can see four problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;/ros-status&lt;/code&gt; crashes.&lt;/strong&gt; It calls &lt;code&gt;.poll()&lt;/code&gt;, which exists on &lt;code&gt;subprocess.Popen&lt;/code&gt; but not on the asyncio process object, so the endpoint raises an error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;/kill-ros&lt;/code&gt; is very broad.&lt;/strong&gt; It runs &lt;code&gt;ros2 daemon stop&lt;/code&gt;, &lt;code&gt;pkill -f ros2&lt;/code&gt;, kills the driver processes and rosbridge, then, if &lt;code&gt;ps aux&lt;/code&gt; still mentions "ros" anywhere, runs &lt;code&gt;pkill -f ros&lt;/code&gt;, which matches any process whose command line contains those three letters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Undrained pipes can stall ROS.&lt;/strong&gt; stderr is piped and never read, and stdout is read only while the log terminal is open. Once a pipe's buffer fills, the launch process blocks on its next write.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No authentication, and CORS allows any origin&lt;/strong&gt;, on endpoints that reboot the machine. Acceptable on an isolated lab network, risky anywhere else.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A terminal in the browser
&lt;/h2&gt;

&lt;p&gt;The last feature was the in-browser terminal: an &lt;strong&gt;xterm.js&lt;/strong&gt; terminal, sized with its fit addon, inside a modal. Opening it connects to &lt;code&gt;/ws/logs&lt;/code&gt; and writes whatever arrives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ws://localhost:3000/ws/logs'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;terminal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;Blob&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the server, the WebSocket handler reads the launch process's stdout line by line and forwards each line. So it's a &lt;strong&gt;read-only log viewer&lt;/strong&gt;, not a shell: nothing typed goes back to the machine. For seeing why a driver failed to start, that's the right scope; a real shell on an unauthenticated server would be a remote-access hole.&lt;/p&gt;

&lt;h2&gt;
  
  
  What was unfinished
&lt;/h2&gt;

&lt;p&gt;By the last commit in August, these were the gaps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Playback in the browser&lt;/strong&gt;: the grid is a placeholder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Point cloud and GPS previews&lt;/strong&gt;: decode-only and stub.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Per-topic previews&lt;/strong&gt;: viewers are chosen by substring and subscribe to hard-coded topics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Live file size&lt;/strong&gt; during a recording, and a stop button that waits for the recorder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Start recording gated on every topic being live&lt;/strong&gt;: in my notes, not in the code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reconnection&lt;/strong&gt;: refreshing the page was the reconnect strategy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hard-coded addresses and credentials&lt;/strong&gt; in the Express server and the HTML viewers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I'd do differently
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One rosbridge connection with reconnection and a status banner&lt;/strong&gt;, instead of one per iframe and a page refresh.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Throttle at the source.&lt;/strong&gt; roslibjs can ask rosbridge for a &lt;code&gt;throttle_rate&lt;/code&gt;, and a preview needs a few frames per second, not thirty. For point clouds, a downsampled preview topic on the rig.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pass the topic and message type to every viewer&lt;/strong&gt; and make them generic: one image viewer for any &lt;code&gt;CompressedImage&lt;/code&gt; topic, one chart for any &lt;code&gt;Imu&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Let the recorder own state.&lt;/strong&gt; Publish a recording-status topic (file, size, duration, closed or not) and have the UI only display it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Look at existing tools first.&lt;/strong&gt; My earliest page was built from Foxglove's rosbridge tutorial. Before writing viewers by hand again, I'd check how far an existing robotics visualiser gets, and build only the rig-specific controls.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The lesson that stuck: the hard parts weren't Vue or D3. They were bytes (what one message costs once it becomes JSON) and state (who knows whether ROS is running, or whether a recording really stopped). Both are easier to get right on the robot side than in the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  More from Research at NISER &amp;amp; IISER Bhopal
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-internship-retrospective" rel="noopener noreferrer"&gt;NISER Internship Retrospective&lt;/a&gt;: going back to the lab, and what the internship gave me.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/orb-slam3" rel="noopener noreferrer"&gt;From ORB Features to ORB-SLAM3&lt;/a&gt;: the lab talk on feature extraction, matching and ORB-SLAM3.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-slam-bayes-filter" rel="noopener noreferrer"&gt;SLAM from the Bayes Filter Up&lt;/a&gt;: the probabilistic basics behind SLAM.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-iphone-sensor-rig" rel="noopener noreferrer"&gt;Turning an iPhone into a LiDAR, IMU and GPS Sensor&lt;/a&gt;: streaming phone sensors over TCP, and an ARKit scanner that exports meshes.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-perception-notes" rel="noopener noreferrer"&gt;Field Notes on Perception for Two-Wheeler Autonomy&lt;/a&gt;: computer vision, point clouds and prediction for autonomous two-wheelers, written for the Swayansaasita project site.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/iiser-bhopal-quantum-workshop" rel="noopener noreferrer"&gt;Hands-on Quantum Programming at IISER Bhopal&lt;/a&gt;: qubits, Bell states and Deutsch–Jozsa worked through from scratch at the QuCIS workshop.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ros</category>
      <category>robotics</category>
      <category>vue</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Field Notes on Perception for Two-Wheeler Autonomy</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:33 +0000</pubDate>
      <link>https://dev.to/aharshit123456/field-notes-on-perception-for-two-wheeler-autonomy-48l9</link>
      <guid>https://dev.to/aharshit123456/field-notes-on-perception-for-two-wheeler-autonomy-48l9</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/niser-perception-notes" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.niser.ac.in/~smishra/project/swayansaasita/" rel="noopener noreferrer"&gt;Swayansaasita&lt;/a&gt; is SMLab's research initiative on autonomous vehicles and driver-assistance systems for two-wheelers, with a focus on the conditions of developing countries like India. Its central goal is data: a large multi-modal driving dataset with 360-degree point clouds, several IMUs, GPS and camera images, annotated for machine learning. During my internship in 2024, besides the SLAM reading and the sensor dashboard, I wrote the project website's technical articles: computer vision, computer vision for self-driving motorcycles, prediction, point clouds, hardware-software integration, and ROS. I drafted them in August 2024; the first ones went up on the site at the end of that month, and in September I rewrote those and added the point cloud and ROS articles.&lt;/p&gt;

&lt;p&gt;Those articles were written fast, for a general audience, and some sections were outlines. This post is the version I would write for engineers: the same topics, explained properly, in the order the data actually flows on a vehicle. Sensors produce measurements, ROS moves them around, calibration puts them in one frame, vision and point-cloud processing turn them into structure, and prediction turns structure into a belief about where the vehicle is and where it is going.&lt;/p&gt;

&lt;p&gt;Sensors cameras, point clouds, IMU, GPS → ROS nodes + topics drivers, timestamps → Calibration one frame, one clock → Vision + point clouds features, depth, registration → &lt;strong&gt;Prediction state, map, path&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — the perception chain these notes follow. Each article on the project site covered one box; this post connects them.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hardware and software: why integration is the hard part
&lt;/h2&gt;

&lt;p&gt;My hardware-software article started from a simple framing: hardware is processors, sensors and transducers, and network modules; software is the operating system, drivers and the networking code that ties devices together. The useful point in it is that software must be designed around what the hardware can actually do. On a vehicle that becomes concrete very quickly.&lt;/p&gt;

&lt;p&gt;The rig I worked with had a ZED stereo camera (which carries its own IMU), a CS20 depth camera and GPS, with the data handled by ROS running on NVIDIA Jetson boards. A Jetson is a capable embedded computer, but it is not a workstation. Uncompressed stereo video, a dense point cloud and high-rate IMU data all compete for the same memory bandwidth, disk throughput and network link. Every design decision downstream, from what gets recorded to what gets previewed live, is a decision about that budget. When I built the browser dashboard for the rig, much of the early work went into getting images to the browser at all: raw &lt;code&gt;bgr8&lt;/code&gt; frames had to be drawn onto a canvas by hand, JPEG-compressed frames decoded from base64, and Theora video needed a separate JavaScript decoder.&lt;/p&gt;

&lt;p&gt;The section of that article I would keep is the one on &lt;strong&gt;calibration&lt;/strong&gt;, which I split into two kinds. Here they are properly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type 1: calibrating each sensor on its own (intrinsics)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Camera intrinsics.&lt;/strong&gt; A pinhole camera maps a 3D point in its own frame to a pixel through a focal length, a principal point and lens distortion coefficients. You estimate these by imaging a known pattern (a checkerboard) from many angles. Wrong intrinsics bend straight lines and corrupt every depth estimate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IMU intrinsics.&lt;/strong&gt; Accelerometers and gyroscopes have biases (a non-zero reading at rest), scale errors and noise. Some of this is estimated once on a bench; biases drift with temperature and time, so good estimators keep estimating them online, as ORB-SLAM3 does.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Type 2: aligning sensors with each other (extrinsics and time)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Extrinsics.&lt;/strong&gt; Each sensor needs a rigid transform to a common body frame: where the camera sits relative to the IMU, the lidar relative to the camera. These are the &lt;code&gt;T_body_sensor&lt;/code&gt; matrices from the coordinate-frames section of my &lt;a href="https://www.aharshit123456.space/blog/niser-slam-bayes-filter" rel="noopener noreferrer"&gt;Bayes filter post&lt;/a&gt;. A one-degree error in a lidar-to-camera rotation misplaces a point 30 m away by about half a metre.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time.&lt;/strong&gt; Fusion needs every measurement stamped on the same clock. A camera frame and an IMU sample that are 20 ms apart in reality but stamped together will make the estimator see motion that did not happen. On a vehicle at speed, small timing offsets become position errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the original article I also listed tuning detection and path-planning algorithms under Type 2. That is better called validation than calibration; the point I was reaching for is that data from several sensors is only useful once it is consistent, in space and in time.&lt;/p&gt;

&lt;h2&gt;
  
  
  ROS: the plumbing
&lt;/h2&gt;

&lt;p&gt;My ROS article opened with the problem ROS solves: before it, every robotics project wrote its own hardware drivers, inter-process communication, logging and visualisation. ROS (Robot Operating System) is not an operating system. It is a framework of conventions, libraries and tools for building robot software out of small processes that talk to each other.&lt;/p&gt;

&lt;p&gt;The concepts that matter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nodes&lt;/strong&gt; are processes that do one job each: a camera driver, an IMU driver, a localisation node, a recorder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Topics&lt;/strong&gt; are named, typed channels. A node &lt;em&gt;publishes&lt;/em&gt; messages on a topic and any number of nodes &lt;em&gt;subscribe&lt;/em&gt; to it. Publishers and subscribers do not know about each other, which is what makes the system modular.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Messages&lt;/strong&gt; are typed structures: &lt;code&gt;sensor_msgs/Imu&lt;/code&gt;, &lt;code&gt;sensor_msgs/Image&lt;/code&gt;, &lt;code&gt;sensor_msgs/PointCloud2&lt;/code&gt;, &lt;code&gt;sensor_msgs/NavSatFix&lt;/code&gt;. Every sensor message carries a header with a timestamp and a &lt;code&gt;frame_id&lt;/code&gt;, which is how the calibration and timing above are attached to the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Services&lt;/strong&gt; are request-response calls for things that are not continuous, like "start recording".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Actions&lt;/strong&gt; are for long-running goals with feedback and cancellation, like "drive to this waypoint".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bags&lt;/strong&gt; record selected topics to disk with their timestamps so a drive can be replayed later as if it were live. For a dataset project, bags &lt;em&gt;are&lt;/em&gt; the product: everything else is how you get clean bags.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;camera driver → IMU driver → GPS driver → topics /image, /imu, /fix, /points → &lt;strong&gt;recorder (bag)&lt;/strong&gt; → rosbridge → browser preview&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 2 — publish-subscribe on a sensor rig. Drivers do not know who is listening; the recorder and the live preview subscribe to the same topics independently.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;On Swayansaasita, ROS ran on the Jetsons (while I worked on it, one ran ROS 1 and the other ROS 2). My dashboard connected to it through rosbridge, which exposes topics over a WebSocket, and used roslibjs in the browser. It showed the live frequency of every topic, start and stop buttons that called the recording services, disk usage, and previews of each stream. One small rule turned out to be useful: only allow recording to start when every expected topic is publishing at a non-zero rate, so a dead sensor is caught before a drive rather than after. The dashboard has its own post, &lt;a href="https://www.aharshit123456.space/blog/niser-ros-dashboard" rel="noopener noreferrer"&gt;A Browser Dashboard for a ROS 2 Sensor Rig&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A minimal ROS 2 subscriber in Python shows how little code a node needs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;rclpy&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;rclpy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sensor_msgs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Imu&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ImuListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"imu_listener"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_subscription&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Imu&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/imu/data"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;on_imu&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_imu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Imu&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# header.stamp and header.frame_id say when and in which frame&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;linear_acceleration&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{msg.header.stamp.sec}: a=({a.x:.2f}, {a.y:.2f}, {a.z:.2f})"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;rclpy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;rclpy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;spin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ImuListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(The topic name here is illustrative; each driver chooses its own.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Computer vision: what the cameras are for
&lt;/h2&gt;

&lt;p&gt;My computer vision article defined vision as the ability to acquire, process, analyse and understand information from the electromagnetic spectrum, and computer vision as the same thing for digital images. The goal is to go from pixels to high-level understanding by building up from low-level features, filtering out what is not useful (which also saves compute), and associating what is left.&lt;/p&gt;

&lt;p&gt;The classic high-level tasks are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Classification:&lt;/strong&gt; what is in this image, as a whole?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Detection:&lt;/strong&gt; what objects are in the image, and where (usually as bounding boxes)?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Segmentation:&lt;/strong&gt; which pixels belong to which object or class?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a two-wheeler, all three matter, but they sit on top of something more basic: geometry. A detector says "there is a car"; geometry says how far away it is and how it is moving relative to you. The rest of the article was about that geometric layer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Features and data association
&lt;/h3&gt;

&lt;p&gt;Feature extraction finds keypoints that can be found again and describes them so they can be matched. The article surveyed SIFT, SURF, BRISK and ORB. The short version: SIFT and SURF use floating-point descriptors and are robust but expensive; BRISK and ORB use binary descriptors compared with Hamming distance, and are fast enough to run on every frame. My &lt;a href="https://www.aharshit123456.space/blog/orb-slam3" rel="noopener noreferrer"&gt;ORB-SLAM3 post&lt;/a&gt; covers how ORB works and how these compare in detail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data association&lt;/strong&gt; is deciding which observation corresponds to which thing: this keypoint to that map point, this bounding box to that tracked car, this lidar cluster to that camera detection. It appears in three places on a vehicle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Frame to frame&lt;/strong&gt;, for visual odometry and object tracking. Associating detections across frames turns a series of boxes into tracks with velocities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sensor to sensor&lt;/strong&gt;, for fusion. Projecting lidar points into the camera image (using the extrinsics above) lets a camera detection get a depth, and a lidar cluster get a class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Now to the past&lt;/strong&gt;, for loop closure and relocalisation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Occlusion, sensor noise and changes in appearance all make association ambiguous. That is why probabilistic tools show up here too: Kalman and particle filters for tracking, and graph methods that can tolerate and later reject wrong associations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stereo depth
&lt;/h3&gt;

&lt;p&gt;The rig's main camera was a stereo pair. A stereo camera has two lenses a fixed distance apart (the baseline &lt;em&gt;B&lt;/em&gt;). After rectification, a point appears on the same row in both images, shifted horizontally by a &lt;strong&gt;disparity&lt;/strong&gt; &lt;em&gt;d&lt;/em&gt;. Depth follows from similar triangles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Z = f · B / d

Z : depth of the point (metres)
f : focal length (pixels)
B : baseline between the two lenses (metres)
d : disparity (pixels)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two consequences matter outdoors. Depth error grows with the square of distance, because a one-pixel disparity error is a much bigger fraction of a small disparity. And disparity needs texture to match: a blank wall, a uniformly grey road or a sky gives the matcher nothing. Both are reasons a road vehicle wants lidar as well as stereo.&lt;/p&gt;

&lt;h3&gt;
  
  
  Photogrammetry: geometry from many images
&lt;/h3&gt;

&lt;p&gt;The article walked through the photogrammetry pipeline, which is also, almost step for step, the offline version of visual SLAM:&lt;/p&gt;

&lt;p&gt;Acquire images overlapping views → Calibrate intrinsics + extrinsics → Extract + match features → Triangulate 3D points → &lt;strong&gt;Bundle adjustment refine all poses + points&lt;/strong&gt; → Surface + texture&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 3 — the photogrammetry pipeline. Swap "offline, all images at once" for "online, one frame at a time" and this is visual SLAM.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Triangulation&lt;/strong&gt; finds the 3D point whose projections best match the observed pixels in two or more views: each observation defines a ray from a camera centre, and the point is where the rays (nearly) meet. &lt;strong&gt;Bundle adjustment&lt;/strong&gt; then refines every camera pose and every 3D point together to minimise reprojection error. Dense reconstruction and meshing come after, if you need surfaces rather than points.&lt;/p&gt;

&lt;h3&gt;
  
  
  What makes vision hard on a road
&lt;/h3&gt;

&lt;p&gt;The challenges section listed four, and they hold up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lighting.&lt;/strong&gt; Sun low on the horizon, tunnels, night, headlights, wet reflections. Features that match at noon do not match at dusk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Detection and classification&lt;/strong&gt; in cluttered scenes with occlusion: a scooter half-hidden behind a bus is still a scooter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-time processing&lt;/strong&gt; on embedded hardware, which forces choices like binary descriptors and capped feature counts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Robustness&lt;/strong&gt; to noise, vibration and viewpoint changes, all of which a motorcycle produces more of than a car.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Point clouds
&lt;/h2&gt;

&lt;p&gt;A point cloud is a set of points in some coordinate frame. For a lidar, each point is where one laser return came back from, usually with extra fields: intensity of the return, which laser (ring) fired it, and the time within the sweep. A stereo camera can produce a point cloud too, by turning every pixel with a valid disparity into a 3D point coloured from the image.&lt;/p&gt;

&lt;p&gt;My point cloud article was the one I left closest to an outline on the site, so this section fills it in.&lt;/p&gt;

&lt;h3&gt;
  
  
  How the data is structured
&lt;/h3&gt;

&lt;p&gt;In ROS, a cloud travels as &lt;code&gt;sensor_msgs/PointCloud2&lt;/code&gt;: a flat byte buffer plus a description of its layout.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PointCloud2
  header        stamp + frame_id
  height, width height = 1 for an unordered cloud; rows × columns for an organised one
  fields        e.g. x (float32, offset 0), y (float32, 4), z (float32, 8), intensity (float32, 12)
  point_step    bytes per point
  row_step      bytes per row  (= point_step × width)
  data          the raw bytes
  is_dense      true if there are no invalid (NaN) points
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;strong&gt;organised&lt;/strong&gt; cloud keeps the sensor's grid (each point knows its neighbours by index, like pixels in an image), which makes neighbourhood operations fast. An &lt;strong&gt;unorganised&lt;/strong&gt; cloud is just a list. Parsing this format in JavaScript for the dashboard's point cloud preview was one of the first things I had to work out on the project: you read each point by walking the buffer in &lt;code&gt;point_step&lt;/code&gt; strides and decoding the fields at their offsets. A stereo camera that publishes one point per pixel makes these messages very large, which is its own problem for anything that has to move them over a network.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PCL&lt;/strong&gt;, the Point Cloud Library, is the standard C++ library for this kind of processing: filtering, feature estimation, registration, segmentation and visualisation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Downsampling and denoising
&lt;/h3&gt;

&lt;p&gt;A single lidar sweep can contain a very large number of points, most of them redundant for any given task. &lt;strong&gt;Voxel-grid downsampling&lt;/strong&gt; divides space into cubes of a chosen size and replaces all points in each cube with their centroid. It gives a cloud with roughly uniform density, which also makes later algorithms behave more predictably.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;voxel_downsample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ndarray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;voxel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ndarray&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""points: (N, 3). Returns one centroid per occupied voxel."""&lt;/span&gt;
    &lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;points&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;voxel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;astype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inverse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unique&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_inverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;return_counts&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;inverse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inverse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reshape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inverse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sums&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Denoising&lt;/strong&gt; removes points that are probably not real: returns from dust, rain or the vehicle itself, and stray points at depth discontinuities. The two standard filters are statistical outlier removal (compute each point's mean distance to its &lt;em&gt;k&lt;/em&gt; nearest neighbours, and drop points whose mean distance is far above the global average) and radius outlier removal (drop points with too few neighbours within a radius).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Visualisation&lt;/strong&gt; sounds trivial and is not. A cloud only makes sense to a human when coloured by height, intensity or distance, and viewed with the right frame and scale.&lt;/p&gt;

&lt;h3&gt;
  
  
  Registration with ICP
&lt;/h3&gt;

&lt;p&gt;Registration finds the rigid transform that aligns one cloud with another: two consecutive sweeps (giving lidar odometry), or a sweep and a map (giving localisation). &lt;strong&gt;Iterative Closest Point&lt;/strong&gt; alternates two steps until it converges:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Associate:&lt;/strong&gt; for each point in the source cloud, take the closest point in the target cloud as its correspondence.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Align:&lt;/strong&gt; find the transform &lt;em&gt;T&lt;/em&gt; that minimises the distance between the corresponding pairs, apply it, and repeat.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;E(T) = Σ_(x, y) ∈ K  || x − T · y ||²        K : the set of corresponding pairs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the correspondences are fixed, step 2 has a closed-form answer using the SVD, which is the "known data association" case in the lecture I linked from the article:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;best_rigid_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ndarray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ndarray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""src, dst: (N, 3) corresponding points. Returns R, t with dst ≈ R @ src + t."""&lt;/span&gt;
    &lt;span class="n"&gt;mu_s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mu_d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="no"&gt;W&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dst&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mu_d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; @ &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mu_s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# 3 × 3 cross-covariance&lt;/span&gt;
    &lt;span class="no"&gt;U&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Vt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;linalg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;svd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;W&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="no"&gt;R&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;U&lt;/span&gt; @ &lt;span class="nc"&gt;Vt&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;linalg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;det&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                   &lt;span class="c1"&gt;# guard against a reflection&lt;/span&gt;
        &lt;span class="no"&gt;U&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="no"&gt;R&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;U&lt;/span&gt; @ &lt;span class="nc"&gt;Vt&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mu_d&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt; @ &lt;span class="n"&gt;mu_s&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ICP's weakness is step 1. "Closest point" is only the right correspondence if the clouds are already roughly aligned, so ICP needs a good initial guess, which on a vehicle comes from the IMU or wheel odometry. Practical variants match points to planes rather than points to points, reject pairs that are too far apart, and weight pairs by their reliability.&lt;/p&gt;

&lt;h3&gt;
  
  
  What point clouds are used for
&lt;/h3&gt;

&lt;p&gt;On a vehicle: obstacle detection and free-space estimation, lidar odometry and mapping, localisation against a prior map, and 3D object detection. In a dataset project like this one, annotated clouds are also training data for learned 3D detectors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prediction: from measurements to a belief about the path
&lt;/h2&gt;

&lt;p&gt;My prediction article argued that the primary task of an autonomous vehicle is to estimate a path and a map of its surroundings from its controls and observations. That is the SLAM problem, written as a posterior over the path and the map:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p( x_0:t , m | z_1:t , u_1:t )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I derived the recursive Bayes filter that solves the online version, and the Kalman filter as its linear-Gaussian case, in &lt;a href="https://www.aharshit123456.space/blog/niser-slam-bayes-filter" rel="noopener noreferrer"&gt;SLAM from the Bayes Filter Up&lt;/a&gt;. The figure in the original article was the Kalman filter algorithm; here is what it means for this rig in one paragraph. The IMU drives the prediction step: integrate acceleration and rotation rate to predict the next pose, with uncertainty growing because the IMU is noisy and biased. GPS, visual odometry and lidar registration drive the correction step: each one is a measurement with its own noise model, and the Kalman gain decides how much to trust it against the prediction. Since the motion of a leaning, turning motorcycle is strongly non-linear, a real system uses an extended or unscented variant, or replaces the filter with a smoother.&lt;/p&gt;

&lt;p&gt;That smoother is the &lt;strong&gt;pose graph&lt;/strong&gt;: robot poses (and possibly landmarks) as nodes; odometry, observations and scan matches as edges; loop closures as edges between distant poses that correct accumulated drift; and the whole graph solved as non-linear least squares with Gauss-Newton or Levenberg-Marquardt. The article listed which nodes and edges there are; the Bayes filter post shows the optimisation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sensor fusion
&lt;/h3&gt;

&lt;p&gt;Fusion is what makes a set of mediocre sensors into a good one. Each sensor fails differently:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Sensor&lt;/th&gt;
&lt;th&gt;Good at&lt;/th&gt;
&lt;th&gt;Fails when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Camera&lt;/td&gt;
&lt;td&gt;Texture, colour, classes, lane markings&lt;/td&gt;
&lt;td&gt;Glare, darkness, low texture; no direct depth (mono)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stereo depth&lt;/td&gt;
&lt;td&gt;Dense depth up close&lt;/td&gt;
&lt;td&gt;Error grows with distance squared; texture-less surfaces&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lidar&lt;/td&gt;
&lt;td&gt;Accurate geometry at range, day or night&lt;/td&gt;
&lt;td&gt;Rain and dust returns; sparse at long range; no colour&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IMU&lt;/td&gt;
&lt;td&gt;High-rate motion, no external dependency&lt;/td&gt;
&lt;td&gt;Drifts within seconds when integrated alone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPS&lt;/td&gt;
&lt;td&gt;Global position, no drift&lt;/td&gt;
&lt;td&gt;Urban canyons, trees, tunnels; metre-level noise&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A probabilistic estimator fuses them by giving each one a noise model and letting the math weigh them. The table is also a useful checklist for dataset design: every "fails when" row is a condition the dataset should include on purpose.&lt;/p&gt;

&lt;h3&gt;
  
  
  Learning-based approaches
&lt;/h3&gt;

&lt;p&gt;The article also covered where machine learning comes in. Classical registration (ICP and global methods like Fast Global Registration) uses hand-designed objectives; some newer work instead learns the registration loss itself from data. Learned SLAM systems such as DeepSLAM train networks for tracking and mapping with photometric and geometric consistency losses instead of hand-designed features. And for predicting the future paths of other road users, sequence models (recurrent networks, LSTMs) and convolutional networks trained on large driving datasets are the common tools. All of these depend on large, well-calibrated, well-synchronised datasets, which is the reason the project put data collection first.&lt;/p&gt;

&lt;h3&gt;
  
  
  What makes prediction hard
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic objects.&lt;/strong&gt; Classical SLAM assumes a static world. Traffic is not static, so moving vehicles and people must be detected, tracked and excluded from the map, or modelled explicitly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A low feature pool.&lt;/strong&gt; At night, in rain, on an empty highway or a featureless road, there may not be enough reliable features to localise against. The answer is more sensor types, not a better feature detector.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I would tell myself before writing these
&lt;/h2&gt;

&lt;p&gt;Writing six short articles for a general audience was good practice in knowing which details matter. Rewriting them for engineers, two years later, mostly meant adding the parts I had glossed over: the message format I actually had to parse, the closed-form step inside ICP, why stereo depth degrades with distance, and calibration framed as frames and clocks rather than as "tuning". The project site still has the original articles, linked from the &lt;a href="https://www.niser.ac.in/~smishra/project/swayansaasita/" rel="noopener noreferrer"&gt;Swayansaasita page&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;M. Quigley et al. &lt;em&gt;ROS: an open-source Robot Operating System&lt;/em&gt;. ICRA Workshop on Open Source Software, 2009.&lt;/li&gt;
&lt;li&gt;R. B. Rusu, S. Cousins. &lt;em&gt;3D is here: Point Cloud Library (PCL)&lt;/em&gt;. ICRA 2011.&lt;/li&gt;
&lt;li&gt;P. J. Besl, N. D. McKay. &lt;em&gt;A Method for Registration of 3-D Shapes&lt;/em&gt;. IEEE Transactions on Pattern Analysis and Machine Intelligence 14(2), 1992.&lt;/li&gt;
&lt;li&gt;K. S. Arun, T. S. Huang, S. D. Blostein. &lt;em&gt;Least-Squares Fitting of Two 3-D Point Sets&lt;/em&gt;. IEEE Transactions on Pattern Analysis and Machine Intelligence 9(5), 1987.&lt;/li&gt;
&lt;li&gt;Q.-Y. Zhou, J. Park, V. Koltun. &lt;em&gt;Fast Global Registration&lt;/em&gt;. ECCV 2016.&lt;/li&gt;
&lt;li&gt;C. Stachniss. &lt;em&gt;ICP &amp;amp; Point Cloud Registration, Part 1: Known Data Association &amp;amp; SVD&lt;/em&gt;. Lecture, 2021.&lt;/li&gt;
&lt;li&gt;S. Thrun, W. Burgard, D. Fox. &lt;em&gt;Probabilistic Robotics&lt;/em&gt;. MIT Press, 2005.&lt;/li&gt;
&lt;li&gt;R. Hartley, A. Zisserman. &lt;em&gt;Multiple View Geometry in Computer Vision&lt;/em&gt;. Cambridge University Press, 2nd ed., 2004.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  More from Research at NISER &amp;amp; IISER Bhopal
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-internship-retrospective" rel="noopener noreferrer"&gt;NISER Internship Retrospective&lt;/a&gt;: going back to the lab, and what the internship gave me.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/orb-slam3" rel="noopener noreferrer"&gt;From ORB Features to ORB-SLAM3&lt;/a&gt;: FAST, rotated BRIEF and binary matching, then every thread of ORB-SLAM3.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-slam-bayes-filter" rel="noopener noreferrer"&gt;SLAM from the Bayes Filter Up&lt;/a&gt;: the probabilistic foundations under every SLAM system, from Bayes' rule to pose graphs.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-ros-dashboard" rel="noopener noreferrer"&gt;A Browser Dashboard for a ROS 2 Sensor Rig&lt;/a&gt;: live camera, point cloud, IMU and GPS previews in a web app.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-iphone-sensor-rig" rel="noopener noreferrer"&gt;Turning an iPhone into a LiDAR, IMU and GPS Sensor&lt;/a&gt;: phone sensors streamed to a server for mapping.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/iiser-bhopal-quantum-workshop" rel="noopener noreferrer"&gt;Hands-on Quantum Programming at IISER Bhopal&lt;/a&gt;: qubits, Bell states and Deutsch–Jozsa worked through from scratch at the QuCIS workshop.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>robotics</category>
      <category>computervision</category>
      <category>autonomousvehicles</category>
      <category>ai</category>
    </item>
    <item>
      <title>Turning an iPhone into a LiDAR, IMU and GPS Sensor</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:28 +0000</pubDate>
      <link>https://dev.to/aharshit123456/turning-an-iphone-into-a-lidar-imu-and-gps-sensor-5cfi</link>
      <guid>https://dev.to/aharshit123456/turning-an-iphone-into-a-lidar-imu-and-gps-sensor-5cfi</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/niser-iphone-sensor-rig" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In my first weeks at SMLab, NISER, in May 2024, before the ROS dashboard, I worked on a side track my notes call "iPhone Drone": use a LiDAR-equipped iPhone as the sensor head for a drone, for 3D mesh generation and SLAM. I wrote two iOS apps. &lt;strong&gt;DataSender&lt;/strong&gt; (the &lt;code&gt;SensorRecorder&lt;/code&gt; repo) reads the phone's motion sensors, GPS and LiDAR depth and streams them over TCP to a computer. &lt;strong&gt;LidarModel&lt;/strong&gt; scans a space with ARKit and exports the reconstructed mesh as an OBJ file. This post explains the Apple frameworks involved for readers who have never written iOS code, walks through what each app does, and then audits the streaming code. Re-reading it two years later, it had the right pieces and almost none of the plumbing a SLAM pipeline needs: no message boundaries, no timestamps, and a depth timer that most likely never fired.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;50 Hz&lt;/strong&gt; configured rate for motion and depth&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;3 of 5&lt;/strong&gt; sensor classes in the code wired into the stream&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;0&lt;/strong&gt; timestamps sent with the data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2&lt;/strong&gt; apps, both committed on 21 May 2024&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Sources: my &lt;code&gt;SensorRecorder&lt;/code&gt; and &lt;code&gt;LidarModel&lt;/code&gt; repositories (one commit each, 21 May 2024; the Xcode file headers date the work to 5–13 May) and my internship notes and July report. The receiving server isn't in either repo, so this post doesn't describe it. Code excerpts are trimmed; simplifications are marked.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why use a phone as a sensor rig
&lt;/h2&gt;

&lt;p&gt;A recent Pro iPhone carries, in one calibrated, battery-powered package, most of what a small mapping platform needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;LiDAR scanner&lt;/strong&gt; that produces depth maps,&lt;/li&gt;
&lt;li&gt;an &lt;strong&gt;IMU&lt;/strong&gt; (accelerometer and gyroscope) plus a magnetometer and barometer,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GNSS&lt;/strong&gt; for global position,&lt;/li&gt;
&lt;li&gt;several &lt;strong&gt;cameras&lt;/strong&gt; with factory calibration,&lt;/li&gt;
&lt;li&gt;and a fast processor with &lt;strong&gt;ARKit&lt;/strong&gt;, Apple's tracking framework, which fuses camera and IMU into a 6-DoF pose (visual-inertial odometry) without any work from the developer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a drone payload, that weight-to-capability ratio is hard to match with separate sensors, cables and a computer. The catch is that the phone was designed for apps, not for robotics. You get processed outputs through Apple's APIs, not raw sensor access, and you inherit the phone's limits on heat, background execution and clocks. Most of this post is about those limits.&lt;/p&gt;

&lt;p&gt;My July report described the plan in four phases: learn iOS development; build apps that collect IMU, GPS, LiDAR and camera data; send it over network sockets "with time correlations" to a server for an online SLAM and AliceVision (open-source photogrammetry) pipeline; and build an ARKit app that records a mesh and exports it as OBJ. The two repos are phases two to four.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apple's sensor frameworks, for non-iOS readers
&lt;/h2&gt;

&lt;p&gt;iOS doesn't expose sensors as device files. Each kind of sensor has a framework with a manager object, a permission prompt and a callback that delivers samples.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Framework&lt;/th&gt;
&lt;th&gt;What it gives you&lt;/th&gt;
&lt;th&gt;How the apps use it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Core Motion&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;CMMotionManager&lt;/code&gt;. Raw accelerometer and gyro, or &lt;code&gt;CMDeviceMotion&lt;/code&gt;: Apple's fused attitude, rotation rate, gravity and user acceleration (gravity removed), each with a timestamp in seconds since boot&lt;/td&gt;
&lt;td&gt;Device motion at 50 Hz&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Core Location&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;CLLocationManager&lt;/code&gt; and a delegate callback with &lt;code&gt;CLLocation&lt;/code&gt; objects: coordinate, altitude, accuracy estimates, speed, course and a wall-clock timestamp. Needs a location permission&lt;/td&gt;
&lt;td&gt;Latitude and longitude&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;AVFoundation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The camera stack. An &lt;code&gt;AVCaptureSession&lt;/code&gt; connects an input device (a camera, or since iOS 15.4 the LiDAR depth camera) to outputs that deliver frames or &lt;code&gt;AVDepthData&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Back camera frames; LiDAR depth maps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;ARKit&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ARSession&lt;/code&gt; runs world tracking and delivers an &lt;code&gt;ARFrame&lt;/code&gt; per camera frame: image, camera pose and, if enabled, &lt;code&gt;sceneDepth&lt;/code&gt; (a 256×192 float32 depth map in metres with a confidence map). It can also build a mesh of the environment as &lt;code&gt;ARMeshAnchor&lt;/code&gt;s&lt;/td&gt;
&lt;td&gt;Depth in DataSender (unused); meshing in LidarModel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Network&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;NWConnection&lt;/code&gt;, Apple's modern TCP/UDP API&lt;/td&gt;
&lt;td&gt;The TCP client&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;RealityKit, Model I/O, SceneKit&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Rendering an AR view; mesh assets and file export; a 3D viewer&lt;/td&gt;
&lt;td&gt;LidarModel's view, OBJ export and viewer&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two facts from this table come back later. First, Core Motion and AVFoundation stamp samples against the device's uptime clock, while Core Location stamps fixes with a wall-clock date. Second, a depth map in both AVFoundation and ARKit arrives as a &lt;code&gt;CVPixelBuffer&lt;/code&gt;, a block of memory you must lock before reading on the CPU.&lt;/p&gt;

&lt;h2&gt;
  
  
  DataSender: what it streams
&lt;/h2&gt;

&lt;p&gt;The app has two text fields (IP address and port) and two buttons, Start Streaming and Stop Streaming. Everything else is in four files: one class per sensor in &lt;code&gt;DataSource.swift&lt;/code&gt;, a &lt;code&gt;TCPClient&lt;/code&gt; over &lt;code&gt;NWConnection&lt;/code&gt;, and a &lt;code&gt;DataStreamer&lt;/code&gt; that ties them together.&lt;/p&gt;

&lt;p&gt;Core Location lat, lon as JSON → Core Motion accel + gyro as JSON, 50 Hz → AVFoundation LiDAR float32 depth bytes, 50 Hz timer → &lt;strong&gt;One TCP stream to the server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — DataSender's pipeline. Each stage is started from the previous stage's callback, and everything shares one connection.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Motion
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;MotionSensor&lt;/code&gt; asks Core Motion for fused device motion fifty times a second and turns each sample into JSON:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;motionManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deviceMotionUpdateInterval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;
&lt;span class="n"&gt;motionManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startDeviceMotionUpdates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;motionData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;jsonData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;motionDataToJSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;motionData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;motionData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// motionDataToJSON:&lt;/span&gt;
&lt;span class="s2"&gt;"acceleration"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"x"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;motionData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;userAcceleration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="s2"&gt;"gyroscope"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"x"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;motionData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rotationRate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;userAcceleration&lt;/code&gt; has gravity already removed by Apple's sensor fusion, and &lt;code&gt;rotationRate&lt;/code&gt; is the bias-corrected gyro in radians per second. For SLAM that's a questionable choice: visual-inertial estimators usually want the raw accelerometer including gravity, because gravity is how they find "down". The attitude Core Motion computes is dropped, and so is &lt;code&gt;motionData.timestamp&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Location
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;LocationSensor&lt;/code&gt; requests "when in use" permission and starts location updates. Each fix becomes &lt;code&gt;{"latitude": ..., "longitude": ...}&lt;/code&gt;. Altitude, the accuracy estimates and the fix's own timestamp are all dropped. Without &lt;code&gt;horizontalAccuracy&lt;/code&gt;, a downstream filter can't tell a good fix from a poor one.&lt;/p&gt;

&lt;h3&gt;
  
  
  LiDAR depth
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;LidarSensor&lt;/code&gt; opens an &lt;code&gt;AVCaptureSession&lt;/code&gt; on &lt;code&gt;.builtInLiDARDepthCamera&lt;/code&gt; with an &lt;code&gt;AVCaptureDepthDataOutput&lt;/code&gt;, keeps the latest depth map as bytes, and a 50 Hz timer hands that buffer to the sender. The conversion is where the worst bugs are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;depthDataToBinary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;depthData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AVDepthData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Data&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;depthDataMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;depthData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depthDataMap&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CVPixelBufferGetWidth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;depthDataMap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CVPixelBufferGetHeight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;depthDataMap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;baseAddress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CVPixelBufferGetBaseAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;depthData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depthDataMap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;depthDataArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UnsafeBufferPointer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;baseAddress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assumingMemoryBound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;depthDataBytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;UInt8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;depthValue&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;depthDataArray&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;depthDataBytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contentsOf&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;withUnsafeBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;depthValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;depthDataBytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;depthDataBytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;MemoryLayout&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Float32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four problems, in order of severity:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;It reads four times past the end of its buffer.&lt;/strong&gt; &lt;code&gt;depthDataBytes&lt;/code&gt; is already a byte array, four bytes per float. Passing &lt;code&gt;count: depthDataBytes.count * 4&lt;/code&gt; tells &lt;code&gt;Data&lt;/code&gt; to copy four times as many bytes as exist. The extra three quarters is whatever memory follows the array. It may crash, or it may quietly send garbage after every real depth map.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The pixel buffer is never locked.&lt;/strong&gt; Core Video requires &lt;code&gt;CVPixelBufferLockBaseAddress&lt;/code&gt; before CPU access; without it the base address may be invalid.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It assumes 32-bit floats&lt;/strong&gt; without checking the buffer's pixel format. &lt;code&gt;AVDepthData&lt;/code&gt; can arrive as 16-bit depth or as disparity; &lt;code&gt;converting(toDepthDataType:)&lt;/code&gt; exists to normalise it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It ignores row padding.&lt;/strong&gt; Pixel buffers can have more bytes per row than &lt;code&gt;width × 4&lt;/code&gt;, and reading &lt;code&gt;width × height&lt;/code&gt; contiguous floats then misaligns every row after the first.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The fix for all four is short: lock the buffer, convert to &lt;code&gt;kCVPixelFormatType_DepthFloat32&lt;/code&gt;, copy row by row using &lt;code&gt;CVPixelBufferGetBytesPerRow&lt;/code&gt;, and build &lt;code&gt;Data&lt;/code&gt; from the exact byte count.&lt;/p&gt;

&lt;h3&gt;
  
  
  Camera and ARKit depth: written, not wired
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;DataSource.swift&lt;/code&gt; also has a &lt;code&gt;CameraSensor&lt;/code&gt; (back camera frames as JPEG at quality 1.0) and an &lt;code&gt;ARCameraSensor&lt;/code&gt; (ARKit &lt;code&gt;sceneDepth&lt;/code&gt;). &lt;code&gt;DataStreamer&lt;/code&gt; never uses either: the camera line is commented out and the AR class is never created. Only location, motion and LiDAR depth were wired into the stream, three of the five sensor classes in the code, and the depth path has the timer problem described below. Neither unused class would have worked as written. &lt;code&gt;CameraSensor&lt;/code&gt; creates a new &lt;code&gt;CIContext&lt;/code&gt; for every frame on the main queue, an expensive object Apple recommends reusing. &lt;code&gt;ARCameraSensor&lt;/code&gt; turns on scene reconstruction but never adds &lt;code&gt;.sceneDepth&lt;/code&gt; to the configuration's &lt;code&gt;frameSemantics&lt;/code&gt;, so &lt;code&gt;frame.sceneDepth&lt;/code&gt; is always nil and, once recording, it would print "No scene depth available" on every frame.&lt;/p&gt;

&lt;h2&gt;
  
  
  The send chain
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;DataStreamer.startStreaming&lt;/code&gt; connects, sends the string "Hello Server", and then starts each sensor from inside the previous one's success callback. Simplified:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ipAddress&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hello Server"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;using&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
        &lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;locationSensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startUpdatingLocation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;          &lt;span class="c1"&gt;// every fix:&lt;/span&gt;
            &lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;locationData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
                &lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;motionSensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startMotionUpdates&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;       &lt;span class="c1"&gt;// every 20 ms:&lt;/span&gt;
                    &lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;motionData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
                        &lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lidarSensor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;updateDepthMap&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;    &lt;span class="c1"&gt;// starts a new 50 Hz timer&lt;/span&gt;
                            &lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;depthMapData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                        &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read the comments down the right side. Every location fix restarts motion updates. Every motion sample, fifty a second, calls &lt;code&gt;updateDepthMap&lt;/code&gt;, and &lt;code&gt;updateDepthMap&lt;/code&gt; schedules a &lt;strong&gt;new repeating 50 Hz timer&lt;/strong&gt; each time it's called. Where that timer lands decides how it fails. &lt;code&gt;updateDepthMap&lt;/code&gt; runs inside a send completion, and &lt;code&gt;NWConnection&lt;/code&gt; calls completions on its own dispatch queue. &lt;code&gt;Timer.scheduledTimer&lt;/code&gt; attaches the timer to the current thread's run loop, and GCD worker threads don't run one, so these timers most likely &lt;strong&gt;never fire and depth is never sent&lt;/strong&gt;. Had the call been on the main thread, the timers would pile up instead: about 500 after ten seconds, each resending the latest depth map fifty times a second, with nothing to stop them, since &lt;code&gt;stopStreaming&lt;/code&gt; holds no reference to them. I never confirmed which happened on the device. The intent was one depth stream at 50 Hz; the fix is to start each sensor once, independently, with its own send path.&lt;/p&gt;

&lt;p&gt;Two smaller issues in &lt;code&gt;TCPClient&lt;/code&gt;: &lt;code&gt;connect&lt;/code&gt; calls its completion with no error immediately after &lt;code&gt;start()&lt;/code&gt;, before the TCP handshake, and the check that the connection is ready before sending is commented out. So the code can't tell a wrong IP address from a working one at connect time. And &lt;code&gt;receive&lt;/code&gt; is armed only once, so only the server's first reply is ever read.&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually goes over the wire
&lt;/h2&gt;

&lt;p&gt;Put together, the server receives one TCP byte stream that looks like this:&lt;/p&gt;

&lt;p&gt;"Hello Server" → { "latitude": …, "longitude": … } → { "acceleration": …, "gyroscope": … } → &lt;strong&gt;float32 depth bytes (if the timer fires)&lt;/strong&gt; → more motion JSON …&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 2 — the stream as the server sees it: text, JSON and binary back to back, with no lengths, no type tags and no timestamps.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;TCP delivers bytes, not messages. One &lt;code&gt;send&lt;/code&gt; on the phone can arrive as two reads on the server, and three sends can arrive as one. A protocol on TCP therefore needs &lt;strong&gt;framing&lt;/strong&gt;: some way to know where each message ends. Common answers are a length prefix, or a delimiter that can't appear inside a message. DataSender has neither. The JSON is pretty-printed, so newlines can't serve as delimiters, and the depth bytes can contain any value at all. A server could guess by looking for &lt;code&gt;{&lt;/code&gt;, but a float in a depth map can contain that byte too.&lt;/p&gt;

&lt;p&gt;A minimal framed format is a fixed header before every payload. This is a sketch of what I'd write now, not code from the repo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// [type: UInt8][timestamp: Float64, seconds][length: UInt32][payload: length bytes]&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UInt8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Data&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;withUnsafeBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bitPattern&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bigEndian&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contentsOf&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;withUnsafeBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UInt32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bigEndian&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contentsOf&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server reads 13 bytes, learns the type, time and length, then reads exactly that many bytes. Depth maps go as raw float32 with width and height in a small prefix, and motion goes as eight doubles rather than pretty-printed JSON.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time: the part SLAM needs most
&lt;/h2&gt;

&lt;p&gt;SLAM and sensor fusion depend on knowing when each measurement was taken, to well under the interval between samples. A visual-inertial estimator integrates IMU samples between camera frames, so an IMU sample assigned to the wrong 20 ms window is a wrong motion estimate.&lt;/p&gt;

&lt;p&gt;DataSender sends no timestamps at all. The one attempt is in the location path, and it's a near miss:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeIntervalSince1970&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;jsonDictionary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="nc"&gt;JSONSerialization&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;jsonObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;locationData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;jsonDictionary&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"timestamp"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;jsonData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="nc"&gt;JSONSerialization&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;withJSONObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;jsonDictionary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;locationData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;// sends the copy without the timestamp&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The timestamped &lt;code&gt;jsonData&lt;/code&gt; is built and never used. Even if it had been sent, it would be the time the callback ran, not the time of the fix.&lt;/p&gt;

&lt;p&gt;Doing this properly takes three steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Keep the sensor's own timestamp.&lt;/strong&gt; &lt;code&gt;CMDeviceMotion.timestamp&lt;/code&gt;, the depth callback's &lt;code&gt;timestamp: CMTime&lt;/code&gt; parameter (the code receives it and ignores it), and &lt;code&gt;ARFrame.timestamp&lt;/code&gt; all mark when the sample was taken.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Put them on one clock.&lt;/strong&gt; Core Motion, AVFoundation and ARKit use the device's uptime clock; Core Location uses a wall-clock &lt;code&gt;Date&lt;/code&gt;. Convert everything to uptime, which doesn't jump when the network corrects the wall clock.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Map the phone's clock to the server's.&lt;/strong&gt; The phone sends its uptime &lt;em&gt;t₁&lt;/em&gt;, the server replies with its own time &lt;em&gt;s&lt;/em&gt;, and the phone notes when the reply arrives, &lt;em&gt;t₂&lt;/em&gt;. The offset is about &lt;em&gt;s&lt;/em&gt; − (&lt;em&gt;t₁&lt;/em&gt; + &lt;em&gt;t₂&lt;/em&gt;)/2, with an error of at most half the round trip. Repeat it, keep the estimate from the fastest round trip, and refresh it periodically because the two clocks drift.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There's a simpler alternative that avoids most of this: use &lt;strong&gt;ARKit as the only source&lt;/strong&gt;. Every &lt;code&gt;ARFrame&lt;/code&gt; carries the camera image, the scene depth and the camera's pose under one timestamp, already aligned by Apple. You give up the 50 Hz IMU stream, but ARKit has already fused it into the pose.&lt;/p&gt;

&lt;h2&gt;
  
  
  LidarModel: scanning a space into an OBJ
&lt;/h2&gt;

&lt;p&gt;The second app is closer to finished. It shows a live AR view; you move the phone around a room, then tap Export, type a file name, and it writes the reconstructed mesh to an OBJ file. A list on the left shows saved files, and tapping one opens a 3D viewer.&lt;/p&gt;

&lt;p&gt;ARKit world tracking + LiDAR → ARMeshAnchors local mesh chunks → &lt;strong&gt;World-space MDLMesh per anchor&lt;/strong&gt; → MDLAsset.export Documents/OBJ_FILES/*.obj → SceneKit viewer or share sheet&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 3 — LidarModel's export path.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The session configuration is the core of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ARWorldTrackingConfiguration&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environmentTexturing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;automatic&lt;/span&gt;
&lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sceneReconstruction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;meshWithClassification&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;supportsFrameSemantics&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sceneDepth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;frameSemantics&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sceneDepth&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;arView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;debugOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;showFeaturePoints&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;arView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;debugOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;showSceneUnderstanding&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sceneReconstruction&lt;/code&gt; is the LiDAR feature that matters. ARKit builds a triangle mesh of the surroundings as you move and splits it into chunks, each an &lt;strong&gt;&lt;code&gt;ARMeshAnchor&lt;/code&gt;&lt;/strong&gt; with its own transform and an &lt;code&gt;ARMeshGeometry&lt;/code&gt; (vertex buffer, face index buffer, normals and, with &lt;code&gt;.meshWithClassification&lt;/code&gt;, a label per face such as wall, floor or table). The two debug options draw ARKit's tracked feature points and the mesh itself over the camera feed, so you can see which areas still have holes before exporting.&lt;/p&gt;

&lt;p&gt;Each anchor's vertices are stored relative to the anchor. To export one coherent model, every vertex is multiplied by its anchor's transform into world coordinates, and the anchors become &lt;code&gt;MDLMesh&lt;/code&gt; objects in one Model I/O &lt;code&gt;MDLAsset&lt;/code&gt;. Model I/O then writes the asset in the format the file extension names. The conversion function is &lt;strong&gt;not my code&lt;/strong&gt;: its header credits another project, and it links the Stack Overflow answer it's based on. One thing to know about it: it writes the world-space vertices back into ARKit's own vertex buffer instead of a copy, which is risky with memory ARKit owns.&lt;/p&gt;

&lt;p&gt;The saved-files list reads &lt;code&gt;Documents/OBJ_FILES&lt;/code&gt;, supports swipe-to-delete, and opens a file in a SceneKit view with camera control and a default light. Getting a file off the phone goes through the iOS share sheet.&lt;/p&gt;

&lt;p&gt;What didn't work, from the code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Export works on every other tap.&lt;/strong&gt; The button toggles a Boolean, and the AR view exports when it sees &lt;code&gt;true&lt;/code&gt;. The first tap sets it to true and exports; the second sets it to false and does nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The share sheet shares the wrong file.&lt;/strong&gt; The exported URL is never passed back to the view (that binding is commented out), so the share sheet always gets a default path, &lt;code&gt;file1234.obj&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Every SwiftUI update re-runs the AR session configuration&lt;/strong&gt;, because &lt;code&gt;session.run&lt;/code&gt; sits in &lt;code&gt;updateUIView&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The other ways out were stubs.&lt;/strong&gt; Sending the OBJ over TCP to the computer is commented out, and so is a Google Drive upload, though the Google sign-in pods are installed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The plan in my report, sending meshes "outside of the app's sandbox to the computing system for further post-processing and cloud comparing", got as far as the share sheet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations of a phone as a sensor rig
&lt;/h2&gt;

&lt;p&gt;Beyond my bugs, these are the constraints the platform imposes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Limitation&lt;/th&gt;
&lt;th&gt;Why it matters&lt;/th&gt;
&lt;th&gt;What to do about it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Processed depth, not raw LiDAR returns&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;You get Apple's depth maps and meshes, not range measurements per laser point&lt;/td&gt;
&lt;td&gt;Treat it as an RGB-D camera, not a LiDAR&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Short-range depth&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The scanner is built for rooms and objects, not open roads&lt;/td&gt;
&lt;td&gt;Fine for indoor mapping; outdoors, use it for near-field only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Clocks&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Uptime and wall clocks differ, and the phone's clock drifts from the server's&lt;/td&gt;
&lt;td&gt;Stamp at the source, use uptime, estimate an offset&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Bandwidth&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;One 256×192 float32 depth map is 196,608 bytes; at 30 frames a second that's about 5.9 MB/s before images&lt;/td&gt;
&lt;td&gt;Send raw binary, not JSON; compress depth; or record on the phone and upload later&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Heat&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Camera, LiDAR, ARKit and the radio together load the phone continuously, and phones throttle when hot. DataSender starts the LiDAR session at app launch, not at Start&lt;/td&gt;
&lt;td&gt;Run sensors only while recording; I never measured this&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Background execution&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Neither app declares background modes, so iOS suspends them when the screen locks or the app loses focus&lt;/td&gt;
&lt;td&gt;Keep the app in front with the screen on, or add the needed background modes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What I'd do now
&lt;/h2&gt;

&lt;p&gt;If I rebuilt DataSender today:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;One clock, stamped at the source&lt;/strong&gt;, and a clock-offset exchange at connect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A framed binary protocol&lt;/strong&gt;: type, timestamp and length before every payload.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Independent sensor pipelines&lt;/strong&gt; that each start once, instead of a callback chain that restarts sensors and multiplies timers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ARKit frames as the main stream&lt;/strong&gt; (image, depth and pose share a timestamp), with raw Core Motion only if the estimator really needs 50 Hz or faster IMU.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Record locally, upload later&lt;/strong&gt; for anything bigger than poses. A phone's Wi-Fi link is the weakest part of the chain, and a dropped TCP connection loses data that a file wouldn't.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The phone was a good idea for a drone payload, and ARKit's meshing worked with very little code. The lesson I took from re-reading DataSender is that a sensor stream is only as useful as its metadata. Bytes without boundaries or timestamps can't be fused, however good the sensor that produced them.&lt;/p&gt;

&lt;h2&gt;
  
  
  More from Research at NISER &amp;amp; IISER Bhopal
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-internship-retrospective" rel="noopener noreferrer"&gt;NISER Internship Retrospective&lt;/a&gt;: going back to the lab, and what the internship gave me.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/orb-slam3" rel="noopener noreferrer"&gt;From ORB Features to ORB-SLAM3&lt;/a&gt;: the lab talk on feature extraction, matching and ORB-SLAM3.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-slam-bayes-filter" rel="noopener noreferrer"&gt;SLAM from the Bayes Filter Up&lt;/a&gt;: the probabilistic basics behind SLAM.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-ros-dashboard" rel="noopener noreferrer"&gt;A Browser Dashboard for a ROS 2 Sensor Rig&lt;/a&gt;: a Vue and rosbridge control panel for the lab's recording rig.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-perception-notes" rel="noopener noreferrer"&gt;Field Notes on Perception for Two-Wheeler Autonomy&lt;/a&gt;: computer vision, point clouds and prediction for autonomous two-wheelers, written for the Swayansaasita project site.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/iiser-bhopal-quantum-workshop" rel="noopener noreferrer"&gt;Hands-on Quantum Programming at IISER Bhopal&lt;/a&gt;: qubits, Bell states and Deutsch–Jozsa worked through from scratch at the QuCIS workshop.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>arkit</category>
      <category>robotics</category>
    </item>
    <item>
      <title>Matching Riders in Real Time over WebSockets</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:24 +0000</pubDate>
      <link>https://dev.to/aharshit123456/matching-riders-in-real-time-over-websockets-41cp</link>
      <guid>https://dev.to/aharshit123456/matching-riders-in-real-time-over-websockets-41cp</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/kurukuru-realtime-matching" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A student with a scooter is about to leave the hostel gate. Another student wants to get to the library, which is on the way. The app has a few seconds to notice, propose the pair, let both sides agree, and then show the co-pilot a moving dot as the pilot approaches. Two things must never happen along the way: one pilot promised to two people, and one slow phone freezing the live view for everyone else. This post is about how the Go backend of Kurukuru does live matching, scheduled matching for repeat commutes, and the WebSocket hub that streams a trip, and about the concurrency bugs each of them taught me.&lt;/p&gt;

&lt;p&gt;Kurukuru is a gamified, college-first, peer-to-peer ride-sharing app I built at CosQ. A &lt;strong&gt;pilot&lt;/strong&gt; is already going somewhere with a free seat; a &lt;strong&gt;co-pilot&lt;/strong&gt; needs a ride and pays the pilot in in-app coins. The co-pilot gets on at a &lt;strong&gt;Kuru in&lt;/strong&gt; point and off at a &lt;strong&gt;Kuru out&lt;/strong&gt; point along the pilot's route. The backend is a single Go service on Postgres with PostGIS (&lt;a href="https://www.aharshit123456.space/blog/kurukuru-architecture" rel="noopener noreferrer"&gt;the architecture post&lt;/a&gt; covers the rest of it).&lt;/p&gt;

&lt;h2&gt;
  
  
  Three speeds of "real time"
&lt;/h2&gt;

&lt;p&gt;"Real time" turned out to mean three different things, and each got a different transport:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What&lt;/th&gt;
&lt;th&gt;Latency it needs&lt;/th&gt;
&lt;th&gt;Transport&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The pilot's position during a trip&lt;/td&gt;
&lt;td&gt;a few seconds, both directions&lt;/td&gt;
&lt;td&gt;WebSocket per trip&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"Has anyone matched my ride yet?"&lt;/td&gt;
&lt;td&gt;a couple of seconds, one question&lt;/td&gt;
&lt;td&gt;HTTP polling every 2 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Suggestions for next week's commute&lt;/td&gt;
&lt;td&gt;minutes&lt;/td&gt;
&lt;td&gt;background matcher plus a queue the app reads&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The temptation is to push everything over the socket. I didn't, because a match is a decision that must survive a closed app, a network change or a server restart. A poll against the database always returns the truth; a missed push silently doesn't. So the socket only carries data that is fine to lose: positions superseded three seconds later.&lt;/p&gt;

&lt;h2&gt;
  
  
  The geometry of a good match
&lt;/h2&gt;

&lt;p&gt;Before any concurrency, the matching rule itself. Both riders submit a plan: start, destination and departure time. The question is whether the co-pilot's trip fits &lt;em&gt;along&lt;/em&gt; the pilot's.&lt;/p&gt;

&lt;p&gt;Pilot start → &lt;strong&gt;Kuru in co-pilot's start, projected on the route&lt;/strong&gt; → &lt;strong&gt;Kuru out co-pilot's destination, projected on the route&lt;/strong&gt; → Pilot destination&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — a match. Both of the co-pilot's points are projected onto the pilot's route; the projections become the pickup and drop-off.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The evaluation is a pure function with no database access:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Time.&lt;/strong&gt; The two departure times must be within 20 minutes of each other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distance off route.&lt;/strong&gt; Project the co-pilot's start and destination onto the pilot's route segment. Each must lie within 600 m of it, or the pilot would make a real detour.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Direction.&lt;/strong&gt; The drop-off projection must come &lt;em&gt;after&lt;/em&gt; the pickup along the route. A co-pilot going the opposite way can sit perfectly close to the line and still be a terrible match.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Worth it.&lt;/strong&gt; The shared stretch between pickup and drop-off must be at least 250 m.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Projection uses a local flat approximation around the pilot's start (metres per degree of longitude scaled by the cosine of the latitude), which is plenty accurate at campus distances and far cheaper than spherical geometry. Each candidate also gets pickup and drop-off times from an average campus speed of about 21 km/h, a detour estimate and a fare from the shared distance. The score adds the two off-route distances to ten points per minute of departure gap; lower is better.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nf"&gt;projectOnSegment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pilot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;From&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pilot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;To&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;copilot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;From&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nf"&gt;projectOnSegment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pilot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;From&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pilot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;To&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;copilot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;To&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;maxOffRouteM&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;maxOffRouteM&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Candidate&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// co-pilot is travelling against the pilot's direction&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Candidate&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keeping this pure paid off twice: it is unit-tested without a database, and the scheduled matcher later reused it unchanged.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;strong&gt;The route is a straight line for matching, roads for display.&lt;/strong&gt; Matching treats the pilot's route as the segment from start to destination. The screens show real road paths from a self-hosted OSRM routing server. Running a road route for every candidate pair would put a network call inside the matching loop; on a compact campus the straight line is a good enough filter, and the 600 m tolerance absorbs the difference.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Live matching: one lock, one decision
&lt;/h2&gt;

&lt;p&gt;When a co-pilot opens the matching screen, the app polls &lt;code&gt;GET /api/rides/{id}/match&lt;/code&gt; every two seconds. If no live match exists, the handler tries to make one:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Begin a transaction and take a &lt;strong&gt;transaction-scoped advisory lock&lt;/strong&gt;: &lt;code&gt;SELECT pg_advisory_xact_lock(hashtext('kurukuru-matching'))&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Load up to 200 open plans of the opposite role that are in the same college pool, not blocked by either side, not already in a live match, and never paired with this ride before.&lt;/li&gt;
&lt;li&gt;Evaluate each in Go and keep the best.&lt;/li&gt;
&lt;li&gt;Insert a &lt;code&gt;proposed&lt;/code&gt; match and commit, which releases the lock.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;An advisory lock is a named mutex inside Postgres. It isn't tied to any row, and the &lt;code&gt;xact&lt;/code&gt; variant releases itself when the transaction ends, so a crashed request can never leave it held. Serialising all live matching behind one lock is blunt, and that is the point: without it, two co-pilots searching at the same moment both see the same free pilot, both evaluate it as their best option, and both insert a match. Row locks can't prevent this because the conflict is over the &lt;em&gt;absence&lt;/em&gt; of a match row. A global lock turns "check, then insert" into one atomic step.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;strong&gt;The cost: matching is single-file.&lt;/strong&gt; Every search waits for the one before it. Each search is one indexed query plus some arithmetic over at most 200 rows, so the critical section is milliseconds. On a campus that's fine; on a city I'd shard the lock by area or cohort instead of dropping it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A proposal is not a ride. The pilot must accept and the co-pilot must confirm, in either order. The respond endpoint locks the match row with &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;, records one side's answer, and when both flags are true, creates the trip and marks both plans matched in the same transaction. Each action is allowed only for the right role (a co-pilot can't "accept" on the pilot's behalf), and responding to a match that is already settled just returns its current state, so a double tap is harmless.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg6mzmqhgms8o4yv2w45x.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg6mzmqhgms8o4yv2w45x.webp" alt="Matching screen showing a pilot found, the pilot's level and rating, the fare in coins, and Kuru in and Kuru out times with cancel and confirm buttons" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In the app — a proposed match from the co-pilot's side, rendered from test data: who the pilot is, the fare, and where and when Kuru in and Kuru out happen.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The other path: nearest available pilot
&lt;/h3&gt;

&lt;p&gt;An older flow skips plans: a pilot toggles "available", storing their position as a PostGIS &lt;code&gt;geography&lt;/code&gt; point, and a co-pilot asks for the closest one within a radius.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;user_cohort&lt;/span&gt; &lt;span class="n"&gt;uc&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_available&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;college_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nv"&gt;$5&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nc"&gt;ST_DWithin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ST_MakePoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;geography&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nc"&gt;ST_Distance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ST_MakePoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;geography&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;geography&lt;/code&gt;, distances are in metres on the real Earth, and &lt;code&gt;ST_DWithin&lt;/code&gt; uses a GiST index, so only pilots inside the radius are sorted.&lt;/p&gt;

&lt;h2&gt;
  
  
  The WebSocket hub, and the bug that could crash the server
&lt;/h2&gt;

&lt;p&gt;Once a trip starts, the pilot's phone sends its position, heading and id over a WebSocket every three seconds, and the server fans each message out to everyone subscribed to that trip. The server also pushes system events into the same channel: "trip started", "trip finished", and "pioneer" events when someone uncovers a never-seen part of the map.&lt;/p&gt;

&lt;p&gt;The first hub was the version you write in an afternoon: a map from trip id to a slice of connections behind a read-write mutex, and a &lt;code&gt;Broadcast&lt;/code&gt; that looped over the slice calling &lt;code&gt;conn.WriteMessage&lt;/code&gt;. It worked in testing. It had two serious problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Concurrent writers.&lt;/strong&gt; gorilla/websocket supports exactly one concurrent writer per connection. &lt;code&gt;Broadcast&lt;/code&gt; ran on whichever goroutine called it: the reader goroutine of each subscribed client, plus HTTP handlers pushing system events. Two clients sending at the same instant meant two goroutines writing to the same connection, and the library detects that and panics. An unrecovered panic in a goroutine takes down the whole process, so every trip, every request and every user would go with it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One slow phone stalls everyone.&lt;/strong&gt; &lt;code&gt;Broadcast&lt;/code&gt; held the hub's read lock while writing. A phone that stopped reading (in a tunnel, backgrounded, on a dying network) made its write block, the read lock stayed held, and every join, leave and broadcast for &lt;em&gt;every&lt;/em&gt; trip queued up behind it.&lt;/p&gt;

&lt;p&gt;The fix is the standard pattern for socket servers, and it is worth knowing by heart:&lt;/p&gt;

&lt;p&gt;Any goroutine calls Broadcast → non-blocking send into each client's queue (32) → &lt;strong&gt;one writer goroutine per connection&lt;/strong&gt; → socket 10 s write deadline&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 2 — the hub after the fix. Broadcast never touches a socket; it only queues. A full queue means the client is too slow, and it is disconnected.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;Hub&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Broadcast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tripID&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RLock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;clients&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tripSubscriptions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tripID&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// one copy, shared by every queue&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;clients&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RUnlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// its reader then fails and removes it from the hub&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One writer per connection.&lt;/strong&gt; Each client gets a &lt;code&gt;writePump&lt;/code&gt; goroutine that is the only code allowed to write to its socket. It also sends the keepalive pings, so pings can't race with data either.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bounded queues, never blocking.&lt;/strong&gt; &lt;code&gt;Broadcast&lt;/code&gt; does a non-blocking send into each client's 32-message channel. If the channel is full, that client is closed rather than waited on. The hub lock is held only for in-memory work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Close exactly once, from anywhere.&lt;/strong&gt; Closing is wrapped in &lt;code&gt;sync.Once&lt;/code&gt;, closes a &lt;code&gt;done&lt;/code&gt; channel and the socket, which unblocks both the reader and any write stuck in the writer. The &lt;code&gt;send&lt;/code&gt; channel is deliberately never closed, because a broadcast may still be sending to it; closing it would turn a race into a panic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Liveness and limits.&lt;/strong&gt; Pings every 50 seconds, a 60-second read deadline extended by each pong or message, a 10-second write deadline, and a 64 KB read limit (a GPS message is a few hundred bytes).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No leaks.&lt;/strong&gt; When a trip's last client leaves, its entry is deleted from the map.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tests reproduce both failures under Go's race detector (four clients each sending 200 messages at once, and a subscriber that never reads), and a benchmark showed delivery latency unchanged.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scheduled matching: a queue inside Postgres
&lt;/h2&gt;

&lt;p&gt;Live matching only works while both people are staring at their phones. Commuters want something else: "I go from the hostel to the library at nine, Monday, Wednesday and Friday, for the next four weeks; find me someone." That became scheduled matching, designed in a short spec before any code.&lt;/p&gt;

&lt;p&gt;The data model reuses as much as possible. A &lt;strong&gt;series&lt;/strong&gt; holds the route and repeat pattern; each dated occurrence is an ordinary ride row linked to it. Proposals between series live in their own table, so the live flow is untouched. When both people accept, the backend creates one normal confirmed match and one trip &lt;strong&gt;per shared day&lt;/strong&gt;, so chat, ratings and trip screens work without knowing scheduling exists.&lt;/p&gt;

&lt;p&gt;Two series are compatible when they share at least one calendar day and &lt;strong&gt;every&lt;/strong&gt; shared day passes the same &lt;code&gt;evaluate&lt;/code&gt; function from live matching. "Same day" is computed in India Standard Time with a fixed zone, so the answer doesn't depend on the server's clock settings or on time-zone data being installed in the container.&lt;/p&gt;

&lt;p&gt;Create or edit series + insert job, same transaction → match_jobs table → &lt;strong&gt;matcher goroutine claims with FOR UPDATE SKIP LOCKED&lt;/strong&gt; → up to 5 suggestions per series&lt;/p&gt;

&lt;p&gt;Sweep every 3 min → retire dead series, expire stale suggestions → re-enqueue open series with room in their queue&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 3 — the scheduled matcher. Events produce jobs; a periodic sweep is the safety net for anything an event missed.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  SKIP LOCKED as a job queue
&lt;/h3&gt;

&lt;p&gt;The producer side is trivial: creating a series inserts its rides and a job row in one transaction, so a series can never exist without a job. The consumer is a goroutine started from &lt;code&gt;main&lt;/code&gt; that polls every two seconds and claims the oldest ready job:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;match_jobs&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;claimed_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;series_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;series_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;match_jobs&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;run_after&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimed_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;claimed_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'5 minutes'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;run_after&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="no"&gt;SKIP&lt;/span&gt; &lt;span class="no"&gt;LOCKED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;series_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt; makes a worker skip rows another transaction has locked instead of waiting for them, which is exactly the semantics of a work queue. Several Honbu instances can run the matcher at once and never process the same job twice. A claim older than five minutes is treated as a crashed worker's and becomes claimable again. Re-enqueueing a series clears its claim, and a finished job is deleted only if it is still claimed, so an edit that arrives mid-processing isn't lost.&lt;/p&gt;

&lt;p&gt;Each run fills the series' queue up to five suggestions and also offers itself to the other series' queues. The cap is enforced &lt;strong&gt;inside the insert statement&lt;/strong&gt;, not in Go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;series_matches&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pilot_series_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;copilot_series_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM series_matches WHERE state = 'proposed'
        AND (pilot_series_id = $1 OR copilot_series_id = $1)) &amp;lt; 5
  AND (SELECT count(*)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;series_matches&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'proposed'&lt;/span&gt;
        &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pilot_series_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;copilot_series_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pilot_series_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;copilot_series_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A check in Go followed by an insert is a race between two workers; a guard inside the statement narrows that window to almost nothing. The unique pair constraint also makes rejection permanent: a rejected suggestion keeps its row, so the pair is never suggested again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftpo5s6lrbcdyjzdu9ak7.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftpo5s6lrbcdyjzdu9ak7.webp" alt="Suggestions screen listing two riders who want to share a repeating ride, with the shared dates, coins earned over four rides, and accept and reject buttons" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In the app — a pilot's suggestion queue for a repeating ride, rendered from test data. "They said yes" means the other side has already accepted; one more tap confirms all four days.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Accepting: where the races live
&lt;/h3&gt;

&lt;p&gt;Accepting is the most concurrent operation in the system. Two people might accept different suggestions for the same series at once; the other side might skip a date or cancel while you tap. The accept transaction:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Locks &lt;strong&gt;both series rows, ordered by id&lt;/strong&gt;. Every accept takes these locks in the same global order, so two accepts touching the same pair of series queue up instead of deadlocking.&lt;/li&gt;
&lt;li&gt;Locks and re-reads the suggestion. If it is no longer &lt;code&gt;proposed&lt;/code&gt;, or either series is no longer open, or the riders have blocked each other since, it is marked expired and the caller gets 409.&lt;/li&gt;
&lt;li&gt;Records this side's accept. If the other side hasn't accepted yet, commit and stop.&lt;/li&gt;
&lt;li&gt;If both have accepted, reload the dated rides (some may have been skipped since the suggestion was made) and re-run the pairing. For each shared day, create the confirmed match and trip and flip both rides from open to matched.&lt;/li&gt;
&lt;li&gt;Mark the suggestion confirmed, supersede every other proposal touching either series, and mark both series matched.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Two guards catch the races the locks don't:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A partial unique index&lt;/strong&gt; allows at most one confirmed suggestion per series (&lt;code&gt;... WHERE state = 'confirmed'&lt;/code&gt;). If a concurrent accept confirmed one of these series first, the update fails with a unique violation, which the handler maps to 409 rather than a 500. The database settles the race; the code just reports the verdict.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A row count on the ride update.&lt;/strong&gt; Each day's rides are flipped with &lt;code&gt;WHERE status = 'open'&lt;/code&gt;, and the code requires exactly two rows to change. A concurrent "skip this date" that canceled one of them leaves only one row, and the whole accept rolls back. An earlier version happily overwrote a just-skipped date back to matched.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Failing softly
&lt;/h2&gt;

&lt;p&gt;A background goroutine inside a web server is a liability if it can crash the process. Every unit of matcher work runs inside a small &lt;code&gt;safely&lt;/code&gt; wrapper that recovers panics and logs them, so one malformed series costs one log line, not the server.&lt;/p&gt;

&lt;p&gt;The sweep also turned out to need a garbage collector. A series whose every remaining date had passed (or was within the 30-minute matching cutoff) stayed open forever, showing "nothing ahead" in the app and being re-examined every three minutes. The sweep now retires such series, cancels their leftover dates and drops their queued job, reusing the existing &lt;code&gt;canceled&lt;/code&gt; status so nothing new needed a schema change.&lt;/p&gt;

&lt;p&gt;Routing degrades the same way: a three-second timeout, a 24-hour in-memory cache of up to 2,000 routes, and a straight line when the routing server is down. A routing outage changes how a line is drawn, never whether a ride happens.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd reuse
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Match the transport to the consequence.&lt;/strong&gt; Decisions go through the database and can be polled; disposable streams like GPS go over the socket.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the matching rule a pure function.&lt;/strong&gt; It's easy to test, and the second matcher reused it unchanged.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When the conflict is over a row that doesn't exist yet, take an advisory lock.&lt;/strong&gt; It's the simplest correct answer to "check, then insert".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One writer per WebSocket, bounded queues, drop the slow.&lt;/strong&gt; Never write to a socket from a broadcast loop, and never hold a shared lock across network I/O.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Postgres is a fine job queue.&lt;/strong&gt; &lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt;, a stale-claim timeout and a periodic sweep go a long way before you need a broker.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Let constraints settle races.&lt;/strong&gt; Partial unique indexes and "exactly N rows changed" checks catch what careful locking misses.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Stack in this post: Go (net/http, gorilla/websocket, lib/pq), PostgreSQL with PostGIS, OSRM, Flutter. Figures come from the backend's code, its scheduled-matching spec and plan, and its history, March to November 2026.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  More from Building at CosQ
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-architecture" rel="noopener noreferrer"&gt;Kurukuru: A Ride-Sharing Game on One Go Binary&lt;/a&gt;: the architecture of a college ride-sharing game in one Go monolith.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-fog-of-war" rel="noopener noreferrer"&gt;Fog of War on a Real Map with PostGIS&lt;/a&gt;: turning GPS trails into an explorable, game-like map.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-pixel-map-app" rel="noopener noreferrer"&gt;Turning a Real Map into a Pixel World&lt;/a&gt;: the Kurukuru app's map, 3D world and pixel UI in Flutter.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-quests-and-road-watch" rel="noopener noreferrer"&gt;Quest Books, an In-App Editor and Road Watch&lt;/a&gt;: game content you can edit in the app, and road safety reports.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/cosq-ecosystem" rel="noopener noreferrer"&gt;Everything Else I Built at CosQ&lt;/a&gt;: WHAM! OTT, MemoChan, PreviouslyOn, Gothicco and more.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>go</category>
      <category>websockets</category>
      <category>concurrency</category>
      <category>backend</category>
    </item>
    <item>
      <title>Quest Books, an In-App Editor and Road Watch</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:21 +0000</pubDate>
      <link>https://dev.to/aharshit123456/quest-books-an-in-app-editor-and-road-watch-3n5k</link>
      <guid>https://dev.to/aharshit123456/quest-books-an-in-app-editor-and-road-watch-3n5k</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/kurukuru-quests-and-road-watch" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A game lives on its content, and content is always slightly wrong: a cafe closed last month, a park pin sits in the middle of a lake, a new food stall nobody has added yet. A road safety tool lives on trust, and trust breaks the first time the app claims something that is not true. Both need to change faster than an app store review allows, and both need the phone and the server to agree exactly on the rules. This post covers three features of the Kurukuru app that sit on that line: quest books, an editor for them inside the app, and Road Watch, a way for riders to report road problems.&lt;/p&gt;

&lt;p&gt;Kurukuru is a gamified peer-to-peer ride-sharing app I built at CosQ for college campuses. A &lt;strong&gt;pilot&lt;/strong&gt; who is already going somewhere offers a seat; a &lt;strong&gt;co-pilot&lt;/strong&gt; who needs one pays a few coins. The map of the campus is a pixel-art world with a fog of war that clears as you travel. The app is Flutter; the backend is one Go service on Postgres and PostGIS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quest books: places as collectible stamps
&lt;/h2&gt;

&lt;p&gt;A quest book is a curated list of real places in an area, organised like a passport. Each book has chapters (one per neighbourhood or campus zone) and five sections: food and drink, culture and arts, nature, adventure, and games and play. Every place is a &lt;strong&gt;stamp&lt;/strong&gt;, and you collect it by being there. There is a book for the campus and one for Bengaluru; the Bengaluru book started from a community-curated list of about 9,000 places, credited in the app.&lt;/p&gt;

&lt;p&gt;The interesting design choice is what you see &lt;em&gt;before&lt;/em&gt; you collect something. A hidden place shows only its kind and distance: "a bakery, 140 m". You can ask for directions and the map draws a path to it, but the name stays secret until you arrive. That turns a list into a treasure hunt, and it means the book itself is a reason to go somewhere new.&lt;/p&gt;

&lt;p&gt;GPS fixes batched on the phone → Sync request impossible jumps dropped → &lt;strong&gt;PostGIS: within a place's radius, inside the book's region&lt;/strong&gt; → New stamps slam onto the map&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — stamping. The phone never decides that you collected something; it only reports where it has been.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Stamping rides on the same GPS batches that clear the fog. The phone sends timestamped fixes; the server drops fixes that would need more than about 144 km/h to reach from the previous one, then asks PostGIS which uncollected places those fixes came within range of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;cand&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;MIN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ST_Distance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;geog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;dist&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;quest_places&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
  &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;quest_books&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;active&lt;/span&gt;
  &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="nc"&gt;ST_Covers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;region&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nc"&gt;ST_DWithin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;geog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;radius_m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;active&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;quest_stamps&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
                    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;place_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each place has its own radius: 50 m by default, 150 m for parks, lakes, stadiums and other places whose coordinate is a centre rather than a door. Nearest places are stamped first, with a cap per sync and per day so a bus ride through a dense neighbourhood does not empty a chapter. Stamps give XP and badges at 1, 10, 50 and 100, but never coins. Coins are what co-pilots pay pilots with, so a reward that a GPS spoofer could farm is kept out of the ride economy.&lt;/p&gt;

&lt;p&gt;On the phone, the book is a leather cover that swings open at the chapter you are standing in, with category tabs on the page edge and round ink stamps. Asking for a chapter you are not in gets a distinct "not here" answer, so the app can explain instead of showing an empty page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frobddiu9rpy3hpxcp9mg.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frobddiu9rpy3hpxcp9mg.webp" alt="Closed leather quest book cover for the campus, 3 of 40 stamps, tap to open" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The campus quest book, closed. Tapping the cover swings it open at the chapter you are standing in.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Editing game content without shipping the app
&lt;/h2&gt;

&lt;p&gt;Fixing a quest place used to mean a staff member editing a draft in a web editor and someone running a seeding script from a terminal. I wanted two things: people we trust (a campus ambassador, say) should be able to fix places from inside the app while walking past them, and going live should be a button, not a script. And under no circumstances should an app user be able to change what everyone sees.&lt;/p&gt;

&lt;p&gt;The answer is the classic &lt;strong&gt;draft and publish&lt;/strong&gt; split.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Who&lt;/th&gt;
&lt;th&gt;Where&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Edit the draft&lt;/td&gt;
&lt;td&gt;Staff editors, plus app users switched on individually&lt;/td&gt;
&lt;td&gt;Web editor or the app&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Validate the draft&lt;/td&gt;
&lt;td&gt;Everyone, continuously&lt;/td&gt;
&lt;td&gt;Web, app and server, same rules&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Publish draft to live&lt;/td&gt;
&lt;td&gt;Admin staff only&lt;/td&gt;
&lt;td&gt;Admin panel button&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;App users reach the editor API with their normal sign-in token, but only if a per-user switch is on for them. If it is off, the API answers exactly as it would for a bad token. Every admin route refuses app tokens outright, so there is no path from the app to publishing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Publishing&lt;/strong&gt; copies the draft to the live tables in one transaction, with rules chosen to be safe for players:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Places are upserted by a stable external id, so editing a place never orphans the stamps people already hold.&lt;/li&gt;
&lt;li&gt;A place missing from the draft is switched off, never deleted. Its stamps survive.&lt;/li&gt;
&lt;li&gt;Chapters are rebuilt from the places in them: a 400 m buffer around the convex hull of each area's points.&lt;/li&gt;
&lt;li&gt;A draft with validation errors is refused, and so is an empty one, which would otherwise switch every live place off.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Optimistic concurrency, two people, one cafe
&lt;/h3&gt;

&lt;p&gt;Two editors can open the same place. Each row has a version, and a save carries the version it started from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;quest_editor_places&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;   &lt;span class="c1"&gt;-- the base version&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zero rows updated means someone else got there first; the server answers 409 with the row as it is now. The app shows the edit immediately, and on a conflict it rolls back, reloads and says "Someone else changed this. Reloaded." It never overwrites. The API client returns a sealed result rather than throwing, so every screen has to handle every case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;putPlace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_view&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Saved&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="n"&gt;_places&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;replaceById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_places&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Conflict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;           &lt;span class="n"&gt;_places&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;_notice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conflictNotice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="n"&gt;_places&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'no_access'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;_noAccess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The web editor gets live updates over server-sent events. The app polls every 20 seconds while the editor is open, which is simpler on mobile and plenty for a handful of editors. One subtle bug to avoid: a poll that lands while a save is in flight would replace the optimistic edit with stale server data. A counter of saves in flight makes the poll skip that round.&lt;/p&gt;

&lt;h3&gt;
  
  
  One set of rules, three languages
&lt;/h3&gt;

&lt;p&gt;What counts as a valid place has to be identical in the web editor (JavaScript), the app (Dart) and the publish step (Go), or an editor sees green in the app and the publish refuses it. There is no shared runtime between the three, so the rules are ported, not shared, and the tests carry the same cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the book needs a slug, a name and a region with at least three points;&lt;/li&gt;
&lt;li&gt;a place needs a name, an area (its chapter), one of the five categories and a position;&lt;/li&gt;
&lt;li&gt;the radius, if set, is a whole number from 10 to 500 m;&lt;/li&gt;
&lt;li&gt;a place outside the book's region is an error (ray casting on the polygon);&lt;/li&gt;
&lt;li&gt;two places with the same name within 15 m is a warning, not an error.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The duplicate check groups places by name first, so it stays fast on a 9,000-place book. Picking a position uses the map with a fixed crosshair in the centre: you move the map, not a pin, which works one-handed and never needs a long press. If the crosshair is outside the book's region, the screen says so before you save.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fts78y7rolfkciz5nawdm.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fts78y7rolfkciz5nawdm.webp" alt="Quest editor place list with a draft-only banner and counts of places, errors and warnings" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fay5yj3z6qjyduexai6j5.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fay5yj3z6qjyduexai6j5.webp" alt="Edit place form listing problems: needs an area, pick a category" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6n6k1zmg4sewrbnxpby6.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6n6k1zmg4sewrbnxpby6.webp" alt="Quest editor showing the notice: someone else changed this, reloaded" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3jvphppqsoljvpxxtdo1.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3jvphppqsoljvpxxtdo1.webp" alt="Map picker with a centre crosshair and a warning that the spot is outside the book region" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The in-app editor: the draft banner and error counts, a form with problems listed at the top, a conflict after someone else saved first, and the crosshair picker outside the book region.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Road Watch: road reports from riders
&lt;/h2&gt;

&lt;p&gt;Students who share rides know exactly where the potholes are. Road Watch lets anyone plant a &lt;strong&gt;warning flag&lt;/strong&gt; where the road has a problem: a pothole, a damaged surface, waterlogging, illegal parking, vending blocking the road, a broken light, a jam. Others &lt;strong&gt;back&lt;/strong&gt; it (+1 per person) and later &lt;strong&gt;confirm&lt;/strong&gt; when it has been fixed. Every issue shows how many days it has been open.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxxmru3d7x971o8fp8r93.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxxmru3d7x971o8fp8r93.webp" alt="What did you see: eight road problem types as pixel icon tiles" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbecenscwjo5ndgbcyzea.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbecenscwjo5ndgbcyzea.webp" alt="Add a photo step with camera and gallery buttons and a note that a moderator checks photos" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft4tupqi4q1tc7h2cb8o6.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft4tupqi4q1tc7h2cb8o6.webp" alt="Where is it: map pin 23 m from you, GPS accuracy chip and three severity levels with examples" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F070311z3xyqort9z3aqj.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F070311z3xyqort9z3aqj.webp" alt="Already flagged nearby: a pothole reported 19 m away with a back this report button" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The report flow: type (one tap moves on), an optional photo, place and severity with a plain example for each level, and the duplicate check.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The flow is four steps: type, photo, place with severity and an optional note, then review. A duplicate check sits before the review: a report of the same type within 40 m is offered as "Back this report" instead, because one issue with fourteen backers is worth more than fourteen issues. You can still file yours if it really is a different problem. There is a cap of three reports a day, and reports are accepted only inside the area the map covers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Honest by construction
&lt;/h3&gt;

&lt;p&gt;The design prototype for Road Watch was beautiful and slightly too optimistic. It said new reports were "Reported to the city office", that the right office owned each issue, and that faces and number plates were blurred automatically. None of that was true yet, so none of it shipped:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A new issue is "open", not "reported".&lt;/strong&gt; Nothing forwards reports automatically. It becomes "reported" only when a staff member actually passes it on, and only then does the "reported to" line appear.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ownership is a field, not a claim.&lt;/strong&gt; Every issue says "Owner: unconfirmed" until someone confirms it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Photos are moderated, not blurred.&lt;/strong&gt; The copy says a moderator checks photos before other people see them, because that is what happens.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Widget tests assert that words like "blurred" and "reward" never appear on these screens, so the prototype's promises cannot creep back in through a later copy change.&lt;/p&gt;

&lt;h3&gt;
  
  
  Never while driving
&lt;/h3&gt;

&lt;p&gt;The most important rule: &lt;strong&gt;a pilot never interacts with Road Watch while a trip is moving.&lt;/strong&gt; Typing a report is exactly the kind of distraction a road safety feature must not cause.&lt;/p&gt;

&lt;p&gt;The app enforces it with a check that says yes only when it is &lt;em&gt;certain&lt;/em&gt; the user is not piloting a trip in motion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;mayReport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;stateKnown&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isTripActive&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="nc"&gt;TripDto&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;trip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;myId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;matchPilotInProgress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;stateKnown&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;matchPilotInProgress&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// unknown counts as blocked&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trip&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isTripActive&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'in_progress'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;myId&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;trip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pilotId&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;trip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pilotId&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;myId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// co-pilots may report&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key is &lt;code&gt;stateKnown&lt;/code&gt;. The ride-state lookups return a small &lt;code&gt;Lookup&lt;/code&gt; type that tells "there is no trip" apart from "I could not find out". A failed network call never opens the gate. The check also runs again right before any action, not just when the screen opened.&lt;/p&gt;

&lt;p&gt;Instead of the report button, a pilot in motion gets one big gold button: &lt;strong&gt;Mark spot for later&lt;/strong&gt;. One tap stores the position and time on the phone, with no network, no photo and no typing. Marked spots last 24 hours. When the trip ends, the trip summary offers to finish them, which takes a few taps each while parked.&lt;/p&gt;

&lt;p&gt;The server enforces the same rule independently: creating a report or uploading a photo as the pilot of a trip in progress is refused with &lt;code&gt;409 trip_in_motion&lt;/code&gt;. Client checks are for experience; server checks are for safety.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Falkkpy7ujf4eh6t760wa.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Falkkpy7ujf4eh6t760wa.webp" alt="Pilot view during a trip: type, photo and note locked, a large mark spot for later button" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4uroolo09gxeareylhju.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4uroolo09gxeareylhju.webp" alt="Spot anything on the road: two marked spots with finish buttons after the trip" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F94d0ayfz63t0vi5mb6hy.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F94d0ayfz63t0vi5mb6hy.webp" alt="Saved on this phone: a queued report that will send when online" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Mark spot for later while piloting, finishing marked spots after the trip, and a report saved offline.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Photos on a phone budget
&lt;/h3&gt;

&lt;p&gt;Phone cameras produce multi-megabyte images full of metadata, including the GPS position of wherever the photo was taken. The app shrinks every photo before it leaves the phone, in a background isolate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Uint8List&lt;/span&gt; &lt;span class="nf"&gt;shrink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Uint8List&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;im&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decodeImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;PhotoTooLarge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'not a readable image'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;im&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bakeOrientation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;im&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                       &lt;span class="c1"&gt;// apply EXIF rotation to the pixels&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;im&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;im&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1280&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;im&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;resizeLongEdge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;im&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1280&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;im&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exif&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                    &lt;span class="c1"&gt;// re-encoding drops EXIF and GPS&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Uint8List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encodeJpg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;im&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;560&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// server allows 600 KB; keep headroom&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;PhotoTooLarge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Orientation has to be baked into the pixels &lt;em&gt;before&lt;/em&gt; the metadata is cleared, or portrait photos arrive sideways. Up to three photos are uploaded after the report is created, each with its own client-generated key, so a retried upload never creates a second copy. The server checks the JPEG magic bytes rather than trusting the content type, refuses a file that still carries an EXIF GPS block (a best-effort scan that never rejects a file just because it could not parse it), and keeps each photo visible only to its author until a moderator approves it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Offline first, without duplicates
&lt;/h3&gt;

&lt;p&gt;Roads with problems are often roads with bad signal. A report that fails to send goes into a queue on the phone and is sent again when the app resumes. The danger with retries is duplicates, so every report carries a UUID made on the phone, and the server has a unique constraint on author plus that key. Sending the same report twice is harmless.&lt;/p&gt;

&lt;p&gt;The queue flush has a few small rules that each fixed a real failure mode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A network error stops the flush (try later); a permanent refusal such as "outside the area" or "daily limit" drops that report so it cannot block the rest.&lt;/li&gt;
&lt;li&gt;Two flushes at once share one run instead of sending twice.&lt;/li&gt;
&lt;li&gt;The queue is re-read at the end, so a report added while a flush was running is kept.&lt;/li&gt;
&lt;li&gt;A corrupt stored value reads as an empty queue instead of wedging the feature.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are no push notifications, so status changes reach people through an &lt;strong&gt;inbox derived from data&lt;/strong&gt;: the phone takes the newest status event of each report you made or backed and turns it into an alert. Nothing is sent; the inbox is computed from the same data as your report list. Only events done by staff are worded as "Kurukuru staff marked it"; a fix confirmed by riders says so. A public "oldest open" list ranks unresolved issues by age, which is the accountability half of the feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbybfiyw3aem6rzqqgk72.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbybfiyw3aem6rzqqgk72.webp" alt="Issue detail with photo, 4 days open, 4 backers and a status timeline with future steps waiting" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwf65i2tle108y4xh238i.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwf65i2tle108y4xh238i.webp" alt="Oldest open list ranking unresolved road issues by days open, with owners marked unconfirmed" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;An issue with its timeline (future steps shown as waiting) and the public oldest-open list.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  One model, two codebases
&lt;/h2&gt;

&lt;p&gt;The app and the backend share no code: one is Dart, the other Go. What they share is a contract, and I found it holds up best when it is made of three things.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Outcomes, not exceptions.&lt;/strong&gt; Every API client returns a sealed type that names the cases the server can produce: &lt;code&gt;Created&lt;/code&gt;, &lt;code&gt;Duplicate(existing)&lt;/code&gt;, &lt;code&gt;Rejected(code)&lt;/code&gt; for reports; &lt;code&gt;Saved&lt;/code&gt;, &lt;code&gt;Conflict(current)&lt;/code&gt;, &lt;code&gt;Failed(code)&lt;/code&gt; for the editor. The compiler then makes every screen deal with every case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stable error codes as strings.&lt;/strong&gt; HTTP status codes are too coarse (two different 409s mean different things), so the server returns a short code and the app maps each one to a behaviour:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Code&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;What the app does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;duplicate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Same type within 40 m&lt;/td&gt;
&lt;td&gt;Shows the existing issue and "Back this report"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;trip_in_motion&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Caller is piloting a moving trip&lt;/td&gt;
&lt;td&gt;Closes the flow; suggests Mark spot&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;outside_area&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Outside the mapped area&lt;/td&gt;
&lt;td&gt;Explains that reporting is limited here&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;daily_limit&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Three reports today&lt;/td&gt;
&lt;td&gt;Says so; the queue drops it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;conflict&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Stale editor save&lt;/td&gt;
&lt;td&gt;Rolls back, reloads, shows the notice&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Constants owned by the server, mirrored by the app.&lt;/strong&gt; The daily cap, the 40 m duplicate radius, three confirmations to verify a fix, the 600 KB photo limit: the server enforces all of them, and the app mirrors them only to give a better answer sooner. If they ever drift, the server wins and the app shows the server's answer.&lt;/p&gt;

&lt;p&gt;Code generation from a schema would remove the duplication. For a two-language system this size, hand-written models with tests on both sides of every code were faster to change and easier to read, and the written design spec that listed every endpoint and code did the job a schema would have done.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Separate draft from live&lt;/strong&gt;, and make publishing an admin action that is safe for players: upsert by stable ids, deactivate instead of delete, refuse empty or invalid drafts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version every editable row&lt;/strong&gt; and let the database decide conflicts with &lt;code&gt;WHERE version = $base&lt;/code&gt;. Roll back and reload; never overwrite.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port validation rules deliberately&lt;/strong&gt; when you cannot share code, and keep the same test cases in every language.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Unknown" must fail closed&lt;/strong&gt; for anything safety-related. Model "could not find out" separately from "nothing there".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Give the risky user one safe action&lt;/strong&gt; (Mark spot for later) instead of just a disabled button.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shrink, strip and key every upload on the device&lt;/strong&gt;: bake orientation, drop location metadata, attach an idempotency key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ship only the promises that are true.&lt;/strong&gt; Write tests that keep the untrue ones out.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Stack in this piece: Flutter, Dart sealed classes, image and image_picker, shared_preferences; Go with Postgres and PostGIS on the backend. Quest data for Bengaluru from a community-curated list; map data © OpenStreetMap contributors.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  More from Building at CosQ
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-architecture" rel="noopener noreferrer"&gt;Kurukuru: A Ride-Sharing Game on One Go Binary&lt;/a&gt;: the architecture of a college ride-sharing game in one Go monolith.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-realtime-matching" rel="noopener noreferrer"&gt;Matching Riders in Real Time over WebSockets&lt;/a&gt;: live and scheduled ride matching over a Go WebSocket hub.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-fog-of-war" rel="noopener noreferrer"&gt;Fog of War on a Real Map with PostGIS&lt;/a&gt;: turning GPS trails into an explorable, game-like map.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-pixel-map-app" rel="noopener noreferrer"&gt;Turning a Real Map into a Pixel World&lt;/a&gt;: the Kurukuru app's map, 3D world and pixel UI in Flutter.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/cosq-ecosystem" rel="noopener noreferrer"&gt;Everything Else I Built at CosQ&lt;/a&gt;: WHAM! OTT, MemoChan, PreviouslyOn, Gothicco and more.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>flutter</category>
      <category>go</category>
      <category>gamedev</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Turning a Real Map into a Pixel World</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:17 +0000</pubDate>
      <link>https://dev.to/aharshit123456/turning-a-real-map-into-a-pixel-world-1ei</link>
      <guid>https://dev.to/aharshit123456/turning-a-real-map-into-a-pixel-world-1ei</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/kurukuru-pixel-map-app" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every map app looks like every other map app: grey roads, beige blocks, a blue dot. That is fine when the map is a tool, and a problem when the map is the game board and the brief says "make the campus look like a block world you want to explore". You still need real roads, real buildings and a real GPS position, on a mid-range phone, for a whole commute. This post is how the Kurukuru app turned OpenStreetMap into a pixel world, plus the plumbing around it: GPS streaming, sockets, a pixel design system and a golden image for every screen.&lt;/p&gt;

&lt;p&gt;Kurukuru is a gamified peer-to-peer ride-sharing app I built at CosQ for college campuses. A &lt;strong&gt;pilot&lt;/strong&gt; who is already going somewhere offers the spare seat; a &lt;strong&gt;co-pilot&lt;/strong&gt; who needs a ride pays a few coins for it. Along the way you earn XP, plant flags, collect stamps and clear a fog of war that hides the parts of the map you have never travelled. The app is Flutter; the backend is a single Go service with Postgres and PostGIS. I wrote the app end to end.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;3&lt;/strong&gt; map renderers, from raster tiles to a custom painter&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;10 m / 5 m&lt;/strong&gt; block size at the two levels of detail&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;218&lt;/strong&gt; golden images: every screen state on Android and iOS&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Three maps in eight months
&lt;/h2&gt;

&lt;p&gt;The map went through three renderers, each right for the design of its time.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Renderer&lt;/th&gt;
&lt;th&gt;How it draws&lt;/th&gt;
&lt;th&gt;What you can restyle&lt;/th&gt;
&lt;th&gt;Why it stopped being enough&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;flutter_map&lt;/code&gt; + raster tiles&lt;/td&gt;
&lt;td&gt;Downloads pre-rendered PNG tiles&lt;/td&gt;
&lt;td&gt;Almost nothing: tint, opacity&lt;/td&gt;
&lt;td&gt;A picture of a road cannot be told to become a block&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MapLibre + vector tiles&lt;/td&gt;
&lt;td&gt;The phone renders geometry with a style JSON&lt;/td&gt;
&lt;td&gt;Every layer: colour, width by zoom, labels&lt;/td&gt;
&lt;td&gt;Still a flat map; no buildings with height, no trees, no blocks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Custom voxel painter&lt;/td&gt;
&lt;td&gt;Server bakes a grid of block classes; the app paints cells&lt;/td&gt;
&lt;td&gt;Everything, including what exists at all&lt;/td&gt;
&lt;td&gt;(current)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Raster tiles&lt;/strong&gt; came first, with a dark basemap that matched the original dark design. Raster tiles are images; when the design moved to a bright parchment block world, there was nothing to restyle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Vector tiles&lt;/strong&gt; fixed that. With MapLibre the phone receives geometry (roads, water, buildings, labels in the OpenMapTiles schema, from a free tile host) and renders it with a style document you write yourself. The whole Kurukuru street style is about 140 lines of Dart that emits that JSON:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;dynamic&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;_roadLayer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;casing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'type'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'line'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'source'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'omt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'source-layer'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'transportation'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'paint'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s1"&gt;'line-color'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;casing&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;_roadCasing&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'match'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'get'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'class'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'motorway'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'trunk'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'primary'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_roadMajor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'#FFFFFF'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// exponential width by zoom: hairlines far out, fat cartoon roads close in&lt;/span&gt;
    &lt;span class="s1"&gt;'line-width'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;casing&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;_widthByZoom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;_widthByZoom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stock pins ruin a pixel style instantly, so the app draws its own markers at startup: flags, the player square and the labelled "kuru in / kuru out" pins are painted onto a &lt;code&gt;Canvas&lt;/code&gt; in the app palette and pixel font, encoded to PNG and registered with MapLibre as sprites.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The voxel map&lt;/strong&gt; came when the design asked for buildings with height, trees and roads made of blocks. A style sheet can recolour a polygon, but it cannot turn a building footprint into a stack of blocks with a shaded south wall. That needs data shaped like the picture you want.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bake on the server, paint on the phone
&lt;/h2&gt;

&lt;p&gt;The trick is to move the hard GIS work offline. A Python tool on the backend reads an OpenStreetMap extract and &lt;strong&gt;bakes&lt;/strong&gt; it into a grid: every cell gets a ground class, an optional object and a building height class.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Codes (one byte per cell)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ground&lt;/td&gt;
&lt;td&gt;grass, park, forest, farmland, water, path, lane, road, main road, bridge, railway, ...&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;object&lt;/td&gt;
&lt;td&gt;nothing, tree, or a building kind: house, flats, shop, institution, religious, industrial&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;height&lt;/td&gt;
&lt;td&gt;building height class 0 to 9, about 4 m each&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Rows are run-length encoded (&lt;code&gt;g12r3&lt;/code&gt; is twelve grass cells then three road cells), which compresses a campus to almost nothing because most of the world is long runs of the same ground.&lt;/p&gt;

&lt;p&gt;A whole city cannot be one file, so the grid is cut into square tiles on one shared grid: at 10 m cells, a tile of 200 by 200 cells covers 2 km. A finer level with 5 m cells exists for zooming in. The tool writes a manifest listing every tile as &lt;code&gt;[row, col, bytes, sha1]&lt;/code&gt;, and that hash does a lot of work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The app requests a tile with its hash in the URL, so the server can answer with &lt;code&gt;Cache-Control: immutable&lt;/code&gt;. A re-bake changes the hash, which changes the URL. Cache invalidation for free.&lt;/li&gt;
&lt;li&gt;A tile missing from the manifest is open grass. The app does not even ask for it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;OSM extract (offline bake) → Manifest [r, c, bytes, sha1] → 3×3 tiles around camera LRU cache, decoded off the UI thread → &lt;strong&gt;Stitched window painted cell by cell&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — the voxel map pipeline. The heavy geometry work runs once on the server; the phone only fetches small tiles by hash and paints a grid.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;On the phone, a tile source keeps the 48 most recently used tiles, shares in-flight downloads so two requests for the same tile make one network call, and decodes JSON in a background isolate. It fetches the 3×3 tiles around the camera and &lt;strong&gt;stitches&lt;/strong&gt; them into one world object, so the painter never knows tiles exist. A tile that fails to download is drawn as grass and retried a few seconds later.&lt;/p&gt;

&lt;p&gt;Two details made level-of-detail switching invisible:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Zoom is stored in pixels per metre, not pixels per cell.&lt;/strong&gt; When the 5 m level takes over from the 10 m one, a building stays exactly the same size on screen. Positions travel through latitude and longitude, so the camera does not jump either.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A dead band around the switch zoom.&lt;/strong&gt; The finer level takes over at 104% of the switch point and hands back at 96%. Without it, a pinch that hovers on the boundary flips levels every frame.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The painter itself is a &lt;code&gt;CustomPainter&lt;/code&gt; that walks visible rows top to bottom, so nearer buildings cover farther ones. Buildings get a soft shadow towards the south-east, a front wall on their south face and an outline only where the building ends, so neighbouring cells of the same building read as one block. Fog of war is a single path with the even-odd fill rule: the outer rectangle plus the explored outline, so the explored area cancels out and stays clear.&lt;/p&gt;

&lt;h3&gt;
  
  
  Labels that never fight
&lt;/h3&gt;

&lt;p&gt;Real campuses have hundreds of named things, and pixel labels are wide. Every landmark is baked with a rank (0 for campus, hospital or stadium; 1 for departments, hostels and gates; 2 for the rest), and the zoom decides the least important rank worth drawing. Then a greedy pass keeps labels in importance order and drops any that would overlap one already placed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;declutter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Iterable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="nc"&gt;Rect&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;boxOf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;rankOf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;gap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ordered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;byRank&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rankOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;$&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compareTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;rankOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;$&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;byRank&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;byRank&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;$&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compareTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;$&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ties keep input order&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;kept&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;boxes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Rect&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ordered&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;box&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;boxOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inflate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;boxes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;overlaps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;boxes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;kept&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;kept&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding more names never makes the map busier; it only fills free space. Labels are drawn &lt;em&gt;after&lt;/em&gt; the fog, so you can read the name of a place you have not been to yet, which is most of the reason to go there.&lt;/p&gt;

&lt;h2&gt;
  
  
  A 3D campus inside a WebView
&lt;/h2&gt;

&lt;p&gt;As an experiment, the app can open a walkable 3D campus: a three.js world built from the same OpenStreetMap data, with roads as ribbon meshes, extruded buildings and a voxel character on a joystick. It is a small web app bundled into the APK (about 2.4 MB, in 500 m tiles) and shown in a WebView.&lt;/p&gt;

&lt;p&gt;The snag: a WebView can load one HTML file from the app bundle, but the page cannot &lt;code&gt;fetch&lt;/code&gt; its sibling JSON tiles from there. The fix is a tiny HTTP server inside the app, bound to the loopback address on a random port, that serves only the bundled world folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;cleanPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;uriPath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uriPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'\\'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;uriPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'\u0000'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uriPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isNotEmpty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'..'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'.'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// no escaping the folder&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'index.html'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The WebView's navigation delegate allows only &lt;code&gt;http://127.0.0.1:&amp;lt;that port&amp;gt;&lt;/code&gt;, so the page cannot wander onto the internet, and the server answers only &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;HEAD&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  GPS for a whole commute
&lt;/h2&gt;

&lt;p&gt;Location is the most expensive thing the app does, and it has three consumers with very different needs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Consumer&lt;/th&gt;
&lt;th&gt;What it needs&lt;/th&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The map&lt;/td&gt;
&lt;td&gt;Smooth, current position&lt;/td&gt;
&lt;td&gt;Stream with a 10 m distance filter (5 m on the live route screen)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fog and stamps&lt;/td&gt;
&lt;td&gt;Every place you passed, eventually&lt;/td&gt;
&lt;td&gt;Batched upload every 5 s in front, every 20 s in the background&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The co-pilot watching the pilot&lt;/td&gt;
&lt;td&gt;A moving dot, a few times a minute&lt;/td&gt;
&lt;td&gt;Pilot broadcasts at most once every 3 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;One service owns the GPS stream, so it survives screens opening and closing, and fans fixes out. The upload side is a pure-Dart class with no plugins, easy to test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fixes queue with their own timestamp, so the server can drop impossible jumps (anything faster than about 144 km/h from the previous kept fix).&lt;/li&gt;
&lt;li&gt;The queue is capped at 200 fixes; a failed upload puts the batch back at the front.&lt;/li&gt;
&lt;li&gt;While the app is in the background, gains are tallied separately and shown as "while you were away" on return.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Background location on Android means a &lt;strong&gt;foreground service with a visible notification&lt;/strong&gt;, a battery and trust cost. So it is opt-in, off by default, and the notification says plainly what it does ("clearing the fog as you walk"). With it off, the fog only clears while the app is open.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sockets on a phone: fast path, not source of truth
&lt;/h2&gt;

&lt;p&gt;During a trip, both phones join a WebSocket for that trip. It carries the pilot's position, a "trip finished" event and discovery events. Phones are hostile to long-lived sockets: tunnels, network handovers between Wi-Fi and mobile data, the OS freezing a backgrounded app.&lt;/p&gt;

&lt;p&gt;Rather than build reconnect-and-replay, I split the jobs. The socket is the &lt;strong&gt;fast path&lt;/strong&gt; for things that are fine to miss, like the next position of a moving dot. The &lt;strong&gt;truth&lt;/strong&gt; about the trip (requested, matched, in progress, completed) comes from polling the trip every two to three seconds while a trip screen is open. If the socket drops, the dot pauses; the trip still ends correctly for both people. Each position message carries the sender's id, so a phone ignores its own echo.&lt;/p&gt;

&lt;p&gt;A few small requests per minute is a fair price for never leaving a rider stuck on a trip screen.&lt;/p&gt;

&lt;h2&gt;
  
  
  A design system with three rules
&lt;/h2&gt;

&lt;p&gt;The look is a parchment and block world: grass, dirt, river and gold on a warm paper background, outlined in a dark green-black "ink".&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Token&lt;/th&gt;
&lt;th&gt;Hex&lt;/th&gt;
&lt;th&gt;Used for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ink&lt;/td&gt;
&lt;td&gt;&lt;code&gt;#1F2A1D&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Every outline and all text&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;parchment&lt;/td&gt;
&lt;td&gt;&lt;code&gt;#F5EEDC&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Panels, sheets, screen backgrounds&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;grass&lt;/td&gt;
&lt;td&gt;&lt;code&gt;#7DBB4A&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Primary buttons, progress&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;gold&lt;/td&gt;
&lt;td&gt;&lt;code&gt;#E8B23A&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Coins, pilot, rewards&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;red&lt;/td&gt;
&lt;td&gt;&lt;code&gt;#D0452F&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Flags, warnings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;river / sky&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;#4FA3C7&lt;/code&gt; / &lt;code&gt;#9FD3EA&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Water, login sky&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Three rules carry most of the style:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Two faces, strictly split.&lt;/strong&gt; Pixelify Sans for headings, numbers and button labels; DM Sans for anything you actually read. The split paid off in an unexpected way: Pixelify draws &lt;code&gt;(&lt;/code&gt; and &lt;code&gt;)&lt;/code&gt; as &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;, so a building called "CSE Block (New Block)" rendered as &lt;code&gt;{New Block}&lt;/code&gt;. Place names now always use the body face.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Everything is lowercase.&lt;/strong&gt; Instead of trusting every string, the app has its own &lt;code&gt;Text&lt;/code&gt; widget that lowercases its input, and files import it in place of Flutter's (&lt;code&gt;hide Text&lt;/code&gt;). Letter spacing from the theme is cut to a third, because wide tracking suits capitals and looks broken in lowercase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blocks, not cards.&lt;/strong&gt; Panels have a 3 px ink outline, a hard bottom edge and inset shading; buttons sink when pressed. A dozen widgets (box, button, field, tile, toggle, XP bar, level badge, ground strip) cover almost every screen.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqvir2ekamax37g2vnwyz.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqvir2ekamax37g2vnwyz.webp" alt="Kurukuru login screen with a block logo, pixel clouds and a grass and dirt floor" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F53wijbf8ziu657aotrrq.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F53wijbf8ziu657aotrrq.webp" alt="Onboarding page explaining the pilot and co-pilot roles in parchment cards" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6rqelfpcbvs56n1zqppu.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6rqelfpcbvs56n1zqppu.webp" alt="Plan a ride screen with pilot and co-pilot toggle, start and destination, match now or schedule" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg6mzmqhgms8o4yv2w45x.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg6mzmqhgms8o4yv2w45x.webp" alt="Match offer sheet showing a pilot found, fare in coins and kuru in and out times" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The design system in use: login, the roles page of onboarding, the ride planner and a match offer. People in test fixtures are pseudonyms like "rider one".&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  From an HTML prototype to Flutter
&lt;/h2&gt;

&lt;p&gt;The screens were designed as a clickable HTML prototype: one page with a rail of numbered screens, plain JavaScript flows and sample data everywhere. It becomes the thing everyone compares the app to. Two lessons from turning it into Flutter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The prototype specifies the look, not the truth.&lt;/strong&gt; It happily shows "Reported to the city office" or "faces are blurred automatically" because sample data costs nothing. The app has to decide what is real, and say only that. I kept a written list of every place the app deliberately differs and why.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compare pictures, not memories.&lt;/strong&gt; For the biggest feature I rendered every prototype screen in headless Chrome, put the frames next to the app's screenshots and wrote a ranked gap list. The app was faithful to the written spec, but the spec had cut so much of the prototype that nothing seemed to match. A screen-by-screen parity pass, driven by that list, fixed it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A golden image for every screen
&lt;/h2&gt;

&lt;p&gt;Those app screenshots come from a &lt;strong&gt;golden test&lt;/strong&gt; suite. A golden test renders a widget, saves the image next to the code, and fails if a later render differs. I wanted one for every screen in every state that matters: empty, loading, error, populated, and the ride lifecycle in order.&lt;/p&gt;

&lt;p&gt;The harness is small:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;androidPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GoldenDevice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'android'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;360&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TargetPlatform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;iosPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GoldenDevice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ios'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;390&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;844&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TargetPlatform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iOS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;goldenTest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Widget&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;build&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;androidPhone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iosPhone&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;testWidgets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'$description [${device.name}]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tester&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;pumpGolden&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tester&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;expectLater&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;byType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MaterialApp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="nf"&gt;matchesGoldenFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'goldens/$screen/$state.${device.name}.png'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Around it: a fixed "now" so "today" and "in 2 days" never change, fonts bundled as assets with runtime font fetching switched off, the Material icon font loaded by hand (otherwise tests draw icons as boxes), and only the two phone sizes the app ships on. The real map is a platform view that cannot render in a test, so it is swapped for a flat grass tile with the player square. That is why the map looks empty in the screenshots in these posts. Fixtures are shaped from real ride data (routes, dates, fares, match states) with every person pseudonymised. A short script tiles the images into one contact sheet per flow for design review.&lt;/p&gt;

&lt;p&gt;The first full audit found real bugs, not just pixels:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A weekly scheduled ride showed "locked in" for the whole week when only some days had a match, and the list was capped so a matched day simply vanished.&lt;/li&gt;
&lt;li&gt;Two screens showed 12:00 and 12:01 for the same ride. Not an offset bug: one showed departure, the other pickup time, and neither said which.&lt;/li&gt;
&lt;li&gt;The trip drawer took its label, headline and button from three different sources, so it could say "pilot en route", "searching" and "waiting for pilot" at once. All three now derive from one trip status.&lt;/li&gt;
&lt;li&gt;A "scheduled rides" button rendered at half width because a &lt;code&gt;Stack&lt;/code&gt; loosened its parent's width constraint.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffuzam4za3xjzvpa90xpk.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffuzam4za3xjzvpa90xpk.webp" alt="Scheduled rides list with per-day state, 4 of 6 days locked in and no match yet tags" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn7ky11yzahrcwzd4k1cb.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn7ky11yzahrcwzd4k1cb.webp" alt="Trip complete screen with coins earned, XP gained and a first ride badge" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh9th5p33042pgk2be32o.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh9th5p33042pgk2be32o.webp" alt="Vault screen showing a coin balance and recent ride earnings and fares" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Goldens after the audit fixes: per-day state on scheduled rides (and place names in the body face, so parentheses stay parentheses), trip complete, and the coin vault.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting builds to testers
&lt;/h2&gt;

&lt;p&gt;Early builds went to testers as APKs through Firebase App Distribution: build, upload, send a link. Once the app was on the Play Store's testing tracks, I moved everything to fastlane so a release is one command and the store listing lives in the repo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# android/fastlane
fastlane build                          signed release .aab (does not upload)
fastlane internal notes:"Fixed login"   bump, build, upload to Internal testing
fastlane closed   notes:"Fixed login"   bump, build, upload to Closed testing
fastlane promote from:alpha to:internal copy a build to another track
fastlane metadata                       push store listing text and screenshots only
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each upload lane bumps the build number, writes the changelog and uploads, so tester release notes are versioned with the code. The Play service account key stays out of git.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pick the renderer by what you need to change, not by what is popular.&lt;/strong&gt; Raster tiles can be tinted, vector tiles can be restyled, and only your own data can change what exists on the map.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bake offline, paint simply.&lt;/strong&gt; A server-side grid with content-hashed tiles gave a custom-looking map with almost no work on the phone and caching that invalidates itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store zoom in real-world units&lt;/strong&gt; and add hysteresis to any threshold a finger can hover on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Give each GPS consumer its own rate.&lt;/strong&gt; Display, sync and broadcast need different frequencies; one owner for the stream keeps the battery cost honest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Treat sockets on mobile as a fast path.&lt;/strong&gt; Poll for state that must be right; push what is fine to miss.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Golden-test every state&lt;/strong&gt;, and read the pictures. The worst bugs we found were wrong words, not wrong pixels.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Stack in this piece: Flutter, flutter_map and then maplibre_gl on OpenMapTiles-schema vector tiles, a CustomPainter voxel renderer, three.js in webview_flutter, geolocator, web_socket_channel, flutter_test goldens, fastlane. Map data © OpenStreetMap contributors.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  More from Building at CosQ
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-architecture" rel="noopener noreferrer"&gt;Kurukuru: A Ride-Sharing Game on One Go Binary&lt;/a&gt;: the architecture of a college ride-sharing game in one Go monolith.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-realtime-matching" rel="noopener noreferrer"&gt;Matching Riders in Real Time over WebSockets&lt;/a&gt;: live and scheduled ride matching over a Go WebSocket hub.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-fog-of-war" rel="noopener noreferrer"&gt;Fog of War on a Real Map with PostGIS&lt;/a&gt;: turning GPS trails into an explorable, game-like map.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-quests-and-road-watch" rel="noopener noreferrer"&gt;Quest Books, an In-App Editor and Road Watch&lt;/a&gt;: game content you can edit in the app, and road safety reports.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/cosq-ecosystem" rel="noopener noreferrer"&gt;Everything Else I Built at CosQ&lt;/a&gt;: WHAM! OTT, MemoChan, PreviouslyOn, Gothicco and more.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>maps</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>Fog of War on a Real Map with PostGIS</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:13 +0000</pubDate>
      <link>https://dev.to/aharshit123456/fog-of-war-on-a-real-map-with-postgis-deo</link>
      <guid>https://dev.to/aharshit123456/fog-of-war-on-a-real-map-with-postgis-deo</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/kurukuru-fog-of-war" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In strategy games the map starts dark. You only see what your units have walked through, and the black slowly peels back into terrain. It is one of the oldest tricks for making a map feel like something you earn. So what happens when the map is your actual campus, the unit is you, and the fog only lifts where your phone's GPS has really been? This post is how Kurukuru's backend turns a stream of noisy GPS fixes into a personal explored area, serves it back as a single shape, pays out progress for it, and draws a blocky world from OpenStreetMap underneath.&lt;/p&gt;

&lt;p&gt;Kurukuru is a gamified, college-first, peer-to-peer ride-sharing app I built at CosQ: a &lt;strong&gt;pilot&lt;/strong&gt; with a free seat gives a &lt;strong&gt;co-pilot&lt;/strong&gt; a lift and earns in-app coins. The game layer exists to give people a reason to open the app on days they don't need a ride, and to make every ride also count for something. Exploration is the core of that layer. Its backend is a few hundred lines of Go and some well-chosen PostGIS calls (&lt;a href="https://www.aharshit123456.space/blog/kurukuru-architecture" rel="noopener noreferrer"&gt;the architecture post&lt;/a&gt; covers the rest of the service).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;~36 × 19 m&lt;/strong&gt; size of one fog tile at the campus's latitude&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;~75&lt;/strong&gt; tiles lifted around a single GPS fix (120 m radius)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;23,750&lt;/strong&gt; tiles in the campus area, the denominator of "% explored"&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pick a grid before you pick a shape
&lt;/h2&gt;

&lt;p&gt;The naive fog stores the user's GPS trail and buffers it into a polygon. That breaks quickly: trails are huge, buffering a long line on every request is slow, and "how much have I explored?" has no stable answer because two overlapping walks count twice unless you union them.&lt;/p&gt;

&lt;p&gt;The standard alternative is to &lt;strong&gt;discretise the world into tiles&lt;/strong&gt; and store, per user, the set of tiles they have seen. Membership is a primary-key lookup, overlap is free (a tile seen twice is still one row), and "explored percentage" is a count. The question is which grid.&lt;/p&gt;

&lt;p&gt;I used &lt;strong&gt;geohash&lt;/strong&gt;. A geohash encodes a latitude and longitude by repeatedly halving the world: the first bit says east or west of the prime meridian, the second north or south of the equator, then the same again inside the chosen half, alternating. Every five bits become one character from a 32-letter alphabet. Each extra character makes the cell 32 times smaller, and every cell has a short, stable string name.&lt;/p&gt;

&lt;p&gt;Eight characters (40 bits, 20 for longitude and 20 for latitude) give a cell 360/2²⁰ degrees wide and 180/2²⁰ degrees tall. Near the campus that is about 36 m east to west and 19 m north to south: big enough that a walk produces a manageable number of rows, small enough that the fog's edge still follows paths and buildings.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;strong&gt;Why not hexagons?&lt;/strong&gt; Hex grids such as Uber's H3 have nicer neighbour properties and equal-area cells. But PostGIS can turn a geohash straight into its polygon with &lt;code&gt;ST_GeomFromGeoHash&lt;/code&gt; and back with &lt;code&gt;ST_GeoHash&lt;/code&gt;, the encoder is fifty lines of Go with no dependency, and the merged outline only needs edges that line up. Rectangles were enough.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  From GPS fixes to tiles
&lt;/h2&gt;

&lt;p&gt;The app collects fixes in the background and uploads them in batches: every 5 seconds while it is on screen and every 20 seconds when it isn't, keeping at most the newest 200 fixes. A failed upload puts the batch back at the front of the queue, so walking through a dead zone costs nothing. Each fix carries the time it was taken, not the time it was sent.&lt;/p&gt;

&lt;p&gt;The server first makes the fog lift in a &lt;strong&gt;radius&lt;/strong&gt; rather than in the single tile under the fix. GPS on a phone is often off by tens of metres, and a 36 m wide tile under a wobbling dot looks like a broken trail. Revealing everything within 120 m of each fix gives a smooth band along the path you walked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c1"&gt;// tiles whose area intersects the circle of radiusM around (lat, lng)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;la&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;lat&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;dLat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;la&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;lat&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;dLat&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;cellLat&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;la&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;cellLat&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;lng&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;dLng&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;lng&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;dLng&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;cellLng&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;cellLng&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;la&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;mPerDegLat&lt;/span&gt;
        &lt;span class="n"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lng&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;mPerDegLng&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;dy&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;radiusM&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;radiusM&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;seen&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;geohashEncode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;la&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sampling at half a cell in each direction guarantees every tile inside the circle is hit at least once; the set removes duplicates. One fix produces about 75 tiles, and a batch of fixes from a walk shares most of them, so the server collects the distinct tiles for the whole batch before touching the database. A straight one-kilometre walk with a fix every ten metres reveals about 440 distinct tiles.&lt;/p&gt;

&lt;h3&gt;
  
  
  Learning which tiles are new, in one round trip
&lt;/h3&gt;

&lt;p&gt;Then come two bulk statements, each passing the whole tile list as one Postgres array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;grid_tiles&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;geom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ST_GeomFromGeoHash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;unnest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;user_explorations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;grid_tile_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;unnest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;grid_tile_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first lazily creates global tile rows with their polygons, so the grid only ever contains places someone has actually been. The second is the useful trick: &lt;code&gt;ON CONFLICT DO NOTHING ... RETURNING&lt;/code&gt; returns &lt;strong&gt;only the rows that were actually inserted&lt;/strong&gt;. Tiles the user had already seen are silently skipped, so the result is exactly "tiles newly revealed by this batch", with no read-before-write and no race between two uploads from the same phone.&lt;/p&gt;

&lt;p&gt;The newly revealed tiles go through one more conditional update to find &lt;strong&gt;pioneer&lt;/strong&gt; tiles, the ones nobody in the whole app has seen before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;grid_tiles&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;first_discoverer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ANY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;first_discoverer_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the &lt;code&gt;IS NULL&lt;/code&gt; check and the write happen in one statement, two people walking into the same unexplored corner at the same moment can't both be first. If the upload belongs to a trip in progress, the server pushes a "pioneer" event into that trip's WebSocket channel, and both riders see a toast that they just mapped a new corner of campus.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# fog sync · illustrative request and response
$ curl -X POST $HONBU/api/explore/sync -H "Authorization: Bearer &amp;lt;token&amp;gt;" \
    -d '{"trip_id":"","gps_trace":[{"lat":20.35,"lng":85.81,"t":1763100000000}, …]}'
{
  "status": "synchronized",
  "new_tiles_unlocked": 132,
  "total_unlocked": 1250,
  "pioneer_tiles": ["tgt5…"],
  "discoveries": 2,
  "xp_gained": 100,
  "coins_gained": 20,
  "stamps": []
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The response also carries the user's new XP, level and coin totals, so the app can animate a reward without a second request. The user id in the body is ignored and replaced with the token's subject: a client can report where it has been, but never on someone else's behalf.&lt;/p&gt;

&lt;h2&gt;
  
  
  Serving the fog as one shape
&lt;/h2&gt;

&lt;p&gt;The map screen needs the inverse: everything this user has explored. Sending thousands of rectangles and letting the phone draw each one would be slow and would leave hairline seams between tiles. Instead the server merges them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nc"&gt;ST_AsGeoJSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ST_Union&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;geom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*)
FROM user_explorations e
JOIN grid_tiles t ON e.grid_tile_id = t.id
WHERE e.user_id = $1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ST_Union&lt;/code&gt; dissolves the shared edges and returns one (multi)polygon: an outline with stepped edges, plus &lt;strong&gt;holes&lt;/strong&gt; for unexplored pockets enclosed by explored ground, like a courtyard you walked around but never into. The app receives a single GeoJSON shape and the tile count.&lt;/p&gt;

&lt;p&gt;user's tiles rows of rectangles → &lt;strong&gt;ST_Union one outline + holes&lt;/strong&gt; → GeoJSON rings and pockets → app: screen-sized fog, outline cut out (even-odd), pockets painted back&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — from rows to fog. The server does the geometry; the client draws one path.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Drawing it is then cheap. The app builds one path containing a rectangle the size of the screen plus every explored ring, fills it with the &lt;strong&gt;even-odd&lt;/strong&gt; rule (so the rings become cut-outs in the fog), and paints the pockets back in. Before the first outline arrives it clears a small circle around the player, so a new user never starts in total darkness.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;strong&gt;The trade-off: the union is recomputed on every map load.&lt;/strong&gt; For one user on one campus that is a few thousand small polygons and stays fast. A heavy explorer of a whole city would make it expensive; the obvious next step is to cache the outline per user and invalidate it when a sync reveals new tiles, or to union only the new tiles into the cached shape.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpbu7bio9c98eh2ud1hvj.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpbu7bio9c98eh2ud1hvj.webp" alt="Exploring sheet showing a percentage of the campus explored, the tile count, and a switch to keep exploring in the background" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In the app — the exploring sheet, rendered from test data: percentage of campus, tile count, and the opt-in switch for background exploration.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Turning tiles into progress
&lt;/h2&gt;

&lt;p&gt;The percentage needs a denominator. The campus is a fixed bounding box, so the number of geohash-8 tiles inside it is a constant computed once: 23,750. Tiles revealed off campus still count toward your total and your rewards; the percentage is simply capped at 100.&lt;/p&gt;

&lt;p&gt;Rewards are paid in &lt;strong&gt;discoveries&lt;/strong&gt;. Each user has a small counter of newly revealed tiles that haven't been paid for yet. Every sync adds the new tiles to it; every full 60 tiles converts into one discovery worth 50 XP and 10 coins, and the remainder stays banked for next time. The increment, the conversion and the grant happen in one transaction, so two overlapping syncs can't both cash in the same 60 tiles. That one-kilometre walk is worth about seven discoveries.&lt;/p&gt;

&lt;p&gt;Levels are deliberately dull: one level per 500 XP, computed from total XP whenever needed. Badges are not stored at all. Each badge is a definition (a metric and a target, like "explore 500 tiles" or "finish 10 trips"), and a user's badges are evaluated from live counts of their tiles, flags, trips and stamps. "Newly unlocked" is computed by evaluating the counts before and after an action and diffing. With no badge table there is nothing to migrate when a badge is added or a target changes, and nothing that can drift out of sync with the underlying data.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the fog doesn't defend against
&lt;/h3&gt;

&lt;p&gt;An open question in any location game is cheating. The fog trusts the positions it is sent, apart from dropping impossible coordinates. Quest stamps, the part of the game where you collect specific places, are stricter: they drop any fix that would need more than 40 m/s (about 144 km/h) to reach from the previous kept fix, cap stamps per sync and per day, and pay only XP, never coins. For the fog itself I accepted the risk, because what you gain by spoofing it is a prettier personal map and a slow trickle of coins. If exploration ever paid real value, the same speed filter would be the first thing to move over.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flags
&lt;/h3&gt;

&lt;p&gt;The other way to mark the map is to plant a &lt;strong&gt;flag&lt;/strong&gt; with a short note. The tile under it is computed in SQL with &lt;code&gt;ST_GeoHash(point, 8)&lt;/code&gt;; if you are that tile's first discoverer, the flag is marked as a pioneer flag. Flags are anonymous by design: the API never returns an author, only whether a flag is yours. Nearby flags are fetched with a bounding-box prefilter on plain latitude and longitude columns, and a flag disappears for everyone after three reports, and immediately for the person who reported it or blocked its author.&lt;/p&gt;

&lt;h2&gt;
  
  
  The world under the fog
&lt;/h2&gt;

&lt;p&gt;Fog needs something to hide. The first map was a standard tile map; the current one is a pixel-art &lt;strong&gt;block world&lt;/strong&gt; baked from OpenStreetMap, so roads, water, parks and buildings each have their own block type and buildings rise according to their height.&lt;/p&gt;

&lt;p&gt;The baker is a Python tool that runs offline:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Download&lt;/strong&gt; OpenStreetMap features for the area: land use, water, roads, railways, buildings and trees.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rasterise&lt;/strong&gt; everything onto a grid three times finer than the target, in painter's order (land use first, then water, then roads at their real width), with proper even-odd filling so courtyards and ponds become holes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Downsample&lt;/strong&gt; each cell from its 3×3 subsamples by weighted majority, with boosts for roads and water so a 5 m lane or a narrow canal survives on a 10 m grid instead of being voted away by the grass around it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Classify buildings&lt;/strong&gt; by kind (house, flats, shop, institution, religious, industrial) and height class, from OSM's height or floor count where present and a per-kind default otherwise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encode&lt;/strong&gt; each row with run-length encoding (&lt;code&gt;g12r3g40&lt;/code&gt; is twelve grass, three road, forty grass) and gzip the result.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One region file was fine for one campus. A whole city was not: a public OSM query server can't serve that much data, and a single file would be tens of megabytes. So the city baker clips a regional extract locally, cuts the city into square tiles that all sit on &lt;strong&gt;one shared grid&lt;/strong&gt; (so neighbouring tiles meet on exact cell edges), and writes a manifest listing each tile with its size and SHA-1 hash.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Map&lt;/th&gt;
&lt;th&gt;Levels of detail&lt;/th&gt;
&lt;th&gt;Tiles&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Campus&lt;/td&gt;
&lt;td&gt;4 m and 2 m cells&lt;/td&gt;
&lt;td&gt;30 and 99&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bengaluru&lt;/td&gt;
&lt;td&gt;10 m and 5 m cells&lt;/td&gt;
&lt;td&gt;650 per level&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The Go server embeds all of it with &lt;code&gt;go:embed&lt;/code&gt;, about 21 MB in total, and serves a tile with its hash as the &lt;code&gt;ETag&lt;/code&gt;. A request that includes the hash from the manifest gets &lt;code&gt;Cache-Control: immutable&lt;/code&gt; for a year, because a tile's content can never change under the same hash; anything else gets an hour. The app switches from the coarse level to the fine one past a zoom threshold given in the manifest. Tiles that would be nothing but grass are never written; a 404 for a tile means "open grass", which keeps the city's outskirts free.&lt;/p&gt;

&lt;h3&gt;
  
  
  Toward an open world
&lt;/h3&gt;

&lt;p&gt;The block map is top-down. The next design walks around in it: a real-time 3D world built from the same kind of map data, where you move a voxel character through the streets near campus. The design splits it cleanly. An offline builder precomputes the area as JSON tiles, 500 m square, in &lt;strong&gt;local metres&lt;/strong&gt; east and north of one origin, so the renderer never handles latitude and longitude. Roads are polylines with their real width and class, buildings are footprints with heights, and land cover, shops, trees and lamps ride along. A three.js renderer streams tiles around the player, turns roads into ribbon meshes along the real polylines, extrudes buildings and checks collisions against footprints.&lt;/p&gt;

&lt;p&gt;The tile JSON, with a version field in every file, is the only contract between the two halves: the renderer knows nothing about where the data came from. Terrain height, traffic, pedestrians and interiors were explicitly left out, because the area is flat enough and each would have doubled the scope. A trial of it sits behind a feature switch in the app.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd reuse
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Discretise before you geometrise.&lt;/strong&gt; A grid turns "where have I been?" into set membership, makes overlaps free and gives you a stable denominator.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Let &lt;code&gt;RETURNING&lt;/code&gt; tell you what changed.&lt;/strong&gt; &lt;code&gt;INSERT ... ON CONFLICT DO NOTHING RETURNING&lt;/code&gt; answers "which of these are new?" in one race-free round trip.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Put the geometry on the server and send one shape.&lt;/strong&gt; &lt;code&gt;ST_Union&lt;/code&gt; plus an even-odd fill beats thousands of client-side rectangles.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bank progress in a counter and pay out in whole units.&lt;/strong&gt; Thresholds like "every 60 tiles" stay atomic inside one transaction and never double-pay.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Derive badges from facts.&lt;/strong&gt; Definitions in code evaluated against live counts can't drift out of sync with the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Content-address static tiles.&lt;/strong&gt; A hash in the manifest lets clients cache forever and lets you redeploy without invalidation headaches.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Stack in this post: Go, PostgreSQL with PostGIS, geohash, OpenStreetMap with Overpass and osmium, Python, three.js, Flutter. Figures come from the backend's code and baked map manifests, the open-world design spec and the app's exploration code, March to November 2026.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  More from Building at CosQ
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-architecture" rel="noopener noreferrer"&gt;Kurukuru: A Ride-Sharing Game on One Go Binary&lt;/a&gt;: the architecture of a college ride-sharing game in one Go monolith.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-realtime-matching" rel="noopener noreferrer"&gt;Matching Riders in Real Time over WebSockets&lt;/a&gt;: live and scheduled ride matching over a Go WebSocket hub.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-pixel-map-app" rel="noopener noreferrer"&gt;Turning a Real Map into a Pixel World&lt;/a&gt;: the Kurukuru app's map, 3D world and pixel UI in Flutter.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-quests-and-road-watch" rel="noopener noreferrer"&gt;Quest Books, an In-App Editor and Road Watch&lt;/a&gt;: game content you can edit in the app, and road safety reports.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/cosq-ecosystem" rel="noopener noreferrer"&gt;Everything Else I Built at CosQ&lt;/a&gt;: WHAM! OTT, MemoChan, PreviouslyOn, Gothicco and more.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postgis</category>
      <category>gamedev</category>
      <category>maps</category>
      <category>go</category>
    </item>
    <item>
      <title>Kurukuru: A Ride-Sharing Game on One Go Binary</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:10 +0000</pubDate>
      <link>https://dev.to/aharshit123456/kurukuru-a-ride-sharing-game-on-one-go-binary-42jk</link>
      <guid>https://dev.to/aharshit123456/kurukuru-a-ride-sharing-game-on-one-go-binary-42jk</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/kurukuru-architecture" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Two students leave the same hostel gate every morning for the same library. One has a scooter and an empty back seat; the other is about to pay for an auto. A ride-sharing app could pair them, but on a campus the hard part is not the matching maths. It is trust (who is this person?), money (who pays whom, and how much?), and giving people a reason to open the app on a day they don't need a ride. This post is about the backend I built for that app: one Go binary that handles identity, trips, a coin wallet, live matching and a map game.&lt;/p&gt;

&lt;p&gt;Kurukuru is a gamified, college-first, peer-to-peer ride-sharing app I lead at CosQ. A &lt;strong&gt;pilot&lt;/strong&gt; is a student already going somewhere with a seat to share; a &lt;strong&gt;co-pilot&lt;/strong&gt; needs a ride. The app finds a pilot whose route passes near the co-pilot, picks where the co-pilot gets on (&lt;strong&gt;Kuru in&lt;/strong&gt;) and off (&lt;strong&gt;Kuru out&lt;/strong&gt;), and the co-pilot pays the pilot in &lt;strong&gt;KuruCoins&lt;/strong&gt;. Around that sits a pixel-art map of the real campus covered in fog that clears as you move, flags you can plant, quest books of places to find, and levels and badges. The backend, called Honbu, is mine end to end: every commit from the first one in March 2026 to the latest in November.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1&lt;/strong&gt; Go binary serving the app, two staff tools and the map tiles&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;~12.6k&lt;/strong&gt; lines of Go, plus ~5.6k lines of Go tests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;~21 MB&lt;/strong&gt; of baked map tiles embedded in the binary&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F53wijbf8ziu657aotrrq.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F53wijbf8ziu657aotrrq.webp" alt="Onboarding screen explaining the two roles, pilot and co-pilot, and the Kuru in and Kuru out stops" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In the app — the onboarding card that explains the vocabulary the whole backend is built around: pilot, co-pilot, Kuru in and Kuru out.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why one binary
&lt;/h2&gt;

&lt;p&gt;The usual advice for a product with this many moving parts is to split it: an auth service, a trips service, a payments service, a realtime gateway. I went the other way, for three reasons.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One person, one deploy.&lt;/strong&gt; Every service you add is another thing to deploy, monitor, version and keep compatible. With a single developer, the cost of a service boundary is paid entirely in my evenings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The interesting operations cross domains.&lt;/strong&gt; Finishing a trip debits the co-pilot, credits the pilot, writes two ledger rows, marks the trip complete and grants experience points to both riders. Planting a flag inserts the flag, grants XP and coins, and writes a ledger row. In a monolith with one Postgres database, the money movement and state change in each of these is a single SQL transaction (the trip's XP follows in its own small one). Split across services, each becomes a saga with compensation logic, and a bug in it costs real (or at least virtual) money.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Go makes a monolith cheap to keep tidy.&lt;/strong&gt; The standard library's &lt;code&gt;net/http&lt;/code&gt; is enough; there is no web framework. Each domain is a package under &lt;code&gt;internal/&lt;/code&gt; with a &lt;code&gt;RegisterHandlers(mux)&lt;/code&gt; function, and &lt;code&gt;main.go&lt;/code&gt; is little more than a list of those calls. Packages can only reach each other through exported functions, so the boundaries are real even though they share a process.&lt;/p&gt;

&lt;p&gt;Flutter app Supabase login → &lt;strong&gt;Honbu (Go) one http.ServeMux, ~20 domain packages&lt;/strong&gt; → Postgres + PostGIS (managed, Supabase)&lt;/p&gt;

&lt;p&gt;Staff web tools quest editor, admin → &lt;strong&gt;same binary separate staff logins&lt;/strong&gt; → Routing service (OSRM, private network)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — the whole system. The app talks only to Honbu; Supabase is used for sign-in and as the database, never as a data API from the app.&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;What it owns&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;auth&lt;/code&gt;, &lt;code&gt;cohort&lt;/code&gt;, &lt;code&gt;staffauth&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;verifying app logins, deciding a user's matching pool, staff tool logins&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;rides&lt;/code&gt;, &lt;code&gt;trip&lt;/code&gt;, &lt;code&gt;copilot&lt;/code&gt;, &lt;code&gt;pilot&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;ride plans, matching, the trip lifecycle, the live trip socket, pilot availability&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;wallet&lt;/code&gt;, &lt;code&gt;payments&lt;/code&gt;, &lt;code&gt;progress&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;coin pricing and ledger, coin top-ups and cash-out, XP, levels and badges&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;exploration&lt;/code&gt;, &lt;code&gt;voxel&lt;/code&gt;, &lt;code&gt;routing&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;fog of war and flags, baked map tiles, road paths&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;quests&lt;/code&gt;, &lt;code&gt;editor&lt;/code&gt;, &lt;code&gt;roadwatch&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;quest books, their collaborative editor, road problem reports&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;features&lt;/code&gt;, &lt;code&gt;admin&lt;/code&gt;, &lt;code&gt;account&lt;/code&gt;, &lt;code&gt;verification&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;feature switches, staff panel with audit log, account deletion, pilot verification&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Shared plumbing lives in &lt;code&gt;pkg/&lt;/code&gt;: the database pool and the WebSocket hub.&lt;/p&gt;

&lt;h2&gt;
  
  
  A walled garden on top of someone else's login
&lt;/h2&gt;

&lt;p&gt;The product promise was that you only ride with people from your own college. That makes identity the most security-sensitive part of the system, and I didn't want to own passwords or email verification. So sign-in is delegated to Supabase Auth (email with confirmation, or Google), and Honbu's job is to &lt;strong&gt;verify&lt;/strong&gt; the token the app sends, not to issue it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verifying someone else's tokens
&lt;/h3&gt;

&lt;p&gt;A JSON Web Token is a signed claim: "this is user &lt;code&gt;sub&lt;/code&gt;, valid until &lt;code&gt;exp&lt;/code&gt;". The signature can be symmetric (HS256: both sides share a secret) or asymmetric (ES256: the issuer signs with a private key and publishes the public half as a &lt;strong&gt;JWKS&lt;/strong&gt;, a JSON Web Key Set, at a well-known URL). Asymmetric is the better fit for a third-party issuer because the verifier never holds anything that can mint tokens.&lt;/p&gt;

&lt;p&gt;Supabase signs access tokens with ES256, so Honbu's verifier looks at the token header, picks a key by its &lt;code&gt;kid&lt;/code&gt; (key id) from a cached JWKS, and verifies. HS256 tokens are still accepted under a server-side secret, which is what the end-to-end test suite uses to impersonate users without a real login.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Alg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"ES256"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;kid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"kid"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;jwksKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// cached; refetched at most every 30 s on an unknown kid&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"HS256"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"unexpected signing method %v"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"alg"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WithValidMethods&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"ES256"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"HS256"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter. &lt;code&gt;WithValidMethods&lt;/code&gt; pins the algorithms, which closes the classic "alg: none" and algorithm-confusion attacks. And the JWKS cache refetches on an unknown &lt;code&gt;kid&lt;/code&gt; (so key rotation just works) but at most once every 30 seconds, so a flood of tokens with random key ids can't turn Honbu into a request amplifier against the identity provider.&lt;/p&gt;

&lt;h3&gt;
  
  
  The endpoint I deleted
&lt;/h3&gt;

&lt;p&gt;The first version of Honbu did run its own &lt;code&gt;register&lt;/code&gt; and &lt;code&gt;login&lt;/code&gt; endpoints, minting its own HS256 tokens. Registration only checked that the email &lt;em&gt;string&lt;/em&gt; ended with the college domain. It never proved the person owned that mailbox, so anyone could create an account for any student's address and receive a token Honbu fully trusted. The campus-only gate was a text filter.&lt;/p&gt;

&lt;p&gt;The fix was to stop serving those routes entirely, and a test now asserts they stay unregistered. Identity comes only from the provider, which only issues a session after the mailbox is confirmed. Removing code was the security fix.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cohorts: the college is derived, never stored
&lt;/h3&gt;

&lt;p&gt;Later the app opened to anyone with an email address, while keeping students of one college matched only with each other. Everyone else lands in a public pool called the &lt;strong&gt;Kurukuru Commune&lt;/strong&gt;. The rule is one SQL view:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;user_cohort&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;college_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email_confirmed_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="k"&gt;LATERAL&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;colleges&lt;/span&gt; &lt;span class="n"&gt;cc&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nb"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;split_part&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'@'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;domain&lt;/span&gt;
       &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="nb"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;split_part&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'@'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%.'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;domain&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nb"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;domain&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="kc"&gt;TRUE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three choices are deliberate. The email comes from the &lt;strong&gt;auth provider's own table&lt;/strong&gt;, not from the profile the app sends, so a client can't claim a college. Only a &lt;strong&gt;confirmed&lt;/strong&gt; email counts. And the domain match is anchored on a dot, so &lt;code&gt;cs.college.ac.in&lt;/code&gt; belongs to the college but &lt;code&gt;college.ac.in.evil.com&lt;/code&gt; does not. Because the cohort is computed rather than stored, adding a college to the &lt;code&gt;colleges&lt;/code&gt; table takes effect for existing users immediately.&lt;/p&gt;

&lt;p&gt;Every matching query then joins the view and requires &lt;code&gt;college_id IS NOT DISTINCT FROM&lt;/code&gt; the searcher's own. &lt;code&gt;IS NOT DISTINCT FROM&lt;/code&gt; is the null-safe equality: two Commune members (both &lt;code&gt;NULL&lt;/code&gt;) match each other, which plain &lt;code&gt;=&lt;/code&gt; would not do.&lt;/p&gt;

&lt;h3&gt;
  
  
  Staff logins with separate audiences
&lt;/h3&gt;

&lt;p&gt;The two staff tools, a quest book editor and an admin panel, do get tokens minted by Honbu: a bcrypt-checked shared password per tool, then a short-lived HS256 token. Each tool has its own password hash, its own signing secret and its own &lt;code&gt;aud&lt;/code&gt; (audience) claim, so an editor token is useless on the admin API and neither works on the app API. Failed logins are rate limited both per client address and across all addresses, so rotating IPs doesn't buy unlimited guesses. Every admin change and every user lookup goes to an audit table.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trip lifecycle as compare-and-set
&lt;/h2&gt;

&lt;p&gt;A trip moves through a small state machine, and every transition is a conditional &lt;code&gt;UPDATE&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;matched both sides agreed → in_progress either rider starts → &lt;strong&gt;completed coins settle, XP granted&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 2 — the main path. An older on-demand flow also enters as &lt;code&gt;requested&lt;/code&gt; and moves to &lt;code&gt;in_progress&lt;/code&gt; when a pilot accepts; any open state can end as &lt;code&gt;canceled&lt;/code&gt;.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;trips&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'in_progress'&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'matched'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pilot_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;copilot_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If zero rows change, the trip was not in the expected state or the caller is not on it, and the API answers 409. This is optimistic concurrency without a version column: the &lt;code&gt;WHERE&lt;/code&gt; clause &lt;em&gt;is&lt;/em&gt; the precondition, and Postgres evaluates it atomically. Two taps on "start" can't both succeed, and nobody can start a trip they aren't part of.&lt;/p&gt;

&lt;p&gt;Completion is the one transition that needs more than a single statement. It locks the trip row with &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;, re-checks the riders and status, prices the trip from the stored distance (never from the client), locks both balances, writes the debit, the credit and two ledger rows, and finally flips the status with the same conditional-update trick. If anything fails, the whole thing rolls back.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn7ky11yzahrcwzd4k1cb.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn7ky11yzahrcwzd4k1cb.webp" alt="Trip complete screen showing coins spent, the remaining vault balance and XP gained toward the next level" width="390" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In the app — the end of a trip as a co-pilot sees it, rendered from test data: coins out of the vault and XP toward the next level, both from the same server response.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When "active" needed a time window
&lt;/h3&gt;

&lt;p&gt;One surprise came from scheduled rides. Confirming a repeating ride creates one &lt;code&gt;matched&lt;/code&gt; trip per shared day, up front, so chat and ratings work the same as for a live ride. But the home screen asked for "the newest trip that isn't finished", and a ride three days away suddenly showed up as a live trip with a pilot en route. The fix was to define &lt;em&gt;active&lt;/em&gt; in time: a trip linked to a match counts only from an hour before pickup to three hours after, or whenever it is actually in progress. "Open" and "happening now" are different questions, and the schema had only answered the first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coins in one paragraph
&lt;/h2&gt;

&lt;p&gt;KuruCoins are the app's currency. Fares are priced from fuel cost (distance over typical two-wheeler mileage times the local petrol price, with a five-coin minimum), so a pilot is reimbursed roughly what the trip cost them. Balances live on the user row, and every movement also appends a row to a &lt;code&gt;wallet_transactions&lt;/code&gt; ledger carrying the delta and the balance after it, written in the same transaction as the balance change and under row locks, so a debit is refused rather than allowed to go below zero. Coins also come from playing: exploring and planting flags pay a few. Buying coins through a payment gateway and cashing them out exist in the code but are dark-launched: off unless switched on, and in test mode the app says plainly that no real money moves. A cash-out only debits coins and queues a request for a person to pay out by hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  One database doing five jobs
&lt;/h2&gt;

&lt;p&gt;Postgres with the PostGIS extension is the only stateful component, and it does more than store rows:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Job&lt;/th&gt;
&lt;th&gt;Postgres feature&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Nearest available pilot&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;geography&lt;/code&gt; points with &lt;code&gt;ST_DWithin&lt;/code&gt; and &lt;code&gt;ST_Distance&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fog of war&lt;/td&gt;
&lt;td&gt;geohash tiles as polygons, merged with &lt;code&gt;ST_Union&lt;/code&gt; and served as GeoJSON&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serialising live matching&lt;/td&gt;
&lt;td&gt;a transaction-scoped advisory lock&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Background job queue&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt; on a jobs table&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Race-proof confirmations&lt;/td&gt;
&lt;td&gt;partial unique indexes (one confirmed match per series)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;None of these needed a new piece of infrastructure: no Redis for locks, no queue broker, no separate geo service. The trade-off is that the database is a single point of failure and the main scaling limit. For a campus-scale app that is the right trade.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploy shape
&lt;/h2&gt;

&lt;p&gt;The deploy is deliberately boring. A multi-stage Dockerfile compiles a static binary (&lt;code&gt;CGO_ENABLED=0&lt;/code&gt;) and copies it into a small Alpine image with CA certificates and time-zone data. A container PaaS runs it and injects &lt;code&gt;PORT&lt;/code&gt;. The routing server runs as a second, private service that only Honbu can reach. Configuration is environment variables. A missing database URL produces a clear error at boot naming the variable and the kind of URL to use, never a silent fallback to a default connection string in the source.&lt;/p&gt;

&lt;p&gt;Two details were learned the hard way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The database's direct host was IPv6-only&lt;/strong&gt;, unreachable from Docker on many laptops and from the PaaS. Connecting through the provider's session pooler fixed both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Static data rides inside the binary.&lt;/strong&gt; Baked map tiles for the campus and a whole city, region files and editor data are compiled in with &lt;code&gt;go:embed&lt;/code&gt;. A deploy can never ship code that expects tiles it doesn't have, and serving a tile is a read from memory with an &lt;code&gt;ETag&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two health endpoints: &lt;code&gt;/health&lt;/code&gt; says the process is up, and &lt;code&gt;/health/db&lt;/code&gt; pings the pool and runs a read-only count on the ledger table, so a broken connection string shows up as a failing check rather than as the first user's error. Feature switches are read from environment variables, overridable at runtime from the admin panel with a ten-second cache; if the flags table is unreachable, the environment value wins, so the app never loses its switches because of a database blip.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the monolith costs
&lt;/h2&gt;

&lt;p&gt;It isn't free. Handlers reach a package-level database handle instead of receiving it, which makes some handlers harder to unit test; the newer packages take a store interface instead and test against an in-memory fake plus a real PostGIS database. The WebSocket hub keeps its subscribers in process memory, so two replicas would each see only half the clients of a trip (the background matcher, by contrast, was built to run on several instances at once). And a sibling project's small API shares the binary and the database, which is convenient until its traffic or its bugs affect Kurukuru. I'd split along those seams first, and only when one of them hurts.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd reuse
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Start with one binary and real package boundaries.&lt;/strong&gt; Cross-domain operations like "finish trip, move money, grant XP" stay a single transaction for as long as you can keep them there.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delegate identity, verify strictly.&lt;/strong&gt; Pin the accepted algorithms, cache the JWKS, throttle refetches. Never let your own endpoint mint tokens the identity provider wouldn't.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Derive membership from the authority, not the client.&lt;/strong&gt; A view over the provider's confirmed email is harder to fool than a profile field, and it updates itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write state transitions as conditional updates.&lt;/strong&gt; &lt;code&gt;UPDATE ... WHERE status = 'expected'&lt;/code&gt; plus a row count is a concurrency control you get for free.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use the database's own tools before adding infrastructure.&lt;/strong&gt; Advisory locks, &lt;code&gt;SKIP LOCKED&lt;/code&gt;, partial unique indexes and PostGIS covered locks, queues, race safety and geo queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Close the doors your platform opens.&lt;/strong&gt; If your server is the only client, turn the managed data API off with deny-by-default security.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Stack in this post: Go (net/http, gorilla/websocket, golang-jwt, lib/pq), PostgreSQL with PostGIS on Supabase, Supabase Auth, OSRM, Docker, Flutter. Figures come from the backend repository's code, schema and history, March to November 2026.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  More from Building at CosQ
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-realtime-matching" rel="noopener noreferrer"&gt;Matching Riders in Real Time over WebSockets&lt;/a&gt;: live and scheduled ride matching over a Go WebSocket hub.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-fog-of-war" rel="noopener noreferrer"&gt;Fog of War on a Real Map with PostGIS&lt;/a&gt;: turning GPS trails into an explorable, game-like map.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-pixel-map-app" rel="noopener noreferrer"&gt;Turning a Real Map into a Pixel World&lt;/a&gt;: the Kurukuru app's map, 3D world and pixel UI in Flutter.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/kurukuru-quests-and-road-watch" rel="noopener noreferrer"&gt;Quest Books, an In-App Editor and Road Watch&lt;/a&gt;: game content you can edit in the app, and road safety reports.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/cosq-ecosystem" rel="noopener noreferrer"&gt;Everything Else I Built at CosQ&lt;/a&gt;: WHAM! OTT, MemoChan, PreviouslyOn, Gothicco and more.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>go</category>
      <category>postgres</category>
      <category>architecture</category>
      <category>backend</category>
    </item>
    <item>
      <title>Hands-on Quantum Programming at IISER Bhopal</title>
      <dc:creator>Harshit Agarwal</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:41:06 +0000</pubDate>
      <link>https://dev.to/aharshit123456/hands-on-quantum-programming-at-iiser-bhopal-1p46</link>
      <guid>https://dev.to/aharshit123456/hands-on-quantum-programming-at-iiser-bhopal-1p46</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.aharshit123456.space/blog/iiser-bhopal-quantum-workshop" rel="noopener noreferrer"&gt;aharshit123456.space&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In June 2024, in the middle of my NISER internship, I attended the NPTEL workshop "Hands on programming on Quantum computers" at IISER Bhopal, run by the institute's QuCIS group: five days of PennyLane notebooks, from single qubits to Grover's search and real hardware models.&lt;/p&gt;

&lt;p&gt;This post covers what the workshop taught, then goes deep on the parts I worked through and kept notes on: Bell states and measurement, universal gate sets, and the Deutsch-Jozsa algorithm derived from scratch. My notes from that week had mistakes in them, and finding them taught me as much as the notebooks did, so I list those too. It is written for engineers who know linear algebra and Python but have never touched a quantum circuit.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;5&lt;/strong&gt; tutorial days in the workshop repo&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;20&lt;/strong&gt; Jupyter notebooks across those days&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;97&lt;/strong&gt; commits by the two organisers, 8 to 22 June 2024&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;0.36.0&lt;/strong&gt; PennyLane version pinned in requirements.txt&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The notebooks, slides and figures are the workshop's material, written by the QuCIS organisers (credits at the end). I did not author any of it. Code below is short excerpts adapted from those notebooks, and each one says which notebook it comes from. The derivations and the corrections are mine, re-checked for this post. On dates: the repo README says "17 Jul - 22 Jul 2024", but the folder is named &lt;code&gt;nptel_jun'24&lt;/code&gt;, every commit is from June, and my own notes page for the workshop is linked from my journal on 18 and 20 June 2024 (the 20 June entry lists Deutsch, Bernstein-Vazirani and the qutrit version as that day's work). So I am confident it ran in June 2024, and I read "Jul" as a typo.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What the week covered
&lt;/h2&gt;

&lt;p&gt;The repo is organised as one folder per day. This is the structure as committed:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Day&lt;/th&gt;
&lt;th&gt;Notebooks&lt;/th&gt;
&lt;th&gt;Topics&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;
&lt;code&gt;Introduction_pennylane&lt;/code&gt;, &lt;code&gt;double_slit&lt;/code&gt;, &lt;code&gt;random_walk_quantum_coin&lt;/code&gt;, &lt;code&gt;solution1&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Qubit as a vector, unitaries, Bloch sphere, circuit depth, the measurement postulate, PennyLane devices and QNodes, a random walk driven by a Hadamard "coin"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Bell_state&lt;/code&gt;, &lt;code&gt;teleportation&lt;/code&gt;, &lt;code&gt;superdense&lt;/code&gt;, &lt;code&gt;intro_algorithms&lt;/code&gt;, &lt;code&gt;solution2&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Bell states, GHZ states and depth, no-cloning, teleportation, superdense coding, phase oracles ("breaking a lock")&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;deuch_jozha_algo&lt;/code&gt;, &lt;code&gt;bernstein-vazzirani&lt;/code&gt;, &lt;code&gt;qutrits_bernstein_vazzirani&lt;/code&gt;, &lt;code&gt;circuit_topology&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Deutsch-Jozsa, Bernstein-Vazirani on qubits and qutrits, hardware connectivity and SWAP routing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Grover_search&lt;/code&gt;, &lt;code&gt;brain_storming_problem&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Grover's search, a mini-hackathon (gate identities, binary to Gray code, a 3-bit mapping)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ahs-aquila-pennylane&lt;/code&gt;, &lt;code&gt;neutral-atom&lt;/code&gt;, &lt;code&gt;strawberry-fields-tutorial&lt;/code&gt;, &lt;code&gt;npteljune2024_sf1&lt;/code&gt;, &lt;code&gt;npteljune2024_sf2&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Analog Hamiltonian simulation on neutral atoms (Rydberg drive, QuEra Aquila via Amazon Braket), continuous-variable photonics in Strawberry Fields&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Day 5 includes analog Hamiltonian simulation: a chain of three atoms driven with &lt;code&gt;qml.pulse.rydberg_drive&lt;/code&gt;, with a section on running it on Aquila. I don't cover day 5 here.&lt;/p&gt;

&lt;h2&gt;
  
  
  The minimum math: qubits, gates, measurement
&lt;/h2&gt;

&lt;p&gt;A qubit's state is a unit vector in ℂ². Everything else follows from three rules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State.&lt;/strong&gt; Two complex amplitudes, normalised:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|0⟩ = [1, 0]ᵀ        |1⟩ = [0, 1]ᵀ
|ψ⟩ = α|0⟩ + β|1⟩,   |α|² + |β|² = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because a global phase is unobservable, any single-qubit state can be written with two real angles, which is what the Bloch sphere draws:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tex"&gt;&lt;code&gt;|ψ⟩ = cos(θ/2)|0⟩ + e^{iφ} sin(θ/2)|1⟩,   θ ∈ [0, π],  φ ∈ [0, 2π)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;|0⟩&lt;/code&gt; is the north pole, &lt;code&gt;|1⟩&lt;/code&gt; the south pole, &lt;code&gt;|+⟩ = (|0⟩ + |1⟩)/√2&lt;/code&gt; is on the equator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gates.&lt;/strong&gt; Operations are unitary matrices (U†U = I), because they must keep the vector normalised. The ones used all week:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;X = [[0, 1],     Z = [[1,  0],     H = 1/√2 · [[1,  1],
     [1, 0]]          [0, -1]]                  [1, -1]]

S = [[1, 0],     T = [[1, 0         ],
     [0, i]]          [0, e^{iπ/4}]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;H maps &lt;code&gt;|0⟩ → |+⟩&lt;/code&gt; and &lt;code&gt;|1⟩ → |−⟩&lt;/code&gt;. Two qubits live in ℂ² ⊗ ℂ² with basis &lt;code&gt;|00⟩, |01⟩, |10⟩, |11⟩&lt;/code&gt;; CNOT flips the second qubit when the first is 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Measurement.&lt;/strong&gt; The day-1 notebook states the general postulate from Nielsen and Chuang. A measurement is a set of operators {M_m}. Outcome m happens with probability&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p(m) = ⟨ψ| M_m† M_m |ψ⟩
state after = M_m|ψ⟩ / √p(m)
completeness:  Σ_m M_m† M_m = I
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the usual computational-basis measurement, M₀ = |0⟩⟨0| and M₁ = |1⟩⟨1|, so p(0) = |α|² and p(1) = |β|².&lt;/p&gt;

&lt;p&gt;The first item on my doubts list just says "Completeness problem", and this equation is where it comes from. Summing p(m) over all outcomes gives ⟨ψ| (Σ M_m† M_m) |ψ⟩, which equals 1 for every normalised state exactly when Σ M_m† M_m = I. So completeness is not an extra axiom: it is "probabilities add up to one", written for operators. The notebook's example, M₁ = |+⟩⟨+| and M₂ = |−⟩⟨−|, satisfies it because {|+⟩, |−⟩} is an orthonormal basis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Depth&lt;/strong&gt; is the number of time steps when every gate that can run in parallel does; gates on disjoint qubits share a layer. It matters because real qubits decohere with time.&lt;/p&gt;

&lt;h2&gt;
  
  
  PennyLane in one screen: devices and QNodes
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;quantum function&lt;/strong&gt; is a plain Python function that calls gates and returns a measurement. A &lt;strong&gt;device&lt;/strong&gt; is where it runs (&lt;code&gt;default.qubit&lt;/code&gt; is the built-in simulator; plugins reach Cirq, Braket and hardware). A &lt;strong&gt;QNode&lt;/strong&gt;, usually made with the &lt;code&gt;@qml.qnode(dev)&lt;/code&gt; decorator, binds the two. Adapted from &lt;code&gt;Introduction_pennylane.ipynb&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pennylane&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;qml&lt;/span&gt;

&lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;device&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"default.qubit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;qnode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;circuit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;theta&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Hadamard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CNOT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;RZ&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;theta&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;probs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;circuit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# probabilities of |00&amp;gt;, |01&amp;gt;, |10&amp;gt;, |11&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;draw_mpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;circuit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# draws the circuit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details that bit me later. Gates execute in written order, which is the reverse of the order in a matrix product. And &lt;code&gt;qml.probs&lt;/code&gt; gives exact probabilities, while &lt;code&gt;qml.sample&lt;/code&gt; and &lt;code&gt;qml.counts&lt;/code&gt; need a device created with &lt;code&gt;shots=...&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bell states and what measuring one qubit does
&lt;/h2&gt;

&lt;p&gt;The day-2 notebook builds the first entangled state with two gates.&lt;/p&gt;

&lt;p&gt;q0: |0⟩ → &lt;strong&gt;H&lt;/strong&gt; → CNOT control ● → measure&lt;/p&gt;

&lt;p&gt;q1: |0⟩ → idle → CNOT target ⊕ → measure&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 1 — the Bell-state circuit, one row per qubit, time running left to right.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Step by step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|00⟩  --H on q0--&amp;gt;  (|0⟩ + |1⟩)/√2 ⊗ |0⟩ = (|00⟩ + |10⟩)/√2
      --CNOT--&amp;gt;     (|00⟩ + |11⟩)/√2 = |Φ⁺⟩
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The four Bell states, and the gates that make each from &lt;code&gt;|00⟩&lt;/code&gt; (from my notes, verified by multiplying the matrices out):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;th&gt;Vector&lt;/th&gt;
&lt;th&gt;Gates, in order&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;|Φ⁺⟩&lt;/td&gt;
&lt;td&gt;(|00⟩ + |11⟩)/√2&lt;/td&gt;
&lt;td&gt;H(0), CNOT(0,1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;|Φ⁻⟩&lt;/td&gt;
&lt;td&gt;(|00⟩ − |11⟩)/√2&lt;/td&gt;
&lt;td&gt;H(0), CNOT(0,1), Z(0)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;|Ψ⁺⟩&lt;/td&gt;
&lt;td&gt;(|01⟩ + |10⟩)/√2&lt;/td&gt;
&lt;td&gt;X(1), H(0), CNOT(0,1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;|Ψ⁻⟩&lt;/td&gt;
&lt;td&gt;(|01⟩ − |10⟩)/√2&lt;/td&gt;
&lt;td&gt;X(1), H(0), CNOT(0,1), Z(0)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The Z at the end works because Z on qubit 0 multiplies every term whose first bit is 1 by −1. In &lt;code&gt;|Ψ⁺⟩&lt;/code&gt; that is the &lt;code&gt;|10⟩&lt;/code&gt; term, which gives &lt;code&gt;|Ψ⁻⟩&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In PennyLane, from &lt;code&gt;Bell_state.ipynb&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;device&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"default.qubit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shots&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;qnode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Hadamard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CNOT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# only '00' and '11' appear, roughly 500 each&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second doubt on my list was exactly this: "find the probability distribution of the states when the first qubit of a two-qubit Bell state is measured". Measuring only the first qubit uses M₀ = |0⟩⟨0| ⊗ I and M₁ = |1⟩⟨1| ⊗ I:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p(first = 0) = ⟨Φ⁺| (|0⟩⟨0| ⊗ I) |Φ⁺⟩ = |1/√2|² = 1/2   → state becomes |00⟩
p(first = 1) = ⟨Φ⁺| (|1⟩⟨1| ⊗ I) |Φ⁺⟩ = |1/√2|² = 1/2   → state becomes |11⟩
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first qubit is a fair coin; afterwards the second matches it (for the Ψ states, it is the opposite bit). What confused me was whether this lets one side "send" something. It doesn't: looked at alone, the second qubit is 50/50 whether or not the first was measured. The correlation only appears when results are compared over a classical channel, which is why teleportation, later that day, needs Alice to send two classical bits. Nor is it a computational-basis trick: &lt;code&gt;|Φ⁺⟩ = (|++⟩ + |−−⟩)/√2&lt;/code&gt;, so outcomes also match in the X basis.&lt;/p&gt;

&lt;h3&gt;
  
  
  GHZ states and depth
&lt;/h3&gt;

&lt;p&gt;Exercise 2.2 asks for &lt;code&gt;(|00000⟩ + |11111⟩)/√2&lt;/code&gt;. H on qubit 0 then four CNOTs from qubit 0 works, but they all share qubit 0, so the depth is 5. Exercise 2.3 asks for less, and the solution notebook builds a tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Hadamard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CNOT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CNOT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CNOT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# can run alongside CNOT(2, 4)&lt;/span&gt;
&lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CNOT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By my count that is depth 4, and it is optimal: each CNOT layer can at most double the number of qubits sharing the bit (1, 2, 4, 8), so 5 qubits need three CNOT layers plus the H. In general, 1 + ⌈log₂ n⌉ instead of n.&lt;/p&gt;

&lt;h2&gt;
  
  
  Universal gate sets
&lt;/h2&gt;

&lt;p&gt;The third doubt was "Universal Quantum Gate Set Problem": how do you build an arbitrary unitary from gates you actually have? My notes on this were partly wrong, so here is the version I'd keep.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: any single-qubit unitary is three rotations.&lt;/strong&gt; Nielsen and Chuang (Theorem 4.1) show every 2×2 unitary can be written as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;U = e^{iα} R_z(β) R_y(γ) R_z(δ)

R_z(φ) = [[e^{-iφ/2}, 0        ],      R_y(θ) = [[cos(θ/2), -sin(θ/2)],
          [0,         e^{iφ/2}]]                  [sin(θ/2),  cos(θ/2)]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The U3 gate packages this with the global phase dropped:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;U3(θ, φ, λ) = [[cos(θ/2),          -e^{iλ} sin(θ/2)     ],
               [e^{iφ} sin(θ/2),    e^{i(φ+λ)} cos(θ/2)  ]]

            = e^{i(φ+λ)/2} · R_z(φ) R_y(θ) R_z(λ)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I checked the second line numerically; the phase in front never changes a measurement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: read the angles off a given matrix.&lt;/strong&gt; Write the target as U = [[a, b], [c, d]] = e^{iα} U3(θ, φ, λ). Matching entries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = e^{iα} cos(θ/2)
b = -e^{i(α+λ)} sin(θ/2)
c = e^{i(α+φ)} sin(θ/2)

⇒  θ = 2·arccos|a|
    α = arg(a)
    φ = arg(c) − α
    λ = arg(−b) − α
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a = 0 (θ = π) take the phase from c instead. When b = c = 0 (θ = 0), only φ + λ is defined, and any split works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where my notes went wrong&lt;/strong&gt;, in three easy-to-make ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;My notes had λ = arg(−b) − arg(c). With α = 0 that equals λ − φ, not λ. The correct formula subtracts arg(a).&lt;/li&gt;
&lt;li&gt;The PennyLane code in my notes applied &lt;code&gt;RZ(phi)&lt;/code&gt;, then &lt;code&gt;RY(theta)&lt;/code&gt;, then &lt;code&gt;RZ(lambda)&lt;/code&gt;. Since PennyLane applies gates in written order, that circuit's matrix is R_z(λ) R_y(θ) R_z(φ), the reverse of what was intended. To get R_z(φ) R_y(θ) R_z(λ), write &lt;code&gt;RZ(lambda)&lt;/code&gt; first, or use &lt;code&gt;qml.Rot(lambda, theta, phi)&lt;/code&gt;, which is defined as RZ(ω)RY(θ)RZ(φ) on its arguments (φ, θ, ω).&lt;/li&gt;
&lt;li&gt;The test matrix in my notes was [[0.5, −0.5], [0.5, 0.5]]. That is not unitary: U†U = I/2. The entries should be ±1/√2, which gives R_y(π/2).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 3: finite gate sets.&lt;/strong&gt; Single-qubit unitaries plus CNOT are exactly universal. Hardware offers a finite menu, and a finite set produces only countably many circuits, so it can only approximate. Which sets are enough?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;{H, S, CNOT}&lt;/strong&gt; generates the Clifford group: not universal, and by the Gottesman-Knill theorem classically simulable from basis states.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clifford + T&lt;/strong&gt; ({H, S, CNOT, T}) is universal. Since T² = S, {H, T, CNOT} is the same set.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cost.&lt;/strong&gt; By the Solovay-Kitaev theorem, accuracy ε takes O(logᶜ(1/ε)) gates, with c around 4 (Dawson and Nielsen give about 3.97).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My notes also built the six cardinal states: &lt;code&gt;|1⟩&lt;/code&gt; (X), &lt;code&gt;|+⟩&lt;/code&gt; (H), &lt;code&gt;|−⟩&lt;/code&gt; (H then Z), &lt;code&gt;|i⟩&lt;/code&gt; (H then S), &lt;code&gt;|−i⟩&lt;/code&gt; (H then S†). The S† line used &lt;code&gt;qml.S(wires=0).inv()&lt;/code&gt;, an old PennyLane API since removed; today it is &lt;code&gt;qml.adjoint(qml.S)(wires=0)&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deutsch-Jozsa from scratch
&lt;/h2&gt;

&lt;p&gt;This is the part I spent most time on; my notes page for it runs to about 3,200 words. Here it is, cleaned up and re-checked.&lt;/p&gt;

&lt;h3&gt;
  
  
  The problem
&lt;/h3&gt;

&lt;p&gt;You get a black box computing f: {0,1}ⁿ → {0,1}. You are promised f is either &lt;strong&gt;constant&lt;/strong&gt; (same output for every input) or &lt;strong&gt;balanced&lt;/strong&gt; (0 on exactly half the inputs, 1 on the other half). Decide which.&lt;/p&gt;

&lt;p&gt;A deterministic classical algorithm could see 0 on the first 2ⁿ⁻¹ inputs and still not know, so it needs 2ⁿ⁻¹ + 1 queries. The quantum algorithm needs one.&lt;/p&gt;

&lt;h3&gt;
  
  
  The oracle
&lt;/h3&gt;

&lt;p&gt;How does a circuit "call" f? The map |x⟩ → |f(x)⟩ is not reversible (for constant f every input lands on the same output), so it cannot be unitary. Instead, keep the input and XOR the answer into an extra qubit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;U_f |x⟩|y⟩ = |x⟩|y ⊕ f(x)⟩
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This permutes the basis states, so it is unitary. It is also its own inverse, because y ⊕ f(x) ⊕ f(x) = y.&lt;/p&gt;

&lt;p&gt;For n = 1, order the basis as |x y⟩ = |00⟩, |01⟩, |10⟩, |11⟩. Column |x y⟩ of the matrix is the basis vector |x, y ⊕ f(x)⟩, which gives&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;U_f = [[1−f(0), f(0),   0,      0     ],
       [f(0),   1−f(0), 0,      0     ],
       [0,      0,      1−f(1), f(1)  ],
       [0,      0,      f(1),   1−f(1)]]

    = |0⟩⟨0| ⊗ X^{f(0)}  +  |1⟩⟨1| ⊗ X^{f(1)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each 2×2 block is I or X: the ancilla flips exactly when f(x) = 1. This is the matrix the workshop notebook's &lt;code&gt;oracle(g)&lt;/code&gt; builds. The four one-bit functions:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;f&lt;/th&gt;
&lt;th&gt;f(0), f(1)&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;U_f&lt;/th&gt;
&lt;th&gt;As gates&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;f(x) = 0&lt;/td&gt;
&lt;td&gt;0, 0&lt;/td&gt;
&lt;td&gt;constant&lt;/td&gt;
&lt;td&gt;I ⊗ I&lt;/td&gt;
&lt;td&gt;nothing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;f(x) = 1&lt;/td&gt;
&lt;td&gt;1, 1&lt;/td&gt;
&lt;td&gt;constant&lt;/td&gt;
&lt;td&gt;I ⊗ X&lt;/td&gt;
&lt;td&gt;X on the ancilla&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;f(x) = x&lt;/td&gt;
&lt;td&gt;0, 1&lt;/td&gt;
&lt;td&gt;balanced&lt;/td&gt;
&lt;td&gt;CNOT&lt;/td&gt;
&lt;td&gt;CNOT(x → y)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;f(x) = 1 ⊕ x&lt;/td&gt;
&lt;td&gt;1, 0&lt;/td&gt;
&lt;td&gt;balanced&lt;/td&gt;
&lt;td&gt;blockdiag(X, I)&lt;/td&gt;
&lt;td&gt;CNOT, then X on the ancilla&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;My notes got the last row wrong: correctly U_f = (I ⊗ X)·CNOT, but then the product was written as the I ⊗ X matrix. It is actually [[0,1,0,0],[1,0,0,0],[0,0,1,0],[0,0,0,1]]: flip the ancilla when x = 0. The habit that catches this is checking each column by asking "where does this basis state go?"&lt;/p&gt;

&lt;h3&gt;
  
  
  Phase kickback
&lt;/h3&gt;

&lt;p&gt;The trick is to put the ancilla in &lt;code&gt;|−⟩ = (|0⟩ − |1⟩)/√2&lt;/code&gt; instead of a basis state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;U_f |x⟩|−⟩ = |x⟩ (|0 ⊕ f(x)⟩ − |1 ⊕ f(x)⟩)/√2

  f(x) = 0:  |x⟩ (|0⟩ − |1⟩)/√2 = +|x⟩|−⟩
  f(x) = 1:  |x⟩ (|1⟩ − |0⟩)/√2 = −|x⟩|−⟩

⇒ U_f |x⟩|−⟩ = (−1)^{f(x)} |x⟩|−⟩
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ancilla comes out unchanged and the answer becomes a sign on the input. A sign on one basis state is invisible, but in a superposition it is a relative phase, and a Hadamard turns relative phases into different outcomes. The day-2 &lt;code&gt;intro_algorithms&lt;/code&gt; notebook makes the same point with its "pair testing" lock: a phase flip is only detectable in the {|+⟩, |−⟩} basis.&lt;/p&gt;

&lt;h3&gt;
  
  
  One input bit, the whole run
&lt;/h3&gt;

&lt;p&gt;x: |0⟩ → &lt;strong&gt;H&lt;/strong&gt; → U_f (input) → &lt;strong&gt;H&lt;/strong&gt; → measure&lt;/p&gt;

&lt;p&gt;y: |1⟩ → &lt;strong&gt;H&lt;/strong&gt; → U_f (ancilla) → discard&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 2 — Deutsch's circuit. The ancilla starts in |1⟩ so that H turns it into |−⟩. U_f spans both wires.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|ψ₀⟩ = |0⟩|1⟩
|ψ₁⟩ = (H ⊗ H)|ψ₀⟩ = |+⟩|−⟩ = (|0⟩ + |1⟩)/√2 ⊗ |−⟩
|ψ₂⟩ = U_f|ψ₁⟩     = ((−1)^{f(0)}|0⟩ + (−1)^{f(1)}|1⟩)/√2 ⊗ |−⟩
                   = (−1)^{f(0)} · (|0⟩ + (−1)^{f(0)⊕f(1)}|1⟩)/√2 ⊗ |−⟩
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first qubit is &lt;code&gt;|+⟩&lt;/code&gt; if f(0) = f(1) and &lt;code&gt;|−⟩&lt;/code&gt; if not, up to a global sign. Since H|+⟩ = |0⟩ and H|−⟩ = |1⟩:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|ψ₃⟩ = (H ⊗ I)|ψ₂⟩ = ±|f(0) ⊕ f(1)⟩ ⊗ |−⟩
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Measure the first qubit: 0 means constant, 1 means balanced, with certainty. One call answered a question about f(0) and f(1) jointly without learning either.&lt;/p&gt;

&lt;h3&gt;
  
  
  n input bits
&lt;/h3&gt;

&lt;p&gt;Two identities do all the work. With N = 2ⁿ and x·z the bitwise inner product mod 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tex"&gt;&lt;code&gt;H^{⊗n} |0…0⟩ = (1/√N) Σ_x |x⟩
H^{⊗n} |x⟩   = (1/√N) Σ_z (−1)^{x·z} |z⟩
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the same circuit with n input qubits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;start:          |0…0⟩|1⟩
after H's:      (1/√N) Σ_x |x⟩ ⊗ |−⟩
after U_f:      (1/√N) Σ_x (−1)^{f(x)} |x⟩ ⊗ |−⟩          (phase kickback)
after H^{⊗n}:   Σ_z [ (1/N) Σ_x (−1)^{f(x) ⊕ x·z} ] |z⟩ ⊗ |−⟩
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now look only at the amplitude of z = 0…0, where x·z = 0 for every x:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;amp(0…0) = (1/N) Σ_x (−1)^{f(x)}

constant f:  every term is the same sign  → amp = ±1  → P(0…0) = 1
balanced f:  N/2 terms +1, N/2 terms −1   → amp = 0   → P(0…0) = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All zeros means constant; anything else means balanced. I checked this numerically for n = 1, 2, 3.&lt;/p&gt;

&lt;h3&gt;
  
  
  The two-input oracle, and the second mistake in my notes
&lt;/h3&gt;

&lt;p&gt;For n = 2 there are three qubits (two inputs, one ancilla), so U_f is 8×8, not 16×16 as my notes said. In general it is 2ⁿ⁺¹ × 2ⁿ⁺¹, and it is always block diagonal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;U_f = Σ_x |x⟩⟨x| ⊗ X^{f(x)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the balanced f(x₁, x₂) = x₁ ⊕ x₂, U_f = blockdiag(I, X, X, I). The 8×8 matrix in my notes for it had the wrong entries, and the notes wrote "f(x₁ ⊕ x₂)", confusing the function with one example of it. You don't need the matrix anyway: two CNOTs, one from each input onto the ancilla, add x₁ ⊕ x₂ to it.&lt;/p&gt;

&lt;p&gt;My notes also said the algorithm decides "with high probability". On ideal hardware it is exact, which is why the workshop notebook uses &lt;code&gt;shots=1&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  In PennyLane
&lt;/h3&gt;

&lt;p&gt;Adapted from &lt;code&gt;deuch_jozha_algo.ipynb&lt;/code&gt;, which passes the oracle in as a matrix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pennylane&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;qml&lt;/span&gt;

&lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;device&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"default.qubit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shots&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;oracle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                     &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                     &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                     &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;qnode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deutsch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;U&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PauliX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="c1"&gt;# ancilla |0&amp;gt; -&amp;gt; |1&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Hadamard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Hadamard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;             &lt;span class="c1"&gt;# ancilla is now |-&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;QubitUnitary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;U&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Hadamard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;deutsch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;oracle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# constant: always 0&lt;/span&gt;
&lt;span class="nf"&gt;deutsch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;oracle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# balanced: always 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;code&gt;qml.PauliX(wires=1)&lt;/code&gt; line is my addition. The notebook's derivation starts from |01⟩, but the committed code applies H to both wires without flipping the ancilla first. An ancilla in |0⟩ becomes |+⟩, an eigenvector of X with eigenvalue +1, so there is no kickback and the circuit returns 0 for all four functions (I checked by multiplying out the matrices). If you run the notebook, add the X.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For n inputs I skip the matrix. My own generalisation, not from the notebook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;device&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"default.qubit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shots&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;qnode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deutsch_jozsa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;oracle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PauliX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Hadamard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;oracle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Hadamard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                      &lt;span class="c1"&gt;# balanced: f(x) = x0 ⊕ x1 ⊕ x2&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CNOT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;constant_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                &lt;span class="c1"&gt;# constant: f(x) = 1&lt;/span&gt;
    &lt;span class="n"&gt;qml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PauliX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;constant_one&lt;/code&gt; returns &lt;code&gt;000&lt;/code&gt;; &lt;code&gt;parity&lt;/code&gt; returns &lt;code&gt;111&lt;/code&gt;. That is the bridge to the Bernstein-Vazirani notebook: for any f(x) = s·x, the amplitude formula puts all the weight on z = s, so one query reads out the hidden string s.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the speed-up is, and isn't
&lt;/h3&gt;

&lt;p&gt;The day-3 notebook calls this an exponential speed-up, and against exact deterministic algorithms it is: 1 query against 2ⁿ⁻¹ + 1. Against randomized ones the gap mostly disappears. Query k random inputs; if f is balanced, the chance all k answers agree is 2·(1/2)ᵏ = 2¹⁻ᵏ, so ten queries push the error below 0.2% for any n. Deutsch-Jozsa is a clean demonstration of interference and phase kickback, not a useful algorithm.&lt;/p&gt;

&lt;h2&gt;
  
  
  Grover's search, briefly
&lt;/h2&gt;

&lt;p&gt;Day 4 follows the PennyLane demo. Mark the solution with a phase oracle, then reflect about the uniform superposition |s⟩:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;U_ω = I − 2|ω⟩⟨ω|          (flip the sign of the marked state)
U_D = 2|s⟩⟨s| − I          (diffusion: reflect about |s⟩)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;H on all wires |s⟩ → Oracle U_ω qml.FlipSign → Diffusion U_D GroverOperator → &lt;strong&gt;repeat r times&lt;/strong&gt; → measure&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fig. 3 — the Grover loop as built in the day-4 notebook.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Two reflections make a rotation. With M marked items out of N and sin θ₀ = √(M/N), after r iterations the probability of measuring a marked item is sin²((2r + 1)θ₀).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;N = 4, M = 1.&lt;/strong&gt; θ₀ = 30°, so one iteration gives sin²(90°) = 1. The notebook's 2-qubit diffusion circuit (H and Z on each wire, controlled-Z, H again) equals 2|s⟩⟨s| − I exactly; I multiplied it out. One trap: the rule r ≈ (π/4)√(N/M) rounds to 2 here, and two iterations overshoot to sin²(150°) = 0.25. The notebook does this case with one iteration by hand, so it is right.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;N = 32, M = 2&lt;/strong&gt; (marking &lt;code&gt;00000&lt;/code&gt; and &lt;code&gt;11111&lt;/code&gt;). r = round((π/4)·4) = 3, and sin²(7·arcsin(1/4)) ≈ 0.961, split between the two marked states.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Real devices: topology and routing
&lt;/h2&gt;

&lt;p&gt;A simulator lets you CNOT any pair of qubits; hardware has a fixed connectivity graph. &lt;code&gt;circuit_topology.ipynb&lt;/code&gt; gives a 9-qubit topology and asks what a CNOT between non-neighbours costs in SWAPs. Qubits 2 and 8 are three edges apart (2, 1, 4, 8): two SWAPs bring them together, the CNOT runs, two more restore the layout. That is 4 SWAPs, or 2(d − 1) for distance d, and each SWAP is 3 CNOTs, so routing can cost more than the algorithm.&lt;/p&gt;

&lt;h2&gt;
  
  
  What tripped me up, and how each resolved
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;From my notes&lt;/th&gt;
&lt;th&gt;What was wrong or unclear&lt;/th&gt;
&lt;th&gt;Resolution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;"Completeness problem"&lt;/td&gt;
&lt;td&gt;Why Σ M_m† M_m = I is required&lt;/td&gt;
&lt;td&gt;It is exactly the condition for probabilities to sum to 1 for every state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Measuring the first qubit of a Bell state&lt;/td&gt;
&lt;td&gt;The distribution, and whether it affects the other qubit&lt;/td&gt;
&lt;td&gt;1/2 and 1/2; the pair collapses to |00⟩ or |11⟩; the second qubit alone stays 50/50, so no signalling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Universal gate set&lt;/td&gt;
&lt;td&gt;How to turn any U into gates&lt;/td&gt;
&lt;td&gt;ZYZ / U3 decomposition, correct angle extraction, then Clifford + T with Solovay-Kitaev&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;U3 extraction code&lt;/td&gt;
&lt;td&gt;Wrong λ, reversed gate order, non-unitary test matrix&lt;/td&gt;
&lt;td&gt;λ = arg(−b) − arg(a); write RZ(λ) first; use ±1/√2 entries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deutsch-Jozsa oracles&lt;/td&gt;
&lt;td&gt;(I ⊗ X)·CNOT written as I ⊗ X; two-input oracle 16×16&lt;/td&gt;
&lt;td&gt;blockdiag(X, I); 8×8, blockdiag(I, X, X, I) for x₁ ⊕ x₂&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Observables&lt;/td&gt;
&lt;td&gt;Claimed outcome λᵢ has probability |aᵢ|² for any expansion of |ψ⟩&lt;/td&gt;
&lt;td&gt;Only when |ψ⟩ is expanded in the eigenbasis of A: p(λᵢ) = |⟨i|ψ⟩|², and ⟨A⟩ = Σ λᵢ p(λᵢ), which is what &lt;code&gt;qml.expval&lt;/code&gt; returns&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Most of my errors came from trusting a derivation that looked right instead of checking one column or one 2×2 product. With matrices this small, that takes a minute.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;The workshop "Hands on programming on Quantum computers" was organised by the QuCIS group at IISER Bhopal as an NPTEL workshop. The repo README credits its authors as &lt;a href="https://github.com/mainak-bhattacharyya" rel="noopener noreferrer"&gt;Mainak Bhattacharyya&lt;/a&gt; and harsh-quantum (Harsh Gupta), whose commits make up the whole repo. The introductory slides list Ankur Raina, Mainak Bhattacharyya, Harsh Gupta, Sanidhya Gupta and Anuprita Kulkarni. The workshop slides link the original repo as mainak-bhattacharyya/QuCIS_outreach, which is no longer public; my copy of the material is at &lt;a href="https://github.com/aharshit123456/QuCIS_outreach" rel="noopener noreferrer"&gt;aharshit123456/QuCIS_outreach&lt;/a&gt;. The Grover notebook follows PennyLane's own demo, and the measurement postulate and gate decomposition follow Nielsen and Chuang, &lt;em&gt;Quantum Computation and Quantum Information&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  More from Research at NISER &amp;amp; IISER Bhopal
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-internship-retrospective" rel="noopener noreferrer"&gt;NISER Internship Retrospective&lt;/a&gt;: going back to SMLAB, and what the May to October 2024 internship actually taught me.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/orb-slam3" rel="noopener noreferrer"&gt;From ORB Features to ORB-SLAM3&lt;/a&gt;: how a modern visual SLAM system is built up from keypoints.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-slam-bayes-filter" rel="noopener noreferrer"&gt;SLAM from the Bayes Filter Up&lt;/a&gt;: the probabilistic foundations under localization and mapping.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-ros-dashboard" rel="noopener noreferrer"&gt;A Browser Dashboard for a ROS 2 Sensor Rig&lt;/a&gt;: watching live sensor topics from a web page.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-iphone-sensor-rig" rel="noopener noreferrer"&gt;Turning an iPhone into a LiDAR, IMU and GPS Sensor&lt;/a&gt;: using a phone as a cheap multi-sensor rig.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.aharshit123456.space/blog/niser-perception-notes" rel="noopener noreferrer"&gt;Field Notes on Perception for Two-Wheeler Autonomy&lt;/a&gt;: computer vision, point clouds and prediction for autonomous two-wheelers, written for the Swayansaasita project site.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>quantum</category>
      <category>quantumcomputing</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
