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 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]: ";
     }
 }
Esempio n. 4
0
        private void listBoxControllers_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // specify the new connection as the currently selected one
                connection_selected = connections[listBoxModules.SelectedIndex];

                // enable or disable reference track bars
                Helpers.ManageTrackbars(this);

                // update GUI values according to the connected controller
                Helpers.UpdateGuiControls(this, connection_selected);

                // clear charts
                dataChart.Series.Clear();
                residualChart.Series.Clear();
                securityChart.Series.Clear();

                // clear checkbox series
                clbSeries.Items.Clear();

                // refresh the chart, load all data points
                UpdateChart(connection_selected.references, setting: DataPointSetting.LOAD_ALL);       // reference
                UpdateChart(connection_selected.received_packets, setting: DataPointSetting.LOAD_ALL); // states
                UpdateChart(connection_selected.estimates, setting: DataPointSetting.LOAD_ALL);        // kalman filter estimates

                // scale y-axis for the charts
                Charting.ChangeYScale(dataChart, treshold_interval: "one", grid_interval: "one");
                Charting.ChangeYScale(residualChart, treshold_interval: "one", grid_interval: "one");
                Charting.ChangeYScale(securityChart, treshold_interval: "one", grid_interval: "one");

                // update treshold strip lines
                updateThresholdStripLines();

                // select the corresponding item in the treeview
                treeViewControllers.SelectedNode = treeViewControllers.Nodes[connection_selected.name];
            }
            catch { }
        }
Esempio n. 5
0
        private void btnAllowConnection_Click_1(object sender, EventArgs e)
        {
            // enable parameter settings
            numUpDownKp.Enabled = true;
            numUpDownKi.Enabled = true;
            numUpDownKd.Enabled = true;

            // control module name and corresponding ip:port pairs
            string name          = textBoxName.Text;
            string IP_endpoint   = textBox_ip_send.Text;
            string IP_this       = textBox_ip_receive.Text;
            int    port_endpoint = Convert.ToInt16(numericUpDown_port_send.Value);
            int    port_this     = Convert.ToInt16(numericUpDown_port_receive.Value);

            // look for name and port conflicts with present allowed traffics
            bool connection_already_exists = false;

            foreach (CommunicationManager controller in connections)
            {
                if ((controller.Controller_EP.PortThis == port_this && controller.Controller_EP.Port == port_endpoint) || controller.name == name)
                {
                    connection_already_exists = true;
                }
            }

            if (connection_already_exists == true)
            {
                log("Error: name or ip:port pair already in use");
            }
            else
            {
                // connection parameters
                ConnectionParameters connectionParameters = new ConnectionParameters(IP_endpoint, port_endpoint, port_this);

                // create and add controller
                PIDparameters        controllerParameters  = new PIDparameters(Convert.ToDouble(numUpDownKp.Value), Convert.ToDouble(numUpDownKi.Value), Convert.ToDouble(numUpDownKd.Value));
                CommunicationManager controller_connection = new CommunicationManager(this, name, controllerParameters, Channel_EP, connectionParameters);
                connections.Add(controller_connection);
                connection_selected = controller_connection;
                listBoxModules.Items.Add(name);

                // select the added module in the listbox
                listBoxModules.SelectedIndex = listBoxModules.Items.Count - 1;

                // start timers
                timerCharts.Start();
                timerUpdateGUI.Start();

                // update tree
                treeViewControllers.Nodes.Add(name, name);
                treeViewControllers.Nodes[name].Nodes.Add("Communication", "Communication");
                treeViewControllers.Nodes[name].Nodes["Communication"].Nodes.Add("ip", "IP (endpoint): " + IP_endpoint.ToString());
                treeViewControllers.Nodes[name].Nodes["Communication"].Nodes.Add("port", "Port (endpoint): " + port_endpoint.ToString());
                treeViewControllers.Nodes[name].Nodes["Communication"].Nodes.Add("port_gui", "Port (this): " + port_this.ToString());
                treeViewControllers.Nodes[name].Nodes.Add("Controller", "Controller settings");
                treeViewControllers.Nodes[name].Nodes["Controller"].Nodes.Add("Kp", "Kp: " + controllerParameters.Kp.ToString());
                treeViewControllers.Nodes[name].Nodes["Controller"].Nodes.Add("Ki", "Ki: " + controllerParameters.Ki.ToString());
                treeViewControllers.Nodes[name].Nodes["Controller"].Nodes.Add("Kd", "Ks: " + controllerParameters.Kd.ToString());
                treeViewControllers.ExpandAll();

                // update counter
                n_connections   += 1;
                textBoxName.Text = "Module" + (n_connections + 1);
                log("Communication to a control module enabled");

                numericUpDown_port_receive.Value += 1;
                numericUpDown_port_send.Value    += 1;
            }
        }
        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;
        }