|
1 | 1 | # MAVLink C UDP Example |
2 | 2 |
|
3 | | -The [MAVLink UDP Example](https://github.com/mavlink/mavlink/tree/master/examples/c) is a simple C example that sends some data to _QGroundControl_ using MAVLink over UDP. |
4 | | -_QGroundControl_ responds with heartbeats and other messages, which are then printed by this program. |
| 3 | +The [MAVLink UDP Example](https://github.com/mavlink/mavlink/tree/master/examples/c) is a simple C example that sends and receives MAVLink HEARTBEATS over UDP. |
5 | 4 |
|
6 | 5 | :::info |
7 | 6 | The example should work on any Unix-like system (Linux, MacOS, BSD, etc.). |
8 | | -These instructions were tested on a clean _Ubuntu LTS 20.04_ installation using the default version of _gcc_ (9.3.0). |
| 7 | +These instructions were tested on a _Ubuntu LTS 22.04_ installation with either PX4 or ArduPilot dependencies installed (such as cmake). |
9 | 8 | ::: |
10 | 9 |
|
11 | 10 | ## Building/Running the Example |
12 | 11 |
|
13 | 12 | The following instructions show how to build and run the example. |
14 | 13 |
|
15 | | -1. [Install MAVLink](../getting_started/installation.md) and [generate](../getting_started/generate_libraries.md) the MAVLink 2.0 libraries into the **mavlink/include** directory. |
16 | | - For example, to generate the headers for common.xml you could use the command line: |
| 14 | +1. Clone the [mavlink/mavlink](https://github.com/mavlink/mavlink/) repository |
17 | 15 |
|
18 | | - ```sh |
19 | | - python3 -m pymavlink.tools.mavgen --lang=C --wire-protocol=2.0 --output=./include/ message_definitions/v1.0/common.xml |
20 | | - ``` |
21 | | - |
22 | | - ::: tip |
23 | | - Alternatively you can clone the [mavlink/mavlink](https://github.com/mavlink/mavlink/) repository and [Download prebuilt headers](../index.md#prebuilt_libraries) to the same location. |
24 | | - ::: |
25 | | - |
26 | | - ::: info |
27 | | - The example will not work with MAVLink 1 because it uses a message that includes extension fields which do not exist in MAVLink 1 (`SYS_STATUS`). |
28 | | - ::: |
| 16 | +2. Open a terminal in the repository root. |
29 | 17 |
|
30 | | - ::: info |
31 | | - You can put/generate the library wherever you like, but the build command below assumes they are located in directory named **include** below the MAVLink root directory. |
32 | | - ::: |
| 18 | +3. Use `cmake` to install MAVLink locally: |
33 | 19 |
|
34 | | -2. Open a terminal and navigate to [examples/c](https://github.com/mavlink/mavlink/tree/master/examples/c) |
| 20 | + ```sh |
| 21 | + cmake -Bbuild -H. -DCMAKE_INSTALL_PREFIX=install |
| 22 | + cmake --build build --target install |
| 23 | + ``` |
35 | 24 |
|
36 | | -3. Compile with GCC using the following command: |
| 25 | +4. Navigate to [examples/c](https://github.com/mavlink/mavlink/tree/master/examples/c) |
37 | 26 |
|
38 | 27 | ```sh |
39 | | - gcc -std=c99 -I ../../include/common -o mavlink_udp udp_example.c |
| 28 | + cd examples/c |
40 | 29 | ``` |
41 | 30 |
|
42 | | - ::: info |
43 | | - The MAVLink header directory must be added to the include path. |
44 | | - The path here assumes you are building the code from the example directory, and that have installed the headers in **mavlink/include**. |
45 | | - ::: |
46 | | - |
47 | | -4. Run the executable from the terminal: |
| 31 | +5. Use `cmake` to compile and build the example: |
48 | 32 |
|
49 | | - ```bash |
50 | | - ./mavlink_udp |
| 33 | + ```sh |
| 34 | + cmake -Bbuild -H. -DCMAKE_PREFIX_PATH=$(pwd)/../../install |
| 35 | + cmake --build build |
51 | 36 | ``` |
52 | 37 |
|
53 | | - By default, the example will listen for data on the localhost IP address, port 14551. |
54 | | - You can specify another IP address as a command line argument (use `./mavlink_udp --help` to see usage). |
| 38 | +6. Run the executable from the terminal: |
55 | 39 |
|
56 | | -5. Open _QGroundControl_ on the same machine. |
| 40 | + ```sh |
| 41 | + ./build/udp_example |
| 42 | + ``` |
57 | 43 |
|
58 | | - _QGroundControl_ immediately starts broadcasting its `HEARTBEAT` on port 14551. |
| 44 | + By default, the example will listen for data on the localhost IP address, port 14550. |
59 | 45 |
|
60 | | - ::: info |
61 | | - _QGroundControl_ returns data, but will not actually "connect" to the example (it will continue to display the message _Waiting for Vehicle Connection_). |
62 | | - ::: |
| 46 | +7. Open another terminal on the same machine and start either PX4 or ArduPilot. |
| 47 | + These publish to port 14550 on localhost by default. |
63 | 48 |
|
64 | | -6. The example should start displaying the received data in the terminal: |
| 49 | +8. The example should start displaying messages about sent and received HEARTBEAT messages in the terminal. |
| 50 | + The following output is displayed if you connect to PX4: |
65 | 51 |
|
66 | 52 | ```sh |
67 | | - ~/github/mavlink/examples/c$ ./mavlink_udp |
68 | | - Bytes Received: 17 |
69 | | - Datagram: fe 09 00 ff 00 00 00 00 00 00 06 08 c0 04 03 19 87 |
70 | | - Received packet: SYS: 255, COMP: 0, LEN: 9, MSG ID: 0 |
71 | | - |
72 | | - Bytes Received: 17 |
73 | | - Datagram: fe 09 01 ff 00 00 00 00 00 00 06 08 c0 04 03 f3 f9 |
74 | | - Received packet: SYS: 255, COMP: 0, LEN: 9, MSG ID: 0 |
75 | | - |
| 53 | + ~/github/mavlink/mavlink/examples/c$ ./build/udp_example |
| 54 | + |
| 55 | + Sent heartbeat |
| 56 | + Got heartbeat from PX4 autopilot |
| 57 | + Sent heartbeat |
| 58 | + Got heartbeat from PX4 autopilot |
| 59 | + Sent heartbeat |
| 60 | + Got heartbeat from PX4 autopilot |
| 61 | + Sent heartbeat |
| 62 | + Got heartbeat from PX4 autopilot |
| 63 | + Sent heartbeat |
| 64 | + Got heartbeat from PX4 autopilot |
76 | 65 | ... |
77 | 66 | ``` |
| 67 | + |
| 68 | +Note that the build and installation instructions are from [examples/c/README.md](https://github.com/mavlink/mavlink/blob/master/examples/c/README.md). |
0 commit comments