Skip to content

Commit 1873afb

Browse files
committed
Merge remote-tracking branch 'origin/checkCalibration' into dev
2 parents 98c3959 + d5dc9d7 commit 1873afb

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

+hw/calibrate.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
function calibration = calibrate(channel, rewardController, scales, tMin, tMax, varargin)
22
%HW.CALIBRATE Performs measured reward deliveries for calibration
33
% This function is used by srv.expServer to return a water calibration.
4-
% It still requires some scales to be attached to the computer.
4+
% It still requires some scales to be attached to the computer. The
5+
% resulting calibration data are returned and also appended to the
6+
% rewardController 'Calibrations' property.
57
%
68
% Inputs:
79
% channel (char) - the name of the channel to use. Must match one of
@@ -26,6 +28,10 @@
2628
% settleWait (double) - time in seconds to wait between delivering
2729
% sample and recording a new weight. Gives the scale reading time to
2830
% stabalize. Default 2 seconds.
31+
%
32+
% Output:
33+
% calibration (struct): An 1xnVolumes structure with the fields
34+
% 'durationSecs' and 'volumeMicroLitres'.
2935
%
3036
% TODO: Sanitize and integrate into HW.REWARDVALVECONTROL
3137
%

cortexlab/+hw/checkCalibration.m

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)