예제 #1
0
        public static void Main(string[] args)
        {
            // Create serial port
            string portName = "COM3";

            myPort = new SerialPort(portName);

            // Try to send data over port
            string testString = "Hi there";

            sendStringSerial(testString);

            // Start receiving data over serial port (Arduino serial.write commands will show up)
            startSerialListen();

            // Get most recent CubeExplorer solution
            string webData = cubeExplorer.mostRecentSolution();

            // Display the solution received from CubeExplorer
            Console.WriteLine("Solution to carry out:");
            Console.WriteLine(webData + "\n");

            // Stores the best sequence of moves to carry out the solution from CubeExplorer discovered so far
            SolvingSequence bestSequence = Solver.findBestSeq(webData);

            // Prints the best sequence found
            Console.WriteLine("Best sequence found:");
            bestSequence.printSequence();
            Console.WriteLine();

            // Print statistics on the solving sequence
            Console.WriteLine("Best sequence statistics:");
            bestSequence.calculate();
            bestSequence.printStatistics();

            // Print low level claw instructions to carry out bestSequence
            printLowLevel(bestSequence.moves);

            // Keeps the console window open
            Console.ReadLine();
        }
예제 #2
0
파일: Solver.cs 프로젝트: dEgolf/Cube-Robot
        // Find the best way to carry out the solution "inSoln" using our claws
        public static SolvingSequence findBestSeq(string inSoln)
        {
            SolvingSequence bestSequence = new SolvingSequence(new List <string> {
            });

            bestSequence.totalTime = 9000; // The total time of this sequence is initialized to a very high value so that the first sequence found will replace this empty starting one

            // The program loops a certain number of times. Each time, it finds a sequence of moves that will carry out the solution given by CubeExplorer.
            // The best solution is stored and displayed at the end.
            for (int j = 0; j < 1000; j++)
            {
                // For debugging purposes, one possible color arrangement is as follows: Red = front, green = right, yellow = up, orange = back, blue = left, white = down.
                // Using colors makes it easier to keep track which face is the F face, for instance, after the cube has been rotated.
                // This constructor takes the three cycles produced by the NE, SE, and equatorial rotations
                Cube myCube = new Cube(new List <string> {
                    "F", "D", "B", "U"
                }, new List <string> {
                    "R", "U", "L", "D"
                }, new List <string> {
                    "R", "B", "L", "F"
                });

                // Creates a SolvingSequence to hold the sequence of moves that will be generated
                SolvingSequence sequence = new SolvingSequence(new List <string> {
                });

                // Goes through each instruction the solving method provided by CubeExplorer, and determines a way in which the robot could carry it out
                for (int i = 0; i < inSoln.Length; i++)
                {
                    // Gets the next character in the instructions.
                    // This can be the next face to access, but also spaces and modifiers like "2" and "'"
                    // We only work with it if it is a face, and we look ahead to see modifiers
                    char face = inSoln[i];

                    // If the next character specifies a face to be accessed, this section stores a move to access the face in sequence.moves
                    if (face == 'R' || face == 'L' || face == 'F' || face == 'U' || face == 'B' || face == 'D')
                    {
                        // Determines the needed cube rotation, if any
                        string rotationUsed = myCube.AccessFace(Convert.ToString(face));

                        // Stores the cube rotation, if applicable
                        if (rotationUsed != "")
                        {
                            sequence.moves.Add(rotationUsed);
                        }

                        // Determines and stores the face that needs to be turned
                        if (myCube.ne.faceList[0] == Convert.ToString(face))
                        {
                            if (inSoln[i + 1] == ' ')
                            {
                                sequence.moves.Add("1");
                            }
                            else if (inSoln[i + 1] == '\'')
                            {
                                sequence.moves.Add("1p");
                            }
                            else if (inSoln[i + 1] == '2')
                            {
                                sequence.moves.Add("12");
                            }
                        }
                        else if (myCube.se.faceList[0] == Convert.ToString(face))
                        {
                            if (inSoln[i + 1] == ' ')
                            {
                                sequence.moves.Add("2");
                            }
                            else if (inSoln[i + 1] == '\'')
                            {
                                sequence.moves.Add("2p");
                            }
                            else if (inSoln[i + 1] == '2')
                            {
                                sequence.moves.Add("22");
                            }
                        }
                    }
                }

                // Calculates how long the sequence takes to execute
                sequence.calculate();

                // Stores the sequence just calculated in bestSequence if the just calculated sequence is faster than the best sequence found so far
                if (sequence.totalTime < bestSequence.totalTime)
                {
                    bestSequence.totalTime = sequence.totalTime;
                    bestSequence.moves.Clear();

                    for (int i = 0; i < sequence.moves.Count; i++)
                    {
                        bestSequence.moves.Add(sequence.moves[i]);
                    }

                    bool verboseSeqResults = false;
                    if (verboseSeqResults == true)
                    {
                        bestSequence.printSequence();
                        bestSequence.calculate();
                        bestSequence.printStatistics();
                    }
                }
            }

            return(bestSequence);
        }