• Stars
    star
    236
  • Rank 170,480 (Top 4 %)
  • Language
    C++
  • Created about 12 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple MAVLink to UART interface example for *nix systems

C-UART Interface Example

This is a simple MAVLink to UART interface example for *nix systems that can allow communication between Pixhawk and an offboard computer.

This example will receive one MAVLink message and send one MAVLink message.

Building

$ cd c_uart_interface_example/
$ make

Hardware Setup

Connect the USB programming cable to your Pixhawk.

If you want to be able to interact with this example in Pixhawk's NuttX shell, you'll need a Telemetry Radio or an FTDI developer's cable. See the Exploration section below for more detail.

Also Note: Using a UART (serial) connection should be preferred over using the USB port for flying systems. The reason being that the driver for the USB port is much more complicated, so the UART is a much more trusted port for flight-critical functions. To learn how this works though the USB port will be fine and instructive.

Execution

You have to pick a port name, try searching for it with


$ ls /dev/ttyACM* 
$ ls /dev/ttyUSB*

Alternatively, plug in Pixhawk USB cable again and issue the command:

$ dmesg

The device described at the bottom of dmesg's output will be the port on which the Pixhawk is mounted.

The Pixhawk USB port will show up on a ttyACM*, an FTDI cable will show up on a ttyUSB*.

Run the example executable on the host shell:

$ cd c_uart_interface_example/
$ ./mavlink_control -d /dev/ttyACM0

To stop the program, use the key sequence Ctrl-C.

Here's an example output:

OPEN PORT
Connected to /dev/ttyUSB0 with 57600 baud, 8 data bits, no parity, 1 stop bit (8N1)

START READ THREAD 

CHECK FOR HEARTBEAT
Found

GOT VEHICLE SYSTEM ID: 1
GOT AUTOPILOT COMPONENT ID: 50

INITIAL POSITION XYZ = [ 8.2935 , -1.1447 , -0.7609 ] 
INITIAL POSITION YAW = 2.1539 

START WRITE THREAD 

ENABLE OFFBOARD MODE

SEND OFFBOARD COMMANDS
POSITION SETPOINT XYZ = [ 3.2935 , -6.1447 , -0.7609 ] 
POSITION SETPOINT YAW = 2.1539 
0 CURRENT POSITION XYZ = [  8.2935 , -1.1447 , -0.7609 ] 
1 CURRENT POSITION XYZ = [  8.2935 , -1.1447 , -0.7609 ] 
2 CURRENT POSITION XYZ = [  8.2524 , -1.1444 , -0.7667 ] 
3 CURRENT POSITION XYZ = [  8.2205 , -1.1431 , -0.7747 ] 
4 CURRENT POSITION XYZ = [  8.1920 , -1.1421 , -0.7737 ] 
5 CURRENT POSITION XYZ = [  8.1920 , -1.1421 , -0.7737 ] 
6 CURRENT POSITION XYZ = [  8.1539 , -1.1414 , -0.7847 ] 
7 CURRENT POSITION XYZ = [  8.1522 , -1.1417 , -0.7820 ] 

DISABLE OFFBOARD MODE

READ SOME MESSAGES 
Got message LOCAL_POSITION_NED (spec: https://mavlink.io/en/messages/common.html#LOCAL_POSITION_NED)
    pos  (NED):  8.152975 -1.141093 -0.784075 (m)
Got message HIGHRES_IMU (spec: https://mavlink.io/en/messages/common.html#HIGHRES_IMU)
    ap time:     3611390110 
    acc  (NED):   0.005503  0.044659 -9.740363 (m/s^2)
    gyro (NED):  -0.003064  0.003857  0.000005 (rad/s)
    mag  (NED):  -0.117767 -0.335362 -0.253204 (Ga)
    baro:        1020.519958 (mBar) 
    altitude:    -60.341393 (m) 
    temperature: 46.779999 C 

CLOSE THREADS

CLOSE PORT

Exploration

There are a few things to explore past this example.

First you can connect via:

Note

  • Serial 5 can't be used to receive data without reconfiguration (its receive pin is occupied by a second NuttX shell).
  • TELEM1 is typically dedicated to Telemetry Radio, but if you're using another port you will need to make sure it is not configured for use by another peripheral.
  • If using FTDI with a TELEM port, connect all the pins to corresponding pins on port.
  • If using FTDI with SERIAL4 connect the TX, RX GND and 5V pins (CTS, RTS need not be connected).

With this you'll be able to start a second port for communication, and leave the USB port available for viewing prints in the NuttX shell.

For steps 2 and 3 from the above tutorial, you'll use a different port. On the off-board computer side, the port might now be /dev/ttyUSB0. On the Pixhawk side, here the port mappings are in the table below.

PX4 UART NuttX UART
Telem 1 /dev/ttyS1
Telem 2 /dev/ttyS2
Serial 4 /dev/ttyS6

Now add a print statement in the Pixhawk Firmware to see received messages. Build and upload this to Pixhawk.

[Firmware/src/modules/mavlink/mavlink_receiver.cpp]
/* if read failed, this loop won't execute */
for (ssize_t i = 0; i < nread; i++) {
	if (mavlink_parse_char(_mavlink->get_channel(), buf[i], &msg, &status)) {

		/* --- REPORT HANDLING OF MESSAGE --- */
		printf("\n");
		printf("HANDLE MESSAGE\n");
		printf("MSGID:%i\n",msg.msgid);

		/* handle generic messages and commands */
		handle_message(&msg);

Open the system terminal as described here: https://dev.px4.io/en/debug/system_console.html

On the off-board side, in another terminal run the c_uart_interface_example executable. You should see output in the NuttX shell similar to this:

HANDLE MESSAGE
MSGID:76

HANDLE MESSAGE
MSGID:84

(...)

HANDLE MESSAGE
MSGID:84

HANDLE MESSAGE
MSGID:76

Past this, you can:

  • Modify the received message data type
  • Modify the sent message data type

Simulation

There is also the possibility to connect this example to the simulator using:

$ ./mavlink_control -u 127.0.0.1 -a

The -a argument enables arming, takeoff and landing of the copter. Use this argument with care on a real copter!

More Repositories

1

qgroundcontrol

Cross-platform ground control station for drones (Android, iOS, Mac OS, Linux, Windows)
C++
3,232
star
2

mavlink

Marshalling / communication library for drones.
Python
1,703
star
3

mavros

MAVLink to ROS gateway with proxy for Ground Control Station
C++
752
star
4

MAVSDK

API and library for MAVLink compatible systems written in C++17
C++
607
star
5

MAVSDK-Python

MAVSDK client for Python.
Python
308
star
6

c_library_v2

Official reference C / C++ library for the v2 protocol
C
163
star
7

rust-mavlink

MAVLink library for Rust.
Rust
127
star
8

mavlink-devguide

MAVLink Developer Guide
HTML
98
star
9

mavlink-camera-manager

MAVLink Camera Manager Service
Rust
87
star
10

c_library_v1

MAVLink protocol C/C++ implementation auto-generated from latest protocol specs.
C
77
star
11

MAVSDK-Java

MAVSDK client for Java.
Jinja
70
star
12

mavlink2rest

mavlink2rest creates a REST server that provides mavlink information from a mavlink source
Rust
57
star
13

qgc-user-guide

QGroundControl User Guide (Gitbook source)
CSS
42
star
14

qgc-dev-guide

QGroundControl Developers Guide
41
star
15

MAVSDK-Proto

Collection of proto files used by gRPC in MAVSDK
Python
36
star
16

MAVSDK-Swift

MAVSDK client for Swift.
Swift
25
star
17

MAVSDK-JavaScript

JS wrapper for MAVSDK using grpc-web to generate a static http client, communicating through the Envoy proxy.
JavaScript
24
star
18

MAVSDK-docs

MAVSDK Guide Docs - Source Code
23
star
19

mavlink-gbp-release

git-buildpackage repository for releasing mavlink as 3-rd party library for ROS
CMake
21
star
20

MAVSDK-Rust

MAVSDK client for Rust. https://mavsdk.mavlink.io
Rust
16
star
21

MAVSDK-CSharp

MAVSDK client for C#. https://mavsdk.mavlink.io
C#
16
star
22

MAVSDK-Go

Go
15
star
23

containers

Docker image for building QGroundControl
Shell
15
star
24

MAVSDK-Swift-Example

Example app using MAVSDK for iOS (Swift)
Swift
14
star
25

libevents

C++
11
star
26

rfcs

Requests for Comment for MAVLink protocol spec
9
star
27

mavros-release

Bloom release repository for mavros.
7
star
28

mavlink-ivy-interface

MAVLink to IVY Bus Interface
C
6
star
29

docs.qgroundcontrol.com

QGroundControl User Guide Content: See https://github.com/mavlink/qgc-user-guide
HTML
5
star
30

dev.qgroundcontrol.com

QGroundControl Developers Guide Content: See https://github.com/mavlink/qgc-dev-guide
HTML
2
star
31

mavsdk.mavlink.io

Mavlink SDK guide
HTML
2
star
32

mavlink.io

Mavlink Developer Guide Content: See https://github.com/mavlink/mavlink-devguide
HTML
2
star
33

homebrew-mavsdk

Homebrew repository for MAVSDK
Ruby
1
star
34

MAVSDK-XCFramework

Swift
1
star