用C#编写EV3

时间:2018-12-20 12:34:54

标签: c#

我和我的朋友们正在一个学校项目上,我们希望它可以在纸上写东西。我们正在Visual Studio中使用c#对其进行编程。

我们遇到的问题是它以错误的顺序执行命令。如果我们要使其前进然后转弯,它将尝试同时进行。我们已经使用Task.Delay来解决此问题,但是代码变得非常混乱,试图找到延迟的确切时间很烦人。

有人知道其他解决方法吗?下面的代码显示了我们的工作方式。

    public static async void Go(Brick brick, uint duration, bool forward)//Makes it go forward
    {
        if (!forward)
        {
            await brick.DirectCommand.TurnMotorAtSpeedForTimeAsync(OutputPort.B | OutputPort.C, -speed, duration, true);
            Console.WriteLine("Going backwards");
        }
        else
        {
            await brick.DirectCommand.TurnMotorAtSpeedForTimeAsync(OutputPort.B | OutputPort.C, speed, duration, true);
            Console.WriteLine("Going forward");
        }

    }

    public static async void Turn(Brick brick,float degrees, bool Clockwise)//Turns the robot. If it goes over for exampel 90 degrees it turns back a little
    {
        int direction;
        int speed = 4;
        degrees = Math.Abs(degrees);
        DateTime start = DateTime.Now;
        if (Clockwise)
        {
            direction = 1;
        }
        else
        {
            direction = -1;
        }

        float startposition = brick.Ports[InputPort.Two].SIValue;
        await brick.DirectCommand.TurnMotorAtSpeedAsync(OutputPort.B, speed * direction);
        await brick.DirectCommand.TurnMotorAtSpeedAsync(OutputPort.C, -speed * direction);
        bool loopar = true;
        while (loopar)
        {
            float DeltaPosition = Math.Abs(brick.Ports[InputPort.Two].SIValue - startposition);
            if (DeltaPosition >= degrees)
            {
                await brick.DirectCommand.TurnMotorAtSpeedAsync(OutputPort.B | OutputPort.C, 0);
                loopar = false;
            }
            //await Task.Delay(1);
            Console.WriteLine(DeltaPosition);
        }
        loopar = true;
        await Task.Delay(100);
        await brick.DirectCommand.TurnMotorAtSpeedAsync(OutputPort.B, -1 * direction);
        await brick.DirectCommand.TurnMotorAtSpeedAsync(OutputPort.C, 1 * direction);
        while (loopar)
        {
            float DeltaPosition = Math.Abs(brick.Ports[InputPort.Two].SIValue - startposition);
            if (DeltaPosition <= degrees)
            {
                await brick.DirectCommand.TurnMotorAtSpeedAsync(OutputPort.B | OutputPort.C, 0);
                loopar = false;
            }
            Console.WriteLine(DeltaPosition);
        }
        TimeSpan span = DateTime.Now - start;
        Console.WriteLine(span);
    }

    public static async void L(Brick brick)
    {
        Go(brick, 1000, true);
        await Task.Delay(1000);
        Up(brick);
        await Task.Delay(500);
        Turn(brick, 90, false);
        await Task.Delay(5000);
        Down(brick);
        await Task.Delay(500);
        Go(brick, 500, true);
    }

正如我所说,我们不想使用Task.Delay,但这仅是我们目前拥有的解决方案。

谢谢!

1 个答案:

答案 0 :(得分:5)

async void是异步的,但不能等待。所有这些调用将在需要时执行。

像这样使它们async Task

// public static async void Go(Brick brick, uint duration, bool forward)
   public static async Task Go(Brick brick, uint duration, bool forward)

然后等待呼叫:

public static async void L(Brick brick)
{
    await Go(brick, 1000, true);        
    await Up(brick);        
    await Turn(brick, 90, false);        
    await Down(brick);        
    await Go(brick, 500, true);
}

L()方法也可能也是async Task,这里没有足够的信息可以说明。

通常,async void是危险的,应该避免使用,除非您非常确定自己在做什么。