Skip to content

Commit 2ef8a57

Browse files
fix pressure conversions
1 parent affcb57 commit 2ef8a57

File tree

6 files changed

+68
-50
lines changed

6 files changed

+68
-50
lines changed

stm32-modules/include/flex-stacker/firmware/i2c_hardware.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" {
1010
#endif // __cplusplus
1111

1212
typedef enum I2C_BUS {
13+
I2C_BUS_1,
1314
I2C_BUS_2,
1415
I2C_BUS_3,
1516
NO_BUS,
@@ -18,6 +19,7 @@ typedef enum I2C_BUS {
1819
typedef void *HAL_I2C_HANDLE;
1920

2021
typedef struct HandlerStruct {
22+
HAL_I2C_HANDLE i2c1;
2123
HAL_I2C_HANDLE i2c2;
2224
HAL_I2C_HANDLE i2c3;
2325
} I2CHandlerStruct;

stm32-modules/include/vacuum-module/vacuum-module/MPRLL0025PA00001A.hpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,30 @@
77
#include "firmware/vacuum_pressure_sensor_policy.hpp"
88

99
namespace vacuum_pressure_sensor {
10+
using hardware::VacuumPressureSensorPolicy;
11+
using i2c::hardware::RxTxReturn;
1012

1113
template <typename P>
1214
concept MPRPolicy = requires(P p, uint16_t dev_addr, uint16_t reg,
1315
uint16_t size, uint8_t* data) {
14-
{
15-
p.i2c_read(dev_addr, reg, data, size)
16-
} -> std::same_as<i2c::hardware::RxTxReturn>;
17-
{
18-
p.i2c_write(dev_addr, reg, data, size)
19-
} -> std::same_as<i2c::hardware::RxTxReturn>;
16+
{ p.i2c_read(dev_addr, reg, data, size) } -> std::same_as<RxTxReturn>;
17+
{ p.i2c_write(dev_addr, reg, data, size) } -> std::same_as<RxTxReturn>;
2018
};
2119

2220
constexpr uint8_t DEV_ADDRESS = 0x18 << 1;
2321
constexpr uint8_t MEASURE_PRESSURE_COMMAND = 0xAA;
2422
constexpr uint8_t STATUS_BUSY_FLAG = 0x20;
23+
// max sensor output in counts
2524
constexpr uint32_t OUTPUT_MAX = 15099494;
25+
// min sensor output in counts
2626
constexpr uint32_t OUTPUT_MIN = 1677722;
2727
constexpr uint32_t OUTPUT_RANGE_COUNTS = OUTPUT_MAX - OUTPUT_MIN;
2828
// range for this particular model is 0-25 PSI
2929
constexpr uint16_t PRESSURE_RANGE_PSI = 25;
30-
30+
// 1 PSI = 68.9476 mbar
31+
constexpr uint32_t MBAR_CONVERSION_FACTOR = 68.9476;
3132
constexpr uint8_t PRESSURE_FRAME_LEN = 10;
32-
std::array<uint8_t, PRESSURE_FRAME_LEN> READ_BUFF = {0};
33-
std::array<uint8_t, PRESSURE_FRAME_LEN> WRITE = {MEASURE_PRESSURE_COMMAND};
34-
constexpr int retries = 3;
33+
constexpr int DEFAULT_RETRIES = 3;
3534

3635
struct StatusByte {
3736
uint8_t power_indication;
@@ -42,20 +41,21 @@ struct StatusByte {
4241

4342
class MPRLL0025PA00001 {
4443
public:
45-
auto initialize(hardware::VacuumPressureSensorPolicy* policy) -> void {
44+
auto initialize(VacuumPressureSensorPolicy* policy) -> void {
4645
if (_policy == nullptr) {
4746
_policy = policy;
4847
}
4948
}
5049

51-
auto read_pressure() -> uint16_t {
52-
bool sensor_busy = true;
53-
50+
// TODO: separate sending the write pressure command,
51+
// and read the pressure from a callback for an eoc pin irq
52+
auto read_pressure() -> std::optional<double> {
5453
_policy->i2c_master_write(DEVICE_ADDRESS, MEASURE_PRESSURE_COMMAND,
5554
WRITE_BUFF, 1);
5655

57-
for (int i = 0; i < (retries + 1); i++) {
56+
for (int i = 0; i < (DEFAULT_RETRIES + 1); i++) {
5857
_policy->i2c_master_read(DEV_ADDRESS, READ_BUFF, 4);
58+
_policy->sleep_ms(3);
5959
auto status_byte = READ_BUFF[0];
6060
sensor_busy = static_cast<bool>(status_byte & STATUS_BUSY_FLAG);
6161

@@ -64,18 +64,29 @@ class MPRLL0025PA00001 {
6464
}
6565
}
6666

67-
auto pressure_psi = convert_pressure(READ_BUFF);
67+
pressure_psi = convert_pressure(READ_BUFF);
68+
pressure_mbar = pressure_psi * MBAR_CONVERSION_FACTOR;
6869
return pressure_psi;
6970
}
7071

71-
private:
72-
hardware::VacuumPressureSensorPolicy* _policy{nullptr};
72+
std::array<uint8_t, PRESSURE_FRAME_LEN> READ_BUFF = {0};
73+
std::array<uint8_t, PRESSURE_FRAME_LEN> WRITE = {MEASURE_PRESSURE_COMMAND};
74+
double pressure_psi;
75+
double pressure_mbar;
76+
bool sensor_busy = true;
7377

74-
auto convert_pressure(uint8_t* sensor_output) -> uint16_t {
75-
auto pressure_read_counts =
76-
sensor_output[1] << 16 | sensor_output[2] << 8 | sensor_output[3];
77-
auto pressure_psi =
78-
(pressure_read_counts * PRESSURE_RANGE_PSI) / OUTPUT_RANGE_COUNTS;
78+
private:
79+
VacuumPressureSensorPolicy* _policy{nullptr};
80+
81+
auto convert_pressure(uint8_t* sensor_output) -> double {
82+
// auto pressure_read_counts =
83+
// sensor_output[1] << 16 | sensor_output[2] << 8 |
84+
// sensor_output[3];
85+
uint32_t pressure_read_counts = sensor_output[1] << 24 |
86+
sensor_output[2] << 16 |
87+
sensor_output[3] << 8;
88+
double pressure_psi = static_cast<double>(
89+
(pressure_read_counts * PRESSURE_RANGE_PSI) / OUTPUT_RANGE_COUNTS);
7990
return pressure_psi;
8091
}
8192
};

stm32-modules/include/vacuum-module/vacuum-module/lps22df.hpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "firmware/atmosphere_pressure_sensor_policy.hpp"
88

99
namespace lps22df {
10+
using atmosphere_pressure_sensor::hardware::AtmospherePressureSensorPolicy;
11+
using i2c::hardware::RxTxReturn;
1012

1113
template <typename P>
1214
concept LPS22DFPolicy = requires(P p, uint16_t dev_addr, uint16_t reg,
@@ -21,7 +23,7 @@ constexpr uint8_t DEVICE_ADDRESS = 0x5C << 1;
2123
constexpr uint8_t CTRL_REG2 = 0x11;
2224
// pressure reading is 4 bytes, starting with status at 0x27
2325
constexpr uint8_t PRESSURE_OUTPUT_REGISTER = 0x27;
24-
// write bit 1 to CTRL REG 2 to read pressure once
26+
// write bit 0 to CTRL REG 2 to read pressure once
2527
constexpr uint8_t ONE_SHOT_PRESSURE_READ[1] = {0x01};
2628
// pressure in hectoPascals is the 3 byte output value divided by the
2729
// sensitivity
@@ -30,28 +32,25 @@ constexpr uint16_t SENSOR_SENSITIVITY = 4096;
3032
constexpr uint8_t PRESSURE_READY_FLAG = 0x01;
3133

3234
constexpr uint8_t PRESSURE_FRAME_LEN = 10;
33-
std::array<uint8_t, PRESSURE_FRAME_LEN> READ_BUFF = {0};
34-
std::array<uint8_t, PRESSURE_FRAME_LEN> WRITE_BUFF = {0};
35-
constexpr int retries = 3;
35+
constexpr int DEFAULT_RETRIES = 3;
3636

3737
class LPS222DF {
3838
public:
39-
auto initialize(
40-
atmosphere_pressure_sensor::hardware::AtmospherePressureSensorPolicy*
41-
policy) -> void {
39+
auto initialize(AtmospherePressureSensorPolicy* policy) -> void {
4240
if (_policy == nullptr) {
4341
_policy = policy;
4442
}
4543
}
4644

47-
auto read_pressure() -> uint16_t {
48-
bool pressure_reading_ready = false;
49-
45+
// TODO: separate sending the write pressure command from the read so
46+
// the task doesn't need to wait for the conversion
47+
auto read_pressure() -> std::optional<double> {
5048
_policy->i2c_write(DEVICE_ADDRESS, CTRL_REG2, ONE_SHOT_PRESSURE_READ,
5149
1);
52-
for (int i = 0; i < (retries + 1); i++) {
50+
for (int i = 0; i < (DEFAULT_RETRIES + 1); i++) {
5351
_policy->i2c_read(DEVICE_ADDRESS, PRESSURE_OUTPUT_REGISTER,
5452
READ_BUFF, 4);
53+
_policy->sleep_ms(3);
5554
auto status_byte = READ_BUFF[0];
5655
pressure_reading_ready =
5756
static_cast<bool>(status_byte & PRESSURE_READY_FLAG);
@@ -60,25 +59,30 @@ class LPS222DF {
6059
break;
6160
}
6261
}
63-
if (!pressure_reading_ready) {
64-
// raise an error here
65-
}
6662

6763
auto pressure_hPa = convert_pressure(READ_BUFF);
68-
return pressure_hPa
64+
pressure_mbar = pressure_hPa;
65+
return pressure_hPa;
6966
}
7067

68+
std::array<uint8_t, PRESSURE_FRAME_LEN> READ_BUFF = {0};
69+
std::array<uint8_t, PRESSURE_FRAME_LEN> WRITE_BUFF = {0};
70+
// 1 hectoPascal = mbar
71+
double pressure_mbar;
72+
bool pressure_reading_ready = false;
73+
7174
private:
72-
atmosphere_pressure_sensor::hardware::AtmospherePressureSensorPolicy*
73-
_policy{nullptr};
75+
AtmospherePressureSensorPolicy* _policy{nullptr};
7476

75-
auto convert_pressure(uint8_t* sensor_output) -> uint16_t {
77+
auto convert_pressure(uint8_t* sensor_output) -> double {
7678
auto pressure_read_bytes = {sensor_output[1], sensor_output[2],
7779
sensor_output[3]};
7880
// test that this is accurate
79-
auto pressure_read_counts =
80-
sensor_output[1] << 16 | sensor_output[2] << 8 | sensor_output[3];
81-
auto pressure_hPa = pressure_read_counts / SENSOR_SENSITIVITY;
81+
auto pressure_read_counts = sensor_output[1] << 24 |
82+
sensor_output[2] << 16 |
83+
sensor_output[3] << 8;
84+
double pressure_hPa =
85+
static_cast<double>(pressure_read_counts / SENSOR_SENSITIVITY);
8286
return pressure_hPa
8387
}
8488
};

stm32-modules/vacuum-module/firmware/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ static auto system_task =
4646
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
4747
static auto aggregator = tasks::FirmwareTasks::QueueAggregator();
4848

49+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
50+
static auto i2c1_comms = i2c::hardware::I2C();
4951
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
5052
static auto i2c2_comms = i2c::hardware::I2C();
5153
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
@@ -61,6 +63,7 @@ auto main() -> int {
6163
i2c_hardware_init(&i2c_handles);
6264
vacuum_pressure_sensor_hardware_init();
6365

66+
i2c1_comms.set_handle(i2c_handles.i2c1, I2C_BUS_1);
6467
i2c2_comms.set_handle(i2c_handles.i2c2, I2C_BUS_2);
6568
i2c3_comms.set_handle(i2c_handles.i2c3, I2C_BUS_3);
6669

stm32-modules/vacuum-module/firmware/system/i2c_comms.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ auto I2C::i2c_read(uint16_t dev_addr, uint16_t reg, uint8_t *data,
2727

2828
auto I2C::i2c_master_write(uint16_t dev_addr, uint8_t *data, uint16_t size)
2929
-> RxTxReturn {
30-
auto ret = hal_i2c_master_write(this->bus, dev_addr, data, size);
30+
auto ret = hal_i2c_master_write(bus, dev_addr, data, size);
3131
return RxTxReturn(ret);
3232
}
3333

3434
auto I2C::i2c_master_read(uint16_t dev_addr, uint8_t *data, uint16_t size)
3535
-> RxTxReturn {
36-
auto ret = hal_i2c_master_read(this->bus, dev_addr, data, size);
36+
auto ret = hal_i2c_master_read(bus, dev_addr, data, size);
3737
return RxTxReturn(ret);
3838
}

stm32-modules/vacuum-module/firmware/vacuum-pressure-sensor/vacuum_pressure_sensor_policy.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ auto VacuumPressureSensorPolicy::i2c_read(uint16_t dev_addr, uint16_t reg,
2626
auto VacuumPressureSensorPolicy::i2c_master_write(uint16_t dev_addr,
2727
uint8_t* data, uint16_t size)
2828
-> RxTxReturn {
29-
auto ret = i2c_comms->i2c_master_write(dev_addr, data, size);
30-
return ret;
29+
return i2c_comms->i2c_master_write(dev_addr, data, size);
3130
}
3231

3332
auto VacuumPressureSensorPolicy::i2c_master_read(uint16_t dev_addr,
3433
uint8_t* data, uint16_t size)
3534
-> RxTxReturn {
36-
auto ret = i2c_comms->i2c_master_write(dev_addr, data, size);
37-
return ret;
35+
return i2c_comms->i2c_master_write(dev_addr, data, size);
3836
}
3937

4038
auto VacuumPressureSensorPolicy::conversion_ended(

0 commit comments

Comments
 (0)