Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set flight mode function #2128

Open
alireza787b opened this issue Sep 6, 2023 · 7 comments
Open

set flight mode function #2128

alireza787b opened this issue Sep 6, 2023 · 7 comments
Labels

Comments

@alireza787b
Copy link
Contributor

Hi
I was just tryin to build a new function to set the flight mode command in mavsdk. I created the proto s and were just implementing the cpp when I found that there is already a function there called set_flight_mode_async that does it. is that correct? how can I change flight mode from python to sth like hold?

@julianoes
Copy link
Collaborator

For PX4, to go into Hold, you can do mission->pause.

For proper support of flight modes, I'd like to use the the common flight modes which is a new MAVLink spec allowing standard and custom modes which would be (eventually) compatible with both PX4 and ArduPilot: https://github.com/mavlink/mavlink/blob/fe14d798662f687407b03e383d2a9841a8590392/message_definitions/v1.0/development.xml#L473-L502.

@alireza787b
Copy link
Contributor Author

alireza787b commented Sep 7, 2023

what if I am not already in a mission mode? maybe in offboard switching to hold (maybe stopping offboard would do that) but generally eg. on the ground I want to change flight mode to position hold. or stabilized
I was trying to so sth like this :

Action::Result ActionImpl::SetFlightMode(const std::string& flight_mode)
{
    // Initialize variables for the MAVLink command
    uint8_t base_mode = 0;
    uint8_t main_mode = 0;
    uint8_t sub_mode = 0;

    // Convert flight_mode string to base_mode, main_mode, and sub_mode
    if (flight_mode == "Manual") {
        base_mode = 217;
        main_mode = 1;
        sub_mode = 0;
    } else if (flight_mode == "Stabilized") {
        base_mode = 209;
        main_mode = 7;
        sub_mode = 0;
    } else if (flight_mode == "Acro") {
        base_mode = 209;
        main_mode = 5;
        sub_mode = 0;
    } else if (flight_mode == "Rattitude") {
        base_mode = 193;
        main_mode = 8;
        sub_mode = 0;
    } else if (flight_mode == "Altitude") {
        base_mode = 193;
        main_mode = 2;
        sub_mode = 0;
    } else if (flight_mode == "Offboard") {
        base_mode = 209;
        main_mode = 6;
        sub_mode = 0;
    } else if (flight_mode == "Position") {
        base_mode = 209;
        main_mode = 3;
        sub_mode = 0;
    } else if (flight_mode == "Hold") {
        base_mode = 217;
        main_mode = 4;
        sub_mode = 3;
    } else {
        // If the mode is not recognized, return an error
        return Action::Result::UNKNOWN_ERROR;
    }

    // Prepare the MAVLink message
    mavlink_message_t msg;
    mavlink_msg_command_long_pack(
        _parent->get_own_system_id(),
        _parent->get_own_component_id(),
        &msg,
        _parent->get_target_system_id(),
        _parent->get_target_component_id(),
        MAV_CMD_DO_SET_MODE,  // command id
        0,                    // confirmation
        base_mode,             // base_mode
        main_mode,             // main_mode
        sub_mode,              // sub_mode
        0, 0, 0, 0             // unused parameters
    );

    // Send the MAVLink message and receive the result
    auto send_result = _parent->send_message(msg);

    // Convert the MavlinkCommandSender::Result to Action::Result and return
    return action_result_from_command_result(send_result);
}

I got this mode codes from forums . when I try to implement this I notice there is already a set_flight_mode in the codes. just wanted to make sure if I am on correct path or doing sth wrong?

void ActionImpl::hold_async(const Action::ResultCallback& callback) const
{
    _system_impl->set_flight_mode_async(
        FlightMode::Hold, [this, callback](MavlinkCommandSender::Result result, float) {
            command_result_callback(result, callback);
        });
}

@julianoes
Copy link
Collaborator

what if I am not already in a mission mode?

It doesn't matter. Check what it does:

void MissionImpl::pause_mission_async(const Mission::ResultCallback& callback)
{
_system_impl->set_flight_mode_async(
FlightMode::Hold, [this, callback](MavlinkCommandSender::Result result, float) {
report_flight_mode_change(callback, result);
});
}

Yes, you should be able to build on top of the set_flight_mode.

@acofis
Copy link

acofis commented Sep 11, 2023

Is there any existing code for setting other modes?

I am starting work with ardupilot drones and need them to be set to "guided" before they can get a takeoff command, and must return from "land" to "guided" to fly again.

I am considering contributing once I have looked over the methodology for doing so but wanted to be sure this is not already in place

@JonasVautherin
Copy link
Collaborator

I am starting work with ardupilot drones and need them to be set to "guided" before they can get a takeoff command

You should not have to do that, MAVSDK should set the mode correctly when you drone.action.takeoff(). However, maybe that's not yet in the latest release v1.4.x.

For Ardupilot you should use the latest main branch until v2 is out, I would say.

@acofis
Copy link

acofis commented Sep 11, 2023

I was using the latest 1.4 built server exe for windows. I will attempt to buld main and run the same command to see how this goes.

@acofis
Copy link

acofis commented Sep 12, 2023

I am starting work with ardupilot drones and need them to be set to "guided" before they can get a takeoff command

You should not have to do that, MAVSDK should set the mode correctly when you drone.action.takeoff(). However, maybe that's not yet in the latest release v1.4.x.

For Ardupilot you should use the latest main branch until v2 is out, I would say.

I am attempting to build a mavDSK-server.exe from main and I have 2 problems:

  1. The instructions do not mention windows.
  2. The lines provided for Linux and mac (which are identical as far as I can tell) do not work
 No update step for 'openssl'
  No patch step for 'openssl'
  Performing configure step for 'openssl'
  Can't locate Locale/Maketext/Simple.pm in @INC (you may need to install the Locale::Maketext::Simple module) (@INC contains: /c/MAVSDK/MAVSDK/build/default/third_party/openssl/openssl/src/openssl/util/perl /usr/lib/perl5/site_perl /usr/share/perl5/site_perl /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5/core_perl /usr/share/perl5/core_perl /c/MAVSDK/MAVSDK/build/default/third_party/openssl/openssl/src/openssl/external/perl/Text-Template-1.56/lib) at /usr/share/perl5/core_perl/Params/Check.pm line 6.
  BEGIN failed--compilation aborted at /usr/share/perl5/core_perl/Params/Check.pm line 6.
  Compilation failed in require at /usr/share/perl5/core_perl/IPC/Cmd.pm line 59.
  BEGIN failed--compilation aborted at /usr/share/perl5/core_perl/IPC/Cmd.pm line 59.
  Compilation failed in require at /c/MAVSDK/MAVSDK/build/default/third_party/openssl/openssl/src/openssl/util/perl/OpenSSL/config.pm line 19.
  BEGIN failed--compilation aborted at /c/MAVSDK/MAVSDK/build/default/third_party/openssl/openssl/src/openssl/util/perl/OpenSSL/config.pm line 19.
  Compilation failed in require at C:/MAVSDK/MAVSDK/build/default/third_party/openssl/openssl/src/openssl/Configure line 23.
  BEGIN failed--compilation aborted at C:/MAVSDK/MAVSDK/build/default/third_party/openssl/openssl/src/openssl/Configure line 23.
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(249,5): error MSB8066: Custom build for 'C:\MAVSDK\MAVSDK\build\default\third_party\openssl\CMakeFiles\9fd51fc370f4e1eee9ede0f03f96e7cc\openssl-mkdir.rule;C:\MAVSDK\MAVSDK\build\default\third_party\openssl\CMakeFiles\9fd51fc370f4e1eee9ede0f03f96e7cc\openssl-download.rule;C:\MAVSDK\MAVSDK\build\default\third_party\openssl\CMakeFiles\9fd51fc370f4e1eee9ede0f03f96e7cc\openssl-update.rule;C:\MAVSDK\MAVSDK\build\default\third_party\openssl\CMakeFiles\9fd51fc370f4e1eee9ede0f03f96e7cc\openssl-patch.rule;C:\MAVSDK\MAVSDK\build\default\third_party\openssl\CMakeFiles\9fd51fc370f4e1eee9ede0f03f96e7cc\openssl-configure.rule;C:\MAVSDK\MAVSDK\build\default\third_party\openssl\CMakeFiles\9fd51fc370f4e1eee9ede0f03f96e7cc\openssl-build.rule;C:\MAVSDK\MAVSDK\build\default\third_party\openssl\CMakeFiles\9fd51fc370f4e1eee9ede0f03f96e7cc\openssl-install.rule;C:\MAVSDK\MAVSDK\build\default\third_party\openssl\CMakeFiles\91fe7f23fb265bc3325a0f7fdda7f7e7\openssl-complete.rule;C:\MAVSDK\MAVSDK\build\default\third_party\openssl\CMakeFiles\166370b8cb89e2a7e4ed27f6cd2c8dff\openssl.rule;C:\MAVSDK\MAVSDK\third_party\openssl\CMakeLists.txt' exited with code 2. [C:\MAVSDK\MAVSDK\build\default\third_party\openssl\openssl.vcxproj]
CMake Error at third_party/cmake/build_target.cmake:51 (message):
  C:/MAVSDK/MAVSDK/build/default/third_party/openssl failed to build!
Call Stack (most recent call first):
  third_party/CMakeLists.txt:22 (build_target)


-- Configuring incomplete, errors occurred!

Am I just missing something in the docs or is windows missing specifically? Should I make a new ticket to discuss this?

Lastly are there nightly builds of the binaries that I could use to get the latest snapshot of main and avoid this build process?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants