如何“造假” 8 位处理器上的多任务处理?
我的机器人有一个带有 Adafruit 电机扩展板的 Arduino。我想在压电元件上演奏音调的同时运行电机。
问题是我不太知道如何在代码中伪造/模拟多任务处理。我尝试了这样的操作:
void goForward(int duration) {
for (int i; i<duration; i++) {
tl.run(FORWARD);
tr.run(BACKWARD);
bl.run(FORWARD);
br.run(BACKWARD);
counter++;
if (counter%4==0) {
piezo != piezo;
}
delay(1);
}
}
这会运行电机,但是它不会在我的压电元件上产生音调。有什么更好的方法来对此进行编程,以便在电机命令运行时可以以 440 Hz 的频率打开/关闭压电?
I have an Arduino with an Adafruit Motor Shield as part of my robot. I want to run the motors at the same time I play a tone on the piezo element.
The problem is that I don't quite know how to fake/simulate multitasking in my code. I tried something like this:
void goForward(int duration) {
for (int i; i<duration; i++) {
tl.run(FORWARD);
tr.run(BACKWARD);
bl.run(FORWARD);
br.run(BACKWARD);
counter++;
if (counter%4==0) {
piezo != piezo;
}
delay(1);
}
}
This runs the motors, however it doesn't create a tone on my piezo element. What would be a better way to program this so the piezo could be switched on/off at a frequency of 440 Hz while the motor commands are running?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道Arduino板,但大多数微控制器都有一些定时器中断。您是否尝试过设置这样的计时器?在定时器中断处理程序中,您可以启用/禁用压电元件,而主循环可用于控制电机。
I don't know the Arduino board, but most microcontrollers have some timer interrupt. Have you tried to setup such a timer? In the timer interrupt handler you could enable/disable the piezo element, while the main loop could be used for controlling the motor.
普通的Arduino板只有3个定时器。这将“多任务”能力限制为 3 个“线程”。 Arduino Mega 有 16 个定时器。换句话说,Arduino Mega 可以成为您的解决方案。不?
Normal Arduino boards only have 3 timers. This limits "multitask" capability to 3 "threads". Arduino Mega has 16 timers. In other words, the Arduino Mega can be your solution. No?
创建调度程序来创建并行任务,并为电机使用两个计时器,如果有第三个计时器,则将其用作蜂鸣器。
Create scheduler to create parallel tasks, and use two timers for your motors, and if you have a third one use it for the buzzer.