Search notes

Search titles and full post content.

    ← All notes

    DT series board for DT9828 signal collection

    MatLab Code

    The following MATLAB code is specifically used for DT9828 boards.

    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
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    % DT9828 Instant Data Collection
    % Created by YX in Melbourne, on 2024.10.9

    clear; close all;
    daqreset;
    daqlist;

    % Global variables for data and timestamps
    global data1 timestamps1 data2 timestamps2 data3 timestamps3 data4 timestamps4

    % Create a new folder for saving data
    nameFolder = newFolderCreate();
    %% Device Setup
    deviceName = {'DT9828(00)', 'DT9828(01)'};
    pName = {'DAQ1';'DAQ2'};
    cName = {'0---7';'0---7'};
    ppName = {'0---7';'0---7'};

    samplingTime = 20;
    samplingRate = 25;

    jChan = 1;
    for iDevice = 1:length(deviceName)
    info = table(string(deviceName{iDevice}), string(pName{iDevice}), string(cName{iDevice}), string(ppName{iDevice}), ...
    'VariableNames', {'Device Name', 'DAQ Board', 'Channel No.', 'Thermocouple Label'});
    writetable(info, fullfile(nameFolder, [deviceName{iDevice} '.txt']), 'Delimiter', '\t');

    devID = [deviceName{iDevice}(1:6), deviceName{iDevice}(8:9)];
    ai.(devID) = daq('dt');
    for iChan = 0:7
    ch(jChan) = addinput(ai.(devID), deviceName{iDevice}, num2str(iChan), 'Voltage');
    pause(0.2);
    if strcmp(deviceName{iDevice}, 'DT9806(00)') && iChan == 0
    ch(jChan).Range = [-1, 1];
    else
    ch(jChan).Range = [-0.02, 0.02];
    end
    jChan = jChan + 1;
    end
    samplingRate = 25;
    ai.(devID).Rate = samplingRate;
    fprintf('\n Sampling rate is %.f\n', samplingRate);
    end

    %% Data Acquisition

    if contains(deviceName, 'DT9832(00)')
    ai.DT983200 = daq('dt'); % thermistor calibration
    addinput(ai.DT983200, 'DT9832(00)', '3', 'Voltage');
    ai.DT983200.Rate = samplingRate;
    end
    for iDevice = 1:length(deviceName)
    devID = [deviceName{iDevice}(1:6), deviceName{iDevice}(8:9)];
    fName = [devID, 'tcData', num2str(iCali)];
    fComplete = fullfile(nameFolder, fName);
    if isfile([fComplete '.dat'])
    % If it exists, delete the old file
    delete([fComplete '.dat']);
    end
    ai.(devID).ScansAvailableFcn = @(src, evt) logData(src, evt, fComplete);
    flush(ai.(devID));
    start(ai.(devID), 'Duration', seconds(samplingTime));
    end

    % Wait for data collection
    kk = 0;
    col = [0 0.4470 0.7410;
    0.8500 0.3250 0.0980;
    0.9290 0.6940 0.1250;
    0.4940 0.1840 0.5560;
    0.4660 0.6740 0.1880;
    0.3010 0.7450 0.9330;
    0.6350 0.0780 0.1840;
    1 0 1 ;
    0 1 1 ;
    ];
    sym = 'sov^h';

    hFig = figure;ax(1) = axes('box','on');hold(ax(1),'on')
    set(ax,'ylim',[0 50]);
    xlabel(ax,'$t$ (s)','Interpreter','LaTex');
    ylabel(ax,'$T$ (C)','Interpreter','LaTex');

    while ai.(devID).Running
    pause(0.5)
    fprintf('\n Running %d s \n');
    % Function to calculate mean data and time
    calculateMean = @(data, timestamps) deal(mean(data, 1), mean(timestamps));
    % Define data and timestamp variables
    dataSets = {data1, data2, data3, data4};
    timeSets = {timestamps1, timestamps2, timestamps3, timestamps4};
    meanData = cell(1, length(dataSets));
    meanTime = cell(1, length(timeSets));

    % Calculate means for each data set
    for i = 1:length(dataSets)
    if ~isempty(dataSets{i}) && ~isempty(timeSets{i})
    [meanData{i}, meanTime{i}] = calculateMean(dataSets{i}, timeSets{i});
    else
    [meanData{i}, meanTime{i}] = deal(NaN(1, 8), NaN);
    end
    end

    % Plotting
    for iData = 1:8
    for jData = 1:length(meanData)
    if ~isnan(meanTime{jData}) && ~isnan(meanData{jData}(iData))
    % Use MarkerFaceColor for datasets 2, 3, and 4
    plot(meanTime{jData}, meanData{jData}(iData), sym(jData), 'Color', col(iData,:), ...
    'linewidth', 1, 'MarkerFaceColor', col(iData,:), 'MarkerSize', 4);
    meanTime{jData}
    end
    end
    end
    drawnow()
    kk = kk + 1;
    end

    % Plot results
    hFig = figure;ax(1) = axes;hold(ax(1),'on')
    for iDevice = 1:length(deviceName)
    devID = [deviceName{iDevice}(1:6), deviceName{iDevice}(8:9)];
    fName = [devID, 'tcData', num2str(iCali)];
    fComplete = fullfile(nameFolder, fName);
    alldata = load([fComplete, '.dat']);
    insTC = alldata(:, 2:end);
    plot(alldata(:, 1), insTC, 'linewidth', 1.2);
    hold on;
    end
    set(ax,'ylim',[0 50]);
    xlabel('$t$ (s)', 'Interpreter', 'Latex');
    ylabel('T (C)', 'Interpreter', 'Latex');



    %% Helper Functions

    function nameFolder = newFolderCreate
    nFolder = clock;
    nameFolder = strcat(num2str(nFolder(2)), num2str(nFolder(3)));
    if ~isfolder(nameFolder)
    mkdir(nameFolder);
    end
    end

    function logData(src, ~, fname)
    global data1 timestamps1 data2 timestamps2 data3 timestamps3 data4 timestamps4
    % Read the available data
    [data, timestamps, ~] = read(src, src.ScansAvailableFcnCount, "OutputFormat", "Matrix");

    if contains(src.Channels(1,1).Device.ID,'DT9828(00)')
    % Combine timestamps and data into a matrix
    data1 = data; timestamps1 = timestamps;
    elseif contains(src.Channels(1,1).Device.ID,'DT9828(01)')
    data2 = data; timestamps2 = timestamps;
    elseif contains(src.Channels(1,1).Device.ID,'DT9828(02)')
    data3 = data; timestamps3 = timestamps;
    elseif contains(src.Channels(1,1).Device.ID,'DT9828(03)')
    data4 = data; timestamps4 = timestamps;
    end

    alldata = [timestamps, data];
    % Save data to a MAT-file
    %writematrix([fname1 '.mat'], '-mat', 'alldata');
    % fwrite(fid,data,'double');
    writematrix(alldata,[fname '.dat'],'WriteMode','append')
    end