Skip to content

Commit

Permalink
first pass idea
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanthecoder committed Jun 26, 2024
1 parent f0a0fb8 commit 052468e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
5 changes: 5 additions & 0 deletions include/sensors/core/sensor_hardware_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <functional>
#include <optional>

#include "can/core/ids.hpp"
#include "common/firmware/gpio.hpp"

namespace sensors {
Expand All @@ -28,6 +29,10 @@ class SensorHardwareBase {
virtual auto set_sync() -> void = 0;
virtual auto reset_sync() -> void = 0;
virtual auto check_tip_presence() -> bool = 0;
virtual auto set_sync(can::ids::SensorId sensor) -> void = 0;
virtual auto reset_sync(can::ids::SensorId sensor) -> void = 0;
virtual auto set_sync_required(can::ids::SensorId sensor, bool required) -> void = 0;
virtual auto reset_sync_required(can::ids::SensorId sensor, bool required) -> void = 0;
};

struct SensorHardwareContainer {
Expand Down
11 changes: 6 additions & 5 deletions include/sensors/core/tasks/capacitive_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ class FDC1004 {

void set_bind_sync(bool should_bind) {
bind_sync = should_bind;
hardware.reset_sync();
hardware.set_sync_required(sensor_id);
hardware.reset_sync(sensor_id);
}

void set_max_bind_sync(bool should_bind) {
max_capacitance_sync = should_bind;
hardware.reset_sync();
hardware.reset_sync(sensor_id);
}

auto set_bind_flags(uint8_t binding) -> void { sensor_binding = binding; }
Expand Down Expand Up @@ -280,14 +281,14 @@ class FDC1004 {
if (capacitance > fdc1004::MAX_CAPACITANCE_READING) {
hardware.set_sync();
} else {
hardware.reset_sync();
hardware.reset_sync(sensor_id);
}
}
if (bind_sync) {
if (capacitance > zero_threshold_pf) {
hardware.set_sync();
hardware.set_sync(sensor_id);
} else {
hardware.reset_sync();
hardware.reset_sync(sensor_id);
}
}

Expand Down
16 changes: 9 additions & 7 deletions include/sensors/core/tasks/pressure_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ class MMR920 {

void set_bind_sync(bool should_bind) {
bind_sync = should_bind;
hardware.reset_sync();
hardware.set_sync_required(sensor_id, should_bind);
hardware.reset_sync(sensor_id);
}

void set_max_bind_sync(bool should_bind) {
max_pressure_sync = should_bind;
hardware.reset_sync();
hardware.reset_sync(sensor_id);
}

auto get_threshold() -> int32_t { return threshold_pascals; }
Expand Down Expand Up @@ -368,16 +369,16 @@ class MMR920 {
(std::fabs(pressure - current_pressure_baseline_pa -
current_moving_pressure_baseline_pa) >
threshold_pascals)) {
hardware.set_sync();
hardware.set_sync(sensor_id);
} else {
hardware.reset_sync();
hardware.reset_sync(sensor_id);
}
} else {
if (std::fabs(pressure - current_pressure_baseline_pa) >
threshold_pascals) {
hardware.set_sync();
hardware.set_sync(sensor_id);
} else {
hardware.reset_sync();
hardware.reset_sync(sensor_id);
}
}
}
Expand Down Expand Up @@ -418,6 +419,7 @@ class MMR920 {
max_pressure_consecutive_readings = 0;
}
if (over_threshold) {
// Use the set_sync that always sets the sync line here
hardware.set_sync();
can_client.send_can_message(
can::ids::NodeId::host,
Expand All @@ -431,7 +433,7 @@ class MMR920 {
// 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();
hardware.reset_sync(sensor_id);
}
}
if (bind_sync) {
Expand Down
47 changes: 46 additions & 1 deletion include/sensors/firmware/sensor_hardware.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@
namespace sensors {
namespace hardware {

enum class SensorIdBitMask : uint8_t {
S0 = 0x01,
S1 = 0x02,
UNUSED = 0x00,
BOTH = 0x03,
};

static auto get_mask_from_id(can::ids::SensorId sensor) -> SensorIdBitMask{
switch(sensor) {
case can::ids::SensorId::BOTH:
return SensorIdBitMask::BOTH;
case can::ids::SensorId::S0 :
return SensorIdBitMask::S0;
case can::ids::SensorId::S1 :
return SensorIdBitMask::S1;
case can::ids::SensorId::UNUSED :
default :
return SensorIdBitMask::UNUSED;
}
}

class SensorHardware : public SensorHardwareBase {
public:
SensorHardware(sensors::hardware::SensorHardwareConfiguration hardware)
Expand All @@ -19,8 +40,32 @@ class SensorHardware : public SensorHardwareBase {
}
return false;
}
auto set_sync(can::ids::SensorId sensor) -> void {
sync_state_mask |= get_mask_from_id(sensor);
if (sync_state_mask & set_sync_required_mask == set_sync_required_mask) {
set_sync();
}
}

auto reset_sync(can::ids::SensorId sensor) -> void {
sync_state_mask &= 0xFF ^ get_mask_from_id(sensor);
if (sync_state_mask & set_sync_required_mask != set_sync_required_mask) {
reset_sync();
}
}

auto set_sync_required(can::ids::SensorId sensor, bool required) -> void {
uint8_t applied_mask = get_mask_from_id(sensor);
if (!required) {
applied_mask ^= 0xFF;
}
set_sync_required_mask &= applied_mask;
}

uint8_t set_sync_required_mask = 0x00;
uint8_t sync_state_mask = 0x00;

sensors::hardware::SensorHardwareConfiguration hardware;
};

}; // namespace hardware
}; // namespace sensors

0 comments on commit 052468e

Please sign in to comment.