Decode MAPEM and show traffic lights per lane
MapemUperCodec decodes MAPEM (MapData) from the ESP32-C5 (BTP port 2003) and the CiT One's v2x/rx/mapem topic: intersection and lane names, the reference point, lane types and directions, all node offset sizes and node attributes, and each lane's connections with their signal groups. It returns null on what it cannot size (extensions, regional data, computed lanes) rather than guess. TrafficLights.join places SPATEM signal groups on the MAPEM's ingress lanes, gives each connection its turn (its maneuver, else the lane's, else the angle between the lanes), picks the MAPEM whose revision the SPATEM names when two are on air, and matches the rider to an ingress lane from position and heading. The dashboard card then names the intersection and shows the rider's lane, the distance to its stop line and one chip per signal group with its turn arrows; the V2X map puts the light on the reference point and draws the lanes in their signal colours. Tested on recorded messages only: every distinct MAPEM on record (17 from the 2026-03-18 drive, 288-1348 bytes, plus the bench RSU's) decodes field for field like asn1tools; with a recorded SPATEM of the same intersection and revision, 340 of 341 signalled exits find their movement (3/165's group 65 never appears in its SPATEMs), and the geometric turn agrees with 198 of 201 stated maneuvers. tools/mapem_corpus.py rebuilds the fixtures.
This commit is contained in:
@@ -20,7 +20,11 @@ import com.hawhamburg.micr0bu.data.transport.OutgoingMessage
|
||||
import com.hawhamburg.micr0bu.data.transport.StationStatus
|
||||
import com.hawhamburg.micr0bu.domain.denm.DenmEvent
|
||||
import com.hawhamburg.micr0bu.domain.denm.DenmParser
|
||||
import com.hawhamburg.micr0bu.domain.map.IntersectionGeometry
|
||||
import com.hawhamburg.micr0bu.domain.map.MapPoint
|
||||
import com.hawhamburg.micr0bu.domain.spat.SpatIntersection
|
||||
import com.hawhamburg.micr0bu.domain.spat.TrafficLight
|
||||
import com.hawhamburg.micr0bu.domain.spat.TrafficLights
|
||||
import com.hawhamburg.micr0bu.domain.denm.DenmUseCase
|
||||
import com.hawhamburg.micr0bu.domain.usecase.UseCaseAlert
|
||||
import com.hawhamburg.micr0bu.domain.usecase.UseCaseType
|
||||
@@ -269,6 +273,47 @@ class MqttViewModel @Inject constructor(
|
||||
.sortedByDescending { it.timestamp }
|
||||
}.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())
|
||||
|
||||
/**
|
||||
* Intersection geometry heard over MAPEM, oldest first. Kept per intersection AND revision:
|
||||
* two revisions of one intersection can be on air at once, and a SPATEM's signal groups mean
|
||||
* whatever its own revision's MAPEM says ([TrafficLights.pickGeometry]).
|
||||
*/
|
||||
private val mapGeometries: StateFlow<List<IntersectionGeometry>> = combine(
|
||||
camUseCaseRepository.decodedMap
|
||||
.runningFold(emptyMap<String, Pair<IntersectionGeometry, Long>>()) { acc, map ->
|
||||
acc + map.intersections.associate { "${it.key}#${it.revision}" to (it to map.timestamp) }
|
||||
},
|
||||
tickerFlow(MAP_EXPIRY_TICK_MS),
|
||||
) { byKey, _ ->
|
||||
val now = System.currentTimeMillis()
|
||||
byKey.values
|
||||
.filter { (_, heardAt) -> now - heardAt <= MAP_TTL_MS }
|
||||
.sortedBy { (_, heardAt) -> heardAt }
|
||||
.map { (geometry, _) -> geometry }
|
||||
}.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())
|
||||
|
||||
/**
|
||||
* [spatIntersections] with each SPATEM's signal groups placed on its MAPEM's lanes, and the
|
||||
* rider's own approach picked from their position and heading. An intersection whose MAPEM has
|
||||
* not been heard (yet) is still listed, with no lanes: its signal groups stay bare numbers.
|
||||
*/
|
||||
val trafficLights: StateFlow<List<TrafficLight>> = combine(
|
||||
spatIntersections,
|
||||
mapGeometries,
|
||||
camUseCaseRepository.ownPosition,
|
||||
) { spats, geometries, own ->
|
||||
val byKey = geometries.groupBy { it.key }
|
||||
spats.map { spat ->
|
||||
TrafficLights.join(
|
||||
signal = spat,
|
||||
geometry = TrafficLights.pickGeometry(byKey[spat.key].orEmpty(), spat.state.revision),
|
||||
own = own?.let { MapPoint(it.latitude, it.longitude) },
|
||||
ownHeadingDeg = own?.headingDeg,
|
||||
ownSpeedMps = own?.speedMps,
|
||||
)
|
||||
}
|
||||
}.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())
|
||||
|
||||
/** Emits immediately, then every [periodMs], purely to re-trigger a time-dependent combine. */
|
||||
private fun tickerFlow(periodMs: Long): Flow<Long> = flow {
|
||||
while (true) {
|
||||
@@ -299,6 +344,13 @@ class MqttViewModel @Inject constructor(
|
||||
const val SPAT_TTL_MS = 15_000L
|
||||
const val SPAT_EXPIRY_TICK_MS = 2_000L
|
||||
|
||||
/**
|
||||
* MAPEM repeats at about 1 Hz and its geometry is static, so a minute of silence is generous;
|
||||
* a light is only ever shown while its SPATEM is live anyway.
|
||||
*/
|
||||
const val MAP_TTL_MS = 60_000L
|
||||
const val MAP_EXPIRY_TICK_MS = 5_000L
|
||||
|
||||
/** RSU CAMs arrive at ~2 Hz, same as any other station, so the same window applies. */
|
||||
const val RSU_TTL_MS = 15_000L
|
||||
const val RSU_EXPIRY_TICK_MS = 2_000L
|
||||
|
||||
Reference in New Issue
Block a user