/**
         * A szabályozási folyamatért felelős fgv
         * */
        public override void Run(APresenter _in)
        {
            run = true;
            getInput();

            // state[0] - Angle
            // state[1] - Position
            double[] state = Process.get();
            double epsilon = 0.001;

            #region Init

            oldTime = DateTime.Now.Millisecond;
            state = Process.get();
            oldError = reference - state[1];

            #endregion

            // Addig megy a szabályozási folyamat, amíg a referencia érték epsilon sugarú körébe nem kerül a pozíció
            while (run && !(state[1] > (reference - epsilon) && state[1] < (reference + epsilon)))
            {

                state = Process.get();
                _in.updateDraw(state);

                newTime = DateTime.Now.Millisecond;
                double[] u = new double[] { 0.0 };

                //PID logika
                double error = reference - state[1];
                I = I + error;
                double D = (error - oldError) / (newTime - oldTime);

                u[0] = clap(clap(Kp * error) + clap(Ki * I) + clap(Kd * D));

                if( Double.IsNaN(u[0]))
                {
                    u[0] = 0.0;
                }
                Process.set(u);
                _in.updateLog(new string[] { DateTime.Now.ToString("HH:mm:ss.fff"), state[0].ToString("f5"),state[1].ToString("f5"), u[0].ToString("f5") });

                oldTime = newTime;
                oldError = error;

                Thread.Sleep(25);
                state = Process.get();
            }

            double[] stop = new double[] { 0.0 };
            Process.set(stop);
        }
예제 #2
0
        /**
         * Ha kiválasztottuk a megfelelő assembly-ket, akkor a controller user interface-e jelenik meg, és a folyamat megjelenítője
         * Az AssemblyPicker osztályból hívódik meg
         * */
        public void controllerSelected(UserControl _inputUI,APresenter _inputPres)
        {
            pck.Dispose();
            UI = _inputUI;
            UI.Location = new System.Drawing.Point(10, 20);
            UI.Size = new System.Drawing.Size(440, 376);
            UI.TabIndex = 2;
            this.groupBox1.Controls.Add(UI);
            groupBox1.Text = "Controller";

            groupBox2.Hide();
            pres = _inputPres;
            pres.Location = new System.Drawing.Point(10, 0);
            this.splitContainer1.Panel2.Controls.Add(pres);

            runnable = true;
        }
        /**
         * A szabályozási folyamatért felelős fgv
         * */
        public override void Run(APresenter _in)
        {
            run = true;
            getInput();

            // state[0] - Angle
            // state[1] - Position
            double[] state = Process.get();
            double epsilon = 0.001;
            double[] u = new double[] { 0.0 };

            while (run && !(state[1] > (reference - epsilon) && state[1] < (reference + epsilon)))
            {
                state = Process.get();
                _in.updateDraw(state);

                // Ha a referenciánál kisebb, akkor jobbra megy, ha nagyobb, akkor (lassabban) balra
                if (state[1] <= (reference - epsilon))
                {
                    u[0] = 1.0;
                }
                if (state[1] > (reference + epsilon))
                {
                    u[0] = -0.3;
                }
                Process.set(u);
                _in.updateLog(new string[] { DateTime.Now.ToString("HH:mm:ss.fff"), state[0].ToString("f5"), state[1].ToString("f5"), u[0].ToString("f5") });

                Thread.Sleep(25);
                state = Process.get();
            }

            Process.set(new double[] { 0.0 });

            for (int i = 0; i < 5; i++)
            {
                state = Process.get();
                Thread.Sleep(25);
            }
        }
 public abstract void Run(APresenter _in);
        /**
         * A függvény leellenőrzi az AssemblyPicker felületen beállított assembly-ket, hogy a program számára emészthető formátumúak-e
         * Példányosítja a megfelelő objektumokat
         * */
        public static bool InitSession(Form1 owner)
        {
            if (owner.pck.button1.Enabled) return true;
            Assembly ass;
            Type type = null;
            StreamReader _textStreamReader = null;
            ConstructorInfo con1 = null;
            Type AccessionBaseType = null;
            Type IProcInterface = null;

            #region A folyamat megvalósításának betöltése
            try
            {
                //A Dll fájl betöltése
                ass = Assembly.LoadFrom(owner.pck.textBox4.Text);
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "There is not such a file. The Accession path is probably wrong.";
                return false;
            }

            // Namespace resource megkeresése és betöltése
            foreach (var s in ass.GetManifestResourceNames())
            {
                if(s.Contains("Namespace.txt"))
                {
                    _textStreamReader = new StreamReader(ass.GetManifestResourceStream(s));
                }
            }
            if (_textStreamReader == null)
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "There is no Namespace.txt resource in the Accession assembly. The namespace suppose to be defined in Namespace.txt Embedded Resource.";
                return false;
            }

            string nameSpace = _textStreamReader.ReadLine();

            //A per jel mentén felbontja a stringet, és az utolsó darabot (fájlnév és kiterjesztés) még a pontnál kettévágja, és veszi az elsőt (a fájlnevet kiterjesztés nélkül)
            string className = (owner.pck.textBox4.Text.Split(new char[] { '\\' })).Last<string>().Split(new char[] { '.' }).First<string>();
            string typeName = nameSpace + "." + className;

            try{

                //Megfelelő osztály títpusának lekérése
                type = ass.GetType(typeName);
                if (type == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The Accession assembly doesn't contain any class named: " + typeName;
                    return false;
                }
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "The Accession assembly doesn't contain any class named: " + typeName;
                return false;
            }
            try{
                //Az ősosztály típus eltárolása, később a konstruktor paraméter típusának át kell adni
                AccessionBaseType = type.BaseType;
                if (AccessionBaseType == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The following type doesn't have any basetype: " + typeName;
                    owner.textBox1.AppendText("It most implement one of the provided abstract basetypes.");
                    return false;
                }
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "The following type doesn't have any basetype: " + typeName;
                owner.textBox1.AppendText("It most implement one of the provided abstract basetypes.");
                return false;
            }
            try
            {
                //Megfelelő paraméterezésű konstruktor lekérése
                con1 = type.GetConstructor(new Type[0] { });
                if (con1 == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The the following type doesn't have an empty parameterized constructor: " + typeName;
                    return false;
                }
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "The the following type doesn't have an empty parameterized constructor: " + typeName;
                return false;
            }
            try
            {
                //A Konstruktor meghívása
                tempAcc = con1.Invoke(new object[0] { });
                if (tempAcc == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "Some problem occured during the use of the constructor. (Accession)";
                    return false;
                }
            }
            catch (Exception e)
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "Some problem occured during the use of the constructor. (Logger)";
                owner.textBox1.AppendText(e.Message + "\n");
                owner.textBox1.AppendText(e.StackTrace + "\n\n");
                if (e.InnerException != null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.AppendText("Inner Exception:\n");
                    owner.textBox1.AppendText(e.InnerException + "\n");
                }
                return false;
            }
            #endregion

            #region Az IProcess-t biztosító elfedő osztály betöltése

            _textStreamReader = null;
            type = null;
            con1 = null;

            try
            {
                //DLL betöltése
                ass = Assembly.LoadFrom(owner.pck.textBox3.Text);
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "There is not such a file. The Connection path is probably wrong.";
                return false;
            }

            // Namespace resource megkeresése és betöltése
            foreach (var s in ass.GetManifestResourceNames())
            {
                if(s.Contains("Namespace.txt"))
                {
                    _textStreamReader = new StreamReader(ass.GetManifestResourceStream(s));
                }
            }
            if (_textStreamReader == null)
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "There is no Namespace.txt resource in the Connection assembly. The namespace suppose to be defined in Namespace.txt Embedded Resource.";
                return false;
            }
            nameSpace = _textStreamReader.ReadLine();

            //A per jel mentén felbontja a stringet, és az utolsó darabot (fájlnév és kiterjesztés) még a pontnál kettévágja, és veszi az elsőt (a fájlnevet kiterjesztés nélkül)
            className = (owner.pck.textBox3.Text.Split(new char[] { '\\' })).Last<string>().Split(new char[] { '.' }).First<string>();
            typeName = nameSpace + "." + className;

            try{

                //Megfelelő osztály títpusának lekérése
                type = ass.GetType(typeName);
                if (type == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The Connection assembly doesn't contain any class named: " + typeName;
                    return false;
                }
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "The Connection assembly doesn't contain any class named: " + typeName;
                return false;
            }
            try
            {
                //IProcess interfész eltárolása, később konstruktor paraméternek kell
                foreach (var i in type.GetInterfaces())
                {
                    if (i.Name.Contains("IProcess"))
                    {
                        IProcInterface = i;
                    }
                }
                if (IProcInterface == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The Connection assembly doesn't contain any interface named: IProcess";
                    return false;
                }
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "The Connection assembly doesn't contain any interface named: IProcess";
                return false;
            }
            try
            {
                // Ősosztály paraméterű konstruktor
                con1 = type.GetConstructor(new Type[1] { AccessionBaseType });
                if (con1 == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The the following type doesn't have constructor with parameter " + AccessionBaseType.Name + " : " + typeName;
                    return false;
                }
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "The the following type doesn't have constructor with parameter " + AccessionBaseType.Name + " : " + typeName;
                return false;
            }
            try
            {
                //A Konstruktor meghívása
                tempProc = con1.Invoke(new object[1] { tempAcc });
                if (tempProc == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "Some problem occured during the use of the constructor. (Connection)";
                    return false;
                }
            }
            catch ( Exception e)
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "Some problem occured during the use of the constructor. (Logger)";
                owner.textBox1.AppendText(e.Message + "\n");
                owner.textBox1.AppendText(e.StackTrace + "\n\n");
                if (e.InnerException != null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.AppendText("Inner Exception:\n");
                    owner.textBox1.AppendText(e.InnerException + "\n");
                }
                return false;
            }
            try
            {
                // A folyamathoz tartozó presenter eltárolása
                pres = tempProc.getPresenter();
                if (pres == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The received presenter is null.";
                    return false;
                }
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "The received presenter is null.";
                return false;
            }

            #endregion

            #region Logger betöltése

            // Ha választottunk ki loggert
            if (owner.pck.textBox2.Text != "")
            {
                _textStreamReader = null;
                type = null;
                con1 = null;

                try
                {
                    //DLL betöltése
                    ass = Assembly.LoadFrom(owner.pck.textBox2.Text);
                }
                catch
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "There is not such a file. The Logger path is probably wrong.";
                    return false;
                }

                // Namespace resource megkeresése és betöltése
                foreach (var s in ass.GetManifestResourceNames())
                {
                    if (s.Contains("Namespace.txt"))
                    {
                        _textStreamReader = new StreamReader(ass.GetManifestResourceStream(s));
                    }
                }
                if (_textStreamReader == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "There is no Namespace.txt resource in the Logger assembly. The namespace suppose to be defined in Namespace.txt Embedded Resource.";
                    return false;
                }
                nameSpace = _textStreamReader.ReadLine();

                //A per jel mentén felbontja a stringet, és az utolsó darabot (fájlnév és kiterjesztés) még a pontnál kettévágja, és veszi az elsőt (a fájlnevet kiterjesztés nélkül)
                className = (owner.pck.textBox2.Text.Split(new char[] { '\\' })).Last<string>().Split(new char[] { '.' }).First<string>();
                typeName = nameSpace + "." + className;

                try
                {

                    //Megfelelő osztály títpusának lekérése
                    type = ass.GetType(typeName);
                    if (type == null)
                    {
                        owner.groupBox2.Text = "Error";
                        owner.textBox1.Text = "The Logger assembly doesn't contain any class named: " + typeName;
                        return false;
                    }
                }
                catch (Exception e)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The Logger assembly doesn't contain any class named: " + typeName;

                    return false;
                }
                try
                {
                    // IProcess és 2 string tömb paraméterű konstruktor
                    con1 = type.GetConstructor(new Type[3] { IProcInterface, typeof(string[]), typeof(string[]) });
                    if (con1 == null)
                    {
                        owner.groupBox2.Text = "Error";
                        owner.textBox1.Text = "The the following type doesn't have constructor with parameter " + IProcInterface.Name + " and two string arrays: " + typeName;
                        return false;
                    }
                }
                catch
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The the following type doesn't have constructor with parameter " + IProcInterface.Name + " and two string arrays: " + typeName;
                    return false;
                }
                try
                {
                    //A Konstruktor meghívása
                    tempLogger = con1.Invoke(new object[3] { tempProc, tempProc.getInputLabels(), tempProc.getOutputLabels() });
                    if (tempLogger == null)
                    {
                        owner.groupBox2.Text = "Error";
                        owner.textBox1.Text = "Some problem occured during the use of the constructor. (Logger)";
                        return false;
                    }
                }
                catch (Exception e)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "Some problem occured during the use of the constructor. (Logger)";
                    owner.textBox1.AppendText(e.Message + "\n");
                    owner.textBox1.AppendText(e.StackTrace + "\n\n");
                    if (e.InnerException != null)
                    {
                        owner.groupBox2.Text = "Error";
                        owner.textBox1.AppendText("Inner Exception:\n");
                        owner.textBox1.AppendText(e.InnerException + "\n");
                    }
                    return false;
                }
            }

            #endregion

            #region A szabályozó betöltése

            _textStreamReader = null;
            type = null;
            con1 = null;

            try
            {
                //DLL betöltése
                ass = Assembly.LoadFrom(owner.pck.textBox1.Text);
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "There is not such a file. The Controller path is probably wrong.";
                return false;
            }

            // Namespace resource megkeresése és betöltése
            foreach (var s in ass.GetManifestResourceNames())
            {
                if (s.Contains("Namespace.txt"))
                {
                    _textStreamReader = new StreamReader(ass.GetManifestResourceStream(s));
                }
            }
            if (_textStreamReader == null)
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "There is no Namespace.txt resource in the Controller assembly. The namespace suppose to be defined in Namespace.txt Embedded Resource.";
                return false;
            }
            nameSpace = _textStreamReader.ReadLine();

            //A per jel mentén felbontja a stringet, és az utolsó darabot (fájlnév és kiterjesztés) még a pontnál kettévágja, és veszi az elsőt (a fájlnevet kiterjesztés nélkül)
            className = (owner.pck.textBox1.Text.Split(new char[] { '\\' })).Last<string>().Split(new char[] { '.' }).First<string>();
            typeName = nameSpace + "." + className;

            try
            {
                //Megfelelő osztály títpusának lekérése
                type = ass.GetType(typeName);
                if (type == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The Controller assembly doesn't contain any class named: " + typeName;
                    return false;
                }
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "The Controller assembly doesn't contain any class named: " + typeName;
                return false;
            }
            try
            {
                // Konstruktor meghívása
                con1 = type.GetConstructor(new Type[1] { typeof(IProcess) });
                if (con1 == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "The the following type doesn't have constructor with parameter " + IProcInterface.Name + " : " + typeName;
                    return false;
                }
            }
            catch
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "The the following type doesn't have constructor with parameter " + IProcInterface.Name + " : " + typeName;
                return false;
            }
            try
            {
                // Ha nem választottunk ki Logger-t, akkor közvetlen a process-t hívogatja
                if (owner.pck.textBox2.Text == "")
                {
                    controller = con1.Invoke(new object[1] { tempProc }) as AController;
                }
                else
                {
                    controller = con1.Invoke(new object[1] { tempLogger }) as AController;
                }
                if (controller == null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.Text = "Some problem occured during the use of the constructor. (Controller)";
                    return false;
                }
            }
            catch (Exception e)
            {
                owner.groupBox2.Text = "Error";
                owner.textBox1.Text = "Some problem occured during the use of the constructor. (Logger)";
                owner.textBox1.AppendText(e.Message + "\n");
                owner.textBox1.AppendText(e.StackTrace + "\n\n");
                if (e.InnerException != null)
                {
                    owner.groupBox2.Text = "Error";
                    owner.textBox1.AppendText("Inner Exception:\n");
                    owner.textBox1.AppendText(e.InnerException + "\n");
                }
                return false;
            }

            #endregion

            return true;
        }