def get_avg_sensor(probe, delay=3, sensor='Sensor1'): """ Take a probe measurement of 3 seconds and return the mean of that data. Args: probe: radicl.probe.RAD_Probe object delay: Measurement time in seconds sensor: String name of the column of the data to avg Returns: probe_data: Mean of the Raw Sensor over the delay time period """ log = get_logger(__name__) # Start the probe probe.startMeasurement() log.info("\tProbe measurement started...") time.sleep(delay) # Stop probe probe.stopMeasurement() log.info("Measurement Stopped.") # Probe data extraction and average probe_data = np.array((probe.readRawSensorData())[sensor]) probe_data = probe_data[probe_data < 4096].mean() # Start the reset in another thread probe.resetMeasurement() # If we like the data if probe_data in [None, 65535]: log.error("Probe data invalid! Trying again") return int(probe_data)
def __init__(self, ext_api=None, debug=False): """ Args: ext_api: rad_api.RAD_API object preinstantiated """ self.log = get_logger(__name__, debug=debug) # Check if an external API object was passed in. if ext_api is not None: # An external API object was provided. Use it. Note: We assume here # that the appropriate initialization, identification, and API port # enable procedure has already taken place self.api = ext_api else: # No external API oject was provided. Create new serial and API # objects for internal use port = rs.RAD_Serial(debug=debug) port.openPort() if not port: self.log.info("No device present") else: port.flushPort() # Create the API and FMTR instances The API class is linked to # the port object api = RAD_API(port, debug=debug) # Switch the device over to API mode api.sendApiPortEnable() self.api = api # Delay a bit and then identify the attached device time.sleep(0.5) ret = api.Identify() if ret == 0: self.log.error("Unable to connect to the probe. Unplug and" " power cycle it.") sys.exit() time.sleep(0.5) # Manages the settings settings_funcs = inspect.getmembers(self.api, predicate=inspect.ismethod) ignores = ['reset'] self.settings = parse_func_list(settings_funcs, ['Meas', 'Set'], ignore_keywords=ignores) self.getters = parse_func_list(settings_funcs, ['Meas', 'Get'], ignore_keywords=ignores)
is done using the `getSetting` and `setSetting` functions using the setting name 'calibdata' which is short hand for calibration data. Usage: 1. plug in the probe. 2. Open a terminal 3. python get_calibrated_data.py """ # Import the radicl probe class from radicl.probe import RAD_Probe # Import the colored logging from radicl to report more human readable info from radicl.ui_tools import get_logger # Instantiate the interface probe = RAD_Probe() # Start this scripts logging log = get_logger(__name__, debug=True) # Loop through each sensor and retrieve the calibration data log.info("Retrieving the calibration values for each sensor...") for sensor in range(1, 5): # Grab setting data d = probe.getSetting(setting_name='calibdata', sensor=sensor) # Report data without decimals log.info("Sensor {}: LOW = {:0.0f}, HIGH = {:0.0f}\n".format( sensor, d[0], d[1]))
def test_build_high_resolution_data(cli, data_name, expected_data): log = get_logger('test_high_res') df = build_high_resolution_data(cli, log) np.testing.assert_array_equal(df[data_name].values, np.array(expected_data))
def main(): # Parse command line arguments hdr = 'Lyte Probe High Resolution DAQ Script v{}'.format(__version__) underline = '=' * len(hdr) hdr = '\n'.join([hdr, underline, '']) help_string = ( '\nThis script is used to take high resolution hand driven measurements with' ' the Lyte probe. This script requires: \n' '* A USB connection to the probe\n' '* The user to press the probe button for start and stop\n\n' 'To accurately take High resolution measurements this script automatically' ' downloads the following timeseries from the probe per measurement:\n' '* Hardness\n' '* Active NIR\n' '* Passive NIR\n' '* Depth\n' '* Acceleration in the line of pole\n\n' 'NOTE: The depth and accelerometer timeseries are all recorded at a lower' ' sampling rate than the sensors in the tip so they are interpolated to' ' match the Hardness and NIR timeseries') p = argparse.ArgumentParser(description='\n'.join([hdr, help_string]), formatter_class=RawTextHelpFormatter) p.add_argument('-d', '--debug', dest='debug', action='store_true', help="Debug flag will print out much more info") p.add_argument('-a', '--plot_all', dest='all', action='store_true', help="When used will plot all datasets, otherwise it will " " just plot the depth corrected data.") p.add_argument('--version', action='version', version=('%(prog)s v{version}').format(version=__version__)) args = p.parse_args() print(hdr) # Manage logging # Start this scripts logging log = get_logger("RAD Hi-Res Script", debug=args.debug) finished = False log.info("Starting High Resolution DAQ Script") # Retrieve a connection to the probe cli = RADICL() # Keep count of measurements taken i = 0 # Reset the probe in the event the probe was closed out without reset response = cli.probe.resetMeasurement() # Loop through each sensor and retrieve the calibration data while not finished: # take a measurement input("\nPress enter to start listening for the probe to start...\n") print("Press probe button to start...") cli.listen_for_a_reading() ts = build_high_resolution_data(cli, log) plot_hi_res(df=ts) # ouptut the data to a datetime file cli.write_probe_data(ts, filename='') response = cli.probe.resetMeasurement() i += 1 log.info("{} measurements taken this session".format(i))
def Upgrade(fw_image): app_header_version = 0 app_header_length = 0 app_header_entry = 0 app_header_crc = 0 counter = 0 log = get_logger(__name__) port = rs.RAD_Serial() try: port.openPort() except Exception as e: log.error(e) if port.serial_port is None: log.info("No device present") else: port.flushPort() # Create the API api = RAD_API(port) # The API class is linked to the port object api.sendApiPortEnable() # Delay a bit and then identify the attached device time.sleep(0.5) ret = api.Identify() # Prior to FW revision 1.45, only 16 byte packages were possible. With FW revision 1.45 and above, the package size can be increased up to # 64 byes. Although it is technically possible to send up to 256 bytes in a package, there is a bug in the USB transport layer that causes # issues when transfering more than 64 bytes at a time. Until this is # resolved, 64 bytes is the max. if api.FWRev() >= 1.45: transfer_size = 64 else: transfer_size = 16 # The FW Update class is linked to the API object fw = FW_Update(api, transfer_size) # If the identification succeeded, carry on with the normal operation ret = 1 if ret: # The device was successfully identified. # NORMAL OPERATION ret = fw.loadFile(fw_image) if ret == 1: ret = fw.upgrade() if ret == 1: # FW upgrade succeeded. Close the port, wait a bit, and # then attempt to read the new FW version port.closePort() log.info("***** CHECK NEW FW VERSION ON PROBE *****") log.info("Wait for probe to finish internal upgrade") num_attempt = 1 while num_attempt < 4: log.info("Attempt %d" % num_attempt) num_attempt += 1 num_delay = 10 while num_delay > 0: log.info( "\rAttempting to reconnect in %d seconds " % num_delay) num_delay -= 1 time.sleep(1) # Attempt to reconnect and read the FW version log.info("\n") try: port.openPort() except Exception as e: log.error(e) port.closePort() if port.serial_port is None: log.info("No device found.") port.closePort() else: port.flushPort() # Create the API # The API class is linked to the port object api = RAD_API(port) fw = FW_Update( api, 64 ) # The FW Update class is linked to the API object api.sendApiPortEnable() # Delay a bit and then identify the attached device time.sleep(0.5) ret = api.Identify() if ret == 1: log.info("Device successfully identified") log.info( "FW UPDATE COMPLETE AND VERIFIED. EXITING..." ) port.closePort() exit() # If we get here then we have failed all retries and not # probe is present log.info("##### NO PROBE FOUND #####") else: log.error("Unable to load file. Exiting") fw.closeFile() port.closePort()
def __init__(self, port, debug=False): self.port = port self.log = get_logger(__name__, debug=debug)