private void MenuDeviceRefresh_Click(object sender, EventArgs e) { double tempSet, timeStep, proportionalTerm, integralTerm, derivativeTerm; bool devExists = false; DeviceBx.Items.Clear(); VoltChnnlBx.Items.Clear(); TempChnnlBx.Items.Clear(); try { DeviceBx.Items.AddRange(DaqSystem.Local.Devices); VoltChnnlBx.Items.AddRange(DaqSystem.Local.GetPhysicalChannels(PhysicalChannelTypes.AO, PhysicalChannelAccess.External)); TempChnnlBx.Items.AddRange(DaqSystem.Local.GetPhysicalChannels(PhysicalChannelTypes.AI, PhysicalChannelAccess.External)); if (!double.TryParse(TempSetBx.Text, out tempSet)) { tempSet = 22.0; TempSetBx.Text = "22.0"; } if (!double.TryParse(TimeStepBx.Text, out timeStep)) { timeStep = 0.01; TimeStepBx.Text = "0.01"; } if (!double.TryParse(ProportionalBx.Text, out proportionalTerm)) { proportionalTerm = 0.0; ProportionalBx.Text = "0.0"; } if (!double.TryParse(IntegralBx.Text, out integralTerm)) { integralTerm = 0.0; IntegralBx.Text = "0.0"; } if (!double.TryParse(DerivativeBx.Text, out derivativeTerm)) { derivativeTerm = 0.0; DerivativeBx.Text = "0.0"; } if (DeviceBx.Items.Count != 0) { temperatureController = new TemperatureController(0.01, tempUnits, TempChnnlBx.Items[0].ToString(), AIVoltageUnits.Volts, VoltChnnlBx.Items[0].ToString(), AOVoltageUnits.Volts) { DesiredTemp = tempSet, A = proportionalTerm, B = integralTerm, C = derivativeTerm, //Units = tempUnits }; devExists = true; } else { temperatureController = new TemperatureController(0.01, tempUnits, TempChnnlBx.Text, AIVoltageUnits.Volts, VoltChnnlBx.Text, AOVoltageUnits.Volts) { //TimeStep = timeStep, DesiredTemp = tempSet, A = proportionalTerm, B = integralTerm, C = derivativeTerm, }; devExists = false; } } catch (DaqException) { TempSetBx.Enabled = false; TimeStepBx.Enabled = false; ProportionalBx.Enabled = false; IntegralBx.Enabled = false; DerivativeBx.Enabled = false; StartStopBttn.Enabled = false; MessageBox.Show("No DAQ device is present. Please check device connection in NImax and refresh the device list.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (DeviceBx.Items.Count != 0) { DeviceBx.SelectedIndex = 0; VoltChnnlBx.SelectedIndex = 0; TempChnnlBx.SelectedIndex = 0; } else { DeviceBx.SelectedIndex = -1; VoltChnnlBx.SelectedIndex = -1; TempChnnlBx.SelectedIndex = -1; } if (devExists) { TempSetBx.Enabled = true; TimeStepBx.Enabled = true; ProportionalBx.Enabled = true; IntegralBx.Enabled = true; DerivativeBx.Enabled = true; StartStopBttn.Enabled = true; } } }
private void MainForm_Load(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; formUpdateTimer.Interval = 100; pidTimeStep.Interval = 10; TempSetBx.Text = "22.0"; ProportionalBx.Text = "0.0"; IntegralBx.Text = "0.0"; DerivativeBx.Text = "0.0"; TimeStepBx.Text = "0.01"; TimeStepBx.Enabled = false; ProportionalContributionLbl.Text = "0.0"; IntegralContributionLbl.Text = "0.0"; DerivativeContributionLbl.Text = "0.0"; for (int i = 0; i < textBoxes.Length; i++) { textBoxes[i].KeyPress += new KeyPressEventHandler(Bx_KeyPress); textBoxes[i].TextChanged += new EventHandler(Bx_TextChanged); names[i] = textBoxes[i].Name; } try { DeviceBx.Items.AddRange(DaqSystem.Local.Devices); VoltChnnlBx.Items.AddRange(DaqSystem.Local.GetPhysicalChannels(PhysicalChannelTypes.AO, PhysicalChannelAccess.External)); TempChnnlBx.Items.AddRange(DaqSystem.Local.GetPhysicalChannels(PhysicalChannelTypes.AI, PhysicalChannelAccess.External)); if (DeviceBx.Items.Count != 0) { temperatureController = new TemperatureController(0.01, "C", TempChnnlBx.Items[0].ToString(), AIVoltageUnits.Volts, VoltChnnlBx.Items[0].ToString(), AOVoltageUnits.Volts); } else { temperatureController = new TemperatureController(0.01, "C", TempChnnlBx.Text, AIVoltageUnits.Volts, VoltChnnlBx.Text, AOVoltageUnits.Volts); } } catch (DaqException) { TempSetBx.Enabled = false; TimeStepBx.Enabled = false; ProportionalBx.Enabled = false; IntegralBx.Enabled = false; DerivativeBx.Enabled = false; StartStopBttn.Enabled = false; MessageBox.Show("No DAQ device is present. Please check device connection in NImax and refresh the device list.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (DeviceBx.Items.Count != 0) { DeviceBx.SelectedIndex = 0; VoltChnnlBx.SelectedIndex = 0; TempChnnlBx.SelectedIndex = 0; } else { DeviceBx.SelectedIndex = -1; VoltChnnlBx.SelectedIndex = -1; TempChnnlBx.SelectedIndex = -1; } } MeasureTypeBx.Items.Add("RSE"); MeasureTypeBx.SelectedIndex = 0; taskRunning = false; TempPlot.ChartAreas[0].AxisY.LabelStyle.Format = "###.#"; TempPlot.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; TempPlot.Series[0].Name = "Temperature"; TempPlot.Series.Add("Set Point").Color = Color.Green; TempPlot.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; TempPlot.ChartAreas[0].AxisX.Title = "Time Steps"; TempPlot.ChartAreas[0].AxisY.Title = "Temperature (°C)"; TempPlotExpanded.ChartAreas[0].AxisY.LabelStyle.Format = "###.#"; TempPlotExpanded.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; TempPlotExpanded.Series[0].Name = "Temperature"; TempPlotExpanded.Series.Add("Set Point").Color = Color.Green; TempPlotExpanded.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; TempPlotExpanded.ChartAreas[0].AxisX.Title = "Time Steps"; TempPlotExpanded.ChartAreas[0].AxisY.Title = "Temperature (°C)"; TimeStepsLbl.Text = "0"; Cursor.Current = Cursors.Default; }
private void StartStopBttn_Click(object sender, EventArgs e) { if (!taskRunning) { double tempSet, timeStep, proportionalTerm, integralTerm, derivativeTerm; //C# 7 allows for inline declerations of out parameters StartStopBttn.Text = "Turn Control\nOff"; try { if (double.TryParse(TempSetBx.Text, out tempSet)) { TempPlot.Series[1].Points.AddY(tempSet); } else { tempSet = 22.0; TempSetBx.Text = "22.0"; TempPlot.Series[1].Points.AddY(tempSet); } if (!double.TryParse(TimeStepBx.Text, out timeStep)) { timeStep = 0.01; TimeStepBx.Text = "0.01"; } if (!double.TryParse(ProportionalBx.Text, out proportionalTerm)) { proportionalTerm = 0.0; ProportionalBx.Text = "0.0"; } if (!double.TryParse(IntegralBx.Text, out integralTerm)) { integralTerm = 0.0; IntegralBx.Text = "0.0"; } if (!double.TryParse(DerivativeBx.Text, out derivativeTerm)) { derivativeTerm = 0.0; DerivativeBx.Text = "0.0"; } temperatureController = new TemperatureController(0.01, tempUnits, TempChnnlBx.Text, AIVoltageUnits.Volts, VoltChnnlBx.Text, AOVoltageUnits.Volts) { //TimeStep = timeStep, DesiredTemp = tempSet, A = proportionalTerm, B = integralTerm, C = derivativeTerm, }; TempPlot.ChartAreas[0].AxisY.Minimum = temperatureController.DesiredTemp - 1.0; TempPlot.ChartAreas[0].AxisY.Maximum = temperatureController.DesiredTemp + 1.0; TempPlotExpanded.ChartAreas[0].AxisY.Minimum = temperatureController.DesiredTemp - 15.0; TempPlotExpanded.ChartAreas[0].AxisY.Maximum = temperatureController.DesiredTemp + 15.0; temperatureController.AdjustOutput(); lock (_formTimerLock) { formUpdateTimer.Enabled = true; _formStopped = false; } lock (_pidTimerLock) { pidTimeStep.Enabled = true; _pidStopped = false; pidTimeStep.Interval = (int)(timeStep * 1000); } taskRunning = true; VisualControlStatus.BackColor = Color.Green; VisualControlStatusLbl.Text = "On"; MenuUnitCelsius.Enabled = false; MenuUnitFarenheit.Enabled = false; MenuDeviceRefresh.Enabled = false; pidTimeStep.Start(); formUpdateTimer.Start(); } catch { StartStopBttn.Text = "Turn Control\nOn"; VisualControlStatus.BackColor = Color.Red; VisualControlStatusLbl.Text = "Off"; TempPlot.Series[0].Points.Clear(); TempPlot.Series[1].Points.Clear(); TempPlotExpanded.Series[0].Points.Clear(); TempPlotExpanded.Series[1].Points.Clear(); TempSetBx.Enabled = false; TimeStepBx.Enabled = false; ProportionalBx.Enabled = false; IntegralBx.Enabled = false; DerivativeBx.Enabled = false; StartStopBttn.Enabled = false; taskRunning = false; MenuUnitCelsius.Enabled = true; MenuUnitFarenheit.Enabled = true; MenuDeviceRefresh.Enabled = true; MessageBox.Show("There was an error communicating with the device. Check devices status and refresh the device list.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { timeTicks = 0; TimeStepsLbl.Text = "0"; formUpdateTimer.Enabled = false; pidTimeStep.Enabled = false; lock (_formTimerLock) { formUpdateTimer.Enabled = false; _formStopped = true; } lock (_pidTimerLock) { pidTimeStep.Enabled = false; _pidStopped = true; } temperatureController.Dispose(); TempPlot.Series[0].Points.Clear(); TempPlot.Series[1].Points.Clear(); TempPlotExpanded.Series[0].Points.Clear(); TempPlotExpanded.Series[1].Points.Clear(); StartStopBttn.Text = "Turn Control\nOn"; taskRunning = false; VisualControlStatus.BackColor = Color.Red; VisualControlStatusLbl.Text = "Off"; MenuUnitCelsius.Enabled = true; MenuUnitFarenheit.Enabled = true; MenuDeviceRefresh.Enabled = true; } }