# Next Session: Verify Sonar Works Without LED Lights

## TL;DR

Sonar LEDs were turned off in session 56 to fix camera pink tint. The sonar
(`getDistance()`) still reads on the `/health` endpoint (719mm observed in
session 58). This session: do a systematic accuracy test at known distances
to confirm the ultrasonic sensor works correctly without its LEDs.

## Background

The Hiwonder ultrasonic sensor has built-in RGB LEDs (I2C address 0x77).
These LEDs were causing a pink/blue tint on the camera via auto white balance.
We killed them in `_init_hardware()` at `services/turbopi-server/main.py:216-218`:

```python
sonar.setRGBMode(0)
sonar.setPixelColor(0, (0, 0, 0))
sonar.setPixelColor(1, (0, 0, 0))
```

**Question:** The LEDs are part of the same module as the ultrasonic transducer.
Does turning off the LEDs affect the ultrasonic distance measurement? The LEDs
are RGB decorative, not the ultrasonic emitter/receiver — but we should verify.

## Quick Check (already done)

```bash
ssh pi "curl -s http://localhost:8080/health \
  -H 'Authorization: Bearer 8cX80yIBws1PfBjFuvPz0k9egPSZD0LvS02oUD6ijfg' \
  | python3 -m json.tool | grep distance"
```

Result from session 58: `"distance_mm": 719` — sonar IS returning values.

## Systematic Test Protocol

1. **Place object at known distances:** 10cm, 30cm, 50cm, 100cm
2. **Read sonar at each distance:**
   ```bash
   # Single reading
   ssh pi "curl -s http://localhost:8080/health \
     -H 'Authorization: Bearer 8cX80yIBws1PfBjFuvPz0k9egPSZD0LvS02oUD6ijfg'" \
     | python3 -c "import sys,json; print(json.load(sys.stdin)['distance_mm'])"

   # 10 readings (average)
   for i in $(seq 1 10); do
     ssh pi "curl -s http://localhost:8080/health \
       -H 'Authorization: Bearer 8cX80yIBws1PfBjFuvPz0k9egPSZD0LvS02oUD6ijfg'" \
       | python3 -c "import sys,json; print(json.load(sys.stdin)['distance_mm'])"
   done
   ```
3. **Compare with ruler measurement** — tolerance should be ±2cm
4. **Test edge cases:** no object (>200cm), very close (<5cm), angled surface

## Expected Results

- Sonar should read within ±2cm of ruler measurement at all distances
- The LEDs are purely decorative RGB on the same I2C board — they don't
  affect the ultrasonic transducer (separate TX/RX piezo elements)
- If readings are wrong: re-enable LEDs temporarily to compare, then
  investigate I2C register conflicts

## Also Test

- **Avoidance demo** — uses sonar for obstacle detection. Start via:
  ```bash
  ssh pi "curl -s -X POST http://localhost:8080/demo/start \
    -H 'Authorization: Bearer 8cX80yIBws1PfBjFuvPz0k9egPSZD0LvS02oUD6ijfg' \
    -H 'Content-Type: application/json' \
    -d '{\"name\": \"Avoidance\", \"color\": \"\"}'"
  ```
  Car should stop/turn when approaching an obstacle.

- **Safety daemon** — runs continuously, triggers e-stop at 30cm. Check:
  ```bash
  ssh pi "curl -s http://localhost:8080/health \
    -H 'Authorization: Bearer 8cX80yIBws1PfBjFuvPz0k9egPSZD0LvS02oUD6ijfg'" \
    | python3 -c "import sys,json; d=json.load(sys.stdin); print(f'safety={d[\"safety_daemon_healthy\"]}, sonar={d[\"sonar_healthy\"]}, dist={d[\"distance_mm\"]}mm')"
  ```

## Files

| File | What |
|------|------|
| `services/turbopi-server/main.py:216-218` | LED kill in `_init_hardware()` |
| `services/turbopi-server/main.py:251-252` | `_read_sonar_sync()` |
| `services/turbopi-server/main.py:409-412` | SonarPoller startup (10 Hz) |
| `services/turbopi-server/safety.py` | Safety daemon uses sonar for e-stop |

## Prompt

```
Verify sonar accuracy with LEDs off. Read docs/NEXT-SESSION-SONAR-LED-VERIFY.md.
Test at 10cm, 30cm, 50cm, 100cm. Compare with ruler. Test Avoidance demo.
```
