Skip to content

Commit

Permalink
Creates some "default" configurations for gas sensor.
Browse files Browse the repository at this point in the history
This can be a little difficult to set up, so cribbed the configuration setups from the sampleapps since they (obviously) were intended to work.
  • Loading branch information
EAGrahamJr committed Jun 17, 2024
1 parent 129bcba commit ba4f26f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 16 deletions.
80 changes: 68 additions & 12 deletions diozero-core/src/main/java/com/diozero/devices/BME68x.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Organisation: diozero
* Project: diozero - Core
* Filename: BME68x.java
*
*
* This file is part of the diozero project. More information about this project
* can be found at https://www.diozero.com/.
* %%
Expand All @@ -17,10 +17,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -39,13 +39,20 @@
import com.diozero.util.BitManipulation;
import com.diozero.util.SleepUtil;

/*-
*
* https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme680-ds001.pdf
* https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme688-ds000.pdf
* https://github.com/BoschSensortec/BME68x-Sensor-API
* https://github.com/pimoroni/bme680-python
* https://github.com/knobtviker/bme680
/**
* An environmental sensor: measures temperature, pressure, humity, and a "gas" sensor for general air quality. The
* actual values <i>may not</i> be entirely accurate for your environment, so may need minor adjustments: the typcal
* offset that needs to be applied witll be to the temperature (about 5 degrees high).
* <p>
* <b>NOTEL</b> once activated, it will take about 30 seconds for readings to become more precise.
* </p>
* <ul>
* <li>https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme680-ds001.pdf</li>
* <li>https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme688-ds000.pdf</li>
* <li>https://github.com/BoschSensortec/BME68x-Sensor-API</li>
* <li>https://github.com/pimoroni/bme680-python</li>
* <li>https://github.com/knobtviker/bme680</li>
* </ul>
*/
public class BME68x implements BarometerInterface, ThermometerInterface, HygrometerInterface {
// Chip vendor for the BME680
Expand Down Expand Up @@ -552,7 +559,7 @@ public BME68x(final I2CDeviceInterface device) {
* @param controller I2C bus the sensor is connected to.
* @param address I2C address of the sensor.
* @param humidityOversampling Humidity oversampling.
* @param termperatureOversampling Temperature oversampling.
* @param temperatureOversampling Temperature oversampling.
* @param pressureOversampling Pressure oversampling.
* @param filter Infinite Impulse Response (IIR) filter.
* @param standbyDuration Standby time between sequential mode
Expand All @@ -570,7 +577,7 @@ public BME68x(final int controller, final int address, OversamplingMultiplier hu
*
* @param device I2C device.
* @param humidityOversampling Humidity oversampling.
* @param termperatureOversampling Temperature oversampling.
* @param temperatureOversampling Temperature oversampling.
* @param pressureOversampling Pressure oversampling.
* @param filter Infinite Impulse Response (IIR) filter.
* @param standbyDuration Standby time between sequential mode
Expand Down Expand Up @@ -2012,4 +2019,53 @@ public String toString() {
+ ", heaterResistance=" + heaterResistance + ", gasWaitMs=" + gasWaitMs + "]";
}
}

/**
* Attempts to set reasonable defaults for the device since it's not that easy to configure.
*
* @return the determined operating mode for use with {@link #getSensorData(OperatingMode)}
*/
public OperatingMode calculateDefaultConfiguration() {
// if it's the 688, use parallel mode
if (getVariantId() == BME68x.VARIANT_ID_BM688) {
Logger.debug("Using PARALLEL mode");
setConfiguration(OversamplingMultiplier.X2, OversamplingMultiplier.X2, OversamplingMultiplier.X2,
IirFilterCoefficient.NONE, StandbyDuration.NONE);
OperatingMode targetOperatingMode = OperatingMode.PARALLEL;

// Calculate TPHG measure duration and convert to milliseconds
int measureDurationMs = calculateMeasureDuration(targetOperatingMode) / 1000;
Logger.debug("measureDurationMs: {}", measureDurationMs);

// Assume that 150ms is the required heater duration.
// https://github.com/BoschSensortec/BME68x-Sensor-API/blob/master/examples/parallel_mode/parallel_mode.c#L78
// p39: Measurement duration = gas_wait_X * (gas_wait_shared + TTPHG_duration)
int heaterDurationMs = 150;
int gasWaitSharedMs = heaterDurationMs - measureDurationMs;
Logger.debug("gasWaitSharedMs: {}", gasWaitSharedMs);

HeaterConfig heaterConfig = new HeaterConfig(true, //
// Heater temperature in degree Celsius
new int[] { 320, 100, 100, 100, 200, 200, 200, 320, 320, 320 },
// Multiplier to the shared heater duration
new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
gasWaitSharedMs);
setHeaterConfiguration(targetOperatingMode, heaterConfig);
return targetOperatingMode;
}
// otherwise force mode
setDefaultConfiguration();
return OperatingMode.FORCED;
}

/**
* Sets a reasonable default for FORCED mode operation.
*/
public void setDefaultConfiguration() {
Logger.debug("Using FORCED mode");
setConfiguration(OversamplingMultiplier.X2, OversamplingMultiplier.X2, OversamplingMultiplier.X2,
IirFilterCoefficient._3, StandbyDuration.NONE);

setHeaterConfiguration(OperatingMode.FORCED, new HeaterConfig(true, 320, 150));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Organisation: diozero
* Project: diozero - Sample applications
* Filename: BME68xTest.java
*
*
* This file is part of the diozero project. More information about this project
* can be found at https://www.diozero.com/.
* %%
Expand All @@ -17,10 +17,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -88,9 +88,13 @@ public static void main(String[] args) {

if (bme68x.getVariantId() == BME68x.VARIANT_ID_BM688) {
parallelModeTest(bme68x);
bme68x.softReset();

// should be identical to above
defaultConfigTest(bme68x);
bme68x.softReset();
}

bme68x.softReset();

iaqTest(bme68x);
} finally {
Expand Down Expand Up @@ -169,6 +173,15 @@ private static void parallelModeTest(BME68x bme68x) {

System.out.println("heater_duration_ms: " + heater_duration_ms);

runParallelModeTest(bme68x, heater_duration_ms, target_operating_mode);
}

private static void defaultConfigTest(BME68x bme68x) {
OperatingMode mode = bme68x.calculateDefaultConfiguration();
runParallelModeTest(bme68x, 150, mode);
}

private static void runParallelModeTest(BME68x bme68x, int heater_duration_ms, OperatingMode target_operating_mode) {
int last_gas_meas_idx = -1;
long last_gas_meas_ms = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
Expand Down

0 comments on commit ba4f26f

Please sign in to comment.