34#include <visp3/core/vpConfig.h>
37#if defined(VISP_HAVE_MAVSDK) && ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))) \
38 && defined(VISP_HAVE_THREADS)
44#include <mavsdk/mavsdk.h>
45#include <mavsdk/plugins/action/action.h>
46#include <mavsdk/plugins/calibration/calibration.h>
47#include <mavsdk/plugins/mavlink_passthrough/mavlink_passthrough.h>
48#include <mavsdk/plugins/mocap/mocap.h>
49#include <mavsdk/plugins/offboard/offboard.h>
50#include <mavsdk/plugins/telemetry/telemetry.h>
52#include <visp3/core/vpExponentialMap.h>
53#include <visp3/robot/vpRobotMavsdk.h>
55using std::chrono::milliseconds;
56using std::chrono::seconds;
57using std::this_thread::sleep_for;
58using namespace std::chrono_literals;
61#ifndef DOXYGEN_SHOULD_SKIP_THIS
62class vpRobotMavsdk::vpRobotMavsdkImpl
65 vpRobotMavsdkImpl() : m_takeoffAlt(1.0) { }
66 vpRobotMavsdkImpl(
const std::string &connection_info) : m_takeoffAlt(1.0) { connect(connection_info); }
68 virtual ~vpRobotMavsdkImpl()
70 if (m_has_flying_capability && m_auto_land) {
81 std::shared_ptr<mavsdk::System> getSystem(mavsdk::Mavsdk &mavsdk)
83 std::cout <<
"Waiting to discover system..." << std::endl;
84 auto prom = std::promise<std::shared_ptr<mavsdk::System> > {};
85 auto fut = prom.get_future();
89#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
90 mavsdk::Mavsdk::NewSystemHandle handle = mavsdk.subscribe_on_new_system([&mavsdk, &prom, &handle]() {
92 mavsdk.subscribe_on_new_system([&mavsdk, &prom]() {
94 auto system = mavsdk.systems().back();
96 if (system->has_autopilot()) {
97 std::cout <<
"Discovered autopilot" << std::endl;
100#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
101 mavsdk.unsubscribe_on_new_system(handle);
103 mavsdk.subscribe_on_new_system(
nullptr);
105 prom.set_value(system);
111 if (fut.wait_for(seconds(3)) == std::future_status::timeout) {
112 std::cerr <<
"No autopilot found." << std::endl;
120 MAV_TYPE getVehicleType()
122 auto passthrough = mavsdk::MavlinkPassthrough { m_system };
124 auto prom = std::promise<MAV_TYPE> {};
125 auto fut = prom.get_future();
126#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
127 mavsdk::MavlinkPassthrough::MessageHandle handle = passthrough.subscribe_message(
128 MAVLINK_MSG_ID_HEARTBEAT, [&passthrough, &prom, &handle](
const mavlink_message_t &message) {
130 passthrough.subscribe_message_async(MAVLINK_MSG_ID_HEARTBEAT,
131 [&passthrough, &prom](
const mavlink_message_t &message) {
134 if (message.compid != MAV_COMP_ID_AUTOPILOT1) {
138 mavlink_heartbeat_t heartbeat;
139 mavlink_msg_heartbeat_decode(&message, &heartbeat);
142#if (VISP_HAVE_MAVSDK_VERSION >= 0x020000)
143 passthrough.unsubscribe_message(MAVLINK_MSG_ID_HEARTBEAT, handle);
144#elif (VISP_HAVE_MAVSDK_VERSION > 0x010412)
145 passthrough.unsubscribe_message(handle);
147 passthrough.subscribe_message_async(MAVLINK_MSG_ID_HEARTBEAT,
nullptr);
150 prom.set_value(
static_cast<MAV_TYPE
>(heartbeat.type));
155 if (fut.wait_for(seconds(3)) == std::future_status::timeout) {
156 std::cerr <<
"No heartbeat received to get vehicle type." << std::endl;
164 void calibrate_accelerometer(mavsdk::Calibration &calibration)
166 std::cout <<
"Calibrating accelerometer..." << std::endl;
168 std::promise<void> calibration_promise;
169 auto calibration_future = calibration_promise.get_future();
171 calibration.calibrate_accelerometer_async(create_calibration_callback(calibration_promise));
173 calibration_future.wait();
176 std::function<void(mavsdk::Calibration::Result, mavsdk::Calibration::ProgressData)>
177 create_calibration_callback(std::promise<void> &calibration_promise)
179 return [&calibration_promise](
const mavsdk::Calibration::Result result,
180 const mavsdk::Calibration::ProgressData progress_data) {
182 case mavsdk::Calibration::Result::Success:
183 std::cout <<
"--- Calibration succeeded!" << std::endl;
184 calibration_promise.set_value();
186 case mavsdk::Calibration::Result::Next:
187 if (progress_data.has_progress) {
188 std::cout <<
" Progress: " << progress_data.progress << std::endl;
190 if (progress_data.has_status_text) {
191 std::cout <<
" Instruction: " << progress_data.status_text << std::endl;
195 std::cout <<
"--- Calibration failed with message: " << result << std::endl;
196 calibration_promise.set_value();
202 void calibrate_gyro(mavsdk::Calibration &calibration)
204 std::cout <<
"Calibrating gyro..." << std::endl;
206 std::promise<void> calibration_promise;
207 auto calibration_future = calibration_promise.get_future();
209 calibration.calibrate_gyro_async(create_calibration_callback(calibration_promise));
211 calibration_future.wait();
215 void connect(
const std::string &connectionInfo)
217 m_address = connectionInfo;
218 mavsdk::ConnectionResult connection_result = m_mavsdk.add_any_connection(connectionInfo);
220 if (connection_result != mavsdk::ConnectionResult::Success) {
221 std::cerr <<
"Connection failed: " << connection_result << std::endl;
225 m_system = getSystem(m_mavsdk);
231 m_mav_type = getVehicleType();
233 m_has_flying_capability = hasFlyingCapability(m_mav_type);
235 std::cout << (m_has_flying_capability ?
"Connected to a flying vehicle" :
"Connected to a non flying vehicle")
238 m_action = std::make_shared<mavsdk::Action>(m_system);
239 m_telemetry = std::make_shared<mavsdk::Telemetry>(m_system);
240 m_offboard = std::make_shared<mavsdk::Offboard>(m_system);
243 bool hasFlyingCapability(MAV_TYPE mav_type)
246 case MAV_TYPE::MAV_TYPE_GROUND_ROVER:
247 case MAV_TYPE::MAV_TYPE_SURFACE_BOAT:
248 case MAV_TYPE::MAV_TYPE_SUBMARINE:
255 bool isRunning()
const
257 if (m_system ==
nullptr) {
265 std::string getAddress()
const
267 std::string sequence;
268 std::stringstream ss(m_address);
269 std::string actual_address;
270 std::getline(ss, sequence,
':');
271 if (sequence ==
"serial" || sequence ==
"udp" || sequence ==
"tcp") {
272 getline(ss, sequence,
':');
273 for (
const char &c : sequence) {
275 actual_address.append(1, c);
278 return actual_address;
281 std::cout <<
"ERROR : The address parameter must start with \"serial:\" or \"udp:\" or \"tcp:\"." << std::endl;
282 return std::string();
286 float getBatteryLevel()
const
288 mavsdk::Telemetry::Battery battery = m_telemetry.get()->battery();
289 return battery.voltage_v;
292 void getPosition(vpHomogeneousMatrix &ned_M_frd)
const
294 auto quat = m_telemetry.get()->attitude_quaternion();
295 auto posvel = m_telemetry.get()->position_velocity_ned();
296 vpQuaternionVector q { quat.x, quat.y, quat.z, quat.w };
297 vpTranslationVector
t { posvel.position.north_m, posvel.position.east_m, posvel.position.down_m };
301 void getPosition(
float &ned_north,
float &ned_east,
float &ned_down,
float &ned_yaw)
const
303 auto odom = m_telemetry.get()->odometry();
304 auto angles = m_telemetry.get()->attitude_euler();
305 ned_north = odom.position_body.x_m;
306 ned_east = odom.position_body.y_m;
307 ned_down = odom.position_body.z_m;
311 std::tuple<float, float> getHome()
const
313 auto position = m_telemetry.get()->home();
314 return { float(position.latitude_deg), float(position.longitude_deg) };
317 bool sendMocapData(
const vpHomogeneousMatrix &enu_M_flu,
int display_fps)
323 vpHomogeneousMatrix flu_M_frd;
325 flu_M_frd[1][1] = -1;
326 flu_M_frd[2][2] = -1;
328 vpHomogeneousMatrix enu_M_frd = enu_M_flu * flu_M_frd;
329 auto mocap = mavsdk::Mocap { m_system };
330 mavsdk::Mocap::VisionPositionEstimate pose_estimate;
334 pose_estimate.angle_body.roll_rad = ned_rxyz_frd[0];
335 pose_estimate.angle_body.pitch_rad = ned_rxyz_frd[1];
336 pose_estimate.angle_body.yaw_rad = ned_rxyz_frd[2];
339 pose_estimate.position_body.x_m = ned_t_frd[0];
340 pose_estimate.position_body.y_m = ned_t_frd[1];
341 pose_estimate.position_body.z_m = ned_t_frd[2];
343 pose_estimate.pose_covariance.covariance_matrix.push_back(NAN);
344 pose_estimate.time_usec = 0;
346 const mavsdk::Mocap::Result set_position_result = mocap.set_vision_position_estimate(pose_estimate);
347 if (set_position_result != mavsdk::Mocap::Result::Success) {
348 std::cerr <<
"Set position failed: " << set_position_result <<
'\n';
352 if (display_fps > 0) {
353 double display_time_ms = 1000. / display_fps;
356 std::cout <<
"Send ned_M_frd MoCap data: " << std::endl;
357 std::cout <<
"Translation [m]: " << pose_estimate.position_body.x_m <<
" , "
358 << pose_estimate.position_body.y_m <<
" , " << pose_estimate.position_body.z_m << std::endl;
359 std::cout <<
"Roll [rad]: " << pose_estimate.angle_body.roll_rad
360 <<
" , Pitch [rad]: " << pose_estimate.angle_body.pitch_rad
361 <<
" , Yaw [rad]: " << pose_estimate.angle_body.yaw_rad <<
" ." << std::endl;
368 void setTakeOffAlt(
double altitude)
371 m_takeoffAlt = altitude;
374 std::cerr <<
"ERROR : The take off altitude must be positive." << std::endl;
381 auto calibration = mavsdk::Calibration(m_system);
384 calibrate_accelerometer(calibration);
385 calibrate_gyro(calibration);
391 std::cout <<
"Arming...\n";
392 const mavsdk::Action::Result arm_result = m_action.get()->arm();
394 if (arm_result != mavsdk::Action::Result::Success) {
395 std::cerr <<
"Arming failed: " << arm_result << std::endl;
404 std::cout <<
"Disarming...\n";
405 const mavsdk::Action::Result arm_result = m_action.get()->disarm();
407 if (arm_result != mavsdk::Action::Result::Success) {
408 std::cerr <<
"Disarming failed: " << arm_result << std::endl;
414 bool setGPSGlobalOrigin(
double latitude,
double longitude,
double altitude)
416 auto passthrough = mavsdk::MavlinkPassthrough { m_system };
417#if (VISP_HAVE_MAVSDK_VERSION >= 0x020000)
418 passthrough.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
420 mavlink_message_t message;
421 mavlink_set_gps_global_origin_t gps_global_origin;
422 gps_global_origin.latitude = latitude * 1E7;
423 gps_global_origin.longitude = longitude * 1E7;
424 gps_global_origin.altitude = altitude * 1000;
425 gps_global_origin.target_system = m_system->get_system_id();
426 gps_global_origin.time_usec = 0;
427 mavlink_msg_set_gps_global_origin_encode(
428 mavlink_address.system_id,
429 mavlink_address.component_id,
435 mavlink_set_gps_global_origin_t gps_global_origin;
436 gps_global_origin.latitude = latitude * 1E7;
437 gps_global_origin.longitude = longitude * 1E7;
438 gps_global_origin.altitude = altitude * 1000;
439 gps_global_origin.target_system = m_system->get_system_id();
440 mavlink_message_t msg;
441 mavlink_msg_set_gps_global_origin_encode(passthrough.get_our_sysid(), passthrough.get_our_compid(), &msg,
443 auto resp = passthrough.send_message(msg);
444 if (resp != mavsdk::MavlinkPassthrough::Result::Success) {
445 std::cerr <<
"Set GPS global position failed: " << resp << std::endl;
455 std::cout <<
"Starting offboard mode..." << std::endl;
458 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
459 const mavsdk::Offboard::VelocityBodyYawspeed stay {};
460 m_offboard.get()->set_velocity_body(stay);
462 mavsdk::Offboard::Result offboard_result = m_offboard.get()->start();
463 if (offboard_result != mavsdk::Offboard::Result::Success) {
464 std::cerr <<
"Offboard mode failed: " << offboard_result << std::endl;
468 else if (m_verbose) {
469 std::cout <<
"Already in offboard mode" << std::endl;
474 while (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
476 std::cout <<
"Time out received in takeControl()" << std::endl;
482 std::cout <<
"Offboard mode started" << std::endl;
487 void setPositioningIncertitude(
float position_incertitude,
float yaw_incertitude)
489 m_position_incertitude = position_incertitude;
490 m_yaw_incertitude = yaw_incertitude;
493 bool takeOff(
bool interactive,
int timeout_sec,
bool use_gps)
495 if (!m_has_flying_capability) {
496 std::cerr <<
"Warning: Cannot takeoff this non flying vehicle" << std::endl;
500 bool authorize_takeoff =
false;
503 authorize_takeoff =
true;
506 if (m_telemetry.get()->flight_mode() == mavsdk::Telemetry::FlightMode::Offboard) {
507 authorize_takeoff =
true;
511 while (answer !=
"Y" && answer !=
"y" && answer !=
"N" && answer !=
"n") {
512 std::cout <<
"Current flight mode is not the offboard mode. Do you "
513 "want to force offboard mode ? (y/n)"
516 if (answer ==
"Y" || answer ==
"y") {
517 authorize_takeoff =
true;
523 if (m_telemetry.get()->in_air()) {
524 std::cerr <<
"Cannot take off as the robot is already flying." << std::endl;
527 else if (authorize_takeoff) {
537 while (answer !=
"Y" && answer !=
"y" && answer !=
"N" && answer !=
"n") {
538 std::cout <<
"If vehicle armed ? (y/n)" << std::endl;
540 if (answer ==
"N" || answer ==
"n") {
549 if (m_telemetry.get()->gps_info().fix_type == mavsdk::Telemetry::FixType::NoGps || !use_gps) {
558 auto in_air_promise = std::promise<void> {};
559 auto in_air_future = in_air_promise.get_future();
561 mavsdk::Telemetry::Odometry odom = m_telemetry.get()->odometry();
562 vpQuaternionVector q { odom.q.x, odom.q.y, odom.q.z, odom.q.w };
563 vpRotationMatrix
R(q);
564 vpRxyzVector rxyz(R);
566 double X_init = odom.position_body.x_m;
567 double Y_init = odom.position_body.y_m;
568 double Z_init = odom.position_body.z_m;
571 std::cout <<
"Takeoff using position NED." << std::endl;
573 mavsdk::Offboard::PositionNedYaw takeoff {};
574 takeoff.north_m = X_init;
575 takeoff.east_m = Y_init;
576 takeoff.down_m = Z_init - m_takeoffAlt;
577 takeoff.yaw_deg = yaw_init;
578 m_offboard.get()->set_position_ned(takeoff);
581#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
582 mavsdk::Telemetry::LandedStateHandle handle = m_telemetry.get()->subscribe_landed_state(
583 [
this, &in_air_promise, &handle](mavsdk::Telemetry::LandedState state) {
584 if (state == mavsdk::Telemetry::LandedState::InAir) {
585 std::cout <<
"Drone is taking off\n.";
586 m_telemetry.get()->unsubscribe_landed_state(handle);
587 in_air_promise.set_value();
591 m_telemetry.get()->subscribe_landed_state([
this, &in_air_promise](mavsdk::Telemetry::LandedState state) {
592 if (state == mavsdk::Telemetry::LandedState::InAir) {
593 std::cout <<
"Drone is taking off\n.";
594 m_telemetry.get()->subscribe_landed_state(
nullptr);
595 in_air_promise.set_value();
597 std::cout <<
"state: " << state << std::endl;
600 if (in_air_future.wait_for(seconds(timeout_sec)) == std::future_status::timeout) {
601 std::cerr <<
"Takeoff failed: drone not in air.\n";
602#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
603 m_telemetry.get()->unsubscribe_landed_state(handle);
605 m_telemetry.get()->subscribe_landed_state(
nullptr);
610 auto takeoff_finished_promise = std::promise<void> {};
611 auto takeoff_finished_future = takeoff_finished_promise.get_future();
613#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
614 mavsdk::Telemetry::OdometryHandle handle_odom = m_telemetry.get()->subscribe_odometry(
615 [
this, &takeoff_finished_promise, &handle, &Z_init, &handle_odom](mavsdk::Telemetry::Odometry odom) {
616 if (odom.position_body.z_m < 0.90 * (Z_init - m_takeoffAlt) + m_position_incertitude) {
617 std::cout <<
"Takeoff altitude reached\n.";
618 m_telemetry.get()->unsubscribe_odometry(handle_odom);
619 takeoff_finished_promise.set_value();
623 m_telemetry.get()->subscribe_odometry(
624 [
this, &takeoff_finished_promise, &Z_init](mavsdk::Telemetry::Odometry odom) {
625 if (odom.position_body.z_m < 0.90 * (Z_init - m_takeoffAlt) + m_position_incertitude) {
626 std::cout <<
"Takeoff altitude reached\n.";
627 m_telemetry.get()->subscribe_odometry(
nullptr);
628 takeoff_finished_promise.set_value();
632 if (takeoff_finished_future.wait_for(seconds(timeout_sec)) == std::future_status::timeout) {
633 std::cerr <<
"Takeoff failed: altitude not reached.\n";
634#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
635 m_telemetry.get()->unsubscribe_odometry(handle_odom);
637 m_telemetry.get()->subscribe_odometry(
nullptr);
644 mavsdk::Telemetry::Odometry odom = m_telemetry.get()->odometry();
645 double Z_init = odom.position_body.z_m;
647 m_action.get()->set_takeoff_altitude(m_takeoffAlt);
648 const auto takeoff_result = m_action.get()->takeoff();
649 if (takeoff_result != mavsdk::Action::Result::Success) {
650 std::cerr <<
"Takeoff failed: " << takeoff_result <<
'\n';
654 auto in_air_promise = std::promise<void> {};
655 auto in_air_future = in_air_promise.get_future();
656#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
657 mavsdk::Telemetry::LandedStateHandle handle = m_telemetry.get()->subscribe_landed_state(
658 [
this, &in_air_promise, &handle](mavsdk::Telemetry::LandedState state) {
659 if (state == mavsdk::Telemetry::LandedState::InAir) {
660 std::cout <<
"Taking off has finished\n.";
661 m_telemetry.get()->unsubscribe_landed_state(handle);
662 in_air_promise.set_value();
666 m_telemetry.get()->subscribe_landed_state([
this, &in_air_promise](mavsdk::Telemetry::LandedState state) {
667 if (state == mavsdk::Telemetry::LandedState::InAir) {
668 std::cout <<
"Taking off has finished\n.";
669 m_telemetry.get()->subscribe_landed_state(
nullptr);
670 in_air_promise.set_value();
672 std::cout <<
"state: " << state << std::endl;
675 if (in_air_future.wait_for(seconds(timeout_sec)) == std::future_status::timeout) {
677 std::cerr <<
"Takeoff timed out.\n";
678#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
679 m_telemetry.get()->unsubscribe_landed_state(handle);
681 m_telemetry.get()->subscribe_landed_state(
nullptr);
685 auto takeoff_finished_promise = std::promise<void> {};
686 auto takeoff_finished_future = takeoff_finished_promise.get_future();
688#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
689 mavsdk::Telemetry::OdometryHandle handle_odom = m_telemetry.get()->subscribe_odometry(
690 [
this, &takeoff_finished_promise, &handle, &Z_init, &handle_odom](mavsdk::Telemetry::Odometry odom) {
691 if (odom.position_body.z_m < 0.90 * (Z_init - m_takeoffAlt) + m_position_incertitude) {
692 std::cout <<
"Takeoff altitude reached\n.";
693 m_telemetry.get()->unsubscribe_odometry(handle_odom);
694 takeoff_finished_promise.set_value();
698 m_telemetry.get()->subscribe_odometry(
699 [
this, &takeoff_finished_promise, &Z_init](mavsdk::Telemetry::Odometry odom) {
700 if (odom.position_body.z_m < 0.90 * (Z_init - m_takeoffAlt) + m_position_incertitude) {
701 std::cout <<
"Takeoff altitude reached\n.";
702 m_telemetry.get()->subscribe_odometry(
nullptr);
703 takeoff_finished_promise.set_value();
707 if (takeoff_finished_future.wait_for(seconds(timeout_sec)) == std::future_status::timeout) {
708 std::cerr <<
"Takeoff failed: altitude not reached.\n";
709#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
710 m_telemetry.get()->unsubscribe_odometry(handle_odom);
712 m_telemetry.get()->subscribe_odometry(
nullptr);
723 bool land(
bool use_buildin =
false)
725 if (!m_has_flying_capability) {
726 std::cerr <<
"Warning: Cannot land this non flying vehicle" << std::endl;
739 mavsdk::Telemetry::Odometry odom = m_telemetry.get()->odometry();
740 vpQuaternionVector q { odom.q.x, odom.q.y, odom.q.z, odom.q.w };
741 vpRotationMatrix
R(q);
742 vpRxyzVector rxyz(R);
744 double X_init = odom.position_body.x_m;
745 double Y_init = odom.position_body.y_m;
748 std::cout <<
"Landing using position NED." << std::endl;
750 mavsdk::Offboard::PositionNedYaw landing {};
751 landing.north_m = X_init;
752 landing.east_m = Y_init;
754 landing.yaw_deg = yaw_init;
755 m_offboard.get()->set_position_ned(landing);
757 bool success =
false;
760 auto landing_finished_promise = std::promise<void> {};
761 auto landing_finished_future = landing_finished_promise.get_future();
763#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
764 mavsdk::Telemetry::OdometryHandle handle_odom = m_telemetry.get()->subscribe_odometry(
765 [
this, &landing_finished_promise, &success, &handle_odom](mavsdk::Telemetry::Odometry odom) {
766 if (odom.position_body.z_m > -0.15) {
767 std::cout <<
"Landing altitude reached \n.";
770 m_telemetry.get()->unsubscribe_odometry(handle_odom);
771 landing_finished_promise.set_value();
775 m_telemetry.get()->subscribe_odometry(
776 [
this, &landing_finished_promise, &success](mavsdk::Telemetry::Odometry odom) {
777 if (odom.position_body.z_m > -0.15) {
778 std::cout <<
"Landing altitude reached\n.";
781 m_telemetry.get()->subscribe_odometry(
nullptr);
782 landing_finished_promise.set_value();
786 if (landing_finished_future.wait_for(seconds(10)) == std::future_status::timeout) {
787 std::cerr <<
"failed: altitude not reached.\n";
792 std::cout <<
"Descending\n.";
797 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Land) {
798 std::cout <<
"Landing...\n";
799 const mavsdk::Action::Result land_result = m_action.get()->land();
800 if (land_result != mavsdk::Action::Result::Success) {
801 std::cerr <<
"Land failed: " << land_result << std::endl;
806 while (m_telemetry.get()->in_air()) {
807 std::cout <<
"Vehicle is landing..." << std::endl;
808 sleep_for(seconds(1));
812 std::cout <<
"Landed!" << std::endl;
815 sleep_for(seconds(5));
816 std::cout <<
"Finished..." << std::endl;
820 bool setPosition(
float ned_north,
float ned_east,
float ned_down,
float ned_yaw,
bool blocking,
int timeout_sec)
822 mavsdk::Offboard::PositionNedYaw position_target {};
824 position_target.north_m = ned_north;
825 position_target.east_m = ned_east;
826 position_target.down_m = ned_down;
829 std::cout <<
"NED Pos to reach: " << position_target.north_m <<
" " << position_target.east_m <<
" "
830 << position_target.down_m <<
" " << position_target.yaw_deg << std::endl;
831 m_offboard.get()->set_position_ned(position_target);
833 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
835 std::cout <<
"Cannot set vehicle position: offboard mode not started" << std::endl;
842 auto position_reached_promise = std::promise<void> {};
843 auto position_reached_future = position_reached_promise.get_future();
845#if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
846 mavsdk::Telemetry::OdometryHandle handle_odom = m_telemetry.get()->subscribe_odometry(
847 [
this, &position_reached_promise, &handle_odom, &position_target](mavsdk::Telemetry::Odometry odom) {
848 vpQuaternionVector q { odom.q.x, odom.q.y, odom.q.z, odom.q.w };
849 vpRotationMatrix
R(q);
850 vpRxyzVector rxyz(R);
852 double distance_to_target = std::sqrt(
vpMath::sqr(odom.position_body.x_m - position_target.north_m) +
853 vpMath::sqr(odom.position_body.y_m - position_target.east_m) +
854 vpMath::sqr(odom.position_body.z_m - position_target.down_m));
855 if (distance_to_target < m_position_incertitude &&
856 std::fabs(odom_yaw - position_target.yaw_deg) < m_yaw_incertitude) {
857 std::cout <<
"Position reached\n.";
858 m_telemetry.get()->unsubscribe_odometry(handle_odom);
859 position_reached_promise.set_value();
863 m_telemetry.get()->subscribe_odometry(
864 [
this, &position_reached_promise, &position_target](mavsdk::Telemetry::Odometry odom) {
865 vpQuaternionVector q { odom.q.x, odom.q.y, odom.q.z, odom.q.w };
866 vpRotationMatrix
R(q);
867 vpRxyzVector rxyz(R);
869 double distance_to_target = std::sqrt(
vpMath::sqr(odom.position_body.x_m - position_target.north_m) +
870 vpMath::sqr(odom.position_body.y_m - position_target.east_m) +
871 vpMath::sqr(odom.position_body.z_m - position_target.down_m));
872 if (distance_to_target < m_position_incertitude &&
873 std::fabs(odom_yaw - position_target.yaw_deg) < m_yaw_incertitude) {
874 std::cout <<
"Position reached\n.";
875 m_telemetry.get()->subscribe_odometry(
nullptr);
876 position_reached_promise.set_value();
880 if (position_reached_future.wait_for(seconds(timeout_sec)) == std::future_status::timeout) {
881 std::cerr <<
"Positioning failed: position not reached.\n";
886 std::cout <<
"---- DEBUG timeout: " << timeout_sec << std::endl;
890 bool setPositionRelative(
float ned_delta_north,
float ned_delta_east,
float ned_delta_down,
float ned_delta_yaw,
891 bool blocking,
int timeout_sec)
893 mavsdk::Telemetry::Odometry odom;
894 mavsdk::Telemetry::EulerAngle angles;
895 mavsdk::Offboard::PositionNedYaw position_target {};
897 position_target.north_m = ned_delta_north;
898 position_target.east_m = ned_delta_east;
899 position_target.down_m = ned_delta_down;
900 position_target.yaw_deg =
vpMath::deg(ned_delta_yaw);
903 odom = m_telemetry.get()->odometry();
904 angles = m_telemetry.get()->attitude_euler();
906 position_target.north_m += odom.position_body.x_m;
907 position_target.east_m += odom.position_body.y_m;
908 position_target.down_m += odom.position_body.z_m;
909 position_target.yaw_deg += angles.yaw_deg;
911 return setPosition(position_target.north_m, position_target.east_m, position_target.down_m,
912 vpMath::rad(position_target.yaw_deg), blocking, timeout_sec);
915 bool setPosition(
const vpHomogeneousMatrix &M,
bool absolute,
int timeout_sec)
918 if (XYZvec[0] != 0.0) {
919 std::cerr <<
"ERROR : Can't move, rotation around X axis should be 0." << std::endl;
922 if (XYZvec[1] != 0.0) {
923 std::cerr <<
"ERROR : Can't move, rotation around Y axis should be 0." << std::endl;
927 absolute, timeout_sec);
930 bool setPositionRelative(
const vpHomogeneousMatrix &M,
bool blocking,
int timeout_sec)
933 if (XYZvec[0] != 0.0) {
934 std::cerr <<
"ERROR : Can't move, rotation around X axis should be 0." << std::endl;
937 if (XYZvec[1] != 0.0) {
938 std::cerr <<
"ERROR : Can't move, rotation around Y axis should be 0." << std::endl;
942 XYZvec[2], blocking, timeout_sec);
945 bool setVelocity(
const vpColVector &frd_vel_cmd)
947 if (frd_vel_cmd.
size() != 4) {
949 "ERROR : Can't set velocity, dimension of the velocity vector %d should be equal to 4.",
950 frd_vel_cmd.
size()));
953 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
955 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
959 mavsdk::Offboard::VelocityBodyYawspeed velocity_comm {};
960 velocity_comm.forward_m_s = frd_vel_cmd[0];
961 velocity_comm.right_m_s = frd_vel_cmd[1];
962 velocity_comm.down_m_s = frd_vel_cmd[2];
963 velocity_comm.yawspeed_deg_s =
vpMath::deg(frd_vel_cmd[3]);
964 m_offboard.get()->set_velocity_body(velocity_comm);
971 const mavsdk::Action::Result kill_result = m_action.get()->kill();
972 if (kill_result != mavsdk::Action::Result::Success) {
973 std::cerr <<
"Kill failed: " << kill_result << std::endl;
981 if (m_telemetry.get()->in_air()) {
982 if (m_telemetry.get()->gps_info().fix_type != mavsdk::Telemetry::FixType::NoGps) {
984 const mavsdk::Action::Result hold_result = m_action.get()->hold();
985 if (hold_result != mavsdk::Action::Result::Success) {
986 std::cerr <<
"Hold failed: " << hold_result << std::endl;
991 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
993 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
998 setPositionRelative(0., 0., 0., 0.,
false, 10.);
1006 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
1008 std::cout <<
"Cannot stop moving: offboard mode not started" << std::endl;
1013 const mavsdk::Offboard::VelocityBodyYawspeed stay {};
1014 m_offboard.get()->set_velocity_body(stay);
1019 bool releaseControl()
1021 auto offboard_result = m_offboard.get()->stop();
1022 if (offboard_result != mavsdk::Offboard::Result::Success) {
1023 std::cerr <<
"Offboard stop failed: " << offboard_result <<
'\n';
1026 std::cout <<
"Offboard stopped\n";
1030 void setAutoLand(
bool auto_land) { m_auto_land = auto_land; }
1032 bool setYawSpeed(
double body_frd_wz)
1034 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
1036 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
1040 mavsdk::Offboard::VelocityBodyYawspeed velocity_comm {};
1041 velocity_comm.forward_m_s = 0.0;
1042 velocity_comm.right_m_s = 0.0;
1043 velocity_comm.down_m_s = 0.0;
1044 velocity_comm.yawspeed_deg_s =
vpMath::deg(body_frd_wz);
1045 m_offboard.get()->set_velocity_body(velocity_comm);
1050 bool setForwardSpeed(
double body_frd_vx)
1052 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
1054 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
1059 mavsdk::Offboard::VelocityBodyYawspeed velocity_comm {};
1060 velocity_comm.forward_m_s = body_frd_vx;
1061 velocity_comm.right_m_s = 0.0;
1062 velocity_comm.down_m_s = 0.0;
1063 velocity_comm.yawspeed_deg_s = 0.0;
1064 m_offboard.get()->set_velocity_body(velocity_comm);
1069 bool setLateralSpeed(
double body_frd_vy)
1071 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
1073 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
1077 mavsdk::Offboard::VelocityBodyYawspeed velocity_comm {};
1078 velocity_comm.forward_m_s = 0.0;
1079 velocity_comm.right_m_s = body_frd_vy;
1080 velocity_comm.down_m_s = 0.0;
1081 velocity_comm.yawspeed_deg_s = 0.0;
1082 m_offboard.get()->set_velocity_body(velocity_comm);
1087 bool setVerticalSpeed(
double body_frd_vz)
1089 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
1091 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
1095 mavsdk::Offboard::VelocityBodyYawspeed velocity_comm {};
1096 velocity_comm.forward_m_s = 0.0;
1097 velocity_comm.right_m_s = 0.0;
1098 velocity_comm.down_m_s = body_frd_vz;
1099 velocity_comm.yawspeed_deg_s = 0.0;
1100 m_offboard.get()->set_velocity_body(velocity_comm);
1105 bool getFlyingCapability() {
return m_has_flying_capability; }
1107 void setVerbose(
bool verbose) { m_verbose = verbose; }
1111 std::string m_address {};
1112#if (VISP_HAVE_MAVSDK_VERSION >= 0x020000)
1113 mavsdk::Mavsdk m_mavsdk { mavsdk::Mavsdk::Configuration{mavsdk::ComponentType::GroundStation} };
1115 mavsdk::Mavsdk m_mavsdk { };
1117 std::shared_ptr<mavsdk::System> m_system;
1118 std::shared_ptr<mavsdk::Action> m_action;
1119 std::shared_ptr<mavsdk::Telemetry> m_telemetry;
1120 std::shared_ptr<mavsdk::Offboard> m_offboard;
1122 double m_takeoffAlt { 1.0 };
1124 MAV_TYPE m_mav_type {};
1125 bool m_has_flying_capability {
false };
1127 float m_position_incertitude { 0.05 };
1128 float m_yaw_incertitude { 0.09 };
1129 bool m_verbose {
false };
1130 bool m_auto_land {
true };
1172 m_impl->setPositioningIncertitude(0.05,
vpMath::rad(5));
1191 m_impl->setPositioningIncertitude(0.05,
vpMath::rad(5));
1244 return m_impl->sendMocapData(enu_M_flu, display_fps);
1277 m_impl->getPosition(ned_north, ned_east, ned_down, ned_yaw);
1332 return m_impl->takeOff(interactive, timeout_sec, use_gps);
1351 m_impl->setTakeOffAlt(takeoff_altitude);
1352 return m_impl->takeOff(interactive, timeout_sec, use_gps);
1397 return m_impl->setPosition(ned_north, ned_east, ned_down, ned_yaw, blocking, timeout_sec);
1418 return m_impl->setPosition(ned_M_frd, blocking, timeout_sec);
1436 float ned_delta_yaw,
bool blocking,
int timeout_sec)
1438 return m_impl->setPositionRelative(ned_delta_north, ned_delta_east, ned_delta_down, ned_delta_yaw, blocking,
1459 return m_impl->setPositionRelative(delta_frd_M_frd, blocking, timeout_sec);
1529 return m_impl->setGPSGlobalOrigin(latitude, longitude, altitude);
1574 m_impl->setPositioningIncertitude(position_incertitude, yaw_incertitude);
1603#ifdef ENABLE_VISP_NAMESPACE
1606#elif !defined(VISP_BUILD_SHARED_LIBS)
1608void dummy_vpRobotMavsdk() { }
unsigned int size() const
Return the number of elements of the 2D array.
Implementation of column vector and the associated operations.
@ dimensionError
Bad dimension.
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpRotationMatrix getRotationMatrix() const
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpTranslationVector getTranslationVector() const
static double rad(double deg)
static double sqr(double x)
static vpHomogeneousMatrix enu2ned(const vpHomogeneousMatrix &enu_M)
static double deg(double rad)
float getBatteryLevel() const
std::string getAddress() const
void setPositioningIncertitude(float position_incertitude, float yaw_incertitude)
std::tuple< float, float > getHome() const
bool sendMocapData(const vpHomogeneousMatrix &enu_M_flu, int display_fps=1)
bool takeOff(bool interactive=true, int timeout_sec=10, bool use_gps=false)
void getPosition(float &ned_north, float &ned_east, float &ned_down, float &ned_yaw) const
bool setLateralSpeed(double body_frd_vy)
bool setPositionRelative(float ned_delta_north, float ned_delta_east, float ned_delta_down, float ned_delta_yaw, bool blocking=true, int timeout_sec=10)
bool setVerticalSpeed(double body_frd_vz)
void setAutoLand(bool auto_land)
bool setForwardSpeed(double body_frd_vx)
void setTakeOffAlt(double altitude)
bool hasFlyingCapability()
void setVerbose(bool verbose)
bool setVelocity(const vpColVector &frd_vel_cmd)
void connect(const std::string &connection_info)
bool setGPSGlobalOrigin(double latitude, double longitude, double altitude)
bool setPosition(float ned_north, float ned_east, float ned_down, float ned_yaw, bool blocking=true, int timeout_sec=10)
bool setYawSpeed(double body_frd_wz)
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)