From 53c3fe55d042f668900562e6bc6effa10df6e535 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Fri, 28 Jan 2022 17:22:31 +0000 Subject: [PATCH 01/50] Initial Commit Project Framework --- .gitignore | 2 ++ README.md | 3 +++ main.py | 1 + requirements.txt | 3 +++ 4 files changed, 9 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..02bf5ba --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv/ +.vscode/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..5bb3b3c --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# ELEC60013 Embedded Systems: Coursework 1 + +- Raspberry Pi Zero W IoT Device diff --git a/main.py b/main.py new file mode 100644 index 0000000..cc8d835 --- /dev/null +++ b/main.py @@ -0,0 +1 @@ +print("Raspberry Pi Zero W, up and running!") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c3ea966 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +colorzero==2.0 +gpiozero==1.6.2 +smbus2==0.4.1 From 3d946a168bdf235bcb6b65992bb682ea6d04dfbe Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Tue, 1 Feb 2022 21:22:54 +0000 Subject: [PATCH 02/50] Update .gitignore, readme --- .gitignore | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++- README.md | 10 ++++ 2 files changed, 165 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 02bf5ba..89e509d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,155 @@ -.venv/ -.vscode/ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +# VSCode +.vscode/ \ No newline at end of file diff --git a/README.md b/README.md index 5bb3b3c..573523a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # ELEC60013 Embedded Systems: Coursework 1 - Raspberry Pi Zero W IoT Device +- Competing product: Fi Collar + - Our features can outcompete: + - Battery Life + - Tracking Accuracy? + - Much Larger Network + - Health Metrics + - Can do advanced features too: + - Sleep Tracking + - Step Counts + - Escape Detection? As soon as out of range of owner device? From 4076623ef8ff33023a9e5f9191d6e8a601a3f7b6 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Tue, 1 Feb 2022 23:11:36 +0000 Subject: [PATCH 03/50] Add Custom Si7201 Lib Si7201 temperature and relative humidity sensor class, with callable properties --- si7201.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 si7201.py diff --git a/si7201.py b/si7201.py new file mode 100644 index 0000000..9ea2db3 --- /dev/null +++ b/si7201.py @@ -0,0 +1,40 @@ +"""Library for interacting with Si7201 Temperature & Humidity Sensor.""" + +import smbus2 +from time import sleep + + +class Si7201: + def __init__(self, i2cBus, i2cAddress=0x40): + self.i2c = i2cBus + self.addr = i2cAddress + i2cBus.pec = True # enable smbus2 Packet Error Checking + + @property + def temperature(self, decimals=1): + """Measured temperature in degrees Celsius, with configurable decimal places, default 1.""" + measure_temp = smbus2.i2c_msg.write(self.addr, [0xF3]) + read_temp = smbus2.i2c_msg.read(self.addr, 2) + self.i2c.i2c_rdwr(measure_temp) + sleep(0.1) + self.i2c.i2c_rdwr(read_temp) + temp_code = int.from_bytes(read_temp.buf[0] + read_temp.buf[1], "big") + temp = round(((175.72 * temp_code) / 65536 - 46.85), decimals) + return temp + + @property + def humidity(self, decimals=1): + """Measured relative humidity in percent, with configurable decimal places, default 1.""" + measure_hum = smbus2.i2c_msg.write(self.addr, [0xF5]) + read_rh = smbus2.i2c_msg.read(self.addr, 2) + self.i2c.i2c_rdwr(measure_hum) + sleep(0.1) + self.i2c.i2c_rdwr(read_rh) + rh_code = int.from_bytes(read_rh.buf[0] + read_rh.buf[1], "big") + hum = round((125 * rh_code) / 65536 - 6.0, decimals) + return hum + + def reset(self): + """Reset the sensor.""" + resetcmd = smbus2.i2c_msg.write(self.addr, [0xFE]) + self.i2c.i2c_rdwr(resetcmd) From 66403e9e030e385b95c03bd7360ffedd8772faf4 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Fri, 4 Feb 2022 14:42:03 +0000 Subject: [PATCH 04/50] Add Custom TMP006 Lib TMP006 IR temperature sensor class, with callable properties --- tmp006.py | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tmp006.py diff --git a/tmp006.py b/tmp006.py new file mode 100644 index 0000000..f71d2b2 --- /dev/null +++ b/tmp006.py @@ -0,0 +1,122 @@ +"""Library for interacting with TMP006 Thermopile (IR Temperature) Sensor.""" + +import smbus2 +from time import sleep + +# Pointer Register Locations +_REG_VOBJ = bytes([0x00]) +_REG_TAMB = bytes([0x01]) +_REG_CNFG = bytes([0x02]) +_REG_M_ID = bytes([0xFE]) +_REG_D_ID = bytes([0xFF]) +# Configuration Flags +_MODE_ON = bytes([0x70]) +SAMPLERATE_4HZ = bytes([0x00]) +SAMPLERATE_2HZ = bytes([0x02]) +SAMPLERATE_1HZ = bytes([0x04]) +SAMPLERATE_0_5HZ = bytes([0x06]) +SAMPLERATE_0_25HZ = bytes([0x08]) +_DRDY_EN = bytes([0x01]) + + +class TMP006: + def __init__(self, i2cBus, i2cAddress=0x40, samplerate=SAMPLERATE_1HZ): + self.i2c = i2cBus + self.addr = i2cAddress + self.samplerate = samplerate + i2cBus.pec = True # enable smbus2 Packet Error Checking + self.config = bytes([0x00, 0x00]) + self.config = ( + self.config[0] | samplerate[0] | _MODE_ON[0] | _DRDY_EN[0] + self.config[1] + ) + ptrConfig = smbus2.i2c_msg.write(self.addr, _REG_CNFG) + writeConfig = smbus2.i2c_msg.write(self.addr, self.config) + self.i2c.i2c_rdwr(ptrConfig, writeConfig) + + @property + def temperature(self) -> float: + Vobj = self.vObject() + Tdie = self.tAmbient() + # Values for Calculations + S0 = 6.4e-14 # Calibration Factor TODO: Calibrate + a1 = 1.75e-3 + a2 = -1.678e-5 + Tref = 298.15 + b0 = -2.94e-5 + b1 = -5.7e-7 + b2 = 4.63e-9 + c2 = 13.4 + # Calculate Sensitivity of Thermopile + S = S0 * (1 + a1 * (Tdie - Tref) + a2 * ((Tdie - Tref) ** 2)) + # Calculate Coltage offset due to package thermal resistance + Voffset = b0 + b1 * (Tdie - Tref) + b2 * ((Tdie - Tref) ** 2) + # Calculate Seebeck coefficients + fVobj = (Vobj - Voffset) + c2 * ((Vobj - Voffset) ** 2) + # Calculate object temperature in Kelvin + Tobj = (Tdie**4 + (fVobj / S)) ** 0.25 + # Convert from Kelvin to Celsius + return Tobj - 273.15 + + @property + def active(self) -> bool: + """Check if Sensor is powered on.""" + ptrPower = smbus2.i2c_msg.write(self.addr, _REG_CNFG) + power = smbus2.i2c_msg.read(self.addr, 2) + self.i2c.i2c_rdwr(ptrPower, power) + return power.buf[0] & _MODE_ON[0] != 0 + + @active.setter + def active(self, value: bool): + """Set the sensor to active or inactive.""" + if value: + ptrPower = smbus2.i2c_msg.write(self.addr, _REG_CNFG) + power = smbus2.i2c_msg.read(self.addr, 2) + self.i2c.i2c_rdwr(ptrPower, power) + newPower = power.buf[0] | _MODE_ON[0] + power.buf[1] + updatePower = smbus2.i2c_msg.write(self.addr, newPower) + self.i2c.i2c_rdwr(ptrPower, updatePower) + else: + ptrPower = smbus2.i2c_msg.write(self.addr, _REG_CNFG) + power = smbus2.i2c_msg.read(self.addr, 2) + self.i2c.i2c_rdwr(ptrPower, power) + newPower = power.buf[0] & ~_MODE_ON[0] + power.buf[1] + updatePower = smbus2.i2c_msg.write(self.addr, newPower) + self.i2c.i2c_rdwr(ptrPower, updatePower) + + def vObject(self) -> float: + """Reading from Sensor Voltage Register in Volts""" + ptrVobject = smbus2.i2c_msg.write(self.addr, _REG_VOBJ) + readVobject = smbus2.i2c_msg.read(self.addr, 2) + self.i2c.i2c_rdwr(ptrVobject, readVobject) + scaledVoltage = int.from_bytes( + readVobject.buf[0] + readVobject.buf[1], byteorder="big", signed=True + ) + return round(scaledVoltage * 156.25e-9, 1) + # convert to Volts (156.25nV per LSB * 1e-9 for scaling from nV to Volts) + + def tAmbient(self) -> float: + """Reading from Ambient Temperature Register in Degrees Celsius""" + ptrTambient = smbus2.i2c_msg.write(self.addr, _REG_TAMB) + readTambient = smbus2.i2c_msg.read(self.addr, 2) + self.i2c.i2c_rdwr(ptrTambient, readTambient) + scaledTemp = int.from_bytes( + readTambient.buf[0] + readTambient.buf[1], byteorder="big", signed=True + ) + return round(scaledTemp * 0.0078125, 1) + # convert to degrees Celsius (1/32 for scaling * 1/4 for 2 bit shift) + + @property + def manID(self) -> bytes: + """Sensor manufacturer ID""" + ptrManID = smbus2.i2c_msg.write(self.addr, _REG_M_ID) + readManID = smbus2.i2c_msg.read(self.addr, 2) + self.i2c.i2c_rdwr(ptrManID, readManID) + return readManID.buf[0] + readManID.buf[1] + + @property + def devID(self) -> bytes: + """Sensor device ID""" + ptrDevID = smbus2.i2c_msg.write(self.addr, _REG_D_ID) + readDevID = smbus2.i2c_msg.read(self.addr, 2) + self.i2c.i2c_rdwr(ptrDevID, readDevID) + return readDevID.buf[0] + readDevID.buf[1] From be0aa0fb873f46a9b30caa525b829f0723de0a84 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Wed, 9 Feb 2022 10:49:00 +0000 Subject: [PATCH 05/50] Testing script for Si7201 and TMP006 --- main.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index cc8d835..b3717e1 100644 --- a/main.py +++ b/main.py @@ -1 +1,26 @@ -print("Raspberry Pi Zero W, up and running!") \ No newline at end of file +from time import sleep +import smbus2 +import si7201 +import tmp006 + +bus = smbus2.SMBus(1) # set up I2C bus 1 + +print("========== Testing Si7201 ==========") +temphumsensor = si7201.Si7201(bus) # set up Si7201 sensor +temphumsensor.reset() # reset the sensor +sleep(1) # wait for sensor to reset +print(temphumsensor.temperature) # read the temperature and print +print(temphumsensor.humidity) # read the humidity and print + +print("========== Testing TMP006 ==========") +irtempsensor = tmp006.TMP006(bus, 0x41, tmp006.SAMPLERATE_4HZ) # set up TMP006 sensor +irtempsensor.active(1) # turn on the sensor +sleep(1) # wait for sensor to turn on +print(irtempsensor.manID) # read the manufacturer ID and print +print(irtempsensor.devID) # read the device ID and print +print(irtempsensor.temperature) # read the temperature and print + +print("========= TMP006 Test Loop =========") +while True: + print(irtempsensor.temperature) # read the temperature and print + sleep(1) # wait for 1 second From 540c3e44efc7d7a3c6b064155fcc0009c495ebca Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Wed, 9 Feb 2022 11:21:19 +0000 Subject: [PATCH 06/50] Fix errors in TMP006 Lib Byte values in int form need to be converted to bytes before concatenation Active flag buffer needs to be converted to int before comparison with int literal --- tmp006.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tmp006.py b/tmp006.py index f71d2b2..f14fd93 100644 --- a/tmp006.py +++ b/tmp006.py @@ -26,8 +26,8 @@ class TMP006: self.samplerate = samplerate i2cBus.pec = True # enable smbus2 Packet Error Checking self.config = bytes([0x00, 0x00]) - self.config = ( - self.config[0] | samplerate[0] | _MODE_ON[0] | _DRDY_EN[0] + self.config[1] + self.config = bytes( + [self.config[0] | samplerate[0] | _MODE_ON[0] | _DRDY_EN[0], self.config[1]] ) ptrConfig = smbus2.i2c_msg.write(self.addr, _REG_CNFG) writeConfig = smbus2.i2c_msg.write(self.addr, self.config) @@ -63,7 +63,7 @@ class TMP006: ptrPower = smbus2.i2c_msg.write(self.addr, _REG_CNFG) power = smbus2.i2c_msg.read(self.addr, 2) self.i2c.i2c_rdwr(ptrPower, power) - return power.buf[0] & _MODE_ON[0] != 0 + return power.buf[0][0] & _MODE_ON[0] != 0 @active.setter def active(self, value: bool): @@ -72,14 +72,14 @@ class TMP006: ptrPower = smbus2.i2c_msg.write(self.addr, _REG_CNFG) power = smbus2.i2c_msg.read(self.addr, 2) self.i2c.i2c_rdwr(ptrPower, power) - newPower = power.buf[0] | _MODE_ON[0] + power.buf[1] + newPower = bytes([power.buf[0][0] | _MODE_ON[0], power.buf[1][0]]) updatePower = smbus2.i2c_msg.write(self.addr, newPower) self.i2c.i2c_rdwr(ptrPower, updatePower) else: ptrPower = smbus2.i2c_msg.write(self.addr, _REG_CNFG) power = smbus2.i2c_msg.read(self.addr, 2) self.i2c.i2c_rdwr(ptrPower, power) - newPower = power.buf[0] & ~_MODE_ON[0] + power.buf[1] + newPower = bytes([power.buf[0][0] & ~_MODE_ON[0], power.buf[1][0]]) updatePower = smbus2.i2c_msg.write(self.addr, newPower) self.i2c.i2c_rdwr(ptrPower, updatePower) From d758ed6f97186ba23ba2f162f5c044d34d645721 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Wed, 9 Feb 2022 11:38:10 +0000 Subject: [PATCH 07/50] Correct Active Setter Usage --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index b3717e1..94e2c6a 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,7 @@ print(temphumsensor.humidity) # read the humidity and print print("========== Testing TMP006 ==========") irtempsensor = tmp006.TMP006(bus, 0x41, tmp006.SAMPLERATE_4HZ) # set up TMP006 sensor -irtempsensor.active(1) # turn on the sensor +irtempsensor.active = 1 # turn on the sensor sleep(1) # wait for sensor to turn on print(irtempsensor.manID) # read the manufacturer ID and print print(irtempsensor.devID) # read the device ID and print From 69d2e7e2a1e0c0e748f6189b232cf961ff474cd0 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Fri, 18 Feb 2022 17:43:49 +0000 Subject: [PATCH 08/50] Add HCI Cast Lib & PyYaml HCI Cast yet to be tested / debugged PyYaml to ingest secrets file --- .gitignore | 5 +++- hci.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 17 +++++++++-- requirements.txt | 1 + 4 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 hci.py diff --git a/.gitignore b/.gitignore index 89e509d..ba5791d 100644 --- a/.gitignore +++ b/.gitignore @@ -152,4 +152,7 @@ cython_debug/ #.idea/ # VSCode -.vscode/ \ No newline at end of file +.vscode/ + +# Secrets file +.secrets.yml diff --git a/hci.py b/hci.py new file mode 100644 index 0000000..7aaffed --- /dev/null +++ b/hci.py @@ -0,0 +1,73 @@ +import base64 +import subprocess +from time import sleep +from struct import pack + + +class HCIBroadcaster: + def __init__(self, b64key): + self.key = base64.b64decode(b64key) + + def _advertisement_template(): + adv = "" + adv += "1e" # length (30) + adv += "ff" # manufacturer specific data + adv += "4c00" # company ID (Apple) + adv += "1219" # offline finding type and length + adv += "00" # state + for _ in range(22): # key[6:28] + adv += "00" + adv += "00" # first two bits of key[0] + adv += "00" # hint + return bytearray.fromhex(adv) + + def _bytes_to_strarray(self, bytes_, with_prefix=False): + if with_prefix: + return [hex(b) for b in bytes_] + else: + return [format(b, "x") for b in bytes_] + + def _run_hci_cmd(self, cmd, hci="hci0", wait=1): + cmd_ = ["hcitool", "-i", hci, "cmd"] + cmd_ += cmd + print(cmd_) + subprocess.run(cmd_) + if wait > 0: + sleep(wait) + + def start_advertising(self, interval_ms=2000): + key = self.key + addr = bytearray(key[:6]) + addr[0] |= 0b11000000 + + adv = self._advertisement_template() + adv[7:29] = key[6:28] + adv[29] = key[0] >> 6 + + print(f"key ({len(key):2}) {key.hex()}") + print(f"address ({len(addr):2}) {addr.hex()}") + print(f"payload ({len(adv):2}) {adv.hex()}") + + # Set BLE address + self._run_hci_cmd( + ["0x3f", "0x001"] + self._bytes_to_strarray(addr, with_prefix=True)[::-1] + ) + subprocess.run(["systemctl", "restart", "bluetooth"]) + sleep(1) + + # Set BLE advertisement payload + self._run_hci_cmd( + ["0x08", "0x0008"] + [format(len(adv), "x")] + self._bytes_to_strarray(adv) + ) + + # Set BLE advertising mode + interval_enc = pack(" Date: Thu, 24 Feb 2022 20:29:49 +0000 Subject: [PATCH 09/50] Update to final program design Accelerometer setup and class code to be added --- main.py | 72 ++++++++++++++++++++++++++++++++---------------- requirements.txt | 5 +++- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/main.py b/main.py index cc0f23f..82cbad4 100644 --- a/main.py +++ b/main.py @@ -1,37 +1,61 @@ +import os, sys from time import sleep -import yaml -import smbus2 -import si7201 -import tmp006 -import hci +from strictyaml import load, Map, Str, YAMLError +import paho.mqtt.client as mqtt +import json, smbus2, si7201, tmp006, hci, gpiozero +# Global Sensor Data Variables +dailysteps = 0 +fallen = False + +def incrementStepCount() -> None: + global dailysteps + dailysteps += 1 + +def setFallen() -> None: + global fallen + fallen = True + +# Setup bus = smbus2.SMBus(1) # set up I2C bus 1 -print("========== Testing Si7201 ==========") -temphumsensor = si7201.Si7201(bus) # set up Si7201 sensor -temphumsensor.reset() # reset the sensor -sleep(1) # wait for sensor to reset -print(temphumsensor.temperature) # read the temperature and print -print(temphumsensor.humidity) # read the humidity and print +temphum = si7201.Si7201(bus, 0x40) # set up Si7201 sensor +temphum.reset() # reset Si7201 -print("========== Testing TMP006 ==========") -irtempsensor = tmp006.TMP006(bus, 0x41, tmp006.SAMPLERATE_4HZ) # set up TMP006 sensor -irtempsensor.active = 1 # turn on the sensor -sleep(1) # wait for sensor to turn on -print(irtempsensor.manID) # read the manufacturer ID and print -print(irtempsensor.devID) # read the device ID and print -print(irtempsensor.temperature) # read the temperature and print +irtemp = tmp006.TMP006(bus, 0x41, tmp006.SAMPLERATE_4HZ) # set up TMP006 sensor +irtemp.active = 1 # turn on TMP006 + +# accel = lis3dh.LIS3DH(bus, 0x18, otherconfigflags) # set up LIS3DH sensor +# TODO: other LIS3DH Setup +step = gpiozero.Button(18, pull_up = False) # GPIO18: Step Counter Interrupt (INT1) +step.when_activated = incrementStepCount # increment step count when Step Counter Interrupt (INT1) is triggered +fall = gpiozero.Button(17, pull_up = False) # GPIO17: Freefall Interrupt (INT2) +fall.when_activated = setFallen # set fallen to True when Freefall Interrupt (INT2) is triggered -print("========= Testing HCI Cast =========") with open(".secrets.yml", "r") as secrets: try: - secrets = yaml.safe_load(secrets) + secrets = load(secrets, schema = Map({"key": Str()})) key = secrets["key"] # Get Base64 encoded device public key from secrets file - except yaml.YAMLError as exc: + except YAMLError as exc: print(exc) - + sleep(60) # 60s delay before restarting + os.execl(sys.executable, os.path.abspath(__file__), *sys.argv) # Restart propgram btcast = hci.HCIBroadcaster(key) # set up HCI Broadcaster -btcast.start_advertising(5000) # start advertising with interval of 5 seconds +client = mqtt.Client("RaspberryPi") # set up MQTT client +client.connect("add8.duckdns.org", 8883, 60) # connect to MQTT broker +client.loop_start() # Start a new thread to handle sending MQTT messages + +# Main Loop while True: - btcast.start_advertising(5000) # start advertising with interval of 5 seconds + data = { + "devID": "testdoggo", + "air_temp": temphum.temperature, + "day_steps": dailysteps, + "hum_perc": temphum.humidity, + "pet_temp": irtemp.temperature + } + mqtt_data = json.dumps(data) + client.publish("/data", mqtt_data) + btcast.start_advertising() # Send out BT advertisement + sleep(5) # Sleep for 5 seconds to lower power consumption diff --git a/requirements.txt b/requirements.txt index bf38714..d6290a9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,7 @@ colorzero==2.0 gpiozero==1.6.2 -PyYAML==6.0 +paho-mqtt==1.6.1 +python-dateutil==2.8.2 +six==1.16.0 smbus2==0.4.1 +strictyaml==1.6.1 From f7da767fbb9091d3ee12e63c8e3421d5f645870a Mon Sep 17 00:00:00 2001 From: Kacper Date: Sat, 26 Feb 2022 19:51:43 +0000 Subject: [PATCH 10/50] 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 11/50] 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 12/50] 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 13/50] 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 14/50] 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 15/50] 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 16/50] 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 17/50] 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 18/50] 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 19/50] 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 20/50] 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 21/50] 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 22/50] 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 23/50] 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 24/50] 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 25/50] 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 26/50] 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 27/50] 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 28/50] 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 29/50] 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 30/50] 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 31/50] 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 32/50] 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 33/50] 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 34/50] 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 35/50] 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 36/50] 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 37/50] 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 38/50] 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 39/50] 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 40/50] 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 41/50] 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 42/50] 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 43/50] 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 9ed8732ef6707e5fc966bbfcbb3f9aeaf918002a Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Tue, 1 Mar 2022 16:29:24 +0000 Subject: [PATCH 44/50] Add matlab files to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index ba5791d..f34863c 100644 --- a/.gitignore +++ b/.gitignore @@ -154,5 +154,8 @@ cython_debug/ # VSCode .vscode/ +# Matlab files +.m + # Secrets file .secrets.yml 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 45/50] 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 From 4db2a4d34d9d367436ec7648ab158d76955c9fa3 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Tue, 1 Mar 2022 16:48:55 +0000 Subject: [PATCH 46/50] Integrate Accelerometer --- lis3dh.py | 2 +- main.py | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index c96813d..38f8f97 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -43,7 +43,7 @@ INT1_DURATION_V = bytes([0x03]) INT1_CFG_V = bytes([0x95]) EMPTY = bytes([0x00]) -class lis3dh: +class LIS3DH: def __init__(self, i2cBus, samplerate=10, i2cAddress=0x18): sleep(0.005) self.i2c = i2cBus diff --git a/main.py b/main.py index 82cbad4..03c8bf1 100644 --- a/main.py +++ b/main.py @@ -2,19 +2,21 @@ import os, sys from time import sleep from strictyaml import load, Map, Str, YAMLError import paho.mqtt.client as mqtt -import json, smbus2, si7201, tmp006, hci, gpiozero +import json, smbus2, si7201, tmp006, lis3dh, hci, gpiozero # Global Sensor Data Variables dailysteps = 0 fallen = False -def incrementStepCount() -> None: +def incrementStepCount(interrupt, sensor) -> None: global dailysteps dailysteps += 1 + sensor.resetint2() -def setFallen() -> None: +def setFallen(interrupt, sensor) -> None: global fallen fallen = True + sensor.resetint1() # Setup bus = smbus2.SMBus(1) # set up I2C bus 1 @@ -25,12 +27,11 @@ temphum.reset() # reset Si7201 irtemp = tmp006.TMP006(bus, 0x41, tmp006.SAMPLERATE_4HZ) # set up TMP006 sensor irtemp.active = 1 # turn on TMP006 -# accel = lis3dh.LIS3DH(bus, 0x18, otherconfigflags) # set up LIS3DH sensor -# TODO: other LIS3DH Setup -step = gpiozero.Button(18, pull_up = False) # GPIO18: Step Counter Interrupt (INT1) -step.when_activated = incrementStepCount # increment step count when Step Counter Interrupt (INT1) is triggered -fall = gpiozero.Button(17, pull_up = False) # GPIO17: Freefall Interrupt (INT2) -fall.when_activated = setFallen # set fallen to True when Freefall Interrupt (INT2) is triggered +accel = lis3dh.LIS3DH(bus, 10, 0x18) # set up LIS3DH sensor +fall = gpiozero.Button(18, pull_up = False) # GPIO17: Freefall Interrupt (INT1) +fall.when_activated = setFallen(fall, accel) # set fallen to True when Freefall Interrupt (INT1) is triggered +step = gpiozero.Button(17, pull_up = False) # GPIO18: Step Counter Interrupt (INT2) +step.when_activated = incrementStepCount(step, accel) # increment step count when Step Counter Interrupt (INT2) is triggered with open(".secrets.yml", "r") as secrets: try: From d6e996bad88ab1eb7ea1dc76c8bc52450f2dd992 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Tue, 1 Mar 2022 17:19:44 +0000 Subject: [PATCH 47/50] Bugfixes --- lis3dh.py | 4 ++-- main.py | 10 +++++----- requirements.txt | 3 ++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lis3dh.py b/lis3dh.py index 38f8f97..1ddfdd2 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -82,7 +82,7 @@ class LIS3DH: 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: + if read_int1_src.buf[0] != None: return True else: return False @@ -92,7 +92,7 @@ class LIS3DH: 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: + if read_int2_src.buf[0] != None: return True else: return False diff --git a/main.py b/main.py index 03c8bf1..876e62f 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import os, sys from time import sleep -from strictyaml import load, Map, Str, YAMLError +import yaml import paho.mqtt.client as mqtt import json, smbus2, si7201, tmp006, lis3dh, hci, gpiozero @@ -29,15 +29,15 @@ irtemp.active = 1 # turn on TMP006 accel = lis3dh.LIS3DH(bus, 10, 0x18) # set up LIS3DH sensor fall = gpiozero.Button(18, pull_up = False) # GPIO17: Freefall Interrupt (INT1) -fall.when_activated = setFallen(fall, accel) # set fallen to True when Freefall Interrupt (INT1) is triggered +fall.when_activated = lambda: setFallen(fall, accel) # set fallen to True when Freefall Interrupt (INT1) is triggered step = gpiozero.Button(17, pull_up = False) # GPIO18: Step Counter Interrupt (INT2) -step.when_activated = incrementStepCount(step, accel) # increment step count when Step Counter Interrupt (INT2) is triggered +step.when_activated = lambda: incrementStepCount(step, accel) # increment step count when Step Counter Interrupt (INT2) is triggered with open(".secrets.yml", "r") as secrets: try: - secrets = load(secrets, schema = Map({"key": Str()})) + secrets = yaml.load(secrets, Loader = yaml.SafeLoader) key = secrets["key"] # Get Base64 encoded device public key from secrets file - except YAMLError as exc: + except ImportError as exc: print(exc) sleep(60) # 60s delay before restarting os.execl(sys.executable, os.path.abspath(__file__), *sys.argv) # Restart propgram diff --git a/requirements.txt b/requirements.txt index d6290a9..1d471d5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ colorzero==2.0 gpiozero==1.6.2 paho-mqtt==1.6.1 python-dateutil==2.8.2 +PyYAML==6.0 +RPi.GPIO==0.7.1 six==1.16.0 smbus2==0.4.1 -strictyaml==1.6.1 From fe8f4b328304b8482e2a26b6ae8fae94a22b596a Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Tue, 1 Mar 2022 17:29:27 +0000 Subject: [PATCH 48/50] HCI Bugfix --- hci.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hci.py b/hci.py index 7aaffed..30ade9b 100644 --- a/hci.py +++ b/hci.py @@ -8,7 +8,7 @@ class HCIBroadcaster: def __init__(self, b64key): self.key = base64.b64decode(b64key) - def _advertisement_template(): + def _advertisement_template(self): adv = "" adv += "1e" # length (30) adv += "ff" # manufacturer specific data From 9ee374dc2768c711a4cde1ed66c8edee92bfa492 Mon Sep 17 00:00:00 2001 From: Kacper Date: Wed, 2 Mar 2022 09:09:44 +0000 Subject: [PATCH 49/50] Decrease threshold for step count --- lis3dh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lis3dh.py b/lis3dh.py index c96813d..40f0cec 100644 --- a/lis3dh.py +++ b/lis3dh.py @@ -73,7 +73,7 @@ class lis3dh: 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]) + c11 = smbus2.i2c_msg.write(self.addr, [0x36,0x32]) 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) From 36eecf3a0082bdc745fe3fe04fa1506ca66702a2 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Wed, 2 Mar 2022 10:53:45 +0000 Subject: [PATCH 50/50] Add rounded output to TMP006 temperature output --- tmp006.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tmp006.py b/tmp006.py index f14fd93..4bffaa3 100644 --- a/tmp006.py +++ b/tmp006.py @@ -35,6 +35,7 @@ class TMP006: @property def temperature(self) -> float: + """Measured temperature in degrees Celsius, to 2 decimel places""" Vobj = self.vObject() Tdie = self.tAmbient() # Values for Calculations @@ -55,7 +56,7 @@ class TMP006: # Calculate object temperature in Kelvin Tobj = (Tdie**4 + (fVobj / S)) ** 0.25 # Convert from Kelvin to Celsius - return Tobj - 273.15 + return round(Tobj - 273.15, 2) @property def active(self) -> bool: