Learning ArduPilot - UARTs and the Console

Learning ArduPilot - UARTs and the Console

A lot of components in ArduPilot rely on UARTs. They are used for debug output, telemetry, GPS modules and more. Understanding how to talk to the UARTs via the HAL will help you understand a lot of ArduPilot code.

The 5 UARTs

The ArduPilot HAL currently defines 5 UARTs. The HAL itself doesn’t define any particular roles for these UARTs, but the other parts of ArduPilot assume they will be assigned particular functions

  • uartA - the console (usually USB, runs MAVLink telemetry)
  • uartB - the first GPS
  • uartC - primary telemetry (telem1 on Pixhawk, 2nd radio on APM2)
  • uartD - secondary telemetry (telem2 on Pixhawk)
  • uartE - 2nd GPS

If you are writing your own sketch using the ArduPilot HAL then you can use these UARTs for any purpose you like, but if possible you should try to use the above assignments as it will allow you to fit in more easily to existing code.

Some UARTs have dual roles. For example there is a parameter SERIAL2_PROTOCOL changes uartD from being used for MAVLink versus being used for Frsky telemetry.

Go and have a look at the libraries/AP_HAL/examples/UART_test example sketch. It prints a hello message to all 5 UARTs. Try it on your board and see if you can get all the outputs displaying using a USB serial adapter. Try changing the baudrate in the sketch.

Debug console

In addition to these 5 UARTs there is an additional debug console available on some platforms. You can tell if the platform you are on has a debug console by checking for the HAVE_OS_POSIX_IO macro, like this:

#if HAVE_OS_POSIX_IO
  ::printf("hello console\n");
#endif

If you have a board that does have HAVE_OS_POSIX_IO set (check that inAP_HAL/AP_HAL_Boards.h) then try adding some ::printf() and other stdio functions to the UART_test sketch.

Note that on some boards (eg. the Pixhawk) hal.console->printf() goes to a different place to ::printf(). On the Pixhawk a hal.console->printf() goes to the USB port whereas ::printf() goes to the dedicated debug console (which also runs the nsh shell for NuttX access).

UART Functions

Every UART has a number of basis IO functions available. The key functions are:

  • printf - formatted print
  • printf_P - formatted print with progmem string (saves memory on AVR boards)
  • println - print and line feed
  • write - write a bunch of bytes
  • read - read some bytes
  • available - check if any bytes are waiting
  • txspace - check how much outgoing buffer space is available
  • get_flow_control - check if the UART has flow control capabilities

Go and have a look at the declarations of each of these in AP_HAL and try them in UART_test.

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值