From e1c7bd7fcbbc851687cf4e03631db0656c60e7af Mon Sep 17 00:00:00 2001 From: DJ2LS <75909252+DJ2LS@users.noreply.github.com> Date: Thu, 18 Mar 2021 11:39:12 +0100 Subject: [PATCH] tests with unsync to prevent sync 10 trigger --- data_handler.py | 17 ++++++++++++----- helpers.py | 28 ++++++++++++++++++++++++++++ modem.py | 29 ++++++++++++++++++++++------- sock.py | 4 ++-- static.py | 5 +++-- 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/data_handler.py b/data_handler.py index a9fe3e4b..8e55d353 100644 --- a/data_handler.py +++ b/data_handler.py @@ -112,6 +112,7 @@ def arq_data_received(data_in): # TRANSMIT ACK FRAME FOR BURST----------------------------------------------- modem.transmit_signalling(ack_frame) + while static.CHANNEL_STATE == 'SENDING_SIGNALLING': time.sleep(0.01) static.CHANNEL_STATE = 'RECEIVING_DATA' @@ -212,6 +213,7 @@ def arq_data_received(data_in): logging.info("ARQ | TX | ARQ DATA FRAME ACK [" + str(static.FRAME_CRC.hex()) + "] [BER." + str(static.BER) + "]") modem.transmit_signalling(ack_frame) + while static.CHANNEL_STATE == 'SENDING_SIGNALLING': time.sleep(0.01) @@ -289,7 +291,7 @@ def arq_transmit(data_out): for static.TX_N_RETRIES in range(static.TX_N_MAX_RETRIES): if static.ARQ_N_SENT_FRAMES + 1 <= static.TX_BUFFER_SIZE: - logging.log(24, "ARQ | TX | M:" + str(static.ARQ_DATA_CHANNEL_MODE) + " | F:[" + str(static.ARQ_N_SENT_FRAMES + 1) + "-" + str(static.ARQ_N_SENT_FRAMES + static.ARQ_TX_N_FRAMES_PER_BURST) + "] | T:[" + str(static.ARQ_N_SENT_FRAMES) + "/" + str(static.TX_BUFFER_SIZE) + "] [" + str(int(static.ARQ_N_SENT_FRAMES / (static.TX_BUFFER_SIZE) * 100)).zfill(3) + "%] | A:[" + str(static.TX_N_RETRIES + 1) + "/" + str(static.TX_N_MAX_RETRIES) + "] [BER." + str(static.BER) + "]") + logging.log(24, "ARQ | TX | M:" + str(static.ARQ_DATA_CHANNEL_MODE) + " | F:[" + str(static.ARQ_N_SENT_FRAMES + 1) + "-" + str(static.ARQ_N_SENT_FRAMES + static.ARQ_TX_N_FRAMES_PER_BURST) + "] | T:[" + str(static.ARQ_N_SENT_FRAMES) + "/" + str(static.TX_BUFFER_SIZE) + "] [" + str(int(static.ARQ_N_SENT_FRAMES / (static.TX_BUFFER_SIZE) * 100)).zfill(3) + "%] | A:[" + str(static.TX_N_RETRIES + 1) + "/" + str(static.TX_N_MAX_RETRIES) + "]") modem.transmit_arq_burst() @@ -364,7 +366,8 @@ def arq_transmit(data_out): # --------------- BREAK LOOP IF ACK HAS BEEN RECEIVED elif static.ARQ_ACK_RECEIVED == True: - logging.info("ARQ | RX | ACK") + transfer_rates = helpers.calculate_transfer_rate() + logging.info("ARQ | RX | ACK [" + str(transfer_rates[2]) + " bit/s | " + str(transfer_rates[3]) + " B/min]") acktimer.cancel() # -----------IF ACK RECEIVED, INCREMENT ITERATOR FOR MAIN LOOP TO PROCEED WITH NEXT FRAMES/BURST static.ARQ_N_SENT_FRAMES = static.ARQ_N_SENT_FRAMES + static.ARQ_TX_N_FRAMES_PER_BURST @@ -438,8 +441,8 @@ def arq_transmit(data_out): # IF TX BUFFER IS EMPTY / ALL FRAMES HAVE BEEN SENT --> HERE WE COULD ADD AN static.VAR for IDLE STATE transfer_rates = helpers.calculate_transfer_rate() - print(str(transfer_rates[0]) + " bit/s") - print(str(transfer_rates[1]) + " B/min") + logging.info("RATE (DATA/ACK) :[" + str(transfer_rates[0]) + " bit/s | " + str(transfer_rates[1]) + " B/min]") + logging.info("ARQ | TX | BUFFER EMPTY") helpers.arq_reset_frame_machine() # await asyncio.sleep(2) @@ -461,7 +464,10 @@ def get_n_frames_per_burst(): def get_best_mode_for_transmission(): - return 12 + + mode = 12 + + return mode @@ -537,6 +543,7 @@ def arq_received_data_channel_opener(data_in): connection_frame[12:13] = bytes([static.ARQ_DATA_CHANNEL_MODE]) + modem.transmit_signalling(connection_frame) modem.transmit_signalling(connection_frame) while static.CHANNEL_STATE == 'SENDING_SIGNALLING': time.sleep(0.01) diff --git a/helpers.py b/helpers.py index c33a2151..7f16da0a 100644 --- a/helpers.py +++ b/helpers.py @@ -157,6 +157,34 @@ def arq_reset_frame_machine(): static.CHANNEL_STATE = 'RECEIVING_SIGNALLING' static.ARQ_READY_FOR_DATA = False + static.ARQ_START_OF_TRANSMISSION = 0 + +def calculate_transfer_rate(): + + if static.ARQ_TX_N_TOTAL_ARQ_FRAMES == 0: + total_n_frames = static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME + elif static.ARQ_N_ARQ_FRAMES_PER_DATA_FRAME == 0: + total_n_frames = int.from_bytes(static.ARQ_TX_N_TOTAL_ARQ_FRAMES, "big") + + + total_bytes = (total_n_frames * static.ARQ_PAYLOAD_PER_FRAME) + total_transmission_time = time.time() - static.ARQ_START_OF_TRANSMISSION + + burst_bytes = static.ARQ_PAYLOAD_PER_FRAME + burst_transmission_time = time.time() - static.ARQ_START_OF_BURST + + static.ARQ_BITS_PER_SECOND_TRANSMISSION = int((total_bytes * 8) / total_transmission_time) + static.ARQ_BYTES_PER_MINUTE_TRANSMISSION = int(((total_bytes) / total_transmission_time) * 60) + + static.ARQ_BITS_PER_SECOND_BURST = int((burst_bytes * 8) / burst_transmission_time) + static.ARQ_BYTES_PER_MINUTE_BURST = int(((burst_bytes) / burst_transmission_time) * 60) + + + return [static.ARQ_BITS_PER_SECOND_TRANSMISSION, static.ARQ_BYTES_PER_MINUTE_TRANSMISSION, static.ARQ_BITS_PER_SECOND_BURST, static.ARQ_BYTES_PER_MINUTE_BURST] + + + + def add_to_heard_stations(dxcallsign, datatype): # check if buffer empty diff --git a/modem.py b/modem.py index c2f932a8..23e3a750 100644 --- a/modem.py +++ b/modem.py @@ -43,7 +43,7 @@ class RF(): rate=static.AUDIO_SAMPLE_RATE_RX, frames_per_buffer=static.AUDIO_FRAMES_PER_BUFFER, input=True, - input_device_index=static.AUDIO_INPUT_DEVICE, + input_device_index=static.AUDIO_INPUT_DEVICE ) # --------------------------------------------OPEN AUDIO CHANNEL TX self.stream_tx = self.p.open(format=pyaudio.paInt16, @@ -98,6 +98,10 @@ class RF(): self.my_rig.set_ptt(self.hamlib_ptt_type, 0) + + + + # -------------------------------------------------------------------------------------------------------- def play_audio(self): @@ -178,6 +182,7 @@ class RF(): # we could place this timing part inside the modem... # lets see if this is a good idea.. static.ARQ_DATA_CHANNEL_LAST_RECEIVED = int(time.time()) # we need to update our timeout timestamp + static.ARQ_START_OF_BURST = int(time.time()) # we need to update our timeout timestamp self.my_rig.set_ptt(self.hamlib_ptt_type, 1) @@ -330,13 +335,18 @@ class RF(): stuck_in_sync_10_counter = 0 # + + # here we do an unsync to be sure, the modem is in idle state and ready for new data - self.c_lib.freedv_set_sync(freedv, 0) + # tests are showing, that this causes sync 10 triggers. So we should definitely not do this here! + #self.c_lib.freedv_set_sync(freedv, 0) # here we do a buffer cleanup before returning to demod loop - #for i in range(0, 10): + # tests are showing, that this causes sync 10 triggers. So we should definitely not do this here! + #for i in range(0, 3): # dummy_mod = bytes(self.c_lib.freedv_nin(freedv)) # self.c_lib.freedv_rawdatarx(freedv, bytes_out, dummy_mod) + # #self.stream_rx.read(10, exception_on_overflow=False) # demod loop @@ -349,7 +359,11 @@ class RF(): nin = self.c_lib.freedv_nin(freedv) #nin = int(nin*(static.AUDIO_SAMPLE_RATE_RX/static.MODEM_SAMPLE_RATE)) + data_in = self.stream_rx.read(nin, exception_on_overflow=False) + + + static.AUDIO_RMS = audioop.rms(data_in, 2) # self.c_lib.freedv_rawdatarx.argtype = [ctypes.POINTER(ctypes.c_ubyte), data_bytes_out, data_in] # check if really neccessary nbytes = self.c_lib.freedv_rawdatarx(freedv, bytes_out, data_in) # demodulate audio @@ -360,8 +374,9 @@ class RF(): stuck_in_sync_counter += 1 if self.c_lib.freedv_get_rx_status(freedv) == 10: stuck_in_sync_10_counter += 1 - #self.c_lib.freedv_set_sync(freedv, 0) - logging.warning("MODEM | SYNC 10 TRIGGER | M:" + str(mode) + " | " + str(static.CHANNEL_STATE)) + if mode != 14: + self.c_lib.freedv_set_sync(freedv, 0) + logging.warning("MODEM | SYNC 10 TRIGGER | M:" + str(mode) + " | " + str(static.CHANNEL_STATE)) if stuck_in_sync_counter == 33 and self.c_lib.freedv_get_rx_status(freedv) == 10: logging.critical("MODEM | stuck in sync #1") @@ -480,7 +495,7 @@ class RF(): if frame == n_frames_per_burst: logging.debug("LAST FRAME ---> UNSYNC") self.c_lib.freedv_set_sync(freedv, 0) # FORCE UNSYNC - for i in range(0, 10): + for i in range(0, 3): dummy_mod = bytes(self.c_lib.freedv_nin(freedv)) self.c_lib.freedv_rawdatarx(freedv, bytes_out, dummy_mod) @@ -491,7 +506,7 @@ class RF(): if mode == 14: self.c_lib.freedv_set_sync(freedv, 0) - for i in range(0, 10): + for i in range(0, 3): dummy_mod = bytes(self.c_lib.freedv_nin(freedv)) self.c_lib.freedv_rawdatarx(freedv, bytes_out, dummy_mod) else: diff --git a/sock.py b/sock.py index b3df0e7c..289f28c7 100644 --- a/sock.py +++ b/sock.py @@ -89,8 +89,8 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler): asyncio.run(data_handler.arq_connect()) # ARQ DISCONNECT FROM CALLSIGN ---------------------------------------- - if received_json["command"] == "ARQ:DISCONNECT": - asyncio.run(data_handler.arq_disconnect()) + #if received_json["command"] == "ARQ:DISCONNECT": + # asyncio.run(data_handler.arq_disconnect()) if received_json["command"] == "ARQ:OPEN_DATA_CHANNEL": # and static.ARQ_STATE == 'CONNECTED': diff --git a/static.py b/static.py index 3aab5b05..4fd938c9 100644 --- a/static.py +++ b/static.py @@ -73,7 +73,7 @@ AUDIO_OUTPUT_DEVICE = 1 #AUDIO_SAMPLE_RATE_RX = 44100 #AUDIO_SAMPLE_RATE_TX = 44100 MODEM_SAMPLE_RATE = 8000 # 8000 -AUDIO_FRAMES_PER_BUFFER = 2048 +AUDIO_FRAMES_PER_BUFFER = 2048 # 256 # 512 # 1024 #2048 --> nicht 880 # 128 gut, 256, 1024 AUDIO_CHANNELS = 1 AUDIO_RMS = 0 # --------------------------------- @@ -107,7 +107,7 @@ ARQ_ACK_PAYLOAD_PER_FRAME = 0 # PAYLOAD per ACK frame ARQ_ACK_RECEIVED = False # set to 1 if ACK received ARQ_RX_ACK_TIMEOUT = False # set to 1 if timeut reached -ARQ_RX_ACK_TIMEOUT_SECONDS = 10.0 # timeout for waiting for ACK frames +ARQ_RX_ACK_TIMEOUT_SECONDS = 7.0 # timeout for waiting for ACK frames ARQ_FRAME_ACK_RECEIVED = False # set to 1 if FRAME ACK received ARQ_RX_FRAME_TIMEOUT = False @@ -156,6 +156,7 @@ ARQ_DATA_CHANNEL_LAST_RECEIVED = 0 # BIT RATE MESSUREMENT ARQ_START_OF_TRANSMISSION = 0 +ARQ_START_OF_BURST = 0 #ARQ_END_OF_TRANSMISSION = 0 ARQ_BITS_PER_SECOND = 0 ARQ_BYTES_PER_MINUTE = 0