public static M1 Bind <M0, M1, T0, T1, T2>(this TEither <T0, T1> .ParentType <M0> monad, Func <T0, IMonad <M1, T2, T1> > m) where M0 : IMonad <M0, T0, T1> where M1 : IMonad <M1, T2, T1> where T0 : class where T1 : class where T2 : class { return((M1)monad.Match(m, (e) => new Factory <M1, T1>(monad, e).Instance())); }
public static Either <T0, T2> Fmap <F0, T0, T1, T2>( this TEither <T0, T1> .P <F0> e, Func <T1, T2> f ) where F0 : TFunctor <T1> .T <T0> .P <F0> { return(e .Match( (l) => new Factory <Either <T0, T2> >(l).Instance(), (r) => new Factory <Either <T0, T2> >(f(r)).Instance() )); }
public static M1 Bind <M0, M1, T0, T1, T2>( this TEither <T0, T1> .P <M0> e, Func <T1, TMonad <T2> .T <T0> .P <M1> > fm ) where M0 : TMonad <T1> .T <T0> .P <M0> { return(e .Match( (l) => new Factory <M1>(l).Instance(), (r) => (M1)fm(r) )); }
public static Either <T0, TR> Apply <A0, A1, T0, TI, TR>( this TEither <T0, IFunc <TI, TR> > .P <A0> a, TEither <T0, TI> .P <A1> fm ) where A0 : TApplicative <IFunc <TI, TR> > .T <T0> .P <A0> where A1 : TApplicative <TI> .T <T0> .P <A1> { return(a.Match( (l) => new Factory <Either <T0, TR> >(l).Instance(), (r) => fm .Match( (ll) => new Factory <Either <T0, TR> >(ll).Instance(), (rr) => new Factory <Either <T0, TR> >(r.Execute(rr)).Instance() ) )); }
public bool OpHome() { var resHome = new TEither(Stg as IStgCtrl, 12) .Bind(x => x.Act(f => f.SendAndReady(f.Home + Axis.W.ToIdx())).ToTEither(12), "R or X Home Fail"); //.Bind( x => x.Act( f => // f.SendAndReady( f.GoAbs // + 0.ToOffPos(Axis.X) ) ) // .ToTEither( 1 ) , "Opposit Movement Fail" ) //.Bind( x => x.Act( f => // f.SendAndReady( f.Go ) ).ToTEither( 1 ) , "Stage Movement Fail" ); if (!resHome.IsRight) { return(false.Act(x => FlgHomeDone = false)); } return(true.Act(x => FlgHomeDone = true)); }
public async Task <bool> ScanAutoRun() // Use Internal Config , not get config from method parameter { LoadModel(ModelFullPath); OpMaxSpeed(); OpORGMaxSpeed(); counter = 0; SetHWInternalParm( Config.RStgSpeed, Config.XStgSpeed, Config.Scan2Avg, Config.IntegrationTime, Config.Boxcar, Config.EdgeEnd ); // Ref Check -- if (!FlgRefReady) { return(false.Act(x => MessageBox.Show("Set Referance Please"))); } // Home And Check -- //if ( !FlgHomeDone ) if ( !OpHome() ) return false; OpHome(); FlgHomeDone = true; // Scan Ready And Check -- if (FlgDarkReady == false) { if (!OpReady(ScanReadyMode.Dark)) { return(false); } } FlgDarkReady = true; var toReflect = FnCalReflections(Darks, Refs, SelectedReflctFactors); var toSelected = FnIdxDataPicker(PickedIdx); var thetas = GetPosThetas(ScanPos); var calcTaskList = new Task <Tuple <PlrCrd, LEither <double>, double[]> > [ScanPos.ThetaList.Select(x => x.Length).Aggregate((f, s) => f + s)]; var wavelength = SDWaves; var res = new TEither(Stg as IStgCtrl, 12); // Controller , Timeout // Get Pos-Intensity || Task Calc Thickness -- int taskcounter = 0; try { var posIntenlist = ScanPos.RhoList.Select((rho, i) => { var logs = res.Bind(x => x.Act(f => { f.SendAndReady(f.GoAbs + rho.mmToPulse().ToOffPos(Axis.X)); f.SendAndReady(f.Go); }).ToTEither(), " X Stage Move Command Fail"); return(thetas[i].Select((theta, j) => { var pos = new PlrCrd(theta, rho).Print(); var logres = logs.Bind(x => x.Act(f => { f.SendAndReady(f.GoAbs + ((ScanPos.MovePosList[i][j] + 360 * (i > 1 ? i - 1 : 0))).Degree2Pulse().ToPos(Axis.R)); f.SendAndReady(f.Go); Thread.Sleep(500); }).ToTEither(), "R Stage Move Command Fail") .ToLEither(new double [] { }); //Thread.Sleep(SpectrometerDelayTime); var intenlist = logres.IsRight // Todo : Change to Match Function //? logres.Bind( x => toSelected( BkD_Spctrm ) ) ? logres.Bind(x => toSelected(GetSpectrum())) // Todo : Change to only toSelected( Spctr.GetSpectrum() ), not bind. cuz endpoint : logres.Act(x => Lggr.Log(x.Left, true)); // Logging Error evtSpectrum(intenlist.Right, SelectedWaves); var temp1 = ToThickness( toReflect(intenlist.Right) .Act(x => evtRefleectivity(x, SelectedWaves)) .ToLEither(intenlist.Left), wavelength, pos); // Estimate Thickness calcTaskList [taskcounter++] // Clac Thickness = Task.Run <Tuple <PlrCrd, LEither <double>, double[]> >( () => logres.IsRight ? ToThickness( toReflect(intenlist.Right) .Act(x => evtRefleectivity(x, SelectedWaves)) .ToLEither(intenlist.Left), wavelength, pos) // Estimate Thickness : Tuple.Create(pos, new LEither <double>(), new double[] {})); return Tuple.Create(pos, intenlist); }).ToList()); }).SelectMany(x => x).ToList(); // List [ Result , Result , Result ] // Thickness Result List -- // ( You dont need to use try catch pattern for catch all exception from tasklist ) //var posThicknesses = Task.WhenAll( calcTaskList ).Result.Duplicate0ToAllTheta().ToList(); var posThickReflec = Task.WhenAll(calcTaskList).Result.ToList(); int nochiIdx = 0; for (int i = 0; i < posThickReflec.Count; i++) { if (posThickReflec [i].Item1.Rho == 148) { if (posThickReflec [i].Item1.Theta == 0) { nochiIdx = i; } } } posThickReflec [nochiIdx] = Tuple.Create( posThickReflec [nochiIdx].Item1, ((posThickReflec.Last().Item2.Right + posThickReflec [nochiIdx + 1].Item2.Right) / 2.0) .ToLEither(posThickReflec [nochiIdx].Item2.Left), (posThickReflec.Last().Item3.Zip( posThickReflec [nochiIdx + 1].Item3, (f, s) => (f + s) / 2.0) ).ToArray()); // Reset Pos -- await Task.Run(() => res.Bind( x => x.Act(f => f.SendAndReady(f.GoAbs + 0.ToPos(Axis.W) + 0.ToPos())).ToTEither(1)) .Bind(x => x.Act(f => f.SendAndReady(f.Go)).ToTEither(1))); FlgAutoUpdate = false; //Zip ( Pos , Thickness , Intencity ) -- var posThickIntenReflc = posThickReflec .Zip(posIntenlist, (f, s) => new { Pos = f.Item1, Thickness = f.Item2, Intensity = s.Item2, Refelctivity = f.Item3 }); // -- Integration Log Result -- // bool isright = posThickReflec.Select(x => x.Item2.IsRight).Aggregate((f, s) => f && s); if (!isright) // If Task Error { var tasklogs = posThickIntenReflc.Where(x => x.Thickness.IsRight == false) .Select(x => x.Pos.ToString() + " ||" + x.Thickness.Left) .ActLoop(x => Lggr.Log(x, true)); ImgScanResult = new Image <Bgr, byte>(100, 100, new Bgr(100, 100, 100)); return(false); } else { var poses = posThickIntenReflc.Select(x => x.Pos).ToList(); var thckneses = posThickIntenReflc.Select(x => x.Thickness.Right).ToList(); var intens = posThickIntenReflc.Select(x => x.Intensity.Right).ToList(); var reflec = posThickIntenReflc.Select(x => x.Refelctivity).ToList(); ResultData = ToResult(poses, thckneses, intens, reflec, SelectedWaves); Stopwatch stw = new Stopwatch(); stw.Start(); CreateMapandBar(ResultData, 6) .Act(x => ImgScanResult = x); stw.ElapsedMilliseconds.Print("Draw Time"); return(true); } } catch (Exception e) { MessageBox.Show(e.ToString()); return(false); } }
public bool ScanManualRun(double [] TargetPosTR, int intervalsec, int count) { OpMaxSpeed(); OpORGMaxSpeed(); // Ref Check -- if (!FlgRefReady) { return(false.Act(x => MessageBox.Show("Set Referance Please"))); } // Home And Check -- //if ( !FlgHomeDone ) if ( !OpHome() ) return false; ///OpHome(); FlgHomeDone = true; // Scan Ready And Check -- if (FlgDarkReady == false) { if (!OpReady(ScanReadyMode.Dark)) { return(false); } } FlgDarkReady = true; var toReflect = FnCalReflections(Darks, Refs, SelectedReflctFactors); var plrpos = new CrtnCrd(TargetPosTR[0], TargetPosTR[1]) .ToPolar() as PlrCrd; var stgMoveRes = new TEither(Stg as IStgCtrl, 12) .Bind(x => x.Act(f => { f.SendAndReady(f.GoAbs + plrpos.Theta.Degree2Pulse().ToPos(Axis.W) + plrpos.Rho.mmToPulse().ToOffPos()); f.SendAndReady(f.Go); }).ToTEither(), "R Stage Move Command Fail"); var moveResLog = stgMoveRes.ToLEither(new double[] { }); lock ( keySingle ) { int curcount = 0; while (FlgCoreSingleScan) { Console.WriteLine("Current Count : {0} , Limit Count : {1}", curcount, count); if (curcount == count) { break; } var currentInten = GetSpectrum(); var reflet = toReflect(currentInten); var thckn = ToThickness( reflet.ToLEither(), SelectedWaves, plrpos) .Item2.Right; evtSngSignal(currentInten, reflet, SelectedWaves, thckn, curcount); Thread.Sleep(intervalsec * 1000); curcount++; } } Console.WriteLine("Complete"); evtSingleMeasureComplete(); FlgCoreSingleScan = false; return(true); }