public bool Init() { try { var firstIdentifyer = 0x0957; var secondIdentifyer = 0x1718; var visaBuilder = new StringBuilder(); visaBuilder.AppendFormat("USB0::{0}::{1}::TW54334510::INSTR", firstIdentifyer.ToString(NumberFormatInfo.InvariantInfo), secondIdentifyer.ToString(NumberFormatInfo.InvariantInfo)); var initSuccess = boxController.Init(visaBuilder.ToString()); if (initSuccess == true) { VdsMotorPotentiometer = new BS350_MotorPotentiometer(boxController, Agilent_ExtensionBox.IO.BOX_AnalogOutChannelsEnum.BOX_AOut_02); VgMotorPotentiometer = new BS350_MotorPotentiometer(boxController, Agilent_ExtensionBox.IO.BOX_AnalogOutChannelsEnum.BOX_AOut_09); } return(initSuccess); } catch { return(false); } }
private void setVoltage(BS350_MotorPotentiometer motorPotentiometer, int voltNum, double voltage, double voltageDev) { voltage = Math.Abs(voltage); var intervalCoarse = voltage * (1.0 - 1.0 / Math.Sqrt(2.0)); double drainVoltageCurr = 0.0, drainVoltagePrev = 0.0, factorCoarse = 0.0; confAIChannelsForDC_Measurement(); accuracyStopWatch.Start(); while (true) { var voltages = boxController.VoltageMeasurement_AllChannels(averagingNumberFast); drainVoltageCurr = Math.Abs(voltages[voltNum]); var lowerVal = Math.Min(drainVoltageCurr, voltage); var higherVal = Math.Max(drainVoltageCurr, voltage); onProgressChanged(this, new ProgressChanged_EventArgs((int)(lowerVal / higherVal * 100.0))); var speed = minSpeed; try { if (Math.Abs(voltage - drainVoltageCurr) <= 0.05) { factorCoarse = (1.0 - Math.Tanh(-1.0 * Math.Abs(voltage - drainVoltageCurr) / intervalCoarse * Math.PI + Math.PI)) / 2.0; speed = (byte)(minSpeed + (maxSpeed - minSpeed) * factorCoarse); } else { speed = maxSpeed; } } catch { speed = minSpeed; } if ((drainVoltageCurr >= Math.Abs(voltage - voltageDev)) && (drainVoltageCurr <= Math.Abs(voltage + voltageDev))) { motorPotentiometer.StopMotion(); accuracyStopWatch.Stop(); break; } else { // Implementing voltage set with enchansed accuracy if (estimationList.Count > estimationCollectionSize) { estimationList.RemoveFirst(); } estimationList.AddLast(new Point(accuracyStopWatch.ElapsedMilliseconds, Math.Abs(drainVoltagePrev - drainVoltageCurr))); var timeAVG = estimationList.Select(val => val.X).Average(); var voltAVG = estimationList.Select(val => val.Y).Average(); var voltPerMilisecond = timeAVG != 0 ? voltAVG / timeAVG : voltAVG; var stepTime = (int)(Math.Abs(voltage - drainVoltageCurr) / voltPerMilisecond); if (drainVoltageCurr > voltage) { if (voltageDev >= 0.006 || drainVoltageCurr - voltage > 2.0 * voltageDev) { averagingNumberFast = 2; motorPotentiometer.StartMotion(speed, MotionDirection.cw); } else { motorPotentiometer.StartMotion(speed, MotionDirection.cw); Thread.Sleep(stepTime); motorPotentiometer.StopMotion(); averagingNumberFast = 25; } } else { if (voltageDev >= 0.006 || voltage - drainVoltageCurr > 2.0 * voltageDev) { averagingNumberFast = 2; motorPotentiometer.StartMotion(speed, MotionDirection.ccw); } else { motorPotentiometer.StartMotion(speed, MotionDirection.ccw); Thread.Sleep(stepTime); motorPotentiometer.StopMotion(); averagingNumberFast = 25; } } accuracyStopWatch.Restart(); } drainVoltagePrev = drainVoltageCurr; } motorPotentiometer.StopMotion(); }