private bool TuneTorqueLimiter(string filename, SymbolCollection symbols, int peakTorque, int peakHP, bool autoUpdateChecksum)
{
bool retval = true;
//Increase driverwish to peakIQ if it is not already (only the rightmost 2 columns)
int tlAddress = (int)Tools.Instance.GetSymbolAddressLike(Tools.Instance.m_symbols, "Torque limiter");
if (tlAddress > 0)
{
foreach (SymbolHelper sh in Tools.Instance.m_symbols)
{
if (sh.Flash_start_address == tlAddress)
{
byte[] tldata = Tools.Instance.readdatafromfile(filename, tlAddress, sh.Length, EDCFileType.EDC15P);
// Console.WriteLine(sh.Varname + " " + sh.Y_axis_length.ToString() + " " + sh.X_axis_length.ToString());
int[] rpms = Tools.Instance.readdatafromfileasint(filename, sh.Y_axis_address, sh.Y_axis_length, EDCFileType.EDC15P);
for (int rows = 0; rows < sh.X_axis_length; rows++)
{
for (int cols = 0; cols < sh.Y_axis_length; cols++)
{
// whats the rpm?
int rpm = rpms[cols];
int targetTorque = peakTorque;
int targetPower = Tools.Instance.TorqueToPower(targetTorque, rpm);
if (targetPower > peakHP) targetTorque = Tools.Instance.PowerToTorque(peakHP, rpm);
int targetIQ = Tools.Instance.TorqueToIQ(targetTorque, rpm, 4);
int maxIQ = targetIQ;
if (rpm < 550) maxIQ = 0;
if (rpm > 5000) maxIQ = 0;
if (cols == 0) maxIQ = 0;
if (cols == sh.Y_axis_length - 1) maxIQ = 0;
if (rpm >= 550 && rpm < 1000) maxIQ = 30;
if (rpm >= 1000 && rpm < 1250) maxIQ = 45;
if (rpm >= 1250 && rpm < 1500) maxIQ = 55;
if (rpm >= 1500 && rpm < 1750) maxIQ = 60;
if (rpm >= 1750 && rpm < 1900) maxIQ = 65;
if (targetIQ > maxIQ) targetIQ = maxIQ;
//Console.WriteLine("Calculate for rpm: " + rpm.ToString() + " targetT: " + targetTorque.ToString() + " targetP: " + targetPower.ToString() + " targetIQ: " + targetIQ.ToString());
targetIQ *= 100;
byte b1 = (byte)((targetIQ & 0x00FF00) / 256);
byte b2 = (byte)(targetIQ & 0x0000FF);
tldata[rows * sh.Y_axis_length * 2 + cols * 2] = b1;
tldata[rows * sh.Y_axis_length * 2 + (cols * 2) + 1] = b2;
}
}
SaveAndSyncData(tldata.Length, (int)sh.Flash_start_address, tldata, filename, true, "Tuned torque limiter", autoUpdateChecksum);
}
}
}
else retval = false;
return retval;
}