Skip to content

Instantly share code, notes, and snippets.

@buhii
Created July 22, 2012 19:05
Show Gist options
  • Save buhii/3160723 to your computer and use it in GitHub Desktop.
Save buhii/3160723 to your computer and use it in GitHub Desktop.
LSM303DLH
from struct import unpack
import time
import serial
v = serial.Serial('COM14', parity=serial.PARITY_NONE, baudrate=9600)
sax, say, saz, count = 0, 0, 0, 0
cax, cay, caz = 198, -129, 16974
c = 0
counter = 0
old_counter = 0
sum_diff = 0
now = time.time()
while True:
c += 1
if v.read() == '\xff' and v.read() == '\x27' and v.read() == '\x95':
ax = unpack('<h', v.read(2))[0]
ay = unpack('<h', v.read(2))[0]
az = unpack('<h', v.read(2))[0]
mx = unpack('<h', v.read(2))[0] >> 4
my = unpack('<h', v.read(2))[0] >> 4
mz = unpack('<h', v.read(2))[0] >> 4
old_counter = counter
old = now
now = time.time()
diff_time = now - old
counter = unpack('<H', v.read(2))[0]
if counter > old_counter:
diff = counter - old_counter
else:
diff = counter + 65536 - old_counter
sax += ax
say += ay
saz += az
sum_diff += diff
print "count: %d\t: %d\ty: %d\tz:%d\tmx: %d\tmy: %d\tmz: %d" % (diff, (ax - cax), (ay - cay), (az - caz), mx, my, mz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment