class Mapper_ROStoLSL(Mapper): def __init__(self, commonType, topic, channelInfo, cyclicMode, useLSLTypesBidirectional, includeLSLTimestamps): self.binarySubscriber = None self.lslStreamInfo = None super(Mapper_ROStoLSL, self).__init__(commonType, topic, channelInfo, cyclicMode, useLSLTypesBidirectional, includeLSLTimestamps) if useLSLTypesBidirectional is False and self.converter.rosStdType: self.subscriber = rospy.Subscriber(self.topic, self.converter.rosStdType, self.SubscriberCallback) else: self.subscriber = rospy.Subscriber(self.topic, self.converter.rosType, self.SubscriberCallback) self.publisher = StreamOutlet(self.lslStreamInfo) def SubscriberCallback(self, data): self.lastRosMsg = data if not self.cyclicMode: self.CollectData() self.UpdateData() def CollectData(self): self.lastCollectedRosMsg = self.lastRosMsg self.lastRosMsg = None def UpdateData(self): if self.lastCollectedRosMsg is not None and self.publisher is not None: self.publisher.push_sample(self.ToLSL(self.lastCollectedRosMsg)) self.lastCollectedRosMsg = None pass def __del__(self): self.lslStreamInfo.__del__() self.publisher.close_stream()
class bciBoardConnect(): def __init__(self): self.board = bci.OpenBCIBoard(port="/dev/ttyS0") self.eeg_channels = self.board.getNbEEGChannels() self.aux_channels = self.board.getNbAUXChannels() self.sample_rate = self.board.getSampleRate() self.save = False self.graphppg = [] self.graphecg = [] self.ecg = [] self.ppg = [] self.ecg30 = [] self.ppg30 = [] #setting channel 6 beg = 'x6000000X' #setting default and reseting board s = 'sv' s = s + 'd' #writing data to board s = s + 'F' for c in s: if sys.hexversion > 0x03000000: self.board.ser.write(bytes(c, 'utf-8')) else: self.board.ser.write(bytes(c)) time.sleep(0.100) #writing channel six data to board time.sleep(0.100) # for dat in beg: for x in beg: if sys.hexversion > 0x03000000: self.board.ser.write(bytes(x, 'utf-8')) else: self.board.ser.write(bytes(x)) time.sleep(0.100) self.ecg = [] self.ppg = [] self.ecgT = [] self.ppgT = [] #function to callback while board streaming data in and save it def send(self, sample): #print(sample.channel_data) self.ecg = (sample.channel_data[3]) self.ppg = (sample.channel_data[5]) if len(self.graphppg) < 1250: self.graphecg.append(sample.channel_data[3]) self.graphppg.append(sample.channel_data[5]) else: self.graphppg = [] self.graphecg = [] self.outlet_eeg.push_sample(sample.channel_data) self.outlet_aux.push_sample(sample.aux_data) if self.save: self.ecgT.append(sample.channel_data[3]) self.ppgT.append(sample.channel_data[5]) if len(self.ppg30) < 7500: self.ppg30.append(sample.channel_data[5]) self.ecg30.append(sample.channel_data[3]) else: self.ppg30 = [] self.ppg30 = [] def createlsl(self): info_eeg = StreamInfo("OpenBCI_EEG", 'EEG', self.eeg_channels, self.sample_rate, 'float32', "openbci_eeg_id1") info_aux = StreamInfo("OpenBCI_AUX", 'AUX', self.aux_channels, self.sample_rate, 'float32', "openbci_aux_id1") self.outlet_eeg = StreamOutlet(info_eeg) self.outlet_aux = StreamOutlet(info_aux) def clean(self): self.board.disconnect() self.outlet_eeg.close_stream() self.outlet_aux.close_stream() atexit.register(clean) def startstreaming(self): self.boardThread = threading.Thread(target=self.board.start_streaming, args=(self.send, -1)) self.boardThread.daemon = True try: self.boardThread.start() except: raise def stopstreaming(self): self.board.stop() time.sleep(.1) line = '' while self.board.ser.inWaiting(): c = self.board.ser.read().decode('utf-8', errors='replace') line += c time.sleep(0.001) if (c == '\n'): line = '' def retdataecgchunk(self): return self.graphecg def retdatappgchunk(self): return self.graphppg def retbothdata(self): return self.ecg, self.ppg def retecgT(self): return self.ecgT def retppgT(self): return self.ppgT def flush(self): self.ppgT = [] self.ecgT = [] def chSave(self): if self.save == False: self.save = True else: self.save = False def ret30(self): return self.ecg30, self.ppg30