Tutorials

AI-generated illustration of a compact wheeled robot on a dark grid floor, with a teal traveled path, a gold remaining route, and a frosted glass phone slab showing an abstract unlabeled map and a red latch disc. Simulation only.
Tutorial

How I built a Jetpack Compose Android companion for a simulated robot with robot-side mission rules

A portfolio, simulation-only walkthrough of compose-android-robot-companion: a native Kotlin and Jetpack Compose companion for one simulated robot, with live telemetry, a mission catalog, and a latched e-stop whose safety rules run on the robot side. How to build it, run the tests, and what the GitHub Pages screenshot gallery actually shows, including the honest gaps.

Arpan Bom11 min read
ShareXLinkedIn

This is a simulation only

This is a portfolio demo, not a real robot and not a certified safety system. The plant is a deterministic Kotlin simulation of one unicycle robot in a 10 m by 8 m room. There is no physical robot or device in CI, no ROS, no lidar, and no functional-safety claim. I built a native Jetpack Compose Android companion that shows live telemetry, holds a mission catalog, and sends start, pause, resume, cancel, and e-stop commands. The speed limits, the latched e-stop, the heartbeat deadman, and the battery rules are enforced in the simulator on the robot side. The UI only asks. I did not invent those safety ideas, and this is not a substitute for a real emergency circuit or a certified controller.

Try the live demo

The fastest way to see it working is the screenshot gallery on GitHub Pages: https://applycoding.github.io/compose-android-robot-companion/

The page title is "Simulation only, not a certified safety system: Robot companion." The page is a static gallery of six Roborazzi frames, not an interactive Android app or WebSocket client in the browser. The frames are 1080 by 2400 pixels, rendered on the JVM, and they match the committed files under app/src/test/screenshots/. You get the in-app simulator at ticks 0, 150, 300, 450, and 627, plus an e-stop frame at tick 313.

Roborazzi frame of the Robot Companion app at tick 300, with the simulation-only banner, in-app simulator selected, pose (8.44, 2.54), LINK OK, and RUNNING. Simulation only.

Image: app/src/test/screenshots/screenshot_tick_300.png from the repository, also served on the GitHub Pages demo. Mid-mission frame, 1080 by 2400, rendered on the JVM. Simulation only. applycoding.com.

Roborazzi frame of the Robot Companion app at tick 627, with pose (2.03, 6.00), speed 0, battery 93.6%, LINK OK, and COMPLETED. Simulation only.

Image: app/src/test/screenshots/screenshot_tick_627.png from the repository, also served on the GitHub Pages demo. Completed demo mission. Simulation only. applycoding.com.

Roborazzi frame of the Robot Companion app after an e-stop at tick 300, at rest at tick 313, with the E-STOP LATCHED control. Simulation only.

Image: app/src/test/screenshots/screenshot_tick_313_estop.png from the repository, also served on the GitHub Pages demo. Latched e-stop frame. Simulation only. applycoding.com.

The telemetry bar prints pose with two decimal places and battery with one, so the completed frame shows (2.03, 6.00) and 93.6%. The tests assert the tighter values below.

What the project is

compose-android-robot-companion (repo: applycoding/compose-android-robot-companion, commit d2841be) is a native Kotlin and Jetpack Compose companion for one simulated robot. The debug APK is com.applycoding.robot_companion version 1.0.0, minSdk 26, targetSdk 37, with the launcher label "Robot Companion (simulation)." This repo ships a debug build only: no release signing, no R8, no Play Store listing, and no iOS target. That is the difference from flutter-robot-operator, which is a Flutter and Dart operator with its own simulator, protocol, and browser demo. No code was copied, and the protocol here is new. The stack is:

Layer Role
:sim Pure Kotlin plant, unicycle kinematics, mission state machine, and robot-side rules.
:link Wire codec, Transport interface, InProcessTransport, WebSocketTransport, and a 200 ms HeartbeatSender.
:sim-server A Ktor CIO WebSocket server on loopback that owns one RobotHost.
:app Jetpack Compose UI. Default transport is the in-app simulator; a toggle switches to ws://127.0.0.1:8787/v1/robot.

This is an original implementation, inspired by the projects credited in REFERENCES.md and NOTICE: Compose unidirectional data flow and SavedStateHandle (Android architecture guidance), JVM screenshot tests with Roborazzi (Now in Android), a virtual-time test scheduler (kotlinx.coroutines test), a JSON envelope over a Ktor WebSocket, and the general idea of a latched stop that must be reset deliberately (ISO 13850:2015, title only; this repo is not designed, tested, or certified to that standard or any other). I did not copy those sources.

The simulation ticks at 25 Hz (dt is 40 ms). Tests count ticks and do not read the wall clock. The standard limits are 0.8 m/s forward, 0.5 m/s^2 normal accel, 1.6 m/s^2 e-stop braking, a heading-rate limit of 1.2 rad/s, and a 13-tick heartbeat timeout. The demo mission is "Demo patrol": three waypoints at (8.0, 1.0), (8.0, 6.0), and (2.0, 6.0), with a 0.10 m arrival tolerance. Waypoints must sit in [0.5, 9.5] by [0.5, 7.5]. Battery starts at 100%, start is refused below 20%, and a running mission pauses (LOW_BATTERY) below 10%. The in-app catalog also includes a shorter "Patrol B" mission.

Mission rules on the robot side

StartMission is checked in this order: ESTOP_LATCHED, LINK_LOST, BUSY (the mission is RUNNING or PAUSED), INVALID_MISSION, then LOW_BATTERY. Resume is checked as ESTOP_LATCHED, NOT_PAUSED, LINK_LOST, then LOW_BATTERY when the pause reason is low battery and the battery is still below 10%. EStop is always accepted and idempotent. It latches, and it turns a RUNNING or PAUSED mission into ABORTED. ResetEStop is accepted only when the latch is set, the robot is at rest, and the link is up. It does not restart motion.

Three precedence tests are weaker than their names. startPrecedenceEstopOverBusy latches e-stop after a start, which aborts the mission, so BUSY is no longer true when the second start is rejected with ESTOP_LATCHED. startPrecedenceBusyOverLowBattery constructs the simulator at 21% battery, so the 20% start threshold is not crossed and LOW_BATTERY is not active. resumePrecedenceNotPausedOverLinkLost sends Resume on a running mission and never drops the link.

The app sends heartbeats only while the activity is in the STARTED state. When the app is backgrounded, heartbeats stop, and a running mission pauses with LINK_LOST until the operator returns.

Build and run locally

CI pins Temurin JDK 21.0.13+9 on ubuntu-24.04, Gradle 9.9.1, and Android SDK platform 37 with build-tools 37.0.0. Locally:

./gradlew :sim:test :link:test :sim-server:test
./gradlew :app:testDebugUnitTest :app:verifyRoborazziDebug :app:assembleDebug
./gradlew :app:assembleDebug
./gradlew :sim-server:run --args='--port=8787'

On a physical device, expose the server with adb reverse tcp:8787 tcp:8787, then switch the in-app transport to Sim server (ws://127.0.0.1:8787/v1/robot). CI does not install that APK on a device. The WebSocket protocol has no authentication, and the server binds to loopback. A debug-only network security config allows cleartext only to 127.0.0.1 and 10.0.2.2.

The hygiene job runs python3 tools/check_spdx.py, checks that LICENSE matches the official Apache-2.0 text, rejects unpinned GitHub Actions, and greps tests for @Ignore, assumeTrue, and assertTrue(true). The jvm and android jobs run python3 tools/check_junit.py, which fails if any JUnit case is skipped. The android job also runs tools/check_apk.sh against app-debug.apk. The notice job regenerates the Gradle license reports and runs python3 tools/check_notice.py. The mutants job runs python3 tools/mutants.py.

Results from the tests

On commit d2841be, GitHub Actions run 42945398138 completed every job in ci.yml: hygiene, jvm, android, mutants, and notice. The hygiene job printed SPDX ok on 85 files and LICENSE: OK. The jvm and android jobs both printed junit ok: 0 skipped and BUILD SUCCESSFUL. The android job also printed the debug APK badging for com.applycoding.robot_companion version 1.0.0, minSdkVersion:'26', targetSdkVersion:'37', and application-debuggable. The notice job printed NOTICE ok. The mutants job printed all mutants killed after all 14 source mutants (S1 to S7, L1 to L3, A1 to A4) failed their own tests.

At this SHA there are 69 @Test methods and no skipped, ignored, or assumed tests: 60 on the JVM (:sim 46, including 34 in MissionRulesTest; :link 10; :sim-server 4) and 9 in :app.

On the demo mission with a heartbeat every 5 ticks, KinematicsTest and the golden file sim/src/test/resources/golden/demo_trace.txt give these ticks. v is 0.780000 at tick 39 and first reaches vMax 0.800000 at tick 40. The waypoint index switches to 1 at tick 237 and to 2 at tick 407. The mission is RUNNING at tick 626 and COMPLETED at tick 627, with final pose (2.0339, 6.0031), travelled 18.399 m, and battery 93.623%. The mid-mission screenshot tick is pose (8.44, 2.54), v 0.80, RUNNING.

EStopTest.restsAtTick113 applies e-stop before the step at tick 100, with v still greater than 0 at tick 112 and v 0 at tick 113, mission ABORTED, latch set, and stopping distance 0.1843 m, under the bound v^2 / (2 * brake) of 0.2 m. HeartbeatTest.lostAtTick209 sends the last heartbeat at tick 195: the link is still ok at tick 208, LINK_LOST and PAUSED at tick 209, and at rest at tick 221.

LockstepLiveTest.traceEqualsInProcess runs that e-stop script over a real Ktor WebSocket to :sim-server in lockstep mode, collects 130 snapshots, and compares them to an in-process reference. The comparison uses the wire view: w and travelled are zeroed, because those fields are not on the snapshot frame.

License and references

Apache-2.0. Copyright 2026 Arpan Bom. See LICENSE and NOTICE.

NOTICE lists the pinned dependencies with licenses, including Kotlin 2.4.21, the Compose BOM 2026.09.00, Android Gradle Plugin 9.5.1, Gradle 9.9.1, Ktor 3.6.1, kotlinx-coroutines 1.11.0, kotlinx-serialization 1.12.0, activity-compose 1.13.0, lifecycle 2.11.0, Robolectric 4.17, Roborazzi 1.78.0, JUnit 4.13.2, Temurin JDK 21.0.13+9 (GPL-2.0-with-classpath-exception, used to compile), and Android SDK platform 37 (used to build, not redistributed), and it points at REFERENCES.md for idea-only references and their licences. No third-party robot or safety source is vendored.

How this was verified

I ran the standard portfolio-tutorial checks against the build commit d2841bef7f48e525446a9635604309e2ebf03719 on main, and re-verified them at the current tip af856eb97e46593f3ac92785cc7c2dacd3fb80de.

  • The GitHub repo is not a fork. gh api repos/applycoding/compose-android-robot-companion reports "fork": false.
  • All three commits on main have author and committer Arpan Bom (applycoding@gmail.com). git log --format=%B origin/main has no Co-authored-by trailers. The only remote branch at this SHA is main.
  • A search of the tree found no third_party, vendor, or external directories, and no "ported from", "adapted from", or "copied from" markers in the authored source.
  • This is an original implementation, inspired by the Android architecture, Now in Android, kotlinx.coroutines test, Ktor WebSockets, and ISO 13850 idea references credited above. I did not invent those methods, and I did not copy those sources.
  • At d2841be, NOTICE names the direct Gradle, toolkit, and test pins with licenses, and it points at REFERENCES.md for idea-only references. The repo license is Apache-2.0. The LICENSE file is byte-identical to the official Apache-2.0 text from apache.org (sha256 cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30).
  • Commands, paths, APIs, and the numbers in this tutorial were checked against the repo at the build commit d2841be and re-checked at tip af856eb, and against the live Pages gallery. The 14 mutants killed, NOTICE ok, SPDX ok on 85 files, LICENSE: OK, junit ok: 0 skipped on both the jvm and android jobs, and the debug APK badging are from GitHub Actions run 42945398138 on d2841be, a valid ancestor, and again from current run 43027875341 on af856eb (hygiene, jvm, android, mutants, notice). Pages deploy 43028688725 on af856eb returned HTTP 200 for the live demo, whose title is "Simulation only, not a certified safety system: Robot companion." All of those runs have actor and triggering_actor applycoding. The 69 test count is the number of @Test methods in the tree at this SHA.
  • Independent checks at this SHA: the 14 source mutants in tools/mutants.py each fail their own test (S1 reduced brake, S2 stretched heartbeat timeout, S3 reset allowed while moving, S4 start allowed while latched, S5 anticipatory braking removed, S6 non-finite waypoints accepted, S7 e-stop and link-lost start-reject order swapped, L1 dropped e-stop on the lockstep host, L2 estop forced false on the wire, L3 realtime tick that republishes the previous snapshot, A1 e-stop click sent as pause, A2 screenshot robot radius changed, A3 UI that does not collect later state, A4 catalog not written to SavedStateHandle), and the mutants job requires that they are killed; lockstep equality of 130 wire-view snapshots; tools/check_apk.sh on the debug APK; tools/check_junit.py fails if a test is skipped; and authorship as above.
  • This project is simulation only. It is not connected to a robot and is not a certified safety system.

Limits

This is simulation only. It is not a real robot, not exercised on a physical device in CI, and not a certified safety system. The repo ships a debug APK only. The Pages demo is a static Roborazzi gallery rendered on the JVM, not an Android app running in the browser. Lockstep equality compares the wire view, with w and travelled zeroed. Three precedence tests are weaker than their names, as described above. tools/check_notice.py hard-codes three tool labels that must appear in NOTICE (kotlinx-coroutines-test:1.11.0, androidx.compose.ui:ui-test-junit4, and com.github.jk1.dependency-license-report:3.1.5) instead of discovering those strings only from the Gradle files. awaitSnapshotTick polls the latest snapshot for an exact tick and can miss a tick that is overwritten before the next poll. SavedStateHandle restores the UI catalog after process death, not after a force-stop, and it does not restore robot state. The README, the plan, and the code all say 14 mutants.