-
Notifications
You must be signed in to change notification settings - Fork 0
/
accelerometer.py
65 lines (55 loc) · 2.43 KB
/
accelerometer.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
63
64
65
# -*- coding: utf-8 -*-
from __future__ import division
import sys, os, math
import RPi.GPIO as GPIO
from str_bit_helper import *
GP_DATA_READY_INT_PIN = 22
# using Bosch's BMA180 - 3-axial MEMS accelerometer
class Accelerometer():
# technically almost ready to use I2C if provided
def __init__(self, interface):
self.read_acc_values_address = bits2str(fill_bits_to_byte(bin(0x02)[2:]))
self.interface = interface
self.callbacks = set()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(GP_DATA_READY_INT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(
GP_DATA_READY_INT_PIN,
GPIO.RISING,
callback=self.accelerometer_int
)
# allow changing configuration (originally 0x00)
interface.send(False, int2char(0x0D), int2char(0x10))
# cutting bandwidth <10Hz to get better SNR (0x48)
interface.send(False, int2char(0x20), int2char(0x08))
# disable i2c (0x64)
# interface.send(False, int2char(0x27), int2char(0x65))
# set range to +/- 1G (0x54)
interface.send(False, int2char(0x35), int2char(0x51))
# set interrupts on new data available (0x04)
interface.send(False, int2char(0x21), int2char(0x02))
# all changes are temporary (till power off), no saving added
self.__read_acc()
print('Accelerometer initialized with interface ' + str(interface))
def __read_acc(self):
return self.interface.send(True, self.read_acc_values_address, '??????')
def accelerometer_int(self, channel):
x,y,z = self.read_accelerometer_values()
for callback in self.callbacks:
callback(x,y,z)
def read_accelerometer_values(self):
read_data = self.__read_acc()
x_value = self.__get_value(read_data[1:2], read_data[0:1])
y_value = self.__get_value(read_data[3:4], read_data[2:3])
z_value = self.__get_value(read_data[5:6], read_data[4:5])
return (x_value, y_value, z_value)
def add_callback(self, callback):
self.callbacks.add(callback)
def remove_callback(self, callback):
self.callbacks.remove(callback)
def __get_value(self, high_byte_char, low_byte_char):
value = str2bits(high_byte_char) + str2bits(low_byte_char)[:-2]
int_value = int(value, 2)
if (int_value >= (1 << 13)):
int_value -= (1 << 14)
return int_value