Visual Servoing Platform  version 3.6.1 under development (2024-04-20)
keyboardControlBebop2.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Example that shows how to setup keyboard control of Parrot Bebop 2 drone in ViSP.
33  *
34  * Authors:
35  * Gatien Gaumerais
36  *
37 *****************************************************************************/
38 
39 #include <visp3/core/vpConfig.h>
40 
41 #include <visp3/core/vpTime.h>
42 #include <visp3/gui/vpDisplayX.h>
43 #include <visp3/io/vpKeyboard.h>
44 
45 #include <visp3/robot/vpRobotBebop2.h>
46 
47 #include <iostream>
48 
49 #ifdef VISP_HAVE_ARSDK
50 
59 bool handleKeyboardInput(vpRobotBebop2 &drone, int key)
60 {
61  bool running = true;
62  if (drone.isRunning()) {
63  switch (key) {
64  case 'q':
65  // Quit
66  drone.land();
67  running = false;
68  break;
69 
70  case 'e':
71  // Emergency
72  drone.cutMotors();
73  running = false;
74  break;
75 
76  case 't':
77  // Takeoff
78  drone.takeOff(false);
79  break;
80 
81  case ' ':
82  // Landing
83  drone.land();
84  break;
85 
86  case 'i':
87  // Up
88  drone.setVerticalSpeed(50);
89  break;
90 
91  case 'k':
92  // Down
93  drone.setVerticalSpeed(-50);
94  break;
95 
96  case 'l':
97  // Right
98  drone.setYawSpeed(50);
99  break;
100 
101  case 'j':
102  // Left
103  drone.setYawSpeed(-50);
104  break;
105 
106  case 'r':
107  // Forward
108  drone.setPitch(50);
109  break;
110 
111  case 'f':
112  // Backward
113  drone.setPitch(-50);
114  break;
115 
116  case 'd':
117  // Roll left
118  drone.setRoll(-50);
119  break;
120 
121  case 'g':
122  // Roll right
123  drone.setRoll(50);
124  break;
125 
126  default:
127  // No inputs -> drone stops moving
128  drone.stopMoving();
129  break;
130  }
131  vpTime::wait(25); // We wait 25ms to give the drone the time to process the command
132  } else {
133  running = false;
134  }
135  return running;
136 }
137 
148 int main(int argc, char **argv)
149 {
150  try {
151 
152  std::string ip_address = "192.168.42.1";
153 
154  int stream_res = 0; // Default 480p resolution
155 
156  bool verbose = false;
157 
158  for (int i = 1; i < argc; i++) {
159  if (std::string(argv[i]) == "--ip" && i + 1 < argc) {
160  ip_address = std::string(argv[i + 1]);
161  i++;
162  } else if (std::string(argv[i]) == "--hd_stream") {
163  stream_res = 1;
164  } else if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
165  verbose = true;
166  } else if (argc >= 2 && (std::string(argv[1]) == "--help" || std::string(argv[1]) == "-h")) {
167  std::cout << "\nUsage:\n"
168  << " " << argv[0] << "[--ip <drone ip>] [--hd_stream] [--verbose] [-v] [--help] [-h]\n"
169  << std::endl
170  << "Description:\n"
171  << " --ip <drone ip>\n"
172  << " Ip address of the drone to which you want to connect (default : 192.168.42.1).\n\n"
173  << " --hd_stream\n"
174  << " Enables HD 720p streaming instead of default 480p.\n"
175  << " --verbose, -v\n"
176  << " Enables verbose (drone information messages and velocity commands\n"
177  << " are then displayed).\n\n"
178  << " --help, -h\n"
179  << " Print help message.\n"
180  << std::endl;
181  return EXIT_SUCCESS;
182  } else {
183  std::cout << "Error : unknown parameter " << argv[i] << std::endl
184  << "See " << argv[0] << " --help" << std::endl;
185  return EXIT_FAILURE;
186  }
187  }
188 
189  std::cout << "\nWARNING: this program does no sensing or avoiding of obstacles, "
190  "the drone WILL collide with any objects in the way! Make sure the "
191  "drone has approximately 3 meters of free space on all sides.\n"
192  << std::endl;
193 
194  vpRobotBebop2 drone(verbose, true,
195  ip_address); // Create the drone with low verbose level, settings reset and corresponding IP
196 
197  if (drone.isRunning()) {
198 
199  int k = 0;
200  bool running = true;
201 
202  std::cout << "\nConfiguring drone settings ...\n" << std::endl;
203 
204  drone.setMaxTilt(10); // Setting the max roll and pitch values, the drone speed will depend on it
205 
206  drone.doFlatTrim(); // Flat trim calibration
207 
208 #ifdef VISP_HAVE_FFMPEG
209  drone.setVideoResolution(stream_res); // Setting desired stream video resolution
210  drone.setStreamingMode(0); // Set streaming mode 0 : lowest latency
211  std::cout << "\nWaiting for streaming to start ...\n" << std::endl;
212  drone.startStreaming();
213 
214  // Prepare image for display
215  vpImage<vpRGBa> I(1, 1, 0);
216  drone.getRGBaImage(I);
217  vpDisplayX display(I, 100, 100, "DRONE VIEW");
219  vpDisplay::flush(I);
220 #endif
221 
222  vpKeyboard keyboard;
223  std::cout << "\n| Control the drone with the keyboard :\n"
224  "| 't' to takeoff / spacebar to land / 'e' for emergency stop\n"
225  "| ('r','f','d','g') and ('i','k','j','l') to move\n"
226  "| 'q' to quit.\n"
227  << std::endl;
228 
229  while (running && drone.isRunning() && drone.isStreaming()) {
230 
231  k = '0'; // If no key is hit, we send a non-assigned key
232  if (keyboard.kbhit()) {
233  k = keyboard.getchar();
234  }
235  running = handleKeyboardInput(drone, k);
236 
237 #ifdef VISP_HAVE_FFMPEG
238  drone.getRGBaImage(I);
240  vpDisplay::displayText(I, 10, 10, "Press q to quit", vpColor::red);
241  vpDisplay::flush(I);
242 #endif
243  }
244  std::cout << "\nQuitting ...\n" << std::endl;
245 
246  } else {
247  std::cout << "ERROR : failed to setup drone control." << std::endl;
248  return EXIT_FAILURE;
249  }
250  } catch (const vpException &e) {
251  std::cout << "\nCaught an exception: " << e << std::endl;
252  return EXIT_FAILURE;
253  }
254 }
255 
256 #else
257 
258 int main()
259 {
260  std::cout << "\nThis example requires Parrot ARSDK3 library. You should install it.\n" << std::endl;
261  return EXIT_SUCCESS;
262 }
263 
264 #endif // #if defined(VISP_HAVE_ARSDK)
static const vpColor red
Definition: vpColor.h:211
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
Keyboard management under unix (Linux or OSX). This class is not available under windows.
Definition: vpKeyboard.h:82
int getchar()
Definition: vpKeyboard.cpp:78
int kbhit()
Definition: vpKeyboard.cpp:96
void setPitch(int value)
void setMaxTilt(double maxTilt)
void setRoll(int value)
void setVerticalSpeed(int value)
void setStreamingMode(int mode)
static void land()
void getRGBaImage(vpImage< vpRGBa > &I)
void setYawSpeed(int value)
void takeOff(bool blocking=true)
void setVideoResolution(int mode)
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.
VISP_EXPORT int wait(double t0, double t)