forked from Blackfrosch/VAGEDCSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrlAirmassResult.cs
1324 lines (1176 loc) · 54.3 KB
/
ctrlAirmassResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors.Controls;
namespace VAGSuite
{
public enum TurboType : int
{
Stock,
GT17,
TD0415T,
TD0419T,
GT28BB,
GT28RS,
GT3071R,
HX35w,
HX40w
}
public enum limitType : int
{
None,
TorqueLimiterEngine,
TorqueLimiterGear,
AirmassLimiter,
TurboSpeedLimiter,
FuelCutLimiter,
AirTorqueCalibration
}
public partial class ctrlAirmassResult : DevExpress.XtraEditors.XtraUserControl
{
int rows;
int columns;
int m_injectorConstant = 0;
private int[] y_axisvalues;
private int[] x_axisvalues;
private int[] limitermap;
private string m_currentfile = string.Empty;
private int m_MaxValueInTable = 0;
public string Currentfile
{
get { return m_currentfile; }
set { m_currentfile = value; }
}
private SymbolCollection m_symbols;
public SymbolCollection Symbols
{
get { return m_symbols; }
set { m_symbols = value; }
}
public delegate void StartTableViewer(object sender, StartTableViewerEventArgs e);
public event ctrlAirmassResult.StartTableViewer onStartTableViewer;
public class StartTableViewerEventArgs : System.EventArgs
{
private string _mapname;
public string SymbolName
{
get
{
return _mapname;
}
}
public StartTableViewerEventArgs(string mapname)
{
this._mapname = mapname;
}
}
public ctrlAirmassResult()
{
InitializeComponent();
}
private int m_currentfile_size = 0x80000;
public int Currentfile_size
{
get { return m_currentfile_size; }
set { m_currentfile_size = value; }
}
private Int64 GetSymbolAddress(SymbolCollection curSymbolCollection, string symbolname)
{
foreach (SymbolHelper sh in curSymbolCollection)
{
if (sh.Userdescription == symbolname || sh.Varname == symbolname)
{
return sh.Flash_start_address;
}
}
return 0;
}
private int GetSymbolLength(SymbolCollection curSymbolCollection, string symbolname)
{
foreach (SymbolHelper sh in curSymbolCollection)
{
if (sh.Userdescription == symbolname || sh.Varname == symbolname)
{
return sh.Length;
}
}
return 0;
}
private byte[] reverseEndian(byte[] retval)
{
byte[] ret = new byte[retval.Length];
try
{
if (retval.Length > 0 && retval.Length % 2 == 0)
{
for (int i = 0; i < retval.Length; i += 2)
{
byte b1 = retval[i];
byte b2 = retval[i + 1];
ret[i] = b2;
ret[i + 1] = b1;
}
}
}
catch (Exception E)
{
}
return ret;
}
private byte[] readdatafromfile(string filename, int address, int length)
{
byte[] retval = new byte[length];
try
{
FileStream fsi1 = File.OpenRead(filename);
while (address > fsi1.Length) address -= (int)fsi1.Length;
BinaryReader br1 = new BinaryReader(fsi1);
fsi1.Position = address;
string temp = string.Empty;
for (int i = 0; i < length; i++)
{
retval.SetValue(br1.ReadByte(), i);
}
retval = reverseEndian(retval);
fsi1.Flush();
br1.Close();
fsi1.Close();
fsi1.Dispose();
}
catch (Exception E)
{
Console.WriteLine(E.Message);
}
return retval;
}
private int[] readIntdatafromfile(string filename, int address, int length)
{
int[] retval = new int[length / 2];
try
{
FileStream fsi1 = File.OpenRead(filename);
while (address > fsi1.Length) address -= (int)fsi1.Length;
BinaryReader br1 = new BinaryReader(fsi1);
fsi1.Position = address;
string temp = string.Empty;
int j = 0;
for (int i = 0; i < length; i += 2)
{
byte b1 = br1.ReadByte();
byte b2 = br1.ReadByte();
int value = Convert.ToInt32(b1) + Convert.ToInt32(b2) * 256; // LoHi
if (_ECUType.Contains("EDC16"))
{
value = Convert.ToInt32(b1) * 256 + Convert.ToInt32(b2); // HiLo
}
retval.SetValue(value, j++);
}
fsi1.Flush();
br1.Close();
fsi1.Close();
fsi1.Dispose();
}
catch (Exception E)
{
Console.WriteLine(E.Message);
}
return retval;
}
private string _ECUType = string.Empty;
public string ECUType
{
get { return _ECUType; }
set { _ECUType = value; }
}
private int m_numberCylinders = 4;
public int NumberCylinders
{
set
{
m_numberCylinders = value;
}
}
private SymbolHelper GetSymbolLike(SymbolCollection sc, string symbolname, int codeblock)
{
foreach (SymbolHelper sh in sc)
{
if (sh.Varname.StartsWith(symbolname) && sh.CodeBlock == codeblock)
{
return sh;
}
}
foreach (SymbolHelper sh in sc)
{
if (sh.Varname.StartsWith(symbolname))
{
return sh;
}
}
return new SymbolHelper();
}
public PerformanceResults Calculate(string filename, SymbolCollection symbols)
{
// do the math!
PerformanceResults pr = new PerformanceResults();
FillComboBoxBankSelection();
SymbolHelper driverWishHelper = GetSymbolLike(symbols, "Driver wish", selectedBank);
int[] pedalrequestmap = readIntdatafromfile(filename, (int)driverWishHelper.Flash_start_address, driverWishHelper.Length);
limitermap = new int[pedalrequestmap.Length];
int[] resulttable = new int[pedalrequestmap.Length]; // result
rows = driverWishHelper.Y_axis_length;
columns = driverWishHelper.X_axis_length;
int[] pedalXAxis = readIntdatafromfile(filename, driverWishHelper.Y_axis_address, driverWishHelper.Y_axis_length * 2);
int[] pedalYAxis = readIntdatafromfile(filename, driverWishHelper.X_axis_address, driverWishHelper.X_axis_length * 2);
y_axisvalues = pedalYAxis;
x_axisvalues = pedalXAxis;
for (int colcount = 0; colcount < columns; colcount++)
{
for (int rowcount = 0; rowcount < rows; rowcount++)
{
// get the current value from the request map
if (_ECUType.Contains("EDC16"))
{
int requestedTorque = (int)pedalrequestmap.GetValue((colcount * rows) + rowcount);
limitType limiterType = limitType.None;
int resultingAirMass = CalculateMaxAirmassforcell(symbols, filename, (int)pedalXAxis.GetValue(rowcount), ((int)pedalYAxis.GetValue(colcount)), requestedTorque, checkEdit1.Checked, out limiterType, true);
resulttable.SetValue(resultingAirMass, (colcount * rows) + rowcount);
limitermap.SetValue(limiterType, (colcount * rows) + rowcount);
}
else
{
int InjectedQuantity = (int)pedalrequestmap.GetValue((colcount * rows) + rowcount);
limitType limiterType = limitType.None;
int resultingAirMass = CalculateMaxAirmassforcell(symbols, filename, (int)pedalXAxis.GetValue(rowcount), ((int)pedalYAxis.GetValue(colcount)), InjectedQuantity, checkEdit1.Checked, out limiterType, false);
resulttable.SetValue(resultingAirMass, (colcount * rows) + rowcount);
limitermap.SetValue(limiterType, (colcount * rows) + rowcount);
}
}
}
// now show resulttable
DataTable dt = new DataTable();
foreach (int xvalue in pedalYAxis)
{
dt.Columns.Add(xvalue.ToString());
}
// now fill the table rows
m_MaxValueInTable = 0;
for (int r = 0; r < pedalXAxis.Length ; r++)
{
object[] values = new object[columns];
for (int t = 0; t < pedalYAxis.Length; t++)
{
int currValue = (int)resulttable.GetValue( (((t + 1) * rows) - 1) - r );
if (currValue > m_MaxValueInTable) m_MaxValueInTable = currValue;
values.SetValue(currValue, t);
}
dt.Rows.Add(values);
}
gridControl1.DataSource = dt;
if (xtraTabControl1.SelectedTabPage.Name == xtraTabPage2.Name)
{
LoadGraphWithDetails();
}
try
{
for (int i = 0; i < dt.Rows[0].ItemArray.Length; i++)
{
double o = Convert.ToDouble(dt.Rows[0].ItemArray.GetValue(i));
// convert to hp
int rpm = Convert.ToInt32(y_axisvalues.GetValue(i));
int torque = Tools.Instance.IQToTorque(Convert.ToInt32(o), rpm, m_numberCylinders);
if (_ECUType.Contains("EDC16"))
{
torque = Convert.ToInt32(o);
torque *= 10; // correction to keep the code identical from here
double temptorque = torque * Tools.Instance.GetCorrectionFactorForRpm(rpm, m_numberCylinders);
torque = Convert.ToInt32(temptorque);
}
int horsepower = Tools.Instance.TorqueToPower(torque, rpm);
// if (checkEdit5.Checked) horsepower = Tools.Instance.TorqueToPowerkW(torque, rpm);
// if (checkEdit6.Checked) torque = Tools.Instance.IQToTorque(Convert.ToInt32(o), rpm, m_numberCylinders);//AirmassToTorqueLbft(Convert.ToInt32(o), rpm);
horsepower /= 100;
torque /= 100;
if (torque > pr.Torque) pr.Torque = torque;
if (horsepower > pr.Horsepower) pr.Horsepower = horsepower;
}
}
catch (Exception)
{
}
return pr;
}
private void FillComboBoxBankSelection()
{
if (Tools.Instance.codeBlockList != null)
{
foreach (CodeBlock cb in Tools.Instance.codeBlockList)
{
try
{
bool found = false;
foreach (string cbi in comboBoxEdit1.Properties.Items)
{
if (cbi == "Codebank " + cb.CodeID.ToString())
{
found = true;
break;
}
}
if (!found)
{
comboBoxEdit1.Properties.Items.Add("Codebank " + cb.CodeID.ToString());
}
}
catch (Exception)
{
}
}
}
}
private bool SymbolExists(string symbolname)
{
foreach (SymbolHelper sh in m_symbols)
{
if (sh.Varname == symbolname || sh.Userdescription == symbolname) return true;
}
return false;
}
private int CalculateMaxAirmassforcell(SymbolCollection symbols, string filename, int pedalposition, int rpm, int requestedQuantity, bool autogearbox, out limitType limiterType, bool torqueBased)
{
limiterType = limitType.None;
// check against torque limiter
SymbolHelper trqLimiter = GetSymbolLike(symbols, "Torque limiter", selectedBank);
int[] trqLimitMap = readIntdatafromfile(filename, (int)trqLimiter.Flash_start_address, trqLimiter.Length);
int[] trqXAxis = readIntdatafromfile(filename, trqLimiter.Y_axis_address, trqLimiter.Y_axis_length * 2);
int[] trqYAxis = readIntdatafromfile(filename, trqLimiter.X_axis_address, trqLimiter.X_axis_length * 2);
if (torqueBased)
{
int Nmlimit = Convert.ToInt32(GetInterpolatedTableValue(trqLimitMap, trqXAxis, trqYAxis, Convert.ToInt32(spinEdit1.EditValue) * 10, rpm));
//requestedairmass
if (requestedQuantity > Nmlimit)
{
Console.WriteLine("Torque is limited from " + requestedQuantity.ToString() + " to " + Nmlimit.ToString() + " at " + rpm.ToString() + " rpm");
requestedQuantity = Nmlimit;
limiterType = limitType.TorqueLimiterEngine;
}
}
else
{
int IQlimit = requestedQuantity;
if (trqXAxis.Length == 1)
{
// 2d map
IQlimit = Convert.ToInt32(GetInterpolatedTableValue(trqLimitMap, trqXAxis, trqYAxis, rpm, Convert.ToInt32(spinEdit1.EditValue) * 10));
}
else
{
IQlimit = Convert.ToInt32(GetInterpolatedTableValue(trqLimitMap, trqXAxis, trqYAxis, Convert.ToInt32(spinEdit1.EditValue) * 10, rpm));
}
//requestedairmass
if (requestedQuantity > IQlimit)
{
// Console.WriteLine("IQ is limited from " + requestedQuantity.ToString() + " to " + IQlimit.ToString() + " at " + rpm.ToString() + " rpm");
requestedQuantity = IQlimit;
limiterType = limitType.TorqueLimiterEngine;
}
}
// check against smoke limiter
return requestedQuantity; // no limits ...
}
private int CheckAgainstTorqueLimiters(SymbolCollection symbols, string filename, int rpm, int requestedairmass, bool Automatic, out limitType TrqLimiter)
{
int torque = 0;
TrqLimiter = limitType.None;
int LimitedAirMass = requestedairmass;
int[] xaxis = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "TorqueCal.m_AirXSP"), GetSymbolLength(symbols, "TorqueCal.m_AirXSP"));
int[] yaxis = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "TorqueCal.n_EngYSP"), GetSymbolLength(symbols, "TorqueCal.n_EngYSP"));
torque = Tools.Instance.IQToTorque(requestedairmass, rpm, m_numberCylinders);
int[] enginetorquelim = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "TorqueCal.M_EngMaxTab"), GetSymbolLength(symbols, "TorqueCal.M_EngMaxTab"));
if (Automatic)
{
enginetorquelim = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "TorqueCal.M_EngMaxAutTab"), GetSymbolLength(symbols, "TorqueCal.M_EngMaxAutTab"));
}
int[] xdummy = new int[1];
xdummy.SetValue(0, 0);
int torquelimit1 = Convert.ToInt32(GetInterpolatedTableValue(enginetorquelim, xdummy, yaxis, rpm, 0));
//requestedairmass
if (torque > torquelimit1)
{
//Console.WriteLine("Torque is limited from " + torque.ToString() + " to " + torquelimit1.ToString() + " at " + rpm.ToString() + " rpm");
torque = torquelimit1;
TrqLimiter = limitType.TorqueLimiterEngine;
}
int[] airtorquemap = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "TorqueCal.m_AirTorqMap"), GetSymbolLength(symbols, "TorqueCal.m_AirTorqMap"));
int[] xairtorque = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "TorqueCal.M_EngXSP"), GetSymbolLength(symbols, "TorqueCal.M_EngXSP"));
int TestLimitedAirmass = Convert.ToInt32(GetInterpolatedTableValue(airtorquemap, xairtorque, yaxis, rpm, torque));
if (TestLimitedAirmass < LimitedAirMass)
{
LimitedAirMass = TestLimitedAirmass;
if (TrqLimiter == limitType.None) TrqLimiter = limitType.AirTorqueCalibration;
}
if (TrqLimiter == limitType.None) LimitedAirMass = requestedairmass; // bugfix for if no limiter is active
return LimitedAirMass;
}
private int CheckAgainstFuelcutLimiter(SymbolCollection symbols, string filename, int requestedairmass, ref limitType AirmassLimiter)
{
int retval = requestedairmass;
if ((int)GetSymbolAddress(symbols, "FCutCal.m_AirInletLimit") > 0)
{
int[] fuelcutlimit = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "FCutCal.m_AirInletLimit"), GetSymbolLength(symbols, "FCutCal.m_AirInletLimit"));
if (fuelcutlimit.Length > 0)
{
if (Convert.ToInt32(fuelcutlimit.GetValue(0)) < requestedairmass)
{
retval = Convert.ToInt32(fuelcutlimit.GetValue(0));
AirmassLimiter = limitType.FuelCutLimiter;
}
}
}
return retval;
}
private int CheckAgainstTurboSpeedLimiter(SymbolCollection symbols, string filename, int rpm, int requestedairmass, ref limitType AirmassLimiter)
{
int cols = GetSymbolLength(symbols, "LimEngCal.p_AirSP") / 2;
int[] turbospeed = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "LimEngCal.TurboSpeedTab"), GetSymbolLength(symbols, "LimEngCal.TurboSpeedTab"));
int[] xaxis = new int[1];
xaxis.SetValue(0, 0);
int[] yaxis = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "LimEngCal.p_AirSP"), GetSymbolLength(symbols, "LimEngCal.p_AirSP"));
int ambientpressure = /*1000*/ Convert.ToInt32(spinEdit1.EditValue) * 10;
int airmasslimit = Convert.ToInt32(GetInterpolatedTableValue(turbospeed, xaxis, yaxis, /*rpm*/ ambientpressure, 0));
if (airmasslimit < requestedairmass)
{
requestedairmass = airmasslimit;
AirmassLimiter = limitType.TurboSpeedLimiter;
}
return requestedairmass;
}
private int CheckAgainstAirmassLimiters(SymbolCollection symbols, string filename, int rpm, int requestedairmass, bool autogearbox, ref limitType AirmassLimiter)
{
//AirmassLimiter = limitType.None;
//Y axis = BstKnkCal.n_EngYSP needed for interpolation (16)
//X axis = BstKnkCal.OffsetXSP needed for length (16)
// check against BstKnkCal.MaxAirmass
int cols = GetSymbolLength(symbols, "BstKnkCal.OffsetXSP") / 2;
int rows = GetSymbolLength(symbols, "BstKnkCal.n_EngYSP") / 2;
// only the right-most column (no knock)
int[] bstknk = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "BstKnkCal.MaxAirmass"), GetSymbolLength(symbols, "BstKnkCal.MaxAirmass"));
if (autogearbox)
{
bstknk = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "BstKnkCal.MaxAirmassAu"), GetSymbolLength(symbols, "BstKnkCal.MaxAirmassAu"));
}
int[] xaxis = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "BstKnkCal.OffsetXSP"), GetSymbolLength(symbols, "BstKnkCal.OffsetXSP"));
for (int a = 0; a < xaxis.Length; a++)
{
int val = (int)xaxis.GetValue(a);
if (val > 32000) val = -(65536 - val);
xaxis.SetValue(val, a);
}
int[] yaxis = readIntdatafromfile(filename, (int)GetSymbolAddress(symbols, "BstKnkCal.n_EngYSP"), GetSymbolLength(symbols, "BstKnkCal.n_EngYSP"));
int airmasslimit = Convert.ToInt32(GetInterpolatedTableValue(bstknk, xaxis, yaxis, rpm, 0));
if (airmasslimit < requestedairmass)
{
requestedairmass = airmasslimit;
AirmassLimiter = limitType.AirmassLimiter;
//Console.WriteLine("Reduced airmass because of BstKnkCal.MaxAirmass: " + requestedairmass.ToString() + " rpm: " + rpm.ToString());
}
return requestedairmass;
}
private double GetInterpolatedTableValue(int[] table, int[] xaxis, int[] yaxis, int yvalue, int xvalue)
{
int m_yindex = 0;
int m_xindex = 0;
double result = 0;
double m_ydiff = 0;
double m_xdiff = 0;
double m_ypercentage = 0;
double m_xpercentage = 0;
try
{
for (int yindex = 0; yindex < yaxis.Length; yindex++)
{
if (yvalue > Convert.ToInt32(yaxis.GetValue(yindex)))
{
m_yindex = yindex;
m_ydiff = Math.Abs(Convert.ToDouble(yaxis.GetValue(yindex)) - yvalue);
if (m_yindex < yaxis.Length - 1)
{
m_ypercentage = (double)m_ydiff / (Convert.ToInt32(yaxis.GetValue(yindex + 1)) - Convert.ToInt32(yaxis.GetValue(yindex)));
}
else
{
m_ypercentage = 0;
}
// break;
}
}
for (int xindex = 0; xindex < xaxis.Length; xindex++)
{
if (xvalue > Convert.ToDouble(xaxis.GetValue(xindex)))
{
m_xindex = xindex;
m_xdiff = Math.Abs(Convert.ToDouble(xaxis.GetValue(xindex)) - xvalue);
if (m_xindex < xaxis.Length - 1)
{
m_xpercentage = m_xdiff / (Convert.ToInt32(xaxis.GetValue(xindex + 1)) - Convert.ToInt32(xaxis.GetValue(xindex)));
}
else
{
m_xpercentage = 0;
}
// break;
}
}
//AddDebugLog("RPMindex = " + m_rpmindex + " Percentage = " + m_rpmpercentage.ToString() + " MAPindex = " + m_mapindex.ToString() + " Percentage = " + m_mappercentage.ToString());
// now we found the indexes of the smaller values
int a1 = 0;
int a2 = 0;
int b1 = 0;
int b2 = 0;
if (m_yindex == (yaxis.Length - 1) && (m_xindex < xaxis.Length - 1))
{
// last row in table, extend with the same values
a1 = (int)table.GetValue((m_yindex * xaxis.Length) + m_xindex);
a2 = (int)table.GetValue((m_yindex * xaxis.Length) + m_xindex + 1);
b1 = a1;
b2 = a2;
}
else if (m_yindex == (yaxis.Length - 1) && (m_xindex == xaxis.Length - 1))
{
a1 = (int)table.GetValue((m_yindex * xaxis.Length) + m_xindex);
a2 = a1;
b1 = a1;
b2 = a1;
return Convert.ToDouble(a1);
}
else if (m_yindex <= (yaxis.Length - 1) && (m_xindex == xaxis.Length - 1))
{
a1 = (int)table.GetValue((m_yindex * xaxis.Length) + m_xindex);
a2 = a1;
b1 = (int)table.GetValue(((m_yindex + 1) * xaxis.Length) + m_xindex);
b2 = b1;
}
else
{
a1 = (int)table.GetValue((m_yindex * xaxis.Length) + m_xindex);
a2 = (int)table.GetValue((m_yindex * xaxis.Length) + m_xindex + 1);
b1 = (int)table.GetValue(((m_yindex + 1) * xaxis.Length) + m_xindex);
b2 = (int)table.GetValue(((m_yindex + 1) * xaxis.Length) + m_xindex + 1);
}
//AddDebugLog("a1 = " + a1.ToString() + " a2 = " + a2.ToString() + " b1 = " + b1.ToString() + " b2 = " + b2.ToString());
// now interpolate the values found
double aval = 0;
double bval = 0;
double adiff = Math.Abs((double)a1 - (double)a2);
if (a1 > a2) aval = a1 - (m_xpercentage * adiff);
else aval = a1 + (m_xpercentage * adiff);
double bdiff = Math.Abs((double)b1 - (double)b2);
if (b1 > b2) bval = b1 - (m_xpercentage * bdiff);
else bval = b1 + (m_xpercentage * bdiff);
// now interpolate vertically (RPM axis)
//AddDebugLog("aval = " + aval.ToString() + " bval = " + bval.ToString());
double abdiff = Math.Abs(aval - bval);
if (aval > bval) result = aval - (m_ypercentage * abdiff);
else result = aval + (m_ypercentage * abdiff);
//AddDebugLog("result = " + result.ToString());
}
catch (Exception E)
{
Console.WriteLine("Failed to interpolate: " + E.Message);
}
return result;
}
private double GetInterpolatedTableValue(byte[] table, int[] xaxis, int[] yaxis, int yvalue, int xvalue)
{
int m_yindex = 0;
int m_xindex = 0;
double result = 0;
double m_ydiff = 0;
double m_xdiff = 0;
double m_ypercentage = 0;
double m_xpercentage = 0;
try
{
for (int yindex = 0; yindex < yaxis.Length; yindex++)
{
if (yvalue > Convert.ToInt32(yaxis.GetValue(yindex)))
{
m_yindex = yindex;
m_ydiff = Math.Abs(Convert.ToDouble(yaxis.GetValue(yindex)) - yvalue);
if (m_yindex < yaxis.Length - 1)
{
m_ypercentage = (double)m_ydiff / (Convert.ToInt32(yaxis.GetValue(yindex + 1)) - Convert.ToInt32(yaxis.GetValue(yindex)));
}
else
{
m_ypercentage = 0;
}
// break;
}
}
for (int xindex = 0; xindex < xaxis.Length; xindex++)
{
if (xvalue > Convert.ToDouble(xaxis.GetValue(xindex)))
{
m_xindex = xindex;
m_xdiff = Math.Abs(Convert.ToDouble(xaxis.GetValue(xindex)) - xvalue);
if (m_xindex < xaxis.Length - 1)
{
m_xpercentage = m_xdiff / (Convert.ToInt32(xaxis.GetValue(xindex + 1)) - Convert.ToInt32(xaxis.GetValue(xindex)));
}
else
{
m_xpercentage = 0;
}
// break;
}
}
//AddDebugLog("RPMindex = " + m_rpmindex + " Percentage = " + m_rpmpercentage.ToString() + " MAPindex = " + m_mapindex.ToString() + " Percentage = " + m_mappercentage.ToString());
// now we found the indexes of the smaller values
byte a1 = 0;
byte a2 = 0;
byte b1 = 0;
byte b2 = 0;
if (m_yindex == (yaxis.Length - 1) && (m_xindex < xaxis.Length - 1))
{
// last row in table, extend with the same values
a1 = (byte)table.GetValue((m_yindex * xaxis.Length) + m_xindex);
a2 = (byte)table.GetValue((m_yindex * xaxis.Length) + m_xindex + 1);
b1 = a1;
b2 = a2;
}
else if (m_yindex == (yaxis.Length - 1) && (m_xindex == xaxis.Length - 1))
{
a1 = (byte)table.GetValue((m_yindex * xaxis.Length) + m_xindex);
a2 = a1;
b1 = a1;
b2 = a1;
return Convert.ToDouble(a1);
}
else if (m_yindex <= (yaxis.Length - 1) && (m_xindex == xaxis.Length - 1))
{
a1 = (byte)table.GetValue((m_yindex * xaxis.Length) + m_xindex);
a2 = a1;
b1 = (byte)table.GetValue(((m_yindex + 1) * xaxis.Length) + m_xindex);
b2 = b1;
}
else
{
a1 = (byte)table.GetValue((m_yindex * xaxis.Length) + m_xindex);
a2 = (byte)table.GetValue((m_yindex * xaxis.Length) + m_xindex + 1);
b1 = (byte)table.GetValue(((m_yindex + 1) * xaxis.Length) + m_xindex);
b2 = (byte)table.GetValue(((m_yindex + 1) * xaxis.Length) + m_xindex + 1);
}
//AddDebugLog("a1 = " + a1.ToString() + " a2 = " + a2.ToString() + " b1 = " + b1.ToString() + " b2 = " + b2.ToString());
// now interpolate the values found
double aval = 0;
double bval = 0;
double adiff = Math.Abs((double)a1 - (double)a2);
if (a1 > a2) aval = a1 - (m_xpercentage * adiff);
else aval = a1 + (m_xpercentage * adiff);
double bdiff = Math.Abs((double)b1 - (double)b2);
if (b1 > b2) bval = b1 - (m_xpercentage * bdiff);
else bval = b1 + (m_xpercentage * bdiff);
// now interpolate vertically (RPM axis)
//AddDebugLog("aval = " + aval.ToString() + " bval = " + bval.ToString());
double abdiff = Math.Abs(aval - bval);
if (aval > bval) result = aval - (m_ypercentage * abdiff);
else result = aval + (m_ypercentage * abdiff);
//AddDebugLog("result = " + result.ToString());
}
catch (Exception E)
{
Console.WriteLine("Failed to interpolate: " + E.Message);
}
return result;
}
/*private int AirmassToTorqueLbft(int IQ, int rpm)
{
double tq = Convert.ToDouble(IQ) * 6;
double correction = Tools.Instance.GetCorrectionFactorForRpm(rpm);
tq *= correction;
//tq /= 1.56; // to lbft
tq /= 1.3558; //<GS-22032010> bugfix
return Convert.ToInt32(tq);
}*/
private void CastStartViewerEvent(string mapname)
{
if (onStartTableViewer != null)
{
onStartTableViewer(this, new StartTableViewerEventArgs(mapname));
}
}
int powerSeries = -1;
int torqueSeries = -1;
private string MaximizeFileLength(string filename)
{
string retval = filename;
if (retval.Length > 16) retval = retval.Substring(0, 14) + "..";
return retval;
}
private void LoadGraphWithDetails()
{
if (gridControl1.DataSource != null)
{
DataTable dt = (DataTable)gridControl1.DataSource;
// get only the WOT cells, the last 16 integers
// and the columns which hold the rpm stages
chartControl1.Series.Clear();
string powerLabel = "Power (bhp)";
if (checkEdit5.Checked) powerLabel = "Power (kW)";
string torqueLabel = "Torque (Nm)";
if (checkEdit6.Checked) torqueLabel = "Torque (lbft)";
string injectorDCLabel = "Injector DC";
string targetLambdaLabel = "Target lambda";
powerSeries = chartControl1.Series.Add(powerLabel, DevExpress.XtraCharts.ViewType.Spline);
torqueSeries = chartControl1.Series.Add(torqueLabel, DevExpress.XtraCharts.ViewType.Spline);
// set line colors
chartControl1.Series[powerSeries].Label.Border.Visible = false;
chartControl1.Series[torqueSeries].Label.Border.Visible = false;
chartControl1.Series[powerSeries].Label.Shadow.Visible = true;
chartControl1.Series[torqueSeries].Label.Shadow.Visible = true;
chartControl1.Series[powerSeries].View.Color = Color.Red;
chartControl1.Series[torqueSeries].View.Color = Color.Blue;
chartControl1.Series[powerSeries].ArgumentScaleType = ScaleType.Qualitative;
chartControl1.Series[powerSeries].ValueScaleType = ScaleType.Numerical;
chartControl1.Series[torqueSeries].ArgumentScaleType = ScaleType.Qualitative;
chartControl1.Series[torqueSeries].ValueScaleType = ScaleType.Numerical;
SplineSeriesView sv = (SplineSeriesView)chartControl1.Series[powerSeries].View;
sv.LineMarkerOptions.Visible = false;
sv = (SplineSeriesView)chartControl1.Series[torqueSeries].View;
sv.LineMarkerOptions.Visible = false;
for (int i = 0; i < dt.Rows[0].ItemArray.Length; i++)
{
double o = Convert.ToDouble(dt.Rows[0].ItemArray.GetValue(i));
// convert to hp
int rpm = Convert.ToInt32(y_axisvalues.GetValue(i));
int torque = Tools.Instance.IQToTorque(Convert.ToInt32(o), rpm, m_numberCylinders);
if (_ECUType.Contains("EDC16"))
{
torque = Convert.ToInt32(o);
torque *= 10; // correction to keep the code identical from here
double temptorque = torque * Tools.Instance.GetCorrectionFactorForRpm(rpm, m_numberCylinders);
torque = Convert.ToInt32(temptorque);
}
int horsepower = Tools.Instance.TorqueToPower(torque, rpm);
if (checkEdit5.Checked) horsepower = Tools.Instance.TorqueToPowerkW(torque, rpm);
if (checkEdit6.Checked) torque = Tools.Instance.IQToTorque(Convert.ToInt32(o), rpm, m_numberCylinders);//AirmassToTorqueLbft(Convert.ToInt32(o), rpm);
horsepower /= 100;
torque /= 100;
double[] dvals = new double[1];
dvals.SetValue(Convert.ToDouble(horsepower), 0);
chartControl1.Series[powerSeries].Points.Add(new SeriesPoint(Convert.ToDouble(rpm), dvals));
double[] dvalstorq = new double[1];
dvalstorq.SetValue(Convert.ToDouble(torque), 0);
chartControl1.Series[torqueSeries].Points.Add(new SeriesPoint(Convert.ToDouble(rpm), dvalstorq));
}
}
}
private void UpdateGraphVisibility()
{
if (powerSeries >= 0) chartControl1.Series[powerSeries].Visible = checkEdit8.Checked;
if (torqueSeries >= 0) chartControl1.Series[torqueSeries].Visible = checkEdit9.Checked;
}
string m_current_softwareversion = string.Empty;
string m_current_comparefilename = string.Empty;
SymbolCollection Compare_symbol_collection = new SymbolCollection();
private void simpleButton3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Binary files|*.bin";
if (ofd.ShowDialog() == DialogResult.OK)
{
m_current_comparefilename = ofd.FileName;
SymbolTranslator translator = new SymbolTranslator();
string help = string.Empty;
FileInfo fi = new FileInfo(m_current_comparefilename);
fi.IsReadOnly = false;
try
{
m_current_softwareversion = "";
}
catch (Exception E2)
{
Console.WriteLine(E2.Message);
}
// show the dynograph
xtraTabControl1.SelectedTabPage = xtraTabPage2;
LoadGraphWithDetails(); // initial values from original bin
}
}
private void simpleButton2_Click(object sender, EventArgs e)
{
Calculate(m_currentfile, m_symbols);
}
private void simpleButton1_Click(object sender, EventArgs e)
{
CastCloseEvent();
}
public delegate void ViewerClose(object sender, EventArgs e);
public event ctrlAirmassResult.ViewerClose onClose;
private void CastCloseEvent()
{
if (onClose != null)
{
onClose(this, EventArgs.Empty);
}
}
private void checkEdit1_CheckedChanged(object sender, EventArgs e)
{
Calculate(m_currentfile, m_symbols);
}
private void checkEdit2_CheckedChanged(object sender, EventArgs e)
{
Calculate(m_currentfile, m_symbols);
}
private void checkEdit3_CheckedChanged(object sender, EventArgs e)
{
Calculate(m_currentfile, m_symbols);
}
private void checkEdit4_CheckedChanged(object sender, EventArgs e)
{
Calculate(m_currentfile, m_symbols);
}
private void spinEdit1_EditValueChanged(object sender, EventArgs e)
{
Calculate(m_currentfile, m_symbols);
}
private void comboBoxEdit2_SelectedIndexChanged(object sender, EventArgs e)
{
gridControl1.Refresh();
if (xtraTabControl1.SelectedTabPage.Name == xtraTabPage2.Name)
{
LoadGraphWithDetails();
}
}
private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
{
Calculate(m_currentfile, m_symbols);
}
private void checkEdit7_CheckedChanged(object sender, EventArgs e)
{
Calculate(m_currentfile, m_symbols);
}
private void checkEdit5_CheckedChanged(object sender, EventArgs e)
{
// refresh
//Calculate();
gridControl1.Refresh();
if (xtraTabControl1.SelectedTabPage.Name == xtraTabPage2.Name)
{
LoadGraphWithDetails();
}
}
private void checkEdit6_CheckedChanged(object sender, EventArgs e)
{
// refresh
//Calculate();
gridControl1.Refresh();
if (xtraTabControl1.SelectedTabPage.Name == xtraTabPage2.Name)
{
LoadGraphWithDetails();
}
}
private void labelControl8_DoubleClick(object sender, EventArgs e)
{