/* public byte[] GetBatteryCorrectionMap(float[] correctionFactors)
        {
            byte[] retval = new byte[22];
            int bcount = 0;
            for (int i = 0; i < 11; i++)
            {
                float val = GetBatteryCorrection(i) / 0.004F;
                int ival = Convert.ToInt32(val);
                byte b1 = (byte)(ival / 256);
                byte b2 = (byte)(ival - (int)b1 * 256);
                retval[bcount++] = b1;
                retval[bcount++] = b2;
            }
            return retval;

        }*/
        private void SetInjectorBatteryCorrectionMap(IECUFile m_file, InjectorType injectorType)
        {
            byte[] batt_korr_tab = new byte[22]; // 11 values, first one is for 15 volt
            float tempvalue = 0;
            switch (injectorType)
            {
                case InjectorType.Stock:
                case InjectorType.Siemens875Dekas:
                case InjectorType.Siemens1000cc:
                    batt_korr_tab.SetValue((byte)0x00, 0); // 15 volt = 0.59
                    batt_korr_tab.SetValue((byte)0x93, 1);
                    batt_korr_tab.SetValue((byte)0x00, 2); // 14 volt = 0.77
                    batt_korr_tab.SetValue((byte)0xC0, 3);
                    batt_korr_tab.SetValue((byte)0x00, 4); // 13 volt = 0.78
                    batt_korr_tab.SetValue((byte)0xC3, 5);
                    batt_korr_tab.SetValue((byte)0x00, 6); // 12 volt = 0.94
                    batt_korr_tab.SetValue((byte)0xEB, 7);
                    batt_korr_tab.SetValue((byte)0x01, 8); // 11 volt = 1.28
                    batt_korr_tab.SetValue((byte)0x40, 9);
                    batt_korr_tab.SetValue((byte)0x01, 10); // 10 volt = 1.50
                    batt_korr_tab.SetValue((byte)0x77, 11);
                    batt_korr_tab.SetValue((byte)0x01, 12); // 9 volt = 1.85
                    batt_korr_tab.SetValue((byte)0xCE, 13);
                    batt_korr_tab.SetValue((byte)0x02, 14); // 8 volt = 2.32
                    batt_korr_tab.SetValue((byte)0x44, 15);
                    batt_korr_tab.SetValue((byte)0x03, 16); // 7 volt = 3.73
                    batt_korr_tab.SetValue((byte)0xA4, 17);
                    batt_korr_tab.SetValue((byte)0x03, 18); // 6 volt = 3.73
                    batt_korr_tab.SetValue((byte)0xA4, 19);
                    batt_korr_tab.SetValue((byte)0x03, 20); // 5 volt = 3.73
                    batt_korr_tab.SetValue((byte)0xA4, 21);
                    break;
                case InjectorType.GreenGiants:
                    batt_korr_tab.SetValue((byte)0x00, 0); // 15 volt = 0.894
                    batt_korr_tab.SetValue((byte)0xDF, 1);
                    batt_korr_tab.SetValue((byte)0x00, 2); // 14 volt = 1.003
                    batt_korr_tab.SetValue((byte)0xFA, 3);
                    batt_korr_tab.SetValue((byte)0x01, 4); // 13 volt = 1.15
                    batt_korr_tab.SetValue((byte)0x1F, 5);
                    batt_korr_tab.SetValue((byte)0x01, 6); // 12 volt = 1.308
                    batt_korr_tab.SetValue((byte)0x47, 7);
                    batt_korr_tab.SetValue((byte)0x01, 8); // 11 volt = 1.521
                    batt_korr_tab.SetValue((byte)0x7C, 9);
                    batt_korr_tab.SetValue((byte)0x01, 10); // 10 volt = 1.768
                    batt_korr_tab.SetValue((byte)0xBA, 11);
                    batt_korr_tab.SetValue((byte)0x02, 12); // 9 volt = 2.102
                    batt_korr_tab.SetValue((byte)0x0D, 13);
                    batt_korr_tab.SetValue((byte)0x02, 14); // 8 volt = 2.545
                    batt_korr_tab.SetValue((byte)0x7C, 15);
                    batt_korr_tab.SetValue((byte)0x03, 16); // 7 volt = 3.216
                    batt_korr_tab.SetValue((byte)0x24, 17);
                    batt_korr_tab.SetValue((byte)0x04, 18); // 6 volt = 4.142
                    batt_korr_tab.SetValue((byte)0x0B, 19);
                    batt_korr_tab.SetValue((byte)0x05, 20); // 5 volt = 5.45
                    batt_korr_tab.SetValue((byte)0x52, 21);
                    break;
                case InjectorType.Siemens630Dekas:
                    batt_korr_tab.SetValue((byte)0x00, 0); // 15 volt = 0.33
                    batt_korr_tab.SetValue((byte)0x52, 1);
                    batt_korr_tab.SetValue((byte)0x00, 2); // 14 volt = 0.433
                    batt_korr_tab.SetValue((byte)0x6C, 3);
                    batt_korr_tab.SetValue((byte)0x00, 4); // 13 volt = 0.548
                    batt_korr_tab.SetValue((byte)0x89, 5);
                    batt_korr_tab.SetValue((byte)0x00, 6); // 12 volt = 0.673
                    batt_korr_tab.SetValue((byte)0xA8, 7);
                    batt_korr_tab.SetValue((byte)0x00, 8); // 11 volt = 0.802
                    batt_korr_tab.SetValue((byte)0xC8, 9);
                    batt_korr_tab.SetValue((byte)0x00, 10); // 10 volt = 0.974
                    batt_korr_tab.SetValue((byte)0xF3, 11);
                    batt_korr_tab.SetValue((byte)0x01, 12); // 9 volt = 1.208
                    batt_korr_tab.SetValue((byte)0x2E, 13);
                    batt_korr_tab.SetValue((byte)0x01, 14); // 8 volt = 1.524
                    batt_korr_tab.SetValue((byte)0x7D, 15);
                    batt_korr_tab.SetValue((byte)0x01, 16); // 7 volt = 2.023
                    batt_korr_tab.SetValue((byte)0xF9, 17);
                    batt_korr_tab.SetValue((byte)0x02, 18); // 6 volt = 2.74
                    batt_korr_tab.SetValue((byte)0xAD, 19);
                    batt_korr_tab.SetValue((byte)0x03, 20); // 5 volt = 3.6
                    batt_korr_tab.SetValue((byte)0x84, 21);
                    break;

            }
            // write to batt_korr_tab
            m_file.WriteData(batt_korr_tab, (uint)m_file.GetFileInfo().GetSymbolAddressFlash("Batt_korr_tab!"));
        }
        public TuningResult FreeTuneBinary(IECUFile m_File, double peakTorque, double peakBoost, bool tuneBasedOnTorque, MapSensorType mapType, TurboType turboType, InjectorType injectorType, BPCType valve, int rpmlimiter, int knockTime)
        {
            Trionic5Resume _localResume = new Trionic5Resume();
            m_fileInformation = m_File.GetFileInfo();
            string filename = m_fileInformation.Filename;
            // first set things right by running the tunetostagex wizard
            // generate a nice x_scale for ignition map (18 long)
            PressureToTorque ptt = new PressureToTorque();
            double peak_boost = ptt.CalculatePressureFromTorque(peakTorque, turboType);
            if (!tuneBasedOnTorque) peak_boost = peakBoost;
            double peak_boost_request = peak_boost;
            double correction = 1.0;
            if (mapType == MapSensorType.MapSensor30) correction = 1.2;
            if (mapType == MapSensorType.MapSensor35) correction = 1.4;
            if (mapType == MapSensorType.MapSensor40) correction = 1.6;
            if (mapType == MapSensorType.MapSensor50) correction = 2.0;
            peak_boost_request *= 100;
            peak_boost_request += 100;
            peak_boost_request /= correction;
            peak_boost_request -= 100;
            peak_boost_request /= 100;

            double min_pressure = -1;
            double max_pressure = peak_boost;

            #region preparation

            /********* start of prepare phase *********/
            string enginetp = readenginetype(filename);
            string partnumber = readpartnumber(filename);
            PartNumberConverter pnc = new PartNumberConverter();
            ECUInformation ecuinfo = pnc.GetECUInfo(partnumber, enginetp);
            bool isLpt = true;
            if (ReadTunedToStageMarker(filename) > 0)
            {
                return TuningResult.TuningFailedAlreadyTuned;
            }
            else if (ReadThreeBarConversionMarker(filename) > 0)
            {
                return TuningResult.TuningFailedThreebarSensor;
            }
            Trionic5Properties t5p = m_File.GetTrionicProperties();
            if (ecuinfo.Valid)
            {
                if (ecuinfo.Isaero || ecuinfo.Isfpt)
                {
                    isLpt = false;
                }
            }

            if (t5p.MapSensorType != mapType)
            {
                ConvertFileToThreeBarMapSensor(m_fileInformation, t5p.MapSensorType, mapType);
            }
            // check injector type
            if (t5p.InjectorType != injectorType)
            {
                int inj_konst_diff = DetermineDifferenceInInjectorConstant(t5p.InjectorType, injectorType);
                AddToInjectorConstant(filename, inj_konst_diff);
                SetInjectorBatteryCorrectionMap(m_File, injectorType); //TODO: check this function for correctness!
            }
            /*if (injectorType == InjectorType.Stock) writebyteinfile(filename, GetSymbolAddress("Inj_konst!"), 19);
            else if (injectorType == InjectorType.GreenGiants) writebyteinfile(filename, GetSymbolAddress("Inj_konst!"), 18);
            else if (injectorType == InjectorType.Siemens630Dekas) writebyteinfile(filename, GetSymbolAddress("Inj_konst!"), 15);
            else if (injectorType == InjectorType.Siemens875Dekas) writebyteinfile(filename, GetSymbolAddress("Inj_konst!"), 13);
            else if (injectorType == InjectorType.Siemens875Dekas) writebyteinfile(filename, GetSymbolAddress("Inj_konst!"), 13);*/
            t5p.TurboType = turboType;
            t5p.InjectorType = injectorType;
            t5p.MapSensorType = mapType;
            // determine stage??
            int stage = 0;
            if (peak_boost < 1.2) stage = 1;
            else if (peak_boost < 1.3) stage = 2;
            else if (peak_boost < 1.4) stage = 3;
            else if (peak_boost < 1.5) stage = 4;
            else if (peak_boost < 1.6) stage = 5;
            else if (peak_boost < 1.7) stage = 6;
            else if (peak_boost < 1.8) stage = 7;
            else if (peak_boost < 1.9) stage = 8;
            else stage = 9;

            m_File.SetTrionicOptions(t5p);
            TuneToStage(filename, stage, peak_boost_request, 0.52, 1.0, 0.52, 1.54, 90, isLpt, turboType, injectorType, mapType);
            _localResume.ResumeTuning = m_resume.ResumeTuning.Copy();
            /*********** end of prepare phase **************/

            // set limiter, bpc valve type and knock time
            SetBPCValveType(filename, valve);
            _localResume.AddToResumeTable("Set BPC driving frequencies");
            SetRPMLimiter(filename, rpmlimiter);
            _localResume.AddToResumeTable("Set RPM limiter");
            SetKnockTime(filename, knockTime);
            _localResume.AddToResumeTable("Set knock time value");

            #endregion

            // if mapsensor != stock and injectors are 630 cc or bigger
            if (mapType != MapSensorType.MapSensor25 && (injectorType == InjectorType.Siemens630Dekas || injectorType == InjectorType.Siemens875Dekas || injectorType == InjectorType.Siemens1000cc))
            {
                // now scale it
                double step = (max_pressure - min_pressure) / 17;
                double[] axisforIgnitionMap = new double[18];
                for (int i = 0; i < 18; i++)
                {
                    axisforIgnitionMap.SetValue(min_pressure + (i * step), i);
                }
                byte[] actualAxis = new byte[36];
                int j = 0;
                for (int i = 0; i < 18; i++)
                {
                    double currValue = Convert.ToDouble(axisforIgnitionMap.GetValue(i));
                    currValue *= 100;
                    currValue += 100;
                    if (mapType == MapSensorType.MapSensor30) currValue /= 1.2;
                    else if (mapType == MapSensorType.MapSensor35) currValue /= 1.4;
                    else if (mapType == MapSensorType.MapSensor40) currValue /= 1.6;
                    else if (mapType == MapSensorType.MapSensor50) currValue /= 2.0;
                    int ival = Convert.ToInt32(currValue);
                    byte v1 = (byte)(ival / 256);
                    byte v2 = (byte)(ival - (int)v1 * 256);

                    actualAxis.SetValue(v1, j++);
                    actualAxis.SetValue(v2, j++);
                }
                m_File.WriteData(actualAxis, (uint)m_File.GetFileInfo().GetSymbolAddressFlash("Ign_map_0_x_axis!"));
                _localResume.AddToResumeTable("Generated and saved new ignition map x axis");
                //Generate the ignition map based on the axis values
                GenerateAndSaveNewIgnitionMap(m_File, false);
                _localResume.AddToResumeTable("Generated and saved new ignition map");
                min_pressure = -0.8;
                step = (max_pressure - min_pressure) / 15;
                // now setup x axis for fuel map
                double[] axisforFuelMap = new double[16];
                for (int i = 0; i < 16; i++)
                {
                    axisforFuelMap.SetValue(min_pressure + (i * step), i);
                }
                byte[] actualFuelAxis = new byte[16];
                for (int i = 0; i < 16; i++)
                {
                    double currValue = Convert.ToDouble(axisforFuelMap.GetValue(i));
                    currValue *= 100;
                    currValue += 100;
                    if (mapType == MapSensorType.MapSensor30) currValue /= 1.2;
                    else if (mapType == MapSensorType.MapSensor35) currValue /= 1.4;
                    else if (mapType == MapSensorType.MapSensor40) currValue /= 1.6;
                    else if (mapType == MapSensorType.MapSensor50) currValue /= 2.0;
                    int ival = Convert.ToInt32(currValue);
                    if (ival > 255) ival = 255;
                    actualFuelAxis.SetValue((byte)ival, i);
                }
                m_File.WriteData(actualFuelAxis, (uint)m_File.GetFileInfo().GetSymbolAddressFlash("Fuel_map_xaxis!"));
                _localResume.AddToResumeTable("Generated and saved new fuel map x axis");

                //Generate the ignition map based on the axis values
                GenerateAndSaveNewFuelMap(m_File);
                _localResume.AddToResumeTable("Generated and saved new fuel map");

                min_pressure = -0.3;
                step = (max_pressure - min_pressure) / 11;
                // now setup x axis for fuel map
                double[] axisforFuelKnockMap = new double[12];
                for (int i = 0; i < 12; i++)
                {
                    axisforFuelKnockMap.SetValue(min_pressure + (i * step), i);
                }
                byte[] actualFuelKnockAxis = new byte[12];
                for (int i = 0; i < 12; i++)
                {
                    double currValue = Convert.ToDouble(axisforFuelKnockMap.GetValue(i));
                    currValue *= 100;
                    currValue += 100;
                    if (mapType == MapSensorType.MapSensor30) currValue /= 1.2;
                    else if (mapType == MapSensorType.MapSensor35) currValue /= 1.4;
                    else if (mapType == MapSensorType.MapSensor40) currValue /= 1.6;
                    else if (mapType == MapSensorType.MapSensor50) currValue /= 2.0;
                    int ival = Convert.ToInt32(currValue);
                    if (ival > 255) ival = 255;
                    actualFuelKnockAxis.SetValue((byte)ival, i);
                }
                m_File.WriteData(actualFuelKnockAxis, (uint)m_File.GetFileInfo().GetSymbolAddressFlash("Fuel_knock_xaxis!"));
                _localResume.AddToResumeTable("Generated and saved new fuel knock map x axis");

                //Generate the ignition map based on the axis values
                GenerateAndSaveNewFuelKnockMap(m_File);
                _localResume.AddToResumeTable("Generated and saved new fuel knock map");

                // mesh up a boost request map for this.. already possible
                // adjust peak boost to be scaled for the mapsensor type

                SetBoostRequestMaps(turboType, injectorType, mapType, m_File.GetFileInfo().Filename, peak_boost_request, 100, isLpt);
                _localResume.AddToResumeTable("Generated boost request maps");

            }
            m_resume.ResumeTuning = _localResume.ResumeTuning.Copy();
            return TuningResult.TuningSuccess;
        }
 private void IncreaseByteVariableWith140Percent(IECUFile m_File, string symbol)
 {
     // read value
     byte[] data = m_File.ReadData((uint)m_File.GetFileInfo().GetSymbolAddressFlash(symbol), (uint)m_File.GetFileInfo().GetSymbolLength(symbol));
     for (int t = 0; t < data.Length; t++)
     {
         double dval = Convert.ToDouble(data[t]);
         dval *= 1.4;
         data[t] = Convert.ToByte(dval);
     }
     m_File.WriteData(data, (uint)m_File.GetFileInfo().GetSymbolAddressFlash(symbol));
 }
 private void GenerateStartVevFak(IECUFile m_File)
 {
     byte[] vev_fak = new byte[15] { 8, 12, 16, 20, 23, 32, 36, 45, 60, 104, 128, 168, 208, 254, 255 };
     m_File.WriteData(vev_fak, (uint)m_File.GetFileInfo().GetSymbolAddressFlash("Startvev_fak!"));
 }
        private void GenerateAndSaveNewIgnitionMap(IECUFile m_File, bool runsE85)
        {
            //TODO: Implement
            // get the axis
            TuningReferenceMaps _referenceMaps = new TuningReferenceMaps();
            byte[] pressure_axis = m_File.ReadData((uint)m_File.GetFileInfo().GetSymbolAddressFlash("Ign_map_0_x_axis!"), (uint)m_File.GetFileInfo().GetSymbolLength("Ign_map_0_x_axis!"));
            byte[] rpm_axis = m_File.ReadData((uint)m_File.GetFileInfo().GetSymbolAddressFlash("Ign_map_0_y_axis!"), (uint)m_File.GetFileInfo().GetSymbolLength("Ign_map_0_y_axis!"));
            byte[] new_ignition_map = new byte[16 * 18 * 2];
            int ign_map_index = 0;
            for (int rpm_index = rpm_axis.Length-2; rpm_index >=0; rpm_index -= 2)
            {
                for (int pressure_index = 0; pressure_index < pressure_axis.Length; pressure_index += 2)
                {

                    int irpm = Convert.ToInt32(rpm_axis[rpm_index]) * 256;
                    irpm += Convert.ToInt32(rpm_axis[rpm_index+1]);
                    int ipressure = Convert.ToInt32(pressure_axis[pressure_index]) * 256;
                    ipressure += Convert.ToInt32(pressure_axis[pressure_index + 1]);
                    double correctionFactor = 1;
                    double pressure = ipressure;
                    MapSensorType mapsensor = m_File.GetMapSensorType(false);
                    if (mapsensor == MapSensorType.MapSensor30) correctionFactor = 1.2;
                    else if (mapsensor == MapSensorType.MapSensor35) correctionFactor = 1.4;
                    else if (mapsensor == MapSensorType.MapSensor40) correctionFactor = 1.6;
                    else if (mapsensor == MapSensorType.MapSensor50) correctionFactor = 2.0;
                    pressure *= correctionFactor;
                    pressure -= 100;
                    pressure /= 100;
                    double ignition_advance = _referenceMaps.GetIgnitionAdvanceForPressureRpm(pressure, (double)irpm);
                    if (runsE85) ignition_advance = _referenceMaps.GetIgnitionAdvanceE85ForPressureRpm(pressure, (double)irpm);
                    // write ignition advance into the map
                    ignition_advance *= 10; // correction factor
                    Int32 iAdvance = Convert.ToInt32(ignition_advance);
                    byte v1 = (byte)(iAdvance / 256);
                    byte v2 = (byte)(iAdvance - (int)v1 * 256);
                    //Console.WriteLine("Writing data rpmidx : " + rpm_index.ToString() + " mapidx: " + pressure_index.ToString() + " ignidx: " + ign_map_index.ToString());
                    new_ignition_map[ign_map_index++] = v1;
                    new_ignition_map[ign_map_index++] = v2;
                }
            }
            // now save the map
            m_File.WriteData(new_ignition_map, (uint)m_File.GetFileInfo().GetSymbolAddressFlash("Ign_map_0!"));
            m_File.WriteData(new_ignition_map, (uint)m_File.GetFileInfo().GetSymbolAddressFlash("Ign_map_4!")); // save as warmup map as well
        }
        private void GenerateAndSaveNewFuelMap(IECUFile m_File)
        {
            TuningReferenceMaps _referenceMaps = new TuningReferenceMaps();
            byte[] pressure_axis = m_File.ReadData((uint)m_File.GetFileInfo().GetSymbolAddressFlash("Fuel_map_xaxis!"), (uint)m_File.GetFileInfo().GetSymbolLength("Fuel_map_xaxis!"));
            byte[] rpm_axis = m_File.ReadData((uint)m_File.GetFileInfo().GetSymbolAddressFlash("Fuel_map_yaxis!"), (uint)m_File.GetFileInfo().GetSymbolLength("Fuel_map_yaxis!"));
            byte[] new_fuel_map = new byte[16 * 16];
            int fuel_map_index = 0;
            for (int rpm_index = rpm_axis.Length - 2; rpm_index >= 0; rpm_index -= 2)
            {
                for (int pressure_index = 0; pressure_index < pressure_axis.Length; pressure_index ++)
                {

                    int irpm = Convert.ToInt32(rpm_axis[rpm_index]) * 256;
                    irpm += Convert.ToInt32(rpm_axis[rpm_index + 1]);
                    irpm *= 10;
                    int ipressure = Convert.ToInt32(pressure_axis[pressure_index]);
                    double correctionFactor = 1;
                    double pressure = ipressure;
                    MapSensorType mapsensor = m_File.GetMapSensorType(false);
                    if (mapsensor == MapSensorType.MapSensor30) correctionFactor = 1.2;
                    else if (mapsensor == MapSensorType.MapSensor35) correctionFactor = 1.4;
                    else if (mapsensor == MapSensorType.MapSensor40) correctionFactor = 1.6;
                    else if (mapsensor == MapSensorType.MapSensor50) correctionFactor = 2.0;
                    pressure *= correctionFactor;
                    pressure -= 100;
                    pressure /= 100;
                    double fuel_correction = _referenceMaps.FuelCorrectionForPressureRpm(pressure, (double)irpm);
                    // write ignition advance into the map
                    fuel_correction -= 0.5;
                    fuel_correction /= 0.00390625;

                    Int32 icorrection = Convert.ToInt32(fuel_correction);
                    if (icorrection > 255) icorrection = 255;
                    new_fuel_map[fuel_map_index++] = (byte)icorrection;
                }
            }
            // now save the map
            m_File.WriteData(new_fuel_map, (uint)m_File.GetFileInfo().GetSymbolAddressFlash("Insp_mat!"));
        }
Exemple #7
0
 private void CopySymbol(string symbolname, string fromfilename, IECUFile FileToTransferTo, int fromflashaddress, int fromlength, string targetfilename, int targetflashaddress, int targetlength, Trionic5Resume resume)
 {
     if (fromlength != targetlength)
     {
         resume.AddToResumeTable("Unable to transfer symbol " + symbolname + " because source and target lengths don't match!");
     }
     else
     {
         try
         {
             while (fromflashaddress > m_trionicFileInformation.Filelength) fromflashaddress -= m_trionicFileInformation.Filelength;
             FileInfo fi = new FileInfo(targetfilename);
             while (targetflashaddress > fi.Length) targetflashaddress -= (int)fi.Length;
             byte[] mapdata = m_trionicFile.ReadDataFromFile(fromfilename, (uint)fromflashaddress, (uint)fromlength);
             FileToTransferTo.WriteData(mapdata, (uint)targetflashaddress);
             resume.AddToResumeTable("Transferred symbol " + symbolname + " successfully");
         }
         catch (Exception E)
         {
             resume.AddToResumeTable("Failed to transfer symbol " + symbolname + ": " + E.Message);
         }
     }
 }