From f7da767fbb9091d3ee12e63c8e3421d5f645870a Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 19:51:43 +0000 Subject: [PATCH 01/35] Testing basic functionality --- lis3dh.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 12 ++++++++- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 lis3dh.py diff --git a/lis3dh.py b/lis3dh.py new file mode 100644 index 0000000..f5ab202 --- /dev/null +++ b/lis3dh.py @@ -0,0 +1,76 @@ +"""Library for interacting with LIS3DH triple-axis accelerometer.""" + +from importlib.resources import read_text +from tabnanny import check +import smbus2 +from time import sleep + +# Register addresses +_CTRL_REG1 = bytes([0x20]) +_CTRL_REG2 = bytes([0x21]) +_CTRL_REG3 = bytes([0x22]) +_CTRL_REG4 = bytes([0x23]) +_CTRL_REG5 = bytes([0x24]) +_CTRL_REG6 = bytes([0x25]) +_REF_REG = bytes([0x26]) +_STATUS_REG = bytes([0x27]) +_OUT_X_L = bytes([0x28]) +_OUT_X_H = bytes([0x29]) +_OUT_Y_L = bytes([0x2A]) +_OUT_Y_H = bytes([0x2B]) +_OUT_Z_L = bytes([0x2C]) +_OUT_Z_H = bytes([0x2D]) +_INT1_CFG = bytes([0x30]) +_INT1_SRC = bytes([0x31]) +_INT1_THS = bytes([0x32]) +_INT1_DURATION = bytes([0x33]) +_INT2_CFG = bytes([0x34]) +_INT2_SRC = bytes([0x35]) +_INT2_THS = bytes([0x36]) +_INT2_DURATION = bytes([0x37]) +_CLICK_CFG = bytes([0x38]) +# Config flags +SAMPLERATE_1HZ = bytes([0x17]) +SAMPLERATE_10HZ = bytes([0x27]) +SAMPLERATE_25HZ = bytes([0x37]) +HP_DISABLE = bytes([0x00]) +CTRL_REG3_V = bytes([0x40]) +CTRL_REG4_V = bytes([0x00]) # sensitivity set to 2g +CTRL_REG5_V = bytes([0x08]) +INT1_THS_V = bytes([0x16]) # free-fall threshold at 350 mg +INT1_DURATION_V = bytes([0x03]) +INT1_CFG_V = bytes([0x95]) +EMPTY = bytes([0x00]) + +class lis3dh: + def __init__(self, i2cBus, i2cAddress=0x18, samplerate=SAMPLERATE_1HZ): + sleep(0.005) + self.i2c = i2cBus + self.addr = i2cAddress + self.samplerate = samplerate + i2cBus.pec = True # enable smbus2 Packet Error Checking + + #First try; configure beginning from 0x20 with MSB = 1 to increment + config1 = smbus2.i2c_msg.write(self.addr, [0xC0,0x1F,0x00,0x00,0x00,0x00,0x00,0x00]) + config2 = smbus2.i2c_msg.write(self.addr, [0xB2,0x00,0x00]) #Configure 0x32 with MSB = 1 to increment + config3 = smbus2.i2c_msg.write(self.addr, [0x30,0x00]) #Configure 0x30 + config4 = smbus2.i2c_msg.write(self.addr, [0x24,0x00]) #Configure 0x24 again + self.i2c.i2c_rdwr(config1, config2, config3, config4) + + def readAll(self) -> list: + check_status = smbus2.i2c_msg.write(self.addr, [0x27]) + read_axis = smbus2.i2c_msg.read(self.addr, 1) + prepare_x = smbus2.i2c_msg.write(self.addr, [0x28]) + prepare_y = smbus2.i2c_msg.write(self.addr, [0x2A]) + prepare_z = smbus2.i2c_msg.write(self.addr, [0x2C]) + read_status = smbus2.i2c_msg.read(self.addr, 1) + status = self.i2c.i2c_rdwr(check_status, read_status) + while status.buf[0] != 0b1111: + sleep(0.00001) + x = self.i2c.i2c_rdwr(prepare_x, read_axis) + y = self.i2c.i2c_rdwr(prepare_y, read_axis) + z = self.i2c.i2c_rdwr(prepare_z, read_axis) + X = int.from_bytes(x.buf[0]) + Y = int.from_bytes(y.buf[0]) + Z = int.from_bytes(z.buf[0]) + return [X,Y,Z] \ No newline at end of file diff --git a/main.py b/main.py index cc8d835..4a2a346 100644 --- a/main.py +++ b/main.py @@ -1 +1,11 @@ -print("Raspberry Pi Zero W, up and running!") \ No newline at end of file +import lis3dh.py + +print("Raspberry Pi Zero W, up and running!") + +accel = lis3dh() + +print("LIS3DH initiated successfully!") + +while True: + [X,Y,Z] = accel.readAll() + print("X: ",X,"\tY: ",Y,"\t Z: ",Z,"\n") \ No newline at end of file From 9ac478555050bd0b5dff430a9a65e3472ad1adf7 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 22:34:27 +0000 Subject: [PATCH 02/35] Fixed import --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 4a2a346..2d7a35c 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -import lis3dh.py +from . import lis3dh.py print("Raspberry Pi Zero W, up and running!") From 4f7c676e53f4108c167066efdec646547c9e503e Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 22:36:47 +0000 Subject: [PATCH 03/35] Maybe not fixed per se --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 2d7a35c..0a6e931 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -from . import lis3dh.py +from lis3dh import * print("Raspberry Pi Zero W, up and running!") From 735cac77685c33315ecd2ca8496837f8bbd3dfb0 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 22:56:00 +0000 Subject: [PATCH 04/35] Update lis3dh.py --- lis3dh.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index f5ab202..309716e 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -65,8 +65,9 @@ class lis3dh: prepare_z = smbus2.i2c_msg.write(self.addr, [0x2C]) read_status = smbus2.i2c_msg.read(self.addr, 1) status = self.i2c.i2c_rdwr(check_status, read_status) - while status.buf[0] != 0b1111: - sleep(0.00001) + #while status.buf[0] != 0b1111: + # sleep(0.00001) + print(status) x = self.i2c.i2c_rdwr(prepare_x, read_axis) y = self.i2c.i2c_rdwr(prepare_y, read_axis) z = self.i2c.i2c_rdwr(prepare_z, read_axis) From c881e589fb79b72381f6c1b1b2f61bdc36f7be02 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 22:59:11 +0000 Subject: [PATCH 05/35] Update lis3dh.py --- lis3dh.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index 309716e..e4b0abe 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -59,18 +59,17 @@ class lis3dh: def readAll(self) -> list: check_status = smbus2.i2c_msg.write(self.addr, [0x27]) - read_axis = smbus2.i2c_msg.read(self.addr, 1) + x = smbus2.i2c_msg.read(self.addr, 1) + y = smbus2.i2c_msg.read(self.addr, 1) + z = smbus2.i2c_msg.read(self.addr, 1) prepare_x = smbus2.i2c_msg.write(self.addr, [0x28]) prepare_y = smbus2.i2c_msg.write(self.addr, [0x2A]) prepare_z = smbus2.i2c_msg.write(self.addr, [0x2C]) read_status = smbus2.i2c_msg.read(self.addr, 1) - status = self.i2c.i2c_rdwr(check_status, read_status) + self.i2c.i2c_rdwr(check_status, read_status) #while status.buf[0] != 0b1111: # sleep(0.00001) - print(status) - x = self.i2c.i2c_rdwr(prepare_x, read_axis) - y = self.i2c.i2c_rdwr(prepare_y, read_axis) - z = self.i2c.i2c_rdwr(prepare_z, read_axis) + self.i2c.i2c_rdwr(prepare_x, x, prepare_y, y, prepare_z, z) X = int.from_bytes(x.buf[0]) Y = int.from_bytes(y.buf[0]) Z = int.from_bytes(z.buf[0]) From 39b971a3c6863c2c57cd57638ac111429a117981 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:01:43 +0000 Subject: [PATCH 06/35] Update lis3dh.py --- lis3dh.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index e4b0abe..d2d1274 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -65,8 +65,8 @@ class lis3dh: prepare_x = smbus2.i2c_msg.write(self.addr, [0x28]) prepare_y = smbus2.i2c_msg.write(self.addr, [0x2A]) prepare_z = smbus2.i2c_msg.write(self.addr, [0x2C]) - read_status = smbus2.i2c_msg.read(self.addr, 1) - self.i2c.i2c_rdwr(check_status, read_status) + status = smbus2.i2c_msg.read(self.addr, 1) + self.i2c.i2c_rdwr(check_status, status) #while status.buf[0] != 0b1111: # sleep(0.00001) self.i2c.i2c_rdwr(prepare_x, x, prepare_y, y, prepare_z, z) From 53b5f4cc19f9e20c5ca9d970a7c3c4053ce3306b Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:02:44 +0000 Subject: [PATCH 07/35] Update lis3dh.py --- lis3dh.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index d2d1274..beb1e5e 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -69,8 +69,8 @@ class lis3dh: self.i2c.i2c_rdwr(check_status, status) #while status.buf[0] != 0b1111: # sleep(0.00001) - self.i2c.i2c_rdwr(prepare_x, x, prepare_y, y, prepare_z, z) + self.i2c.i2c_rdwr(prepare_x, x)#, prepare_y, y, prepare_z, z) X = int.from_bytes(x.buf[0]) - Y = int.from_bytes(y.buf[0]) - Z = int.from_bytes(z.buf[0]) - return [X,Y,Z] \ No newline at end of file + #Y = int.from_bytes(y.buf[0]) + #Z = int.from_bytes(z.buf[0]) + return [X] \ No newline at end of file From 222bd2fe3ab20f5b1612e51cba74c569c30e2561 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:03:15 +0000 Subject: [PATCH 08/35] Update lis3dh.py --- lis3dh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lis3dh.py b/lis3dh.py index beb1e5e..4f46334 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -70,7 +70,7 @@ class lis3dh: #while status.buf[0] != 0b1111: # sleep(0.00001) self.i2c.i2c_rdwr(prepare_x, x)#, prepare_y, y, prepare_z, z) - X = int.from_bytes(x.buf[0]) + X = int.from_bytes(x.buf[0],"big") #Y = int.from_bytes(y.buf[0]) #Z = int.from_bytes(z.buf[0]) return [X] \ No newline at end of file From 7091f70b4677306d230f8015591eed5ca544d61d Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:04:22 +0000 Subject: [PATCH 09/35] Update lis3dh.py --- lis3dh.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index 4f46334..51df0c7 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -69,8 +69,10 @@ class lis3dh: self.i2c.i2c_rdwr(check_status, status) #while status.buf[0] != 0b1111: # sleep(0.00001) - self.i2c.i2c_rdwr(prepare_x, x)#, prepare_y, y, prepare_z, z) + self.i2c.i2c_rdwr(prepare_x, x) + self.i2c.i2c_rdwr(prepare_y, y) + self.i2c.i2c_rdwr(prepare_z, z) X = int.from_bytes(x.buf[0],"big") - #Y = int.from_bytes(y.buf[0]) - #Z = int.from_bytes(z.buf[0]) - return [X] \ No newline at end of file + Y = int.from_bytes(y.buf[0],"big") + Z = int.from_bytes(z.buf[0],"big") + return [X,Y,Z] \ No newline at end of file From 36a7d87bb0623762f90a68e531d1d74d81cab1d6 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:05:48 +0000 Subject: [PATCH 10/35] Update main.py --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 0a6e931..2309f88 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,8 @@ from lis3dh import * print("Raspberry Pi Zero W, up and running!") - -accel = lis3dh() +bus = smbus2.SMBus(1) +accel = lis3dh(bus) print("LIS3DH initiated successfully!") From e32e5f93b3f263be0ef63bcf4d3f1095436f2e47 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:08:41 +0000 Subject: [PATCH 11/35] Update lis3dh.py --- lis3dh.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index 51df0c7..4a458a4 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -67,12 +67,14 @@ class lis3dh: prepare_z = smbus2.i2c_msg.write(self.addr, [0x2C]) status = smbus2.i2c_msg.read(self.addr, 1) self.i2c.i2c_rdwr(check_status, status) - #while status.buf[0] != 0b1111: - # sleep(0.00001) - self.i2c.i2c_rdwr(prepare_x, x) - self.i2c.i2c_rdwr(prepare_y, y) - self.i2c.i2c_rdwr(prepare_z, z) - X = int.from_bytes(x.buf[0],"big") - Y = int.from_bytes(y.buf[0],"big") - Z = int.from_bytes(z.buf[0],"big") - return [X,Y,Z] \ No newline at end of file + while status.buf[0] != 0b1111: + sleep(1) + self.i2c.i2c_rdwr(check_status, status) + if status.buf[0] == 0b1111: + self.i2c.i2c_rdwr(prepare_x, x) + self.i2c.i2c_rdwr(prepare_y, y) + self.i2c.i2c_rdwr(prepare_z, z) + X = int.from_bytes(x.buf[0],"big") + Y = int.from_bytes(y.buf[0],"big") + Z = int.from_bytes(z.buf[0],"big") + return [X,Y,Z] \ No newline at end of file From 4314109f75d0ba9bbf7b7af2ab93d241439de3a7 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:22:36 +0000 Subject: [PATCH 12/35] Update lis3dh.py --- lis3dh.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lis3dh.py b/lis3dh.py index 4a458a4..34da1b8 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -1,6 +1,7 @@ """Library for interacting with LIS3DH triple-axis accelerometer.""" from importlib.resources import read_text +from re import S from tabnanny import check import smbus2 from time import sleep @@ -56,6 +57,10 @@ class lis3dh: config3 = smbus2.i2c_msg.write(self.addr, [0x30,0x00]) #Configure 0x30 config4 = smbus2.i2c_msg.write(self.addr, [0x24,0x00]) #Configure 0x24 again self.i2c.i2c_rdwr(config1, config2, config3, config4) + check_CTRL_REG1 = smbus2.i2c_msg.write(self.addr, [0x20]) + ctrl_reg1 = smbus2.i2c_msg.read(self.addr, 1) + self.i2c.i2c_rdwr(check_CTRL_REG1, ctrl_reg1) + print(ctrl_reg1.buf[0]) def readAll(self) -> list: check_status = smbus2.i2c_msg.write(self.addr, [0x27]) From 062b180b1b0a76c1d056d381457df9e9642a4f7f Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:24:29 +0000 Subject: [PATCH 13/35] Update lis3dh.py --- lis3dh.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index 34da1b8..b610e71 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -52,11 +52,12 @@ class lis3dh: i2cBus.pec = True # enable smbus2 Packet Error Checking #First try; configure beginning from 0x20 with MSB = 1 to increment - config1 = smbus2.i2c_msg.write(self.addr, [0xC0,0x1F,0x00,0x00,0x00,0x00,0x00,0x00]) + config0 = smbus2.i2c_msg.write(self.addr, [0x20,0x1F]) + config1 = smbus2.i2c_msg.write(self.addr, [0xC1,0x00,0x00,0x00,0x00,0x00,0x00]) config2 = smbus2.i2c_msg.write(self.addr, [0xB2,0x00,0x00]) #Configure 0x32 with MSB = 1 to increment config3 = smbus2.i2c_msg.write(self.addr, [0x30,0x00]) #Configure 0x30 config4 = smbus2.i2c_msg.write(self.addr, [0x24,0x00]) #Configure 0x24 again - self.i2c.i2c_rdwr(config1, config2, config3, config4) + self.i2c.i2c_rdwr(config0, config1, config2, config3, config4) check_CTRL_REG1 = smbus2.i2c_msg.write(self.addr, [0x20]) ctrl_reg1 = smbus2.i2c_msg.read(self.addr, 1) self.i2c.i2c_rdwr(check_CTRL_REG1, ctrl_reg1) From 3bc60950149da3f6ba0d3517ca995f546a5b9deb Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:34:23 +0000 Subject: [PATCH 14/35] Update lis3dh.py --- lis3dh.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lis3dh.py b/lis3dh.py index b610e71..6bc8b7b 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -74,6 +74,7 @@ class lis3dh: status = smbus2.i2c_msg.read(self.addr, 1) self.i2c.i2c_rdwr(check_status, status) while status.buf[0] != 0b1111: + print(status.buf[0], "\n") sleep(1) self.i2c.i2c_rdwr(check_status, status) if status.buf[0] == 0b1111: From e534469ab57867f198bb6b8142a5765264d1b9c5 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:36:07 +0000 Subject: [PATCH 15/35] Update lis3dh.py --- lis3dh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lis3dh.py b/lis3dh.py index 6bc8b7b..753e518 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -73,7 +73,7 @@ class lis3dh: prepare_z = smbus2.i2c_msg.write(self.addr, [0x2C]) status = smbus2.i2c_msg.read(self.addr, 1) self.i2c.i2c_rdwr(check_status, status) - while status.buf[0] != 0b1111: + while (status.buf[0] & 0b1111) != 0b1111: print(status.buf[0], "\n") sleep(1) self.i2c.i2c_rdwr(check_status, status) From c3403bd47d8ef12a437cca7f6c7785e85e62c23d Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:38:45 +0000 Subject: [PATCH 16/35] Update lis3dh.py --- lis3dh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lis3dh.py b/lis3dh.py index 753e518..ad7f503 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -73,7 +73,7 @@ class lis3dh: prepare_z = smbus2.i2c_msg.write(self.addr, [0x2C]) status = smbus2.i2c_msg.read(self.addr, 1) self.i2c.i2c_rdwr(check_status, status) - while (status.buf[0] & 0b1111) != 0b1111: + while (int.from_bytes(status.buf[0],"big") & 0b1111) != 0b1111: print(status.buf[0], "\n") sleep(1) self.i2c.i2c_rdwr(check_status, status) From 6c0806a37731c01dba1555953a267aaf0017f365 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:40:28 +0000 Subject: [PATCH 17/35] Update lis3dh.py --- lis3dh.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lis3dh.py b/lis3dh.py index ad7f503..54e7c20 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -77,11 +77,12 @@ class lis3dh: print(status.buf[0], "\n") sleep(1) self.i2c.i2c_rdwr(check_status, status) - if status.buf[0] == 0b1111: + if (int.from_bytes(status.buf[0],"big") & 0b1111) == 0b1111: self.i2c.i2c_rdwr(prepare_x, x) self.i2c.i2c_rdwr(prepare_y, y) self.i2c.i2c_rdwr(prepare_z, z) X = int.from_bytes(x.buf[0],"big") Y = int.from_bytes(y.buf[0],"big") Z = int.from_bytes(z.buf[0],"big") + print(X,Y,Z) return [X,Y,Z] \ No newline at end of file From 756b07f52ad6cdb53dd8557eed7689f62bd49c03 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:41:31 +0000 Subject: [PATCH 18/35] Update lis3dh.py --- lis3dh.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lis3dh.py b/lis3dh.py index 54e7c20..39b4199 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -78,6 +78,7 @@ class lis3dh: sleep(1) self.i2c.i2c_rdwr(check_status, status) if (int.from_bytes(status.buf[0],"big") & 0b1111) == 0b1111: + print("Status: ",status.buf[0], "\n") self.i2c.i2c_rdwr(prepare_x, x) self.i2c.i2c_rdwr(prepare_y, y) self.i2c.i2c_rdwr(prepare_z, z) From 1aa1c426d4b6cd4cc22c287a1ef8878aba279290 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:47:48 +0000 Subject: [PATCH 19/35] Update lis3dh.py --- lis3dh.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index 39b4199..cc897b7 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -68,14 +68,14 @@ class lis3dh: x = smbus2.i2c_msg.read(self.addr, 1) y = smbus2.i2c_msg.read(self.addr, 1) z = smbus2.i2c_msg.read(self.addr, 1) - prepare_x = smbus2.i2c_msg.write(self.addr, [0x28]) - prepare_y = smbus2.i2c_msg.write(self.addr, [0x2A]) - prepare_z = smbus2.i2c_msg.write(self.addr, [0x2C]) + prepare_x = smbus2.i2c_msg.write(self.addr, [0x29]) + prepare_y = smbus2.i2c_msg.write(self.addr, [0x2B]) + prepare_z = smbus2.i2c_msg.write(self.addr, [0x2D]) status = smbus2.i2c_msg.read(self.addr, 1) self.i2c.i2c_rdwr(check_status, status) while (int.from_bytes(status.buf[0],"big") & 0b1111) != 0b1111: print(status.buf[0], "\n") - sleep(1) + sleep(0.01) self.i2c.i2c_rdwr(check_status, status) if (int.from_bytes(status.buf[0],"big") & 0b1111) == 0b1111: print("Status: ",status.buf[0], "\n") @@ -85,5 +85,4 @@ class lis3dh: X = int.from_bytes(x.buf[0],"big") Y = int.from_bytes(y.buf[0],"big") Z = int.from_bytes(z.buf[0],"big") - print(X,Y,Z) return [X,Y,Z] \ No newline at end of file From d21f79a2970a73bc12c95b84760b0344246fcc3d Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:48:24 +0000 Subject: [PATCH 20/35] Update lis3dh.py --- lis3dh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lis3dh.py b/lis3dh.py index cc897b7..c98f0e5 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -78,7 +78,7 @@ class lis3dh: sleep(0.01) self.i2c.i2c_rdwr(check_status, status) if (int.from_bytes(status.buf[0],"big") & 0b1111) == 0b1111: - print("Status: ",status.buf[0], "\n") + #print("Status: ",status.buf[0], "\n") self.i2c.i2c_rdwr(prepare_x, x) self.i2c.i2c_rdwr(prepare_y, y) self.i2c.i2c_rdwr(prepare_z, z) From eae0e52cddea319ff9469009f54cfefd93a3a982 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 23:48:47 +0000 Subject: [PATCH 21/35] Update lis3dh.py --- lis3dh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lis3dh.py b/lis3dh.py index c98f0e5..8a3b9be 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -74,7 +74,7 @@ class lis3dh: status = smbus2.i2c_msg.read(self.addr, 1) self.i2c.i2c_rdwr(check_status, status) while (int.from_bytes(status.buf[0],"big") & 0b1111) != 0b1111: - print(status.buf[0], "\n") + #print(status.buf[0], "\n") sleep(0.01) self.i2c.i2c_rdwr(check_status, status) if (int.from_bytes(status.buf[0],"big") & 0b1111) == 0b1111: From 898cd451dbe2ca7652e09b2ff45d39bab3012f1b Mon Sep 17 00:00:00 2001 From: Kacper Date: Sun, 27 Feb 2022 01:13:05 +0000 Subject: [PATCH 22/35] Basic reading almost complete --- lis3dh.py | 52 +++++++++++++++++++++++++++++++++++----------------- main.py | 2 +- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index 8a3b9be..1d5eb7a 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -44,26 +44,41 @@ INT1_CFG_V = bytes([0x95]) EMPTY = bytes([0x00]) class lis3dh: - def __init__(self, i2cBus, i2cAddress=0x18, samplerate=SAMPLERATE_1HZ): + def __init__(self, i2cBus, resolution=2, samplerate=10, i2cAddress=0x18): sleep(0.005) self.i2c = i2cBus self.addr = i2cAddress self.samplerate = samplerate i2cBus.pec = True # enable smbus2 Packet Error Checking + res_modes = { + 2: 0b00, 4: 0b01, + 8: 0b10, 16: 0b11 + } + sample_modes = { + 0:0x0, 1:0x1, 10:0x2, 25:0x3, 50:0x4, + 100:0x5, 200:0x6, 400:0x7 + } - #First try; configure beginning from 0x20 with MSB = 1 to increment - config0 = smbus2.i2c_msg.write(self.addr, [0x20,0x1F]) - config1 = smbus2.i2c_msg.write(self.addr, [0xC1,0x00,0x00,0x00,0x00,0x00,0x00]) - config2 = smbus2.i2c_msg.write(self.addr, [0xB2,0x00,0x00]) #Configure 0x32 with MSB = 1 to increment - config3 = smbus2.i2c_msg.write(self.addr, [0x30,0x00]) #Configure 0x30 - config4 = smbus2.i2c_msg.write(self.addr, [0x24,0x00]) #Configure 0x24 again + # 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: + self.samplerate = sample_modes[samplerate] + else: + raise Exception("Invalid sample rate.") + + # First try; configure beginning from 0x20 + config0 = 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]) + config2 = smbus2.i2c_msg.write(self.addr, [0xB2,0x00,0x00]) # Configure 0x32 with MSB = 1 to increment + config3 = smbus2.i2c_msg.write(self.addr, [0x30,0x00]) # Configure 0x30 + config4 = smbus2.i2c_msg.write(self.addr, [0x24,0x00]) # Configure 0x24 again self.i2c.i2c_rdwr(config0, config1, config2, config3, config4) - check_CTRL_REG1 = smbus2.i2c_msg.write(self.addr, [0x20]) - ctrl_reg1 = smbus2.i2c_msg.read(self.addr, 1) - self.i2c.i2c_rdwr(check_CTRL_REG1, ctrl_reg1) - print(ctrl_reg1.buf[0]) def readAll(self) -> list: + '''Read acceleration data from all axes. Returns values as a list [X,Y,Z].''' check_status = smbus2.i2c_msg.write(self.addr, [0x27]) x = smbus2.i2c_msg.read(self.addr, 1) y = smbus2.i2c_msg.read(self.addr, 1) @@ -73,16 +88,19 @@ class lis3dh: prepare_z = smbus2.i2c_msg.write(self.addr, [0x2D]) status = smbus2.i2c_msg.read(self.addr, 1) self.i2c.i2c_rdwr(check_status, status) - while (int.from_bytes(status.buf[0],"big") & 0b1111) != 0b1111: - #print(status.buf[0], "\n") - sleep(0.01) + + while status.buf[0][0] & 0b1111 != 0b1111: # Wait for data to be available + sleep(0.001) self.i2c.i2c_rdwr(check_status, status) - if (int.from_bytes(status.buf[0],"big") & 0b1111) == 0b1111: - #print("Status: ",status.buf[0], "\n") + + if status.buf[0][0] & 0b1111 == 0b1111: # If data is available, read self.i2c.i2c_rdwr(prepare_x, x) self.i2c.i2c_rdwr(prepare_y, y) self.i2c.i2c_rdwr(prepare_z, z) X = int.from_bytes(x.buf[0],"big") Y = int.from_bytes(y.buf[0],"big") Z = int.from_bytes(z.buf[0],"big") - return [X,Y,Z] \ No newline at end of file + # TODO: Need to format this so that it returns values in g + return [X,Y,Z] + else: + return None # Should never get here lol \ No newline at end of file diff --git a/main.py b/main.py index 2309f88..5c02b6f 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ from lis3dh import * print("Raspberry Pi Zero W, up and running!") bus = smbus2.SMBus(1) -accel = lis3dh(bus) +accel = lis3dh(bus,10) print("LIS3DH initiated successfully!") From 65bddab7f591d3863b7383c9b07f3a44b2625bb8 Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 09:05:45 +0000 Subject: [PATCH 23/35] Update lis3dh.py --- lis3dh.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index 1d5eb7a..5a3cf44 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -100,7 +100,14 @@ class lis3dh: X = int.from_bytes(x.buf[0],"big") Y = int.from_bytes(y.buf[0],"big") Z = int.from_bytes(z.buf[0],"big") - # TODO: Need to format this so that it returns values in g - return [X,Y,Z] + new_values = [] + for D in [X,Y,Z]: + MSB = D >> 7 + if MSB == 1: + res = -128 + (D - 128) + else: + res = D + new_values.append(res) + return new_values else: return None # Should never get here lol \ No newline at end of file From f9082650ea7267268030d542ead3e90e2830ec0b Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 09:06:53 +0000 Subject: [PATCH 24/35] Update main.py --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 5c02b6f..ba97ab1 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ from lis3dh import * print("Raspberry Pi Zero W, up and running!") bus = smbus2.SMBus(1) -accel = lis3dh(bus,10) +accel = lis3dh(bus,2,10) print("LIS3DH initiated successfully!") From 2468ac039f7dbd7c28f478317b050ad3f4bc2146 Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 09:10:01 +0000 Subject: [PATCH 25/35] Update lis3dh.py --- lis3dh.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index 5a3cf44..8865725 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -104,9 +104,9 @@ class lis3dh: for D in [X,Y,Z]: MSB = D >> 7 if MSB == 1: - res = -128 + (D - 128) + res = (-128 + (D - 128))/(128*self.resolution) else: - res = D + res = D/(128*self.resolution) new_values.append(res) return new_values else: From 3ff2e4eecebdd6e47e0feb669e3d98cb41920794 Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 09:12:01 +0000 Subject: [PATCH 26/35] Update lis3dh.py --- lis3dh.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index 8865725..770083e 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -104,9 +104,9 @@ class lis3dh: for D in [X,Y,Z]: MSB = D >> 7 if MSB == 1: - res = (-128 + (D - 128))/(128*self.resolution) + res = (-128 + (D - 128))*self.resolution/128 else: - res = D/(128*self.resolution) + res = (D*self.resolution)/128 new_values.append(res) return new_values else: From 84d18b2764f6ad71335490962d67cc3651451a6f Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 09:18:04 +0000 Subject: [PATCH 27/35] Update main.py --- main.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index ba97ab1..d7f785d 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,12 @@ accel = lis3dh(bus,2,10) print("LIS3DH initiated successfully!") +f = open("output.txt","x") +print("X","Y","Z", file=f) +f.close() + while True: [X,Y,Z] = accel.readAll() - print("X: ",X,"\tY: ",Y,"\t Z: ",Z,"\n") \ No newline at end of file + with open("output.txt","a") as f: + print("X: ",X,"\tY: ",Y,"\t Z: ",Z,"\n") + print(X,Y,Z, file=f) \ No newline at end of file From 123e092d4c3f11ab55ffaf9da1035ea2a9dce300 Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 14:26:58 +0000 Subject: [PATCH 28/35] Update main.py --- main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index d7f785d..87e8e4d 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ from lis3dh import * +from datetime import datetime print("Raspberry Pi Zero W, up and running!") bus = smbus2.SMBus(1) @@ -6,7 +7,8 @@ accel = lis3dh(bus,2,10) print("LIS3DH initiated successfully!") -f = open("output.txt","x") +name = "output_"+datetime.now()+".txt" +f = open(name,"x") print("X","Y","Z", file=f) f.close() From b8d21cf0e7ee25785a492b640e9a9f89b0331e50 Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 14:29:35 +0000 Subject: [PATCH 29/35] Update main.py --- main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 87e8e4d..a13041a 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,9 @@ accel = lis3dh(bus,2,10) print("LIS3DH initiated successfully!") -name = "output_"+datetime.now()+".txt" +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() From ac5f883981bfcbad989db0d270b14dccf19ff5cb Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 14:30:22 +0000 Subject: [PATCH 30/35] Update main.py --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index a13041a..a6cc3f5 100644 --- a/main.py +++ b/main.py @@ -16,6 +16,6 @@ f.close() while True: [X,Y,Z] = accel.readAll() - with open("output.txt","a") as f: + with open(name,"a") as f: print("X: ",X,"\tY: ",Y,"\t Z: ",Z,"\n") print(X,Y,Z, file=f) \ No newline at end of file From 4c10e5de06989f7c822bdcc09e4ab875b8759509 Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 14:33:09 +0000 Subject: [PATCH 31/35] Tweaking --- lis3dh.py | 3 +++ main.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lis3dh.py b/lis3dh.py index 770083e..9e3c606 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -100,6 +100,8 @@ class lis3dh: X = int.from_bytes(x.buf[0],"big") Y = int.from_bytes(y.buf[0],"big") Z = int.from_bytes(z.buf[0],"big") + + # Convert from binary 2s complement to useful data new_values = [] for D in [X,Y,Z]: MSB = D >> 7 @@ -108,6 +110,7 @@ class lis3dh: else: res = (D*self.resolution)/128 new_values.append(res) + return new_values else: return None # Should never get here lol \ No newline at end of file diff --git a/main.py b/main.py index a6cc3f5..756a94d 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ from datetime import datetime print("Raspberry Pi Zero W, up and running!") bus = smbus2.SMBus(1) -accel = lis3dh(bus,2,10) +accel = lis3dh(bus,2,1) print("LIS3DH initiated successfully!") From 3e7a3cfad6a097e34468a239438d1aef39752d38 Mon Sep 17 00:00:00 2001 From: kmn219 <58340321+kmn219@users.noreply.github.com> Date: Tue, 1 Mar 2022 14:37:20 +0000 Subject: [PATCH 32/35] Step measurement --- output_01_03_2022_14_35_09.txt | 587 +++++++++++++++++++++++++++++++++ 1 file changed, 587 insertions(+) create mode 100644 output_01_03_2022_14_35_09.txt diff --git a/output_01_03_2022_14_35_09.txt b/output_01_03_2022_14_35_09.txt new file mode 100644 index 0000000..ad2d544 --- /dev/null +++ b/output_01_03_2022_14_35_09.txt @@ -0,0 +1,587 @@ +X Y Z +0.09375 0.03125 1.03125 +0.03125 -0.015625 1.03125 +0.046875 0.015625 1.015625 +0.0625 0.015625 1.03125 +0.0625 0.0 1.03125 +0.125 0.0 1.0 +0.046875 0.015625 1.03125 +0.046875 0.015625 1.046875 +0.0625 0.015625 1.03125 +0.046875 0.015625 0.90625 +0.046875 0.015625 0.890625 +0.0625 0.03125 0.984375 +0.046875 0.015625 1.03125 +0.046875 0.015625 1.03125 +0.0625 0.015625 1.03125 +0.0625 0.015625 1.03125 +0.03125 0.03125 1.03125 +0.0625 0.015625 1.015625 +0.03125 0.0 1.03125 +0.046875 0.015625 1.03125 +0.03125 -0.015625 1.078125 +0.0625 0.015625 1.03125 +0.046875 0.015625 1.03125 +0.0625 0.015625 1.03125 +0.0625 0.03125 0.984375 +0.03125 -0.015625 1.03125 +0.0625 0.0 1.015625 +0.046875 0.0 1.015625 +0.0625 0.03125 1.0 +0.0625 0.015625 1.03125 +0.03125 0.0 1.03125 +0.046875 0.03125 0.953125 +0.015625 0.015625 1.03125 +0.0625 0.015625 1.03125 +0.046875 0.015625 1.03125 +0.046875 0.0 1.0625 +0.03125 0.015625 1.03125 +0.046875 0.015625 1.03125 +0.03125 0.015625 1.03125 +0.015625 -0.015625 1.078125 +0.015625 0.03125 1.03125 +0.046875 0.015625 1.03125 +0.09375 -0.015625 0.9375 +0.046875 0.015625 1.03125 +0.0625 0.015625 1.03125 +0.046875 0.0 1.015625 +0.046875 0.015625 1.015625 +0.046875 0.0 1.015625 +0.046875 0.015625 1.015625 +0.046875 0.015625 1.0 +0.0625 -0.03125 1.078125 +0.0625 0.03125 1.03125 +0.046875 0.015625 1.0625 +0.0625 0.015625 0.953125 +0.0625 0.015625 1.03125 +0.0625 0.015625 1.015625 +0.0625 0.015625 1.0 +0.078125 0.015625 0.9375 +0.0625 0.015625 1.03125 +0.046875 0.0 1.03125 +0.0625 0.015625 1.046875 +0.046875 -0.015625 1.03125 +0.0625 0.015625 1.046875 +0.0625 0.0 1.046875 +0.046875 0.0 1.0 +0.046875 0.03125 1.03125 +0.03125 0.0 1.03125 +0.046875 0.0 0.9375 +0.0625 0.015625 1.109375 +0.046875 0.0 1.03125 +0.0625 0.015625 1.046875 +0.046875 0.0 1.03125 +0.046875 0.015625 1.109375 +0.0625 0.015625 1.015625 +0.03125 -0.015625 1.03125 +0.015625 0.0 0.921875 +0.0625 0.03125 1.015625 +0.0625 0.015625 1.03125 +0.0625 0.015625 1.03125 +0.046875 0.0 1.03125 +0.078125 0.015625 1.03125 +0.0625 0.0 1.046875 +0.046875 0.015625 1.046875 +0.03125 -0.015625 1.078125 +0.046875 0.0 1.03125 +0.046875 0.015625 1.03125 +0.046875 -0.015625 0.953125 +0.046875 0.015625 1.015625 +0.0625 0.0 1.03125 +0.046875 0.0 0.953125 +0.046875 0.015625 1.03125 +0.046875 0.0 1.046875 +0.046875 0.015625 1.03125 +0.046875 0.015625 1.046875 +0.0625 -0.421875 0.96875 +-0.15625 0.09375 1.0625 +-0.03125 0.109375 1.171875 +0.0 0.046875 1.171875 +0.09375 0.078125 1.0625 +0.109375 0.234375 1.09375 +0.1875 0.3125 1.125 +0.234375 0.296875 1.0 +0.328125 0.390625 0.90625 +0.3125 0.46875 0.71875 +0.34375 0.546875 0.59375 +0.375 0.703125 0.609375 +0.25 0.765625 0.453125 +0.234375 0.75 0.359375 +0.140625 0.65625 0.265625 +0.0625 0.703125 0.328125 +0.0 0.859375 0.390625 +0.0625 0.875 0.359375 +0.21875 0.921875 0.4375 +0.21875 0.984375 0.703125 +0.390625 0.75 0.78125 +0.5 0.671875 0.796875 +0.375 0.640625 0.875 +0.25 0.5625 0.84375 +0.234375 0.578125 0.796875 +0.234375 0.703125 0.828125 +0.328125 0.609375 0.78125 +0.25 0.609375 0.71875 +0.109375 0.9375 0.734375 +0.015625 0.84375 0.828125 +0.140625 0.625 0.84375 +0.265625 0.578125 0.78125 +0.390625 0.390625 0.8125 +0.359375 0.578125 0.796875 +0.34375 0.703125 0.84375 +0.359375 0.375 0.765625 +0.515625 -0.40625 0.53125 +0.40625 -0.71875 0.59375 +0.390625 -0.390625 0.828125 +0.375 -0.640625 0.671875 +0.375 -1.328125 0.421875 +0.25 -0.984375 0.234375 +0.09375 -0.921875 0.046875 +0.140625 -1.0625 -0.078125 +-0.015625 -0.890625 -0.265625 +0.015625 -0.9375 -0.09375 +-0.34375 -0.828125 -0.0625 +-0.296875 -0.84375 -0.4375 +-0.1875 -0.9375 -0.5 +-0.265625 -0.78125 -0.578125 +-0.234375 -0.796875 -0.453125 +-0.4375 -0.796875 -0.546875 +-0.3125 -0.859375 -0.5 +-0.25 -1.109375 -0.46875 +0.0 -0.890625 -0.421875 +-0.296875 -0.828125 -2.0 +-0.25 -0.578125 -0.59375 +-0.3125 -0.703125 -0.75 +-0.15625 -0.390625 -0.5625 +-0.015625 -0.296875 -0.703125 +0.15625 -0.46875 -0.796875 +0.203125 -0.515625 -0.71875 +0.40625 -0.578125 -0.484375 +0.140625 -0.265625 -0.359375 +0.078125 -0.703125 -0.609375 +0.015625 -0.625 -0.4375 +-0.234375 -0.78125 -0.265625 +-0.25 -0.828125 -0.453125 +-0.15625 -0.859375 -0.359375 +-0.03125 -0.796875 -0.046875 +0.09375 -1.109375 0.5625 +-0.125 -0.6875 0.46875 +-0.140625 -0.546875 0.84375 +-0.21875 -0.4375 0.875 +-0.359375 -0.390625 0.734375 +-0.40625 -0.34375 0.84375 +-0.421875 -0.34375 0.71875 +-0.421875 -0.46875 0.890625 +-0.4375 -0.421875 0.703125 +-0.21875 -0.5625 1.078125 +-0.21875 -0.625 0.6875 +-0.484375 -0.59375 0.375 +-0.703125 -0.703125 0.4375 +-0.40625 -0.609375 0.375 +-0.546875 -0.796875 0.65625 +-0.6875 -0.9375 0.578125 +-0.484375 -0.609375 0.4375 +-0.4375 -0.703125 0.421875 +-0.5 -0.609375 0.421875 +-0.4375 -0.5 0.5625 +-0.40625 -0.390625 0.234375 +-0.234375 -0.25 0.3125 +-0.328125 -1.1875 0.59375 +-0.265625 -0.9375 0.796875 +0.046875 -0.921875 0.625 +-0.015625 -0.984375 0.75 +0.03125 -0.734375 0.65625 +0.03125 -0.8125 0.640625 +0.03125 -0.765625 0.75 +0.0 -0.875 0.703125 +-0.015625 -0.640625 0.59375 +-0.046875 -0.734375 0.609375 +-0.078125 -0.6875 0.640625 +0.046875 -0.546875 0.640625 +-0.09375 -0.65625 0.765625 +0.0625 -0.875 1.0 +-0.140625 -0.78125 0.9375 +0.15625 -0.875 0.59375 +-0.015625 -0.71875 0.796875 +0.125 -0.609375 0.53125 +0.140625 -0.5625 0.5625 +0.09375 -0.71875 0.71875 +0.0625 -1.09375 0.75 +-0.09375 -1.0 0.796875 +0.15625 -0.703125 0.5 +0.1875 -0.5 0.53125 +0.125 -0.671875 0.59375 +-0.046875 -0.8125 0.71875 +0.3125 -1.0 0.671875 +0.28125 -0.5625 0.40625 +0.296875 -0.8125 0.484375 +0.28125 -0.890625 0.5625 +0.265625 -0.75 0.609375 +0.234375 -0.8125 0.578125 +0.25 -0.828125 0.5 +0.28125 -0.828125 0.609375 +0.3125 -1.015625 0.5625 +0.21875 -0.75 0.6875 +0.09375 -0.671875 0.484375 +0.0625 -0.640625 0.609375 +-0.09375 -0.78125 0.65625 +-0.03125 -0.828125 0.59375 +-0.046875 -0.65625 0.71875 +-0.0625 -0.6875 0.65625 +-0.109375 -0.671875 0.578125 +-0.234375 -0.703125 0.515625 +-0.328125 -0.75 0.53125 +-0.375 -0.78125 0.515625 +-0.40625 -0.828125 0.40625 +-0.359375 -0.703125 0.5 +-0.34375 -0.6875 0.4375 +-0.3125 -0.671875 0.59375 +-0.3125 -0.671875 0.625 +-0.3125 -0.640625 0.640625 +-0.421875 -0.75 0.6875 +-0.3125 -0.59375 0.75 +-0.34375 -0.71875 0.6875 +-0.296875 -0.5 0.796875 +-0.28125 -0.609375 0.6875 +-0.25 -0.546875 0.765625 +-0.1875 -0.546875 0.75 +-0.203125 -0.59375 0.765625 +-0.265625 -0.6875 0.8125 +-0.125 -0.59375 0.625 +-0.171875 -0.5 0.828125 +-0.171875 -0.59375 0.8125 +-0.09375 -0.59375 0.796875 +-0.140625 -0.765625 0.765625 +-0.09375 -0.859375 0.75 +-0.015625 -0.75 0.625 +-0.078125 -0.71875 0.671875 +-0.046875 -0.765625 0.625 +0.0 -0.640625 0.65625 +-0.03125 -0.796875 0.640625 +0.015625 -0.625 0.765625 +-0.0625 -0.625 0.828125 +0.015625 -0.515625 0.90625 +0.0625 -0.546875 0.8125 +0.0 -0.53125 0.984375 +0.03125 -0.671875 0.859375 +0.015625 -0.5 0.828125 +0.078125 -0.5625 0.90625 +0.078125 -0.578125 0.96875 +0.0625 -0.46875 0.828125 +0.015625 -0.515625 0.890625 +0.0625 -0.53125 0.796875 +0.109375 -0.4375 0.890625 +0.125 -0.578125 0.84375 +0.4375 -0.703125 0.84375 +0.28125 -0.671875 0.78125 +-0.125 -0.765625 0.71875 +-0.046875 -0.796875 0.84375 +0.265625 -0.625 0.609375 +0.359375 -0.546875 0.5 +0.5625 -0.640625 0.59375 +0.25 -0.734375 0.6875 +0.28125 -0.875 0.546875 +0.265625 -0.84375 0.578125 +0.328125 -0.78125 0.515625 +0.359375 -0.71875 0.671875 +0.59375 -0.890625 0.71875 +0.609375 -0.421875 0.625 +0.5625 -0.59375 0.5 +0.5625 -0.625 0.5 +0.484375 -0.5625 0.6875 +0.34375 -0.78125 0.65625 +0.3125 -0.796875 0.78125 +0.484375 -0.828125 0.625 +0.40625 -0.859375 0.671875 +0.359375 -1.03125 0.390625 +0.328125 -0.890625 0.859375 +0.421875 -0.765625 0.78125 +0.421875 -0.46875 0.203125 +0.1875 -0.515625 0.65625 +0.359375 -0.734375 0.453125 +0.203125 -0.71875 0.640625 +0.09375 -0.828125 0.65625 +0.21875 -1.03125 0.84375 +0.046875 -0.890625 0.71875 +-0.1875 -0.734375 0.84375 +-0.125 -0.796875 0.9375 +0.0 -1.0625 0.703125 +0.15625 -0.890625 0.6875 +0.0625 -0.765625 0.71875 +-0.046875 -0.71875 0.46875 +-0.125 -0.578125 0.515625 +-0.25 -0.515625 0.578125 +-0.21875 -0.5 0.703125 +-0.234375 -0.5 0.8125 +-0.25 -0.5 0.953125 +-0.109375 -0.765625 0.75 +-0.03125 -0.6875 0.8125 +0.0 -0.65625 0.828125 +-0.015625 -0.671875 0.8125 +-0.015625 -0.9375 0.53125 +-0.125 -0.828125 0.546875 +-0.046875 -0.828125 0.421875 +-0.109375 -0.90625 0.3125 +-0.1875 -0.828125 0.40625 +-0.390625 -0.890625 0.5 +-0.25 -0.890625 0.390625 +-0.078125 -0.609375 0.53125 +0.015625 -0.59375 0.46875 +-0.09375 -0.515625 0.484375 +-0.15625 -0.90625 0.515625 +-0.046875 -1.0 0.375 +-0.046875 -1.265625 0.234375 +-0.015625 -0.921875 0.296875 +-0.109375 -0.78125 0.234375 +0.078125 -1.0 0.28125 +0.0 -1.046875 0.078125 +0.09375 -0.90625 0.203125 +0.0 -0.953125 0.171875 +0.015625 -0.953125 0.203125 +-0.0625 -1.109375 0.078125 +0.15625 -0.984375 0.1875 +0.328125 -0.890625 -0.09375 +0.453125 -0.796875 0.1875 +0.453125 -0.78125 -0.1875 +0.578125 -0.71875 -0.03125 +1.15625 -1.375 -1.578125 +0.59375 -0.40625 -0.125 +1.046875 -0.375 -0.046875 +1.015625 -0.359375 0.0625 +1.078125 -0.078125 0.1875 +1.078125 -0.0625 0.140625 +1.015625 0.078125 0.109375 +0.921875 -0.046875 0.078125 +1.0 -0.046875 -0.015625 +1.0 -0.015625 0.109375 +1.046875 0.15625 0.09375 +0.953125 0.109375 0.125 +0.953125 0.078125 0.125 +0.984375 0.078125 0.109375 +1.015625 0.078125 0.15625 +0.890625 0.109375 0.0625 +0.890625 0.234375 0.1875 +0.953125 0.015625 0.09375 +1.03125 0.0625 0.1875 +0.984375 -0.046875 0.109375 +0.984375 -0.0625 0.1875 +0.890625 0.15625 -0.046875 +1.015625 -0.046875 0.40625 +0.984375 -0.015625 0.296875 +0.796875 0.078125 0.3125 +0.875 0.015625 0.390625 +0.859375 -0.078125 0.5 +1.265625 0.21875 0.46875 +0.9375 0.109375 -0.0625 +0.90625 0.078125 0.21875 +0.90625 0.015625 0.171875 +0.921875 0.015625 0.28125 +1.09375 -0.0625 0.1875 +1.25 0.0625 -0.0625 +0.75 0.40625 0.140625 +0.953125 0.0625 0.125 +0.8125 0.046875 0.1875 +0.90625 0.0625 0.3125 +0.796875 0.078125 0.390625 +1.3125 0.078125 0.125 +0.984375 0.296875 -0.09375 +0.96875 0.0625 0.03125 +1.03125 0.109375 0.09375 +0.984375 0.140625 0.0625 +0.9375 0.109375 0.09375 +0.953125 0.09375 0.078125 +0.984375 0.171875 0.046875 +1.03125 0.140625 0.015625 +0.953125 0.171875 -0.015625 +0.984375 0.234375 -0.09375 +0.78125 0.328125 -0.03125 +1.046875 0.234375 0.03125 +1.578125 0.25 0.578125 +0.546875 -0.0625 0.234375 +0.859375 0.109375 0.203125 +0.859375 0.28125 0.140625 +0.78125 0.25 0.03125 +1.609375 0.234375 0.109375 +1.5 -0.21875 0.21875 +0.6875 0.140625 0.125 +1.015625 0.078125 0.15625 +0.921875 0.125 -0.015625 +0.8125 0.296875 -0.1875 +1.21875 0.203125 0.34375 +1.25 0.0625 0.375 +0.671875 -0.125 0.390625 +1.0 0.125 0.265625 +0.921875 0.015625 0.28125 +0.921875 0.203125 0.03125 +0.9375 0.0625 0.046875 +0.921875 0.140625 0.0625 +0.984375 0.15625 0.09375 +0.9375 0.078125 0.015625 +0.9375 0.09375 0.015625 +0.984375 0.078125 0.03125 +0.984375 0.046875 0.0625 +0.859375 0.1875 -0.015625 +0.9375 0.046875 0.4375 +0.953125 0.171875 0.234375 +0.765625 0.046875 0.234375 +0.875 0.09375 0.421875 +0.96875 0.0625 0.4375 +1.171875 0.34375 -0.09375 +0.84375 0.375 -0.125 +0.921875 -0.109375 0.125 +0.828125 0.140625 0.171875 +1.0 -0.015625 0.4375 +1.0625 -0.046875 0.046875 +0.953125 0.53125 -0.0625 +1.078125 0.03125 -0.0625 +0.671875 -0.015625 0.15625 +0.890625 -0.09375 0.375 +1.25 0.34375 0.03125 +0.78125 0.109375 0.15625 +0.75 0.203125 -0.015625 +0.953125 0.1875 0.140625 +0.96875 0.171875 0.109375 +0.953125 0.09375 0.140625 +0.96875 0.1875 0.15625 +0.96875 0.078125 0.09375 +0.9375 0.03125 0.078125 +0.9375 0.25 0.140625 +0.90625 0.25 0.078125 +0.875 0.265625 0.0625 +0.96875 0.421875 0.0 +0.96875 0.109375 0.359375 +0.96875 0.046875 0.4375 +0.8125 0.234375 0.21875 +0.765625 0.296875 0.0625 +0.859375 0.5 0.109375 +1.59375 -0.015625 -0.203125 +1.515625 -0.109375 0.359375 +0.71875 0.125 0.15625 +0.984375 0.1875 0.109375 +1.0 0.140625 0.078125 +0.8125 0.28125 -0.171875 +1.296875 0.4375 0.234375 +1.046875 0.171875 0.484375 +0.890625 -0.09375 0.4375 +0.71875 0.140625 0.234375 +0.9375 0.25 0.25 +0.828125 0.15625 0.234375 +1.421875 0.5 0.296875 +0.65625 0.0625 0.15625 +1.015625 0.328125 0.109375 +0.875 0.265625 0.171875 +0.953125 0.1875 0.21875 +0.984375 0.296875 0.125 +0.9375 0.125 0.28125 +0.96875 0.328125 0.15625 +0.921875 0.21875 0.109375 +0.953125 0.3125 0.125 +0.921875 0.25 0.15625 +0.9375 0.265625 0.171875 +0.953125 0.234375 0.1875 +0.921875 0.25 0.1875 +0.9375 0.25 0.1875 +0.9375 0.234375 0.1875 +0.9375 0.25 0.1875 +0.9375 0.265625 0.171875 +0.921875 0.25 0.109375 +0.9375 0.234375 0.203125 +1.015625 0.265625 0.171875 +0.9375 0.234375 0.09375 +1.0 0.21875 0.171875 +0.953125 0.140625 0.15625 +0.984375 0.140625 0.078125 +0.984375 0.015625 0.046875 +0.90625 -0.03125 0.09375 +0.984375 -0.015625 0.0 +1.0 -0.0625 -0.03125 +1.0 -0.21875 -0.21875 +0.78125 -0.359375 -0.15625 +0.953125 -0.578125 -0.328125 +0.90625 -0.484375 -0.359375 +0.9375 -1.21875 -1.59375 +0.765625 -0.5625 -0.515625 +0.671875 -0.53125 -0.8125 +0.625 -0.5625 -0.515625 +0.90625 -0.796875 0.515625 +0.4375 -0.4375 -0.359375 +0.609375 -0.65625 -0.15625 +0.484375 -0.734375 0.359375 +0.734375 -0.90625 0.203125 +0.28125 -1.171875 0.1875 +-0.015625 -0.984375 0.0625 +0.046875 -1.046875 0.09375 +0.40625 -1.109375 0.171875 +0.578125 -0.875 -0.078125 +0.3125 -0.84375 -0.09375 +0.078125 -0.75 0.0 +0.078125 -0.734375 0.125 +-0.1875 -0.890625 0.53125 +-0.46875 -0.46875 1.234375 +-0.359375 -0.203125 1.140625 +-0.359375 -0.046875 0.859375 +-0.078125 -0.484375 1.171875 +-0.203125 0.125 0.890625 +0.015625 -0.03125 1.125 +-0.21875 -0.171875 0.203125 +0.0 -0.0625 0.65625 +0.03125 -0.015625 0.96875 +-0.015625 -0.015625 1.03125 +0.03125 0.0 1.015625 +0.03125 0.0 1.015625 +0.03125 0.0 0.9375 +0.046875 0.0 1.03125 +0.046875 0.0 1.015625 +0.03125 0.015625 1.03125 +0.046875 0.0 1.046875 +0.046875 0.015625 1.015625 +0.046875 0.0 0.984375 +0.046875 0.015625 1.015625 +0.046875 0.0 1.03125 +0.046875 0.015625 1.03125 +0.0 0.0 0.984375 +0.046875 0.015625 1.03125 +0.03125 0.015625 1.03125 +0.046875 0.0 1.03125 +0.046875 0.0 1.015625 +0.046875 0.0 1.03125 +0.046875 0.0 1.03125 +0.03125 0.0 1.03125 +0.03125 0.015625 1.03125 +-0.03125 0.015625 0.96875 +0.046875 0.0 1.03125 +0.03125 0.0 1.03125 +0.046875 0.015625 1.03125 +0.03125 0.015625 1.015625 +0.03125 0.0 1.03125 +0.03125 -0.015625 1.03125 +0.03125 0.015625 1.015625 +0.046875 0.0 1.03125 +-0.0625 0.0 0.984375 +0.03125 0.0 1.03125 +0.03125 0.015625 1.015625 +0.015625 0.0 1.046875 +0.046875 0.0 1.046875 +0.046875 0.0 1.03125 +0.046875 -0.015625 1.015625 +0.03125 0.015625 1.03125 +0.046875 0.015625 1.03125 +0.03125 0.0 0.921875 +0.046875 0.015625 1.03125 +0.03125 0.0 1.03125 +0.03125 0.0 1.015625 +0.03125 -0.015625 1.03125 +0.03125 0.0 1.03125 +0.03125 0.0 1.015625 +0.03125 0.046875 1.109375 +0.046875 0.015625 1.046875 +0.03125 0.03125 1.03125 +0.046875 0.03125 1.015625 +0.03125 0.0 1.03125 +0.046875 0.015625 1.015625 +-0.015625 0.0 1.03125 +0.046875 0.015625 1.03125 +0.03125 0.0 0.953125 +0.03125 0.015625 1.046875 +0.046875 0.015625 1.03125 +0.03125 0.0 0.90625 +0.03125 0.0 1.015625 From 298e7660b77b602886849faacef22ad868e94b40 Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 16:06:12 +0000 Subject: [PATCH 33/35] Programmed in interrupts --- lis3dh.py | 54 +++++++++++++++++++++++++++++++++++++----------------- main.py | 34 +++++++++++++++++++++++++--------- untitled.m | 14 ++++++++++++++ 3 files changed, 76 insertions(+), 26 deletions(-) create mode 100644 untitled.m diff --git a/lis3dh.py b/lis3dh.py index 9e3c606..c96813d 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -44,38 +44,58 @@ INT1_CFG_V = bytes([0x95]) EMPTY = bytes([0x00]) class lis3dh: - def __init__(self, i2cBus, resolution=2, samplerate=10, i2cAddress=0x18): + def __init__(self, i2cBus, samplerate=10, i2cAddress=0x18): sleep(0.005) self.i2c = i2cBus self.addr = i2cAddress self.samplerate = samplerate i2cBus.pec = True # enable smbus2 Packet Error Checking - res_modes = { - 2: 0b00, 4: 0b01, - 8: 0b10, 16: 0b11 - } sample_modes = { 0:0x0, 1:0x1, 10:0x2, 25:0x3, 50:0x4, 100:0x5, 200:0x6, 400:0x7 } # 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: self.samplerate = sample_modes[samplerate] else: raise Exception("Invalid sample rate.") - # First try; configure beginning from 0x20 - config0 = 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]) - config2 = smbus2.i2c_msg.write(self.addr, [0xB2,0x00,0x00]) # Configure 0x32 with MSB = 1 to increment - config3 = smbus2.i2c_msg.write(self.addr, [0x30,0x00]) # Configure 0x30 - config4 = smbus2.i2c_msg.write(self.addr, [0x24,0x00]) # Configure 0x24 again - self.i2c.i2c_rdwr(config0, config1, config2, config3, config4) + # Configure all registers in +-2g mode + c0 = smbus2.i2c_msg.write(self.addr, [0x20,(sample_modes[samplerate]<<4)|0xF]) # Initialise in low power mode + c1 = smbus2.i2c_msg.write(self.addr, [0x21,0x0A]) + c2 = smbus2.i2c_msg.write(self.addr, [0x22,0x40]) + c3 = smbus2.i2c_msg.write(self.addr, [0x23,0x00]) + c4 = smbus2.i2c_msg.write(self.addr, [0x24,0x0A]) + 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: '''Read acceleration data from all axes. Returns values as a list [X,Y,Z].''' @@ -110,7 +130,7 @@ class lis3dh: else: res = (D*self.resolution)/128 new_values.append(res) - + return new_values else: return None # Should never get here lol \ No newline at end of file diff --git a/main.py b/main.py index 756a94d..d2f4e19 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,8 @@ from lis3dh import * from datetime import datetime +import signal +import sys +import RPi.GPIO as GPIO print("Raspberry Pi Zero W, up and running!") bus = smbus2.SMBus(1) @@ -7,15 +10,28 @@ accel = lis3dh(bus,2,1) print("LIS3DH initiated successfully!") -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() +INT1 = 18 +INT2 = 17 + +def signal_handler(sig,frame): + GPIO.cleanup() + 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: [X,Y,Z] = accel.readAll() - with open(name,"a") as f: - print("X: ",X,"\tY: ",Y,"\t Z: ",Z,"\n") - print(X,Y,Z, file=f) \ No newline at end of file + print("X: ",X,"\tY: ",Y,"\t Z: ",Z,"\n") + # with open(name,"a") as f: + # print(X,Y,Z, file=f) \ No newline at end of file diff --git a/untitled.m b/untitled.m new file mode 100644 index 0000000..dfa92a9 --- /dev/null +++ b/untitled.m @@ -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) \ No newline at end of file From eb0e171a881971a7a751cc0b14152202d5e7bee3 Mon Sep 17 00:00:00 2001 From: Kacper Date: Tue, 1 Mar 2022 16:08:21 +0000 Subject: [PATCH 34/35] Ignored MATLAB files --- .gitignore | 5 ++++- untitled.m | 14 -------------- 2 files changed, 4 insertions(+), 15 deletions(-) delete mode 100644 untitled.m diff --git a/.gitignore b/.gitignore index 89e509d..fd1b15a 100644 --- a/.gitignore +++ b/.gitignore @@ -152,4 +152,7 @@ cython_debug/ #.idea/ # VSCode -.vscode/ \ No newline at end of file +.vscode/ + +# Matlab files +.m \ No newline at end of file diff --git a/untitled.m b/untitled.m deleted file mode 100644 index dfa92a9..0000000 --- a/untitled.m +++ /dev/null @@ -1,14 +0,0 @@ -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) \ No newline at end of file From ba771c7606d8f4b769006c5bd1e1ae53bc4c3ab9 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Tue, 1 Mar 2022 16:42:12 +0000 Subject: [PATCH 35/35] Prepare for Merge --- .gitignore | 5 +- main.py | 38 +-- output_01_03_2022_14_35_09.txt | 587 --------------------------------- 3 files changed, 2 insertions(+), 628 deletions(-) delete mode 100644 output_01_03_2022_14_35_09.txt diff --git a/.gitignore b/.gitignore index fd1b15a..89e509d 100644 --- a/.gitignore +++ b/.gitignore @@ -152,7 +152,4 @@ cython_debug/ #.idea/ # VSCode -.vscode/ - -# Matlab files -.m \ No newline at end of file +.vscode/ \ No newline at end of file diff --git a/main.py b/main.py index d2f4e19..cc8d835 100644 --- a/main.py +++ b/main.py @@ -1,37 +1 @@ -from lis3dh import * -from datetime import datetime -import signal -import sys -import RPi.GPIO as GPIO - -print("Raspberry Pi Zero W, up and running!") -bus = smbus2.SMBus(1) -accel = lis3dh(bus,2,1) - -print("LIS3DH initiated successfully!") - -INT1 = 18 -INT2 = 17 - -def signal_handler(sig,frame): - GPIO.cleanup() - 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: - [X,Y,Z] = accel.readAll() - print("X: ",X,"\tY: ",Y,"\t Z: ",Z,"\n") - # with open(name,"a") as f: - # print(X,Y,Z, file=f) \ No newline at end of file +print("Raspberry Pi Zero W, up and running!") \ No newline at end of file diff --git a/output_01_03_2022_14_35_09.txt b/output_01_03_2022_14_35_09.txt deleted file mode 100644 index ad2d544..0000000 --- a/output_01_03_2022_14_35_09.txt +++ /dev/null @@ -1,587 +0,0 @@ -X Y Z -0.09375 0.03125 1.03125 -0.03125 -0.015625 1.03125 -0.046875 0.015625 1.015625 -0.0625 0.015625 1.03125 -0.0625 0.0 1.03125 -0.125 0.0 1.0 -0.046875 0.015625 1.03125 -0.046875 0.015625 1.046875 -0.0625 0.015625 1.03125 -0.046875 0.015625 0.90625 -0.046875 0.015625 0.890625 -0.0625 0.03125 0.984375 -0.046875 0.015625 1.03125 -0.046875 0.015625 1.03125 -0.0625 0.015625 1.03125 -0.0625 0.015625 1.03125 -0.03125 0.03125 1.03125 -0.0625 0.015625 1.015625 -0.03125 0.0 1.03125 -0.046875 0.015625 1.03125 -0.03125 -0.015625 1.078125 -0.0625 0.015625 1.03125 -0.046875 0.015625 1.03125 -0.0625 0.015625 1.03125 -0.0625 0.03125 0.984375 -0.03125 -0.015625 1.03125 -0.0625 0.0 1.015625 -0.046875 0.0 1.015625 -0.0625 0.03125 1.0 -0.0625 0.015625 1.03125 -0.03125 0.0 1.03125 -0.046875 0.03125 0.953125 -0.015625 0.015625 1.03125 -0.0625 0.015625 1.03125 -0.046875 0.015625 1.03125 -0.046875 0.0 1.0625 -0.03125 0.015625 1.03125 -0.046875 0.015625 1.03125 -0.03125 0.015625 1.03125 -0.015625 -0.015625 1.078125 -0.015625 0.03125 1.03125 -0.046875 0.015625 1.03125 -0.09375 -0.015625 0.9375 -0.046875 0.015625 1.03125 -0.0625 0.015625 1.03125 -0.046875 0.0 1.015625 -0.046875 0.015625 1.015625 -0.046875 0.0 1.015625 -0.046875 0.015625 1.015625 -0.046875 0.015625 1.0 -0.0625 -0.03125 1.078125 -0.0625 0.03125 1.03125 -0.046875 0.015625 1.0625 -0.0625 0.015625 0.953125 -0.0625 0.015625 1.03125 -0.0625 0.015625 1.015625 -0.0625 0.015625 1.0 -0.078125 0.015625 0.9375 -0.0625 0.015625 1.03125 -0.046875 0.0 1.03125 -0.0625 0.015625 1.046875 -0.046875 -0.015625 1.03125 -0.0625 0.015625 1.046875 -0.0625 0.0 1.046875 -0.046875 0.0 1.0 -0.046875 0.03125 1.03125 -0.03125 0.0 1.03125 -0.046875 0.0 0.9375 -0.0625 0.015625 1.109375 -0.046875 0.0 1.03125 -0.0625 0.015625 1.046875 -0.046875 0.0 1.03125 -0.046875 0.015625 1.109375 -0.0625 0.015625 1.015625 -0.03125 -0.015625 1.03125 -0.015625 0.0 0.921875 -0.0625 0.03125 1.015625 -0.0625 0.015625 1.03125 -0.0625 0.015625 1.03125 -0.046875 0.0 1.03125 -0.078125 0.015625 1.03125 -0.0625 0.0 1.046875 -0.046875 0.015625 1.046875 -0.03125 -0.015625 1.078125 -0.046875 0.0 1.03125 -0.046875 0.015625 1.03125 -0.046875 -0.015625 0.953125 -0.046875 0.015625 1.015625 -0.0625 0.0 1.03125 -0.046875 0.0 0.953125 -0.046875 0.015625 1.03125 -0.046875 0.0 1.046875 -0.046875 0.015625 1.03125 -0.046875 0.015625 1.046875 -0.0625 -0.421875 0.96875 --0.15625 0.09375 1.0625 --0.03125 0.109375 1.171875 -0.0 0.046875 1.171875 -0.09375 0.078125 1.0625 -0.109375 0.234375 1.09375 -0.1875 0.3125 1.125 -0.234375 0.296875 1.0 -0.328125 0.390625 0.90625 -0.3125 0.46875 0.71875 -0.34375 0.546875 0.59375 -0.375 0.703125 0.609375 -0.25 0.765625 0.453125 -0.234375 0.75 0.359375 -0.140625 0.65625 0.265625 -0.0625 0.703125 0.328125 -0.0 0.859375 0.390625 -0.0625 0.875 0.359375 -0.21875 0.921875 0.4375 -0.21875 0.984375 0.703125 -0.390625 0.75 0.78125 -0.5 0.671875 0.796875 -0.375 0.640625 0.875 -0.25 0.5625 0.84375 -0.234375 0.578125 0.796875 -0.234375 0.703125 0.828125 -0.328125 0.609375 0.78125 -0.25 0.609375 0.71875 -0.109375 0.9375 0.734375 -0.015625 0.84375 0.828125 -0.140625 0.625 0.84375 -0.265625 0.578125 0.78125 -0.390625 0.390625 0.8125 -0.359375 0.578125 0.796875 -0.34375 0.703125 0.84375 -0.359375 0.375 0.765625 -0.515625 -0.40625 0.53125 -0.40625 -0.71875 0.59375 -0.390625 -0.390625 0.828125 -0.375 -0.640625 0.671875 -0.375 -1.328125 0.421875 -0.25 -0.984375 0.234375 -0.09375 -0.921875 0.046875 -0.140625 -1.0625 -0.078125 --0.015625 -0.890625 -0.265625 -0.015625 -0.9375 -0.09375 --0.34375 -0.828125 -0.0625 --0.296875 -0.84375 -0.4375 --0.1875 -0.9375 -0.5 --0.265625 -0.78125 -0.578125 --0.234375 -0.796875 -0.453125 --0.4375 -0.796875 -0.546875 --0.3125 -0.859375 -0.5 --0.25 -1.109375 -0.46875 -0.0 -0.890625 -0.421875 --0.296875 -0.828125 -2.0 --0.25 -0.578125 -0.59375 --0.3125 -0.703125 -0.75 --0.15625 -0.390625 -0.5625 --0.015625 -0.296875 -0.703125 -0.15625 -0.46875 -0.796875 -0.203125 -0.515625 -0.71875 -0.40625 -0.578125 -0.484375 -0.140625 -0.265625 -0.359375 -0.078125 -0.703125 -0.609375 -0.015625 -0.625 -0.4375 --0.234375 -0.78125 -0.265625 --0.25 -0.828125 -0.453125 --0.15625 -0.859375 -0.359375 --0.03125 -0.796875 -0.046875 -0.09375 -1.109375 0.5625 --0.125 -0.6875 0.46875 --0.140625 -0.546875 0.84375 --0.21875 -0.4375 0.875 --0.359375 -0.390625 0.734375 --0.40625 -0.34375 0.84375 --0.421875 -0.34375 0.71875 --0.421875 -0.46875 0.890625 --0.4375 -0.421875 0.703125 --0.21875 -0.5625 1.078125 --0.21875 -0.625 0.6875 --0.484375 -0.59375 0.375 --0.703125 -0.703125 0.4375 --0.40625 -0.609375 0.375 --0.546875 -0.796875 0.65625 --0.6875 -0.9375 0.578125 --0.484375 -0.609375 0.4375 --0.4375 -0.703125 0.421875 --0.5 -0.609375 0.421875 --0.4375 -0.5 0.5625 --0.40625 -0.390625 0.234375 --0.234375 -0.25 0.3125 --0.328125 -1.1875 0.59375 --0.265625 -0.9375 0.796875 -0.046875 -0.921875 0.625 --0.015625 -0.984375 0.75 -0.03125 -0.734375 0.65625 -0.03125 -0.8125 0.640625 -0.03125 -0.765625 0.75 -0.0 -0.875 0.703125 --0.015625 -0.640625 0.59375 --0.046875 -0.734375 0.609375 --0.078125 -0.6875 0.640625 -0.046875 -0.546875 0.640625 --0.09375 -0.65625 0.765625 -0.0625 -0.875 1.0 --0.140625 -0.78125 0.9375 -0.15625 -0.875 0.59375 --0.015625 -0.71875 0.796875 -0.125 -0.609375 0.53125 -0.140625 -0.5625 0.5625 -0.09375 -0.71875 0.71875 -0.0625 -1.09375 0.75 --0.09375 -1.0 0.796875 -0.15625 -0.703125 0.5 -0.1875 -0.5 0.53125 -0.125 -0.671875 0.59375 --0.046875 -0.8125 0.71875 -0.3125 -1.0 0.671875 -0.28125 -0.5625 0.40625 -0.296875 -0.8125 0.484375 -0.28125 -0.890625 0.5625 -0.265625 -0.75 0.609375 -0.234375 -0.8125 0.578125 -0.25 -0.828125 0.5 -0.28125 -0.828125 0.609375 -0.3125 -1.015625 0.5625 -0.21875 -0.75 0.6875 -0.09375 -0.671875 0.484375 -0.0625 -0.640625 0.609375 --0.09375 -0.78125 0.65625 --0.03125 -0.828125 0.59375 --0.046875 -0.65625 0.71875 --0.0625 -0.6875 0.65625 --0.109375 -0.671875 0.578125 --0.234375 -0.703125 0.515625 --0.328125 -0.75 0.53125 --0.375 -0.78125 0.515625 --0.40625 -0.828125 0.40625 --0.359375 -0.703125 0.5 --0.34375 -0.6875 0.4375 --0.3125 -0.671875 0.59375 --0.3125 -0.671875 0.625 --0.3125 -0.640625 0.640625 --0.421875 -0.75 0.6875 --0.3125 -0.59375 0.75 --0.34375 -0.71875 0.6875 --0.296875 -0.5 0.796875 --0.28125 -0.609375 0.6875 --0.25 -0.546875 0.765625 --0.1875 -0.546875 0.75 --0.203125 -0.59375 0.765625 --0.265625 -0.6875 0.8125 --0.125 -0.59375 0.625 --0.171875 -0.5 0.828125 --0.171875 -0.59375 0.8125 --0.09375 -0.59375 0.796875 --0.140625 -0.765625 0.765625 --0.09375 -0.859375 0.75 --0.015625 -0.75 0.625 --0.078125 -0.71875 0.671875 --0.046875 -0.765625 0.625 -0.0 -0.640625 0.65625 --0.03125 -0.796875 0.640625 -0.015625 -0.625 0.765625 --0.0625 -0.625 0.828125 -0.015625 -0.515625 0.90625 -0.0625 -0.546875 0.8125 -0.0 -0.53125 0.984375 -0.03125 -0.671875 0.859375 -0.015625 -0.5 0.828125 -0.078125 -0.5625 0.90625 -0.078125 -0.578125 0.96875 -0.0625 -0.46875 0.828125 -0.015625 -0.515625 0.890625 -0.0625 -0.53125 0.796875 -0.109375 -0.4375 0.890625 -0.125 -0.578125 0.84375 -0.4375 -0.703125 0.84375 -0.28125 -0.671875 0.78125 --0.125 -0.765625 0.71875 --0.046875 -0.796875 0.84375 -0.265625 -0.625 0.609375 -0.359375 -0.546875 0.5 -0.5625 -0.640625 0.59375 -0.25 -0.734375 0.6875 -0.28125 -0.875 0.546875 -0.265625 -0.84375 0.578125 -0.328125 -0.78125 0.515625 -0.359375 -0.71875 0.671875 -0.59375 -0.890625 0.71875 -0.609375 -0.421875 0.625 -0.5625 -0.59375 0.5 -0.5625 -0.625 0.5 -0.484375 -0.5625 0.6875 -0.34375 -0.78125 0.65625 -0.3125 -0.796875 0.78125 -0.484375 -0.828125 0.625 -0.40625 -0.859375 0.671875 -0.359375 -1.03125 0.390625 -0.328125 -0.890625 0.859375 -0.421875 -0.765625 0.78125 -0.421875 -0.46875 0.203125 -0.1875 -0.515625 0.65625 -0.359375 -0.734375 0.453125 -0.203125 -0.71875 0.640625 -0.09375 -0.828125 0.65625 -0.21875 -1.03125 0.84375 -0.046875 -0.890625 0.71875 --0.1875 -0.734375 0.84375 --0.125 -0.796875 0.9375 -0.0 -1.0625 0.703125 -0.15625 -0.890625 0.6875 -0.0625 -0.765625 0.71875 --0.046875 -0.71875 0.46875 --0.125 -0.578125 0.515625 --0.25 -0.515625 0.578125 --0.21875 -0.5 0.703125 --0.234375 -0.5 0.8125 --0.25 -0.5 0.953125 --0.109375 -0.765625 0.75 --0.03125 -0.6875 0.8125 -0.0 -0.65625 0.828125 --0.015625 -0.671875 0.8125 --0.015625 -0.9375 0.53125 --0.125 -0.828125 0.546875 --0.046875 -0.828125 0.421875 --0.109375 -0.90625 0.3125 --0.1875 -0.828125 0.40625 --0.390625 -0.890625 0.5 --0.25 -0.890625 0.390625 --0.078125 -0.609375 0.53125 -0.015625 -0.59375 0.46875 --0.09375 -0.515625 0.484375 --0.15625 -0.90625 0.515625 --0.046875 -1.0 0.375 --0.046875 -1.265625 0.234375 --0.015625 -0.921875 0.296875 --0.109375 -0.78125 0.234375 -0.078125 -1.0 0.28125 -0.0 -1.046875 0.078125 -0.09375 -0.90625 0.203125 -0.0 -0.953125 0.171875 -0.015625 -0.953125 0.203125 --0.0625 -1.109375 0.078125 -0.15625 -0.984375 0.1875 -0.328125 -0.890625 -0.09375 -0.453125 -0.796875 0.1875 -0.453125 -0.78125 -0.1875 -0.578125 -0.71875 -0.03125 -1.15625 -1.375 -1.578125 -0.59375 -0.40625 -0.125 -1.046875 -0.375 -0.046875 -1.015625 -0.359375 0.0625 -1.078125 -0.078125 0.1875 -1.078125 -0.0625 0.140625 -1.015625 0.078125 0.109375 -0.921875 -0.046875 0.078125 -1.0 -0.046875 -0.015625 -1.0 -0.015625 0.109375 -1.046875 0.15625 0.09375 -0.953125 0.109375 0.125 -0.953125 0.078125 0.125 -0.984375 0.078125 0.109375 -1.015625 0.078125 0.15625 -0.890625 0.109375 0.0625 -0.890625 0.234375 0.1875 -0.953125 0.015625 0.09375 -1.03125 0.0625 0.1875 -0.984375 -0.046875 0.109375 -0.984375 -0.0625 0.1875 -0.890625 0.15625 -0.046875 -1.015625 -0.046875 0.40625 -0.984375 -0.015625 0.296875 -0.796875 0.078125 0.3125 -0.875 0.015625 0.390625 -0.859375 -0.078125 0.5 -1.265625 0.21875 0.46875 -0.9375 0.109375 -0.0625 -0.90625 0.078125 0.21875 -0.90625 0.015625 0.171875 -0.921875 0.015625 0.28125 -1.09375 -0.0625 0.1875 -1.25 0.0625 -0.0625 -0.75 0.40625 0.140625 -0.953125 0.0625 0.125 -0.8125 0.046875 0.1875 -0.90625 0.0625 0.3125 -0.796875 0.078125 0.390625 -1.3125 0.078125 0.125 -0.984375 0.296875 -0.09375 -0.96875 0.0625 0.03125 -1.03125 0.109375 0.09375 -0.984375 0.140625 0.0625 -0.9375 0.109375 0.09375 -0.953125 0.09375 0.078125 -0.984375 0.171875 0.046875 -1.03125 0.140625 0.015625 -0.953125 0.171875 -0.015625 -0.984375 0.234375 -0.09375 -0.78125 0.328125 -0.03125 -1.046875 0.234375 0.03125 -1.578125 0.25 0.578125 -0.546875 -0.0625 0.234375 -0.859375 0.109375 0.203125 -0.859375 0.28125 0.140625 -0.78125 0.25 0.03125 -1.609375 0.234375 0.109375 -1.5 -0.21875 0.21875 -0.6875 0.140625 0.125 -1.015625 0.078125 0.15625 -0.921875 0.125 -0.015625 -0.8125 0.296875 -0.1875 -1.21875 0.203125 0.34375 -1.25 0.0625 0.375 -0.671875 -0.125 0.390625 -1.0 0.125 0.265625 -0.921875 0.015625 0.28125 -0.921875 0.203125 0.03125 -0.9375 0.0625 0.046875 -0.921875 0.140625 0.0625 -0.984375 0.15625 0.09375 -0.9375 0.078125 0.015625 -0.9375 0.09375 0.015625 -0.984375 0.078125 0.03125 -0.984375 0.046875 0.0625 -0.859375 0.1875 -0.015625 -0.9375 0.046875 0.4375 -0.953125 0.171875 0.234375 -0.765625 0.046875 0.234375 -0.875 0.09375 0.421875 -0.96875 0.0625 0.4375 -1.171875 0.34375 -0.09375 -0.84375 0.375 -0.125 -0.921875 -0.109375 0.125 -0.828125 0.140625 0.171875 -1.0 -0.015625 0.4375 -1.0625 -0.046875 0.046875 -0.953125 0.53125 -0.0625 -1.078125 0.03125 -0.0625 -0.671875 -0.015625 0.15625 -0.890625 -0.09375 0.375 -1.25 0.34375 0.03125 -0.78125 0.109375 0.15625 -0.75 0.203125 -0.015625 -0.953125 0.1875 0.140625 -0.96875 0.171875 0.109375 -0.953125 0.09375 0.140625 -0.96875 0.1875 0.15625 -0.96875 0.078125 0.09375 -0.9375 0.03125 0.078125 -0.9375 0.25 0.140625 -0.90625 0.25 0.078125 -0.875 0.265625 0.0625 -0.96875 0.421875 0.0 -0.96875 0.109375 0.359375 -0.96875 0.046875 0.4375 -0.8125 0.234375 0.21875 -0.765625 0.296875 0.0625 -0.859375 0.5 0.109375 -1.59375 -0.015625 -0.203125 -1.515625 -0.109375 0.359375 -0.71875 0.125 0.15625 -0.984375 0.1875 0.109375 -1.0 0.140625 0.078125 -0.8125 0.28125 -0.171875 -1.296875 0.4375 0.234375 -1.046875 0.171875 0.484375 -0.890625 -0.09375 0.4375 -0.71875 0.140625 0.234375 -0.9375 0.25 0.25 -0.828125 0.15625 0.234375 -1.421875 0.5 0.296875 -0.65625 0.0625 0.15625 -1.015625 0.328125 0.109375 -0.875 0.265625 0.171875 -0.953125 0.1875 0.21875 -0.984375 0.296875 0.125 -0.9375 0.125 0.28125 -0.96875 0.328125 0.15625 -0.921875 0.21875 0.109375 -0.953125 0.3125 0.125 -0.921875 0.25 0.15625 -0.9375 0.265625 0.171875 -0.953125 0.234375 0.1875 -0.921875 0.25 0.1875 -0.9375 0.25 0.1875 -0.9375 0.234375 0.1875 -0.9375 0.25 0.1875 -0.9375 0.265625 0.171875 -0.921875 0.25 0.109375 -0.9375 0.234375 0.203125 -1.015625 0.265625 0.171875 -0.9375 0.234375 0.09375 -1.0 0.21875 0.171875 -0.953125 0.140625 0.15625 -0.984375 0.140625 0.078125 -0.984375 0.015625 0.046875 -0.90625 -0.03125 0.09375 -0.984375 -0.015625 0.0 -1.0 -0.0625 -0.03125 -1.0 -0.21875 -0.21875 -0.78125 -0.359375 -0.15625 -0.953125 -0.578125 -0.328125 -0.90625 -0.484375 -0.359375 -0.9375 -1.21875 -1.59375 -0.765625 -0.5625 -0.515625 -0.671875 -0.53125 -0.8125 -0.625 -0.5625 -0.515625 -0.90625 -0.796875 0.515625 -0.4375 -0.4375 -0.359375 -0.609375 -0.65625 -0.15625 -0.484375 -0.734375 0.359375 -0.734375 -0.90625 0.203125 -0.28125 -1.171875 0.1875 --0.015625 -0.984375 0.0625 -0.046875 -1.046875 0.09375 -0.40625 -1.109375 0.171875 -0.578125 -0.875 -0.078125 -0.3125 -0.84375 -0.09375 -0.078125 -0.75 0.0 -0.078125 -0.734375 0.125 --0.1875 -0.890625 0.53125 --0.46875 -0.46875 1.234375 --0.359375 -0.203125 1.140625 --0.359375 -0.046875 0.859375 --0.078125 -0.484375 1.171875 --0.203125 0.125 0.890625 -0.015625 -0.03125 1.125 --0.21875 -0.171875 0.203125 -0.0 -0.0625 0.65625 -0.03125 -0.015625 0.96875 --0.015625 -0.015625 1.03125 -0.03125 0.0 1.015625 -0.03125 0.0 1.015625 -0.03125 0.0 0.9375 -0.046875 0.0 1.03125 -0.046875 0.0 1.015625 -0.03125 0.015625 1.03125 -0.046875 0.0 1.046875 -0.046875 0.015625 1.015625 -0.046875 0.0 0.984375 -0.046875 0.015625 1.015625 -0.046875 0.0 1.03125 -0.046875 0.015625 1.03125 -0.0 0.0 0.984375 -0.046875 0.015625 1.03125 -0.03125 0.015625 1.03125 -0.046875 0.0 1.03125 -0.046875 0.0 1.015625 -0.046875 0.0 1.03125 -0.046875 0.0 1.03125 -0.03125 0.0 1.03125 -0.03125 0.015625 1.03125 --0.03125 0.015625 0.96875 -0.046875 0.0 1.03125 -0.03125 0.0 1.03125 -0.046875 0.015625 1.03125 -0.03125 0.015625 1.015625 -0.03125 0.0 1.03125 -0.03125 -0.015625 1.03125 -0.03125 0.015625 1.015625 -0.046875 0.0 1.03125 --0.0625 0.0 0.984375 -0.03125 0.0 1.03125 -0.03125 0.015625 1.015625 -0.015625 0.0 1.046875 -0.046875 0.0 1.046875 -0.046875 0.0 1.03125 -0.046875 -0.015625 1.015625 -0.03125 0.015625 1.03125 -0.046875 0.015625 1.03125 -0.03125 0.0 0.921875 -0.046875 0.015625 1.03125 -0.03125 0.0 1.03125 -0.03125 0.0 1.015625 -0.03125 -0.015625 1.03125 -0.03125 0.0 1.03125 -0.03125 0.0 1.015625 -0.03125 0.046875 1.109375 -0.046875 0.015625 1.046875 -0.03125 0.03125 1.03125 -0.046875 0.03125 1.015625 -0.03125 0.0 1.03125 -0.046875 0.015625 1.015625 --0.015625 0.0 1.03125 -0.046875 0.015625 1.03125 -0.03125 0.0 0.953125 -0.03125 0.015625 1.046875 -0.046875 0.015625 1.03125 -0.03125 0.0 0.90625 -0.03125 0.0 1.015625