Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TEACHING-MATERIALS/sample_code/CMPS12_compass_I2C.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
35 lines (27 sloc)
802 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# This code sample reads the current bearing of a CMPS12 Tilt-Compensated | |
# compass attached to the I2C interface on a Raspberry Pi. | |
# https://www.robot-electronics.co.uk/files/cmps12.pdf | |
# $ sudo apt-get install python3 python3-pip python-dev python-smbus | |
# $ sudo python3 -m pip install --upgrade pip setuptools wheel | |
# $ sudo pip3 install smbus | |
#!/usr/bin/env python3 | |
#!/usr/bin/env python3 | |
import smbus | |
import time | |
import statistics | |
bus = smbus.SMBus(1) | |
DEVICE_ADDRESS = 0x60 | |
DEVICE_REG_BEARING = 0x01 | |
values = [] | |
while 1: | |
try: | |
bearing = bus.read_byte_data(DEVICE_ADDRESS, DEVICE_REG_BEARING) | |
bearing = int((bearing / 255) * 360) | |
values.append(bearing) | |
if len(values) > 10: | |
values.pop(0) | |
print(statistics.mode(values)) | |
time.sleep(0.1) | |
except: | |
pass |