save useful scripts
[aversive.git] / projects / microb2010 / tests / static_beacon / coding.py
1 #!/usr/bin/python
2
3 import sys, math
4
5 RPS = 10.
6 LASER_RADIUS = 25. # mm
7
8 MIN = 200.
9 MAX = 3500.
10 NBITS = 9
11 STEPS = (1 << 9)
12 k = math.pow(MAX/MIN, 1./STEPS)
13
14 # t is in us, result is 9 bits
15 def time_to_frame(t):
16     # process angle from t
17     a = (t / (1000000./RPS)) * 2. * math.pi
18
19     # process d from a (between 20cm and 350cm)
20     d = LASER_RADIUS / math.sin(a/2)
21
22     frame = math.log(d/MIN)/math.log(k)
23     if frame >= 512:
24         frame = 511
25     else:
26         frame = int(frame)
27     print frame
28     return frame
29
30 # frame is integer 9 bits, result is distance
31 def frame_to_distance(frame):
32     d = MIN*(math.pow(k, frame))
33     print d
34     return d
35
36 x = time_to_frame(float(sys.argv[1]))
37 frame_to_distance(x)