clearvars
format shortEng

freq = 50;
T = 1/freq;

percents = [5.17 5.5:0.5:10] % corresponding to the measurements

% experimental RPM measurement with HTI HT522 Tachometer
rpms = [5530, 8900, 12200, 13800, 14500, 15200, 15600, 15800, 16200, 16700, 17700]

pulseWidths = percents*T/100
fittedPoly = polyfit(pulseWidths, rpms, 3)

theoPercents = 5.17:1e-3:10;
theoPulseWidths = theoPercents*T/100
theoRpms = polyval(fittedPoly, theoPulseWidths);
figure()
hold on
grid on
grid minor
plot(pulseWidths*1e6, rpms, "x-")
plot(theoPulseWidths*1e6, theoRpms)
xlabel("Pulse Width (\mus)")
ylabel("Motor RPM")
legend("Experimental", "Fitted", Location="southeast")
hold off
saveas(gcf, "graph_us.png")

figure()
hold on
grid on
grid minor
plot(percents, rpms, "x-")
plot(theoPercents, theoRpms)
xlabel("Pulse Width (%)")
ylabel("Motor RPM")
legend("Experimental", "Fitted", Location="southeast")
hold off
saveas(gcf, "graph_percent.png")

%% Expected RPM at maximum using KV
motorKV = 2300;
appliedvoltage = 7.5;
expectedMaxRPM = motorKV*appliedvoltage