Skip to content

(1) Scale factor bug fix in parser according to https://docs.openbci.… #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion openbci/utils/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 15 additions & 3 deletions openbci/wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def handle_sample(sample):
import re
import socket
import timeit
import time

try:
import urllib2
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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'
Expand All @@ -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:
Expand Down
46 changes: 46 additions & 0 deletions scripts/stream_data_wifi_high_speed_no_auto_connect.py
Original file line number Diff line number Diff line change
@@ -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()