2015-10-04 47 views
5

所以我正在寻找扩展/适应我自己需要的API。我正在谈论乐高Mindstorms C#API。我正在构建自己的API(基于适配器模式),所以我可以用更好的OO方式编写机器人。对象链接与API扩展中的方法

以下是关于API是如何工作的链接:Lego Mindstorms EV3 C# API

但现在我被困在C#API处理命令砖一个非常奇怪的方式。

绝对不是面向对象的方式...

一个例子:要发送一个命令,你需要有砖的实例发送命令砖。但是DirectCommand实例与砖块无关。

await brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, 50, 5000); 

所以我想要做的是让砖和DirectCommand松耦合。

这是另一个例子:执行一批命令。你必须写出所有的命令,然后执行一个特定的方法。在当前的API中,没有办法循环访问数组并添加堆栈元素,以便稍后执行它们。

brick.BatchCommand.TurnMotorAtSpeedForTime(OutputPort.A, 50, 1000, false); 
brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.C, 50, 1000, false); 
brick.BatchCommand.PlayTone(50, 1000, 500); 
await brick.BatchCommand.SendCommandAsync(); 

所以我喜欢做的事情是:

创建像普雷通公司的命令(..)将其添加到命令的ArrayList,然后遍历它...

List<Command> toBeExecuted = new List<Command>; 
toBeExecuted.Add(DirectCommand.PlayTone(50, 1000, 500)); 
brick.DirectCommand(toBeExecuted[0]); 

所以,如果有人可以帮助...我会很高兴:)

回答

1

不正是他们设计的,但你可以排队起来的人ist of Tasks,并通过它来代替?

像这样:

static void Main(string[] args) 
{ 
    //---- queue commands into a "batch" 
    List<Task> toBeExecuted = new List<Task>(); 
    toBeExecuted.Add(Task.Run(() => dothing())); 
    toBeExecuted.Add(Task.Run(() => dothing())); 
    toBeExecuted.Add(Task.Run(() => dothing())); 
    toBeExecuted.Add(Task.Run(() => dothing())); 
    toBeExecuted.Add(Task.Run(() => dothing())); 


    //---- elsewhere 
    Task.WaitAll(toBeExecuted.ToArray()); //fire off the batch 
    await brick.BatchCommand.SendCommandAsync(); //send to brick 
} 

代dothing()为要排队batchcommand:

.Add(Task.Run(() => brick.BatchCommand...()));

+0

呀,这将是一个伟大的想法。这个实现的唯一问题是,我不能使用Command类的实例与砖对象分开。这是我想要做的主要事情之一,因为我希望能够将它们与稍后执行的砖块分开执行。 –