-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for BMP390 sensor and shuttle. Add examples: * Register read/write via I2C/SPi; * Interrupt streaming via I2C/SPI; * Polling streaming via I2C/SPI. Update documentation and add unit tests.
- Loading branch information
1 parent
26143ea
commit 89350a3
Showing
13 changed files
with
992 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import logging | ||
import struct | ||
import sys | ||
import time | ||
from pathlib import Path | ||
|
||
from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle | ||
|
||
|
||
def setup_logging(level: int = logging.DEBUG) -> logging.Logger: | ||
logger = logging.getLogger() | ||
logger.setLevel(level) | ||
stdout_handler = logging.StreamHandler(sys.stdout) | ||
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s" | ||
log_formatter = logging.Formatter(log_format) | ||
stdout_handler.setFormatter(log_formatter) | ||
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w") | ||
file_handler.setFormatter(log_formatter) | ||
logger.addHandler(stdout_handler) | ||
logger.addHandler(file_handler) | ||
return logger | ||
|
||
|
||
if __name__ == "__main__": | ||
logger = setup_logging() | ||
# This example is for Application Board 3.1 hardware | ||
shuttle = BMP390Shuttle.on_hardware_v3_rev1() | ||
shuttle.initialize() | ||
shuttle.check_connected_hw() | ||
|
||
shuttle.configure_i2c() | ||
logger.info(f"chip_id=0x{shuttle.sensor.chip_id:02X}") | ||
assert shuttle.sensor.chip_id == 0x60 | ||
_ = shuttle.sensor.compensate_temperature(0x0000) # cache temperature NVM registers | ||
_ = shuttle.sensor.compensate_pressure(0x00, 24.0) # cache pressure NVM registers | ||
shuttle.configure_interrupt_streaming() | ||
shuttle.start_streaming() | ||
time.sleep(0.1) | ||
for idx in range(1000): | ||
for streaming in shuttle.board.receive_interrupt_streaming_multiple(includes_mcu_timestamp=False): | ||
sensor_id, packet, time_stamp, payload = streaming | ||
pressure_b0, pressure_b1, pressure_b2, temp_b0, temp_b1, temp_b2, time_b0, time_b1, time_b2 = struct.unpack( | ||
"<BBBBBBxxBBB", payload | ||
) | ||
raw_pressure = (pressure_b2 << 16) | (pressure_b1 << 8) | pressure_b0 | ||
raw_temperature = (temp_b2 << 16) | (temp_b1 << 8) | temp_b0 | ||
sensor_time = (time_b2 << 16) | (time_b1 << 8) | time_b0 | ||
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature) | ||
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature) | ||
logger.info( | ||
f"[{idx=:03d}], pressure(Pa)={compensated_pressure:7.3f}, " | ||
f"temperature(C)={compensated_temperature:3.3f}, " | ||
f"time_stamp(s)={sensor_time / 25908.533}" | ||
) | ||
time.sleep(0.05) | ||
shuttle.board.stop_interrupt_streaming() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import logging | ||
import struct | ||
import sys | ||
import time | ||
from pathlib import Path | ||
|
||
from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle | ||
|
||
|
||
def setup_logging(level: int = logging.DEBUG) -> logging.Logger: | ||
logger = logging.getLogger() | ||
logger.setLevel(level) | ||
stdout_handler = logging.StreamHandler(sys.stdout) | ||
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s" | ||
log_formatter = logging.Formatter(log_format) | ||
stdout_handler.setFormatter(log_formatter) | ||
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w") | ||
file_handler.setFormatter(log_formatter) | ||
logger.addHandler(stdout_handler) | ||
logger.addHandler(file_handler) | ||
return logger | ||
|
||
|
||
if __name__ == "__main__": | ||
logger = setup_logging() | ||
# This example is for Application Board 3.1 hardware | ||
shuttle = BMP390Shuttle.on_hardware_v3_rev1() | ||
shuttle.initialize() | ||
shuttle.check_connected_hw() | ||
|
||
shuttle.configure_i2c() | ||
logger.info(f"chip_id=0x{shuttle.sensor.chip_id:04X}") | ||
assert shuttle.sensor.chip_id == 0x60 | ||
_ = shuttle.sensor.compensate_temperature(0x0000) # caching NVM registers | ||
_ = shuttle.sensor.compensate_pressure(0x00, 24.0) # caching NVM registers | ||
shuttle.configure_polling_streaming() | ||
shuttle.start_streaming() | ||
time.sleep(0.1) | ||
for idx in range(1000): | ||
for streaming in shuttle.board.receive_polling_streaming_multiple(): | ||
sensor_id, payload = streaming | ||
(pressure_b0, pressure_b1, pressure_b2, temp_b0, temp_b1, temp_b2, time_b0, time_b1, time_b2) = ( | ||
struct.unpack("<BBBBBBxxBBB", payload) | ||
) | ||
raw_pressure = (pressure_b2 << 16) | (pressure_b1 << 8) | pressure_b0 | ||
raw_temperature = (temp_b2 << 16) | (temp_b1 << 8) | temp_b0 | ||
sensor_time = (time_b2 << 16) | (time_b1 << 8) | time_b0 | ||
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature) | ||
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature) | ||
logger.info( | ||
f"[{idx=:03d}], pressure(Pa)={compensated_pressure:7.3f}, " | ||
f"temperature(C)={compensated_temperature:3.3f}, " | ||
f"time_stamp(s)={sensor_time / 25908.533}" | ||
) | ||
time.sleep(0.05) | ||
shuttle.board.stop_polling_streaming() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import logging | ||
import sys | ||
import time | ||
from pathlib import Path | ||
|
||
from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle | ||
|
||
|
||
def setup_logging(level: int = logging.DEBUG) -> logging.Logger: | ||
logger = logging.getLogger() | ||
logger.setLevel(level) | ||
stdout_handler = logging.StreamHandler(sys.stdout) | ||
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s" | ||
log_formatter = logging.Formatter(log_format) | ||
stdout_handler.setFormatter(log_formatter) | ||
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w") | ||
file_handler.setFormatter(log_formatter) | ||
logger.addHandler(stdout_handler) | ||
logger.addHandler(file_handler) | ||
return logger | ||
|
||
|
||
if __name__ == "__main__": | ||
logger = setup_logging() | ||
shuttle = BMP390Shuttle.on_hardware_v3_rev1() | ||
shuttle.initialize() | ||
shuttle.check_connected_hw() | ||
|
||
shuttle.configure_i2c() | ||
|
||
logger.info(f"chip_id=0x{shuttle.sensor.chip_id:02X}") | ||
logger.info(f"rev_id=0x{shuttle.sensor.rev_id:02X}") | ||
logger.info(f"err_reg=0x{shuttle.sensor.err_reg:02X}") | ||
logger.info(f"status=0x{shuttle.sensor.status:02X}") | ||
logger.info(f"pwr_ctrl=0b{shuttle.sensor.pwr_ctrl:08b}") | ||
logger.info(f"odr=0b{shuttle.sensor.odr:08b}") | ||
logger.info(f"osr=0b{shuttle.sensor.osr:08b}") | ||
|
||
shuttle.sensor.pwr_ctrl = (1 << 0) | (1 << 1) | (0b11 << 4) | ||
time.sleep(0.1) | ||
logger.info(f"pwr_ctrl=0b{shuttle.sensor.pwr_ctrl:08b}") | ||
|
||
raw_temperature = shuttle.sensor.temperature | ||
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature) | ||
logger.info(f"temperature(raw)=0x{raw_temperature:06X}, temperature(C)={compensated_temperature}") | ||
raw_pressure = shuttle.sensor.pressure | ||
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature) | ||
logger.info(f"pressure(raw)=0x{raw_pressure:06X}, pressure(Pa)={compensated_pressure}") | ||
logger.info(f"sensor_time=0x{shuttle.sensor.sensor_time:06X}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import logging | ||
import struct | ||
import sys | ||
import time | ||
from pathlib import Path | ||
|
||
from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle | ||
|
||
|
||
def setup_logging(level: int = logging.DEBUG) -> logging.Logger: | ||
logger = logging.getLogger() | ||
logger.setLevel(level) | ||
stdout_handler = logging.StreamHandler(sys.stdout) | ||
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s" | ||
log_formatter = logging.Formatter(log_format) | ||
stdout_handler.setFormatter(log_formatter) | ||
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w") | ||
file_handler.setFormatter(log_formatter) | ||
logger.addHandler(stdout_handler) | ||
logger.addHandler(file_handler) | ||
return logger | ||
|
||
|
||
if __name__ == "__main__": | ||
logger = setup_logging() | ||
shuttle = BMP390Shuttle.on_hardware_v3_rev1() | ||
shuttle.initialize() | ||
shuttle.check_connected_hw() | ||
|
||
shuttle.configure_spi() | ||
_ = shuttle.board.read_spi(shuttle.CS, 0, 1) # dummy read is required, do not delete | ||
|
||
logger.info(f"chip_id=0x{shuttle.sensor.chip_id:02X}") | ||
assert shuttle.sensor.chip_id == 0x60 | ||
_ = shuttle.sensor.compensate_temperature(0x0000) # cache temperature NVM registers | ||
_ = shuttle.sensor.compensate_pressure(0x00, 24.0) # cache pressure NVM registers | ||
shuttle.configure_interrupt_streaming() | ||
shuttle.start_streaming() | ||
time.sleep(0.1) | ||
for idx in range(1000): | ||
for streaming in shuttle.board.receive_interrupt_streaming_multiple(includes_mcu_timestamp=False): | ||
sensor_id, packet, time_stamp, payload = streaming | ||
pressure_b0, pressure_b1, pressure_b2, temp_b0, temp_b1, temp_b2, time_b0, time_b1, time_b2 = struct.unpack( | ||
"<xBBBBBBxxBBB", payload | ||
) | ||
raw_pressure = (pressure_b2 << 16) | (pressure_b1 << 8) | pressure_b0 | ||
raw_temperature = (temp_b2 << 16) | (temp_b1 << 8) | temp_b0 | ||
sensor_time = (time_b2 << 16) | (time_b1 << 8) | time_b0 | ||
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature) | ||
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature) | ||
logger.info( | ||
f"[{idx=:03d}], pressure(Pa)={compensated_pressure:7.3f}, " | ||
f"temperature(C)={compensated_temperature:3.3f}, " | ||
f"time_stamp(s)={sensor_time / 25908.533:.3f}" | ||
) | ||
time.sleep(0.05) | ||
shuttle.board.stop_interrupt_streaming() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import logging | ||
import struct | ||
import sys | ||
import time | ||
from pathlib import Path | ||
|
||
from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle | ||
|
||
|
||
def setup_logging(level: int = logging.DEBUG) -> logging.Logger: | ||
logger = logging.getLogger() | ||
logger.setLevel(level) | ||
stdout_handler = logging.StreamHandler(sys.stdout) | ||
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s" | ||
log_formatter = logging.Formatter(log_format) | ||
stdout_handler.setFormatter(log_formatter) | ||
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w") | ||
file_handler.setFormatter(log_formatter) | ||
logger.addHandler(stdout_handler) | ||
logger.addHandler(file_handler) | ||
return logger | ||
|
||
|
||
if __name__ == "__main__": | ||
logger = setup_logging() | ||
# This example is for Application Board 3.1 hardware | ||
shuttle = BMP390Shuttle.on_hardware_v3_rev1() | ||
shuttle.initialize() | ||
shuttle.check_connected_hw() | ||
|
||
shuttle.configure_spi() | ||
_ = shuttle.board.read_spi(shuttle.CS, 0, 1) # dummy read is required, do not delete | ||
|
||
logger.info(f"chip_id=0x{shuttle.sensor.chip_id:02X}") | ||
assert shuttle.sensor.chip_id == 0x60 | ||
_ = shuttle.sensor.compensate_temperature(0x0000) # caching NVM registers | ||
_ = shuttle.sensor.compensate_pressure(0x00, 24.0) # caching NVM registers | ||
shuttle.configure_polling_streaming() | ||
shuttle.start_streaming() | ||
time.sleep(0.1) | ||
for idx in range(1000): | ||
for streaming in shuttle.board.receive_polling_streaming_multiple(): | ||
sensor_id, payload = streaming | ||
(pressure_b0, pressure_b1, pressure_b2, temp_b0, temp_b1, temp_b2, time_b0, time_b1, time_b2) = ( | ||
struct.unpack("<xBBBBBBxxBBB", payload) | ||
) | ||
raw_pressure = (pressure_b2 << 16) | (pressure_b1 << 8) | pressure_b0 | ||
raw_temperature = (temp_b2 << 16) | (temp_b1 << 8) | temp_b0 | ||
sensor_time = (time_b2 << 16) | (time_b1 << 8) | time_b0 | ||
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature) | ||
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature) | ||
logger.info( | ||
f"[{idx=:03d}], pressure(Pa)={compensated_pressure:7.3f}, " | ||
f"temperature(C)={compensated_temperature:3.3f}, " | ||
f"time_stamp(s)={sensor_time / 25908.533:.3f}" | ||
) | ||
time.sleep(0.05) | ||
shuttle.board.stop_polling_streaming() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import sys | ||
import time | ||
from pathlib import Path | ||
|
||
from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle | ||
|
||
|
||
def setup_logging(level: int = logging.DEBUG) -> logging.Logger: | ||
logger = logging.getLogger() | ||
logger.setLevel(level) | ||
stdout_handler = logging.StreamHandler(sys.stdout) | ||
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s" | ||
log_formatter = logging.Formatter(log_format) | ||
stdout_handler.setFormatter(log_formatter) | ||
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w") | ||
file_handler.setFormatter(log_formatter) | ||
logger.addHandler(stdout_handler) | ||
logger.addHandler(file_handler) | ||
return logger | ||
|
||
|
||
if __name__ == "__main__": | ||
logger = setup_logging() | ||
shuttle = BMP390Shuttle.on_hardware_v3_rev1() | ||
shuttle.initialize() | ||
shuttle.check_connected_hw() | ||
|
||
shuttle.configure_spi() | ||
_ = shuttle.board.read_spi(shuttle.CS, 0, 1) # dummy read is required, do not delete | ||
|
||
logger.info(f"chip_id=0x{shuttle.sensor.chip_id:02X}") | ||
logger.info(f"rev_id=0x{shuttle.sensor.rev_id:02X}") | ||
logger.info(f"err_reg=0x{shuttle.sensor.err_reg:02X}") | ||
logger.info(f"status=0x{shuttle.sensor.status:02X}") | ||
logger.info(f"pwr_ctrl=0b{shuttle.sensor.pwr_ctrl:08b}") | ||
logger.info(f"odr=0b{shuttle.sensor.odr:08b}") | ||
logger.info(f"osr=0b{shuttle.sensor.osr:08b}") | ||
|
||
shuttle.sensor.pwr_ctrl = (1 << 0) | (1 << 1) | (0b11 << 4) | ||
time.sleep(0.1) | ||
logger.info(f"pwr_ctrl=0b{shuttle.sensor.pwr_ctrl:08b}") | ||
|
||
raw_temperature = shuttle.sensor.temperature | ||
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature) | ||
logger.info(f"temperature(raw)=0x{raw_temperature:06X}, temperature(C)={compensated_temperature}") | ||
raw_pressure = shuttle.sensor.pressure | ||
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature) | ||
logger.info(f"pressure(raw)=0x{raw_pressure:06X}, pressure(Pa)={compensated_pressure}") | ||
logger.info(f"sensor_time=0x{shuttle.sensor.sensor_time:06X}") |
Oops, something went wrong.