@@ -15,7 +15,7 @@ class State(enum.Enum):
1515class Message (enum .Enum ):
1616 start_stream = b'b'
1717 stop_stream = b's'
18- ack_values = (b'd' , b'~6' , b'~5' , b'o' , b'F0' )
18+ ack_values = (b'd' , b'~6' , b'~5' , b'~4' , b' o' , b'F0' )
1919 ack_from_device = b'A'
2020 time_calc_command = b'F4444444'
2121
@@ -34,11 +34,16 @@ def __init__(self):
3434 self .package_size = 114
3535 self .sampling_rate = 250
3636 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
37+ self .transaction_speed_500 = .024 - .00962005
38+ self .transaction_speed_1000 = .00000000001
39+ self .transaction_speed = self .transaction_speed_250
3740 self .server_socket .settimeout (
3841 .0000001 ) # decreases sampling rate significantly because it will wait for receive to timeout
3942 self .total_packets_sent = 0
4043 self .start_streaming_time = 0
41- self .debug_mode = False
44+ self .python_loop_rate_counter = 0
45+ self .python_loop_rate_timer = time .time ()
46+ self .debug_mode = True
4247 self .channel_on_off = [1 ] * 24
4348 self .channel_identifiers = array ('u' , [
4449 '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' ,
@@ -59,6 +64,9 @@ def run(self):
5964 self .state = State .wait .value
6065 self .total_packets_sent = 0
6166 self .start_streaming_time
67+ elif msg .decode ('utf-8' ).startswith ('~' ):
68+ self .server_socket .sendto (Message .ack_from_device .value , self .addr )
69+ self .process_sampling_rate (msg .decode ('utf-8' ))
6270 elif msg in Message .ack_values .value or msg .decode ('utf-8' ).startswith ('x' ):
6371 self .server_socket .sendto (Message .ack_from_device .value , self .addr )
6472 self .process_channel_on_off (msg .decode ('utf-8' ))
@@ -85,8 +93,7 @@ def run(self):
8593 sample = i - 4
8694 if (sample % 3 == 1 ):
8795 channel += 1
88- if (self .debug_mode ):
89- print (t , i , channel )
96+ #if (self.debug_mode) print(t, i, channel)
9097 if (self .channel_on_off [channel - 1 ] == 1 ):
9198 if (sample % 3 == 2 ):
9299 single_package .append (random .randint (0 , 8 + (channel * 2 )))
@@ -100,7 +107,7 @@ def run(self):
100107
101108 cur_time = time .time ()
102109 timestamp = bytearray (struct .pack ('d' , (cur_time - start_time ) * 1000 ))
103- eda = bytearray (struct .pack ('f' , random . random () ))
110+ eda = bytearray (struct .pack ('f' , .5 ))
104111 ppg_red = bytearray (struct .pack ('i' , int (random .random () * 5000 )))
105112 ppg_ir = bytearray (struct .pack ('i' , int (random .random () * 5000 )))
106113
@@ -127,11 +134,23 @@ def run(self):
127134 except socket .timeout :
128135 logging .info ('timeout for send' )
129136
130- time .sleep (self .transaction_speed_250 )
137+ time .sleep (self .transaction_speed )
131138
132139 if (self .debug_mode ):
133140 elapsed_time = (time .time () - self .start_streaming_time )
134141 sampling_rate = self .total_packets_sent / elapsed_time
142+
143+ self .python_loop_rate_counter += 1
144+ # Check elapsed time every second
145+ current_time = time .time ()
146+ loop_rate_elapsed_time = current_time - self .python_loop_rate_timer
147+
148+ if elapsed_time >= 1 :
149+ iterations_per_second = self .python_loop_rate_counter / loop_rate_elapsed_time
150+ print (f"Iterations per second: { iterations_per_second :.2f} " )
151+ self .python_loop_rate_counter = 0
152+ self .python_loop_rate_timer = current_time
153+
135154 print ('elapsed time: ' + str (elapsed_time ) + ' sampling rate: ' + str (sampling_rate ))
136155
137156 def process_channel_on_off (self , msg ):
@@ -146,6 +165,26 @@ def process_channel_on_off(self, msg):
146165 self .channel_on_off [channel_num ] = 0
147166 print ('channel ' + str (channel_num + 1 ) + ' is off' )
148167
168+ def process_sampling_rate (self , msg ):
169+ if (self .debug_mode ):
170+ print (msg )
171+ if (msg [1 ] == '6' ):
172+ self .sampling_rate = 250
173+ self .transaction_speed = self .transaction_speed_250
174+ print ('sampling rate is 250Hz' )
175+ elif (msg [1 ] == '5' ):
176+ self .sampling_rate = 500
177+ self .transaction_speed = self .transaction_speed_500
178+ print ('sampling rate is 500Hz' )
179+ elif (msg [1 ] == '4' ):
180+ self .sampling_rate = 1000
181+ self .transaction_speed = self .transaction_speed_1000
182+ self .server_socket .settimeout (
183+ .00000000001 )
184+ print ('sampling rate is 1000Hz' )
185+ else :
186+ print ('did not recognize sampling rate command' )
187+
149188def main ():
150189 emulator = GaleaEmulator ()
151190 emulator .run ()
0 commit comments