unit mainunit; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, CustomDrawnControls,Dos, Spin, StdCtrls, CustomDrawn_Common, ExtCtrls, TAGraph, TASeries, TATools, TAChartUtils, Types; type { Tmain } Tmain = class(TForm) Command: TEdit; Fluxevaul: TEdit; FluxChart: TChart; FluxChartConstantLine1: TConstantLine; FluxChartConstantLine2: TConstantLine; FluxChartLineSeries: TLineSeries; ChartToolset: TChartToolset; ChartToolsetDataPointDragTool: TDataPointDragTool; FluxmaxEdit: TFloatSpinEdit; FluxminEdit: TFloatSpinEdit; FluxmeasureButton: TCDButton; FluxsaveButton: TCDButton; Flux12Button: TCDButton; note: TEdit; temp: TEdit; OffsetEdit: TFloatSpinEdit; OffsetButton: TCDButton; HallcurrentEdit: TFloatSpinEdit; CoilcurrentButton: TCDButton; CoilcurrentEdit: TFloatSpinEdit; Coilcurrent: TEdit; Hallcurrent: TEdit; QuitButton: TCDButton; SaveDialog: TSaveDialog; SaveDialogflux: TSaveDialog; SelectDirectoryDialog: TSelectDirectoryDialog; SetfileButton: TCDButton; SaveButton: TCDButton; Timer: TTimer; Timer1: TTimer; Uhall: TEdit; Flux: TEdit; Force: TEdit; tareButton: TCDButton; HallcurrentButton: TCDButton; ManualButton: TCDButton; FluxzeroButton: TCDButton; procedure ChartToolsetDataPointDragToolAfterMouseUp(ATool: TChartTool; APoint: TPoint); procedure ChartToolsetDataPointDragToolDrag(ASender: TDataPointDragTool; var AGraphPoint: TDoublePoint); procedure CoilcurrentButtonClick(Sender: TObject); procedure CoilcurrentEditKeyPress(Sender: TObject; var Key: char); procedure CoilcurrentEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure CommandKeyPress(Sender: TObject; var Key: char); procedure Flux12ButtonClick(Sender: TObject); procedure FluxmaxEditKeyPress(Sender: TObject; var Key: char); procedure FluxmaxEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FluxmeasureButtonClick(Sender: TObject); procedure FluxminEditKeyPress(Sender: TObject; var Key: char); procedure FluxminEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FluxsaveButtonClick(Sender: TObject); procedure FluxzeroButtonClick(Sender: TObject); procedure FormActivate(Sender: TObject); procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); procedure FormCreate(Sender: TObject); procedure FormPaint(Sender: TObject); procedure HallcurrentButtonClick(Sender: TObject); procedure HallcurrentEditKeyPress(Sender: TObject; var Key: char); procedure HallcurrentEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure ManualButtonClick(Sender: TObject); procedure OffsetButtonClick(Sender: TObject); procedure OffsetEditKeyPress(Sender: TObject; var Key: char); procedure OffsetEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure QuitButtonClick(Sender: TObject); procedure SaveButtonClick(Sender: TObject); procedure SetfileButtonClick(Sender: TObject); procedure SlopeEditChange(Sender: TObject); procedure tareButtonClick(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure TimerTimer(Sender: TObject); private public end; var main: Tmain; version:longint=1; mainpath:string; paramsavefile:string='version'; implementation uses comUSB,connect; {$R *.lfm} type data_t=record I_coil:double; I_hall:double; U_hall:double; force:double; sforce:string; flux:double; int:double; time:double; temp:double; end; { Tmain } var savefile:String; savefluxfile:String; saveselect:boolean=false; {$ifdef LINUX} imagedirectory:string='measures/'; savedirectory:string='measures/'; bbs:string='/'; {$else} imagedirectory:string='measures\'; savedirectory:string='measures\'; bbs:string='\'; {$endif} curdata:data_t; forcezero:double=0; manualb:boolean=true; timepointer:longint=0; flux_data:array[0..300] of double; fluxrequest:boolean=false; fluxallowed:boolean=false; fluxpointer:longint=0; hours: word; minutes: word; seconds: word; milliseconds: word; connected:boolean=false; notconnected:boolean=false; function get_instdir:string; var ss:string; begin ss:=paramstr(0); {$IFDEF LINUX} get_instdir:=copy(ss,1,pos('susceptibility',ss)-1)+'susceptibility/'; mainpath:=getuserdir; //+'susceptibility/'; {$ELSE} mainpath:=copy(ss,1,pos('susceptibility.exe',ss)-1); get_instdir:=mainpath; {$ENDIF} imagedirectory:=mainpath+imagedirectory; savedirectory:=mainpath+savedirectory; writeln(mainpath); end; procedure drawrect; var x0,y0,x1,y1:longint; begin x0:=main.flux.Left-5; y0:=main.offsetbutton.Top-5; x1:=main.offsetedit.Left+main.offsetedit.Width+5; y1:=main.fluxmeasurebutton.Top+main.fluxmeasurebutton.Height+5; main.Canvas.Brush.Color := cldefault; main.Canvas.Font.Color:=clblack; main.Canvas.Font.size:=22; main.Canvas.TextOut(x0+60,y0+30,'Flux'); main.Canvas.Brush.Color := clBlack; main.Canvas.Brush.Style := bsSolid; main.Canvas.Pen.Style:=psSolid; main.Canvas.Pen.Color := clblack; main.Canvas.Pen.Width := 2; main.Canvas.MoveTo(x0,y0); main.Canvas.LineTo(x0,y1); // delta X = 2 main.Canvas.LineTo(x1,y1); // delta Y = 2 main.Canvas.LineTo(x1,y0); main.Canvas.LineTo(x0,y0); end; procedure initvalues; var res:longint; begin get_instdir; savedirectory:=mainpath+savedirectory; Main.SaveDialog.InitialDir:=savedirectory; Main.SaveDialogFlux.InitialDir:=savedirectory; main.Flux12Button.Caption:='I'; end; procedure Tmain.QuitButtonClick(Sender: TObject); begin manualb:=true; sendUSB('HRESET'); halt; end; procedure setsavefile; var f:Text; begin Main.SaveDialog.InitialDir:=savedirectory; if not Main.SaveDialog.Execute then exit; savefile:=Main.SaveDialog.FileName; saveselect:=true; {$I-} ioresult; assign(f,savefile); rewrite(f); writeln(f,' I_coil I_hall, U_hall, Force Flux'); close(f); end; procedure Tmain.SetfileButtonClick(Sender: TObject); begin setsavefile; end; procedure Tmain.SlopeEditChange(Sender: TObject); begin end; procedure Tmain.tareButtonClick(Sender: TObject); begin forcezero:=curdata.force; end; procedure Tmain.Timer1Timer(Sender: TObject); begin Timer1.Enabled:=false; Main.SelectDirectoryDialog.InitialDir:=mainpath+'measures'+bbs; Main.SelectDirectoryDialog.Execute; savedirectory:=Main.SelectDirectoryDialog.FileName+bbs; imagedirectory:=Main.SelectDirectoryDialog.FileName+bbs; end; procedure redraw; var i:longint; begin main.fluxchartlineseries.clear; for i:=1 to 150 do begin main.fluxchartlineseries.addXY(i/10.0,flux_data[i]); end; end; procedure readUSB; var s:string; code:longint; i,j:longint; s1,s2,s3:string; d:double; integral:double; begin case timepointer of 0: sendUSB( 'GETDATA'); 1: begin s:=getUSB(5000); if s<>'Data' then repeat s:=getUSB(500); writeln(s); until s='End'; if s='End' then exit; s:=getUSB(500); val(s,curdata.I_coil,code); s:=getUSB(500); val(s,curdata.I_hall,code); s:=getUSB(500); val(s,curdata.U_hall,code); s:=getUSB(500); curdata.sforce:=copy(s,1,2); s:=copy(s,3,20); val(s,curdata.force,code); s:=getUSB(500); val(s,curdata.temp,code); s:=getUSB(500); val(s,curdata.int,code); s:=getUSB(500); if s<>'End' then begin main.FluxChart.AxisList[0].range.Max:=main.fluxmaxedit.Value; main.FluxChart.AxisList[0].range.Min:=main.fluxminedit.Value; for i:=1 to 150 do begin s:=getUSB(500); write(i); writeln(' '+s); val(s,flux_data[i],code); end; //main.fluxevaul.text:=s; s:=getUSB(500); redraw; end; main.CoilCurrent.caption:='I_coil='+floatTostrF(curdata.I_coil,ffFixed,10,3)+' A'; main.Hallcurrent.caption:='I_hall='+floatTostrF(curdata.I_hall,ffFixed,10,3)+' mA'; main.Uhall.caption:='U_hall='+floatTostrF(curdata.u_hall,ffFixed,10,1)+' mV'; main.Flux.caption:='Flux='+floatTostrF(curdata.int,ffFixed,10,2)+' mVs'; main.Force.caption:=curdata.sforce+'M='+floatTostrF(curdata.force-forcezero,ffFixed,10,4)+' g'; main.temp.caption:='T='+floatTostrF(curdata.temp,ffFixed,10,1)+' C'; end; end; inc(timepointer); if timepointer>3 then timepointer:=0; end; procedure StartClock; begin GetTime(hours, minutes, seconds, milliseconds); end; function StopClock:word; var seconds_count : longint; c_hours: word; c_minutes: word; c_seconds: word; c_milliseconds: word; begin GetTime(c_hours, c_minutes, c_seconds, c_milliseconds); seconds_count := c_seconds - seconds + (c_minutes - minutes) * 60 + (c_hours - hours) * 3600; stopclock:=seconds_count; end; procedure Tmain.TimerTimer(Sender: TObject); var t:longint; begin if notconnected then exit; if not connected then begin Showmessage('Cannot connect to USB'); notconnected:=true; halt; end; if connectform.visible then connectform.visible:=false; readUSB; if fluxrequest and (timepointer=0)then begin inc(fluxpointer); if fluxpointer=1 then begin //sendUSB('OFFSET '+floatTostrF(main.offsetedit.Value,ffFixed,10,3)); //sendUSB('FLUXDATA 100'); Startclock; end; t:=stopclock; if t>16 then begin fluxrequest:=false; main.fluxmeasurebutton.color:=clsilver; main.FluxmeasureButton.caption:='Flux measure'; end else begin main.FluxmeasureButton.caption:=floatTostrF(16-t,ffFixed,6,2); end; end; end; procedure savedata; var f:text; begin assign(f,savefile); append(f); writeln(f,main.note.Caption); write(f,curdata.I_coil:14:6); write(f,curdata.I_hall:14:6); write(f,curdata.U_hall:14:6); write(f,curdata.force-forcezero:14:6); writeln(f,curdata.flux:14:6); close(f); end; procedure Tmain.SaveButtonClick(Sender: TObject); begin if not saveselect then setsavefile; savedata; end; procedure Tmain.FormActivate(Sender: TObject); begin end; procedure Tmain.FormCreate(Sender: TObject); begin initvalues; if startUSB<0 then begin connected:=false; main.Timer.Enabled:=true; exit; end; connected:=true; manualb:=false; sendUSB('MANUAL 0'); sendUSB('CURRENT 0'); sendUSB('CURRENT_HULL 5'); sendUSB('OFFSET 0'); sendUSB('FLUX12 1'); main.Timer.Enabled:=true; if paramcount>0 then main.command.visible:=true; end; procedure Tmain.FormPaint(Sender: TObject); begin drawrect; end; procedure Tmain.HallcurrentButtonClick(Sender: TObject); begin if manualb then exit; sendUSB('CURRENT_HULL '+floatTostrF(main.hallcurrentedit.Value,ffFixed,10,3)); end; procedure Tmain.CoilcurrentEditKeyPress(Sender: TObject; var Key: char); begin if manualb then exit; if byte(key) =13 then sendUSB('CURRENT '+floatTostrF(main.coilcurrentedit.Value,ffFixed,10,3)); end; procedure Tmain.ChartToolsetDataPointDragToolDrag(ASender: TDataPointDragTool; var AGraphPoint: TDoublePoint); var y:double; begin y:= AGraphPoint.y; AGraphPoint.y:=y; if ASender <> ChartToolsetDataPointDragTool then exit; end; procedure Tmain.CoilcurrentButtonClick(Sender: TObject); begin if manualb then exit; sendUSB('CURRENT '+floatTostrF(main.coilcurrentedit.Value,ffFixed,10,3)); end; procedure Tmain.ChartToolsetDataPointDragToolAfterMouseUp(ATool: TChartTool; APoint: TPoint); begin curdata.flux:=FluxChartConstantLine2.position-FluxChartConstantLine1.position; main.Fluxevaul.caption:='Flux='+floatTostrF(curdata.flux,ffFixed,10,2)+' mVs'; end; procedure Tmain.CoilcurrentEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin sendUSB('CURRENT '+floatTostrF(main.coilcurrentedit.Value,ffFixed,10,3)); end; procedure Tmain.CommandKeyPress(Sender: TObject; var Key: char); begin if byte(key)=13 then begin sendUSB(command.text); end; end; procedure Tmain.Flux12ButtonClick(Sender: TObject); begin if main.Flux12Button.Caption='I' then begin main.Flux12Button.Caption:='II'; sendUSB('FLUX12 2'); end else begin main.Flux12Button.Caption:='I'; sendUSB('FLUX12 1'); end; end; procedure setfluxmax; var y0,y1:double; begin if main.fluxmaxedit.value<=main.fluxminedit.value then main.fluxmaxedit.value:=main.fluxminedit.value+0.001; main.FluxChart.AxisList[0].Range.Min:=main.fluxminedit.value; main.FluxChart.AxisList[0].Range.Max:=main.fluxmaxedit.value; y0:=main.FluxChart.AxisList[0].Range.Min; y1:=main.FluxChart.AxisList[0].Range.Max; main.FluxChartConstantLine1.Position:=y0+0.25*(y1-y0); main.FluxChartConstantLine2.Position:=y0+0.75*(y1-y0); end; procedure setfluxmin; var y0,y1:double; begin if main.fluxminedit.value>=main.fluxmaxedit.value then main.fluxminedit.value:=main.fluxmaxedit.value-0.001; main.FluxChart.AxisList[0].Range.Min:=main.fluxminedit.value; main.FluxChart.AxisList[0].Range.Max:=main.fluxmaxedit.value; y0:=main.FluxChart.AxisList[0].Range.Min; y1:=main.FluxChart.AxisList[0].Range.Max; main.FluxChartConstantLine1.Position:=y0+0.25*(y1-y0); main.FluxChartConstantLine2.Position:=y0+0.75*(y1-y0); end; procedure Tmain.FluxmaxEditKeyPress(Sender: TObject; var Key: char); begin if byte(key) =13 then setfluxmax; end; procedure Tmain.FluxmaxEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin setfluxmax; end; procedure Tmain.FluxminEditKeyPress(Sender: TObject; var Key: char); begin if byte(key) =13 then setfluxmin; end; procedure Tmain.FluxminEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin setfluxmin; end; procedure Tmain.FluxmeasureButtonClick(Sender: TObject); var i:longint=0; y0,y1:double; begin if main.fluxmeasurebutton.color=clred then exit; sendUSB('OFFSET '+floatTostrF(main.offsetedit.Value,ffFixed,10,3)); sendUSB('CLEAR_C'); fluxrequest:=true; fluxpointer:=0; main.fluxmeasurebutton.color:=clred; main.FluxChart.AxisList[1].Range.Min:=0; main.FluxChart.AxisList[1].Range.Max:=15; main.FluxChart.AxisList[0].Range.Min:=main.fluxminedit.value; main.FluxChart.AxisList[0].Range.Max:=main.fluxmaxedit.value; y0:=main.FluxChart.AxisList[0].Range.Min; y1:=main.FluxChart.AxisList[0].Range.Max; main.FluxChartConstantLine1.Position:=y0+0.25*(y1-y0); main.FluxChartConstantLine1.Position:=0; main.FluxChartConstantLine2.Position:=y0+0.75*(y1-y0); curdata.flux:=0.0; main.Flux.caption:='Flux='+floatTostrF(curdata.flux,ffFixed,10,2)+' mVs'; end; procedure setsavefluxfile; var f:Text; i:longint; begin Main.SaveDialogflux.InitialDir:=savedirectory; if not Main.SaveDialogflux.Execute then exit; savefluxfile:=Main.SaveDialogflux.FileName; {$I-} ioresult; assign(f,Main.SaveDialogflux.FileName); rewrite(f); for i:=1 to 150 do writeln(f,flux_data[i]); close(f); end; procedure Tmain.FluxsaveButtonClick(Sender: TObject); begin setsavefluxfile; end; procedure Tmain.FluxzeroButtonClick(Sender: TObject); begin sendUSB('CLEAR_C'); end; procedure Tmain.FormClose(Sender: TObject; var CloseAction: TCloseAction); begin sendUSB('RESET'); halt; end; procedure Tmain.HallcurrentEditKeyPress(Sender: TObject; var Key: char); begin if manualb then exit; if byte(key) =13 then sendUSB('CURRENT_HULL '+floatTostrF(main.hallcurrentedit.Value,ffFixed,10,3)); end; procedure Tmain.HallcurrentEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin sendUSB('CURRENT_HULL '+floatTostrF(main.hallcurrentedit.Value,ffFixed,10,3)); end; procedure Tmain.ManualButtonClick(Sender: TObject); begin if manualb then begin manualb:=false; main.manualButton.caption:='Remote'; sendUSB('MANUAL 0'); sendUSB('CURRENT '+IntToStr(Trunc(main.Coilcurrentedit.Value))); main.coilCurrentButton.color:=clSilver; main.hallCurrentButton.color:=clSilver; main.FluxzeroButton.color:=clSilver; end else begin manualb:=true; main.manualButton.caption:='Manual'; sendUSB('MANUAL 1'); main.CoilcurrentButton.color:=clWhite; main.hallcurrentButton.color:=clWhite; main.FluxzeroButton.color:=clWhite; end; end; procedure Tmain.OffsetButtonClick(Sender: TObject); begin if manualb then exit; sendUSB('OFFSET '+floatTostrF(main.offsetedit.Value,ffFixed,10,3)); end; procedure Tmain.OffsetEditKeyPress(Sender: TObject; var Key: char); begin if manualb then exit; if byte(key) =13 then sendUSB('OFFSET '+floatTostrF(main.offsetedit.Value,ffFixed,10,3)); end; procedure Tmain.OffsetEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin sendUSB('OFFSET '+floatTostrF(main.offsetedit.Value,ffFixed,10,3)); end; end.