Mindstorm NXT 编程循环退出条件
我正在为工程课开发一个机器人。为了课程的目的,我需要使用 NXT 编程语言。为了移动,机器人需要沿着一条黑实线移动。
如果机器人失去了线,我让它向左扫描 1 秒,或者直到它到达黑线。如果没有找到线,它会向右扫描 2 秒,以便到达初始位置,然后再扫描 1 秒或实现旋转。
我设置了循环,这样如果没有找到线,机器人就会继续移动。它运行了整整 1 秒的时间段。如果找到该线,运动就会停止,但仍需完成整秒。最终,这意味着我的程序运行完美,但速度真的很慢。
tl;dr 有没有办法在 LEGO Mindstorm 编程环境中制作具有两个退出条件的循环? 1 秒过去后,或者传感器获得所需的输入?
I am developing a robot for an engineering class. For the purposes of the class I am required to use the NXT programming language. To move, the robot needs to follow a solid black line.
If the robot looses the line, I have it scan to the left for 1 second, or until it reaches a black line. If no line is found it scans to the right for 2 seconds so the initial position is reached then 1 more second or rotation is achieved.
I have the loop set up so that if the line has not been found, the robot continues to move. That runs for a full 1 second time period. If the line is found, motion stops, but the full second still has to complete. Ultimately that means that my program works perfectly, but is really really slow.
tl;dr Is there a way to make loops with two exit condition in the LEGO Mindstorm programming environment? Either after 1 second has elapsed, or a sensor gets the desired input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后两个块是设置为“OR”的逻辑块。在本例中,正在监视 3 个传感器。如果任何一个被触发,程序就会退出循环。
这是来自 http://www.hightechkids.org/sites/default/files/CoachingLibrary/fll_programming_101_nxt_g.pdf
The last two blocks are Logic blocks set to "OR". In this case, 3 sensors are being watched. If any one is tripped the program drops out of the loop.
This is from page 86 of http://www.hightechkids.org/sites/default/files/CoachingLibrary/fll_programming_101_nxt_g.pdf
您可以做的是缩短超时时间(例如 100 毫秒),并在找到该行或循环运行 10 次时停止。
我不是头脑风暴专家,但我希望它具有 OR 功能。
What you could do is make the timeout shorter (100 ms for instance) and stop if the line is found OR the loop ran 10 times.
I am no mindstorms expert, but I expect it to have an OR function.
Mindstorms 没有 OR 函数。
解决方案是创建一个 exit 变量,然后有两个 switch 语句。在上面的示例中,我有一个带有计时器的 switch 语句,另一个带有传感器输出。将退出变量设置为 false,然后在 while 循环内运行,直到退出变量为 true。
如果计时器到期,则将该变量设置为 true,否则忽略退出变量。对传感器的输出执行相同的操作。
这是一个混乱且相当长的解决方案,但它在相当有限的乐高 Mindstorms 系统中完美地解决了问题。
Mindstorms does not have an OR function.
The solution is to create an exit variable, then have two switch statements. In the example above, I had one switch statement with a timer, and the other with the sensor output. Set the exit variable to false, then inside a while loop, run until the exit variable is true.
If the timer expires, set the variable to true, otherwise ignore the exit variable. Do the same with the sensor's output.
This is a messy and rather long solution, but it solves the problem perfectly within the rather limited LEGO Mindstorms system.