Skip to content

Commit

Permalink
Update galea v4 emulator to control sampling rate
Browse files Browse the repository at this point in the history
  • Loading branch information
retiutut committed Oct 19, 2023
1 parent 3caf957 commit f8ff8c2
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions emulator/brainflow_emulator/galea_manual_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ def __init__(self):
self.local_ip = '127.0.0.1'
self.local_port = 2390
self.server_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
self.server_socket.settimeout(
0.1) # decreases sampling rate significantly because it will wait for recv 0.1 sec but it's just a test
self.server_socket.bind((self.local_ip, self.local_port))
self.state = State.wait.value
self.addr = None
self.package_num = 0
self.transaction_size = 12
self.package_size = 114
self.sampling_rate = 250
self.transaction_speed_250 = .048 - .003225 #.048 is the time it takes to send 12 packages at 250Hz, we have to adjust this with an arbitrary value
self.server_socket.settimeout(
.0000001) # decreases sampling rate significantly because it will wait for receive to timeout
self.total_packets_sent = 0
self.start_streaming_time = 0
self.DEBUG_MODE = False

def run(self):
start_time = time.time()
Expand All @@ -41,8 +46,11 @@ def run(self):
msg, self.addr = self.server_socket.recvfrom(128)
if msg == Message.start_stream.value:
self.state = State.stream.value
self.start_streaming_time = time.time()
elif msg == Message.stop_stream.value:
self.state = State.wait.value
self.total_packets_sent = 0
self.start_streaming_time
elif msg in Message.ack_values.value or msg.decode('utf-8').startswith('x'):
self.server_socket.sendto(Message.ack_from_device.value, self.addr)
elif msg == Message.time_calc_command.value:
Expand Down Expand Up @@ -89,9 +97,16 @@ def run(self):
for i in range(self.transaction_size):
package.extend(bytes(transaction[i]))
self.server_socket.sendto(bytes(package), self.addr)
self.total_packets_sent += self.transaction_size
except socket.timeout:
logging.info('timeout for send')

time.sleep(self.transaction_speed_250)

if (self.DEBUG_MODE):
elapsed_time = (time.time() - self.start_streaming_time)
sampling_rate = self.total_packets_sent / elapsed_time
print('elapsed time: ' + str(elapsed_time) + ' sampling rate: ' + str(sampling_rate))

def main():
emulator = GaleaEmulator()
Expand Down

0 comments on commit f8ff8c2

Please sign in to comment.