def read_config_data(self, file_handle, legacy=False): """ Read in configuration data based on platform. """ config = dict.fromkeys(CONFIG_MSG_FORMAT.keys()) if legacy: config_msg = file_handle.read(44) config['node_id'] = np.frombuffer(config_msg[4:8], dtype='>u4')[0] config['scan_start'] = np.frombuffer(config_msg[8:12], dtype='>i4')[0] config['scan_stop'] = np.frombuffer(config_msg[12:16], dtype='>i4')[0] config['scan_res'] = np.frombuffer(config_msg[16:18], dtype='>u2')[0] config['pii'] = np.frombuffer(config_msg[18:20], dtype='>u2')[0] config['ant_mode'] = np.uint16(config_msg[32]) config['tx_gain_ind'] = np.uint16(config_msg[33]) config['code_channel'] = np.uint16(config_msg[34]) config['persist_flag'] = np.uint16(config_msg[35]) else: config_msg = file_handle.read(32) byte_counter = 0 for config_field in CONFIG_MSG_FORMAT.keys(): num_bytes = CONFIG_MSG_FORMAT[config_field].itemsize config_data = config_msg[byte_counter:(byte_counter + num_bytes)] config[config_field] = np.frombuffer(config_data, dtype=CONFIG_MSG_FORMAT[config_field])[0] config[config_field] = config[config_field].byteswap() byte_counter += num_bytes return config
def set_radar_config(self): """ Set radar configuration based on user settings. """ # Make sure radar is connected self.update_status("Setting radar configuration...") if self.connected: # Determine desired configuration from user settings self.settings_to_config() # Scan resolution; API states that any value aside from 32 will # likely cause undesired behavior so overwrite it if self.config['scan_res'] != REC_SCAN_RES: self.update_status("Overriding scan resolution with " + "recommended value...") self.config['scan_res'] = REC_SCAN_RES # Configuration persistence flag if self.config['persist_flag'] != PERSIST_FLAG: self.update_status("WARNING: Configuration persistence flag " + "not set to recommended value...") # Initialize message to set new configuration message = MRM_SET_CONFIG_REQUEST + MESSAGE_ID # Add all configuration values to message for config_field in CONFIG_MSG_FORMAT.keys(): num_bytes = CONFIG_MSG_FORMAT[config_field].itemsize message += self.value_to_message(self.config[config_field], num_bytes) # Send configuration to radar self.connection['sock'].sendto(message, (self.connection['udp_ip_radar'], self.connection['udp_port'])) # Check for configuration set confirmation from radar while True: try: data, addr = self.connection['sock'].recvfrom( MAX_PACKET_SIZE) succ_flag = int(data[4:8].encode("hex"), 16) if succ_flag != 0: self.update_status("Error code " + str(succ_flag) + " encountered while setting " + "radar configuration!") exit() else: self.update_status("Successfully set radar " + "configuration!") break except: pass # Get and return the latest radar configuration return self.get_radar_config() else: self.update_status("Radar not connected!") exit()
def read_scan_data(self, save_file_name=None, num_packets=None): """ Read data returned from radar scans. """ # Create save file if save_file_name is not None: save_file = open(save_file_name, "wb") # Add all configuration values to save file config = "" for config_field in CONFIG_MSG_FORMAT.keys(): num_bytes = CONFIG_MSG_FORMAT[config_field].itemsize config += self.value_to_message(self.config[config_field], num_bytes) save_file.write(config) # Read fixed length or streaming data off radar self.update_status("Reading data from the radar...") packet_count = 0 while True: try: data, addr = self.connection['sock'].recvfrom(MAX_PACKET_SIZE) if save_file_name is not None: save_file.write(data) packet_count += 1 # Read the specified number of packets if num_packets is not None: if packet_count == num_packets: break # Read until stop flag has been posted to the control file else: stop_flag = self.control_file.read() if stop_flag != "0": break self.control_file.seek(0) except: pass # Read any remaining streaming radar data start = time.time() while (time.time() - start) < STREAM_TIMEOUT: try: data, addr = self.connection['sock'].recvfrom(MAX_PACKET_SIZE) if save_file_name is not None: save_file.write(data) except: pass self.update_status("Successfully read all the data!") # Close save file if save_file_name is not None: save_file.close()
def __init__(self, udp_ip_host="192.168.1.1", udp_ip_radar="192.168.1.151", udp_port=21210, verbose=False): """ Instance initialization. """ # Radar status indicators self.connected = False self.collecting = False # Radar system parameters self.N_bin = [] # Number of bins in scan # Connection settings self.connection = { 'udp_ip_host': udp_ip_host, # Host (computer) IP address 'udp_ip_radar': udp_ip_radar, # Radar IP address 'udp_port': udp_port, # Radar port 'sock': []} # UDP socket # User radar settings; partially higher abstraction than the radar's # internal configuration; initialize to defaults; bounds are inclusive self.settings = { 'dT_0': # Path delay through antennas (ns) {'value': DT_0, 'bounds': (0, float("inf"))}, 'range_start': # Start range (m) {'value': RANGE_START, 'bounds':(0, float("inf"))}, 'range_stop': # Stop range (m) {'value': RANGE_STOP, 'bounds': (0, float("inf"))}, 'tx_gain_ind': # Transmit gain index {'value': TX_GAIN_IND, 'bounds': (0, 63)}, 'pii': # Pulse integration index {'value': PII, 'bounds': (6, 15)}, 'code_channel': # Code channel {'value': CODE_CHANNEL, 'bounds': (0, 10)}, 'node_id': # Node ID {'value': NODE_ID, 'bounds': (1, 2**32 - 2)}, 'persist_flag': # Persist flag {'value': PERSIST_FLAG, 'bounds': (0, 1)}} # Radar internal configuration self.config = dict.fromkeys(CONFIG_MSG_FORMAT.keys()) # Control and status file handles self.status_file = open(STATUS_FILE_NAME, "w") self.control_file = [] # Miscellaneous self.verbose = verbose
def get_radar_config(self): """ Get configuration from radar. """ self.update_status("Requesting radar configuration...") # Make sure radar is connected if self.connected: # Request the current radar configuration message = MRM_GET_CONFIG_REQUEST + MESSAGE_ID self.connection['sock'].sendto( message, (self.connection['udp_ip_radar'], self.connection['udp_port'])) # Parse the configuration from the received packet if available while True: try: data, addr = self.connection['sock'].recvfrom( MAX_PACKET_SIZE) break except: pass byte_counter = 4 config_status = "Received radar configuration!\n" for config_field in CONFIG_MSG_FORMAT.keys(): num_bytes = CONFIG_MSG_FORMAT[config_field].itemsize config_data = data[byte_counter:(byte_counter + num_bytes)] hex_rep = config_data.encode("hex") int_rep = int(hex_rep, 16) self.config[config_field] = ( CONFIG_MSG_FORMAT[config_field].type(int_rep)) config_status += ('\t' + config_field + ": " + str(self.config[config_field]) + "\n") byte_counter += num_bytes # Return received configuration in case needed self.update_status(config_status[:-1]) return data else: self.update_status("Radar not connected!\n") exit()