|
| 1 | +function c = checkValveCalibration(channelName, volumes) |
| 2 | +% HW.CHECKVALVECALIBRATION Check two volumes along calibration plot |
| 3 | +% This function is used to plot the measured volumes at two points along |
| 4 | +% the last recorded calibration curve. This can be used to easily check |
| 5 | +% whether the calibration is still accurate. |
| 6 | +% |
| 7 | +% Inputs (Optional): |
| 8 | +% channelName (char): The name of the reward valve channel to check. |
| 9 | +% The channel name must be associated with a hw.RewardValveControl |
| 10 | +% object in the rig's hw.DaqController object. Default 'rewardValve'. |
| 11 | +% volumes (numerical): A 2-element array of volumes to check. Default |
| 12 | +% is to pick two evenly spaced volumes within the calibration range. |
| 13 | +% |
| 14 | +% Ouput: |
| 15 | +% c (struct): The measured volumes corresponding to the opening times. |
| 16 | +% |
| 17 | +% Examples: |
| 18 | +% % Plot two measured volumes again the previous calibration data |
| 19 | +% hw.checkRewardValveCalibration(); |
| 20 | +% |
| 21 | +% % Check by how much the deliveries have changed for some given volumes |
| 22 | +% volumes = [2 3]; % Check 2 and 3ul deliveries |
| 23 | +% c = hw.checkRewardValveCalibration('rewardValve', volumes); |
| 24 | +% dV = diff([c.volumeMicroLitres; volumes]) |
| 25 | +% |
| 26 | +% See also hw.calibrate, hw.RewardValveControl |
| 27 | + |
| 28 | +% The default channel name of the reward controller |
| 29 | +if nargin < 1, channelName = 'rewardValve'; end |
| 30 | +rig = hw.devices; % Load the hardware file |
| 31 | + |
| 32 | +% Fetch the reward control signal generator object |
| 33 | +rewardId = strcmp(rig.daqController.ChannelNames, channelName); |
| 34 | +signalGen = rig.daqController.SignalGenerators(rewardID); |
| 35 | +% Fetch the most recent calibration |
| 36 | +[newestDate, I] = max([signalGen.Calibrations.dateTime]); |
| 37 | +lastCalibration = signalGen(rewardId).Calibrations(I); |
| 38 | +ul = [lastCalibration.volumeMicroLitres]; % Recorded volumes |
| 39 | +dt = [lastCalibration.durationSecs]; % Previous opening times |
| 40 | + |
| 41 | +if nargin > 1 |
| 42 | + % User provided two specific volumes to test |
| 43 | + assert(isnumeric(volumes) && numel(volumes) == 2, ... |
| 44 | + 'Rigbox:hw:checkCalibration:volumesIncorrect', ... |
| 45 | + 'volumes must be a two element numerical array') |
| 46 | + % Interpolate previous calibration data to find opening times |
| 47 | + durations = arrayfun(@(x)interp1(ul, dt, x, 'pchip'), volumes); |
| 48 | +else |
| 49 | + % Otherwise pick two equally spaced points within the calibration range |
| 50 | + durations = pick(linspace(dt(1), dt(end), 4), 2:3); |
| 51 | +end |
| 52 | + |
| 53 | +% Run a quick calibration |
| 54 | +c = hw.calibrate(channelName, rig.daqController, rig.scale, ... |
| 55 | + durations(1), ... % Min opening time |
| 56 | + durations(2), ... % Max opening time |
| 57 | + 'settleWait', 1, ... % Set to 1 to trim test time |
| 58 | + 'nPerT', 1, ... % Once per opening time |
| 59 | + 'nVolumes', 2, ... % Pick the min and max opening times |
| 60 | + 'delivPerSample', 200); % 100 fewer than usual |
| 61 | + |
| 62 | +% Plot result over the previous calibration result |
| 63 | +figure('Color', 'w'); |
| 64 | +plot(dt, ul, 'x-'); |
| 65 | +hold on |
| 66 | +plot([c.durationSecs], [c.volumeMicroLitres], 'o'); |
| 67 | + |
| 68 | +% Set some labels, etc. |
| 69 | +xlabel('Duration (sec)'); |
| 70 | +ylabel('Volume (\muL)'); |
| 71 | +legend(["Previous calibration", "Measured deliveries"], 'Location', 'SouthEast') |
| 72 | +title(datestr(newestDate)) |
0 commit comments