The Inspiration: The Silent Tsunami

My journey didn't start in a code editor; it started in the archives of PubMed and deep clinical interviews. I was obsessed with Cortical Spreading Depression (CSD)- a massive wave of electrophysiological depolarization that sweeps across the brain, often acting as the precursor to migraines, strokes, and traumatic brain injuries.

This research eventually led to a book publication with Taylor & Francis, where I mapped the triggers and patterns of this "silent tsunami." But as I looked at my research, a haunting question emerged: If someone built a device to stop this, how would they know it actually works before putting it on a human?

I prototyped an Arduino-based early warning system using EEG sensors, but I quickly realized the "Researcher’s Trap." I was testing my code on a clean, controlled signal. In the real world, a patient’s brain is noisy, irregular, and unpredictable. The inspiration for BioTwin-HIL was born from that gap: the need to move beyond "happy path" testing into Adversarial Clinical Validation.

How We Built It: The Polyglot Matrix

We didn't just want to "simulate" a heart or a brain; we wanted to emulate the silicon.

  • The Execution Layer (Rust & Wokwi): We chose Rust for its zero-cost abstractions and "fearless concurrency." Using the Wokwi CLI, we built a runner that executes the actual compiled .hex binary of the firmware. To the code, there is no difference between our platform and real copper.
  • The AI Layer (BioMistral): We integrated BioMistral-7B to act as our "Adversarial Pathologist." We fine-tuned it on PTB-XL clinical datasets so it could analyze the firmware logic and "hallucinate" the specific physiological edge cases meant to break it.
  • The Biophysical Engine: We used NeuroKit2 to turn AI "intents" into mathematically grounded waveforms. We didn't just tell the AI to "make an ECG"; we used biophysical constants to ensure the signals followed the laws of biology.

The Science: Modeling the Chaos

To ensure the system was medically grounded, we modeled the electrical potential of the heart and nerves. For example, to test if an Arduino could detect a widening QRS complex, we had to model the propagation delay mathematically:

$$E_{m} = \frac{RT}{F} \ln \left( \frac{P_{K}[K^{+}]{out} + P{Na}[Na^{+}]{out} + P{Cl}[Cl^{-}]{in}}{P{K}[K^{+}]{in} + P{Na}[Na^{+}]{in} + P{Cl}[Cl^{-}]_{out}} \right)$$

By injecting these calculated micro-voltages directly into the virtual ADC of the microcontroller, we could simulate conditions like Hyperkalemia (tall T-waves) or Bundle Branch Blocks (wide QRS) with $1\text{ ms}$ precision.

Challenges Faced: The "Hallucination" Firewall

The biggest hurdle was the AI itself. Early in development, the AI would occasionally suggest "impossible" heartbeats—rhythms that looked like ECGs but violated the laws of physics.

The Solution: We built a Biophysical Validator (our "Hallucination Firewall") in Rust. It acts as a deterministic gatekeeper. If BioMistral suggests a heart rate of $400\text{ BPM}$ with a $10\text{ ms}$ QRS, the Rust core rejects it instantly. We learned that in MedTech, AI provides the creativity, but Physics must provide the boundaries.

Another challenge was Latency Jitter. Real-time HIL requires strict timing. If the simulation lags by even $5\text{ ms}$, the Arduino's internal timers fail. We moved the entire orchestration to a lock-free, MPMC (Multi-Producer Multi-Consumer) architecture in Rust to ensure the "Digital Twin" stayed perfectly synced with the virtual silicon.

What We Learned: Medicine is Harder than Code

This project taught us that "Tested" does not mean "Safe." Most hackathon projects are prototypes that work for 5 minutes on a demo stage. By building BioTwin-HIL, we learned how to subject code to a "Virtual Clinical Trial."

We learned that the most important part of a medical device isn't the feature that works—it's the failure mode it handles. Using DeepSeek-R1 to generate FDA-style reports taught us how to speak the language of regulators, turning "It works on my machine" into "This firmware is validated against 500 clinical edge cases with a $99.8\%$ detection accuracy."

Built With

Share this project:

Updates

Submission history