private void ConnectionThread()
    {
        // Model related logic
        connection = new ClientConnectionHandler();
        process    = new Process();

        // Calls python training script.
        process.StartInfo.FileName = @"C:\Users\Michael\AppData\Local\Microsoft\WindowsApps\python.exe";
        string scriptPath = @"C:\Git\Virtual-Hand\PythonScripts\HandController.py";
        // string modelName = "Real"; // TODO, remove hard coded at some point
        // string modelName = "RealData15_shifted20"; // TODO, remove hard coded at some point
        // string modelName = "T180_D6"; // TODO, remove hard coded at some point
        string modelName = "long2"; // TODO, remove hard coded at some point

        process.StartInfo.Arguments = scriptPath;

        // Starts the process
        print("Starting the process: " + process.StartInfo.FileName);
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow  = true;
        process.Start();
        System.Threading.Thread.Sleep(6000);

        connection.println(modelName);

        print("Started the Python process. ");
        print("From_Python: '" + connection.readline() + "'");
        print("From_Python: '" + connection.readline() + "'");
        for (int i = 0; i < 15; i++) // TODO, remove this hard coding at some point
        {
            print("From Python: '" + connection.readline() + "'");
            connection.println("Keep alive message.");
        }

        print("From_Python: '" + connection.readline() + "'");
        print("From_Python: '" + connection.readline() + "'");

        running = true;
        while (running)
        {
            // Sends the command to proceed with th next frame
            connection.println("next");
            // Sends current limb data
            connection.println(GetStringLimbData_forThread());
            // Receives the velocities to apply
            limb_velocities = GeneralData.string2floatArray(connection.readline());
        }
    }
Example #2
0
    private void RunTrainingThread()
    {
        /*
         * Starting Sequence
         */

        // Model related logic
        connection = new ClientConnectionHandler();
        process    = new Process();

        // Calls python training script.
        process.StartInfo.FileName = @"C:\Users\Michael\AppData\Local\Microsoft\WindowsApps\python.exe";
        string scriptPath  = @"C:\Git\Virtual-Hand\PythonScripts\ModelTrainerV4.py";
        string dataSetName = "RealData15_smoothed"; // To be hard-coded (for now)
        string modelName   = "FirstModelTest";

        process.StartInfo.Arguments = scriptPath;


        // Starts the process
        print("Starting the process: " + process.StartInfo.FileName);
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow  = true;
        process.Start();
        System.Threading.Thread.Sleep(6000);

        connection.println(dataSetName);
        connection.println(modelName);

        print("Started the Python process. ");

        // Interactions with the Python Script
        // string acknowledgement = stdoutReadLine();
        string acknowledgement = connection.readline();

        print("Acknowledgement from Python: " + acknowledgement);

        if (acknowledgement.Equals("Ready") == false)
        {
            print("Did not receive acknowledgement from Python script.");
            Quit();
        }
        else
        {
            connection.println("Ready");

            // Obtains starting angles from the python script
            print("Reading start angles...");
            // string[] stringBaseAngles = connection.readline().Split(' ');
            // for (int i = 0; i < stringBaseAngles.Length; i++)
            // {
            //     print(stringBaseAngles[i]);
            //     startingAngles[i] = float.Parse(stringBaseAngles[i]);
            // }

            startingAngles = GeneralData.string2floatArray(connection.readline());

            // startingAngles = new float[movableLimbs.Length]; // todo, remove this

            print("Expecting start angles...");
            foreach (var angle in startingAngles)
            {
                controlled_print(angle.ToString());
            }

            controlled_print("Python angles obtained: " + startingAngles.ToString());

            // waitingForNewFrame = true;
            ResetTrainingSequence_forThread(true);
            Ready();
        }

        // Resets the system multiple times to achieve stable start position
        while (positionSetupResets > 0)
        {
            if (positionSetupResets % 100 == 0)
            {
                critical_print("Positional resets remaining: " + positionSetupResets.ToString());
            }

            zeroPosition = true;
            ResetTrainingSequence_forThread(false);
            positionSetupResets = positionSetupResets - 1;
        }

        // Initial ready (other ready's are received upon restart)
        if (connection.readline().Equals("Ready") == false)
        {
            critical_print("False start! (did not receive loop ready)");
            Quit();
        }

        /*
         * Process Loop
         */
        while (running == true)
        {
            // Step Loop-0 (as per Pprotocol)
            // controlled_print("Stopwatch: " + stopwatch.ElapsedMilliseconds.ToString());
            // if (waitingForNewFrame == true)
            // {
            // controlled_print("Next will be: ");
            // nextFrameTimeMs = long.Parse(connection.readline());
            // controlled_print("Next time frame: " + nextFrameTimeMs);
            // waitingForNewFrame = false;
            // }
            // else if (stopwatch.ElapsedMilliseconds >= nextFrameTimeMs) // Step Loop-1 (as per protocol)
            // {
            // Step Loop-2 (as per protocol)
            string toSend = GetStringLimbData_forThread();

            // Sends data to python script
            // Step Loop-3 (as per protocol)
            long current_time_ms = stopwatch.ElapsedMilliseconds;
            connection.println(current_time_ms.ToString());

            critical_print("Current time: " + current_time_ms.ToString());

            // Step Loop-4 (as per protocol)
            connection.println(toSend);

            expectedAngles =
                GeneralData.string2floatArray(connection
                                              .readline()); // Obtains the expected angles from the Python Script

            // Step Loop-5 (as per protocol)
            string nextCommand = connection.readline();
            // Step Loop-6 (as per protocol)
            if (nextCommand.Equals("Reset"))
            {
                if (FrameViewer.earlyReset == true)
                {
                    ResetTrainingSequence_forThread(true);
                }

                if (connection.readline().Equals("Ready"))
                {
                    ResetTrainingSequence_forThread(true);
                }
                else
                {
                    critical_print("We've got a big mistake. No reset Ready received.");
                }

                Ready();
            }
            else if (nextCommand.Equals("Quit"))
            {
                Quit();
            }
            else if (nextCommand.Equals("Next"))
            {
                // Obtains and applies torques from python script to the limbs
                string stringTorques = connection.readline();
                controlled_print("Received Torques length: " + stringTorques.ToString());

                lock (torquesToApply_locker)
                {
                    // for (int i = 0; i < movableLimbs.Length; i++)
                    // {
                    //     torquesToApply[i] = float.Parse(stringTorques[i]);
                    // }

                    torquesToApply = GeneralData.string2floatArray(stringTorques);
                }

                // waitingForNewFrame = true;
                Ready();
            }
            else
            {
                print(
                    "Unknown nextCommand sent from python script (" + nextCommand + "). Aborting program.");
                Quit();
            }

            // }
        }
    }