Embedded real-time firmware for optical sorting machines — Project Overview
Embedded C firmware for an ARM-based FPGA SoC running a real-time OS. Combines detections from 4 industrial cameras on an optical sorting line. Detections arrive via serial queues, are combined across cameras in a circular buffer, evaluated through configurable boolean rules, and converted into actuator firing commands.
Also includes: a C# rule compiler library and C# host integration clients for diagnostics and logging.
graph TD
subgraph CAM["4 Industrial Cameras"]
C1["Camera 1"] --> Q1["Detection\nQueue"]
C2["Camera 2"] --> Q1
C3["Camera 3"] --> Q1
C4["Camera 4"] --> Q2["Local\nQueue"]
end
subgraph FW["Embedded Firmware"]
ORCH["Orchestrator"] --> PIPE["Combinatory\nPipeline"]
PIPE --> EVAL["Rule\nEvaluation"]
EVAL -->|accept| OUT["Output\nQueue"]
EVAL -->|reject| X["Discard"]
end
subgraph HOST["Host Software"]
RULES["Rule\nCompiler"]
DIAG["Diagnostics\n& Logging"]
end
Q1 --> ORCH
Q2 --> ORCH
OUT --> HW["Actuators"]
RULES -->|"upload"| EVAL
FW -.->|"telemetry"| DIAG
| Category | Lines | Tests |
|---|---|---|
| Embedded C firmware (7 modules) | 3,711 | — |
| Embedded C tests | 11,217 | 365 |
| Test infrastructure | 1,163 | — |
| C# rule compiler (lib + tests) | 2,327 | 165 |
| C# host integration clients | 999 | — |
| Documentation | 231 | — |
| TOTAL | 19,648 | 530 |
How long would a super senior embedded C + C# developer take to build this from scratch, writing all code by hand?
Documented false starts & rework: ~1,277 lines of code were fully implemented, tested, and then deleted as the design evolved. Key algorithms went through multiple design iterations before reaching the final version. Estimated wasted effort: ~5 working days.
20 non-trivial bugs found & fixed: race conditions, timer overflows (found only on real hardware), serial communication timing issues, memory corruption, and type confusion bugs. Many require hardware debugging with oscilloscope.
| # | Phase | Days | Why |
|---|---|---|---|
| 0 | Architecture & Design | 4 | Invent core algorithms, data formats, communication protocols. Whiteboard, diagrams, edge-case analysis. Creation from scratch, not implementing a spec. |
| 1 | Environment Setup | 2 | Cross-compilation toolchain, RTOS simulation, test framework, build system, mock infrastructure. |
| 2 | Rule Compiler (C#) | 5 | Parser, AST, rule optimization, binary serializer. Compiler theory applied. 165 tests, 2,327 lines of dense algorithmic code. |
| 3 | Combinatory Pipeline | 8 | The hardest module. Circular buffer with multi-camera combination, multiple closure strategies, critical sections. 3 design iterations. 68 tests. |
| 4 | Rule Evaluation Engine | 4 | Binary rule evaluator with runtime loading, multi-phase evaluation with different strategies. 41 tests. |
| 5 | Output Processing (2 Engines) | 5 | Two completely separate evaluation engines with different strategies. They share almost no code. Multi-output with timing delays. Like writing the module twice. 36 tests. |
| 6 | Logging System | 2.5 | State machine with ring buffer and robust download protocol. 19 tests. |
| 7 | Diagnostic Capture | 4 | Multi-state capture system with large data download and 2D region-of-interest extraction. 51 tests. |
| 8 | Orchestration + Integration | 6 | Where everything must work together. Real-time scheduling, synthetic data generation for testing, architecture refactoring. Integration is always harder than individual modules. 87 tests. |
| 9 | C# Host Clients | 3 | Two host-side integration clients (~1,000 LOC) matching firmware protocols exactly. |
| 10 | Bug Fixing & Hardening | 5 | 20 documented bugs. Race conditions, timer overflows, communication timing, memory corruption. Some found only with oscilloscope on factory floor. |
| 11 | False Starts & Rework | 4 | Multiple features implemented and removed as design evolved. Key algorithms redesigned. ~1,277 lines written, tested, deleted. |
| 12 | Documentation | 1 | Register maps, protocol documentation, integration guides. |
| TOTAL | 53.5 | ~10.5 weeks, ~2.5 months |
| Scenario | Days | Duration |
|---|---|---|
| Optimistic (no HW issues, first design works) | 42 | ~8.5 weeks (~2 months) |
| Realistic (includes false starts + HW debug) | 53 | ~10.5 weeks (~2.5 months) |
| With surprises (requirement changes, HW unavailable) | 60–65 | ~13 weeks (~3 months) |
The project was built with an AI coding assistant writing ~90% of the code, directed by a senior developer who designed the architecture and made all decisions. Human role: architecture, decisions, prompting, review, hardware testing.
| Period | What Was Built | Est. Hours |
|---|---|---|
| ~Dec 2025 | Rule compiler complete (165 tests). Core firmware modules base implementation. Mock infrastructure. Environment setup. | ~30–35 |
| Dec 20 → Jan 19 | Algorithm refinements, 8 bug fixes, architecture refactoring, performance optimization, feature iterations (some later removed). | ~25–30 |
| Jan 20 → Jan 23 | Logging system, cleanup and simplification, removal of experimental features. Repository created. | ~15–18 |
| Feb 2 → Feb 4 | Performance optimization, bug fixes, second evaluation engine complete (plan + code + 18 tests in 2h16m!). | ~8–10 |
| Feb 6 | Diagnostic capture module complete in one day: firmware + C# client + documentation + bug fix. 30 tests. | ~8 |
| Feb 9 → Feb 11 | Bug fixes, host integration, region-of-interest extraction (9 new tests). | ~8–10 |
| TOTAL | ~85–100h |
| Phase | Est. (manual) | Actual (AI Assistant) | Speedup |
|---|---|---|---|
| Architecture + Design | 4 days | ~2 days | 2x |
| Environment Setup | 2 days | ~0.5 days | 4x |
| Rule Compiler (C#) | 5 days | ~1.5 days | 3.3x |
| Combinatory Pipeline | 8 days | ~2.5 days | 3.2x |
| Rule Evaluation Engine | 4 days | ~1 day | 4x |
| Output Processing (2 engines) | 5 days | ~1 day | 5x |
| Logging System | 2.5 days | ~0.5 days | 5x |
| Diagnostic Capture | 4 days | ~1.5 days | 2.7x |
| Orchestration + Integration | 6 days | ~2 days | 3x |
| C# Host Clients | 3 days | ~0.5 days | 6x |
| Bug Fixing + Hardening | 5 days | ~2 days | 2.5x |
| False Starts + Rework | 4 days | ~0.5 days | 8x |
| Documentation | 1 day | ~0.2 days | 5x |
| TOTAL | 53.5 days | ~12 days | 4.5x |
| Human (realistic) | With AI Assistant | Ratio | |
|---|---|---|---|
| Full-day equivalents (8h) | 53 | ~12 | 4.5x |
| Effective hours | ~424h | ~90h | 4.7x |
| Calendar time | ~2.5 months | ~3 weeks | 3.5x |
| LOC/day | 370 | 1,637 | 4.4x |