This code shows how to setup keyboard control of a drone equipped with a Pixhawk connected to a Jetson TX2 that runs this test using ViSP. The drone is localized thanks to Qualisys Mocap. Communication between the Jetson and the Pixhawk is based on Mavlink using MAVSDK 3rd party.
#include <iostream>
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_MAVSDK) && ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpKeyboard.h>
#include <visp3/robot/vpRobotMavsdk.h>
#ifdef ENABLE_VISP_NAMESPACE
#endif
bool handleKeyboardInput(vpRobotMavsdk &drone, int key, bool &flying, double &lastCommandTime)
{
bool running = true;
if (drone.isRunning()) {
switch (key) {
case 'q':
std::cout << "sending command" << std::endl;
drone.land();
flying = false;
running = false;
break;
case 'a':
if (flying == true) {
std::cout << "sending command" << std::endl;
drone.land();
flying = false;
}
break;
case 'e':
std::cout << "sending command" << std::endl;
drone.kill();
flying = false;
running = false;
break;
case 't':
std::cout << "sending command" << std::endl;
drone.takeOff();
flying = true;
drone.takeControl();
break;
case ' ':
if (flying == true) {
drone.setVerticalSpeed(0.2);
}
break;
case 'u':
if (flying == true) {
drone.setVerticalSpeed(-0.2);
}
break;
case 'd':
if (flying == true) {
drone.setYawSpeed(0.4);
}
break;
case 'g':
if (flying == true) {
drone.setYawSpeed(-0.4);
}
break;
case 'i':
if (flying == true) {
drone.setForwardSpeed(0.2);
}
break;
case 'k':
if (flying == true) {
drone.setForwardSpeed(-0.2);
}
break;
case 'j':
if (flying == true) {
drone.setLateralSpeed(-0.2);
}
break;
case 'l':
if (flying == true) {
drone.setLateralSpeed(0.2);
}
break;
default:
if ((flying == true) && (currentTime - lastCommandTime > 1500.)) {
std::cout << "1.5 s without order, sending command : stop moving." << std::endl;
drone.stopMoving();
}
break;
}
}
else {
running = false;
}
return running;
}
int main(int argc, char **argv)
{
try {
std::string opt_connecting_info = "udp://192.168.30.111:14552";
for (int i = 1; i < argc; i++) {
if (std::string(argv[i]) == "--co" && i + 1 < argc) {
opt_connecting_info = std::string(argv[i + 1]);
i++;
}
else if (argc >= 2 && (std::string(argv[1]) == "--help" || std::string(argv[1]) == "-h")) {
std::cout << "\nUsage:\n"
<< " " << argv[0] << "[--co <connection information>] [--help] [-h]\n"
<< std::endl
<< "Description:\n"
<< " --co <connection information>\n"
<< " - UDP: udp://[host][:port]\n"
<< " - TCP: tcp://[host][:port]\n"
<< " - serial: serial://[path][:baudrate]\n"
<< " - Default: udp://192.168.30.111:14552).\n\n"
<< " For example, to connect to the simulator use URL: udp://:14552\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_SUCCESS;
}
}
std::cout << std::endl
<< "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 approximately 3 meters of free space on all sides." << std::endl
<< std::endl;
vpRobotMavsdk drone(opt_connecting_info);
if (drone.isRunning()) {
int k = 0;
bool running = true;
bool flying = false;
std::cout << "\nConfiguring drone settings ...\n" << std::endl;
drone.setTakeOffAlt(1.0);
std::cout << "\n| Control the drone with the keyboard :\n"
"| 't' to takeoff / 'l' to land / 'e' for emergency stop\n"
"| ('space','u','d','g') and ('i','k','j','l') to move\n"
"| 'q' to quit.\n"
<< std::endl;
while (running && drone.isRunning()) {
k = '0';
}
running = handleKeyboardInput(drone, k, flying, lastCommandTime);
}
std::cout << "\nQuitting ...\n" << std::endl;
}
else {
std::cout << "ERROR : failed to setup drone control." << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "\nCaught an exception: " << e << std::endl;
return EXIT_FAILURE;
}
}
#else
int main()
{
#ifndef VISP_HAVE_MAVSDK
std::cout << "\nThis example requires mavsdk library. You should install it, configure and rebuid ViSP.\n"
<< std::endl;
#endif
#if !((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
std::cout
<< "\nThis example requires at least cxx17. You should enable cxx17 during ViSP configuration with cmake and "
"rebuild ViSP.\n"
<< std::endl;
#endif
return EXIT_SUCCESS;
}
#endif
error that can be emitted by ViSP classes.
Keyboard management under unix (Linux or OSX). This class is not available under windows.
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()