public static void ManageTrackbars(FrameGUI GUI) { // enable or disable trackbars if (GUI.connection_selected.n_controlled_states == 1) { GUI.trackBarReference1.Enabled = true; GUI.numUpDownRef1.Enabled = true; GUI.trackBarReference1.Value = Convert.ToInt16(GUI.connection_selected.references["r1"].GetLastValue()); GUI.numUpDownRef1.Value = Convert.ToInt16(GUI.connection_selected.references["r1"].GetLastValue()); } else if (GUI.connection_selected.n_controlled_states == 2) { GUI.trackBarReference1.Enabled = true; GUI.trackBarReference2.Visible = true; GUI.numUpDownRef1.Enabled = true; GUI.numUpDownRef2.Visible = true; GUI.trackBarReference2.Value = Convert.ToInt16(GUI.connection_selected.references["r2"].GetLastValue()); GUI.numUpDownRef2.Value = Convert.ToInt16(GUI.connection_selected.references["r2"].GetLastValue()); } else { GUI.trackBarReference1.Enabled = false; GUI.trackBarReference2.Visible = false; GUI.numUpDownRef1.Enabled = false; GUI.numUpDownRef2.Visible = false; } }
public static void UpdateTree(FrameGUI GUI, CommunicationManager controller) { // update tree GUI.treeViewControllers.Nodes[controller.name].Text = controller.name + " (" + controller.GetConnectionStatus() + ")"; GUI.treeViewControllers.Nodes[controller.name].Nodes["Controller"].Nodes["Kp"].Text = "Kp: " + GUI.connection_selected.ControllerParameters.Kp.ToString(); GUI.treeViewControllers.Nodes[controller.name].Nodes["Controller"].Nodes["Ki"].Text = "Ki: " + GUI.connection_selected.ControllerParameters.Ki.ToString(); GUI.treeViewControllers.Nodes[controller.name].Nodes["Controller"].Nodes["Kd"].Text = "Kd: " + GUI.connection_selected.ControllerParameters.Kd.ToString(); }
public static void UpdateGuiControls(FrameGUI GUI, CommunicationManager connection_selected) { GUI.numUpDownKp.Value = Convert.ToDecimal(connection_selected.ControllerParameters.Kp); GUI.numUpDownKi.Value = Convert.ToDecimal(connection_selected.ControllerParameters.Ki); GUI.numUpDownKd.Value = Convert.ToDecimal(connection_selected.ControllerParameters.Kd); GUI.textBox_ip_send.Text = connection_selected.Controller_EP.IP; GUI.numericUpDown_port_send.Text = connection_selected.Controller_EP.Port.ToString(); GUI.numericUpDown_port_receive.Text = connection_selected.Controller_EP.PortThis.ToString(); }
public static void ManageReferenceSeries(FrameGUI Main) { for (int i = 0; i < Main.connection_selected.n_controlled_states; i++) { if (Main.dataChart.Series.IndexOf("r" + (i + 1).ToString()) == -1) { AddChartSeries(Main, "r" + (i + 1).ToString(), Main.dataChart); } } }
public static void ManageChartSize(FrameGUI Main) { try { int y_start = Main.residualChart.Location.Y; int height_total = Main.tabControl1.Height - y_start; Main.residualChart.Height = height_total / 2 - y_start; Main.securityChart.Location = new Point(6, +y_start + height_total / 2); Main.securityChart.Height = height_total / 2 - y_start; } catch { } }
public static void UpdateTimeLabels(FrameGUI GUI, CommunicationManager connection, string FMT) { try { DateTime time_sent = DateTime.ParseExact(connection.received_packets["u1"].GetLastTime(), FMT, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal); TimeSpan timeDiff = connection.time_last_received_packet - time_sent; GUI.label_time.Text = "Time now: " + DateTime.UtcNow.ToString("hh:mm:ss.fff tt") + "\n" + "Last received packet: " + connection.time_last_received_packet.ToString("hh:mm:ss.fff tt") + "\n" + "Last packet sent: " + time_sent.ToString("hh:mm:ss.fff tt") + "\n" + "Transmission time [ms]: " + timeDiff.TotalMilliseconds; } catch { GUI.label_time.Text = "Time now: \n" + "Last received packet:: \n" + "Last packet sent: \n" + "Transmission time [ms]: "; } }
static int T_Controller = 100; // [ms] public CommunicationManager(FrameGUI Main, string name, PIDparameters ControllerParameters, AddressEndPoint Channel_EP, ConnectionParameters Controller_EP) { // main form access this.Main = Main; // channel endpoint this.Channel_EP = Channel_EP; // identity parameters this.name = name; this.Controller_EP = Controller_EP; // specify the endpoint (either channel or directly the controller) AddressEndPoint EP = new AddressEndPoint(); if (Main.using_channel == true) { EP = new AddressEndPoint(Channel_EP.IP, Channel_EP.Port); } else { EP = new AddressEndPoint(Controller_EP.IP, Controller_EP.Port); } // controller parameters this.ControllerParameters = ControllerParameters; // create a new thread for the sender Thread thread_sender = new Thread(() => Sender(EP.IP, EP.Port)); thread_sender.Start(); // create a new thread for the listener Thread thread_listener = new Thread(() => Listener(EP.IP, Controller_EP.PortThis)); thread_listener.Start(); // load kalman settings kalman_filter.updateKalmanFilter(Main.config.kalmanDoubleWatertankModel.A1, Main.config.kalmanDoubleWatertankModel.a1, Main.config.kalmanDoubleWatertankModel.A2, Main.config.kalmanDoubleWatertankModel.a2, Main.config.kalmanDoubleWatertankModel.k); kalman_filter.setAnomalyDetectorSettings(Main.config.anomalyDetector.cusumDelta); }
public static void DrawTanks(FrameGUI GUI, CommunicationManager connection) { g = Graphics.FromImage(bm); g.Clear(Color.White); // map tank height in cm to pixels double max_height_p = GUI.pictureBox1.Height / 2.5; // max height [pixels] double max_inflow_width = 10; double max_height_r = 20; // real max height [cm] double cm2pix = max_height_p / max_height_r; // if there are no estimates, there is nothing to animate if (connection.estimates.ContainsKey("yo1_hat") == false || connection.estimates.ContainsKey("yc1_hat") == false) { GUI.pictureBox1.Image = bm; return; } // extract signals int u = Convert.ToInt16(Convert.ToDouble(connection.received_packets["u1"].GetLastValue())); int y1 = Convert.ToInt16(cm2pix * Convert.ToDouble(connection.estimates["yo1_hat"].GetLastValue())); int y2 = Convert.ToInt16(cm2pix * Convert.ToDouble(connection.estimates["yc1_hat"].GetLastValue())); int reference = 0; try { reference = Convert.ToInt16(cm2pix * Convert.ToDouble(connection.references["r1"].GetLastValue())); } catch { } // dimensions double A1 = GUI.config.kalmanDoubleWatertankModel.A1; double a1 = GUI.config.kalmanDoubleWatertankModel.a1; double A2 = GUI.config.kalmanDoubleWatertankModel.A2; double a2 = GUI.config.kalmanDoubleWatertankModel.a2; // TANK 1 ---------------------------------------------------------------- Point T1 = new Point(GUI.pictureBox1.Width / 2, Convert.ToInt16(GUI.pictureBox1.Height / 2)); Point T2 = new Point(T1.X, Convert.ToInt16(GUI.pictureBox1.Height - 20)); // tank dimensions double R1_ = Math.Sqrt(A1 / Math.PI); int R1 = Convert.ToInt16(R1_ * cm2pix); double r1_ = Math.Sqrt(a1 / Math.PI); int r1 = Convert.ToInt16(r1_ * cm2pix); double h1_ = 20; int h1 = Convert.ToInt16(h1_ * cm2pix); // inlet if (u > 0) { Rectangle water_in = new Rectangle(T1.X - R1 + 5, T1.Y - h1 - 50, Convert.ToInt16(max_inflow_width * (u / 7.5)), h1 + 50); g.FillRectangle(brush_b, water_in); } // water Rectangle water1 = new Rectangle(T1.X - R1, T1.Y - y1, 2 * R1, y1); g.FillRectangle(brush_b, water1); if (y1 > 5) { Rectangle water_fall = new Rectangle(T1.X - r1, T1.Y, 2 * r1, T2.Y - T1.Y); g.FillRectangle(brush_b, water_fall); } // walls Point w1_top = new Point(T1.X - R1, T1.Y - h1); Point w1_bot = new Point(T1.X - R1, T1.Y); Point w2_top = new Point(T1.X + R1, T1.Y - h1); Point w2_bot = new Point(T1.X + R1, T1.Y); Point wb1_l = new Point(T1.X - R1, T1.Y); Point wb1_r = new Point(T1.X - r1, T1.Y); Point wb2_l = new Point(T1.X + r1, T1.Y); Point wb2_r = new Point(T1.X + R1, T1.Y); g.DrawLine(pen_b, w1_top, w1_bot); g.DrawLine(pen_b, w2_top, w2_bot); g.DrawLine(pen_b, wb1_l, wb1_r); g.DrawLine(pen_b, wb2_l, wb2_r); // TANK 2 --------------------------------------------------------------- // tank dimensions double R2_ = Math.Sqrt(A2 / Math.PI); int R2 = Convert.ToInt16(R2_ * cm2pix); double r2_ = Math.Sqrt(a2 / Math.PI); int r2 = Convert.ToInt16(r2_ * cm2pix); double h2_ = 20; int h2 = Convert.ToInt16(h2_ * cm2pix); // water Rectangle water2 = new Rectangle(T2.X - R2, T2.Y - y2, 2 * R2, y2); g.FillRectangle(brush_b, water2); if (y2 > 5) { Rectangle water_fall = new Rectangle(T2.X - r2, T2.Y, 2 * r2, 200); g.FillRectangle(brush_b, water_fall); } // walls w1_top = new Point(T2.X - R2, T2.Y - h2); w1_bot = new Point(T2.X - R2, T2.Y); w2_top = new Point(T2.X + R2, T2.Y - h2); w2_bot = new Point(T2.X + R2, T2.Y); wb1_l = new Point(T2.X - R2, T2.Y); wb1_r = new Point(T2.X - r2, T2.Y); wb2_l = new Point(T2.X + r2, T2.Y); wb2_r = new Point(T2.X + R2, T2.Y); g.DrawLine(pen_b, w1_top, w1_bot); g.DrawLine(pen_b, w2_top, w2_bot); g.DrawLine(pen_b, wb1_l, wb1_r); g.DrawLine(pen_b, wb2_l, wb2_r); // draw reference Point reference_l = new Point(T2.X - R2 - 15, T2.Y - reference); Point reference_r = new Point(T2.X - R2 - 3, T2.Y - reference); g.DrawLine(pen_r, reference_l, reference_r); GUI.pictureBox1.Image = bm; }
public static void AddChartSeries(FrameGUI Main, string key, object chart) { //string[] unchecked_keys = new string[] { "yo1", "yo2" }; string[] unchecked_keys = new string[] { "" }; Chart chart_ = (Chart)chart; // add a new time series if (chart_.Series.IndexOf(key) == -1) { // add to the checked list box if (chart_.Name == "dataChart") { Main.clbSeries.Items.Add(key); if (unchecked_keys.Contains(key) == false) { Main.clbSeries.SetItemChecked(Main.clbSeries.Items.Count - 1, true); } else { Main.clbSeries.SetItemChecked(Main.clbSeries.Items.Count - 1, false); } } // add the to the chart chart_.Series.Add(key); chart_.Series[key].ChartType = SeriesChartType.Line; chart_.Series[key].BorderWidth = 1; switch (key) { case "r1": chart_.Series[key].Color = Color.Red; chart_.Series[key].BorderWidth = 1; break; case "r2": chart_.Series[key].Color = Color.OrangeRed; chart_.Series[key].BorderWidth = 1; break; case "u1": chart_.Series[key].Color = Color.Orange; chart_.Series[key].BorderWidth = 1; break; case "u2": chart_.Series[key].Color = Color.Magenta; chart_.Series[key].BorderWidth = 1; break; case "yo1": chart_.Series[key].Color = Color.Gray; chart_.Series[key].BorderWidth = 2; break; case "yo1_hat": chart_.Series[key].Color = Color.Black; chart_.Series[key].BorderWidth = 2; break; case "yo2": chart_.Series[key].Color = Color.Gray; chart_.Series[key].BorderWidth = 2; break; case "yc1": chart_.Series[key].Color = Color.Blue; chart_.Series[key].BorderWidth = 2; break; case "yc1_hat": chart_.Series[key].Color = Color.Magenta; chart_.Series[key].BorderWidth = 2; break; case "yc2": chart_.Series[key].Color = Color.Green; chart_.Series[key].BorderWidth = 2; break; case "Res_yc1": chart_.Series[key].Color = Color.Blue; chart_.Series[key].BorderWidth = 2; break; case "Sec_yc1": chart_.Series[key].Color = Color.Blue; chart_.Series[key].BorderWidth = 2; break; default: break; } // set the x-axis type to DateTime chart_.Series[key].XValueType = ChartValueType.DateTime; } }