From 1b0ecb4ce30221f9914a6f74d33eba549aa72354 Mon Sep 17 00:00:00 2001 From: Chad Mello Date: Fri, 8 Mar 2019 19:22:13 -0700 Subject: [PATCH] (1) Scale factor bug fix in parser according to https://docs.openbci.com/Hardware/03-Cyton_Data_Format#cyton-data-format-binary-format (2) OpenBCIWiFi now applies the sample rate passed into the constructor (3) New option to prevent OpenBCIWiFi from automatically connecting to board upon instantiation (4) scaled_output and micro_volts can now be set in OpenBCIWiFi constructor (5) prevent connection exception during OpenBCIWiFi instantiation by placing small time delay between shield discovery and connecting to the board (6) New test to demonstrate how these changes/new options work --- openbci/utils/parse.py | 5 +- openbci/wifi.py | 18 ++++++-- ...am_data_wifi_high_speed_no_auto_connect.py | 46 +++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 scripts/stream_data_wifi_high_speed_no_auto_connect.py diff --git a/openbci/utils/parse.py b/openbci/utils/parse.py index 424df38..4c3463e 100644 --- a/openbci/utils/parse.py +++ b/openbci/utils/parse.py @@ -11,6 +11,7 @@ def __init__(self, log=False, micro_volts=False, scaled_output=True): + self.board_type = board_type self.gains = gains self.log = log @@ -38,7 +39,9 @@ def is_stop_byte(self, byte): def get_ads1299_scale_factors(self, gains, micro_volts=None): out = [] for gain in gains: - scale_factor = k.ADS1299_VREF / float((pow(2, 23) - 1)) / float(gain) + #according to https://docs.openbci.com/Hardware/03-Cyton_Data_Format#cyton-data-format-binary-format: + #Scale Factor (Volts/count) = 4.5 Volts / gain / (2^23 - 1); + scale_factor = k.ADS1299_VREF / float(gain) / float((pow(2, 23) - 1)) if micro_volts is None: if self.micro_volts: scale_factor *= 1000000. diff --git a/openbci/wifi.py b/openbci/wifi.py index 5f2c7a4..01a2b45 100755 --- a/openbci/wifi.py +++ b/openbci/wifi.py @@ -24,6 +24,7 @@ def handle_sample(sample): import re import socket import timeit +import time try: import urllib2 @@ -65,7 +66,9 @@ class OpenBCIWiFi(object): def __init__(self, ip_address=None, shield_name=None, sample_rate=None, log=True, timeout=3, max_packets_to_skip=20, latency=10000, high_speed=True, ssdp_attempts=5, - num_channels=8, local_ip_address=None): + num_channels=8, local_ip_address=None, micro_volts=False, + scaled_output = True, auto_connect = True): + # these one are used self.daisy = False self.gains = None @@ -81,6 +84,9 @@ def __init__(self, ip_address=None, shield_name=None, sample_rate=None, log=True self.ssdp_attempts = ssdp_attempts self.streaming = False self.timeout = timeout + self.auto_connect = auto_connect + self.micro_volts = micro_volts + self.scaled_output = scaled_output # might be handy to know API self.board_type = "none" @@ -119,10 +125,14 @@ def __init__(self, ip_address=None, shield_name=None, sample_rate=None, log=True def on_shield_found(self, ip_address): self.ip_address = ip_address - self.connect() + # Disconnects from board when terminated atexit.register(self.disconnect) + if self.auto_connect: + time.sleep(.5) + self.connect() + def loop(self): asyncore.loop() @@ -185,7 +195,8 @@ def connect(self): self.daisy = False self.local_wifi_server.set_daisy(daisy=self.daisy) self.local_wifi_server.set_parser( - ParseRaw(gains=self.gains, board_type=self.board_type)) + ParseRaw(gains=self.gains, board_type=self.board_type, + micro_volts=self.micro_volts,scaled_output=self.scaled_output)) if self.high_speed: output_style = 'raw' @@ -202,6 +213,7 @@ def connect(self): if res_tcp_post.status_code == 200: tcp_status = res_tcp_post.json() if tcp_status['connected']: + self.set_sample_rate(self.sample_rate) if self.log: print("WiFi Shield to Python TCP Socket Established") else: diff --git a/scripts/stream_data_wifi_high_speed_no_auto_connect.py b/scripts/stream_data_wifi_high_speed_no_auto_connect.py new file mode 100644 index 0000000..48bdb6f --- /dev/null +++ b/scripts/stream_data_wifi_high_speed_no_auto_connect.py @@ -0,0 +1,46 @@ +from __future__ import print_function +import sys + +sys.path.append('..') # help python find cyton.py relative to scripts folder +from openbci import wifi as bci +import logging + + +def handle_streamed_data(sample): + print(sample.sample_number) + print(sample.channel_data) + + +if __name__ == '__main__': + + logging.basicConfig(filename="test.log", format='%(asctime)s - %(levelname)s : %(message)s', level=logging.DEBUG) + logging.info('---------LOG START-------------') + # If you don't know your IP Address, you can use shield name option + # If you know IP, such as with wifi direct 192.168.4.1, then use ip_address='192.168.4.1' + shield_name = 'OpenBCI-E218' + sample_rate = 500 + ip_address='192.168.4.1' + + shield_wifi= bci.OpenBCIWiFi(ip_address=ip_address, + shield_name=shield_name, + sample_rate=sample_rate, + timeout=15, + max_packets_to_skip=10, + latency=5000, + high_speed=True, + ssdp_attempts=20, + auto_connect=False, + micro_volts=True) + + print("WiFi Shield Instantiated") + time.sleep(1) + shield_wifi.connect() + print("WiFi Shield Connected with board") + time.sleep(1) + print("WiFi Shield streaming started...") + shield_wifi.start_streaming(handle_streamed_data) + shield_wifi.loop() + + # Note: do this when you're finished streaming: + # shield_wifi.stop() + # shield_wifi.disconnect()