--- /dev/null
+#!/usr/bin/python
+
+import random
+
+# val is 12 bits. Return the 16 bits value that includes the 4 bits
+# cksum in MSB.
+def do_cksum(val):
+ x = (val & 0xfff)
+ # add the three 4-bits blocks of x together
+ cksum = x & 0xf
+ x = x >> 4
+ cksum += x & 0xf
+ cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4)
+ x = x >> 4
+ cksum += x & 0xf
+ cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4)
+ cksum = (~cksum) & 0xf
+ return (cksum << 12) + (val & 0xfff)
+
+# val is 16 bits, including 4 bits-cksum in MSB, return 0xFFFF is cksum
+# is wrong, or the 12 bits value on success.
+def verify_cksum(val):
+ x = (val & 0xfff)
+ # add the four 4-bits blocks of val together
+ cksum = val & 0xf
+ val = val >> 4
+ cksum += val & 0xf
+ cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4)
+ val = val >> 4
+ cksum += val & 0xf
+ cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4)
+ val = val >> 4
+ cksum += val & 0xf
+ cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4)
+ if cksum == 0xf:
+ return x
+ return 0xffff # wrong value
+
+
+
+err = 0
+total = 0
+for i in range(200):
+ x = random.randint(0,(1<<12)-1)
+ y = do_cksum(x)
+ y = random.randint(0,(1<<16)-1)
+ z = verify_cksum(y)
+ if z == 0xffff:
+ err += 1
+ total += 1
+print "%d errors / %d"%(err, total)
--- /dev/null
+#!/usr/bin/python
+
+import sys, math
+
+RPS = 10.
+LASER_RADIUS = 25. # mm
+
+MIN = 200.
+MAX = 3500.
+NBITS = 9
+STEPS = (1 << 9)
+k = math.pow(MAX/MIN, 1./STEPS)
+
+# t is in us, result is 9 bits
+def time_to_frame(t):
+ # process angle from t
+ a = (t / (1000000./RPS)) * 2. * math.pi
+
+ # process d from a (between 20cm and 350cm)
+ d = LASER_RADIUS / math.sin(a/2)
+
+ frame = math.log(d/MIN)/math.log(k)
+ if frame >= 512:
+ frame = 511
+ else:
+ frame = int(frame)
+ print frame
+ return frame
+
+# frame is integer 9 bits, result is distance
+def frame_to_distance(frame):
+ d = MIN*(math.pow(k, frame))
+ print d
+ return d
+
+x = time_to_frame(float(sys.argv[1]))
+frame_to_distance(x)