你好,
我正在尝试创建一个程序,从传感器获取方向数据,然后使用PID控制算法创建相应的PWM信号。我希望能够通过按下回车键来停止程序。我尝试使用opencv的waitKey()方法,但它不接受我的键盘输入。我还尝试了cin.get()方法,但它会暂停我的循环,这会导致问题。一旦四轴飞行器改变方向,我的PWM就无法得到刷新。我尝试研究其他方法,但是我无法使它们正常工作。以下是我使用上述两种方法的代码示例。
使用cin.get():
using namespace std;
int main(void){
while(1){
/* get orientation data */
/* output corresponding pwm */
//Press enter to stop loop
if(cin.get() == '\n'){
/* Stop Pwm */
break;
}
}
return 0;
}
使用waitKey()函数:
using namespace std;
using namespace cv;
int main(void){
while(1){
/* get orientation data */
/* output corresponding pwm */
//Press ESC to stop loop
int key = waitKey(33){
if(key == 27){
/* Stop Pwm */
break;
}
}
return 0;
}