Skip to content

Commit

Permalink
add enum for sensor version
Browse files Browse the repository at this point in the history
  • Loading branch information
ahiuchingau committed Jan 31, 2024
1 parent 7f58566 commit 80adf67
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 35 deletions.
7 changes: 5 additions & 2 deletions include/pipettes/core/sensor_tasks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "i2c/core/poller.hpp"
#include "i2c/core/writer.hpp"
#include "motor-control/core/tasks/usage_storage_task.hpp"
#include "sensors/core/mmr920.hpp"
#include "sensors/core/sensor_hardware_interface.hpp"
#include "sensors/core/tasks/capacitive_sensor_task.hpp"
#include "sensors/core/tasks/environmental_sensor_task.hpp"
Expand Down Expand Up @@ -39,7 +40,8 @@ void start_tasks(CanWriterTask& can_writer, I2CClient& i2c3_task_client,
I2CPollerClient& i2c1_poller_client,
sensors::hardware::SensorHardwareBase& sensor_hardware_primary,
can::ids::NodeId id,
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hardware);
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hardware,
sensors::mmr920::SensorVersion sensor_version);

void start_tasks(
CanWriterTask& can_writer, I2CClient& i2c3_task_client,
Expand All @@ -48,7 +50,8 @@ void start_tasks(
sensors::hardware::SensorHardwareBase& sensor_hardware_primary,
sensors::hardware::SensorHardwareBase& sensor_hardware_secondary,
can::ids::NodeId id,
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hardware);
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hardware,
sensors::mmr920::SensorVersion sensor_version);

/**
* Access to all sensor/eeprom tasks. This will be a singleton.
Expand Down
40 changes: 26 additions & 14 deletions include/sensors/core/mmr920.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ constexpr uint16_t ADDRESS = 0x67 << 1;

// Pressure cannot be measured beyond +/-8226.4F on the old sensors
// New sensors have double the threshold but half the fidelity
#if PCBA_PRIMARY_REVISION == 'e'
constexpr float MAX_PRESSURE_READING = 16452.8F;
#else
constexpr float MAX_PRESSURE_READING = 8226.4F;
#endif

enum class SensorVersion : int {
mmr920c04 = 0,
mmr920c10 = 1,
};

[[nodiscard]] inline static auto get_max_pressure_reading(SensorVersion version)
-> float {
if (version == SensorVersion::mmr920c10) {
return 16452.8F;
} else {
return 8226.4F;
}
}

enum class SensorStatus : uint8_t {
SHUTDOWN = 0x0,
IDLE = 0xE5,
Expand Down Expand Up @@ -267,25 +277,27 @@ struct __attribute__((packed, __may_alias__)) StatusCommand {
struct PressureResult {
// Pascals per 1 cmH20
static constexpr float CMH20_TO_PASCALS = 98.0665;
// conversion factor of a given 3 byte measurement to Pascals
static constexpr float PA_PER_COUNT =
#if PCBA_PRIMARY_REVISION == 'e'
2 * 1e-5 * CMH20_TO_PASCALS; // 1.0e-5cmH2O/count * 98.0665Pa/cmH2O
#else
1e-5 * CMH20_TO_PASCALS; // 1.0e-5cmH2O/count * 98.0665Pa/cmH2O
#endif
uint32_t reading : 32 = 0;

[[nodiscard]] static auto to_pressure(uint32_t reg) -> float {
[[nodiscard]] static auto get_pa_per_count(SensorVersion version) -> float {
// conversion factor of a given 3 byte measurement to Pascals
if (version == SensorVersion::mmr920c10) {
return 2 * 1e-5 * CMH20_TO_PASCALS; // 1.0e-5cmH2O/count * 98.0665Pa/cmH2O
}
return 1e-5 * CMH20_TO_PASCALS; // 1.0e-5cmH2O/count * 98.0665Pa/cmH2O
}

[[nodiscard]] static auto to_pressure(uint32_t reg, SensorVersion version) -> float {
// Pressure is converted to pascals
// Sign extend pressure result
if ((reg & 0x00800000) != 0) {
reg |= 0xFF000000;
} else {
reg &= 0x007FFFFF;
}

float pressure =
static_cast<float>(static_cast<int32_t>(reg)) * PA_PER_COUNT;
static_cast<float>(static_cast<int32_t>(reg)) * get_pa_per_count(version);
return pressure;
}
};
Expand Down
13 changes: 8 additions & 5 deletions include/sensors/core/tasks/pressure_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ class MMR920 {
MMR920(I2CQueueWriter &writer, I2CQueuePoller &poller,
CanClient &can_client, OwnQueue &own_queue,
sensors::hardware::SensorHardwareBase &hardware,
const can::ids::SensorId &id)
const can::ids::SensorId &id,
const sensors::mmr920::SensorVersion version)
: writer(writer),
poller(poller),
can_client(can_client),
own_queue(own_queue),
hardware(hardware),
sensor_id(id) {}
sensor_id(id),
sensor_version(version){}

/**
* @brief Check if the MMR92 has been initialized.
Expand Down Expand Up @@ -287,12 +289,12 @@ class MMR920 {

save_pressure(shifted_data_store);
auto pressure = mmr920::PressureResult::to_pressure(
_registers.pressure_result.reading);
_registers.pressure_result.reading, sensor_version);

if (max_pressure_sync) {
bool this_tick_over_threshold =
std::fabs(pressure - current_pressure_baseline_pa) >=
mmr920::MAX_PRESSURE_READING;
mmr920::get_max_pressure_reading(sensor_version);
bool over_threshold = false;
if (this_tick_over_threshold) {
max_pressure_consecutive_readings =
Expand Down Expand Up @@ -374,7 +376,7 @@ class MMR920 {

uint32_t shifted_data_store = temporary_data_store >> 8;

auto pressure = mmr920::PressureResult::to_pressure(shifted_data_store);
auto pressure = mmr920::PressureResult::to_pressure(shifted_data_store, sensor_version);
pressure_running_total += pressure;

if (!m.id.is_completed_poll) {
Expand Down Expand Up @@ -451,6 +453,7 @@ class MMR920 {
OwnQueue &own_queue;
hardware::SensorHardwareBase &hardware;
const can::ids::SensorId &sensor_id;
const sensors::mmr920::SensorVersion sensor_version;

mmr920::MMR920RegisterMap _registers{};
mmr920::FilterSetting filter_setting =
Expand Down
11 changes: 7 additions & 4 deletions include/sensors/core/tasks/pressure_sensor_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "i2c/core/messages.hpp"
#include "i2c/core/poller.hpp"
#include "i2c/core/writer.hpp"
#include "sensors/core/mmr920.hpp"
#include "sensors/core/tasks/pressure_driver.hpp"
#include "sensors/core/utils.hpp"

Expand All @@ -21,8 +22,9 @@ class PressureMessageHandler {
I2CQueueWriter &i2c_writer, I2CQueuePoller &i2c_poller,
CanClient &can_client, OwnQueue &own_queue,
sensors::hardware::SensorHardwareBase &hardware,
const can::ids::SensorId &id)
: driver{i2c_writer, i2c_poller, can_client, own_queue, hardware, id} {}
const can::ids::SensorId &id,
const sensors::mmr920::SensorVersion& version)
: driver{i2c_writer, i2c_poller, can_client, own_queue, hardware, id, version} {}
PressureMessageHandler(const PressureMessageHandler &) = delete;
PressureMessageHandler(const PressureMessageHandler &&) = delete;
auto operator=(const PressureMessageHandler &)
Expand Down Expand Up @@ -192,9 +194,10 @@ class PressureSensorTask {
[[noreturn]] void operator()(
i2c::writer::Writer<QueueImpl> *writer,
i2c::poller::Poller<QueueImpl> *poller, CanClient *can_client,
sensors::hardware::SensorHardwareBase *hardware) {
sensors::hardware::SensorHardwareBase *hardware,
sensors::mmr920::SensorVersion *sensor_version) {
auto handler = PressureMessageHandler{
*writer, *poller, *can_client, get_queue(), *hardware, sensor_id};
*writer, *poller, *can_client, get_queue(), *hardware, sensor_id, *sensor_version};
handler.initialize();
utils::TaskMessage message{};
for (;;) {
Expand Down
14 changes: 9 additions & 5 deletions pipettes/core/sensor_tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ void sensor_tasks::start_tasks(
sensor_tasks::I2CPollerClient& i2c2_poller_client,
sensors::hardware::SensorHardwareBase& sensor_hardware_primary,
can::ids::NodeId id,
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hardware) {
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hardware,
sensors::mmr920::SensorVersion sensor_version) {
// Low throughput sensor task (single channel)
queue_client.set_node_id(id);
auto& queues = sensor_tasks::get_queues();
Expand All @@ -69,7 +70,7 @@ void sensor_tasks::start_tasks(
5, "enviro sensor", i2c3_task_client, i2c3_poller_client, queues);
auto& pressure_sensor_task_rear = pressure_sensor_task_builder_rear.start(
5, "pressure sensor s0", pressure_i2c_client, pressure_i2c_poller,
queues, sensor_hardware_primary);
queues, sensor_hardware_primary, sensor_version);
auto& capacitive_sensor_task_rear =
capacitive_sensor_task_builder_rear.start(
5, "capacitive sensor s0", i2c3_task_client, i2c3_poller_client,
Expand Down Expand Up @@ -102,7 +103,8 @@ void sensor_tasks::start_tasks(
sensors::hardware::SensorHardwareBase& sensor_hardware_primary,
sensors::hardware::SensorHardwareBase& sensor_hardware_secondary,
can::ids::NodeId id,
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hardware) {
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hardware,
sensors::mmr920::SensorVersion sensor_version) {
// High throughput sensor task (eight and ninety six channel)
queue_client.set_node_id(id);
auto& queues = sensor_tasks::get_queues();
Expand Down Expand Up @@ -133,10 +135,12 @@ void sensor_tasks::start_tasks(
5, "enviro sensor", i2c3_task_client, i2c3_poller_client, queues);
auto& pressure_sensor_task_rear = pressure_sensor_task_builder_rear.start(
5, "pressure sensor s0", primary_pressure_i2c_client,
primary_pressure_i2c_poller, queues, sensor_hardware_primary);
primary_pressure_i2c_poller, queues, sensor_hardware_primary,
sensor_version);
auto& pressure_sensor_task_front = pressure_sensor_task_builder_front.start(
5, "pressure sensor s1", secondary_pressure_i2c_client,
secondary_pressure_i2c_poller, queues, sensor_hardware_secondary);
secondary_pressure_i2c_poller, queues, sensor_hardware_secondary,
sensor_version);
auto& capacitive_sensor_task_rear =
capacitive_sensor_task_builder_rear.start(
5, "capacitive sensor s0", i2c3_task_client, i2c3_poller_client,
Expand Down
17 changes: 14 additions & 3 deletions pipettes/firmware/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "pipettes/core/sensor_tasks.hpp"
#include "pipettes/firmware/interfaces_g4.hpp"
#include "pipettes/firmware/utility_configurations.hpp"
#include "sensors/core/mmr920.hpp"
#include "sensors/firmware/sensor_hardware.hpp"
#include "spi/firmware/spi_comms.hpp"

Expand Down Expand Up @@ -147,6 +148,14 @@ extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
}
}

#if PCBA_PRIMARY_REVISION == 'e'
static constexpr auto pressure_sensor_version =
sensors::mmr920::SensorVersion::mmr920c10;
#else
static constexpr auto pressure_sensor_version =
sensors::mmr920::SensorVersion::mmr920c04;
#endif

// Unfortunately, these numbers need to be literals or defines
// to get the compile-time checks to work so we can't actually
// correctly rely on the hal to get these numbers - they need
Expand Down Expand Up @@ -183,7 +192,7 @@ auto initialize_motor_tasks(
peripheral_tasks::get_i2c1_poller_client(),
sensor_hardware_container.primary,
sensor_hardware_container.secondary.value(), id,
eeprom_hardware_iface);
eeprom_hardware_iface, pressure_sensor_version);

initialize_linear_timer(plunger_callback);
initialize_gear_timer(gear_callback_wrapper);
Expand All @@ -210,15 +219,17 @@ auto initialize_motor_tasks(
peripheral_tasks::get_i2c1_poller_client(),
sensor_hardware_container.primary,
sensor_hardware_container.secondary.value(),
id, eeprom_hardware_iface);
id, eeprom_hardware_iface,
pressure_sensor_version);
} else {
sensor_tasks::start_tasks(*central_tasks::get_tasks().can_writer,
peripheral_tasks::get_i2c3_client(),
peripheral_tasks::get_i2c3_poller_client(),
peripheral_tasks::get_i2c1_client(),
peripheral_tasks::get_i2c1_poller_client(),
sensor_hardware_container.primary, id,
eeprom_hardware_iface);
eeprom_hardware_iface,
pressure_sensor_version);
}

initialize_linear_timer(plunger_callback);
Expand Down
3 changes: 2 additions & 1 deletion sensors/tests/test_pressure_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "i2c/tests/mock_response_queue.hpp"
#include "motor-control/core/utils.hpp"
#include "sensors/core/tasks/pressure_driver.hpp"
#include "sensors/core/mmr920.hpp"
#include "sensors/core/tasks/pressure_sensor_task.hpp"
#include "sensors/core/utils.hpp"
#include "sensors/tests/mock_hardware.hpp"
Expand Down Expand Up @@ -58,7 +59,7 @@ SCENARIO("Testing the pressure sensor driver") {
writer.set_queue(&i2c_queue);
poller.set_queue(&i2c_poll_queue);
sensors::tasks::MMR920 driver(writer, poller, queue_client, pressure_queue,
hardware, sensor_id);
hardware, sensor_id, sensors::mmr920::SensorVersion::mmr920c04);

can::message_writer_task::TaskMessage empty_can_msg{};

Expand Down
3 changes: 2 additions & 1 deletion sensors/tests/test_pressure_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ SCENARIO("Receiving messages through the pressure sensor message handler") {
poller.set_queue(&i2c_poll_queue);

auto sensor = sensors::tasks::PressureMessageHandler{
writer, poller, queue_client, response_queue, mock_hw, sensor_id};
writer, poller, queue_client, response_queue, mock_hw, sensor_id,
sensors::mmr920::SensorVersion::mmr920c04};

GIVEN("A TransactionResponse message") {
can_queue.reset();
Expand Down

0 comments on commit 80adf67

Please sign in to comment.