-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sensors): Self baselining during sensor moves #786
Changes from 14 commits
70e8734
d6c1372
c00db25
4e17786
d44cfa7
ac0c76e
095fc71
fb63bf4
7b0e300
920b861
544d9c0
6c30b6e
372383b
4b7b24d
994d6ef
92bc679
0c0e09b
5ba41c1
bbb5df4
3109317
7a26107
787b983
fdd6637
5a60de6
ecd943b
a9cce7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
#pragma once | ||
// Not my favorite way to check this, but if we don't have access | ||
// to vTaskDelay during host compilation so just dummy the function | ||
|
||
#ifdef ENABLE_CROSS_ONLY_HEADERS | ||
#include "FreeRTOS.h" | ||
#endif | ||
|
||
template <typename T> | ||
requires std::is_integral_v<T> | ||
static void vtask_hardware_delay(T ticks) { | ||
#ifndef INC_TASK_H | ||
std::ignore = ticks; | ||
#else | ||
#ifdef ENABLE_CROSS_ONLY_HEADERS | ||
vTaskDelay(ticks); | ||
#else | ||
std::ignore = ticks; | ||
#endif | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
#pragma once | ||
|
||
#include <cmath> | ||
#include <numeric> | ||
|
||
#include "can/core/can_writer_task.hpp" | ||
#include "can/core/ids.hpp" | ||
|
@@ -70,6 +71,8 @@ class MMR920 { | |
echoing = should_echo; | ||
if (should_echo) { | ||
sensor_buffer_index = 0; // reset buffer index | ||
crossed_buffer_index=false; | ||
sensor_buffer->fill(0.0); | ||
} | ||
} | ||
|
||
|
@@ -230,12 +233,12 @@ class MMR920 { | |
} | ||
|
||
auto sensor_buffer_log(float data) -> void { | ||
sensor_buffer->at(sensor_buffer_index) = data; | ||
sensor_buffer_index++; | ||
if (sensor_buffer_index == SENSOR_BUFFER_SIZE) { | ||
sensor_buffer_index = 0; | ||
crossed_buffer_index=true; | ||
} | ||
|
||
sensor_buffer->at(sensor_buffer_index) = data; | ||
sensor_buffer_index++; | ||
} | ||
|
||
auto save_temperature(int32_t data) -> bool { | ||
|
@@ -290,10 +293,21 @@ class MMR920 { | |
} | ||
|
||
void send_accumulated_sensor_data(uint32_t message_index) { | ||
for (int i = 0; i < static_cast<int>(SENSOR_BUFFER_SIZE); i++) { | ||
auto start = 0; | ||
auto count = sensor_buffer_index; | ||
if (crossed_buffer_index == true) { | ||
start = sensor_buffer_index; | ||
count = SENSOR_BUFFER_SIZE; | ||
} | ||
|
||
can_client.send_can_message( | ||
can::ids::NodeId::host, | ||
can::messages::Acknowledgment{ | ||
.message_index = count}); | ||
for (int i = 0; i < count; i++) { | ||
// send over buffer and then clear buffer values | ||
// NOLINTNEXTLINE(div-by-zero) | ||
int current_index = (i + sensor_buffer_index) % | ||
int current_index = (i + start) % | ||
static_cast<int>(SENSOR_BUFFER_SIZE); | ||
|
||
can_client.send_can_message( | ||
|
@@ -306,10 +320,13 @@ class MMR920 { | |
(*sensor_buffer).at(current_index))}); | ||
if (i % 10 == 0) { | ||
// slow it down so the can buffer doesn't choke | ||
vtask_hardware_delay(50); | ||
vtask_hardware_delay(20); | ||
} | ||
(*sensor_buffer).at(current_index) = 0; | ||
} | ||
can_client.send_can_message( | ||
can::ids::NodeId::host, | ||
can::messages::Acknowledgment{ | ||
.message_index = message_index}); | ||
} | ||
|
||
auto handle_ongoing_pressure_response(i2c::messages::TransactionResponse &m) | ||
|
@@ -355,31 +372,42 @@ class MMR920 { | |
.message_index = m.message_index, | ||
.severity = can::ids::ErrorSeverity::unrecoverable, | ||
.error_code = can::ids::ErrorCode::over_pressure}); | ||
} else { | ||
} else if (!bind_sync) { | ||
// if we're not using bind sync turn off the sync line | ||
// we don't do this during bind sync because if it's triggering | ||
// the sync line on purpose this causes bouncing on the line | ||
// that turns off the sync and then immediately turns it back on | ||
// and this can cause disrupt the behavior | ||
hardware.reset_sync(); | ||
} | ||
} | ||
if (bind_sync) { | ||
if (std::fabs(pressure - current_pressure_baseline_pa) > | ||
threshold_pascals) { | ||
hardware.set_sync(); | ||
// force this to stay set long enough to debounce on other nodes | ||
} else { | ||
hardware.reset_sync(); | ||
} | ||
} | ||
|
||
if (echo_this_time) { | ||
auto response_pressure = pressure - current_pressure_baseline_pa; | ||
// do we want pressure or response pressure | ||
sensor_buffer_log(pressure); | ||
can_client.send_can_message( | ||
can::ids::NodeId::host, | ||
can::messages::ReadFromSensorResponse{ | ||
.message_index = m.message_index, | ||
.sensor = can::ids::SensorType::pressure, | ||
.sensor_id = sensor_id, | ||
.sensor_data = | ||
mmr920::reading_to_fixed_point(response_pressure)}); | ||
|
||
sensor_buffer_log(response_pressure); | ||
|
||
if (sensor_buffer_index == 10 && !crossed_buffer_index) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this 10 is setting the baselining window size, right? let's drop that in a define or a constexpr or something |
||
|
||
current_pressure_baseline_pa = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we incrementing instead of setting this? won't this make it run away? also we could put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add a comment that explains why this won't run away There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re-worked it to make it more clear |
||
std::accumulate(sensor_buffer->begin(), sensor_buffer->begin()+10, 0) / | ||
10 + current_pressure_baseline_pa; | ||
for (auto i = sensor_buffer_index - 10; i < sensor_buffer_index; | ||
i++) { | ||
sensor_buffer->at(sensor_buffer_index) = | ||
sensor_buffer->at(sensor_buffer_index) - | ||
current_pressure_baseline_pa; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -565,6 +593,7 @@ class MMR920 { | |
} | ||
std::array<float, SENSOR_BUFFER_SIZE> *sensor_buffer; | ||
uint16_t sensor_buffer_index = 0; | ||
bool crossed_buffer_index = false; | ||
}; | ||
|
||
} // namespace tasks | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this overlap with #785 ?