c++opencvtcpar.drone

Random frame of Video AR Drone is empty and then can't get a new image with OpenCV


I'm doing a very simple program that print the AR.Drone video during the flight on my computer.

It works well during a few seconds and then, randomly, the program abort because the frame is empty. I decided to test it, but now, when a frame is empty, all the following frames are empty too.

Here is my simple code :

int main(void)
{
  cv::VideoCapture cap;
  cv::Mat image;

  if (!cap.open("tcp://192.168.1.1:5555"))
    {
      printf("AR.Drone ERROR CONNECT\n");
      return -1;
    }

  takeoff();

  while (42)
    {
      cap >> image;

      if (!image.empty())
        {
           cv::imshow("AR.Drone", image);
           std::cout << "OK" << std::endl;
        }
      else
        std::cout << "ERROR" << std::endl;
      cv::waitKey(1);
    }    
  return 0;
}

My output is :

>OK
>OK
>[...]
>OK
>ERR
>ERR
>ERR

But it should be :

>OK
>OK
>[...]
>OK
>ERR   // okay you got an error ? ...
>OK    // ... I give you a new frame :)
>OK

Why does it fail forever?

If I don't protect it I get the error “OpenCV Error: Bad flag” and it abort. The connection on the AR.Drone is TCP, so I can't slow down the waitKey...

Any Idea?


Solution

  • I got the same issue when I wanted to get stable video stream with AR.Drone 2.0 It took some time, but I realized the frame missing is because the drone consider the connection as lost after 2 seconds without AT command. So if you open the stream and do nothing more it is fine (the stream will be good), but after you send any AT command then after 2 second the drone will disconnect. So all you need to do to send for example hovering command to the drone if no other command sent to keep the connection alive.