Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testPixhawkDronePositionAbsoluteControl.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 * Simple example to demonstrate how to control in position using mavsdk
33 * a drone equipped with a Pixhawk connected to a Jetson TX2.
34 *
35*****************************************************************************/
36
46#include <iostream>
47
48#include <visp3/core/vpConfig.h>
49
50#if defined(VISP_HAVE_MAVSDK) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_17)
51
52#include <visp3/robot/vpRobotMavsdk.h>
53
54void usage(const std::string &bin_name)
55{
56 std::cerr << "Usage : " << bin_name << " <connection information>\n"
57 << "Connection URL format should be :\n"
58 << " - For TCP : tcp://[server_host][:server_port]\n"
59 << " - For UDP : udp://[bind_host][:bind_port]\n"
60 << " - For Serial : serial:///path/to/serial/dev[:baudrate]\n"
61 << "For example, to connect to the simulator use URL: udp://:14540\n";
62}
63
64int main(int argc, char **argv)
65{
66 if (argc != 2) {
67 usage(argv[0]);
68 return EXIT_SUCCESS;
69 }
70
71 double takeoff_alt = 1.;
72
73 auto drone = vpRobotMavsdk(argv[1]);
74 drone.setTakeOffAlt(takeoff_alt);
75 drone.setVerbose(true);
76
77 if (!drone.takeOff()) {
78 std::cout << "Takeoff failed" << std::endl;
79 return EXIT_FAILURE;
80 }
81
82 drone.takeControl(); // Start PX4 offboard
83
84 // Get position
85 float ned_north, ned_east, ned_down, ned_yaw;
86 drone.getPosition(ned_north, ned_east, ned_down, ned_yaw);
87 std::cout << "Vehicle position in NED frame: " << ned_north << " " << ned_east << " " << ned_down << " [m] and "
88 << vpMath::deg(ned_yaw) << " [deg]" << std::endl;
89
90 vpHomogeneousMatrix ned_M_frd;
91 drone.getPosition(ned_M_frd);
92 vpRxyzVector rxyz(ned_M_frd.getRotationMatrix());
93 std::cout << "Vehicle position in NED frame: " << ned_M_frd.getTranslationVector().t() << " [m] and "
94 << vpMath::deg(rxyz).t() << " [deg]" << std::endl;
95
96 // Set position in NED frame
97 drone.setPositioningIncertitude(0.10, vpMath::rad(5.));
98
99 drone.setPosition(0.0, 1.0, ned_down, 0.0); // East
100 drone.setPosition(1.0, 0.0, ned_down, 0.0); // North
101 drone.setPosition(0.0, -1.0, ned_down, 0.0); // West
102 drone.setPosition(-1.0, 0.0, ned_down, 0.0); // South
103
104 // Land drone
105 drone.land();
106
107 return EXIT_SUCCESS;
108}
109
110#else
111
112int main()
113{
114#ifndef VISP_HAVE_MAVSDK
115 std::cout << "\nThis example requires mavsdk library. You should install it, configure and rebuid ViSP.\n"
116 << std::endl;
117#endif
118#if !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_17)
119 std::cout
120 << "\nThis example requires at least cxx17. You should enable cxx17 during ViSP configuration with cmake and "
121 "rebuild ViSP.\n"
122 << std::endl;
123#endif
124 return EXIT_SUCCESS;
125}
126
127#endif // #if defined(VISP_HAVE_MAVSDK)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpRotationMatrix getRotationMatrix() const
vpTranslationVector getTranslationVector() const
static double rad(double deg)
Definition vpMath.h:116
static double deg(double rad)
Definition vpMath.h:106
vpColVector t() const
Implementation of a rotation vector as Euler angle minimal representation.