public static async Task <int> MoveBetweenTwoPoints(AI.Step current, AI.Step next)
        {
            int delay_waitfor1move = C * 150;

            int delay_upanddown = 8000;

            int delay_betweenaction = 600;

            var actions = GetActionSeq(current, next);

            int delay = 0;

            foreach (var item in actions)
            {
                int movedelay = delay_waitfor1move * (item[1] - '0');
                delay += movedelay;
                switch ((char)item[0])
                {
                case 'B': await Back((int)(item[1] - '0') *C); break;

                case 'F': await Forward((int)(item[1] - '0') *C); break;

                case 'R': await Right((int)(item[1] - '0') *C); break;

                case 'L': await Left((int)(item[1] - '0') *C); break;
                }
                await Task.Delay(movedelay);

                delay += delay_betweenaction;
                await Task.Delay(delay_betweenaction);
            }

            // After take the step, land
            await Comm.SendCommand("land");

            await Task.Delay(delay_upanddown);

            await Comm.SendCommand("takeoff");

            await Task.Delay(delay_upanddown);

            delay += delay_upanddown * 2;

            return(delay);
        }
        // In this method, no yaw will be contained.
        public static List <string> GetActionSeq(AI.Step current, AI.Step next)
        {
            List <string> actions = new List <string>();
            int           delta_x = next.Move.X - current.Move.X;
            int           delta_y = next.Move.Y - current.Move.Y;

            if (delta_x != 0)
            {
                string action = (delta_x > 0 ? "B" : "F") + Math.Abs(delta_x).ToString();
                Debug.WriteLine(action);
                actions.Add(action);
            }
            if (delta_y != 0)
            {
                string action = (delta_y > 0 ? "R" : "L") + Math.Abs(delta_y).ToString();
                Debug.WriteLine(action);
                actions.Add(action);
            }
            return(actions);
        }
 public static async Task MoveToStart(AI.Step current)
 {
     await MoveBetweenTwoPoints(current, new AI.Step {
         Move = new AI.GameState.Move(1, 1, 1)
     });
 }
Example #4
0
        private async void Btn_Start_Click(object sender, RoutedEventArgs e)
        {
            // Start the workflow
            await Flight.Comm.SendCommand("takeoff");

            await Task.Delay(8000);

            var TrainingSteps = AI.Nums.Steps;

            for (int i = 0; i < TrainingSteps.Count; i++)
            {
                var step = TrainingSteps[i];
                if (i == 0)
                {
                    current = new AI.Step {
                        Move = new AI.GameState.Move(1, 1, 1)
                    };
                }
                else
                {
                    // If a game is not finished, the 'current' step
                    // is given by AI. If a game is just finished,
                    // the 'current' location is the center point of the board.
                    if (IsUseLastStep)
                    {
                        current = TrainingSteps[i - 1];
                    }
                }
                if (step.Type == AI.StepType.OUTCOME)
                {
                    ClearBoard();
                    string text;
                    switch (step.Result)
                    {
                    case 1: text = "O WINs"; break;

                    case -1: text = "X WINs"; break;

                    case 0: text = "DRAW"; break;

                    default: text = "NONE"; break;
                    }
                    TB_result.Text = text;

                    // After a game, move the UAV back to the start grid (center)
                    await Flight.Move.MoveBetweenTwoPoints(current, new AI.Step {
                        Move = new AI.GameState.Move(1, 1, 1)
                    });

                    current = new AI.Step {
                        Move = new AI.GameState.Move(1, 1, 1)
                    };
                    IsUseLastStep = false;
                }
                else
                {
                    // Update UI
                    TB_result.Text = "UNFINISHED";
                    TB_status.Text = step.Type.ToString();
                    Image        image  = new Image();
                    BitmapSource source = new BitmapImage(new Uri(string.Format("ms-appx:///Assets//Icons//{0}.png", step.Move.Side)));
                    image.Source = source;
                    image.Margin = new Thickness(8);
                    board.Children.Add(image);
                    Grid.SetRow(image, step.Move.X);
                    Grid.SetColumn(image, step.Move.Y);

                    // Update AI logs
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        StringBuilder builder = new StringBuilder();
                        builder.AppendLine(step.Type.ToString());
                        builder.AppendLine(AI.Log.GetMatString(step.CurrentBoard));
                        TB_AI_log.Text += builder.ToString();
                    });

                    // Move Tello to the next point (node)
                    await Flight.Move.MoveBetweenTwoPoints(current, step);

                    IsUseLastStep = true;
                }
            }
        }