-
Notifications
You must be signed in to change notification settings - Fork 4
/
compass.py
57 lines (44 loc) · 1.55 KB
/
compass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import time
import math
import adafruit_lsm9ds1
import board
import busio
import neopixel
PURPLE = (180, 0, 255)
i2c = busio.I2C(board.SCL, board.SDA)
compass = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)
pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.01, auto_write=False)
# (x, y, z) tuples:
MAG_MIN = (-0.25046, -0.23506, -0.322)
MAG_MAX = (0.68278, 0.70882, 0.59654)
def bearing_to_pixel(bearing, count=16):
pixel = count - int(round((bearing / 360) * count))
if (pixel == 16):
pixel = 0
return pixel
def map_range(x, in_min, in_max, out_min, out_max):
"""
Maps a number from one range to another.
:return: Returns value mapped to new range
:rtype: float
"""
mapped = (x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min
if out_min <= out_max:
return max(min(mapped, out_max), out_min)
return min(max(mapped, out_max), out_min)
while True:
mag_x, mag_y, mag_z = compass.magnetometer
mag_x = map_range(mag_x, MAG_MIN[0], MAG_MAX[0], -1, 1)
mag_y = map_range(mag_y, MAG_MIN[1], MAG_MAX[1], -1, 1)
mag_z = map_range(mag_z, MAG_MIN[2], MAG_MAX[2], -1, 1)
compass_heading = (math.atan2(mag_y, mag_x) * 180) / math.pi;
if compass_heading < 0:
compass_heading = 360 + compass_heading;
pixel = bearing_to_pixel(compass_heading)
print('Magnetometer: ({0:10.3f}, {1:10.3f}, {2:10.3f})'.format(mag_x, mag_y, mag_z))
print(compass_heading)
print('Lighting pixel: {}'.format(pixel))
pixels.fill((0,0,0))
pixels[pixel] = PURPLE
pixels.show()
time.sleep(0.2)