forked from P33a/SimTwo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastChart.pas
487 lines (416 loc) · 13.4 KB
/
FastChart.pas
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
unit FastChart;
{$MODE Delphi}
interface
uses
SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, IniFiles, Menus, ProjConfig,
ComCtrls, IniPropStorage, TAGraph, TASeries;
type
TSeriesFunction = function(r,i: integer): Double;
TSeriesDef = class(TComponent)
public
RobotNum: integer;
AxisNum: integer;
func: TSeriesFunction;
end;
type
{ TFChart }
TFChart = class(TForm)
Chart: TChart;
CBFreeze: TCheckBox;
CBZeroCross: TCheckBox;
IniPropStorage: TIniPropStorage;
Label1: TLabel;
EditMaxPoints: TEdit;
BSet: TButton;
BSave: TButton;
EditFileName: TEdit;
PageControl: TPageControl;
PanelChart: TPanel;
TabUserCharts: TTabSheet;
TabTimeChart: TTabSheet;
TreeView: TTreeView;
ILCheckBox: TImageList;
BChartRefresh: TButton;
BSaveLog: TButton;
BClear: TButton;
procedure CBZeroCrossChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BSetClick(Sender: TObject);
procedure CBFreezeClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure BSaveClick(Sender: TObject);
procedure TreeViewMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure BChartRefreshClick(Sender: TObject);
procedure BSaveLogClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BClearClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
procedure FillTreeView(tree: TTreeView);
procedure FillRobotTreeView(r: integer; root: TTreeNode; tree: TTreeView);
procedure FillTreeViewItem(r, i: integer; str: string; func: TSeriesFunction; root: TTreeNode; tree: TTreeView);
procedure RefreshChart(tree: TTreeView);
function GetNodePathText(tree: TTreeView; node: TTreeNode): string;
procedure RemoveAllSeries(tree: TTreeView);
public
SeriesNameList: TStringList;
MaxPoints : integer;
physTime_zero: double;
procedure AddSeriesValue(var Series: TLineSeries; X, Y: double);
procedure AddPoint(x, r, y, u: double);
procedure AddSample(r: integer; t: double);
procedure FormSave(ProjMemIni : TMemIniFile);
procedure FormLoad(ProjMemIni : TMemIniFile);
end;
procedure ExpandNodePath(node: TTreeNode);
var
FChart: TFChart;
UserCharts: array of TChart;
UserChartsCount: integer;
implementation
uses Params, ODERobotsPublished, Viewer, ODERobots;
{$R *.lfm}
function RandColor(i: integer): TColor;
begin
RandSeed:=i;
random(256);
result:=random(128) or (random(128) shl 8) or (random(128) shl 16);
end;
procedure TFChart.FormSave(ProjMemIni : TMemIniFile);
begin
//ProjMemIni.WriteInteger('Chart','ActiveTab',PageControl.ActivePageIndex);
ProjMemIni.WriteString('Chart','MaxPoints',EditMaxPoints.text);
//SaveFormGeometryToMemIni(ProjMemIni,FChart);
end;
procedure TFChart.FormLoad(ProjMemIni : TMemIniFile);
begin
//PageControl.ActivePageIndex := ProjMemIni.ReadInteger('Main','ActiveTab',PageControl.ActivePageIndex);
EditMaxPoints.text:= ProjMemIni.ReadString('Chart','MaxPoints',EditMaxPoints.text);
//LoadFormGeometryFromMemIni(ProjMemIni,FChart);
end;
procedure TFChart.FormCreate(Sender: TObject);
var i: integer;
begin
SeriesNameList := TStringList.Create;
IniPropStorage.IniFileName := GetIniFineName(copy(name, 2, MaxInt));
physTime_zero := 0;
UserChartsCount := 16;
setlength(UserCharts, UserChartsCount);
for i := 0 to UserChartsCount -1 do begin
UserCharts[i] := TChart.Create(FChart);
UserCharts[i].Align := alClient;
end;
UserCharts[0].Parent := PanelChart;
end;
procedure TFChart.CBZeroCrossChange(Sender: TObject);
begin
Chart.LeftAxis.Range.UseMax := CBZeroCross.Checked;
Chart.LeftAxis.Range.UseMin := CBZeroCross.Checked;
end;
procedure TFChart.AddSeriesValue(var Series: TLineSeries; X,Y: double);
begin
if Series.Count > MaxPoints then begin
Series.Delete(0);
end;
Series.AddXY(X, Y);
end;
procedure TFChart.AddPoint(x,r,y,u: double);
begin
if CBFreeze.Checked then exit;
end;
procedure TFChart.BSetClick(Sender: TObject);
begin
MaxPoints := strtointdef(EditMaxPoints.Text, MaxPoints);
end;
procedure TFChart.CBFreezeClick(Sender: TObject);
var i: integer;
begin
Chart.ZoomFull();
if not CBFreeze.Checked then begin
for i := 0 to Chart.SeriesCount -1 do begin
(Chart.Series[i] as TLineSeries).Clear;
end;
physTime_zero := WorldODE.physTime;
end;
end;
procedure TFChart.FormShow(Sender: TObject);
var fname: string;
begin
MaxPoints := strtointdef(EditMaxPoints.Text, 400);
fname := ChangeFileExt(IniPropStorage.IniFileName,'.LogSeries.txt');
if fileexists(fname) then
SeriesNameList.LoadFromFile(fname);
FillTreeView(TreeView);
RefreshChart(TreeView);
end;
procedure TFChart.AddSample(r: integer; t: double);
var i: integer;
v: double;
df: TSeriesDef;
LS: TLineSeries;
begin
if CBFreeze.Checked then exit;
for i := 0 to Chart.SeriesCount - 1 do begin
LS := Chart.Series[i] as TLineSeries;
with LS do begin
if tag = 0 then continue;
df := TSeriesDef(Tag);
if df.RobotNum = r then begin
v := df.func(df.RobotNum, df.AxisNum);
AddXY(t - physTime_zero, v);
while Count > MaxPoints do Delete(0);
end;
end;
end;
end;
procedure TFChart.BSaveClick(Sender: TObject);
var s: string;
begin
//Chart.PrintResolution := -100;
s := ChangeFileExt(EditFileName.Text, '.emf');
Chart.SaveToBitmapFile(s);
//Chart.PrintResolution := 0;
end;
procedure TFChart.FillTreeViewItem(r, i: integer; str: string; func:TSeriesFunction; root: TTreeNode; tree: TTreeView);
var node: TTreeNode;
df: TSeriesDef;
begin
df := TSeriesDef.Create(tree); //Warning: the TSeriesDef is being owned by the tree
df.RobotNum := R;
df.AxisNum := i;
df.func := func;
node:= tree.Items.AddChild(root, str);
node.Data:=df;
end;
procedure TFChart.FillRobotTreeView(r: integer; root: TTreeNode; tree: TTreeView);
var node, sub_nonde: TTreeNode;
i: integer;
begin
with tree.Items do begin
node:=AddChild(root,'State');
node.Data:=nil;
//FillRobotStateTreeView(num,node,tree);
FillTreeViewItem(r, 0, 'x', @GetSolidX, node, tree);
FillTreeViewItem(r, 0, 'y', @GetSolidY, node, tree);
FillTreeViewItem(r, 0, 'z', @GetSolidZ, node, tree);
FillTreeViewItem(r, 0, 'theta', @GetSolidTheta, node, tree);
FillTreeViewItem(r, 0, 'Vx', @GetSolidVx, node, tree);
FillTreeViewItem(r, 0, 'Vy', @GetSolidVy, node, tree);
FillTreeViewItem(r, 0, 'Vz', @GetSolidVz, node, tree);
node:=AddChild(root,'Axes');
node.Data:=nil;
for i := 0 to WorldODE.Robots[r].Axes.Count - 1 do begin
sub_nonde:=AddChild(node, WorldODE.Robots[r].Axes[i].ParentLink.description);
sub_nonde.Data:=nil;
//FillRobotLinksStateTreeView(num, i,sub_nonde,tree);
FillTreeViewItem(r, i, 'Pos', @GetAxisPosDeg, sub_nonde, tree);
FillTreeViewItem(r, i, 'Speed', @GetAxisSpeed, sub_nonde, tree);
FillTreeViewItem(r, i, 'T', @GetAxisTorque, sub_nonde, tree);
FillTreeViewItem(r, i, 'I', @GetAxisI, sub_nonde, tree);
FillTreeViewItem(r, i, 'U', @GetAxisU, sub_nonde, tree);
FillTreeViewItem(r, i, 'Power', @GetAxisUIPower, sub_nonde, tree);
//FillTreeViewItem(r, i, 'Mech Power', @GetAxisTWPower, sub_nonde, tree);
FillTreeViewItem(r, i, 'Pos ref', @GetAxisPosRefDeg, sub_nonde, tree);
FillTreeViewItem(r, i, 'Speed ref', @GetAxisSpeedRef, sub_nonde, tree);
FillTreeViewItem(r, i, 'Motor Speed', @GetAxisMotorSpeed, sub_nonde, tree);
FillTreeViewItem(r, i, 'Motor Pos', @GetAxisMotorPosDeg, sub_nonde, tree);
end;
end;
end;
procedure TFChart.FillTreeView(tree: TTreeView);
var node: TTreeNode;
i: integer;
begin
tree.Items.Clear;
with tree.Items do begin
//node:=Add(nil,'General');
//node.Data:=nil;
//FillGeneralTreeView(node,tree);
//node:=Add(nil,'Ball');
//node.Data:=nil;
//FillBallTreeView(node,tree);
for i:=0 to WorldODE.Robots.Count-1 do begin
//node:=Add(nil,'Robot '+inttostr(i+1));
node:=Add(nil,WorldODE.Robots[i].ID);
node.Data:=nil;
FillRobotTreeView(i,node,tree);
end;
for i:=0 to Count-1 do begin
if tree.Items[i].Data=nil then begin
tree.Items[i].ImageIndex:=2
end else begin
tree.Items[i].ImageIndex:=0;
end;
tree.Items[i].SelectedIndex := tree.Items[i].ImageIndex;
tree.Items[i].StateIndex:=-1;
if SeriesNameList.IndexOf(GetNodePathText(tree, tree.Items[i])) >= 0 then begin
tree.Items[i].ImageIndex:=1;
//tree.Items[i].Expand(true);
ExpandNodePath(tree.Items[i]);
end;
end;
end;
end;
procedure TFChart.TreeViewMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var node: TTreeNode;
HT: THitTests;
begin
node:=(Sender as TTreeView).GetNodeAt(X,Y);
if node=nil then exit;
if Button=mbLeft then begin
HT:=(Sender as TTreeView).GetHitTestInfoAt(X,Y);
if htOnIndent in HT then exit;
if node.ImageIndex=0 then begin
node.ImageIndex:=1;
node.SelectedIndex:=1;
end else begin
if node.ImageIndex=1 then begin
node.ImageIndex:=0;
node.SelectedIndex:=0;
end;
end;
end else begin
if node.Count=0 then begin
if pos('''',node.Text)<>0 then begin
node.Text:=copy(node.Text,1,length(node.Text)-1);
end else begin
node.Text:=node.Text+'''';
end;
{PointerToOffsetType(node.Data,offset,tipo);
if tipo=logDouble then begin
if pos('''',node.Text)<>0 then begin
node.Text:=copy(node.Text,1,length(node.Text)-1);
end else begin
node.Text:=node.Text+'''';
end;
end;}
end;
end;
TreeView.Refresh;
end;
function TFChart.GetNodePathText(tree: TTreeView; node: TTreeNode): string;
begin
result := '';
while node <> nil do begin
result := node.Text + '.' + result;
node := node.Parent;
end;
end;
procedure ExpandNodePath(node: TTreeNode);
begin
while node <> nil do begin
node.Expand(false);
node := node.Parent;
end;
end;
procedure TFChart.RemoveAllSeries(tree: TTreeView);
var i: integer;
cs: TBasicChartSeries;
// df: TSeriesDef;
// node: TTreeNode;
// derivative: boolean;
begin
SeriesNameList.Clear;
// clear chart series
with Chart do begin
While Series.Count>0 do begin
cs:=Series[0];
RemoveSeries(cs);
cs.Free;
end;
end;
with tree.Items do begin
for i:=0 to Count-1 do begin
if tree.Items[i].ImageIndex=1 then begin
tree.Items[i].ImageIndex :=0 ;
tree.Items[i].SelectedIndex := 0;
end;
end;
end;
end;
procedure TFChart.RefreshChart(tree: TTreeView);
var i{, cnt}: integer;
cs: TLineSeries;
df: TSeriesDef;
rcs: TBasicChartSeries;
begin
SeriesNameList.Clear;
with tree.Items do begin
// clear chart series
with Chart do begin
While Series.Count>0 do begin
rcs := Series[0];
RemoveSeries(Series[0]);
rcs.Free;
end;
end;
//cnt:=0;
for i:=0 to Count-1 do begin
if tree.Items[i].ImageIndex=1 then begin
//Inc(cnt);
// create new chart series for each variable
cs:=TLineSeries.Create(FChart);
cs.SeriesColor:=RandColor(i);
cs.Title := GetNodePathText(tree, tree.Items[i]);
df := TSeriesDef(tree.Items[i].Data);
cs.Tag := PtrUInt(df);
Chart.AddSeries(cs);
SeriesNameList.Add(cs.Title);
end;
end;
end;
end;
procedure TFChart.BChartRefreshClick(Sender: TObject);
begin
RefreshChart(TreeView);
end;
procedure TFChart.BSaveLogClick(Sender: TObject);
var cs: TLineSeries;
i, c, TotLines, TotCols: integer;
str: string;
sl: TStringList;
begin
with Chart do begin
if Series.Count <= 0 then exit;
//if SaveDialogLog.Execute then begin
// SaveDialogLog.InitialDir:=ExtractFilePath(SaveDialogLog.FileName);
TotCols := Series.Count;
TotLines := (Series[0] as TLineSeries).Count;
sl:=TStringList.Create;
try
for i := 0 to TotLines - 1 do begin
str := '';
for c := 0 to TotCols - 1 do begin
cs:=Series[c] as TLineSeries;
str := str + format('%g ',[cs.YValue[i]]);
end;
sl.Add(str);
end;
sl.SaveToFile(ChangeFileExt(EditFileName.Text, '.log'));
//sl.SaveToFile(SaveDialogLog.FileName);
finally
sl.Free;
end;
end;
end;
procedure TFChart.FormDestroy(Sender: TObject);
begin
SeriesNameList.SaveToFile(ChangeFileExt(IniPropStorage.IniFileName,'.LogSeries.txt'));
SeriesNameList.Free;
end;
procedure TFChart.BClearClick(Sender: TObject);
begin
RemoveAllSeries(TreeView);
end;
procedure TFChart.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (ssctrl in Shift) then begin
if (ssShift in Shift) and (key = ord('T')) then CBFreeze.Checked := not CBFreeze.Checked;
end;
end;
end.