3232THE SOFTWARE.
3333'''
3434
35- from grove .i2c import Bus
3635import time
36+ from grove .i2c import Bus
3737
3838# Class for interacting with the HP20x sensor
3939class HP20x :
4040 def __init__ (self ):
4141 # Initialize the I2C bus on Raspberry Pi (bus 1)
42- self .bus = Bus ()
42+ self .bus = Bus ()
4343 # I2C address of the HP206F sensor, may need adjustment based on actual situation
44- self .address = 0x76
44+ self .address = 0x76
4545
4646 # I2C device ID when CSB PIN is at VDD level (address is 0x76)
47- self .HP20X_I2C_DEV_ID = (0xEC ) >> 1
47+ self .HP20X_I2C_DEV_ID = (0xEC ) >> 1
4848 # I2C device ID when CSB PIN is at GND level (address is 0x77)
49- self .HP20X_I2C_DEV_ID2 = (0XEE ) >> 1
49+ self .HP20X_I2C_DEV_ID2 = (0XEE ) >> 1
5050 # Soft reset command for the HP20x sensor
51- self .HP20X_SOFT_RST = 0x06
51+ self .HP20X_SOFT_RST = 0x06
5252 # Write conversion command for the HP20x sensor
53- self .HP20X_WR_CONVERT_CMD = 0x40
53+ self .HP20X_WR_CONVERT_CMD = 0x40
5454 # Different oversampling rate (OSR) configurations for conversion
55- self .HP20X_CONVERT_OSR4096 = 0 << 2
56- self .HP20X_CONVERT_OSR2048 = 1 << 2
57- self .HP20X_CONVERT_OSR1024 = 2 << 2
58- self .HP20X_CONVERT_OSR512 = 3 << 2
59- self .HP20X_CONVERT_OSR256 = 4 << 2
60- self .HP20X_CONVERT_OSR128 = 5 << 2
55+ self .HP20X_CONVERT_OSR4096 = 0 << 2
56+ self .HP20X_CONVERT_OSR2048 = 1 << 2
57+ self .HP20X_CONVERT_OSR1024 = 2 << 2
58+ self .HP20X_CONVERT_OSR512 = 3 << 2
59+ self .HP20X_CONVERT_OSR256 = 4 << 2
60+ self .HP20X_CONVERT_OSR128 = 5 << 2
6161
6262 # Commands for reading pressure, altitude, temperature, etc.
6363 self .HP20X_READ_P = 0x30 # Read pressure command
@@ -68,30 +68,30 @@ def __init__(self):
6868 self .HP20X_READ_CAL = 0X28 # RE-CAL ANALOG command
6969
7070 # Write register mode for the HP20x sensor
71- self .HP20X_WR_REG_MODE = 0xC0
71+ self .HP20X_WR_REG_MODE = 0xC0
7272 # Read register mode for the HP20x sensor
73- self .HP20X_RD_REG_MODE = 0x80
73+ self .HP20X_RD_REG_MODE = 0x80
7474
7575 # Set the oversampling rate configuration
76- self .OSR_CFG = self .HP20X_CONVERT_OSR1024
76+ self .OSR_CFG = self .HP20X_CONVERT_OSR1024
7777 # Conversion time corresponding to the oversampling rate (in milliseconds)
78- self .OSR_ConvertTime = 25
78+ self .OSR_ConvertTime = 25
7979
8080 def begin (self ):
8181 # Send a soft reset command to the HP20x sensor
8282 self .HP20X_IIC_WriteCmd (self .HP20X_SOFT_RST )
8383 # Wait for 0.1 seconds to ensure the reset operation is completed
84- time .sleep (0.1 )
84+ time .sleep (0.1 )
8585
8686 def isAvailable (self ):
8787 # Check if the HP20x sensor is available by reading the register at address 0x0F
88- return self .HP20X_IIC_ReadReg (0x0F )
88+ return self .HP20X_IIC_ReadReg (0x0F )
8989
9090 def ReadTemperature (self ):
9191 # Send a conversion command with the specified oversampling rate configuration
9292 self .HP20X_IIC_WriteCmd (self .HP20X_WR_CONVERT_CMD | self .OSR_CFG )
9393 # Wait for the conversion time (converted to seconds)
94- time .sleep (self .OSR_ConvertTime / 1000.0 )
94+ time .sleep (self .OSR_ConvertTime / 1000.0 )
9595 # Read 3 bytes of raw temperature data from the sensor
9696 t_raw = self .bus .read_i2c_block_data (self .address , self .HP20X_READ_T , 3 )
9797 # Combine the 3 bytes of data to form a single value
@@ -102,13 +102,13 @@ def ReadTemperature(self):
102102 us = (1 << 32 )
103103 t = - 1 * (us - t )
104104 # Return the temperature value in degrees Celsius (divided by 100)
105- return t / 100.0
105+ return t / 100.0
106106
107107 def ReadPressure (self ):
108108 # Send a conversion command with the specified oversampling rate configuration
109109 self .HP20X_IIC_WriteCmd (self .HP20X_WR_CONVERT_CMD | self .OSR_CFG )
110110 # Wait for the conversion time (converted to seconds)
111- time .sleep (self .OSR_ConvertTime / 1000.0 )
111+ time .sleep (self .OSR_ConvertTime / 1000.0 )
112112 # Read 3 bytes of raw pressure data from the sensor
113113 p_raw = self .bus .read_i2c_block_data (self .address , self .HP20X_READ_P , 3 )
114114 # Combine the 3 bytes of data to form a single value
@@ -117,13 +117,13 @@ def ReadPressure(self):
117117 if p & 0x800000 :
118118 p |= 0xff000000
119119 # Return the pressure value in hectopascals (divided by 100)
120- return p / 100.0
120+ return p / 100.0
121121
122122 def ReadAltitude (self ):
123123 # Send a conversion command with the specified oversampling rate configuration
124124 self .HP20X_IIC_WriteCmd (self .HP20X_WR_CONVERT_CMD | self .OSR_CFG )
125125 # Wait for the conversion time (converted to seconds)
126- time .sleep (self .OSR_ConvertTime / 1000.0 )
126+ time .sleep (self .OSR_ConvertTime / 1000.0 )
127127 # Read 3 bytes of raw altitude data from the sensor
128128 a_raw = self .bus .read_i2c_block_data (self .address , self .HP20X_READ_A , 3 )
129129 # Combine the 3 bytes of data to form a single value
@@ -134,29 +134,30 @@ def ReadAltitude(self):
134134 us = (1 << 32 )
135135 a = - 1 * (us - a )
136136 # Return the altitude value in meters (divided by 100)
137- return a / 100.0
137+ return a / 100.0
138138
139139 def HP20X_IIC_WriteCmd (self , uCmd ):
140140 # Write a command byte to the specified I2C address
141- self .bus .write_byte_data ( 0 , self .address ,uCmd )
141+ self .bus .write_byte ( self .address , uCmd )
142142
143143 def HP20X_IIC_ReadReg (self , bReg ):
144144 # Read a byte from the specified register address
145- return self .bus .read_byte_data (self .address , bReg | self .HP20X_RD_REG_MODE )
145+ return self .bus .read_byte_data (self .address , bReg | self .HP20X_RD_REG_MODE )
146+
146147
147148# Class representing the Kalman filter
148149class KalmanFilter :
149150 def __init__ (self ):
150151 # Process noise covariance
151- self .q = 0.01
152+ self .q = 0.01
152153 # Measurement noise covariance
153- self .r = 0.1
154+ self .r = 0.1
154155 # Initial estimated value
155- self .x = 0
156+ self .x = 0
156157 # Initial estimated error covariance
157- self .p = 1
158+ self .p = 1
158159 # Initial Kalman gain
159- self .k = 0
160+ self .k = 0
160161
161162 def Filter (self , measurement ):
162163 # Prediction step: Update the estimated error covariance
@@ -172,26 +173,26 @@ def Filter(self, measurement):
172173
173174
174175# Kalman filter for temperature data
175- t_filter = KalmanFilter ()
176+ t_filter = KalmanFilter ()
176177# Kalman filter for pressure data
177- p_filter = KalmanFilter ()
178+ p_filter = KalmanFilter ()
178179# Kalman filter for altitude data
179- a_filter = KalmanFilter ()
180+ a_filter = KalmanFilter ()
180181
181182# Create an instance of the HP20x sensor
182- hp20x = HP20x ()
183+ hp20x = HP20x ()
183184
184185
185186# Function to simulate the setup process
186187def setup ():
187188 print ("****HP20x_dev demo by seeed studio****\n " )
188189 print ("Calculation formula: H = [8.5(101325-P)]/100 \n " )
189190 # Wait for 0.15 seconds after power-on to stabilize the voltage
190- time .sleep (0.15 )
191+ time .sleep (0.15 )
191192 # Initialize the HP20x sensor
192193 hp20x .begin ()
193194 # Wait for 0.1 seconds
194- time .sleep (0.1 )
195+ time .sleep (0.1 )
195196 # Check if the HP20x sensor is available
196197 ret = hp20x .isAvailable ()
197198 if ret :
@@ -238,4 +239,5 @@ def loop(ret):
238239 # Perform the setup process
239240 ret = setup ()
240241 # Start the loop process if the sensor is available
241- loop (ret )
242+ loop (ret )
243+
0 commit comments