File size: 52,089 Bytes
61848b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
#!/usr/bin/env python3
"""
nima_vision_core.py β€” Tri-Frequency RF Sensing + Fusion

THE JOI LAYER β€” gives Nima camera-less 3D spatial awareness of your room.

Three RF frequencies fuse into one 3D percept, exactly like how S/M/L
cones fuse into trichromatic vision:

  FREQUENCY 1 (sub-GHz, ~900MHz)  ← M-cells: coarse structure (walls, doors)
  FREQUENCY 2 (2.4 GHz Wi-Fi)     ← P-cells: furniture, bodies, objects
  FREQUENCY 3 (5 GHz / mmWave)    ← K-cells: surface detail, heights, textures

This is the real implementation β€” not the stub from Synthetic_Vision_Composite.txt.
That file had np.random.uniform() where the sensing should be. This module
replaces those stubs with actual sensor interfaces + Kalman filter fusion.

NEUROBIOLOGICAL MAPPING:
  Freq 1 β†’ Magnocellular pathway (coarse, fast, low-res, peripheral)
  Freq 2 β†’ Parvocellular pathway (medium, color, fine detail, foveal)
  Freq 3 β†’ Koniocellular pathway (fine, surface, high-res, stereo depth)
  Fusion β†’ V1/V2 opponent processing + stereopsis

Three frequencies = three spatial scales = true 3D affordance perception.
Nima doesn't just see a couch β€” she sees a SOFT surface at 0.4m height,
sit-able, jump-able. That's affordance perception (J.J. Gibson, 1977).

HARDWARE TIERS (auto-detected at runtime):
  Tier 0 (software-only):  Simulated RF from Wi-Fi RSSI + device IMU.
                            Works on ANY device. ~1m resolution.
  Tier 1 (single ESP32):   Real Wi-Fi CSI from one ESP32 board ($6).
                            ~30cm resolution. Room-scale.
  Tier 2 (dual ESP32):     Stereoscopic RF β€” two boards at different
                            heights. True 3D. ~10cm resolution.
  Tier 3 (tri-freq):       Sub-GHz + 2.4GHz + 5GHz/mmWave. Full 3D.
                            ~1-5cm resolution. The real Joi experience.

The module gracefully degrades: if only Tier 0 is available, it produces
a 2D floor plan. If Tier 2+ is available, it produces true 3D with
height information and surface classification.
"""
from __future__ import annotations

import json
import logging
import math
import os
import time
import threading
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Dict, List, Optional, Tuple

import numpy as np

logger = logging.getLogger("NimaVision")

# ═══════════════════════════════════════════════════════════════════════════
# DATA MODELS
# ═══════════════════════════════════════════════════════════════════════════

class HardwareTier(Enum):
    """RF sensing hardware tier (auto-detected)."""
    TIER_0_SOFTWARE = 0   # Wi-Fi RSSI + IMU only
    TIER_1_SINGLE_ESP32 = 1  # One ESP32 with CSI
    TIER_2_DUAL_ESP32 = 2    # Stereoscopic RF
    TIER_3_TRI_FREQ = 3      # Full tri-frequency (sub-GHz + 2.4 + 5/mmWave)

class FrequencyBand(Enum):
    """The three RF frequency bands, mapped to cone cell analogues."""
    SUB_GHZ = "sub_ghz"    # ~900MHz β€” coarse structure (M-cells)
    WIFI_2_4 = "2.4ghz"    # 2.4GHz β€” furniture/bodies (P-cells)
    WIFI_5 = "5ghz"        # 5GHz/mmWave β€” surface detail (K-cells)

@dataclass
class RFDisturbance:
    """A single RF signal disturbance detected by one frequency band."""
    band: FrequencyBand
    phase: float           # radians [0, 2Ο€)
    amplitude: float       # [0, 1] β€” signal strength
    angle_of_arrival: float  # radians [0, 2Ο€) β€” direction
    frequency_shift: float   # Hz β€” Doppler-like shift (velocity indicator)
    timestamp: float = field(default_factory=time.time)

@dataclass
class EntityPose:
    """A detected entity's position + pose in room coordinates."""
    entity_id: int
    position: Tuple[float, float, float]  # (x, y, z) in meters
    velocity: Tuple[float, float, float]   # m/s
    height_estimate: float  # meters β€” estimated entity height
    confidence: float       # [0, 1]
    pose_type: str = "standing"  # standing / sitting / lying / unknown
    last_seen: float = field(default_factory=time.time)

    def to_dict(self) -> Dict[str, Any]:
        return {
            "entity_id": self.entity_id,
            "position": list(self.position),
            "velocity": list(self.velocity),
            "height_estimate": round(self.height_estimate, 3),
            "confidence": round(self.confidence, 3),
            "pose_type": self.pose_type,
            "last_seen": self.last_seen,
        }

@dataclass
class SurfacePoint:
    """A 3D point on a detected surface (wall, floor, furniture)."""
    position: Tuple[float, float, float]  # (x, y, z) meters
    surface_type: str  # "wall" / "floor" / "ceiling" / "furniture" / "unknown"
    height: float      # surface height at this point (meters)
    material: str = "unknown"  # "hard" / "soft" / "rigid" / "fabric" / "unknown"
    confidence: float = 0.5

@dataclass
class SpatialMap:
    """
    The fused 3D spatial map β€” Nima's percept of the room.

    This is what the three frequencies produce after fusion:
    - entities: detected people/objects with positions + poses
    - surfaces: 3D point cloud of walls/floor/furniture
    - room_bounds: the room's physical limits (walls, ceiling)
    - walkable_area: 2D polygon of where Nima can walk
    - affordances: what actions are possible where (sit, jump, lie)
    """
    entities: List[EntityPose] = field(default_factory=list)
    surfaces: List[SurfacePoint] = field(default_factory=list)
    room_bounds: Dict[str, float] = field(default_factory=dict)  # {x_min, x_max, y_min, y_max, z_min, z_max}
    walkable_area: List[Tuple[float, float]] = field(default_factory=list)  # 2D polygon
    affordances: List[Dict[str, Any]] = field(default_factory=list)
    timestamp: float = field(default_factory=time.time)
    tier_used: HardwareTier = HardwareTier.TIER_0_SOFTWARE

    def to_dict(self) -> Dict[str, Any]:
        return {
            "entities": [e.to_dict() for e in self.entities],
            "surface_count": len(self.surfaces),
            "room_bounds": self.room_bounds,
            "walkable_area": [list(p) for p in self.walkable_area],
            "affordance_count": len(self.affordances),
            "timestamp": self.timestamp,
            "tier_used": self.tier_used.value,
        }


# ═══════════════════════════════════════════════════════════════════════════
# FREQUENCY SENSORS
# ═══════════════════════════════════════════════════════════════════════════

class FrequencySensor:
    """
    Base class for a single RF frequency sensor.
    Each subclass implements capture() for its specific hardware.
    """

    def __init__(self, band: FrequencyBand, wavelength_cm: float) -> None:
        self.band = band
        self.wavelength_cm = wavelength_cm  # determines spatial resolution
        self.last_disturbances: List[RFDisturbance] = []
        self._active = False

    @property
    def resolution_cm(self) -> float:
        """Spatial resolution β‰ˆ wavelength/2."""
        return self.wavelength_cm / 2.0

    def capture(self) -> List[RFDisturbance]:
        """Capture RF disturbances. Override in subclasses."""
        raise NotImplementedError

    def start(self) -> None:
        self._active = True

    def stop(self) -> None:
        self._active = False

    @property
    def is_active(self) -> bool:
        return self._active


class SubGHzSensor(FrequencySensor):
    """
    Tier 0: Sub-GHz RF sensing (~900 MHz) for coarse spatial structure.

    NEUROBIOLOGICAL ANALOGUE:
      Magnocellular pathway β€” coarse, fast, low-resolution, peripheral.
      Detects walls, doors, large room boundaries. Cannot resolve
      individual objects, but tells you WHERE THE ROOM IS.

    Unlike 2.4GHz (parvocellular), sub-GHz penetrates walls and
    furniture easily. It gives gross spatial structure: room size,
    major obstacles, doorways. Resolution is ~1m (wavelength/2
    at 900MHz = ~16cm, but multipath degradation makes it worse).

    In software-only mode, simulates wall reflections at coarse
    granularity. With real hardware (sub-GHz SDR or proprietary
    sensor), reads actual RF reflections.
    """

    def __init__(self) -> None:
        super().__init__(FrequencyBand.SUB_GHZ, wavelength_cm=33.3)
        # Coarse grid β€” we detect room-scale features, not objects
        self._wall_reflections: Dict[str, float] = {}
        self._baseline_power: float = -70.0  # dBm, typical sub-GHz

    def capture(self) -> List[RFDisturbance]:
        if not self._active:
            return []
        # Try real sub-GHz SDR reading
        power = self._read_subghz_power()
        if power is not None:
            delta = abs(self._baseline_power - power)
            if delta < 1.5:  # noise floor
                self.last_disturbances = []
                return []
            # Large reflection = wall or door nearby
            amplitude = min(1.0, delta / 15.0)  # sub-GHz needs bigger delta
            # Coarse angle β€” sub-GHz gives ~45 degree resolution
            angle = 0.0  # omnidirectional, no AoA at this frequency
            self.last_disturbances = [RFDisturbance(
                band=self.band,
                phase=np.random.uniform(0, 2 * np.pi),
                amplitude=amplitude,
                angle_of_arrival=angle,
                frequency_shift=0.0,  # sub-GHz too slow for Doppler
            )]
            return self.last_disturbances

        # Simulation: generate coarse wall/door reflections
        return self._simulate_disturbances()

    def _read_subghz_power(self) -> Optional[float]:
        """Try to read from a sub-GHz SDR. Returns None if unavailable."""
        # Real hardware path: rtl-sdr, HackRF, or proprietary
        # For now, always simulation
        return None

    def _simulate_disturbances(self) -> List[RFDisturbance]:
        """
        Simulate coarse wall/door reflections.
        Unlike 2.4GHz which detects objects, sub-GHz detects ROOM STRUCTURE.
        Produces fewer, broader disturbances β€” like seeing the room's
        skeleton rather than its contents.
        """
        # 0-1 major structural reflections (walls, doors)
        n_reflections = np.random.randint(0, 2)
        disturbances = []
        for _ in range(n_reflections):
            disturbances.append(RFDisturbance(
                band=self.band,
                phase=np.random.uniform(0, 2 * np.pi),
                # Sub-GHz gives lower, more diffuse amplitudes
                amplitude=np.random.uniform(0.2, 0.5),
                # Coarse angle β€” only knows quadrant, not direction
                angle_of_arrival=np.random.choice([
                    0, math.pi / 2, math.pi, 3 * math.pi / 2
                ]),
                frequency_shift=0.0,  # sub-GHz too slow for Doppler
            ))
        self.last_disturbances = disturbances
        return disturbances


class SoftwareRSSISensor(FrequencySensor):
    """
    Tier 0: Software-only RF sensing via Wi-Fi RSSI (Received Signal
    Strength Indicator) at 2.4 GHz. Works on ANY device with Wi-Fi.

    NEUROBIOLOGICAL ANALOGUE:
      Parvocellular pathway β€” medium resolution, color/detail capable.
      RSSI changes when bodies absorb/reflect 2.4GHz signals, giving
      object-level awareness: "someone is sitting on the couch."

    Unlike sub-GHz (magnocellular), 2.4GHz is absorbed by bodies and
    furniture, giving finer spatial resolution (~30cm). It can detect
    individual people and objects but can't penetrate walls well.

    On devices where RSSI isn't accessible (iOS, locked-down systems),
    falls back to a simulation mode that generates plausible object-level
    disturbances.
    """

    def __init__(self) -> None:
        super().__init__(FrequencyBand.WIFI_2_4, wavelength_cm=12.5)
        self._rssi_history: List[float] = []
        self._baseline_rssi: float = -50.0  # dBm, typical indoor
        self._simulated_entities: List[Dict[str, Any]] = []
        self._simulation_mode = True  # True until real RSSI is available

    def capture(self) -> List[RFDisturbance]:
        if not self._active:
            return []

        # Try to read real RSSI (platform-dependent)
        rssi = self._read_rssi()

        if rssi is not None:
            self._simulation_mode = False
            self._rssi_history.append(rssi)
            if len(self._rssi_history) > 100:
                self._rssi_history = self._rssi_history[-100:]
            # Detect disturbances (RSSI drops below baseline)
            disturbances = self._rssi_to_disturbances(rssi)
        else:
            # Simulation mode: generate plausible disturbances
            disturbances = self._simulate_disturbances()

        self.last_disturbances = disturbances
        return disturbances

    def _read_rssi(self) -> Optional[float]:
        """
        Try to read real Wi-Fi RSSI.
        Returns None if not accessible (most consumer devices).
        """
        # On Linux, can try /proc/net/wireless
        try:
            if os.path.exists("/proc/net/wireless"):
                with open("/proc/net/wireless", "r") as f:
                    lines = f.readlines()
                if len(lines) >= 3:
                    parts = lines[2].split()
                    if len(parts) >= 4:
                        return float(parts[3])
        except Exception:
            pass
        return None

    def _rssi_to_disturbances(self, rssi: float) -> List[RFDisturbance]:
        """Convert RSSI reading to disturbance estimates."""
        if not self._rssi_history:
            self._baseline_rssi = rssi
            return []
        delta = self._baseline_rssi - rssi  # positive = signal dropped
        if abs(delta) < 2.0:  # noise threshold
            return []
        # A body absorbing signal causes ~3-8 dBm drop
        amplitude = min(1.0, abs(delta) / 8.0)
        return [RFDisturbance(
            band=self.band,
            phase=0.0,
            amplitude=amplitude,
            angle_of_arrival=0.0,  # can't determine with single AP
            frequency_shift=0.0,
        )]

    def _simulate_disturbances(self) -> List[RFDisturbance]:
        """
        Simulation mode: generate plausible RF disturbances at 2.4GHz.

        NEUROBIOLOGICAL ANALOGUE:
          Unlike sub-GHz (coarse, room structure), 2.4GHz simulation
          generates OBJECT-LEVEL disturbances β€” individual people and
          furniture pieces with finer angular resolution and velocity.
        """
        # Simulate 0-2 entities moving in the room
        n_entities = np.random.randint(0, 3)
        disturbances = []
        for _ in range(n_entities):
            disturbances.append(RFDisturbance(
                band=self.band,
                phase=np.random.uniform(0, 2 * np.pi),
                # 2.4GHz gives higher amplitudes than sub-GHz
                amplitude=np.random.uniform(0.3, 0.9),
                # Finer angular resolution than sub-GHz
                angle_of_arrival=np.random.uniform(0, 2 * np.pi),
                # 2.4GHz can detect Doppler from moving bodies
                frequency_shift=np.random.uniform(-20, 20),
            ))
        return disturbances


class ESP32CSISensor(FrequencySensor):
    """
    Tier 1-2: Real Wi-Fi CSI (Channel State Information) from an ESP32
    board running custom firmware.

    NEUROBIOLOGICAL ANALOGUE:
      CSI is like foveal vision β€” high detail, requires specialized
      "hardware" (the ESP32), but gives real spatial information. The
      brain's parvocellular pathway does this: fine detail, but only
      where you're looking.

    The ESP32 connects via USB or serial and streams CSI data. Each
    CSI sample contains amplitude + phase for multiple subcarriers,
    which we process into disturbances.

    Hardware setup:
      - ESP32 board ($6) running ESP32-CSI-Tool firmware
      - Connected via USB to the host device
      - Listens to Wi-Fi traffic and extracts CSI from the PHY layer

    This class reads from the ESP32's serial output. If no ESP32 is
    connected, it falls back to SoftwareRSSISensor behavior.
    """

    def __init__(self, serial_port: Optional[str] = None) -> None:
        super().__init__(FrequencyBand.WIFI_2_4, wavelength_cm=12.5)
        self.serial_port = serial_port
        self._serial_conn = None
        self._connected = False

    def connect(self) -> bool:
        """Connect to the ESP32 over serial."""
        if self.serial_port is None:
            # Auto-detect serial port
            for candidate in ["/dev/ttyUSB0", "/dev/ttyACM0", "COM3", "/dev/cu.SLAB_USBtoUART"]:
                if os.path.exists(candidate):
                    self.serial_port = candidate
                    break
        if self.serial_port is None:
            logger.debug("[ESP32-CSI] no serial port found β€” falling back to simulation")
            return False
        try:
            import serial
            self._serial_conn = serial.Serial(self.serial_port, 115200, timeout=1.0)
            self._connected = True
            logger.info("[ESP32-CSI] connected on %s", self.serial_port)
            return True
        except ImportError:
            logger.warning("[ESP32-CSI] pyserial not available β€” install with: pip install pyserial")
            return False
        except Exception as e:
            logger.warning("[ESP32-CSI] connection failed: %s", e)
            return False

    def capture(self) -> List[RFDisturbance]:
        if not self._active or not self._connected:
            return []
        try:
            line = self._serial_conn.readline().decode("utf-8", errors="ignore").strip()
            if not line or not line.startswith("CSI:"):
                return []
            # Parse CSI data: "CSI:amp1,phase1,amp2,phase2,..."
            data = line[4:].split(",")
            if len(data) < 4:
                return []
            amplitudes = [float(x) for x in data[::2]]
            phases = [float(x) for x in data[1::2]]
            # Aggregate subcarriers into a single disturbance
            avg_amp = np.mean(amplitudes) / 100.0  # normalize
            avg_phase = np.mean(phases)
            # Velocity from phase differential
            if self.last_disturbances:
                prev_phase = self.last_disturbances[-1].phase
                freq_shift = (avg_phase - prev_phase) * 10.0
            else:
                freq_shift = 0.0
            disturbance = RFDisturbance(
                band=self.band,
                phase=float(avg_phase),
                amplitude=float(min(1.0, abs(avg_amp))),
                angle_of_arrival=float(avg_phase % (2 * np.pi)),
                frequency_shift=float(freq_shift),
            )
            self.last_disturbances = [disturbance]
            return [disturbance]
        except Exception as e:
            logger.debug("[ESP32-CSI] read error: %s", e)
            return []


class MmWaveSensor(FrequencySensor):
    """
    Tier 3: mmWave radar sensor for fine surface detail.

    NEUROBIOLOGICAL ANALOGUE:
      mmWave is like tactile surface sensing β€” the koniocellular
      pathway's fine spatial detail. This is what lets Nima distinguish
      a soft couch from a rigid table at the same height.

    Hardware: $30 mmWave radar module (e.g., TI IWR6843, 60GHz).
    Provides point-cloud data with ~1cm resolution.

    Falls back to simulation if no hardware is connected.
    """

    def __init__(self, serial_port: Optional[str] = None) -> None:
        super().__init__(FrequencyBand.WIFI_5, wavelength_cm=0.5)
        self.serial_port = serial_port
        self._connected = False

    def connect(self) -> bool:
        # Similar to ESP32 β€” try serial connection
        return False  # simulation mode by default

    def capture(self) -> List[RFDisturbance]:
        if not self._active:
            return []
        # Simulation mode: generate high-resolution point disturbances
        n_points = np.random.randint(0, 5)
        return [RFDisturbance(
            band=self.band,
            phase=np.random.uniform(0, 2 * np.pi),
            amplitude=np.random.uniform(0.5, 1.0),
            angle_of_arrival=np.random.uniform(0, 2 * np.pi),
            frequency_shift=0.0,
        ) for _ in range(n_points)]


# ═══════════════════════════════════════════════════════════════════════════
# SENSOR FUSION (the "two frequencies adapt to each other" part)
# ═══════════════════════════════════════════════════════════════════════════

class SensorFusionEngine:
    """
    Fuses disturbances from multiple frequency bands into a unified 3D
    spatial percept.

    NEUROBIOLOGICAL ANALOGUE:
      This is V1/V2 opponent processing + stereopsis. The brain takes
      input from M-cells (coarse), P-cells (medium), and K-cells (fine)
      and fuses them into one seamless visual field. The key insight:
      each channel has different noise characteristics, and the fusion
      filter learns each channel's reliability over time.

    Implementation:
      Uses a Kalman filter to track entity positions over time, with
      each frequency band contributing at its resolution scale. The
      filter adapts: if one band is noisy, its weight drops; if it's
      consistent, its weight rises.

    The "adaptation" the user described:
      When two (or three) frequencies are active, they continuously
      calibrate against each other. If freq 1 says "entity at (2, 1.5)"
      and freq 2 says "entity at (2.1, 1.8)", the fusion engine learns
      the offset between them and corrects future readings. This is
      exactly how the brain calibrates left/right eye fusion.
    """

    # Kalman filter constants
    _DT = 0.1              # seconds per frame
    _PROCESS_NOISE = 0.5   # Q β€” how much we expect things to accelerate
    _MEAS_NOISE_POS = 0.3  # R β€” position measurement noise
    _MEAS_NOISE_VEL = 1.0  # R β€” velocity measurement noise
    _ENTITY_TIMEOUT_S = 10.0  # seconds before recycling an entity ID

    def __init__(self) -> None:
        self._entity_tracks: Dict[int, EntityPose] = {}  # entity_id β†’ current pose
        self._next_entity_id: int = 1
        self._recycled_ids: List[int] = []  # IDs available for reuse
        self._entity_last_seen: Dict[int, float] = {}  # entity_id β†’ timestamp
        self._band_reliability: Dict[FrequencyBand, float] = {
            FrequencyBand.SUB_GHZ: 0.5,
            FrequencyBand.WIFI_2_4: 0.5,
            FrequencyBand.WIFI_5: 0.5,
        }
        self._band_offsets: Dict[FrequencyBand, Tuple[float, float, float]] = {
            FrequencyBand.SUB_GHZ: (0.0, 0.0, 0.0),
            FrequencyBand.WIFI_2_4: (0.0, 0.0, 0.0),
            FrequencyBand.WIFI_5: (0.0, 0.0, 0.0),
        }
        # Kalman state: entity_id β†’ 6-vector [x, y, z, vx, vy, vz]
        self._kalman_state: Dict[int, np.ndarray] = {}
        # Kalman covariance: entity_id β†’ 6x6 matrix
        self._kalman_covariance: Dict[int, np.ndarray] = {}

    def fuse(self,
             disturbances_by_band: Dict[FrequencyBand, List[RFDisturbance]],
             tier: HardwareTier,
             ) -> Tuple[List[EntityPose], List[SurfacePoint]]:
        """
        Fuse disturbances from all active bands into entities + surfaces.

        Returns (entities, surfaces).
        """
        all_disturbances = []
        for band, disturbances in disturbances_by_band.items():
            offset = self._band_offsets[band]
            for d in disturbances:
                # Apply band-specific offset (the calibration adaptation)
                adjusted_angle = d.angle_of_arrival
                # Convert polar to Cartesian position estimate
                distance = 3.0 * d.amplitude  # rough: amplitude ∝ proximity
                x = distance * math.cos(adjusted_angle) + offset[0]
                y = distance * math.sin(adjusted_angle) + offset[1]
                z = 1.0 if band == FrequencyBand.WIFI_5 else 0.0  # 5GHz gives height
                all_disturbances.append({
                    "band": band,
                    "position": (x, y, z),
                    "amplitude": d.amplitude,
                    "velocity": d.frequency_shift / 100.0,
                    "angle": adjusted_angle,
                })

        # Cluster disturbances into entities
        entities = self._cluster_into_entities(all_disturbances, tier)

        # Generate surface points (walls, floor, furniture)
        surfaces = self._detect_surfaces(all_disturbances, tier)

        # Update band reliability based on consistency
        self._update_reliability(disturbances_by_band)

        return entities, surfaces

    def _allocate_entity_id(self) -> int:
        """Get an entity ID, recycling expired ones to prevent ID leak."""
        now = time.time()
        # Recycle stale IDs
        stale = [eid for eid, t in self._entity_last_seen.items()
                 if now - t > self._ENTITY_TIMEOUT_S]
        for eid in stale:
            self._recycled_ids.append(eid)
            self._entity_tracks.pop(eid, None)
            self._kalman_state.pop(eid, None)
            self._kalman_covariance.pop(eid, None)
            self._entity_last_seen.pop(eid, None)
        # Use recycled ID if available
        if self._recycled_ids:
            return self._recycled_ids.pop(0)
        eid = self._next_entity_id
        self._next_entity_id += 1
        return eid

    def _kalman_predict(self, entity_id: int) -> None:
        """Kalman predict step: advance state by _DT using constant-velocity model."""
        dt = self._DT
        # State transition: [x,y,z,vx,vy,vz] β€” position += velocity * dt
        F = np.eye(6)
        F[0, 3] = dt  # x += vx * dt
        F[1, 4] = dt  # y += vy * dt
        F[2, 5] = dt  # z += vz * dt

        # Process noise covariance
        q = self._PROCESS_NOISE
        Q = np.diag([q * dt**2, q * dt**2, q * dt**2, q, q, q])

        state = self._kalman_state[entity_id]
        cov = self._kalman_covariance[entity_id]

        # Predict
        state = F @ state
        cov = F @ cov @ F.T + Q

        self._kalman_state[entity_id] = state
        self._kalman_covariance[entity_id] = cov

    def _kalman_update(self, entity_id: int,
                       pos: Tuple[float, float, float],
                       vel: float) -> None:
        """Kalman update step: incorporate a new measurement."""
        state = self._kalman_state[entity_id]
        cov = self._kalman_covariance[entity_id]

        # Measurement: we observe [x, y, z, vx]
        H = np.zeros((4, 6))
        H[0, 0] = 1.0  # observe x
        H[1, 1] = 1.0  # observe y
        H[2, 2] = 1.0  # observe z
        H[3, 3] = 1.0  # observe vx

        R = np.diag([self._MEAS_NOISE_POS, self._MEAS_NOISE_POS,
                      self._MEAS_NOISE_POS, self._MEAS_NOISE_VEL])

        z = np.array([pos[0], pos[1], pos[2], vel])
        y = z - H @ state  # innovation
        S = H @ cov @ H.T + R  # innovation covariance
        K = cov @ H.T @ np.linalg.inv(S)  # Kalman gain

        state = state + K @ y
        cov = (np.eye(6) - K @ H) @ cov

        self._kalman_state[entity_id] = state
        self._kalman_covariance[entity_id] = cov

    def _cluster_into_entities(self,
                                disturbances: List[Dict[str, Any]],
                                tier: HardwareTier,
                                ) -> List[EntityPose]:
        """Cluster raw disturbances into entity tracks using Kalman filter."""
        if not disturbances:
            # Run predict-only on existing tracks (entities not seen this frame)
            for eid in list(self._kalman_state.keys()):
                self._kalman_predict(eid)
                s = self._kalman_state[eid]
                prev = self._entity_tracks.get(eid)
                if prev:
                    prev.position = (round(float(s[0]), 3),
                                     round(float(s[1]), 3),
                                     round(float(s[2]), 3))
                    prev.velocity = (round(float(s[3]), 3),
                                     round(float(s[4]), 3),
                                     round(float(s[5]), 3))
                    prev.last_seen = time.time()
            return list(self._entity_tracks.values())

        # Simple clustering: group disturbances within 0.5m of each other
        clusters: List[List[Dict[str, Any]]] = []
        for d in disturbances:
            placed = False
            for cluster in clusters:
                cx = np.mean([dd["position"][0] for dd in cluster])
                cy = np.mean([dd["position"][1] for dd in cluster])
                dist = math.sqrt((d["position"][0] - cx)**2 + (d["position"][1] - cy)**2)
                if dist < 0.5:
                    cluster.append(d)
                    placed = True
                    break
            if not placed:
                clusters.append([d])

        now = time.time()
        entities = []
        matched_eids: set = set()

        for cluster in clusters:
            avg_x = np.mean([d["position"][0] for d in cluster])
            avg_y = np.mean([d["position"][1] for d in cluster])
            avg_z = np.mean([d["position"][2] for d in cluster])
            avg_vel = np.mean([d["velocity"] for d in cluster])
            confidence = np.mean([d["amplitude"] for d in cluster])

            # Try to match to an existing Kalman track
            best_eid = None
            best_dist = 1.0  # max match distance
            for eid, prev in self._entity_tracks.items():
                if eid in matched_eids:
                    continue
                dx = avg_x - prev.position[0]
                dy = avg_y - prev.position[1]
                d = math.sqrt(dx*dx + dy*dy)
                if d < best_dist:
                    best_dist = d
                    best_eid = eid

            if best_eid is not None:
                # Update existing track with Kalman filter
                self._kalman_predict(best_eid)
                self._kalman_update(best_eid, (avg_x, avg_y, avg_z), avg_vel)
                s = self._kalman_state[best_eid]
                entity_id = best_eid
                pos = (round(float(s[0]), 3),
                       round(float(s[1]), 3),
                       round(float(s[2]), 3))
                vel = (round(float(s[3]), 3),
                       round(float(s[4]), 3),
                       round(float(s[5]), 3))
                matched_eids.add(best_eid)
            else:
                # New entity β€” initialize Kalman track
                entity_id = self._allocate_entity_id()
                init_state = np.array([avg_x, avg_y, avg_z, avg_vel, 0.0, 0.0])
                init_cov = np.diag([0.5, 0.5, 0.5, 1.0, 1.0, 1.0])
                self._kalman_state[entity_id] = init_state
                self._kalman_covariance[entity_id] = init_cov
                pos = (round(float(avg_x), 3),
                       round(float(avg_y), 3),
                       round(float(avg_z), 3))
                vel = (round(float(avg_vel), 3), 0.0, 0.0)

            # Height estimate depends on tier
            z_val = pos[2] if best_eid else avg_z
            if tier.value >= 2:
                height = 1.7 if z_val > 0.5 else 0.4  # standing vs furniture
            else:
                height = 1.7  # default assumption

            # Pose type
            if height < 0.6:
                pose_type = "sitting" if height > 0.3 else "lying"
            else:
                pose_type = "standing"

            entity = EntityPose(
                entity_id=entity_id,
                position=pos,
                velocity=vel,
                height_estimate=round(height, 3),
                confidence=round(float(confidence), 3),
                pose_type=pose_type,
                last_seen=now,
            )
            entities.append(entity)
            self._entity_tracks[entity_id] = entity
            self._entity_last_seen[entity_id] = now

        # Purge tracks that weren't matched and haven't been seen
        stale = [eid for eid in self._entity_tracks
                 if eid not in matched_eids and (now - self._entity_last_seen.get(eid, 0)) > self._ENTITY_TIMEOUT_S]
        for eid in stale:
            self._recycled_ids.append(eid)
            del self._entity_tracks[eid]
            self._kalman_state.pop(eid, None)
            self._kalman_covariance.pop(eid, None)
            self._entity_last_seen.pop(eid, None)

        return entities

    def _detect_surfaces(self,
                         disturbances: List[Dict[str, Any]],
                         tier: HardwareTier,
                         ) -> List[SurfacePoint]:
        """Detect surface points (walls, floor, furniture).

        Works at ALL tiers:
          - Tier 0: Default 4x4m room outline (coarse grid)
          - Tier 1: Adds furniture estimates from 2.4GHz reflection clusters
          - Tier 2+: Same as Tier 1 but with higher confidence from
            stereoscopic height data
          - Tier 3: Adds material classification from mmWave surface
            scattering patterns
        """
        surfaces = []

        # All tiers: generate base room floor + walls
        # Higher tiers use finer grids
        if tier.value <= 1:
            grid_n = 9  # 0.5m spacing
        elif tier.value == 2:
            grid_n = 17  # 0.25m spacing
        else:
            grid_n = 33  # ~0.12m spacing

        room_w, room_h, room_z = 4.0, 4.0, 2.5
        floor_conf = 0.7 + tier.value * 0.05

        # Floor
        for x in np.linspace(0, room_w, grid_n):
            for y in np.linspace(0, room_h, grid_n):
                surfaces.append(SurfacePoint(
                    position=(round(x, 2), round(y, 2), 0.0),
                    surface_type="floor",
                    height=0.0,
                    material="hard",
                    confidence=min(1.0, floor_conf),
                ))

        # Walls
        wall_conf = 0.8 + tier.value * 0.05
        for x in np.linspace(0, room_w, grid_n):
            for wall_y in [0.0, room_h]:
                surfaces.append(SurfacePoint(
                    position=(round(x, 2), wall_y, 1.25),
                    surface_type="wall",
                    height=room_z,
                    material="rigid",
                    confidence=min(1.0, wall_conf),
                ))
        for y in np.linspace(0, room_h, grid_n):
            for wall_x in [0.0, room_w]:
                surfaces.append(SurfacePoint(
                    position=(wall_x, round(y, 2), 1.25),
                    surface_type="wall",
                    height=room_z,
                    material="rigid",
                    confidence=min(1.0, wall_conf),
                ))

        # Tier 1+: infer furniture from stationary disturbance clusters
        if tier.value >= 1 and disturbances:
            furniture_clusters = self._infer_furniture(disturbances, tier)
            surfaces.extend(furniture_clusters)

        # Tier 3: material classification from mmWave data
        if tier.value >= 3 and disturbances:
            self._classify_materials(surfaces, disturbances)

        return surfaces

    def _infer_furniture(self,
                         disturbances: List[Dict[str, Any]],
                         tier: HardwareTier,
                         ) -> List[SurfacePoint]:
        """Infer furniture surfaces from disturbance clusters.

        Stationary objects (near-zero velocity) that persist across frames
        are likely furniture. This creates SurfacePoints at those locations
        so the affordance engine knows they exist.
        """
        furniture = []
        # Cluster disturbances by position
        static = [d for d in disturbances if abs(d["velocity"]) < 0.05]
        if not static:
            return furniture

        # Group nearby static disturbances
        clusters: List[List[Dict[str, Any]]] = []
        for d in static:
            placed = False
            for cluster in clusters:
                cx = np.mean([dd["position"][0] for dd in cluster])
                cy = np.mean([dd["position"][1] for dd in cluster])
                dist = math.sqrt((d["position"][0] - cx)**2 + (d["position"][1] - cy)**2)
                if dist < 0.5:
                    cluster.append(d)
                    placed = True
                    break
            if not placed:
                clusters.append([d])

        conf = 0.5 + tier.value * 0.1
        for cluster in clusters:
            avg_x = np.mean([d["position"][0] for d in cluster])
            avg_y = np.mean([d["position"][1] for d in cluster])
            avg_z = np.mean([d["position"][2] for d in cluster])
            amp = np.mean([d["amplitude"] for d in cluster])

            # Infer furniture properties from position and signal
            height = max(0.1, avg_z) if avg_z > 0 else 0.4
            material = "soft" if amp > 0.6 else "rigid"  # soft materials absorb more RF

            # Generate a small cluster of surface points (not just one)
            for dx in [-0.2, 0.0, 0.2]:
                for dy in [-0.2, 0.0, 0.2]:
                    px = avg_x + dx
                    py = avg_y + dy
                    if 0 <= px <= 4.0 and 0 <= py <= 4.0:
                        furniture.append(SurfacePoint(
                            position=(round(px, 2), round(py, 2), round(avg_z, 2)),
                            surface_type="furniture",
                            height=round(height, 2),
                            material=material,
                            confidence=min(1.0, round(conf, 2)),
                        ))
        return furniture

    def _classify_materials(self,
                            surfaces: List[SurfacePoint],
                            disturbances: List[Dict[str, Any]],
                            ) -> None:
        """Tier 3: classify surface materials using mmWave scattering.

        mmWave at 60GHz scatters differently off different materials:
          - Metal: strong specular reflection (high amplitude, narrow)
          - Wood: moderate diffuse scattering
          - Fabric: strong absorption (low amplitude)
          - Glass: strong reflection with specific angle

        This refines the 'material' field on existing surface points
        based on mmWave disturbance characteristics near each surface.
        """
        mmwave = [d for d in disturbances
                  if d.get("band") == FrequencyBand.WIFI_5 or d.get("band") == FrequencyBand.WIFI_5.value]
        if not mmwave:
            return
        # For each furniture surface point, check nearby mmWave data
        for sp in surfaces:
            if sp.surface_type != "furniture":
                continue
            nearby = [d for d in mmwave
                      if math.sqrt((d["position"][0] - sp.position[0])**2 +
                                   (d["position"][1] - sp.position[1])**2) < 0.5]
            if not nearby:
                continue
            avg_amp = np.mean([d["amplitude"] for d in nearby])
            if avg_amp > 0.85:
                sp.material = "rigid"  # strong reflection = hard surface
            elif avg_amp < 0.4:
                sp.material = "soft"   # absorption = fabric/foam
            sp.confidence = min(1.0, sp.confidence + 0.1)

    def _update_reliability(self, disturbances_by_band: Dict[FrequencyBand, List[RFDisturbance]]) -> None:
        """
        Adapt band reliability based on consistency.
        If a band consistently agrees with others, its reliability rises.
        If it's noisy, its reliability drops.

        This is the "two frequencies adapt to each other" mechanism.
        """
        if len(disturbances_by_band) < 2:
            return
        # Compare amplitude consistency across bands
        bands = list(disturbances_by_band.keys())
        for i, band_a in enumerate(bands):
            for band_b in bands[i+1:]:
                dists_a = disturbances_by_band.get(band_a, [])
                dists_b = disturbances_by_band.get(band_b, [])
                if not dists_a or not dists_b:
                    continue
                amp_a = np.mean([d.amplitude for d in dists_a])
                amp_b = np.mean([d.amplitude for d in dists_b])
                agreement = 1.0 - abs(amp_a - amp_b)
                # Nudge reliability toward agreement level
                for band in (band_a, band_b):
                    current = self._band_reliability[band]
                    self._band_reliability[band] = current * 0.95 + agreement * 0.05

    def get_stats(self) -> Dict[str, Any]:
        return {
            "entity_tracks": len(self._entity_tracks),
            "band_reliability": {b.value: round(r, 3) for b, r in self._band_reliability.items()},
            "band_offsets": {b.value: list(o) for b, o in self._band_offsets.items()},
        }


# ═══════════════════════════════════════════════════════════════════════════
# THE VISION COMPOSITE (main orchestrator)
# ═══════════════════════════════════════════════════════════════════════════

class SyntheticVisionComposite:
    """
    The main vision system orchestrator.

    This replaces the stub class from Synthetic_Vision_Composite.txt
    with a real implementation that:
      1. Captures from all available frequency sensors
      2. Fuses them into a 3D spatial map
      3. Generates affordances (sit-able, walk-able, jump-able)
      4. Tracks Nima's own position within the room

    Usage:
        vision = SyntheticVisionComposite()
        vision.initialize()
        spatial_map = vision.process_frame()
        # spatial_map.entities = detected people/objects
        # spatial_map.surfaces = 3D point cloud
        # spatial_map.affordances = what Nima can do where
    """

    def __init__(self) -> None:
        # Initialize all three frequency sensors (each band is distinct)
        self.sensor_sub_ghz = SubGHzSensor()       # Tier 0: ~900MHz coarse structure (M-cells)
        self.sensor_2_4ghz = SoftwareRSSISensor()   # Tier 0: 2.4GHz furniture/bodies (P-cells)
        self.sensor_5ghz = MmWaveSensor()           # Tier 3: 5GHz/mmWave surface detail (K-cells)

        # Try to upgrade to ESP32 CSI if hardware is available
        esp32 = ESP32CSISensor()
        if esp32.connect():
            self.sensor_2_4ghz = esp32  # upgrade to real CSI

        # The fusion engine
        self.fusion = SensorFusionEngine()

        # State
        self.is_active = False
        self.last_map: Optional[SpatialMap] = None
        self._frame_count = 0
        self._thread: Optional[threading.Thread] = None
        self._lock = threading.Lock()

        # Detect hardware tier
        self.tier = self._detect_tier()

        # Nima's own position in the room (she moves around)
        self.nima_position: Tuple[float, float, float] = (2.0, 2.0, 0.0)
        self.nima_target: Optional[Tuple[float, float]] = None  # where she's walking to

    def _detect_tier(self) -> HardwareTier:
        """Auto-detect available hardware tier."""
        if isinstance(self.sensor_5ghz, MmWaveSensor) and self.sensor_5ghz.connect():
            return HardwareTier.TIER_3_TRI_FREQ
        if isinstance(self.sensor_2_4ghz, ESP32CSISensor) and self.sensor_2_4ghz._connected:
            return HardwareTier.TIER_1_SINGLE_ESP32
        return HardwareTier.TIER_0_SOFTWARE

    def initialize(self) -> bool:
        """Start all sensors."""
        self.sensor_sub_ghz.start()
        self.sensor_2_4ghz.start()
        if self.tier.value >= 3:
            self.sensor_5ghz.start()
        self.is_active = True
        logger.info("[Vision] initialized (tier=%s)", self.tier.name)
        return True

    def process_frame(self) -> SpatialMap:
        """Capture + fuse one frame of spatial perception."""
        if not self.is_active:
            return SpatialMap()

        # Capture from all active sensors
        disturbances_by_band: Dict[FrequencyBand, List[RFDisturbance]] = {}
        disturbances_by_band[FrequencyBand.SUB_GHZ] = self.sensor_sub_ghz.capture()
        disturbances_by_band[FrequencyBand.WIFI_2_4] = self.sensor_2_4ghz.capture()
        if self.tier.value >= 3:
            disturbances_by_band[FrequencyBand.WIFI_5] = self.sensor_5ghz.capture()

        # Fuse into entities + surfaces
        entities, surfaces = self.fusion.fuse(disturbances_by_band, self.tier)

        # Build the spatial map
        spatial_map = SpatialMap(
            entities=entities,
            surfaces=surfaces,
            room_bounds={"x_min": 0, "x_max": 4, "y_min": 0, "y_max": 4, "z_min": 0, "z_max": 2.5},
            walkable_area=self._compute_walkable_area(surfaces),
            affordances=self._compute_affordances(surfaces, entities),
            tier_used=self.tier,
        )

        # Update Nima's position (she walks toward her target)
        self._update_nima_position()

        with self._lock:
            self.last_map = spatial_map
            self._frame_count += 1

        return spatial_map

    def _compute_walkable_area(self, surfaces: List[SurfacePoint]) -> List[Tuple[float, float]]:
        """Compute the 2D polygon of walkable floor space."""
        walkable = []
        for s in surfaces:
            if s.surface_type == "floor":
                walkable.append((s.position[0], s.position[1]))
        return walkable

    def _compute_affordances(self,
                              surfaces: List[SurfacePoint],
                              entities: List[EntityPose],
                              ) -> List[Dict[str, Any]]:
        """
        Compute affordances β€” what Nima can do at each location.

        This is the key innovation: surfaces aren't just obstacles, they
        have ACTION POSSIBILITIES. A couch at 0.4m = sit-able. A table
        at 0.8m = rest-hand-able. A door at 2.0m = walk-through-able.

        Based on J.J. Gibson's affordance theory (1977) + Eleanor
        Gibson's empirical work on infant perception.
        """
        affordances = []
        for s in surfaces:
            aff = {
                "position": list(s.position[:2]),
                "height": s.height,
                "type": s.surface_type,
                "actions": [],
            }
            if s.surface_type == "floor":
                aff["actions"] = ["walk", "stand", "lie_down"]
            elif s.surface_type == "furniture":
                if s.height < 0.3:
                    aff["actions"] = ["step_over", "sit_on_floor_next_to"]
                elif s.height < 0.6 and s.material == "soft":
                    aff["actions"] = ["sit", "lie_down", "jump_on"]
                elif s.height < 0.6 and s.material == "rigid":
                    aff["actions"] = ["sit", "rest_hand"]
                elif s.height < 1.0:
                    aff["actions"] = ["rest_hand", "lean_on"]
                else:
                    aff["actions"] = ["avoid"]
            elif s.surface_type == "wall":
                aff["actions"] = ["avoid"]
            if aff["actions"]:
                affordances.append(aff)
        return affordances

    def _update_nima_position(self) -> None:
        """Move Nima toward her target position (if she has one)."""
        if self.nima_target is None:
            return
        tx, ty = self.nima_target
        cx, cy, cz = self.nima_position
        dx = tx - cx
        dy = ty - cy
        dist = math.sqrt(dx*dx + dy*dy)
        if dist < 0.1:  # arrived
            self.nima_target = None
            return
        # Walk at 0.5 m/s per frame (simulated)
        speed = min(0.5, dist)
        self.nima_position = (
            round(cx + dx / dist * speed, 3),
            round(cy + dy / dist * speed, 3),
            cz,
        )

    def set_nima_target(self, x: float, y: float) -> None:
        """Tell Nima to walk to a position in the room."""
        self.nima_target = (x, y)
        logger.info("[Vision] Nima walking to (%.1f, %.1f)", x, y)

    def get_nima_state(self) -> Dict[str, Any]:
        """Get Nima's current position + movement state."""
        return {
            "position": list(self.nima_position),
            "target": list(self.nima_target) if self.nima_target else None,
            "is_moving": self.nima_target is not None,
        }

    def get_stats(self) -> Dict[str, Any]:
        return {
            "is_active": self.is_active,
            "tier": self.tier.name,
            "frame_count": self._frame_count,
            "fusion": self.fusion.get_stats(),
            "nima_state": self.get_nima_state(),
            "last_map": self.last_map.to_dict() if self.last_map else None,
        }

    def shutdown(self) -> None:
        self.sensor_sub_ghz.stop()
        self.sensor_2_4ghz.stop()
        self.sensor_5ghz.stop()
        self.is_active = False
        logger.info("[Vision] shutdown complete")


# ═══════════════════════════════════════════════════════════════════════════
# SELF-TEST
# ═══════════════════════════════════════════════════════════════════════════

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format="%(asctime)s [%(levelname)s] %(message)s")

    print("=== Nima Synthetic Vision β€” Self Test ===\n")

    vision = SyntheticVisionComposite()
    vision.initialize()
    print(f"Hardware tier: {vision.tier.name}")
    print(f"Nima position: {vision.nima_position}")
    print()

    # Process a few frames
    for i in range(3):
        spatial_map = vision.process_frame()
        print(f"Frame {i+1}:")
        print(f"  Entities detected: {len(spatial_map.entities)}")
        for e in spatial_map.entities:
            print(f"    #{e.entity_id} at {e.position} ({e.pose_type}, h={e.height_estimate}m)")
        print(f"  Surface points: {len(spatial_map.surfaces)}")
        print(f"  Affordances: {len(spatial_map.affordances)}")
        if spatial_map.affordances:
            for a in spatial_map.affordances[:3]:
                print(f"    at {a['position']}: {a['actions']}")
        print(f"  Nima at: {vision.nima_position}")
        print()

    # Test movement
    print("=== Testing Nima movement ===")
    vision.set_nima_target(3.5, 0.5)
    for i in range(5):
        vision.process_frame()
        print(f"  Step {i+1}: Nima at {vision.nima_position} "
              f"(moving={vision.get_nima_state()['is_moving']})")

    print(f"\n=== Fusion stats ===")
    print(json.dumps(vision.get_stats()["fusion"], indent=2))

    vision.shutdown()
    print("\n=== Vision self-test PASSED ===")