import requests import random import time import requests import serial # Function to get the current ADC overload count print('=========================') def get_current_ADC_OV(): response = requests.get('http://192.168.22.239:8073/status') # Check the status code if response.status_code == 200: # Split the response lines by newline character lines = response.text.split('\n') # Iterate through each line for line in lines: # Split the line by equals sign to get parameter name and value parts = line.split('=') # Check if the first part is 'adc_ov' if parts[0] == 'adc_ov': # Assign the value to the variable adc_ov = int(parts[1]) break # Break the loop after finding the value return adc_ov else: print("Error while getting response from the server:", response.status_code) # Function to set the attenuation level of the attenuator def set_attn(level): # Your code to set the attenuation level of the attenuator goes here ser = serial.Serial('/dev/ttyUSB0', 115200) data1 = "attn:" + str(float(level)) ser.write(data1.encode()) response = ser.readline() decoded_response = response.decode('utf-8') ser.close() # For demonstration purposes, we simply print the level print(f"Set attn: {level}" + " dB" +f" ADC ov: {my_adc_count}") # print(f"ADC ov: {my_adc_count}") # Variable to store the previous ADC overload count sleap_time = 2 my_adc_count = 0 previous_count = get_current_ADC_OV() previous_attenuation_level = -1 # Array of constants defining how much to attenuate based on the overload count attenuation_constants = [ (1, 0.5), # Attenuate by 0.5 when there's one overload (30, 1), # Attenuate by 2 when there are 30 overloads (500, 4) # Attenuate by 7 when there are 500 overloads ] # Main control function def control_algorithm(): global sleap_time global my_adc_count global previous_count # Declare previous_count as a global variable global previous_attenuation_level # Declare previous_attenuation_level as a global variable # Initial attenuation level of the attenuator attenuation_level = 0 set_attn(0) # Infinite loop for continuous monitoring and adjustment while True: # Get the current value of the ADC overload count current_count = get_current_ADC_OV() # Calculate the difference between current and previous count values overload_count = current_count - previous_count my_adc_count = overload_count # Update the value of the previous count previous_count = current_count # Determine the attenuation value of the attenuator based on the overload count attenuation_constant = 0 for count, constant in attenuation_constants: if overload_count >= count: attenuation_constant = constant else: break # If the overload count is greater than specified in the array, use the next attenuation value for i in range(len(attenuation_constants)): if overload_count > attenuation_constants[i][0]: attenuation_constant = attenuation_constants[i + 1][1] if i + 1 < len(attenuation_constants) else attenuation_constant else: break # If there are no overloads, decrease the attenuation level by 0.5 if overload_count == 0: attenuation_constant = -0.5 # Increase the attenuation level by the constant value attenuation_level += attenuation_constant # Ensure that the attenuation level is within the range of 0 to 31.5 attenuation_level = min(max(attenuation_level, 0), 31.5) # Check if the attenuation level has changed if attenuation_level != previous_attenuation_level: # Set the new attenuation level of the attenuator set_attn(attenuation_level) # Update the value of the previous attenuation level previous_attenuation_level = attenuation_level # Wait for some time before the next iteration (here 1 second for example) # In a real implementation, this delay can be adjusted according to the system requirements # If there are no overloads, wait for 15 seconds, if there are overloads, wait for 2 seconds if my_adc_count != 0: sleap_time = 2 # Wait for 5 seconds before the next iteration else: sleap_time = 15 print(f" Wait: {sleap_time}" + " sec") time.sleep(sleap_time) # Run the main control function control_algorithm()