Skip to content

Commit ffb107f

Browse files
committed
add remember calibrate
1 parent 7801a48 commit ffb107f

File tree

1 file changed

+39
-10
lines changed
  • ports/esp32/boards/KidBright32/modules

1 file changed

+39
-10
lines changed

ports/esp32/boards/KidBright32/modules/imu.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
def b2i(x, y):
6060
return (x << 8 | y) if not x & 0x80 else (-(((x ^ 255) << 8) | (y ^ 255) + 1))
6161

62+
def i2b(n):
63+
if n < 0:
64+
n = (-n ^ 0xFFFF) + 1
65+
return bytes([ (n >> 8) & 0xFF, n & 0xFF ])
6266

6367
def update():
6468
global acc, gyro, mag, temp
@@ -155,10 +159,13 @@ def heading():
155159

156160

157161
def calibrate_compass():
158-
global calibrateMagFlag
162+
global calibrateMagFlag, mag_min, mag_max
159163
import display
160164
from time import sleep
161165

166+
mag_min = [ 99999, 99999, 99999 ]
167+
mag_max = [ -99999, -99999, -99999 ]
168+
162169
display.scroll("TILT TO FILL SCREEN")
163170

164171
buffer = bytearray(16)
@@ -226,17 +233,39 @@ def calibrate_compass():
226233
calibrateMagFlag = True
227234

228235
def loadCalibrateFromSRAM():
229-
return False
236+
buff = i2c1.readfrom_mem(0x6F, 0x20 + 0, 13)
237+
crc = 0
238+
for i in range(12):
239+
crc = crc + buff[i]
240+
crc = crc & 0xFF
241+
242+
if crc != buff[12]:
243+
return False
244+
245+
for i in range(3):
246+
mag_min[i] = b2i(buff[(i * 2) + 0], buff[(i * 2) + 1]) / 100.0
247+
for i in range(3):
248+
mag_max[i] = b2i(buff[(i * 2) + 0 + 6], buff[(i * 2) + 1 + 6]) / 100.0
230249

231-
def saveCalibrateIntoSRAM():
232250
return True
233251

234-
def calCRC(data, size):
235-
sum = b'\0'
236-
for i in range(size):
237-
sum += data[i]
238-
sum = sum ^ 0xFF
239-
return sum
252+
def saveCalibrateIntoSRAM():
253+
buff = bytearray(13)
254+
for i in range(3):
255+
a = i2b(int(mag_min[i] * 100))
256+
buff[(i * 2) + 0] = a[0]
257+
buff[(i * 2) + 1] = a[1]
258+
for i in range(3):
259+
a = i2b(int(mag_max[i] * 100))
260+
buff[(i * 2) + 0 + 6] = a[0]
261+
buff[(i * 2) + 1 + 6] = a[1]
262+
crc = 0
263+
for i in range(12):
264+
crc = crc + buff[i]
265+
buff[12] = crc & 0xFF
266+
267+
i2c1.writeto_mem(0x6F, 0x20 + 0, buff)
268+
return True
240269

241270
def is_gesture(event, blocking=True):
242271
global __xStartCalc, __startCalcLowStrengthContinue
@@ -268,7 +297,7 @@ def is_gesture(event, blocking=True):
268297
else:
269298
lowStrengthContinue = False
270299
break
271-
return lowStrengthContinue;
300+
return lowStrengthContinue
272301
else:
273302
if acc[3] < 500:
274303
if not __startCalcLowStrengthContinue:

0 commit comments

Comments
 (0)