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