-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcompass.py
62 lines (50 loc) · 1.9 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
58
59
60
61
62
###############################################################################
#
# Copyright (c) 2017-2024 Master AI, Inc.
# ALL RIGHTS RESERVED
#
# Use of this library, in source or binary form, is prohibited without written
# approval from Master AI, Inc.
#
###############################################################################
"""
This module provides a simplified interface to the Compass sensor.
"""
from auto.asyncio_tools import thread_safe
from auto.capabilities import list_caps, acquire
def query():
"""
Query the Compass sensor.
Returns `theta`, a single angle (in degrees) representing your
device's deviation from magnetic north.
- `theta = 0` means your device is pointing directly north.
- `theta = 90` means your device is pointing directly west.
- `theta = 180` means your device is pointing directly south.
- `theta = 270` means your device is pointing directly east.
"""
return _get_compass().query()
def heading():
"""
Query the Compass sensor and return a true heading of your device.
**ONLY AVAILABLE ON VIRTUAL DEVICES!**
Real-world compasses cannot give you this information, but the
"virtual" compass can. :)
Returns a tuple (`theta`, `phi`) describing the true heading of
your device. `theta` is an angle (in degrees) of your device's
rotation in the world's horizontal plane. `phi` is an angle (in
degrees) of your device's rotation away from the vertical axis.
See `lidar.single()` for more info on the concept of `theta` and
`phi` and on the concept of spherical coordinates.
"""
return _get_compass().heading()
@thread_safe
def _get_compass():
global _COMPASS
try:
_COMPASS
except NameError:
caps = list_caps()
if 'Compass' not in caps:
raise AttributeError('This device has no Compass!')
_COMPASS = acquire('Compass')
return _COMPASS