bristlemouth / bm_protocol

Primary Bristlemouth firmware repository
https://www.bristlemouth.org/
Apache License 2.0
10 stars 7 forks source link

fix: keep diff sig capacity const, reset total samples each pass #157

Closed towynlin closed 3 weeks ago

towynlin commented 3 weeks ago

Since creating the difference signals shrinks the total number of samples (passed by reference) by one each time, we need to reset to the full capacity each time. This fixes a bug where the total samples in each message got progressively smaller and smaller over time during a deployment.

towynlin commented 3 weeks ago

Great questions @evanShap. Thanks.

  • Edge case to ponder that I don't fully grok: we're not guaranteed to receive (rawSampleS * 100) / rbrCodaReadingPeriodMs samples from the pressure sensor. -- If we receive more than this, I expect we just truncate those.

Yes, because of https://github.com/bristlemouth/bm_protocol/blob/04d2a0db7e5c1eb8c5836567f1f469f9dffb07c5/src/lib/common/differenceSignal.cpp#L25 where r_i is the number of readings added so far and r_n is the capacity of the buffer, we only add to the difference signal buffer up to the end and then just ignore subsequent readings.

If we receive less than this, do we only send what we received, or do we end up with random data or 0s to fill out the buffer up to diffSignalCapacity?

We only send what was received, because here https://github.com/bristlemouth/bm_protocol/blob/04d2a0db7e5c1eb8c5836567f1f469f9dffb07c5/src/lib/common/differenceSignal.cpp#L50-L51 num_samples is passed by reference and updated to contain only as many as we've actually received (minus one, because it's the first-order difference signal).

Then when encoding to cbor, we only encode num_samples values from the second-order difference signal here https://github.com/bristlemouth/bm_common_messages/blob/2a44e0ca19ce4eae3174c7ec77a16e95d4c49966/bm_rbr_pressure_difference_signal_msg.cpp#L156-L157