def main (): BoardShim.enable_board_logger () DataFilter.enable_data_logger () MLModel.enable_ml_logger () parser = argparse.ArgumentParser () # use docs to check which parameters are required for specific board, e.g. for Cyton - set serial port parser.add_argument ('--timeout', type = int, help = 'timeout for device discovery or connection', required = False, default = 0) parser.add_argument ('--ip-port', type = int, help = 'ip port', required = False, default = 0) parser.add_argument ('--ip-protocol', type = int, help = 'ip protocol, check IpProtocolType enum', required = False, default = 0) parser.add_argument ('--ip-address', type = str, help = 'ip address', required = False, default = '') parser.add_argument ('--serial-port', type = str, help = 'serial port', required = False, default = '') parser.add_argument ('--mac-address', type = str, help = 'mac address', required = False, default = '') parser.add_argument ('--other-info', type = str, help = 'other info', required = False, default = '') parser.add_argument ('--streamer-params', type = str, help = 'streamer params', required = False, default = '') parser.add_argument ('--serial-number', type = str, help = 'serial number', required = False, default = '') parser.add_argument ('--board-id', type = int, help = 'board id, check docs to get a list of supported boards', required = True) parser.add_argument ('--file', type = str, help = 'file', required = False, default = '') args = parser.parse_args () params = BrainFlowInputParams () params.ip_port = args.ip_port params.serial_port = args.serial_port params.mac_address = args.mac_address params.other_info = args.other_info params.serial_number = args.serial_number params.ip_address = args.ip_address params.ip_protocol = args.ip_protocol params.timeout = args.timeout params.file = args.file board = BoardShim (args.board_id, params) master_board_id = board.get_board_id () sampling_rate = BoardShim.get_sampling_rate (master_board_id) board.prepare_session () board.start_stream (45000, args.streamer_params) BoardShim.log_message (LogLevels.LEVEL_INFO.value, 'start sleeping in the main thread') time.sleep (5) # recommended window size for eeg metric calculation is at least 4 seconds, bigger is better data = board.get_board_data () board.stop_stream () board.release_session () eeg_channels = BoardShim.get_eeg_channels (int (master_board_id)) bands = DataFilter.get_avg_band_powers (data, eeg_channels, sampling_rate, True) feature_vector = np.concatenate ((bands[0], bands[1])) print(feature_vector) # calc concentration concentration_params = BrainFlowModelParams (BrainFlowMetrics.CONCENTRATION.value, BrainFlowClassifiers.KNN.value) concentration = MLModel (concentration_params) concentration.prepare () print ('Concentration: %f' % concentration.predict (feature_vector)) concentration.release () # calc relaxation relaxation_params = BrainFlowModelParams (BrainFlowMetrics.RELAXATION.value, BrainFlowClassifiers.REGRESSION.value) relaxation = MLModel (relaxation_params) relaxation.prepare () print ('Relaxation: %f' % relaxation.predict (feature_vector)) relaxation.release ()
def gen_EEG_wifi_LSL(lslname='eeg1A'): args = { "ip_port": 2190, "serial_port": "", "mac_address": "", "other_info": "", "serial_number": "", "ip_address": "192.168.4.1", #这个ip地址需要查看openbci板子的ip "ip_protocol": 2, "timeout": 0, "file": "", "log": True, "streamer_params": "", "board_id": -1, } args['board_id'] = 5 eegchannelsnums = 8 params = BrainFlowInputParams() params.ip_port = args['ip_port'] params.serial_port = args['serial_port'] params.mac_address = args['mac_address'] params.other_info = args['other_info'] params.serial_number = args['serial_number'] params.ip_address = args['ip_address'] params.ip_protocol = args['ip_protocol'] params.timeout = args['timeout'] params.file = args['file'] # self.str.set("ss" + self.eegserialport) if (args['log']): BoardShim.enable_dev_board_logger() else: BoardShim.disable_board_logger() eegboard = BoardShim(args['board_id'], params) eegboard.prepare_session() # eegboard.config_board('~6') sample = eegboard.get_sampling_rate(eegboard.get_board_id()) #print('sample:',sample) eegboard.start_stream(45000, "") lslinfo = StreamInfo(lslname, 'EEG', eegchannelsnums, sample, 'float32', 'brain01') lsloutlet = StreamOutlet(lslinfo) sendeegThead = SendData(eegboard, lsloutlet) sendeegThead.start()
def main(): BoardShim.enable_board_logger() DataFilter.enable_data_logger() MLModel.enable_ml_logger() params = BrainFlowInputParams() board = BoardShim(BoardIds.BRAINBIT_BOARD.value, params) master_board_id = board.get_board_id() sampling_rate = BoardShim.get_sampling_rate(master_board_id) board.prepare_session() board.start_stream(45000, '') eeg_channels = BoardShim.get_eeg_channels(int(master_board_id)) while True: BoardShim.log_message(LogLevels.LEVEL_INFO.value, 'start sleeping in the main thread') time.sleep( 5 ) # recommended window size for eeg metric calculation is at least 4 seconds, bigger is better data = board.get_board_data() bands = DataFilter.get_avg_band_powers(data, eeg_channels, sampling_rate, True) feature_vector = np.concatenate((bands[0], bands[1])) print(feature_vector) # calc concentration concentration_params = BrainFlowModelParams( BrainFlowMetrics.CONCENTRATION.value, BrainFlowClassifiers.KNN.value) concentration = MLModel(concentration_params) concentration.prepare() print('Concentration: %f' % concentration.predict(feature_vector)) concentration.release() # calc relaxation relaxation_params = BrainFlowModelParams( BrainFlowMetrics.RELAXATION.value, BrainFlowClassifiers.REGRESSION.value) relaxation = MLModel(relaxation_params) relaxation.prepare() print('Relaxation: %f' % relaxation.predict(feature_vector)) relaxation.release() board.stop_stream() board.release_session()
class OpenBCIInterface: def __init__(self, serial_port='COM5', board_id=0, log='store_true', streamer_params='', ring_buffer_size=45000): # default board_id 2 for Cyton params = BrainFlowInputParams() params.serial_port = serial_port params.ip_port = 0 params.mac_address = '' params.other_info = '' params.serial_number = '' params.ip_address = '' params.ip_protocol = 0 params.timeout = 0 params.file = '' self.streamer_params = streamer_params self.ring_buffer_size = ring_buffer_size self.board_id = board_id self.info_eeg = None self.info_aux = None self.outlet_eeg = None self.outlet_aux = None if (log): BoardShim.enable_dev_board_logger() else: BoardShim.disable_board_logger() self.board = BoardShim(board_id, params) def start_sensor(self): # tell the sensor to start sending frames self.board.prepare_session() print('OpenBCIInterface: connected to sensor') print(self.board.get_board_id()) try: self.board.start_stream(self.ring_buffer_size, self.streamer_params) self.infor_test() except brainflow.board_shim.BrainFlowError: print('OpenBCIInterface: Board is not ready.') def process_frames(self): # return one or more frames of the sensor frames = self.board.get_board_data() return frames def stop_sensor(self): try: self.board.stop_stream() print('OpenBCIInterface: stopped streaming.') self.board.release_session() print('OpenBCIInterface: released session.') except brainflow.board_shim.BrainFlowError as e: print(e) def create_lsl(self, name='OpenBCI_Cyton_8', type='EEG', channel_count=8, nominal_srate=250.0, channel_format='float32', source_id='Cyton_0'): self.info_eeg = StreamInfo(name=name, type=type, channel_count=channel_count, nominal_srate=nominal_srate, channel_format=channel_format, source_id='') chns = self.info_eeg.desc().append_child('channels') self.labels = ['Fp1', 'Fp2', 'C3', 'C4', 'T5', 'T6', 'O1', 'O2'] for label in self.labels: ch = chns.append_child("channel") ch.append_child_value('label', label) ch.append_child_value('unit', 'microvolts') ch.append_child_value('type', 'EEG') self.info_eeg.desc().append_child_value('manufacturer', 'OpenBCI Inc.') self.outlet_eeg = StreamOutlet(self.info_eeg) print("--------------------------------------\n" + \ "LSL Configuration: \n" + \ " Stream 1: \n" + \ " Name: " + name + " \n" + \ " Type: " + type + " \n" + \ " Channel Count: " + str(channel_count) + "\n" + \ " Sampling Rate: " + str(nominal_srate) + "\n" + \ " Channel Format: " + channel_format + " \n" + \ " Source Id: " + source_id + " \n") def push_sample(self, samples): self.outlet_eeg.push_sample(samples) def infor_test(self): print(self.board.get_eeg_names(self.board_id)) print(self.board.get_sampling_rate(self.board_id)) print(self.board.get_board_id()) print(self.board.get_package_num_channel(self.board_id)) print(self.board.get_timestamp_channel(self.board_id)) print(self.board.get_eeg_channels(self.board_id)) print(self.board.get_accel_channels(self.board_id)) print(self.board.get_marker_channel(self.board_id)) print(self.board.get_other_channels(self.board_id)) print(self.board.get_analog_channels(self.board_id)) print(self.board.get_other_channels(self.board_id))
class OpenBCILSLInterface: def __init__(self, stream_name='OpenBCI_Cyton_8', stream_type='EEG', serial_port='COM5', board_id="0", log='store_true', streamer_params='', ring_buffer_size=45000): # default board_id 2 for Cyton self.params = BrainFlowInputParams() self.params.serial_port = serial_port self.params.ip_port = 0 self.params.mac_address = '' self.params.other_info = '' self.params.serial_number = '' self.params.ip_address = '' self.params.ip_protocol = 0 self.params.timeout = 0 self.params.file = '' self.stream_name = stream_name self.stream_type = stream_type self.board_id = int(board_id) self.streamer_params = streamer_params self.ring_buffer_size = ring_buffer_size if (log): BoardShim.enable_dev_board_logger() else: BoardShim.disable_board_logger() try: self.board = BoardShim(self.board_id, self.params) self.info_print() except brainflow.board_shim.BrainFlowError: print('Cannot connect to board') def start_sensor(self): # tell the sensor to start sending frames try: self.board.prepare_session() except brainflow.board_shim.BrainFlowError: raise AssertionError('Unable to connect to device: please check the sensor connection or the COM port number in device preset.') print('OpenBCIInterface: connected to sensor') try: self.board.start_stream(self.ring_buffer_size, self.streamer_params) except brainflow.board_shim.BrainFlowError: raise AssertionError('Unable to connect to device: please check the sensor connection or the COM port number in device preset.') print('OpenBCIInterface: connected to sensor') self.create_lsl(name=self.stream_name, type=self.stream_type, nominal_srate=self.board.get_sampling_rate(self.board_id), channel_format='float32', source_id='Cyton_' + str(self.board_id)) def process_frames(self): # return one or more frames of the sensor frames = self.board.get_board_data() for frame in frames.T: self.push_frame(frame) # TODO: fine push chunk error # frames = np.transpose(frames) # tesst = [[rand() for chan_ix in range(24)] # for samp_ix in range(6)] # # frames = frames.astype('float32') # if frames.shape[0] > 0: # self.push_frame(samples=frames) return frames def stop_sensor(self): try: self.board.stop_stream() print('OpenBCIInterface: stopped streaming.') self.board.release_session() print('OpenBCIInterface: released session.') except brainflow.board_shim.BrainFlowError as e: print(e) def create_lsl(self, name='OpenBCI_Cyton_8', type='EEG', nominal_srate=250.0, channel_format='float32', source_id='Cyton_0'): channel_count = self.board.get_num_rows(self.board_id) self.info_eeg = StreamInfo(name=name, type=type, channel_count=channel_count, nominal_srate=nominal_srate, channel_format=channel_format, source_id=source_id) # chns = self.info_eeg.desc().append_child('channels') # # self.labels = ['Fp1', 'Fp2', 'C3', 'C4', 'T5', 'T6', 'O1', 'O2'] # # for label in self.labels: # ch = chns.append_child("channel") # ch.append_child_value('label', label) # ch.append_child_value('unit', 'microvolts') # ch.append_child_value('type', 'EEG') # # self.info_eeg.desc().append_child_value('manufacturer', 'OpenBCI Inc.') self.outlet_eeg = StreamOutlet(self.info_eeg) print("--------------------------------------\n" + \ "LSL Configuration: \n" + \ " Stream 1: \n" + \ " Name: " + name + " \n" + \ " Type: " + type + " \n" + \ " Channel Count: " + str(channel_count) + "\n" + \ " Sampling Rate: " + str(nominal_srate) + "\n" + \ " Channel Format: " + channel_format + " \n" + \ " Source Id: " + source_id + " \n") def push_frame(self, samples): self.outlet_eeg.push_sample(samples) def info_print(self): print("Board Information:") print("Sampling Rate:", self.board.get_sampling_rate(self.board_id)) print("Board Id:", self.board.get_board_id()) print("EEG names:", self.board.get_eeg_names(self.board_id)) print("Package Num Channel: ", self.board.get_package_num_channel(self.board_id)) print("EEG Channels:", self.board.get_eeg_channels(self.board_id)) print("Accel Channels: ", self.board.get_accel_channels(self.board_id)) print("Other Channels:", self.board.get_other_channels(self.board_id)) print("Analog Channels: ", self.board.get_analog_channels(self.board_id)) print("TimeStamp: ", self.board.get_timestamp_channel(self.board_id)) print("Marker Channel: ", self.board.get_marker_channel(self.board_id))