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); }
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; } }