Good day,
I am trying to create a program that obtains orientation data from Sensors then uses that in a PID control algorithm to create corresponding PWM signals. I would like to be able to stop the program by pressing enter. I tried using opencv's waitKey(); however it does not accept my keyboard inputs. I also tried cin.get(); but it pauses my loop which creates a problem. My pwm would not get refreshed once the quadcopter changes orientation. I tried researching other methods however I could not get them to work. Here is a sample of my code using the two methods mentioned above.
Using 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;
}
Using 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;
}
You need a way of querying the current state of the keyboard, which C++ does not provide natively. In the past I have used SDL for this exact thing. I like SDL because it is simplistic in nature, and is entirely written in C. There is an example on the SDL wiki that demonstrates detecting keyboard state that allows you to detect if a button is being held down.
Edit: SDL can be put on to a Raspbian Pi. I found an example setup here