11#include " rbr_sensor.h"
22#include " FreeRTOS.h"
33#include " OrderedSeparatorLineParser.h"
4- #include " spotter.h"
54#include " configuration.h"
65#include " payload_uart.h"
76#include " rbr_sensor_util.h"
87#include " serial.h"
8+ #include " spotter.h"
99#include " stm32_rtc.h"
1010#include " task_priorities.h"
1111#include " uptime.h"
@@ -18,10 +18,10 @@ static constexpr char PRESSURE[] = "pressure";
1818static constexpr char TEMPERATURE[] = " temperature" ;
1919
2020/* !
21- * @brief Initialize the RBR sensor driver.
22- * @param type The sensor type.
23- * @param min_probe_period_ms How often to check the sensor type in milliseconds.
24- */
21+ * @brief Initialize the RBR sensor driver.
22+ * @param type The sensor type.
23+ * @param min_probe_period_ms How often to check the sensor type in milliseconds.
24+ */
2525void RbrSensor::init (BmRbrDataMsg::SensorType_t type, uint32_t min_probe_period_ms) {
2626 _type = type;
2727 _stored_type = type;
@@ -52,12 +52,30 @@ void RbrSensor::init(BmRbrDataMsg::SensorType_t type, uint32_t min_probe_period_
5252}
5353
5454/* !
55- * @brief Probe the sensor type if enough time has passed.
56- */
55+ * @brief Probe the sensor type if enough time has passed.
56+ */
5757void RbrSensor::maybeProbeType (uint64_t last_power_on_time) {
5858 uint64_t now = uptimeGetMs ();
59+
60+ // Cannot transmit to the RBR while a reading is ongoing or else the
61+ // RBR will "blank" the streaming data, see:
62+ // https://docs.rbr-global.com/l3-command-reference/N/External/timeouts-output-blanking-and-power-saving
63+ // Create a window in which the probe can occur:
64+ // +--------------+---------------+---------------+
65+ // | 0ms | 200-300ms | 500ms |
66+ // +--------------+---------------+---------------+
67+ // | RBR reading | Probe TX | RBR reading |
68+ // +--------------+---------------+---------------+
69+ static constexpr uint32_t window_start_time =
70+ RBR_READING_PERIOD_MS / 2 - PROBE_WINDOW_WIDTH_MS / 2 ;
71+ static constexpr uint32_t window_end_time =
72+ RBR_READING_PERIOD_MS / 2 + PROBE_WINDOW_WIDTH_MS / 2 ;
73+ bool lower_window = now > _last_data_time + window_start_time;
74+ bool upper_window = now < _last_data_time + window_end_time;
75+ bool can_transmit = lower_window && upper_window;
76+
5977 if (now - _lastProbeTime >= _minProbePeriodMs &&
60- now - last_power_on_time >= _minProbePeriodMs) {
78+ now - last_power_on_time >= _minProbePeriodMs && can_transmit ) {
6179 PLUART::write ((uint8_t *)typeCommand, strlen (typeCommand));
6280 _lastProbeTime = uptimeGetMs ();
6381 _awaitingProbeResponse = true ;
@@ -78,9 +96,12 @@ bool RbrSensor::getData(BmRbrDataMsg::Data &d) {
7896 handleOutputformat (_payload_buffer, read_len);
7997 } else if (BmRbrSensorUtil::validSensorDataString (_payload_buffer, read_len)) {
8098 success = handleDataString (_payload_buffer, read_len, d);
99+ if (success) {
100+ _last_data_time = uptimeGetMs ();
101+ }
81102 } else {
82103 spotter_log (0 , RBR_RAW_LOG, USE_TIMESTAMP, " Invalid line from sensor: %.*s\n " , read_len,
83- _payload_buffer);
104+ _payload_buffer);
84105 spotter_log_console (0 , " Invalid line from sensor: %.*s" , read_len, _payload_buffer);
85106 printf (" Invalid line from sensor: %.*s\n " , read_len, _payload_buffer);
86107 }
@@ -119,7 +140,7 @@ void RbrSensor::handleOutputformat(const char *s, size_t read_len) {
119140 BM_CFG_PARTITION_SYSTEM, CFG_RBR_TYPE, strlen (CFG_RBR_TYPE),
120141 static_cast <uint32_t >(BmRbrDataMsg::SensorType::PRESSURE_AND_TEMPERATURE));
121142 spotter_log (0 , RBR_RAW_LOG, USE_TIMESTAMP,
122- " Detected temp & pressure sensor, saving config\n " );
143+ " Detected temp & pressure sensor, saving config\n " );
123144 spotter_log_console (0 , " Detected temp & pressure sensor, saving config" );
124145 printf (" Detected temp & pressure sensor, saving config.\n " );
125146 save_config (BM_CFG_PARTITION_SYSTEM, true ); // reboot
@@ -160,10 +181,10 @@ bool RbrSensor::handleDataString(const char *s, size_t read_len, BmRbrDataMsg::D
160181 rtcPrint (rtcTimeBuffer, NULL );
161182 if (_sensorBmLogEnable) {
162183 spotter_log (0 , RBR_RAW_LOG, USE_TIMESTAMP, " tick: %" PRIu64 " , rtc: %s, line: %.*s\n " ,
163- uptimeGetMs (), rtcTimeBuffer, read_len, s);
184+ uptimeGetMs (), rtcTimeBuffer, read_len, s);
164185 }
165- spotter_log_console (0 , " rbr | tick: %" PRIu64 " , rtc: %s, line: %.*s" , uptimeGetMs (), rtcTimeBuffer,
166- read_len, s);
186+ spotter_log_console (0 , " rbr | tick: %" PRIu64 " , rtc: %s, line: %.*s" , uptimeGetMs (),
187+ rtcTimeBuffer, read_len, s);
167188 printf (" rbr | tick: %" PRIu64 " , rtc: %s, line: %.*s\n " , uptimeGetMs (), rtcTimeBuffer,
168189 (int )read_len, s);
169190
0 commit comments