Skip to content

Latest commit

 

History

History

java-client

Java client

This project is meant to show examples using MAVSDK-Java, and is a good place to get started.

Prerequisites

MAVSDK-Java will connect to a running instance of mavsdk_server, that can be downloaded from the MAVSDK release page.

The example runs System drone = new System();, which will by default connect to mavsdk_server on port 50051.

Running mavsdk_server -p 50051 from the command line will show output similar to the following:

% ./mavsdk_server -p 50051                                                                        [0]
[10:09:52|Info ] MAVSDK version: 0.25.0-11-g5e25ce90 (mavsdk_impl.cpp:26)
[10:09:52|Debug] New: System ID: 0 Comp ID: 0 (mavsdk_impl.cpp:379)
[10:09:52|Info ] Server started (grpc_server.cpp:38)
[10:09:52|Info ] Server set to listen on 0.0.0.0:50051 (grpc_server.cpp:39)
[10:09:52|Info ] Waiting to discover system on udp://:14540... (connection_initiator.h:22)

The other requirement is to have a MAVLink drone (or simulator) running. You can get started with the simulation environment here. If using docker, an alternative is to run a headless gazebo instance, as described here:

$ docker run --rm -it jonasvautherin/px4-gazebo-headless

Running the examples

The examples can be run with the following commands:

./gradlew takeoffAndLand
./gradlew setRtlAltitude
./gradlew runMission

Note that running ./gradlew run will default to takeoffAndLand.

Playing with jshell, the Java REPL

It can be useful to start with the REPL to get a feeling of how MAVSDK-Java works. Try to start jshell from gradle (note: that may not work on all platforms):

./gradlew jshell

If everything goes well, you should now be in an interactive jshell, showing the prompt:

jshell>

Let's start by importing the Action plugin:

jshell> import io.mavsdk.action.Action

We can now instantiate an action object:

jshell> Action action = new Action()

And, given that mavsdk_server is running and connected to a (possibly simulated) drone, we can directly takeoff:

jshell> action.arm().andThen(action.takeoff()).subscribe()