-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sensors.py
95 lines (76 loc) · 2.79 KB
/
Sensors.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 1 18:53:48 2018
@author: BrianPinto
"""
import Adafruit_GPIO.I2C as Adafruit_I2C
import Adafruit_BBIO.GPIO as GPIO
import time
class MultiPlexer(object):
def __init__(self, address=0x70):
self.i2c = Adafruit_I2C.get_i2c_device(address, busnum=2)
def select(self, port_id):
self.i2c.write8(0, 1 << port_id)
class DPressureSens(object):
plexer = MultiPlexer()
def __init__(self, name, mplx_id, address=0x28, maxpressure=1):
self.mplx_id = mplx_id
self.i2c = Adafruit_I2C.get_i2c_device(address, busnum=2)
self.name = name
self.maxpressure = maxpressure
self.pmin = 0.0
self.pmax = 150.0
self.outmin = int((2**14-1)*.1)
self.outmax = int((2**14-1)*.9)
self.clb = (self.pmax-self.pmin)/(self.outmax - self.outmin)
self.barfact = 1/14.5038
def calc_pressure(self, msb, lsb):
output = msb*256 + lsb
pressure = ((output-self.outmin)*self.clb + self.pmin)*self.barfact
return pressure
def get_value(self):
self.plexer.select(self.mplx_id)
sens_bytes = self.i2c.readList(register=0, length=2)
msb = sens_bytes[0]
lsb = sens_bytes[1]
pressure = self.calc_pressure(msb, lsb)
return pressure/self.maxpressure
def set_maxpressure(self, maxpressure):
self.maxpressure = maxpressure
class MPU_9150(object):
plexer = MultiPlexer(address=0x71)
def __init__(self, name, mplx_id, address=0x68):
power_mgmt_1 = 0x6b # register power management of IMU
self.name = name
self.mplx_id = mplx_id # plex ID of IMU
# MultiPlexer schlaten, um das Modul ansprechen zu koennen
self.i2c = Adafruit_I2C.get_i2c_device(address, busnum=2)
self.plexer.select(self.mplx_id)
time.sleep(.1)
# Power on of Acc
self.i2c.write8(power_mgmt_1, 0x00)
def _read_word(self, reg):
sens_bytes = self.i2c.readList(register=reg, length=2)
msb = sens_bytes[0]
lsb = sens_bytes[1]
value = (msb << 8) + lsb
return value
def _read_word_2c(self, reg):
val = self._read_word(reg)
if (val >= 0x8000):
return -((65535 - val) + 1)
else:
return val
def get_acceleration(self):
self.plexer.select(self.mplx_id)
acc_xout = self._read_word_2c(0x3b)
acc_yout = self._read_word_2c(0x3d)
acc_zout = self._read_word_2c(0x3f)
return (acc_xout, acc_yout, acc_zout)
class Button(object):
def __init__(self, port):
self.port=port
GPIO.setup(self.port, GPIO.IN)
GPIO.add_event_detect(self.port, GPIO.RISING)
def isPressed(self):
return GPIO.event_detected(self.port)