adding files for the commit
This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
# 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/
|
||||
pip-wheel-metadata/
|
||||
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/
|
||||
|
||||
# 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
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
.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
|
||||
|
||||
# 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/
|
||||
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
+21
-9
@@ -1,5 +1,7 @@
|
||||
import logging
|
||||
import struct
|
||||
|
||||
from time import sleep
|
||||
from converters import BinToInt, BinToFloat
|
||||
from .wand_sensors import *
|
||||
from bleak import BleakClient
|
||||
@@ -17,8 +19,9 @@ class KanoBLEClient(object):
|
||||
self.decoder = wand_decoder()
|
||||
self.wand_sensors = WandSensors(self.sensor_update_handler)
|
||||
self.loop = loop
|
||||
self.dateframe_handler = None
|
||||
|
||||
async def connect_and_read(self, debug=False):
|
||||
async def connect(self, debug=False):
|
||||
if debug:
|
||||
import sys
|
||||
l = logging.getLogger("asyncio")
|
||||
@@ -28,25 +31,34 @@ class KanoBLEClient(object):
|
||||
l.addHandler(h)
|
||||
logger.addHandler(h)
|
||||
|
||||
async with BleakClient(self.wand_address, loop=self.loop) as client:
|
||||
self.client = client
|
||||
x = await client.is_connected()
|
||||
logger.info("Connected: {0}".format(x))
|
||||
await start_notify(self.client, self.sensor_handling)
|
||||
self.client = BleakClient(self.wand_address, loop=self.loop)
|
||||
await self.client.connect()
|
||||
x = await self.client.is_connected()
|
||||
logger.info("Connected: {0}".format(x))
|
||||
|
||||
|
||||
async def stop_recieving_data(self):
|
||||
await stop_notify(self.client)
|
||||
async def start_recieving_data(self, sensors=None):
|
||||
await start_notify(self.client, self.sensor_handling, sensors)
|
||||
|
||||
async def stop_recieving_data(self, sensors):
|
||||
await stop_notify(self.client, sensors)
|
||||
|
||||
def change_spell(self, spell):
|
||||
self.wand_sensors.data_folder = "./training_data/" + spell + "/"
|
||||
print(f"changed wand folder to {self.wand_sensors.data_folder}")
|
||||
|
||||
def sensor_update_handler(self, sensors):
|
||||
print(sensors)
|
||||
# print("SENSORS RECIEVED")
|
||||
return
|
||||
|
||||
|
||||
def sensor_handling(self, sender, data):
|
||||
"""Simple notification handler which prints the data received."""
|
||||
sender = CHARACTERISTIC_UUIDS[sender]
|
||||
sender = get_key(sender, CHARACTERISTIC_UUIDS)
|
||||
if sender == BUTTON:
|
||||
self.wand_sensors.set_button(self.decoder.decode_button(data))
|
||||
self.loop.run_until_complete(self.client.write_gatt_char(RESET_CHAR, struct.pack("h", 1)))
|
||||
elif sender == NINE_AXIS:
|
||||
self.wand_sensors.set_gyro(*self.decoder.decode_nine_axis(data))
|
||||
elif sender == ACCELEROMETER:
|
||||
|
||||
@@ -5,11 +5,13 @@ BATTERY = 4
|
||||
TEMPERATURE = 5
|
||||
MAGNETOMETER = 6
|
||||
|
||||
RESET_CHAR = "64a70004-f691-4b93-a6f4-0968f5b648f8"
|
||||
|
||||
CHARACTERISTIC_UUIDS = {
|
||||
("64a7000d-f691-4b93-a6f4-0968f5b648f8"): BUTTON, # Button
|
||||
("64a7000a-f691-4b93-a6f4-0968f5b648f8"): NINE_AXIS, # 9 axis
|
||||
("64a7000c-f691-4b93-a6f4-0968f5b648f8"): ACCELEROMETER, # Accel
|
||||
("64a70007-f691-4b93-a6f4-0968f5b648f8"): BATTERY,
|
||||
("64a70014-f691-4b93-a6f4-0968f5b648f8"): TEMPERATURE,
|
||||
("64a70021-f691-4b93-a6f4-0968f5b648f8"): MAGNETOMETER
|
||||
BUTTON: ("64a7000d-f691-4b93-a6f4-0968f5b648f8"), # Button
|
||||
NINE_AXIS: ("64a7000a-f691-4b93-a6f4-0968f5b648f8"), # 9 axis
|
||||
ACCELEROMETER: ("64a7000c-f691-4b93-a6f4-0968f5b648f8"), # Accel
|
||||
BATTERY: ("64a70007-f691-4b93-a6f4-0968f5b648f8"),
|
||||
TEMPERATURE:("64a70014-f691-4b93-a6f4-0968f5b648f8"),
|
||||
MAGNETOMETER:("64a70021-f691-4b93-a6f4-0968f5b648f8")
|
||||
} # <--- Change to the characteristic you want to enable notifications from.
|
||||
|
||||
+42
-14
@@ -1,5 +1,5 @@
|
||||
import pandas as pd
|
||||
|
||||
import datetime
|
||||
|
||||
class ThreeAxisSensor(object):
|
||||
def __init__(self, x, y, z):
|
||||
@@ -8,31 +8,41 @@ class ThreeAxisSensor(object):
|
||||
self.z = z
|
||||
|
||||
def __str__(self):
|
||||
print(self.x, self.y, self.z)
|
||||
return f"{self.x}, +{self.y}, +{self.z}"
|
||||
|
||||
|
||||
class WandSensors(object):
|
||||
def __init__(self, wand_update_callback):
|
||||
self.gyro = None
|
||||
self.gyro_updates = 0
|
||||
def __init__(self, wand_update_callback, data_folder = "./", dataframe_handler = None):
|
||||
|
||||
self.accel = None
|
||||
self.accel_updates = 0
|
||||
|
||||
self.magneto = 0
|
||||
self.button = 0
|
||||
|
||||
self.gyro = None
|
||||
self.gyro_updates = 0
|
||||
|
||||
self.magneto = 0
|
||||
self.temperature = 0
|
||||
|
||||
self.dataframe = pd.DataFrame(columns=["gyro_x", "gyro_y", "gyro_z", "accel_x", "accel_y", "accel_z"])
|
||||
self.df = pd.DataFrame()
|
||||
|
||||
self.wand_update_callback = wand_update_callback
|
||||
self.data_folder = data_folder
|
||||
self.dataframe_handler = dataframe_handler
|
||||
|
||||
def __str__(self):
|
||||
return super().__str__()
|
||||
return f"Button " \
|
||||
f"{self.button} " \
|
||||
f"Accel " \
|
||||
f"{self.accel} " \
|
||||
f"Gyro " \
|
||||
f"{self.gyro} " \
|
||||
f"Mag {self.magneto} | Temp {self.temperature}"
|
||||
|
||||
def append_to_dataframe(self):
|
||||
if self.gyro_updates == self.accel_updates:
|
||||
self.dataframe = self.dataframe.append(
|
||||
self.df = self.df.append(
|
||||
pd.DataFrame({
|
||||
"gyro_x": self.gyro.x,
|
||||
"gyro_y": self.gyro.y,
|
||||
@@ -44,16 +54,30 @@ class WandSensors(object):
|
||||
"button": self.button
|
||||
}, index=[0])
|
||||
)
|
||||
self.wand_update_callback(self.dataframe)
|
||||
self.send_data_back()
|
||||
|
||||
def save_df_to_file(self, filename):
|
||||
self.dataframe.to_csv(filename, index=False)
|
||||
def send_data_back(self):
|
||||
self.wand_update_callback(self)
|
||||
|
||||
def save_df_to_file(self):
|
||||
filename = self.data_folder + datetime.datetime.now().strftime("%I%M%S-%B%d%Y.csv")
|
||||
import os
|
||||
try:
|
||||
os.makedirs(self.data_folder)
|
||||
except FileExistsError:
|
||||
# directory already exists
|
||||
pass
|
||||
try:
|
||||
self.df.to_csv(filename, index=False)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
def set_gyro(self, x, y, z):
|
||||
self.gyro = ThreeAxisSensor(x, y, z)
|
||||
self.gyro_updates += 1
|
||||
self.append_to_dataframe()
|
||||
|
||||
|
||||
def set_accel(self, x, y, z):
|
||||
self.accel = ThreeAxisSensor(x, y, z)
|
||||
self.accel_updates += 1
|
||||
@@ -64,9 +88,13 @@ class WandSensors(object):
|
||||
|
||||
def set_button(self, button):
|
||||
self.button = button
|
||||
|
||||
self.send_data_back()
|
||||
if not button:
|
||||
self.save_df_to_file()
|
||||
else:
|
||||
self.df = pd.DataFrame()
|
||||
def set_magneto(self, magneto):
|
||||
self.magneto = magneto
|
||||
|
||||
def get_dataframe(self):
|
||||
return self.dataframe
|
||||
return self.df
|
||||
|
||||
+14
-6
@@ -1,13 +1,21 @@
|
||||
from kano_wand.constants import *
|
||||
|
||||
async def start_notify(client, notification_handler):
|
||||
for char in CHARACTERISTIC_UUIDS.keys():
|
||||
await client.start_notify(char, notification_handler)
|
||||
async def start_notify(client, notification_handler, sensors):
|
||||
for char in sensors:
|
||||
await client.start_notify(CHARACTERISTIC_UUIDS[char], notification_handler)
|
||||
|
||||
|
||||
async def stop_notify(client):
|
||||
for char in CHARACTERISTIC_UUIDS.keys():
|
||||
await client.stop_notify(char)
|
||||
async def stop_notify(client, sensors):
|
||||
for char in sensors:
|
||||
await client.stop_notify(CHARACTERISTIC_UUIDS[char])
|
||||
|
||||
def chunker(seq, size):
|
||||
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
|
||||
|
||||
|
||||
def get_key(val, dictionary):
|
||||
for key, value in dictionary.items():
|
||||
if val == value:
|
||||
return key
|
||||
|
||||
return "key doesn't exist"
|
||||
Reference in New Issue
Block a user