-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturntableCalibrate.m
More file actions
91 lines (76 loc) · 3.42 KB
/
turntableCalibrate.m
File metadata and controls
91 lines (76 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
% turntableCalibrate Turntable calibration
%
% turntableCalibrate() calculates certain values for the turntable, e.g.
% what is the voltage when the step sensor ticks to the next angle, or
% how many voltage-up reading we get when going from one angle to the next.
% TODO: This function currently works, but its output is not used by the
% other functions, so there is no difference if you run it or not.
%
% Author: Enzo De Sena
% Date 6/2/2024
function turntableCalibrate()
global turntableController;
if isempty(turntableController)
error('Looks like there is no turntable controller in the workspace. Please call turntableConnect');
end
function runStepCalibration()
global turntableController;
clockwiseGroupLength = runStepCalibrationDirection('clockwise');
counterclockwiseGroupLength = runStepCalibrationDirection('counterclockwise');
turntableController.averageStepSensorGroupLength = (clockwiseGroupLength+counterclockwiseGroupLength)/2;
disp(['Average length of groups above threshold: ', num2str(turntableController.averageStepSensorGroupLength)]);
function averageSensorGroupLength = runStepCalibrationDirection(direction)
global turntableController;
turntablePrivateStart(direction);
N = 400;
voltage = nan(1, N);
for n=1:N
voltage(n) = readVoltage(turntableController.arduinoObj, turntableController.arduinoStepPin);
end
turntablePrivateStop(turntableController);
stem(1:N, voltage);
xlabel('Measurement index')
ylabel('Step sensor [V]')
averageSensorGroupLength = calculateAverageLengthOfGroups(voltage, turntableController.stepVoltageThreshold);
function runZeroCalibration()
global turntableController;
turntableToZero('clockwise');
turntableTick('clockwise');
N = 400;
voltage = nan(1, N);
currentDirection = 'counterclockwise';
turntablePrivateStart(currentDirection);
nLastAboveThreshold = nan;
for n=1:N
voltage(n) = readVoltage(turntableController.arduinoObj, turntableController.arduinoZeroPin);
if voltage(n) > turntableController.zeroVoltageThreshold
nLastAboveThreshold = n;
end
if (n-nLastAboveThreshold) > 30
nLastAboveThreshold = nan;
turntablePrivateStop(turntableController);
if strcmp(currentDirection, 'counterclockwise')
currentDirection = 'clockwise';
else
currentDirection = 'counterclockwise';
end
turntablePrivateStart(currentDirection);
end
end
turntablePrivateStop();
stem(1:N, voltage);
xlabel('Measurement index')
ylabel('Zero sensor [V]')
turntableController.averageZeroSensorGroupLength = calculateAverageLengthOfGroups(voltage, turntableController.zeroVoltageThreshold);
disp(['Average length of groups above threshold: ', num2str(turntableController.averageZeroSensorGroupLength)]);
function averageLength = calculateAverageLengthOfGroups(signal, threshold)
% Find where signal is above the threshold
aboveThreshold = signal > threshold;
% Find the start and end of each group
diffAboveThreshold = diff([0, aboveThreshold, 0]);
starts = find(diffAboveThreshold == 1);
ends = find(diffAboveThreshold == -1) - 1;
% Calculate the lengths of the groups
groupLengths = ends - starts + 1;
% Calculate the average length of the groups
averageLength = mean(groupLengths);