Send and read the CAM yaw rate with the ETSI sign

YawRateValue is positive to the left (degSec-000-01ToLeft(1), TS 102 894-2),
while Cam.yawRateDps is positive clockwise, to the right, which is what the
use-case engine and its heading-trend fallback assume. CamUperCodec used the
value as it is in both directions, so every CAM this phone sent showed a
standard receiver the opposite turn, and received CAMs were read the wrong
way round in turn detection. It now negates on encode and decode. The
expected bytes for a right turn in CamEncodeGoldenTest come from asn1tools,
not from this encoder.

The CiT One's CAM topic (v2x-uca/output/json/cam) is left-positive too, as
its API document says and as 316 of 316 turning samples in the logged runs
confirm, so CamParser negates as well. Its obu_gnss topic is documented as
left-positive but sends yawRate_degps right-positive in practice (27 of 27
turning windows in the 2026-03-18 bacospat log), so ObuGnssParser keeps the
value and now reads that documented key, which it used to miss.
This commit is contained in:
Ashin Walpola
2026-11-18 14:56:17 +01:00
parent 727c9116dd
commit 6fa91d1702
7 changed files with 92 additions and 5 deletions
@@ -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
@@ -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)
}
}