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) FluxChart: TChart; FluxChartConstantLine1: TConstantLine; FluxChartConstantLine2: TConstantLine; FluxChartLineSeries: TLineSeries; ChartToolset: TChartToolset; ChartToolsetDataPointDragTool: TDataPointDragTool; FluxmaxEdit: TFloatSpinEdit; Fluxevaul: TEdit; FluxminEdit: TFloatSpinEdit; FluxmeasureButton: TCDButton; FluxevaulButton: TCDButton; FluxsaveButton: TCDButton; Flux12Button: TCDButton; note: 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 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 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 tareButtonClick(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure TimerTimer(Sender: TObject); private public end; var main: Tmain; implementation uses comUSB,connect; {$R *.lfm} type data_t=record I_coil:double; I_hall:double; U_hall:double; force:double; flux:double; int:double; time:double; temp:double; end; { Tmain } var savefile:String; savefluxfile:String; saveselect:boolean=false; mainpath:string; {$ifdef LINUX} imagedirectory:string='prints/'; savedirectory:string='measures/'; bbs:string='/'; {$else} imagedirectory:string='prints\'; 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/'; {$ELSE} get_instdir:=copy(ss,1,pos('susceptibility.exe',ss)-1); {$ENDIF} mainpath:=get_instdir; end; procedure initvalues; begin 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('RESET'); halt; end; procedure setsavefile; var f:Text; begin 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.tareButtonClick(Sender: TObject); begin forcezero:=curdata.force; end; procedure Tmain.Timer1Timer(Sender: TObject); begin Timer1.Enabled:=false; Main.SelectDirectoryDialog.InitialDir:=mainpath; Main.SelectDirectoryDialog.Execute; savedirectory:=Main.SelectDirectoryDialog.FileName+bbs; imagedirectory:=Main.SelectDirectoryDialog.FileName+bbs; 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); 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; main.fluxchartlineseries.clear; for i:=1 to 150 do begin s:=getUSB(500); //write(i); //writeln(' '+s); val(s,flux_data[i],code); flux_data[i]:=flux_data[i]/1000.0; main.fluxchartlineseries.addXY(i/10.0,flux_data[i]); end; s:=getUSB(500); 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:='Int='+floatTostrF(curdata.int,ffFixed,10,2)+' mVs'; main.Force.caption:='M='+floatTostrF(curdata.force-forcezero,ffFixed,10,4)+' g'; 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; if fluxrequest and (timepointer=0)then begin inc(fluxpointer); if fluxpointer=1 then begin 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 else readUSB; 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; 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:=FluxChartConstantLine1.position-FluxChartConstantLine2.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.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 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.FluxChartConstantLine2.Position:=y0+0.75*(y1-y0); curdata.flux:=1.0; main.Fluxevaul.caption:='Flux='+floatTostrF(curdata.flux,ffFixed,10,2)+' mVs'; end; procedure setsavefluxfile; var f:Text; i:longint; begin 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.