Skip to content

mavsdk-java控制失败 #221

@ButyAfter

Description

@ButyAfter

第一步:make px4_sitl gz_x500 运行起来仿真环境了

linjl@linjl-virtual-machine:~/lin/PX4-Autopilot$ sudo make px4_sitl gz_x500
[sudo] linjl 的密码:
[0/1] cd /home/linjl/lin/PX4-Autopilot/build/px4_sitl_default/src/modules/simulat...x500 GZ_IP=127.0.0.1 /home/linjl/lin/PX4-Autopilot/build/px4_sitl_default/bin/px4


| ___ \ \ \ / / / |
| |/ / \ V / / /| |
| __/ / \ / /
| |
| | / /^\ \ ___ |
_| / / |_/

px4 starting.

INFO [px4] startup script: /bin/sh etc/init.d-posix/rcS 0
INFO [init] found model autostart file as SYS_AUTOSTART=4001
INFO [param] selected parameter default file parameters.bson
INFO [param] importing from 'parameters.bson'
INFO [parameters] BSON document size 333 bytes, decoded 333 bytes (INT32:14, FLOAT:3)
INFO [param] selected parameter backup file parameters_backup.bson
INFO [dataman] data manager file './dataman' size is 1208528 bytes
INFO [init] Gazebo simulator 8.9.0
INFO [init] Starting gazebo with world: /home/linjl/lin/PX4-Autopilot/Tools/simulation/gz/worlds/default.sdf
INFO [init] Starting gz gui
INFO [init] Waiting for Gazebo world...
INFO [init] Gazebo world is ready
INFO [init] Spawning Gazebo model
Warning [Utils.cc:132] [/sdf/model[@name="x500_base"]/link[@name="base_link"]/sensor[@name="air_pressure_sensor"]/gz_frame_id:/home/linjl/lin/PX4-Autopilot/Tools/simulation/gz/models/x500_base/model.sdf:L219]: XML Element[gz_frame_id], child of element[sensor], not defined in SDF. Copying[gz_frame_id] as children of [sensor].
Warning [Utils.cc:132] [/sdf/model[@name="x500_base"]/link[@name="base_link"]/sensor[@name="magnetometer_sensor"]/gz_frame_id:/home/linjl/lin/PX4-Autopilot/Tools/simulation/gz/models/x500_base/model.sdf:L233]: XML Element[gz_frame_id], child of element[sensor], not defined in SDF. Copying[gz_frame_id] as children of [sensor].
Warning [Utils.cc:132] [/sdf/model[@name="x500_base"]/link[@name="base_link"]/sensor[@name="imu_sensor"]/gz_frame_id:/home/linjl/lin/PX4-Autopilot/Tools/simulation/gz/models/x500_base/model.sdf:L259]: XML Element[gz_frame_id], child of element[sensor], not defined in SDF. Copying[gz_frame_id] as children of [sensor].
Warning [Utils.cc:132] [/sdf/model[@name="x500_base"]/link[@name="base_link"]/sensor[@name="navsat_sensor"]/gz_frame_id:/home/linjl/lin/PX4-Autopilot/Tools/simulation/gz/models/x500_base/model.sdf:L311]: XML Element[gz_frame_id], child of element[sensor], not defined in SDF. Copying[gz_frame_id] as children of [sensor].
INFO [gz_bridge] world: default, model: x500_0
INFO [lockstep_scheduler] setting initial absolute time to 2520000 us
INFO [commander] LED: open /dev/led0 failed (22)
WARN [health_and_arming_checks] Preflight Fail: ekf2 missing data
WARN [health_and_arming_checks] Preflight Fail: No connection to the GCS
INFO [tone_alarm] home set
INFO [uxrce_dds_client] init UDP agent IP:127.0.0.1, port:8888
INFO [mavlink] mode: Normal, data rate: 4000000 B/s on udp port 18570 remote port 14550
INFO [mavlink] mode: Onboard, data rate: 4000000 B/s on udp port 14580 remote port 14540
INFO [mavlink] mode: Onboard, data rate: 4000 B/s on udp port 14280 remote port 14030
INFO [mavlink] mode: Gimbal, data rate: 400000 B/s on udp port 13030 remote port 13280
INFO [logger] logger started (mode=all)
INFO [logger] Start file log (type: full)
INFO [logger] [logger] ./log/2025-09-15/10_25_45.ulg
INFO [logger] Opened full log file: ./log/2025-09-15/10_25_45.ulg
INFO [mavlink] MAVLink only on localhost (set param MAV_{i}BROADCAST = 1 to enable network)
INFO [mavlink] MAVLink only on localhost (set param MAV
{i}_BROADCAST = 1 to enable network)
INFO [px4] Startup script returned successfully
pxh> WARN [health_and_arming_checks] Preflight Fail: No connection to the GCS
WARN [health_and_arming_checks] Preflight Fail: No connection to the GCS

第二步:运行mavsdk_server

Image

第三步:查看mavlink实例

Image

可以看到 mavsdk_server_musl_x86_64 应该是和仿真环境连接上了

前三步都是在VMware虚拟机中运行的

第四步:根据mavsdk编写Java程序,使用官方示例

https://github.com/mavlink/MAVSDK-Java/blob/main/examples/java-client/src/main/java/io/mavsdk/example/TakeoffAndLand.java

public class UAVClient {

public static void main(String[] args) {
    // 先创建 System 对象
    io.mavsdk.System drone = new io.mavsdk.System("192.168.245.137",50051);

    CountDownLatch latch = new CountDownLatch(1);


    drone.getAction().arm()
            .doOnComplete(() -> System.out.println("Arming..."))
            .doOnError(throwable -> System.out.println("Failed to arm: "
                    + ((Action.ActionException) throwable).getCode()))
            .andThen(drone.getAction().takeoff()
                    .doOnComplete(() -> System.out.println("Taking off..."))
                    .doOnError(throwable -> System.out.println("Failed to take off: "
                            + ((Action.ActionException) throwable).getCode())))
            .delay(5, TimeUnit.SECONDS)
            .andThen(drone.getAction().land()
                    .doOnComplete(() -> System.out.println("Landing..."))
                    .doOnError(throwable -> System.out.println("Failed t" +
                            "o land: "
                            + ((Action.ActionException) throwable).getCode())))
            .subscribe(latch::countDown, throwable -> latch.countDown());

    try {
        latch.await();
    } catch (InterruptedException ignored) {
        // This is expected
    }
}

}

结果:

Java程序没有任何打印信息,阻塞住了:

Image Image

打印了这个日志后,无人机没有任何反应:

Image

有谁懂吗,帮忙看看

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions