mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-12-22 21:45:48 +00:00
Programmed in interrupts
This commit is contained in:
parent
3e7a3cfad6
commit
298e7660b7
52
lis3dh.py
52
lis3dh.py
|
@ -44,38 +44,58 @@ INT1_CFG_V = bytes([0x95])
|
||||||
EMPTY = bytes([0x00])
|
EMPTY = bytes([0x00])
|
||||||
|
|
||||||
class lis3dh:
|
class lis3dh:
|
||||||
def __init__(self, i2cBus, resolution=2, samplerate=10, i2cAddress=0x18):
|
def __init__(self, i2cBus, samplerate=10, i2cAddress=0x18):
|
||||||
sleep(0.005)
|
sleep(0.005)
|
||||||
self.i2c = i2cBus
|
self.i2c = i2cBus
|
||||||
self.addr = i2cAddress
|
self.addr = i2cAddress
|
||||||
self.samplerate = samplerate
|
self.samplerate = samplerate
|
||||||
i2cBus.pec = True # enable smbus2 Packet Error Checking
|
i2cBus.pec = True # enable smbus2 Packet Error Checking
|
||||||
res_modes = {
|
|
||||||
2: 0b00, 4: 0b01,
|
|
||||||
8: 0b10, 16: 0b11
|
|
||||||
}
|
|
||||||
sample_modes = {
|
sample_modes = {
|
||||||
0:0x0, 1:0x1, 10:0x2, 25:0x3, 50:0x4,
|
0:0x0, 1:0x1, 10:0x2, 25:0x3, 50:0x4,
|
||||||
100:0x5, 200:0x6, 400:0x7
|
100:0x5, 200:0x6, 400:0x7
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if user-entered values are correct
|
# Check if user-entered values are correct
|
||||||
if resolution in res_modes:
|
|
||||||
self.resolution = resolution
|
|
||||||
else:
|
|
||||||
raise Exception("Invalid resolution.")
|
|
||||||
if samplerate in sample_modes:
|
if samplerate in sample_modes:
|
||||||
self.samplerate = sample_modes[samplerate]
|
self.samplerate = sample_modes[samplerate]
|
||||||
else:
|
else:
|
||||||
raise Exception("Invalid sample rate.")
|
raise Exception("Invalid sample rate.")
|
||||||
|
|
||||||
# First try; configure beginning from 0x20
|
# Configure all registers in +-2g mode
|
||||||
config0 = smbus2.i2c_msg.write(self.addr, [0x20,(sample_modes[samplerate]<<4)|0xF]) # Initialise in low power mode
|
c0 = smbus2.i2c_msg.write(self.addr, [0x20,(sample_modes[samplerate]<<4)|0xF]) # Initialise in low power mode
|
||||||
config1 = smbus2.i2c_msg.write(self.addr, [0xC1,0x00,0x00,0x00|self.resolution,0x00,0x00,0x00])
|
c1 = smbus2.i2c_msg.write(self.addr, [0x21,0x0A])
|
||||||
config2 = smbus2.i2c_msg.write(self.addr, [0xB2,0x00,0x00]) # Configure 0x32 with MSB = 1 to increment
|
c2 = smbus2.i2c_msg.write(self.addr, [0x22,0x40])
|
||||||
config3 = smbus2.i2c_msg.write(self.addr, [0x30,0x00]) # Configure 0x30
|
c3 = smbus2.i2c_msg.write(self.addr, [0x23,0x00])
|
||||||
config4 = smbus2.i2c_msg.write(self.addr, [0x24,0x00]) # Configure 0x24 again
|
c4 = smbus2.i2c_msg.write(self.addr, [0x24,0x0A])
|
||||||
self.i2c.i2c_rdwr(config0, config1, config2, config3, config4)
|
c5 = smbus2.i2c_msg.write(self.addr, [0x25,0x20])
|
||||||
|
c6 = smbus2.i2c_msg.write(self.addr, [0x2E,0x00])
|
||||||
|
c7 = smbus2.i2c_msg.write(self.addr, [0x30,0x95])
|
||||||
|
c8 = smbus2.i2c_msg.write(self.addr, [0x32,0x16])
|
||||||
|
c9 = smbus2.i2c_msg.write(self.addr, [0x33,0x03])
|
||||||
|
c10 = smbus2.i2c_msg.write(self.addr, [0x34,0x3F])
|
||||||
|
c11 = smbus2.i2c_msg.write(self.addr, [0x36,0x4A])
|
||||||
|
c12 = smbus2.i2c_msg.write(self.addr, [0x24,0x0A]) # Configure 0x24 again
|
||||||
|
self.i2c.i2c_rdwr(c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12)
|
||||||
|
|
||||||
|
def resetint1(self) -> bool:
|
||||||
|
'''Read INT1_SRC to reset it after an interrupt event.'''
|
||||||
|
int1_src_loc = smbus2.i2c_msg.write(self.addr, [0x31])
|
||||||
|
read_int1_src = smbus2.i2c_msg.read(self.addr, 1)
|
||||||
|
self.i2c.i2c_rdwr(int1_src_loc,read_int1_src)
|
||||||
|
if read_int1_src.bug[0] != None:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def resetint2(self) -> bool:
|
||||||
|
'''Read INT2_SRC to reset it after an interrupt event.'''
|
||||||
|
int2_src_loc = smbus2.i2c_msg.write(self.addr, [0x35])
|
||||||
|
read_int2_src = smbus2.i2c_msg.read(self.addr, 1)
|
||||||
|
self.i2c.i2c_rdwr(int2_src_loc,read_int2_src)
|
||||||
|
if read_int2_src.bug[0] != None:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def readAll(self) -> list:
|
def readAll(self) -> list:
|
||||||
'''Read acceleration data from all axes. Returns values as a list [X,Y,Z].'''
|
'''Read acceleration data from all axes. Returns values as a list [X,Y,Z].'''
|
||||||
|
|
34
main.py
34
main.py
|
@ -1,5 +1,8 @@
|
||||||
from lis3dh import *
|
from lis3dh import *
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import signal
|
||||||
|
import sys
|
||||||
|
import RPi.GPIO as GPIO
|
||||||
|
|
||||||
print("Raspberry Pi Zero W, up and running!")
|
print("Raspberry Pi Zero W, up and running!")
|
||||||
bus = smbus2.SMBus(1)
|
bus = smbus2.SMBus(1)
|
||||||
|
@ -7,15 +10,28 @@ accel = lis3dh(bus,2,1)
|
||||||
|
|
||||||
print("LIS3DH initiated successfully!")
|
print("LIS3DH initiated successfully!")
|
||||||
|
|
||||||
now = datetime.now()
|
INT1 = 18
|
||||||
date_time = now.strftime("%d_%m_%Y_%H_%M_%S")
|
INT2 = 17
|
||||||
name = "output_"+date_time+".txt"
|
|
||||||
f = open(name,"x")
|
def signal_handler(sig,frame):
|
||||||
print("X","Y","Z", file=f)
|
GPIO.cleanup()
|
||||||
f.close()
|
sys.exit(0)
|
||||||
|
|
||||||
|
def int1_callback(channel):
|
||||||
|
# do something here
|
||||||
|
|
||||||
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
|
||||||
|
# # Data logging
|
||||||
|
# now = datetime.now()
|
||||||
|
# date_time = now.strftime("%d_%m_%Y_%H_%M_%S")
|
||||||
|
# name = "output_"+date_time+".txt"
|
||||||
|
# f = open(name,"x")
|
||||||
|
# print("X","Y","Z", file=f)
|
||||||
|
# f.close()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
[X,Y,Z] = accel.readAll()
|
[X,Y,Z] = accel.readAll()
|
||||||
with open(name,"a") as f:
|
print("X: ",X,"\tY: ",Y,"\t Z: ",Z,"\n")
|
||||||
print("X: ",X,"\tY: ",Y,"\t Z: ",Z,"\n")
|
# with open(name,"a") as f:
|
||||||
print(X,Y,Z, file=f)
|
# print(X,Y,Z, file=f)
|
14
untitled.m
Normal file
14
untitled.m
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
A = importdata('output_01_03_2022_14_35_09.txt');
|
||||||
|
X = A.data(:,1);
|
||||||
|
Y = A.data(:,2);
|
||||||
|
Z = A.data(:,3);
|
||||||
|
|
||||||
|
time = linspace(0,length(X)*0.1,length(X));
|
||||||
|
|
||||||
|
figure
|
||||||
|
plot(time,X,time,Y,time,Z)
|
||||||
|
|
||||||
|
sum = sqrt(X.^2+Y.^2+Z.^2);
|
||||||
|
|
||||||
|
figure
|
||||||
|
plot(time,sum)
|
Loading…
Reference in a new issue