36 #include <visp3/core/vpConfig.h>
39 #if defined(VISP_HAVE_MAVSDK) && ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))) \
40 && defined(VISP_HAVE_THREADS)
46 #include <mavsdk/mavsdk.h>
47 #include <mavsdk/plugins/action/action.h>
48 #include <mavsdk/plugins/calibration/calibration.h>
49 #include <mavsdk/plugins/mavlink_passthrough/mavlink_passthrough.h>
50 #include <mavsdk/plugins/mocap/mocap.h>
51 #include <mavsdk/plugins/offboard/offboard.h>
52 #include <mavsdk/plugins/telemetry/telemetry.h>
54 #include <visp3/core/vpExponentialMap.h>
55 #include <visp3/robot/vpRobotMavsdk.h>
57 using std::chrono::milliseconds;
58 using std::chrono::seconds;
59 using std::this_thread::sleep_for;
60 using namespace std::chrono_literals;
63 #ifndef DOXYGEN_SHOULD_SKIP_THIS
64 class vpRobotMavsdk::vpRobotMavsdkImpl
67 vpRobotMavsdkImpl() : m_takeoffAlt(1.0) { }
68 vpRobotMavsdkImpl(
const std::string &connection_info) : m_takeoffAlt(1.0) { connect(connection_info); }
70 virtual ~vpRobotMavsdkImpl()
72 if (m_has_flying_capability && m_auto_land) {
83 std::shared_ptr<mavsdk::System> getSystem(mavsdk::Mavsdk &mavsdk)
85 std::cout <<
"Waiting to discover system..." << std::endl;
86 auto prom = std::promise<std::shared_ptr<mavsdk::System> > {};
87 auto fut = prom.get_future();
91 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
92 mavsdk::Mavsdk::NewSystemHandle handle = mavsdk.subscribe_on_new_system([&mavsdk, &prom, &handle]() {
94 mavsdk.subscribe_on_new_system([&mavsdk, &prom]() {
96 auto system = mavsdk.systems().back();
98 if (system->has_autopilot()) {
99 std::cout <<
"Discovered autopilot" << std::endl;
102 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
103 mavsdk.unsubscribe_on_new_system(handle);
105 mavsdk.subscribe_on_new_system(nullptr);
107 prom.set_value(system);
113 if (fut.wait_for(seconds(3)) == std::future_status::timeout) {
114 std::cerr <<
"No autopilot found." << std::endl;
122 MAV_TYPE getVehicleType()
124 auto passthrough = mavsdk::MavlinkPassthrough { m_system };
126 auto prom = std::promise<MAV_TYPE> {};
127 auto fut = prom.get_future();
128 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
129 mavsdk::MavlinkPassthrough::MessageHandle handle = passthrough.subscribe_message(
130 MAVLINK_MSG_ID_HEARTBEAT, [&passthrough, &prom, &handle](
const mavlink_message_t &message) {
132 passthrough.subscribe_message_async(MAVLINK_MSG_ID_HEARTBEAT,
133 [&passthrough, &prom](
const mavlink_message_t &message) {
136 if (message.compid != MAV_COMP_ID_AUTOPILOT1) {
140 mavlink_heartbeat_t heartbeat;
141 mavlink_msg_heartbeat_decode(&message, &heartbeat);
144 #if (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;
294 auto quat = m_telemetry.get()->attitude_quaternion();
295 auto posvel = m_telemetry.get()->position_velocity_ned();
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) };
325 flu_M_frd[1][1] = -1;
326 flu_M_frd[2][2] = -1;
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 mavlink_set_gps_global_origin_t gps_global_origin;
418 gps_global_origin.latitude = latitude * 10000000;
419 gps_global_origin.longitude = longitude * 10000000;
420 gps_global_origin.altitude = altitude * 1000;
421 gps_global_origin.target_system = m_system->get_system_id();
422 mavlink_message_t msg;
423 mavlink_msg_set_gps_global_origin_encode(passthrough.get_our_sysid(), passthrough.get_our_compid(), &msg,
425 auto resp = passthrough.send_message(msg);
426 if (resp != mavsdk::MavlinkPassthrough::Result::Success) {
427 std::cerr <<
"Set GPS global position failed: " << resp << std::endl;
436 std::cout <<
"Starting offboard mode..." << std::endl;
439 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
440 const mavsdk::Offboard::VelocityBodyYawspeed stay {};
441 m_offboard.get()->set_velocity_body(stay);
443 mavsdk::Offboard::Result offboard_result = m_offboard.get()->start();
444 if (offboard_result != mavsdk::Offboard::Result::Success) {
445 std::cerr <<
"Offboard mode failed: " << offboard_result << std::endl;
449 else if (m_verbose) {
450 std::cout <<
"Already in offboard mode" << std::endl;
455 while (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
457 std::cout <<
"Time out received in takeControl()" << std::endl;
463 std::cout <<
"Offboard mode started" << std::endl;
468 void setPositioningIncertitude(
float position_incertitude,
float yaw_incertitude)
470 m_position_incertitude = position_incertitude;
471 m_yaw_incertitude = yaw_incertitude;
474 bool takeOff(
bool interactive,
int timeout_sec,
bool use_gps)
476 if (!m_has_flying_capability) {
477 std::cerr <<
"Warning: Cannot takeoff this non flying vehicle" << std::endl;
481 bool authorize_takeoff =
false;
484 authorize_takeoff =
true;
487 if (m_telemetry.get()->flight_mode() == mavsdk::Telemetry::FlightMode::Offboard) {
488 authorize_takeoff =
true;
492 while (answer !=
"Y" && answer !=
"y" && answer !=
"N" && answer !=
"n") {
493 std::cout <<
"Current flight mode is not the offboard mode. Do you "
494 "want to force offboard mode ? (y/n)"
497 if (answer ==
"Y" || answer ==
"y") {
498 authorize_takeoff =
true;
504 if (m_telemetry.get()->in_air()) {
505 std::cerr <<
"Cannot take off as the robot is already flying." << std::endl;
508 else if (authorize_takeoff) {
518 while (answer !=
"Y" && answer !=
"y" && answer !=
"N" && answer !=
"n") {
519 std::cout <<
"If vehicle armed ? (y/n)" << std::endl;
521 if (answer ==
"N" || answer ==
"n") {
530 if (m_telemetry.get()->gps_info().fix_type == mavsdk::Telemetry::FixType::NoGps || !use_gps) {
539 auto in_air_promise = std::promise<void> {};
540 auto in_air_future = in_air_promise.get_future();
542 mavsdk::Telemetry::Odometry odom = m_telemetry.get()->odometry();
547 double X_init = odom.position_body.x_m;
548 double Y_init = odom.position_body.y_m;
549 double Z_init = odom.position_body.z_m;
552 std::cout <<
"Takeoff using position NED." << std::endl;
554 mavsdk::Offboard::PositionNedYaw takeoff {};
555 takeoff.north_m = X_init;
556 takeoff.east_m = Y_init;
557 takeoff.down_m = Z_init - m_takeoffAlt;
558 takeoff.yaw_deg = yaw_init;
559 m_offboard.get()->set_position_ned(takeoff);
562 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
563 Telemetry::LandedStateHandle handle = m_telemetry.get()->subscribe_landed_state(
564 [
this, &in_air_promise, &handle](mavsdk::Telemetry::LandedState state) {
565 if (state == mavsdk::Telemetry::LandedState::InAir) {
566 std::cout <<
"Drone is taking off\n.";
567 m_telemetry.get()->unsubscribe_landed_state(handle);
568 in_air_promise.set_value();
572 m_telemetry.get()->subscribe_landed_state([
this, &in_air_promise](mavsdk::Telemetry::LandedState state) {
573 if (state == mavsdk::Telemetry::LandedState::InAir) {
574 std::cout <<
"Drone is taking off\n.";
575 m_telemetry.get()->subscribe_landed_state(
nullptr);
576 in_air_promise.set_value();
578 std::cout <<
"state: " << state << std::endl;
581 if (in_air_future.wait_for(seconds(timeout_sec)) == std::future_status::timeout) {
582 std::cerr <<
"Takeoff failed: drone not in air.\n";
583 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
584 m_telemetry.get()->unsubscribe_landed_state(handle);
586 m_telemetry.get()->subscribe_landed_state(
nullptr);
591 auto takeoff_finished_promise = std::promise<void> {};
592 auto takeoff_finished_future = takeoff_finished_promise.get_future();
594 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
595 auto handle_odom = m_telemetry.get()->subscribe_odometry(
596 [
this, &takeoff_finished_promise, &handle, &Z_init](mavsdk::Telemetry::Odometry odom) {
597 if (odom.position_body.z_m < 0.90 * (Z_init - m_takeoffAlt) + m_position_incertitude) {
598 std::cout <<
"Takeoff altitude reached\n.";
599 m_telemetry.get()->unsubscribe_odometry(handle_odom);
600 takeoff_finished_promise.set_value();
604 m_telemetry.get()->subscribe_odometry(
605 [
this, &takeoff_finished_promise, &Z_init](mavsdk::Telemetry::Odometry odom) {
606 if (odom.position_body.z_m < 0.90 * (Z_init - m_takeoffAlt) + m_position_incertitude) {
607 std::cout <<
"Takeoff altitude reached\n.";
608 m_telemetry.get()->subscribe_odometry(nullptr);
609 takeoff_finished_promise.set_value();
613 if (takeoff_finished_future.wait_for(seconds(timeout_sec)) == std::future_status::timeout) {
614 std::cerr <<
"Takeoff failed: altitude not reached.\n";
615 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
616 m_telemetry.get()->unsubscribe_odometry(handle);
618 m_telemetry.get()->subscribe_odometry(
nullptr);
625 std::cout <<
"---- DEBUG: GPS detected: use action::takeoff()" << std::endl;
627 mavsdk::Telemetry::Odometry odom = m_telemetry.get()->odometry();
628 double Z_init = odom.position_body.z_m;
630 m_action.get()->set_takeoff_altitude(m_takeoffAlt);
631 const auto takeoff_result = m_action.get()->takeoff();
632 if (takeoff_result != mavsdk::Action::Result::Success) {
633 std::cerr <<
"Takeoff failed: " << takeoff_result <<
'\n';
637 auto in_air_promise = std::promise<void> {};
638 auto in_air_future = in_air_promise.get_future();
639 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
640 Telemetry::LandedStateHandle handle = m_telemetry.get()->subscribe_landed_state(
641 [
this, &in_air_promise, &handle](mavsdk::Telemetry::LandedState state) {
642 if (state == mavsdk::Telemetry::LandedState::InAir) {
643 std::cout <<
"Taking off has finished\n.";
644 m_telemetry.get()->unsubscribe_landed_state(handle);
645 in_air_promise.set_value();
649 m_telemetry.get()->subscribe_landed_state([
this, &in_air_promise](mavsdk::Telemetry::LandedState state) {
650 if (state == mavsdk::Telemetry::LandedState::InAir) {
651 std::cout <<
"Taking off has finished\n.";
652 m_telemetry.get()->subscribe_landed_state(
nullptr);
653 in_air_promise.set_value();
655 std::cout <<
"state: " << state << std::endl;
658 if (in_air_future.wait_for(seconds(timeout_sec)) == std::future_status::timeout) {
660 std::cerr <<
"Takeoff timed out.\n";
661 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
662 m_telemetry.get()->unsubscribe_landed_state(handle);
664 m_telemetry.get()->subscribe_landed_state(
nullptr);
668 auto takeoff_finished_promise = std::promise<void> {};
669 auto takeoff_finished_future = takeoff_finished_promise.get_future();
671 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
672 auto handle_odom = m_telemetry.get()->subscribe_odometry(
673 [
this, &takeoff_finished_promise, &handle, &Z_init](mavsdk::Telemetry::Odometry odom) {
674 if (odom.position_body.z_m < 0.90 * (Z_init - m_takeoffAlt) + m_position_incertitude) {
675 std::cout <<
"Takeoff altitude reached\n.";
676 m_telemetry.get()->unsubscribe_odometry(handle_odom);
677 takeoff_finished_promise.set_value();
681 m_telemetry.get()->subscribe_odometry(
682 [
this, &takeoff_finished_promise, &Z_init](mavsdk::Telemetry::Odometry odom) {
683 if (odom.position_body.z_m < 0.90 * (Z_init - m_takeoffAlt) + m_position_incertitude) {
684 std::cout <<
"Takeoff altitude reached\n.";
685 m_telemetry.get()->subscribe_odometry(nullptr);
686 takeoff_finished_promise.set_value();
690 if (takeoff_finished_future.wait_for(seconds(timeout_sec)) == std::future_status::timeout) {
691 std::cerr <<
"Takeoff failed: altitude not reached.\n";
692 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
693 m_telemetry.get()->unsubscribe_odometry(handle);
695 m_telemetry.get()->subscribe_odometry(
nullptr);
706 bool land(
bool use_buildin =
false)
708 if (!m_has_flying_capability) {
709 std::cerr <<
"Warning: Cannot land this non flying vehicle" << std::endl;
722 mavsdk::Telemetry::Odometry odom = m_telemetry.get()->odometry();
727 double X_init = odom.position_body.x_m;
728 double Y_init = odom.position_body.y_m;
731 std::cout <<
"Landing using position NED." << std::endl;
733 mavsdk::Offboard::PositionNedYaw landing {};
734 landing.north_m = X_init;
735 landing.east_m = Y_init;
737 landing.yaw_deg = yaw_init;
738 m_offboard.get()->set_position_ned(landing);
740 bool success =
false;
743 auto landing_finished_promise = std::promise<void> {};
744 auto landing_finished_future = landing_finished_promise.get_future();
746 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
747 auto handle_odom = m_telemetry.get()->subscribe_odometry(
748 [
this, &landing_finished_promise, &success, &handle](mavsdk::Telemetry::Odometry odom) {
749 if (odom.position_body.z_m > -0.15) {
750 std::cout <<
"Landing altitude reached \n.";
753 m_telemetry.get()->unsubscribe_odometry(handle_odom);
754 landing_finished_promise.set_value();
758 m_telemetry.get()->subscribe_odometry(
759 [
this, &landing_finished_promise, &success](mavsdk::Telemetry::Odometry odom) {
760 if (odom.position_body.z_m > -0.15) {
761 std::cout <<
"Landing altitude reached\n.";
764 m_telemetry.get()->subscribe_odometry(nullptr);
765 landing_finished_promise.set_value();
769 if (landing_finished_future.wait_for(seconds(10)) == std::future_status::timeout) {
770 std::cerr <<
"failed: altitude not reached.\n";
775 std::cout <<
"Descending\n.";
780 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Land) {
781 std::cout <<
"Landing...\n";
782 const mavsdk::Action::Result land_result = m_action.get()->land();
783 if (land_result != mavsdk::Action::Result::Success) {
784 std::cerr <<
"Land failed: " << land_result << std::endl;
789 while (m_telemetry.get()->in_air()) {
790 std::cout <<
"Vehicle is landing..." << std::endl;
791 sleep_for(seconds(1));
795 std::cout <<
"Landed!" << std::endl;
798 sleep_for(seconds(5));
799 std::cout <<
"Finished..." << std::endl;
803 bool setPosition(
float ned_north,
float ned_east,
float ned_down,
float ned_yaw,
bool blocking,
int timeout_sec)
805 mavsdk::Offboard::PositionNedYaw position_target {};
807 position_target.north_m = ned_north;
808 position_target.east_m = ned_east;
809 position_target.down_m = ned_down;
812 std::cout <<
"NED Pos to reach: " << position_target.north_m <<
" " << position_target.east_m <<
" "
813 << position_target.down_m <<
" " << position_target.yaw_deg << std::endl;
814 m_offboard.get()->set_position_ned(position_target);
816 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
818 std::cout <<
"Cannot set vehicle position: offboard mode not started" << std::endl;
825 auto position_reached_promise = std::promise<void> {};
826 auto position_reached_future = position_reached_promise.get_future();
828 #if (VISP_HAVE_MAVSDK_VERSION > 0x010412)
829 auto handle_odom = m_telemetry.get()->subscribe_odometry(
830 [
this, &position_reached_promise, &handle, &position_target](mavsdk::Telemetry::Odometry odom) {
835 double distance_to_target = std::sqrt(
vpMath::sqr(odom.position_body.x_m - position_target.north_m) +
836 vpMath::sqr(odom.position_body.y_m - position_target.east_m) +
837 vpMath::sqr(odom.position_body.z_m - position_target.down_m));
838 if (distance_to_target < m_position_incertitude &&
839 std::fabs(odom_yaw - position_target.yaw_deg) < m_yaw_incertitude) {
840 std::cout <<
"Position reached\n.";
841 m_telemetry.get()->unsubscribe_odometry(handle_odom);
842 position_reached_promise.set_value();
846 m_telemetry.get()->subscribe_odometry(
847 [
this, &position_reached_promise, &position_target](mavsdk::Telemetry::Odometry odom) {
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()->subscribe_odometry(
nullptr);
859 position_reached_promise.set_value();
863 if (position_reached_future.wait_for(seconds(timeout_sec)) == std::future_status::timeout) {
864 std::cerr <<
"Positioning failed: position not reached.\n";
869 std::cout <<
"---- DEBUG timeout: " << timeout_sec << std::endl;
873 bool setPositionRelative(
float ned_delta_north,
float ned_delta_east,
float ned_delta_down,
float ned_delta_yaw,
874 bool blocking,
int timeout_sec)
876 mavsdk::Telemetry::Odometry odom;
877 mavsdk::Telemetry::EulerAngle angles;
878 mavsdk::Offboard::PositionNedYaw position_target {};
880 position_target.north_m = ned_delta_north;
881 position_target.east_m = ned_delta_east;
882 position_target.down_m = ned_delta_down;
883 position_target.yaw_deg =
vpMath::deg(ned_delta_yaw);
886 odom = m_telemetry.get()->odometry();
887 angles = m_telemetry.get()->attitude_euler();
889 position_target.north_m += odom.position_body.x_m;
890 position_target.east_m += odom.position_body.y_m;
891 position_target.down_m += odom.position_body.z_m;
892 position_target.yaw_deg += angles.yaw_deg;
894 return setPosition(position_target.north_m, position_target.east_m, position_target.down_m,
895 vpMath::rad(position_target.yaw_deg), blocking, timeout_sec);
901 if (XYZvec[0] != 0.0) {
902 std::cerr <<
"ERROR : Can't move, rotation around X axis should be 0." << std::endl;
905 if (XYZvec[1] != 0.0) {
906 std::cerr <<
"ERROR : Can't move, rotation around Y axis should be 0." << std::endl;
910 absolute, timeout_sec);
916 if (XYZvec[0] != 0.0) {
917 std::cerr <<
"ERROR : Can't move, rotation around X axis should be 0." << std::endl;
920 if (XYZvec[1] != 0.0) {
921 std::cerr <<
"ERROR : Can't move, rotation around Y axis should be 0." << std::endl;
925 XYZvec[2], blocking, timeout_sec);
930 if (frd_vel_cmd.
size() != 4) {
932 "ERROR : Can't set velocity, dimension of the velocity vector %d should be equal to 4.",
933 frd_vel_cmd.
size()));
936 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
938 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
942 mavsdk::Offboard::VelocityBodyYawspeed velocity_comm {};
943 velocity_comm.forward_m_s = frd_vel_cmd[0];
944 velocity_comm.right_m_s = frd_vel_cmd[1];
945 velocity_comm.down_m_s = frd_vel_cmd[2];
946 velocity_comm.yawspeed_deg_s =
vpMath::deg(frd_vel_cmd[3]);
947 m_offboard.get()->set_velocity_body(velocity_comm);
954 const mavsdk::Action::Result kill_result = m_action.get()->kill();
955 if (kill_result != mavsdk::Action::Result::Success) {
956 std::cerr <<
"Kill failed: " << kill_result << std::endl;
964 if (m_telemetry.get()->in_air()) {
965 if (m_telemetry.get()->gps_info().fix_type != mavsdk::Telemetry::FixType::NoGps) {
967 const mavsdk::Action::Result hold_result = m_action.get()->hold();
968 if (hold_result != mavsdk::Action::Result::Success) {
969 std::cerr <<
"Hold failed: " << hold_result << std::endl;
974 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
976 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
981 setPositionRelative(0., 0., 0., 0.,
false, 10.);
989 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
991 std::cout <<
"Cannot stop moving: offboard mode not started" << std::endl;
996 const mavsdk::Offboard::VelocityBodyYawspeed stay {};
997 m_offboard.get()->set_velocity_body(stay);
1002 bool releaseControl()
1004 auto offboard_result = m_offboard.get()->stop();
1005 if (offboard_result != mavsdk::Offboard::Result::Success) {
1006 std::cerr <<
"Offboard stop failed: " << offboard_result <<
'\n';
1009 std::cout <<
"Offboard stopped\n";
1013 void setAutoLand(
bool auto_land) { m_auto_land = auto_land; }
1015 bool setYawSpeed(
double body_frd_wz)
1017 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
1019 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
1023 mavsdk::Offboard::VelocityBodyYawspeed velocity_comm {};
1024 velocity_comm.forward_m_s = 0.0;
1025 velocity_comm.right_m_s = 0.0;
1026 velocity_comm.down_m_s = 0.0;
1027 velocity_comm.yawspeed_deg_s =
vpMath::deg(body_frd_wz);
1028 m_offboard.get()->set_velocity_body(velocity_comm);
1033 bool setForwardSpeed(
double body_frd_vx)
1035 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
1037 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
1042 mavsdk::Offboard::VelocityBodyYawspeed velocity_comm {};
1043 velocity_comm.forward_m_s = body_frd_vx;
1044 velocity_comm.right_m_s = 0.0;
1045 velocity_comm.down_m_s = 0.0;
1046 velocity_comm.yawspeed_deg_s = 0.0;
1047 m_offboard.get()->set_velocity_body(velocity_comm);
1052 bool setLateralSpeed(
double body_frd_vy)
1054 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
1056 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
1060 mavsdk::Offboard::VelocityBodyYawspeed velocity_comm {};
1061 velocity_comm.forward_m_s = 0.0;
1062 velocity_comm.right_m_s = body_frd_vy;
1063 velocity_comm.down_m_s = 0.0;
1064 velocity_comm.yawspeed_deg_s = 0.0;
1065 m_offboard.get()->set_velocity_body(velocity_comm);
1070 bool setVerticalSpeed(
double body_frd_vz)
1072 if (m_telemetry.get()->flight_mode() != mavsdk::Telemetry::FlightMode::Offboard) {
1074 std::cout <<
"Cannot set vehicle velocity: offboard mode not started" << std::endl;
1078 mavsdk::Offboard::VelocityBodyYawspeed velocity_comm {};
1079 velocity_comm.forward_m_s = 0.0;
1080 velocity_comm.right_m_s = 0.0;
1081 velocity_comm.down_m_s = body_frd_vz;
1082 velocity_comm.yawspeed_deg_s = 0.0;
1083 m_offboard.get()->set_velocity_body(velocity_comm);
1088 bool getFlyingCapability() {
return m_has_flying_capability; }
1090 void setVerbose(
bool verbose) { m_verbose = verbose; }
1094 std::string m_address {};
1095 mavsdk::Mavsdk m_mavsdk {};
1096 std::shared_ptr<mavsdk::System> m_system;
1097 std::shared_ptr<mavsdk::Action> m_action;
1098 std::shared_ptr<mavsdk::Telemetry> m_telemetry;
1099 std::shared_ptr<mavsdk::Offboard> m_offboard;
1101 double m_takeoffAlt { 1.0 };
1103 MAV_TYPE m_mav_type {};
1104 bool m_has_flying_capability {
false };
1106 float m_position_incertitude { 0.05 };
1107 float m_yaw_incertitude { 0.09 };
1108 bool m_verbose {
false };
1109 bool m_auto_land {
true };
1149 vpRobotMavsdk::vpRobotMavsdk(
const std::string &connection_info) : m_impl(new vpRobotMavsdkImpl(connection_info))
1151 m_impl->setPositioningIncertitude(0.05,
vpMath::rad(5));
1168 vpRobotMavsdk::vpRobotMavsdk() : m_impl(new vpRobotMavsdkImpl())
1170 m_impl->setPositioningIncertitude(0.05,
vpMath::rad(5));
1180 vpRobotMavsdk::~vpRobotMavsdk() {
delete m_impl; }
1194 void vpRobotMavsdk::connect(
const std::string &connection_info) { m_impl->connect(connection_info); }
1199 bool vpRobotMavsdk::isRunning()
const {
return m_impl->isRunning(); }
1223 return m_impl->sendMocapData(enu_M_flu, display_fps);
1232 std::string vpRobotMavsdk::getAddress()
const {
return m_impl->getAddress(); }
1239 float vpRobotMavsdk::getBatteryLevel()
const {
return m_impl->getBatteryLevel(); }
1245 void vpRobotMavsdk::getPosition(
vpHomogeneousMatrix &ned_M_frd)
const { m_impl->getPosition(ned_M_frd); }
1254 void vpRobotMavsdk::getPosition(
float &ned_north,
float &ned_east,
float &ned_down,
float &ned_yaw)
const
1256 m_impl->getPosition(ned_north, ned_east, ned_down, ned_yaw);
1265 std::tuple<float, float> vpRobotMavsdk::getHome()
const {
return m_impl->getHome(); }
1272 void vpRobotMavsdk::doFlatTrim() { }
1281 void vpRobotMavsdk::setTakeOffAlt(
double altitude) { m_impl->setTakeOffAlt(altitude); }
1287 bool vpRobotMavsdk::arm() {
return m_impl->arm(); }
1293 bool vpRobotMavsdk::disarm() {
return m_impl->disarm(); }
1309 bool vpRobotMavsdk::takeOff(
bool interactive,
int timeout_sec,
bool use_gps)
1311 return m_impl->takeOff(interactive, timeout_sec, use_gps);
1328 bool vpRobotMavsdk::takeOff(
bool interactive,
double takeoff_altitude,
int timeout_sec,
bool use_gps)
1330 m_impl->setTakeOffAlt(takeoff_altitude);
1331 return m_impl->takeOff(interactive, timeout_sec, use_gps);
1341 bool vpRobotMavsdk::holdPosition() {
return m_impl->holdPosition(); };
1348 bool vpRobotMavsdk::stopMoving() {
return m_impl->stopMoving(); };
1357 bool vpRobotMavsdk::land() {
return m_impl->land(); }
1373 bool vpRobotMavsdk::setPosition(
float ned_north,
float ned_east,
float ned_down,
float ned_yaw,
bool blocking,
1376 return m_impl->setPosition(ned_north, ned_east, ned_down, ned_yaw, blocking, timeout_sec);
1395 bool vpRobotMavsdk::setPosition(
const vpHomogeneousMatrix &ned_M_frd,
bool blocking,
int timeout_sec)
1397 return m_impl->setPosition(ned_M_frd, blocking, timeout_sec);
1414 bool vpRobotMavsdk::setPositionRelative(
float ned_delta_north,
float ned_delta_east,
float ned_delta_down,
1415 float ned_delta_yaw,
bool blocking,
int timeout_sec)
1417 return m_impl->setPositionRelative(ned_delta_north, ned_delta_east, ned_delta_down, ned_delta_yaw, blocking,
1436 bool vpRobotMavsdk::setPositionRelative(
const vpHomogeneousMatrix &delta_frd_M_frd,
bool blocking,
int timeout_sec)
1438 return m_impl->setPositionRelative(delta_frd_M_frd, blocking, timeout_sec);
1450 bool vpRobotMavsdk::setVelocity(
const vpColVector &frd_vel_cmd) {
return m_impl->setVelocity(frd_vel_cmd); }
1457 bool vpRobotMavsdk::kill() {
return m_impl->kill(); }
1470 bool vpRobotMavsdk::setYawSpeed(
double body_frd_wz) {
return m_impl->setYawSpeed(body_frd_wz); }
1483 bool vpRobotMavsdk::setForwardSpeed(
double body_frd_vx) {
return m_impl->setForwardSpeed(body_frd_vx); }
1496 bool vpRobotMavsdk::setLateralSpeed(
double body_frd_vy) {
return m_impl->setLateralSpeed(body_frd_vy); }
1506 bool vpRobotMavsdk::setGPSGlobalOrigin(
double latitude,
double longitude,
double altitude)
1508 return m_impl->setGPSGlobalOrigin(latitude, longitude, altitude);
1522 bool vpRobotMavsdk::takeControl() {
return m_impl->takeControl(); }
1533 bool vpRobotMavsdk::releaseControl() {
return m_impl->releaseControl(); }
1542 void vpRobotMavsdk::setAutoLand(
bool auto_land) { m_impl->setAutoLand(auto_land); }
1551 void vpRobotMavsdk::setPositioningIncertitude(
float position_incertitude,
float yaw_incertitude)
1553 m_impl->setPositioningIncertitude(position_incertitude, yaw_incertitude);
1567 bool vpRobotMavsdk::setVerticalSpeed(
double body_frd_vz) {
return m_impl->setVerticalSpeed(body_frd_vz); }
1574 void vpRobotMavsdk::setVerbose(
bool verbose) { m_impl->setVerbose(verbose); }
1581 bool vpRobotMavsdk::hasFlyingCapability() {
return m_impl->getFlyingCapability(); }
1582 #ifdef ENABLE_VISP_NAMESPACE
1585 #elif !defined(VISP_BUILD_SHARED_LIBS)
1587 void dummy_vpRobotMavsdk() { };
unsigned int size() const
Return the number of elements of the 2D array.
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
@ 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)
Implementation of a rotation vector as quaternion angle minimal representation.
const double & x() const
Returns the x-component of the quaternion.
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as Euler angle minimal representation.
Class that consider the case of a translation vector.
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()