|
| 1 | +# HM-10 Web Gamepad Integration Design |
| 2 | + |
| 3 | +## Objective |
| 4 | + |
| 5 | +Enable students to control the AIDriver robot from any Chrome-based browser via Web Bluetooth using an HM-10 BLE module (the new standard interface), while keeping parity between the physical robot experience and the Challenge 7 simulator. |
| 6 | + |
| 7 | +## User Workflow |
| 8 | + |
| 9 | +1. Student powers the robot and the HM-10 module enters advertising mode. |
| 10 | +2. Student opens the browser-based controller and chooses the new **BLE Gamepad** menu item. |
| 11 | +3. The web app prompts for a Bluetooth device; the student selects the HM-10. |
| 12 | +4. Once connected, the on-screen joystick drives the robot in real time. |
| 13 | +5. If two-way telemetry is enabled, ultrasonic distance readings appear on the controller UI. |
| 14 | +6. Students can switch to the Challenge 7 simulator and reuse the same UI controls for practice. |
| 15 | + |
| 16 | +## System Components |
| 17 | + |
| 18 | +- **HM-10 BLE Module** wired to the Pico’s UART (3.3 V, GND, RX, TX) using the existing diagrams (identical wiring to the retired HC-05 instructions). |
| 19 | +- **MicroPython Firmware** running on the Pico to parse joystick packets, drive motors, and optionally publish sensor data back over BLE. |
| 20 | +- **Web Bluetooth Controller** (new web app entry) hosting the joystick UI and BLE logic with a modern, mobile-optimised layout. |
| 21 | +- **Challenge 7 Simulator Integration** extending the existing simulator to reuse the same control surface. |
| 22 | +- **Documentation Updates** revising Challenge 7 instructions to reference the new controller and HM-10-specific setup. |
| 23 | + |
| 24 | +## Communication Protocol |
| 25 | + |
| 26 | +- **Transport:** BLE GATT over HM-10’s default UART service (Service UUID `0000FFE0-0000-1000-8000-00805F9B34FB`, Characteristic UUID `0000FFE1-0000-1000-8000-00805F9B34FB`). |
| 27 | +- **Packet Format (Controller → Robot):** |
| 28 | + - Byte 0: control flags (bit0 brake, bit1 reserved). |
| 29 | + - Byte 1: signed int8 left motor speed (-255..255). |
| 30 | + - Byte 2: signed int8 right motor speed (-255..255). |
| 31 | + - Byte 3 (optional): watchdog counter to detect stale commands. |
| 32 | +- **Packet Format (Robot → Controller, optional):** |
| 33 | + - Byte 0: telemetry type (0x01 = ultrasonic distance mm). |
| 34 | + - Byte 1-2: uint16 distance in millimetres (big-endian). |
| 35 | + - Telemetry updates throttled to 5 Hz to minimise BLE traffic while keeping readings fresh. |
| 36 | + |
| 37 | +## Control Mapping Logic |
| 38 | + |
| 39 | +- On-screen joystick yields `x` and `y` values in range [-1.0, 1.0]. |
| 40 | +- Compute base speed = `y * MAX_SPEED` (MAX_SPEED = 255). |
| 41 | +- Differential steering: |
| 42 | + - `left = clamp(base + x * |y| * MAX_SPEED, -255, 255)`. |
| 43 | + - `right = clamp(base - x * |y| * MAX_SPEED, -255, 255)`. |
| 44 | +- Hard left/right (`x` near ±1 with low |y|) results in spin turns by setting `left = -right = x * MAX_SPEED`. |
| 45 | +- Joystick centre (|x|, |y| < deadzone) sets both speeds to 0 (brake command flagged for immediate stop). |
| 46 | + |
| 47 | +## Web UI Requirements |
| 48 | + |
| 49 | +- Add controller entry as a new menu item alongside existing simulator tools. |
| 50 | +- Layout: |
| 51 | + - Main joystick (touch + mouse support) with responsive sizing and full-screen mobile presentation. |
| 52 | + - Brake indicator and connection status banner. |
| 53 | + - Telemetry panel showing latest ultrasonic reading with color-coded proximity. |
| 54 | + - Connect/Disconnect button using Web Bluetooth flow. |
| 55 | +- Reuse styling and responsive behaviour from existing Challenge 7 simulator controls for student familiarity. |
| 56 | +- Provide fallback messaging for browsers that lack Web Bluetooth. |
| 57 | + |
| 58 | +## Firmware Updates |
| 59 | + |
| 60 | +- Implement BLE UART handler to read joystick packets from HM-10. |
| 61 | +- Integrate with existing AIDriver motor APIs, replacing Dabble-specific logic. |
| 62 | +- Add optional telemetry publisher (periodic ultrasonic distance push if enabled through configuration flag). |
| 63 | +- Remove legacy HC-05 code paths; HM-10 BLE is the sole supported transport. |
| 64 | + |
| 65 | +## Simulator Updates |
| 66 | + |
| 67 | +- Extend Challenge 7 simulator to expose the same joystick component, translating virtual movements into simulated motor speeds. |
| 68 | +- Mirror telemetry display by sampling the simulator’s virtual ultrasonic sensor. |
| 69 | +- Ensure keyboard bindings remain available for accessibility. |
| 70 | + |
| 71 | +## Documentation Changes |
| 72 | + |
| 73 | +- Update `docs/Challenge_7.md`: |
| 74 | + - Replace HC-05-centric instructions with HM-10 wiring and Web Bluetooth controller usage. |
| 75 | + - Document browser requirements and connection steps (including mobile Chrome guidance). |
| 76 | + - Add section describing optional telemetry feedback. |
| 77 | +- Add troubleshooting entry for BLE pairing and browser permission issues. |
| 78 | + |
| 79 | +## Implementation Plan |
| 80 | + |
| 81 | +1. **Hardware Validation** |
| 82 | + |
| 83 | +- Confirm HM-10 wiring and voltage levels on the robot platform (reuse existing VCC/GND/RX/TX instructions). |
| 84 | +- Verify BLE advertising and connection from test browser. |
| 85 | + |
| 86 | +2. **Firmware Development** |
| 87 | + - Create BLE UART reader/writer module (e.g., `hm10_controller.py`). |
| 88 | + - Implement joystick packet parser and motor control logic. |
| 89 | + |
| 90 | +- Add telemetry publisher capped at 5 Hz and configuration switches. |
| 91 | + |
| 92 | +3. **Web Application** |
| 93 | + |
| 94 | + - Build joystick UI component with pointer/touch support. |
| 95 | + - Implement Web Bluetooth connection flow (service/characteristic UUIDs, notifications, writes). |
| 96 | + - Encode joystick values into command packets and handle telemetry notifications. |
| 97 | + - Integrate controller into site navigation/menu. |
| 98 | + |
| 99 | +4. **Simulator Alignment** |
| 100 | + - Reuse joystick component within Challenge 7 simulator page. |
| 101 | + - Ensure motor speed calculations match the physical firmware implementation. |
| 102 | + |
| 103 | +- Display simulated ultrasonic readings in the same UI panel at the same 5 Hz cadence. |
| 104 | + |
| 105 | +5. **Documentation & Testing** |
| 106 | + - Revise Challenge 7 documentation and associated assets. |
| 107 | + - Update sample code snippets in repository. |
| 108 | + - Conduct end-to-end tests on supported browsers and devices. |
| 109 | + |
| 110 | +## Risks & Mitigations |
| 111 | + |
| 112 | +- **Browser Support Variability:** Limit scope to Chrome-based browsers; provide guidance for unsupported platforms. |
| 113 | +- **BLE Latency/Bandwidth:** Keep packet size minimal; apply 40 ms update interval to balance responsiveness. |
| 114 | +- **Student Pairing Issues:** Include clear steps for re-pairing and resetting HM-10. |
| 115 | +- **Simulator Divergence:** Share a single joystick calculation module between firmware, web app, and simulator to ensure consistency. |
| 116 | + |
| 117 | +## Closed Decisions |
| 118 | + |
| 119 | +- **Legacy Support:** HC-05 compatibility will be removed; HM-10 BLE is the only supported wireless path moving forward. |
| 120 | +- **Telemetry Scope:** Ultrasonic telemetry only, sampled at 5 Hz. |
| 121 | + |
| 122 | +## Open Questions |
| 123 | + |
| 124 | +1. Should the existing Dabble/HC-05 workflow remain available alongside the HM-10 controller, or can it be fully replaced? |
| 125 | +2. What Ultrasonic sensor sampling rate is acceptable for telemetry without impacting BLE throughput (e.g., 5 Hz vs. 10 Hz)? |
| 126 | +3. Do we need support for additional feedback (battery voltage, errors) beyond ultrasonic distance in this release? |
0 commit comments