private void ctrlRealtime1_onSwitchClosedLoopOnOff(object sender, ctrlRealtime.ClosedLoopOnOffEventArgs e) { // toggle closed loop on /off based on e.SwitchOn //Fastest way to detect cell stability and such is in ECUConnection but we don't want that logic in there. //Design decision is to feed the AFRMaps object with the data and have that figure out what to do. _ecuConnection.StopECUMonitoring(); Thread.Sleep(10); if (m_AFRMaps == null) { m_AFRMaps = new AFRMaps(); m_AFRMaps.onFuelmapCellChanged += new AFRMaps.FuelmapCellChanged(m_AFRMaps_onFuelmapCellChanged); m_AFRMaps.onIdleFuelmapCellChanged += new AFRMaps.IdleFuelmapCellChanged(m_AFRMaps_onIdleFuelmapCellChanged); m_AFRMaps.onCellLocked += new AFRMaps.CellLocked(m_AFRMaps_onCellLocked); m_AFRMaps.TrionicFile = m_trionicFile; m_AFRMaps.InitializeMaps(); } byte[] pgm_mod = _ecuConnection.ReadSymbolData(m_trionicFileInformation.GetProgramModeSymbol(), (uint)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetProgramModeSymbol()), (uint)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetProgramModeSymbol())); Thread.Sleep(50); // Pgm_mod! byte 0 bit 4 0x10 Lambda control on/off if (pgm_mod.Length > 3) { if (e.SwitchOn) { // re-enable the enrichment factors if (m_ProgramModeSettings.Lambdacontrol) pgm_mod[0] |= 0x10; // switch closed loop back on //if (m_ProgramModeSettings.PurgeControl) pgm_mod[2] |= 0x20; // switch purge control back on //if(m_ProgramModeSettings.AcclerationEnrichment) pgm_mod[1] |= 0x10; // acceleration enrichment back on //if(m_ProgramModeSettings.DecelerationEnleanment) pgm_mod[1] |= 0x20; // deceleration enleanment back on //if(m_ProgramModeSettings.WOTEnrichment) pgm_mod[0] |= 0x02; // WOT enrichment back on //if(m_ProgramModeSettings.Fuelcut) pgm_mod[1] |= 0x04; // Fuelcut back on //if (m_ProgramModeSettings.UseSeperateInjectionMapForIdle) pgm_mod[2] |= 0x02; // Idle map back on //if(m_ProgramModeSettings.LoadControl) pgm_mod[3] |= 0x04; // load control back on //if (pgm_mod.Length >= 5) //{ // if (m_ProgramModeSettings.NoFuelcutR12) pgm_mod[4] |= 0x04; // no fuelcut in R12 back on // if (m_ProgramModeSettings.ConstIdleIgnitionAngleGearOneAndTwo) pgm_mod[4] |= 0x02; // const ign angle back on //} // write Pgm_mod into ECU immediately! if(m_appSettings.DisableClosedLoopOnStartAutotune) _ecuConnection.WriteSymbolDataForced(m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetProgramModeSymbol()), 1, pgm_mod); // <GS-12042010> set to length = 1 to prevent other changes Thread.Sleep(100); if (m_appSettings.AutoUpdateFuelMap) { //TODO: ask the user whether he wants to merge the altered fuelmap into ECU memory! // if he replies NO: revert to the previous fuel map (we still need to preserve a copy!) if (MessageBox.Show("Keep adjusted fuel map?", "Question", MessageBoxButtons.YesNo) == DialogResult.No) { // save the original map back to the ECU if (!props.IsTrionic55) { _ecuConnection.WriteSymbolDataForced((int)m_trionicFileInformation.GetSymbolAddressSRAM("Adapt_korr"), (int)m_trionicFileInformation.GetSymbolLength("Adapt_korr"), m_AFRMaps.GetOriginalFuelmap()); } else { _ecuConnection.WriteSymbolDataForced((int)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetInjectionMap()), (int)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetInjectionMap()), m_AFRMaps.GetOriginalFuelmap()); } // <GS-14032011> this should only be done when idlefuel map autotune is active as well // if (m_appSettings.AllowIdleAutoTune) { _ecuConnection.WriteSymbolDataForced((int)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetIdleFuelMap()), (int)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetIdleFuelMap()), m_AFRMaps.GetIdleOriginalFuelmap()); } // and write to the bin as well } else { // save the altered map into the binary //TODO: <GS-03062010> this alters the syncdatetime in the file!!! DateTime ecudt = _ecuConnection.GetMemorySyncDate(); DateTime filedt = m_trionicFile.GetMemorySyncDate(); bool _updateSync = false; if (ecudt == filedt) _updateSync = true; m_trionicFile.WriteData(_ecuConnection.ReadSymbolData(m_trionicFileInformation.GetInjectionMap(), (uint)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetInjectionMap()), (uint)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetInjectionMap())), (uint)m_trionicFileInformation.GetSymbolAddressFlash(m_trionicFileInformation.GetInjectionMap())); if (m_appSettings.AllowIdleAutoTune) { m_trionicFile.WriteData(_ecuConnection.ReadSymbolData(m_trionicFileInformation.GetIdleFuelMap(), (uint)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetIdleFuelMap()), (uint)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetIdleFuelMap())), (uint)m_trionicFileInformation.GetSymbolAddressFlash(m_trionicFileInformation.GetIdleFuelMap())); } if (_updateSync) { DateTime dtnow = DateTime.Now; _ecuConnection.SetMemorySyncDate(dtnow); m_trionicFile.SetMemorySyncDate(dtnow); // now we should be in sync again } } // init the afrmaps values m_AFRMaps.InitAutoTuneVars(e.SwitchOn); } else { // <GS-02032010> Create a new form for this with multiselect grid! //TODO: in that case, we've maintained the changes in the m_AFRMaps.FuelMapInformation struct // we should now show the proposed changed (in percentages) to the user and let him/her // decide which cells should be updated and which ones should be discarded double[] diffinperc = m_AFRMaps.GetPercentualDifferences(); DataTable dt = new DataTable(); for (int i = 0; i < 16; i++) { dt.Columns.Add(i.ToString(), Type.GetType("System.Double")); } for (int i = 15; i >= 0; i--) { object[] arr = new object[16]; for (int j = 0; j < 16; j++) { arr.SetValue(diffinperc[(i * 16) + j], j); } dt.Rows.Add(arr); } frmFuelMapAccept acceptMap = new frmFuelMapAccept(); acceptMap.onUpdateFuelMap += new frmFuelMapAccept.UpdateFuelMap(acceptMap_onUpdateFuelMap); acceptMap.onSyncDates += new frmFuelMapAccept.SyncDates(acceptMap_onSyncDates); acceptMap.SetDataTable(dt); acceptMap.ShowDialog(); DialogResult = DialogResult.None; //TODO: <GS-28102010> Hoe hier ook te vragen voor Idle fuel map??? // voor nu maar even gewoon domweg schrijven if (m_appSettings.AllowIdleAutoTune) { _ecuConnection.WriteSymbolData(m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetIdleFuelMap()), m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetIdleFuelMap()), m_AFRMaps.GetIdleCurrentlyMutatedFuelMap()); } Application.DoEvents(); } ctrlRealtime1.SetAutoTuneButtonText("Autotune fuel"); SetStatusText("Idle"); } else { //TODO: disable the enrichment factors // init the afrmaps values SetStatusText("Starting autotune..."); System.Windows.Forms.Application.DoEvents(); if ((pgm_mod[0] & 0x10) > 0) m_ProgramModeSettings.Lambdacontrol = true; else m_ProgramModeSettings.Lambdacontrol = false; //if ((pgm_mod[2] & 0x20) > 0) m_ProgramModeSettings.PurgeControl = true; //else m_ProgramModeSettings.PurgeControl = false; // Purge control disable //if ((pgm_mod[1] & 0x10) > 0) m_ProgramModeSettings.AcclerationEnrichment = true; //else m_ProgramModeSettings.AcclerationEnrichment = false; //if ((pgm_mod[1] & 0x20) > 0) m_ProgramModeSettings.DecelerationEnleanment = true; //else m_ProgramModeSettings.DecelerationEnleanment = false; //if ((pgm_mod[0] & 0x02) > 0) m_ProgramModeSettings.WOTEnrichment = true; //else m_ProgramModeSettings.WOTEnrichment = false; //if ((pgm_mod[1] & 0x04) > 0) m_ProgramModeSettings.Fuelcut = true; //else m_ProgramModeSettings.Fuelcut = false; //if ((pgm_mod[2] & 0x02) > 0) m_ProgramModeSettings.UseSeperateInjectionMapForIdle = true; //else m_ProgramModeSettings.UseSeperateInjectionMapForIdle = false; //if ((pgm_mod[3] & 0x04) > 0) m_ProgramModeSettings.LoadControl = true; //else m_ProgramModeSettings.LoadControl = false; //if (pgm_mod.Length >= 5) //{ // if ((pgm_mod[4] & 0x04) > 0) m_ProgramModeSettings.NoFuelcutR12 = true; // else m_ProgramModeSettings.NoFuelcutR12 = false; // if ((pgm_mod[4] & 0x02) > 0) m_ProgramModeSettings.ConstIdleIgnitionAngleGearOneAndTwo = true; // else m_ProgramModeSettings.ConstIdleIgnitionAngleGearOneAndTwo = false; //} // we have to restore this after coming out of autotune! // now set to the needed settings for autotuning pgm_mod[0] &= 0xEF; // switch closed loop off //pgm_mod[2] &= 0xDF; // switch purge control off //pgm_mod[1] &= 0xEF; // acceleration enrichment OFF //pgm_mod[1] &= 0xDF; // deceleration enleanment OFF //pgm_mod[0] &= 0xFD; // WOT enrichment OFF ??? Don't: because it will be on after autotune //pgm_mod[2] &= 0xFD; // Idle map usage OFF (so always in main fuel map) //pgm_mod[1] &= 0xFB; // turn fuelcut function in engine brake OFF //pgm_mod[3] &= 0xFB; // turn load control OFF ??? Don't: because it will be on after autotune //if (pgm_mod.Length >= 5) //{ // pgm_mod[4] |= 0x04; // turn off fuelcut in reverse, 1st and second gear (INVERTED) // pgm_mod[4] &= 0xFD; // turn off constant ignition angle at idle in 1st and second gear //} //Write Pgm_mod into ECU immediately if (m_appSettings.DisableClosedLoopOnStartAutotune) _ecuConnection.WriteSymbolDataForced(m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetProgramModeSymbol()), 1/*m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetProgramModeSymbol())*/, pgm_mod); // <GS-12042010> set to 1 to prevent other changes Thread.Sleep(100); m_AFRMaps.InitAutoTuneVars(e.SwitchOn); // this also clears the afr feedback map // and automatically show the autotune window (feedbackafr/fuel adjustment map) // read ECU data for fuel correction! //<GS-31032010> should be different for T5.2 //adaption data should NOT be merged and Adapt_korr should be used as the fuel map byte[] fuelmap; if (!props.IsTrionic55) { fuelmap = _ecuConnection.ReadSymbolData("Adapt_korr", (uint)m_trionicFileInformation.GetSymbolAddressSRAM("Adapt_korr"), (uint)m_trionicFileInformation.GetSymbolLength("Adapt_korr")); Thread.Sleep(50); } else { byte[] fuelcorrectiondata = _ecuConnection.ReadSymbolData("Adapt_korr", (uint)m_trionicFileInformation.GetSymbolAddressSRAM("Adapt_korr"), (uint)m_trionicFileInformation.GetSymbolLength("Adapt_korr")); Thread.Sleep(50); // AND read the long term fuel trim //byte[] longtermtrim = _ecuConnection.ReadSymbolData("Adapt_injfaktor!", (uint)m_trionicFileInformation.GetSymbolAddressSRAM("Adapt_injfaktor!"), (uint)m_trionicFileInformation.GetSymbolLength("Adapt_injfaktor!")); // and merge it into the main fuel map // get the main fuel map fuelmap = _ecuConnection.ReadSymbolData(m_trionicFileInformation.GetInjectionMap(), (uint)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetInjectionMap()), (uint)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetInjectionMap())); // if all adaption data is 128, don't update to save time Thread.Sleep(50); bool _doUpdateFuelMap = false; for (int t = 0; t < fuelcorrectiondata.Length; t++) { if (Convert.ToInt32(fuelcorrectiondata[t]) != 0x80) { _doUpdateFuelMap = true; } } //int itrim = Convert.ToInt32(longtermtrim[0]); if (_doUpdateFuelMap) { SetStatusText("Updating fuelmaps..."); for (int t = 0; t < fuelcorrectiondata.Length; t++) { int corrval = Convert.ToInt32(fuelcorrectiondata[t]); // ranges from 77 - 160? int fuelmapvalue = Convert.ToInt32(fuelmap[t]); fuelmapvalue *= corrval; fuelmapvalue /= 128; //fuelmapvalue *= itrim; //fuelmapvalue /= 128; // check boundaries if (fuelmapvalue < 1) fuelmapvalue = 1; if (fuelmapvalue > 254) fuelmapvalue = 254; fuelmap[t] = Convert.ToByte(fuelmapvalue); } // save fuel map _ecuConnection.WriteSymbolDataForced((int)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetInjectionMap()), (int)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetInjectionMap()), fuelmap); Thread.Sleep(50); for (int t = 0; t < fuelcorrectiondata.Length; t++) { fuelcorrectiondata[t] = 128; // reinit } _ecuConnection.WriteSymbolDataForced((int)m_trionicFileInformation.GetSymbolAddressSRAM("Adapt_korr"), (int)m_trionicFileInformation.GetSymbolLength("Adapt_korr"), fuelcorrectiondata); Thread.Sleep(50); } } if (m_appSettings.ResetFuelTrims) { // and write 128 to trim byte[] longtermtrim = new byte[1]; longtermtrim[0] = 128; _ecuConnection.WriteSymbolData((int)m_trionicFileInformation.GetSymbolAddressSRAM("Adapt_injfaktor!"), (int)m_trionicFileInformation.GetSymbolLength("Adapt_injfaktor!"), longtermtrim); if (props.IsTrionic55) { // clear idle adaption (trim) as well if(m_appSettings.AllowIdleAutoTune) { _ecuConnection.WriteSymbolData((int)m_trionicFileInformation.GetSymbolAddressSRAM("Adapt_inj_imat!"), (int)m_trionicFileInformation.GetSymbolLength("Adapt_inj_imat!"), longtermtrim); } } } // all done, adaptions have been made m_AFRMaps.SetCurrentFuelMap(fuelmap); m_AFRMaps.SetOriginalFuelMap(fuelmap); byte[] idlefuelmap = _ecuConnection.ReadSymbolData(m_trionicFileInformation.GetIdleFuelMap(), (uint)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetIdleFuelMap()), (uint)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetIdleFuelMap())); m_AFRMaps.SetIdleCurrentFuelMap(idlefuelmap); m_AFRMaps.SetIdleOriginalFuelMap(idlefuelmap); //<GS-14032011> bugfix for erratic idle behaviour after autotune, this was filled with fuelmap in stead of idlefuelmap m_AFRMaps.AutoUpdateFuelMap = m_appSettings.AutoUpdateFuelMap; ctrlRealtime1.SetAutoTuneButtonText("Tuning..."); SetStatusText("Autotune fuel running..."); } //TODO: If the afrmaps object does not contain a working fuelmap yet, create it if (!m_AFRMaps.HasValidFuelmap) // do always because map has been altered { // insert the fuel map from SRAM now byte[] fuelmap; if (!props.IsTrionic55) { fuelmap = _ecuConnection.ReadSymbolData("Adapt_korr", (uint)m_trionicFileInformation.GetSymbolAddressSRAM("Adapt_korr"), (uint)m_trionicFileInformation.GetSymbolLength("Adapt_korr")); } else { fuelmap = _ecuConnection.ReadSymbolData(m_trionicFileInformation.GetInjectionMap(), (uint)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetInjectionMap()), (uint)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetInjectionMap())); } m_AFRMaps.SetCurrentFuelMap(fuelmap); } if (!m_AFRMaps.HasValidIdleFuelmap) // do always because map has been altered { // insert the fuel map from SRAM now byte[] idlefuelmap; idlefuelmap = _ecuConnection.ReadSymbolData(m_trionicFileInformation.GetIdleFuelMap(), (uint)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetIdleFuelMap()), (uint)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetIdleFuelMap())); m_AFRMaps.SetIdleCurrentFuelMap(idlefuelmap); } //the object should consider all constraints given in the autotune settings screen //so it should remember the ORIGINAL fuel map it started with to determine max-correction etc //also it should maintain a counter map for this to be able to monitor how many changes have been made //to the map //The original map should be 'updateable' (=able to delete and re-create from current sram) so that //the user will be able to force more correction onto the map m_AFRMaps.WideBandAFRSymbol = m_appSettings.WidebandLambdaSymbol; m_AFRMaps.AcceptableTargetErrorPercentage = m_appSettings.AcceptableTargetErrorPercentage; m_AFRMaps.AreaCorrectionPercentage = m_appSettings.AreaCorrectionPercentage; m_AFRMaps.AutoUpdateFuelMap = m_appSettings.AutoUpdateFuelMap; m_AFRMaps.CellStableTime_ms = m_appSettings.CellStableTime_ms; m_AFRMaps.CorrectionPercentage = m_appSettings.CorrectionPercentage; m_AFRMaps.DiscardClosedThrottleMeasurements = m_appSettings.DiscardClosedThrottleMeasurements; m_AFRMaps.DiscardFuelcutMeasurements = m_appSettings.DiscardFuelcutMeasurements; m_AFRMaps.EnrichmentFilter = m_appSettings.EnrichmentFilter; m_AFRMaps.FuelCutDecayTime_ms = m_appSettings.FuelCutDecayTime_ms; m_AFRMaps.MaximumAdjustmentPerCyclePercentage = m_appSettings.MaximumAdjustmentPerCyclePercentage; m_AFRMaps.MaximumAFRDeviance = m_appSettings.MaximumAFRDeviance; m_AFRMaps.MinimumAFRMeasurements = m_appSettings.MinimumAFRMeasurements; m_AFRMaps.IsAutoMappingActive = !e.SwitchOn; // closed loop OFF means automapping ON //ctrlRealtime1.SetAutoTuneButtonText("Tuning..."); } else { // could not read pgm_mod...wtf? } _ecuConnection.StartECUMonitoring(); }
void acceptMap_onUpdateFuelMap(object sender, frmFuelMapAccept.UpdateFuelMapEventArgs e) { // write to ECU if possible //Console.WriteLine("Update fuel after accept"); if (_ecuConnection.Opened) { if (e.Value == 0) return; // test for value 0... nothing todo //Console.WriteLine("x: " + e.X.ToString() + " y: " + e.Y.ToString() + " val: " + e.Value.ToString() + " syn: " + e.doSync.ToString()); if (m_AFRMaps == null) { m_AFRMaps = new AFRMaps(); m_AFRMaps.onFuelmapCellChanged += new AFRMaps.FuelmapCellChanged(m_AFRMaps_onFuelmapCellChanged); m_AFRMaps.onIdleFuelmapCellChanged += new AFRMaps.IdleFuelmapCellChanged(m_AFRMaps_onIdleFuelmapCellChanged); m_AFRMaps.onCellLocked += new AFRMaps.CellLocked(m_AFRMaps_onCellLocked); m_AFRMaps.TrionicFile = m_trionicFile; m_AFRMaps.InitializeMaps(); } int y = 15 - e.Y; // first get the original map //_ecuConnection.WriteSymbolDataForced(m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetInjectionMap()) + e.Mapindex, 1, write); //byte[] fuelmap = _ecuConnection.ReadSymbolData(m_trionicFileInformation.GetInjectionMap(), (uint)m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetInjectionMap()), (uint)m_trionicFileInformation.GetSymbolLength(m_trionicFileInformation.GetInjectionMap())); byte[] fuelmap = m_AFRMaps.FuelMapInformation.GetOriginalFuelMap(); double originalbyte = Convert.ToDouble(fuelmap[(y * 16) + e.X]); originalbyte *= (100 + e.Value) / 100; byte newFuelMapByte = Convert.ToByte(originalbyte); byte[] data2Write = new byte[1]; data2Write[0] = newFuelMapByte; int fuelMapAddress = m_trionicFileInformation.GetSymbolAddressSRAM(m_trionicFileInformation.GetInjectionMap()); if (!props.IsTrionic55) { fuelMapAddress = m_trionicFileInformation.GetSymbolAddressSRAM("Adapt_korr"); // write to adapt_korr in stead of insp_mat } fuelMapAddress += (y * 16) + e.X; //Console.WriteLine("Writing data: " + data2Write[0].ToString("D3") + " to address: " + fuelMapAddress.ToString("X6") + " ori was: " + fuelmap[(y * 16) + e.X].ToString("D3")); //_ecuConnection.WriteData(data2Write, (uint)fuelMapAddress); _ecuConnection.WriteSymbolDataForced(fuelMapAddress, 1, data2Write); // and write to the current binary file as well //<GS-22032010> create a backup file first int fuelMapAddressFlash = m_trionicFileInformation.GetSymbolAddressFlash(m_trionicFileInformation.GetInjectionMap()); fuelMapAddressFlash += (y * 16) + e.X; // only on the last update. m_trionicFile.WriteData(data2Write, (uint)fuelMapAddressFlash); } }