Exemplo n.º 1
0
        public void setMovementOfBuggy(int index, BuggyMovement movement)
        {
            if (index < 0 || index >= 2)
            {
                return;
            }

            buggies[index].movement = movement;
        }
Exemplo n.º 2
0
        static void DataReceivedHandler(object sender,
                                        SerialDataReceivedEventArgs e)
        {
            String inmessage = port.ReadLine();

            archive.Add(inmessage);

            Console.WriteLine();
            Console.WriteLine(inmessage);

            if (inmessage.Length == 0 || '0' > inmessage[0] || '9' < inmessage[0])
            {
                Console.Write(">");
                return;
            }

            int buggy = Int32.Parse(inmessage.Substring(0, 1));

            inmessage = inmessage.Substring(3, inmessage.Length - 4).ToLower();

            if (inmessage == "buggy turned on")
            {
                BuggyMovement movement = track.getMovementOfBuggy(buggy - 1);
                double        speed    = track.getSpeedOfBuggy(buggy - 1);

                Thread.Sleep(500);

                if (movement == BuggyMovement.Stopped)
                {
                    send(buggy, "stop");
                }
                else if (movement == BuggyMovement.FollowingLine)
                {
                    send(buggy, "run");
                }
                else if (movement == BuggyMovement.TurningRight)
                {
                    send(buggy, "turn right");
                }
                else if (movement == BuggyMovement.TurningLeft)
                {
                    send(buggy, "turn left");
                }
                else if (movement == BuggyMovement.Rotating)
                {
                    send(buggy, "rotate left");
                }

                Thread.Sleep(100);

                send(buggy, "full power");
                for (int i = 0; i < (1.0 - speed) / 0.1; i++)
                {
                    Thread.Sleep(100);
                    send(buggy, "reduce power");
                }
            }
            else if (inmessage == "buggy id set to 1")
            {
                track.setOrientationOfBuggy(0, BuggyOrientation.Clockwise);
                track.setSectionForBuggy(0, 3);

                Console.WriteLine("Buggy 1 connected!");
            }
            else if (inmessage == "buggy id set to 2")
            {
                track.setOrientationOfBuggy(1, BuggyOrientation.CounterClockwise);
                track.setSectionForBuggy(1, 3);

                Console.WriteLine("Buggy 2 connected!");
            }
            else if (inmessage == "buggy running")
            {
                track.setMovementOfBuggy(buggy - 1, BuggyMovement.FollowingLine);
            }
            else if (inmessage == "buggy stopping")
            {
                track.setMovementOfBuggy(buggy - 1, BuggyMovement.Stopped);
            }
            else if (inmessage == "buggy turning right")
            {
                track.setMovementOfBuggy(buggy - 1, BuggyMovement.TurningRight);
            }
            else if (inmessage == "buggy turning left")
            {
                track.setMovementOfBuggy(buggy - 1, BuggyMovement.TurningLeft);
            }
            else if (inmessage == "buggy rotating left")
            {
                track.setMovementOfBuggy(buggy - 1, BuggyMovement.Rotating);
            }
            else if (inmessage == "buggy reducing power")
            {
                track.setSpeedOfBuggy(buggy - 1, track.getSpeedOfBuggy(buggy - 1) - 0.1);
            }
            else if (inmessage == "buggy increasing power")
            {
                track.setSpeedOfBuggy(buggy - 1, track.getSpeedOfBuggy(buggy - 1) + 0.1);
            }
            else if (inmessage == "buggy half power")
            {
                track.setSpeedOfBuggy(buggy - 1, 0.5);
            }
            else if (inmessage == "buggy full power")
            {
                track.setSpeedOfBuggy(buggy - 1, 1.0);
            }
            else if (inmessage == "passed gantry")
            {
                track.setMovementOfBuggy(buggy - 1, BuggyMovement.FollowingLine);
                track.setSectionForBuggy(buggy - 1, track.getNextSectionForBuggy(buggy - 1, false));
                printTrack();
            }
            else if (inmessage == "buggy parked")
            {
                track.setMovementOfBuggy(buggy - 1, BuggyMovement.Stopped);
                track.setSectionForBuggy(buggy - 1, track.getNextSectionForBuggy(buggy - 1, true));
                printTrack();

                if (mode == OperationMode.Bronze)
                {
                    Console.WriteLine("Bronze challenge complete!");
                }
                else if (mode == OperationMode.Silver)
                {
                    if (buggy == 1)
                    {
                        Console.WriteLine("Silver challenge complete!");
                    }
                    else if (buggy == 2)
                    {
                        send(1, "leave gantry");
                    }
                }
                else if (mode == OperationMode.Gold)
                {
                    if (buggy == 1)
                    {
                        Console.WriteLine("Gold challenge complete!");
                    }
                    else if (buggy == 2)
                    {
                        Thread.Sleep(500);
                        send(1, "leave gantry");
                    }
                }
            }
            else if (inmessage.StartsWith("detected gantry"))
            {
                int gantry = Int32.Parse(inmessage.Substring(16));
                track.setMovementOfBuggy(buggy - 1, BuggyMovement.Stopped);
                track.setGantryForBuggy(buggy - 1, gantry - 1);
                printTrack();

                if (mode == OperationMode.Bronze)
                {
                    if (gantry == 2)
                    {
                        buggy1rounds++;
                    }
                    if (buggy1rounds < 2)
                    {
                        Thread.Sleep(500);
                        send(1, "leave gantry");
                    }
                    else
                    {
                        Thread.Sleep(500);
                        send(1, "park right");
                    }
                }
                else if (mode == OperationMode.Silver)
                {
                    if (buggy == 1)
                    {
                        if (gantry == 0)
                        {
                            gantry = track.getNextGantryForBuggy(0, false);
                        }

                        if (gantry == 1)
                        {
                            Thread.Sleep(500);
                            send(1, "leave gantry");
                        }
                        else if (gantry == 2)
                        {
                            buggy1rounds++;
                            if (buggy1rounds == 1)
                            {
                                Thread.Sleep(500);
                                send(1, "leave gantry");
                            }
                            else if (buggy1rounds == 2)
                            {
                                Thread.Sleep(500);
                                send(1, "park right");
                            }
                        }
                        else if (gantry == 3)
                        {
                            Thread.Sleep(500);
                            send(2, "run");
                        }
                    }
                    else if (buggy == 2)
                    {
                        if (gantry == 0)
                        {
                            gantry = track.getNextGantryForBuggy(1, false);
                        }

                        if (gantry == 2)
                        {
                            Thread.Sleep(500);
                            send(2, "leave gantry");
                        }
                        else if (gantry == 1)
                        {
                            Thread.Sleep(500);
                            send(2, "park left");
                        }
                    }
                }
                else if (mode == OperationMode.Gold)
                {
                    if (buggy == 1)
                    {
                        if (gantry == 0)
                        {
                            gantry = track.getNextGantryForBuggy(0, false);
                        }

                        if (gantry == 1)
                        {
                            Thread.Sleep(500);
                            send(1, "leave gantry");
                        }
                        else if (gantry == 2)
                        {
                            buggy1rounds++;
                            Console.WriteLine("Buggy 1 completed round " + buggy1rounds);
                            if (buggy1rounds < goldtarget)
                            {
                                Thread.Sleep(500);
                                send(1, "leave gantry");
                            }
                            else
                            {
                                Thread.Sleep(500);
                                send(1, "park right");
                            }
                        }
                        else if (gantry == 3)
                        {
                            Thread.Sleep(500);
                            send(2, "run");
                        }
                    }
                    else if (buggy == 2)
                    {
                        if (gantry == 0)
                        {
                            gantry = track.getNextGantryForBuggy(1, false);
                        }

                        if (gantry == 2)
                        {
                            Thread.Sleep(500);
                            send(2, "leave gantry");
                        }
                        else if (gantry == 1)
                        {
                            buggy2rounds++;
                            Thread.Sleep(500);
                            send(2, "park left");
                        }
                    }
                }
            }
            else if (inmessage == "obstacle detected")
            {
                track.setBuggyHasObstacle(buggy - 1, true);
            }
            else if (inmessage == "obstacle gone")
            {
                track.setBuggyHasObstacle(buggy - 1, false);
            }

            Console.Write(">");
        }