diff --git a/TODO.md b/TODO.md index 233d60d..45e20e7 100644 --- a/TODO.md +++ b/TODO.md @@ -23,6 +23,15 @@ With the CAM pinger on and a listener that decodes yawRate: VAMs keep going at their rate, with no gap and no time_regression on the micrOBU. - [ ] Ride with the phone on the handlebar, then in a bag: the yaw rate's sign and size match the turns (left and right, roundabout). +- [ ] Yaw rate sign on air (fixed 2026-11-18: `CamUperCodec` and `CamParser` now flip the ETSI/CiT + left-positive value into the app's clockwise-positive one). Turn the pinging phone left: a + standard decoder (asn1tools, or the CiT One's `v2x-uca/output/json/cam`) shows a positive + YawRateValue, and the app's own decode of the same CAM reads a negative yaw rate (left, in the + app's clockwise-positive convention). + The logs in `databases/` (not committed) already settled the CiT One side: its CAM topic is + left-positive (316 of 316 turning samples), its `obu_gnss` `yawRate_degps` right-positive + despite the API document (27 of 27, bacospat ride of 2026-03-18). Only 27 samples from one + ride: re-check on the next CiT One ride with turns. ### obu-firmware on Niklas Dathe's microbu-firmware (added 2026-11-10, bench run 2026-11-11) diff --git a/app/src/main/java/com/hawhamburg/micr0bu/domain/asn1/CamUperCodec.kt b/app/src/main/java/com/hawhamburg/micr0bu/domain/asn1/CamUperCodec.kt index a97b66e..68a3fac 100644 --- a/app/src/main/java/com/hawhamburg/micr0bu/domain/asn1/CamUperCodec.kt +++ b/app/src/main/java/com/hawhamburg/micr0bu/domain/asn1/CamUperCodec.kt @@ -144,8 +144,11 @@ object CamUperCodec { bw.putBits(0, 1) // extension bit: value is in the root list bw.putBits(2, 2) // curvatureCalculationMode: unavailable(2) + // YawRateValue is positive to the LEFT (degSec-000-01ToLeft(1), TS 102 894-2), the opposite + // of Cam.yawRateDps (positive clockwise, i.e. to the right). Until 2026-11-18 neither side + // negated, so every CAM this phone sent turned the wrong way for a standard receiver. val yawRateCentiDegS = cam.yawRateDps - ?.let { (it * 100.0).roundToInt().coerceIn(-32766, 32766) } + ?.let { (-it * 100.0).roundToInt().coerceIn(-32766, 32766) } ?: YAW_RATE_UNAVAILABLE bw.putBits(yawRateCentiDegS - (-32766), 16) // YawRateConfidence has NINE enumerands (degSec-000-01(0) .. unavailable(8), see @@ -306,7 +309,8 @@ object CamUperCodec { val yawRateRaw = br.getBitsInt(16) + (-32766) br.getBits(4) // yawRateConfidence: 9 enumerands -> 4 bits, see the note in encode() - val yawRateDps = if (yawRateRaw == YAW_RATE_UNAVAILABLE) null else yawRateRaw / 100.0 + // Left-positive on air, clockwise-positive in Cam: negate, as encode() does. + val yawRateDps = if (yawRateRaw == YAW_RATE_UNAVAILABLE) null else -yawRateRaw / 100.0 // Everything after yawRate is deliberately left unread: the 7 optional high-frequency // fields, then lowFrequencyContainer (vehicleRole / exteriorLights / pathHistory), then diff --git a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/Cam.kt b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/Cam.kt index 1ce4f17..94eb3d3 100644 --- a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/Cam.kt +++ b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/Cam.kt @@ -45,8 +45,13 @@ data class Cam( val headingDeg: Double, /** - * Yaw rate, degrees per second, where available (positive = turning clockwise). - * Carried by both `v2x/rx/obu_gnss` (ego, ~4 Hz) and `v2x-uca/output/json/cam` (remote). + * Yaw rate, degrees per second, where available (positive = turning clockwise, to the right, + * like [headingDeg] increasing). Carried by both `v2x/rx/obu_gnss` (ego, ~4 Hz) and + * `v2x-uca/output/json/cam` (remote). + * + * The CAM's YawRateValue and the CiT One's CAM topic count the other way, positive to the + * left, so `CamUperCodec` and `CamParser` flip the sign. The CiT One's `obu_gnss` topic is + * already positive to the right in practice, despite its document, so `ObuGnssParser` does not. * A much more direct "is this road user turning" signal than heading-delta heuristics — * see [com.hawhamburg.micr0bu.domain.usecase.UseCaseDetectionEngine]. */ diff --git a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/CamParser.kt b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/CamParser.kt index 477baf2..e8f58f4 100644 --- a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/CamParser.kt +++ b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/CamParser.kt @@ -33,7 +33,10 @@ object CamParser { val speed = JsonFieldReader.firstDouble(obj, "speed_mps", "speed", "speedMps") ?: 0.0 val heading = JsonFieldReader.firstDouble(obj, "heading_deg", "heading", "headingDeg") ?: 0.0 + // Positive to the left on this topic (CiT MQTT API v6, v2x-uca/output/json/cam), as in the CAM + // itself; negated into Cam.yawRateDps, which is positive clockwise. val yawRate = JsonFieldReader.firstDouble(obj, "yawRate_dps", "yawRateDps", "yaw_rate_dps", "yawRate") + ?.let { -it } val driveDirection = JsonFieldReader.firstInt(obj, "driveDirection", "drive_direction") val vehicleLength = JsonFieldReader.firstDouble(obj, "vehicleLength", "vehicle_length") diff --git a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/ObuGnssParser.kt b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/ObuGnssParser.kt index a1e7e09..a2968b7 100644 --- a/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/ObuGnssParser.kt +++ b/app/src/main/java/com/hawhamburg/micr0bu/domain/cam/ObuGnssParser.kt @@ -33,7 +33,13 @@ object ObuGnssParser { val speed = JsonFieldReader.firstDouble(obj, "speed_mps", "speed", "speedMps") ?: 0.0 val heading = JsonFieldReader.firstDouble(obj, "heading_deg", "heading", "headingDeg") ?: 0.0 - val yawRate = JsonFieldReader.firstDouble(obj, "yawRate_dps", "yawRateDps", "yaw_rate_dps", "yawRate") + // `yawRate_degps` is this topic's name (CiT MQTT API v6, Listing 2.1, own_info, and every + // logged payload). The document says positive to the left, but the CiT One sends it positive + // to the RIGHT: in the 2026-03-18 bacospat log, all 27 one-second windows of turning at over + // 4 m/s had it with the same sign as the clockwise heading change. That is already + // Cam.yawRateDps's convention, so it is taken as it is - unlike the CAM topic (CamParser), + // which the logs show is left-positive, as documented. + val yawRate = JsonFieldReader.firstDouble(obj, "yawRate_degps", "yawRate_dps", "yawRateDps", "yaw_rate_dps", "yawRate") return Cam( stationId = stationId, diff --git a/app/src/test/java/com/hawhamburg/micr0bu/CamEncodeGoldenTest.kt b/app/src/test/java/com/hawhamburg/micr0bu/CamEncodeGoldenTest.kt index e02f09f..ee436af 100644 --- a/app/src/test/java/com/hawhamburg/micr0bu/CamEncodeGoldenTest.kt +++ b/app/src/test/java/com/hawhamburg/micr0bu/CamEncodeGoldenTest.kt @@ -61,6 +61,29 @@ class CamEncodeGoldenTest { assertEquals(expectedHex, encoded) } + /** + * [referenceCam] turning right at 12.34 deg/s, encoded by asn1tools with `yawRateValue` -1234: + * YawRateValue is positive to the LEFT (TS 102 894-2), while [Cam.yawRateDps] is positive + * clockwise. Only the yawRate bits differ from [expectedHex]. Generated 2026-11-18 by decoding + * [expectedHex] with asn1tools (cam_1_4_1 + cdd_1_3_1_1), setting the value, and re-encoding. + */ + private val rightTurnHex = + "0202000f423f4a88402ab215af6e286477dffffffc23b7743e0027ffc0d0fe0118329337fee9ecb2000000" + + @Test + fun `a right turn goes on air as a negative YawRateValue`() { + val encoded = CamUperCodec.encode(referenceCam.copy(yawRateDps = 12.34)) + .joinToString("") { "%02x".format(it) } + assertEquals(rightTurnHex, encoded) + } + + @Test + fun `a negative YawRateValue from the air is a right turn`() { + val bytes = rightTurnHex.chunked(2).map { it.toInt(16).toByte() }.toByteArray() + val cam = requireNotNull(CamUperCodec.decode(bytes, referenceCam.timestamp)) + assertEquals(12.34, cam.yawRateDps!!, 1e-9) + } + @Test fun `own decoder agrees with the encoder on every field it reads`() { // Self-consistency is necessary but NOT sufficient - see the class KDoc. This guards the diff --git a/app/src/test/java/com/hawhamburg/micr0bu/YawRateJsonSignTest.kt b/app/src/test/java/com/hawhamburg/micr0bu/YawRateJsonSignTest.kt new file mode 100644 index 0000000..7c83058 --- /dev/null +++ b/app/src/test/java/com/hawhamburg/micr0bu/YawRateJsonSignTest.kt @@ -0,0 +1,37 @@ +package com.hawhamburg.micr0bu + +import com.hawhamburg.micr0bu.domain.cam.CamParser +import com.hawhamburg.micr0bu.domain.cam.ObuGnssParser +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +/** + * [com.hawhamburg.micr0bu.domain.cam.Cam.yawRateDps] is positive clockwise. The CiT One's CAM topic + * is positive to the left (documented, and 316 of 316 turning samples in the logged runs), so + * [CamParser] flips it. Its `obu_gnss` topic is documented as left-positive too, but the logs show + * it positive to the right (27 of 27), so [ObuGnssParser] keeps it. + */ +class YawRateJsonSignTest { + + @Test + fun `CAM topic - positive is a left turn, so it becomes negative`() { + val json = """{"stationID": 42, "stationType": 5, "latitude": 53.55, "longitude": 10.02, + "yawRate_dps": 15.0}""" + assertEquals(-15.0, CamParser.parse(json, ownStationId = null)!!.yawRateDps!!, 1e-9) + } + + @Test + fun `CAM topic - a missing yaw rate stays unavailable`() { + val json = """{"stationID": 42, "stationType": 5, "latitude": 53.55, "longitude": 10.02}""" + assertNull(CamParser.parse(json, ownStationId = null)!!.yawRateDps) + } + + @Test + fun `obu_gnss - yawRate_degps is read as it is, since the CiT One sends it right-positive`() { + // own_info as in Listing 2.1, with the position placed where this parser reads it. + val json = """{"own_info": {"stationID": 1345678, "stationType": 2, + "latitude": 53.55, "longitude": 10.02, "yawRate_degps": -7.5}}""" + assertEquals(-7.5, ObuGnssParser.parseEgo(json)!!.yawRateDps!!, 1e-9) + } +}