% Funzione che converte i dati grezzi del RSN Link HR in dati di
% angoli (acc_RSNHR)

function [angRSNHR,T_RSNHR] = conv_grezziRSNHR(angRSNHR,tempRSNHR,DCalRSNHRTot,rRSNHR,FileName)

text = 'conv_grezziRSNHR function started';
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);

caX_RSNHR = DCalRSNHRTot(:,1);
intX_RSNHR = DCalRSNHRTot(:,2);
caY_RSNHR = DCalRSNHRTot(:,3);
intY_RSNHR = DCalRSNHRTot(:,4);
caT_RSNHR = DCalRSNHRTot(:,5);
intT_RSNHR = DCalRSNHRTot(:,6);

cont = 1;
cn = 1; 
% Contatore dei nodi, converte i punti ADC con le calibrazioni in valori
% angolari
for ii=1:2*rRSNHR
    if cont==1
       angRSNHR(:,ii) = angRSNHR(:,ii)*caX_RSNHR(cn)+ intX_RSNHR(cn);
       cont = cont+1;
    elseif cont==2
        angRSNHR(:,ii) = angRSNHR(:,ii)*caY_RSNHR(cn)+ intY_RSNHR(cn);
        cont = cont+1;
    else
        cont = 1;
        cn = cn+1;
    end
end

%% Conversione delle temperature
[rT,~] = size(tempRSNHR);
T_RSNHR = zeros(rT,1);

for t = 1:rRSNHR
    T_RSNHR(:,t) = tempRSNHR(:,t)*caT_RSNHR(t,1) + intT_RSNHR(t,1);
end

text = 'Raw Data of RSN Link HR converted into physycal units correctly. conv_grezziRSNHR function ended';
fprintf(fileID,fmt,text);
fclose(fileID);

end