[经验分享] OpenHarmony无人机MAVSDK开源库适配方案分享(二)

陈美汝 显示全部楼层 发表于 2024-3-26 10:21:20

(接上篇)

BUILD.gn 文件

在完成上述步骤后,下面需要在ohos/third目录添加BUILD.gn,根据不同的系统生成静态或者动态库,注意点如下:

文件 作用
build.make 生成目标的源文件
depend.make 需要依赖的其他C源文件
flags.make cflags相关的一些标志
link.txt 链接库相关信息

①注意移植时的cflags、cflags应从原始的编译文件(例如cmake 过程中build的生成文件:在cmake生成makefile的过程中分析build.make、depend.make、flags.make、link.txt等得出三方库对应的依赖关系)中获取相关的标志位

②注意移植的源文件及头文件地址可以使用当前build.gn所在路径下相对位置,例如/src与build.gn在同一路径下则路径可以直接应用src/....

③注意deps若引用的是组件内的其他模块,例如其他build.gn文件中的组件或当前build.gn中位于此deps前的其他组件可以使用”:module进行模块依赖,若是引用组件外的其他组件则需要使用”bundle

import("//build/ohos.gni")  
import("//build/test.gni")

config("mavsdk_cflag_config") {
    #编译时的标志位
    cflags = 
            [  
                "-fexceptions",
                "-Wextra",
                "-Wshadow",
                "-Wno-strict-aliasing",
                "-Wformat=2",
                "-Wextra",
                "-Wno-unused-lambda-capture",
                "-Wno-pragmas",
                "-Wno-unknown-warning-option",
                "-Wduplicated-cond",
                "-Wnull-dereference",
                "-Wduplicated-branches",
                "-Wlogical-op", 
                "-Wno-old-style-cast",
                "-pthread",
            ]
    cflags_cc = [ "-frtti" ] #fix:[OHOS ERROR] ../../prebuilts/clang/ohos/linux-x86_64/llvm/bin/../include/libcxx-ohos/include/c++/v1/memory:3566:16: error: use of dynamic_cast requires -frtti


    ldflags = [ "-fPIC",
                "-g",
                "-shared",
                "-Wl",
                "-soname" ]
}

ohos_source_set("libmavsdk_source") {

    configs = [":mavsdk_cflag_config"]

    deps = [ 
             "//third_party/curl:curl"  
    ] 
    #需要包含的源文件
    sources = [
            "src/mavsdk/core/call_every_handler.cpp" ,
            "src/mavsdk/core/connection.cpp" ,
            "src/mavsdk/core/connection_result.cpp" ,
            "src/mavsdk/core/curl_wrapper.cpp" ,
            "src/mavsdk/core/crc32.cpp" ,
            "src/mavsdk/core/system.cpp" ,
            "src/mavsdk/core/system_impl.cpp" ,
            "src/mavsdk/core/fs.cpp" ,
            "src/mavsdk/core/mavsdk.cpp" ,
            "src/mavsdk/core/mavsdk_impl.cpp" ,
            "src/mavsdk/core/http_loader.cpp" ,
            "src/mavsdk/core/mavlink_channels.cpp" ,
            "src/mavsdk/core/mavlink_command_receiver.cpp" ,
            "src/mavsdk/core/mavlink_command_sender.cpp" ,
            "src/mavsdk/core/mavlink_ftp.cpp" ,
            "src/mavsdk/core/mavlink_mission_transfer.cpp" ,
            "src/mavsdk/core/mavlink_parameters.cpp" ,
            "src/mavsdk/core/mavlink_receiver.cpp" ,
            "src/mavsdk/core/mavlink_request_message_handler.cpp" ,
            "src/mavsdk/core/mavlink_statustext_handler.cpp" ,
            "src/mavsdk/core/mavlink_message_handler.cpp" ,
            "src/mavsdk/core/ping.cpp" ,
            "src/mavsdk/core/plugin_impl_base.cpp" ,
            "src/mavsdk/core/serial_connection.cpp" ,
            "src/mavsdk/core/tcp_connection.cpp" ,
            "src/mavsdk/core/timeout_handler.cpp" ,
            "src/mavsdk/core/udp_connection.cpp" ,
            "src/mavsdk/core/log.cpp" ,
            "src/mavsdk/core/cli_arg.cpp" ,
            "src/mavsdk/core/geometry.cpp" ,
            "src/mavsdk/core/request_message.cpp" ,
            "src/mavsdk/core/mavsdk_time.cpp" ,
            "src/mavsdk/core/timesync.cpp" ,
            "src/mavsdk/plugins/action/action.cpp" ,
            "src/mavsdk/plugins/action/action_impl.cpp" ,
            "src/mavsdk/plugins/action_server/action_server.cpp" ,
            "src/mavsdk/plugins/action_server/action_server_impl.cpp" ,
            "src/mavsdk/plugins/calibration/calibration.cpp" ,
            "src/mavsdk/plugins/calibration/calibration_impl.cpp" ,
            "src/mavsdk/plugins/calibration/calibration_statustext_parser.cpp" ,
            "src/mavsdk/plugins/camera/camera.cpp" ,
            "src/mavsdk/plugins/camera/camera_impl.cpp" ,
            "src/mavsdk/plugins/camera/camera_definition.cpp" ,
            "src/mavsdk/plugins/camera/camera_definition_files/generated/camera_definition_files.cpp" ,
            "src/mavsdk/plugins/component_information/component_information.cpp" ,
            "src/mavsdk/plugins/component_information/component_information_impl.cpp" ,
            "src/mavsdk/plugins/component_information_server/component_information_server.cpp" ,
            "src/mavsdk/plugins/component_information_server/component_information_server_impl.cpp" ,
            "src/mavsdk/plugins/failure/failure.cpp" ,
            "src/mavsdk/plugins/failure/failure_impl.cpp" ,
            "src/mavsdk/plugins/follow_me/follow_me.cpp" ,
            "src/mavsdk/plugins/follow_me/follow_me_impl.cpp" ,
            "src/mavsdk/plugins/ftp/ftp.cpp" ,
            "src/mavsdk/plugins/ftp/ftp_impl.cpp" ,
            "src/mavsdk/plugins/geofence/geofence.cpp" ,
            "src/mavsdk/plugins/geofence/geofence_impl.cpp" ,
            "src/mavsdk/plugins/gimbal/gimbal.cpp" ,
            "src/mavsdk/plugins/gimbal/gimbal_impl.cpp" ,
            "src/mavsdk/plugins/gimbal/gimbal_protocol_v1.cpp" ,
            "src/mavsdk/plugins/gimbal/gimbal_protocol_v2.cpp" ,
            "src/mavsdk/plugins/info/info.cpp" ,
            "src/mavsdk/plugins/info/info_impl.cpp" ,
            "src/mavsdk/plugins/log_files/log_files.cpp" ,
            "src/mavsdk/plugins/log_files/log_files_impl.cpp" ,
            "src/mavsdk/plugins/manual_control/manual_control.cpp" ,
            "src/mavsdk/plugins/manual_control/manual_control_impl.cpp" ,
            "src/mavsdk/plugins/mavlink_passthrough/mavlink_passthrough.cpp" ,
            "src/mavsdk/plugins/mavlink_passthrough/mavlink_passthrough_impl.cpp" ,
            "src/mavsdk/plugins/mission/mission.cpp" ,
            "src/mavsdk/plugins/mission/mission_impl.cpp" ,
            "src/mavsdk/plugins/mission_raw/mission_raw.cpp" ,
            "src/mavsdk/plugins/mission_raw/mission_raw_impl.cpp" ,
            "src/mavsdk/plugins/mission_raw/mission_import.cpp" ,
            "src/mavsdk/plugins/mission_raw_server/mission_raw_server.cpp" ,
            "src/mavsdk/plugins/mission_raw_server/mission_raw_server_impl.cpp" ,
            "src/mavsdk/plugins/mocap/mocap.cpp" ,
            "src/mavsdk/plugins/mocap/mocap_impl.cpp" ,
            "src/mavsdk/plugins/offboard/offboard.cpp" ,
            "src/mavsdk/plugins/offboard/offboard_impl.cpp" ,
            "src/mavsdk/plugins/param/param.cpp" ,
            "src/mavsdk/plugins/param/param_impl.cpp" ,
            "src/mavsdk/plugins/param_server/param_server.cpp" ,
            "src/mavsdk/plugins/param_server/param_server_impl.cpp" ,
            "src/mavsdk/plugins/server_utility/server_utility.cpp" ,
            "src/mavsdk/plugins/server_utility/server_utility_impl.cpp" ,
            "src/mavsdk/plugins/shell/shell.cpp" ,
            "src/mavsdk/plugins/shell/shell_impl.cpp" ,
            "src/mavsdk/plugins/telemetry/telemetry.cpp" ,
            "src/mavsdk/plugins/telemetry/telemetry_impl.cpp" ,
            "src/mavsdk/plugins/telemetry/math_conversions.cpp" ,
            "src/mavsdk/plugins/telemetry_server/telemetry_server.cpp" ,
            "src/mavsdk/plugins/telemetry_server/telemetry_server_impl.cpp" ,
            "src/mavsdk/plugins/tracking_server/tracking_server.cpp" ,
            "src/mavsdk/plugins/tracking_server/tracking_server_impl.cpp" ,
            "src/mavsdk/plugins/transponder/transponder.cpp" ,
            "src/mavsdk/plugins/transponder/transponder_impl.cpp" ,
            "src/mavsdk/plugins/rtk/rtk.cpp" ,
            "src/mavsdk/plugins/rtk/rtk_impl.cpp" ,
            "src/mavsdk/plugins/tune/tune.cpp" ,
            "src/mavsdk/plugins/tune/tune_impl.cpp"
         ]

    #需要包含的头文件
    include_dirs = [
            "src/mavsdk/core",
            "src/mavsdk/core/include/mavsdk",
            "src/mavsdk/plugins/action/include",
            "src/mavsdk/plugins/action_server/include",
            "src/mavsdk/plugins/calibration/include",
            "src/mavsdk/plugins/camera/include",
            "src/mavsdk/plugins/camera/camera_definition_files/generated",
            "src/mavsdk/plugins/component_information/include",
            "src/mavsdk/plugins/component_information_server/include",
            "src/mavsdk/plugins/failure/include",
            "src/mavsdk/plugins/follow_me/include",
            "src/mavsdk/plugins/ftp/include",
            "src/mavsdk/plugins/geofence/include",
            "src/mavsdk/plugins/gimbal/include",
            "src/mavsdk/plugins/info/include",
            "src/mavsdk/plugins/log_files/include",
            "src/mavsdk/plugins/manual_control/include",
            "src/mavsdk/plugins/mavlink_passthrough/include",
            "src/mavsdk/plugins/mission/include",
            "src/mavsdk/plugins/mission_raw/include",
            "src/mavsdk/plugins/mission_raw_server/include",
            "src/mavsdk/plugins/mocap/include",
            "src/mavsdk/plugins/offboard/include",
            "src/mavsdk/plugins/param/include",
            "src/mavsdk/plugins/param_server/include",
            "src/mavsdk/plugins/rtk/include",
            "src/mavsdk/plugins/server_utility/include",
            "src/mavsdk/plugins/shell/include",
            "src/mavsdk/plugins/telemetry/include",
            "src/mavsdk/plugins/telemetry_server/include",
            "src/mavsdk/plugins/tracking_server/include",
            "src/mavsdk/plugins/transponder/include",
            "src/mavsdk/plugins/tune/include",
            "src/mavsdk/plugins/action",
            "src/mavsdk/plugins/action_server",
            "src/mavsdk/plugins/calibration",
            "src/mavsdk/plugins/camera",
            "src/mavsdk/plugins/component_information",
            "src/mavsdk/plugins/component_information_server",
            "src/mavsdk/plugins/failure",
            "src/mavsdk/plugins/follow_me",
            "src/mavsdk/plugins/ftp",
            "src/mavsdk/plugins/geofence",
            "src/mavsdk/plugins/gimbal",
            "src/mavsdk/plugins/info",
            "src/mavsdk/plugins/log_files",
            "src/mavsdk/plugins/manual_control",
            "src/mavsdk/plugins/mavlink_passthrough",
            "src/mavsdk/plugins/mission",
            "src/mavsdk/plugins/mission_raw",
            "src/mavsdk/plugins/mission_raw_server",
            "src/mavsdk/plugins/mocap",
            "src/mavsdk/plugins/offboard",
            "src/mavsdk/plugins/param",
            "src/mavsdk/plugins/param_server",
            "src/mavsdk/plugins/rtk",
            "src/mavsdk/plugins/server_utility",
            "src/mavsdk/plugins/shell",
            "src/mavsdk/plugins/telemetry",
            "src/mavsdk/plugins/telemetry_server",
            "src/mavsdk/plugins/tracking_server",
            "src/mavsdk/plugins/transponder",
            "src/mavsdk/plugins/tune",
            "src/third_party/mavlink",
            "src/third_party/mavlink/mavlink/src/mavlink/include/mavlink/v2.0",
            "src/third_party/mavlink/mavlink/src/mavlink/include/mavlink/v2.0/common",
            "//third_party/jsoncpp/include",
            "//third_party/curl/include",
            "//third_party/tinyxml2",
            "//third_party/zlib",
        ]
}

ohos_shared_library("MAVSDK") {


    deps = [    ":libmavsdk_source",
                "//third_party/curl:curl_shared",
                "//third_party/openssl:libssl_shared",
                "//third_party/openssl:libcrypto_shared",
                "//third_party/jsoncpp:jsoncpp",
                "//third_party/tinyxml2:tinyxml2"
            ]         #依赖的三方库


    part_name = "MAVSDK"
    subsystem_name = "mavsdk"
    output_name = "libmavsdk"
  }

ohos_executable("takeoff_and_land") {


    use_exceptions = true
    output_name = "takeoff_and_land" # 可选,模块输出名

    include_dirs = [
        "src/third_party/mavlink/mavlink/src/mavlink/include",
        "src/mavsdk/core/include/mavsdk",
        "src/mavsdk/plugins/action/include",
        "src/mavsdk/plugins/telemetry/include"
    ]
    sources = [
        "examples/takeoff_and_land/takeoff_and_land.cpp",
        "examples/takeoff_and_land/autopilot_interface.cpp",
    ]



    deps = [
        "//third_party/MAVSDK:MAVSDK"
    ]

    part_name = "MAVSDK"

    subsystem_name = "MAVSDK"

}
#演示编译部分
ohos_executable("offboard") {  

    use_exceptions = true
    output_name = "offboard" # 可选,模块输出名

    include_dirs = [
        "src/third_party/mavlink/mavlink/src/mavlink/include",
        "src/mavsdk/core/include/mavsdk",
        "src/mavsdk/plugins/offboard/include",
        "src/mavsdk/plugins/action/include",
        "src/mavsdk/plugins/telemetry/include"
    ]
    sources = [
        "examples/offboard/offboard.cpp",
    ]

    deps= [
        "//third_party/MAVSDK:MAVSDK"
    ]

    part_name = "MAVSDK"

    subsystem_name = "MAVSDK"

}

#编译测试文件
ohos_unittest("mavsdk_test") {

    module_out_path = "MAVSDK/mavsdk_test" 

    testonly = true

    use_exceptions = true

    include_dirs = [ 
        "src/mavsdk/core/include/mavsdk",
        "src/mavsdk/plugins/action/include",
        "src/mavsdk/plugins/action_server/include",
        "src/mavsdk/plugins/calibration/include",
        "src/mavsdk/plugins/camera/include",
        "src/mavsdk/plugins/camera/camera_definition_files/generated",
        "src/mavsdk/plugins/component_information/include",
        "src/mavsdk/plugins/component_information_server/include",
        "src/mavsdk/plugins/failure/include",
        "src/mavsdk/plugins/follow_me/include",
        "src/mavsdk/plugins/ftp/include",
        "src/mavsdk/plugins/geofence/include",
        "src/mavsdk/plugins/gimbal/include",
        "src/mavsdk/plugins/info/include",
        "src/mavsdk/plugins/log_files/include",
        "src/mavsdk/plugins/manual_control/include",
        "src/mavsdk/plugins/mavlink_passthrough/include",
        "src/mavsdk/plugins/mission/include",
        "src/mavsdk/plugins/mission_raw/include",
        "src/mavsdk/plugins/mission_raw_server/include",
        "src/mavsdk/plugins/mocap/include",
        "src/mavsdk/plugins/offboard/include",
        "src/mavsdk/plugins/param/include",
        "src/mavsdk/plugins/param_server/include",
        "src/mavsdk/plugins/rtk/include",
        "src/mavsdk/plugins/server_utility/include",
        "src/mavsdk/plugins/shell/include",
        "src/mavsdk/plugins/telemetry/include",
        "src/mavsdk/plugins/telemetry_server/include",
        "src/mavsdk/plugins/tracking_server/include",
        "src/mavsdk/plugins/transponder/include",
        "src/mavsdk/plugins/tune/include",
        "src/mavsdk/core",
        "src/mavsdk/plugins/action",
        "src/mavsdk/plugins/action_server",
        "src/mavsdk/plugins/calibration",
        "src/mavsdk/plugins/camera",
        "src/mavsdk/plugins/component_information",
        "src/mavsdk/plugins/component_information_server",
        "src/mavsdk/plugins/failure",
        "src/mavsdk/plugins/follow_me",
        "src/mavsdk/plugins/ftp",
        "src/mavsdk/plugins/geofence",
        "src/mavsdk/plugins/gimbal",
        "src/mavsdk/plugins/info",
        "src/mavsdk/plugins/log_files",
        "src/mavsdk/plugins/manual_control",
        "src/mavsdk/plugins/mavlink_passthrough",
        "src/mavsdk/plugins/mission",
        "src/mavsdk/plugins/mission_raw",
        "src/mavsdk/plugins/mission_raw_server",
        "src/mavsdk/plugins/mocap",
        "src/mavsdk/plugins/offboard",
        "src/mavsdk/plugins/param",
        "src/mavsdk/plugins/param_server",
        "src/mavsdk/plugins/rtk",
        "src/mavsdk/plugins/server_utility",
        "src/mavsdk/plugins/shell",
        "src/mavsdk/plugins/telemetry",
        "src/mavsdk/plugins/telemetry_server",
        "src/mavsdk/plugins/tracking_server",
        "src/mavsdk/plugins/transponder",
        "src/mavsdk/plugins/tune",
        "src/third_party/mavlink",
        "src/third_party/curl/curl/src/curl/include",
        "src/third_party/install/include",
        "src/third_party/mavlink/mavlink/src/mavlink/include/mavlink/v2.0",
        "src/third_party/mavlink/mavlink/src/mavlink/include/mavlink/v2.0/common",

         ]


    sources = [
        "src/mavsdk/core/mavsdk_time_test.cpp" ,
        "src/mavsdk/core/mavsdk_math_test.cpp" ,
        "src/mavsdk/core/mavlink_channels_test.cpp" ,
        "src/mavsdk/core/unittests_main.cpp" ,
        "src/mavsdk/core/timeout_handler_test.cpp" ,
        "src/mavsdk/core/call_every_handler_test.cpp" ,
        "src/mavsdk/core/curl_test.cpp" ,
        "src/mavsdk/core/cli_arg_test.cpp" ,
        "src/mavsdk/core/locked_queue_test.cpp" ,
        "src/mavsdk/core/safe_queue_test.cpp" ,
        "src/mavsdk/core/mavsdk_test.cpp" ,
        "src/mavsdk/core/mavlink_mission_transfer_test.cpp" ,
        "src/mavsdk/core/mavlink_statustext_handler_test.cpp" ,
        "src/mavsdk/core/geometry_test.cpp" ,
        "src/mavsdk/core/ringbuffer_test.cpp" ,
        "src/mavsdk/plugins/calibration/calibration_statustext_parser_test.cpp" ,
        "src/mavsdk/plugins/camera/camera_definition_test.cpp" ,
        "src/mavsdk/plugins/mission_raw/mission_import_test.cpp" ,
        "src/mavsdk/plugins/telemetry/math_conversions_test.cpp"
    ]

    deps = [
        "//third_party/MAVSDK:MAVSDK",
        "//third_party/googletest:gmock_main",
        "//third_party/googletest:gtest_main"
    ]
    part_name = "MAVSDK"
}

通过以上操作以后,就可以在 OpenHarmony 编译源码了:

./build.sh --product-name rpi4 --build-target=MAVSDK --ccache

编译完成以后可以在 out/rpi4/mavsdk/MAVSDK 目录下看到编译生成的 libmavsdk.z.so文件。

同时,还可以在ohos/out/rpi4/tests/unittest/MAVSDK/mavsdk_test/mavsdk_test目录下看到我们编译的测试文件,如下所示:

image.png

image.png

接下来把编译完成的so及测试文件到RPI4开发板中,运行测试文件验证整个库的移植情况。

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部