File: keyboardControlBebop2.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (264 lines) | stat: -rw-r--r-- 7,764 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/****************************************************************************
 *
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Example that shows how to setup keyboard control of Parrot Bebop 2 drone in ViSP.
 *
 * Authors:
 * Gatien Gaumerais
 *
*****************************************************************************/

#include <visp3/core/vpConfig.h>

#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpKeyboard.h>

#include <visp3/robot/vpRobotBebop2.h>

#include <iostream>

#ifdef VISP_HAVE_ARSDK

/*!

  Sends movement commands to the \e drone based on a keyboard input \e key.
  Returns a boolean indicating if the program should be stopping or not.

  \param[in] drone : the drone to control.
  \param[in] key : key input to handle.
*/
bool handleKeyboardInput(vpRobotBebop2 &drone, int key)
{
  bool running = true;
  if (drone.isRunning()) {
    switch (key) {
    case 'q':
      // Quit
      drone.land();
      running = false;
      break;

    case 'e':
      // Emergency
      drone.cutMotors();
      running = false;
      break;

    case 't':
      // Takeoff
      drone.takeOff(false);
      break;

    case ' ':
      // Landing
      drone.land();
      break;

    case 'i':
      // Up
      drone.setVerticalSpeed(50);
      break;

    case 'k':
      // Down
      drone.setVerticalSpeed(-50);
      break;

    case 'l':
      // Right
      drone.setYawSpeed(50);
      break;

    case 'j':
      // Left
      drone.setYawSpeed(-50);
      break;

    case 'r':
      // Forward
      drone.setPitch(50);
      break;

    case 'f':
      // Backward
      drone.setPitch(-50);
      break;

    case 'd':
      // Roll left
      drone.setRoll(-50);
      break;

    case 'g':
      // Roll right
      drone.setRoll(50);
      break;

    default:
      // No inputs -> drone stops moving
      drone.stopMoving();
      break;
    }
    vpTime::wait(25); // We wait 25ms to give the drone the time to process the command
  } else {
    running = false;
  }
  return running;
}

/*!

  \example keyboardControlBebop2.cpp example showing how to setup keyboard control of Parrot Bebop 2 drone.

  WARNING: this program does no sensing or avoiding of obstacles, the drone
  WILL collide with any objects in the way! Make sure the drone has about
  3-4 meters of free space around it before starting the program.

  This program makes the drone controllable with a keyboard, with display of the video streaming.
*/
int main(int argc, char **argv)
{
  try {

    std::string ip_address = "192.168.42.1";

    int stream_res = 0; // Default 480p resolution

    bool verbose = false;

    for (int i = 1; i < argc; i++) {
      if (std::string(argv[i]) == "--ip" && i + 1 < argc) {
        ip_address = std::string(argv[i + 1]);
        i++;
      } else if (std::string(argv[i]) == "--hd_stream") {
        stream_res = 1;
      } else if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
        verbose = true;
      } else if (argc >= 2 && (std::string(argv[1]) == "--help" || std::string(argv[1]) == "-h")) {
        std::cout << "\nUsage:\n"
                  << "  " << argv[0] << "[--ip <drone ip>] [--hd_stream] [--verbose] [-v] [--help] [-h]\n"
                  << std::endl
                  << "Description:\n"
                  << "  --ip <drone ip>\n"
                  << "      Ip address of the drone to which you want to connect (default : 192.168.42.1).\n\n"
                  << "  --hd_stream\n"
                  << "      Enables HD 720p streaming instead of default 480p.\n"
                  << "  --verbose, -v\n"
                  << "      Enables verbose (drone information messages and velocity commands\n"
                  << "      are then displayed).\n\n"
                  << "  --help, -h\n"
                  << "      Print help message.\n"
                  << std::endl;
        return EXIT_SUCCESS;
      } else {
        std::cout << "Error : unknown parameter " << argv[i] << std::endl
                  << "See " << argv[0] << " --help" << std::endl;
        return EXIT_FAILURE;
      }
    }

    std::cout << "\nWARNING: this program does no sensing or avoiding of obstacles, "
                 "the drone WILL collide with any objects in the way! Make sure the "
                 "drone has approximately 3 meters of free space on all sides.\n"
              << std::endl;

    vpRobotBebop2 drone(verbose, true,
                        ip_address); // Create the drone with low verbose level, settings reset and corresponding IP

    if (drone.isRunning()) {

      int k = 0;
      bool running = true;

      std::cout << "\nConfiguring drone settings ...\n" << std::endl;

      drone.setMaxTilt(10); // Setting the max roll and pitch values, the drone speed will depend on it

      drone.doFlatTrim(); // Flat trim calibration

#ifdef VISP_HAVE_FFMPEG
      drone.setVideoResolution(stream_res); // Setting desired stream video resolution
      drone.setStreamingMode(0);            // Set streaming mode 0 : lowest latency
      std::cout << "\nWaiting for streaming to start ...\n" << std::endl;
      drone.startStreaming();

      // Prepare image for display
      vpImage<vpRGBa> I(1, 1, 0);
      drone.getRGBaImage(I);
      vpDisplayX display(I, 100, 100, "DRONE VIEW");
      vpDisplay::display(I);
      vpDisplay::flush(I);
#endif

      vpKeyboard keyboard;
      std::cout << "\n| Control the drone with the keyboard :\n"
                   "|   't' to takeoff / spacebar to land / 'e' for emergency stop\n"
                   "|   ('r','f','d','g') and ('i','k','j','l') to move\n"
                   "|   'q' to quit.\n"
                << std::endl;

      while (running && drone.isRunning() && drone.isStreaming()) {

        k = '0'; // If no key is hit, we send a non-assigned key
        if (keyboard.kbhit()) {
          k = keyboard.getchar();
        }
        running = handleKeyboardInput(drone, k);

#ifdef VISP_HAVE_FFMPEG
        drone.getRGBaImage(I);
        vpDisplay::display(I);
        vpDisplay::displayText(I, 10, 10, "Press q to quit", vpColor::red);
        vpDisplay::flush(I);
#endif
      }
      std::cout << "\nQuitting ...\n" << std::endl;

    } else {
      std::cout << "ERROR : failed to setup drone control." << std::endl;
      return EXIT_FAILURE;
    }
  } catch (const vpException &e) {
    std::cout << "\nCaught an exception: " << e << std::endl;
    return EXIT_FAILURE;
  }
}

#else

int main()
{
  std::cout << "\nThis example requires Parrot ARSDK3 library. You should install it.\n" << std::endl;
  return EXIT_SUCCESS;
}

#endif // #if defined(VISP_HAVE_ARSDK)