ardupilot怎么定义端口?
Ardupilot 串口代码学习
1.usb串口初始化
void Copter::init_ardupilot()
{
//初始化USB------------------initialise serial port
serial_manager.init_console(); //usb终端初始化
}
1
2
3
4
5
void AP_SerialManager::init_console()
{
//初始化终端立即设定字节和波特率-----------initialise console immediately at default size and baud
state[0].uart = hal.uartA; // serial0, uartA, always console
state[0].uart->begin(AP_SERIALMANAGER_CONSOLE_BAUD, //波特率---
AP_SERIALMANAGER_CONSOLE_BUFSIZE_RX, //接收缓冲区---
AP_SERIALMANAGER_CONSOLE_BUFSIZE_TX); //发送缓冲区---
}
1
2
3
4
5
6
7
8
void UARTDriver::begin(uint_t b, uint_t rxS, uint_t txS)
{
thread_init(); //线程初始化
if (sdef.serial == nullptr)
{
return;
}
uint_t min_tx_buffer = ;
uint_t min_rx_buffer = ;
// on PX4 we have enough memory to have a larger transmit and
// receive buffer for all ports. This means we don't get delays
// while waiting to write GPS config packets
if (txS < min_tx_buffer)
{
txS = min_tx_buffer;
}
if (rxS < min_rx_buffer)
{
rxS = min_rx_buffer;
}
/
*allocate the read buffer
we allocate buffers before we successfully open the device as we
want to allocate in the early stages of boot, and cause minimum
thrashing of the heap once we are up. The ttyACM0 driver may not
connect for some time after boot
*/
while (_in_timer)
{
hal.scheduler->delay(1);
}
if (rxS != _readbuf.get_size())
{
_initialised = false;
_readbuf.set_size(rxS);
}
bool clear_buffers = false;
if (b != 0)
{
// clear buffers on baudrate change, but not on the console (which is usually USB)
if (_baudrate != b && hal.console != this)
{
clear_buffers = true;
}
_baudrate = b;
}
if (clear_buffers)
{
_readbuf.clear();
}
if (rx_bounce_buf == nullptr) {
rx_bounce_buf = (uint8_t *)hal.util->malloc_type(RX_BOUNCE_BUFSIZE, AP_HAL::Util::MEM_DMA_SAFE);
}
if (tx_bounce_buf == nullptr) {
tx_bounce_buf = (uint8_t *)hal.util->malloc_type(TX_BOUNCE_BUFSIZE, AP_HAL::Util::MEM_DMA_SAFE);
chVTObjectInit(&tx_timeout);
tx_bounce_buf_ready = true;
}
/
*allocate the write buffer
*/
while (_in_timer)
{
hal.scheduler->delay(1);
}
if (txS != _writebuf.get_size())
{
_initialised = false;
_writebuf.set_size(txS);
}
if (clear_buffers)
{
_writebuf.clear();
}
if (sdef.is_usb)
{
#ifdef HAVE_USB_SERIAL
/
** Initializes a serial-over-USB CDC driver.
*/
if (!_device_initialised)
{
sduObjectInit((SerialUSBDriver*)sdef.serial);
sduStart((SerialUSBDriver*)sdef.serial, &serusbcfg);
/
** Activates the USB driver and then the USB bus pull-up on D+.
* Note, a delay is inserted in order to not have to disconnect the cable
* after a reset.
*/
usbDisconnectBus(serusbcfg.usbp);
hal.scheduler->delay_microseconds();
usbStart(serusbcfg.usbp, &usbcfg);
usbConnectBus(serusbcfg.usbp);
_device_initialised = true;
}
#endif
} else
{
#if HAL_USE_SERIAL == TRUE
if (_baudrate != 0) {
bool was_initialised = _device_initialised;
//setup Rx DMA
if(!_device_initialised) {
if(sdef.dma_rx) {
rxdma = STM_DMA_STREAM(sdef.dma_rx_stream_id);
chSysLock();
bool dma_allocated = dmaStreamAllocate(rxdma,
, //IRQ Priority
(stm_dmaisr_t)rxbuff_full_irq,
(void *)this);
osalDbgAssert(!dma_allocated, "stream already allocated");
chSysUnlock();
#if defined(STMF7)
dmaStreamSetPeripheral(rxdma, &((SerialDriver*)sdef.serial)->usart->RDR);
#else
dmaStreamSetPeripheral(rxdma, &((SerialDriver*)sdef.serial)->usart->DR);
#endif // STMF7
}
if (sdef.dma_tx) {
// we only allow for sharing of the TX DMA channel, not the RX
// DMA channel, as the RX side is active all the time, so
// cannot be shared
dma_handle = new Shared_DMA(sdef.dma_tx_stream_id,
SHARED_DMA_NONE,
FUNCTOR_BIND_MEMBER(&UARTDriver::dma_tx_allocate, void, Shared_DMA *),
FUNCTOR_BIND_MEMBER(&UARTDriver::dma_tx_deallocate, void, Shared_DMA *));
}
_device_initialised = true;
}
sercfg.speed = _baudrate;
if (!sdef.dma_tx && !sdef.dma_rx) {
sercfg.cr1 = 0;
sercfg.cr3 = 0;
} else {
if (sdef.dma_rx) {
sercfg.cr1 = USART_CR1_IDLEIE;
sercfg.cr3 = USART_CR3_DMAR;
}
if (sdef.dma_tx) {
sercfg.cr3 |= USART_CR3_DMAT;
}
}
sercfg.cr2 = USART_CR2_STOP1_BITS;
sercfg.irq_cb = rx_irq_cb;
sercfg.ctx = (void*)this;
sdStart((SerialDriver*)sdef.serial, &sercfg);
if(sdef.dma_rx) {
//Configure serial driver to skip handling RX packets
//because we will handle them via DMA
((SerialDriver*)sdef.serial)->usart->CR1 &= ~USART_CR1_RXNEIE;
//Start DMA
if(!was_initialised) {
uint_t dmamode = STM_DMA_CR_DMEIE | STM_DMA_CR_TEIE;
dmamode |= STM_DMA_CR_CHSEL(STM_DMA_GETCHANNEL(sdef.dma_rx_stream_id,
sdef.dma_rx_channel_id));
dmamode |= STM_DMA_CR_PL(0);
dmaStreamSetMemory0(rxdma, rx_bounce_buf);
dmaStreamSetTransactionSize(rxdma, RX_BOUNCE_BUFSIZE);
dmaStreamSetMode(rxdma, dmamode | STM_DMA_CR_DIR_P2M |
STM_DMA_CR_MINC | STM_DMA_CR_TCIE);
dmaStreamEnable(rxdma);
}
}
}
#endif // HAL_USE_SERIAL
}
if (_writebuf.get_size() && _readbuf.get_size()) {
_initialised = true;
}
_uart_owner_thd = chThdGetSelfX();
// setup flow control
set_flow_control(_flow_control);
if (serial_num == 0 && _initialised) {
#ifndef HAL_STDOUT_SERIAL
// setup hal.console to take printf() output
vprintf_console_hook = hal_console_vprintf;
#endif
}
}
————————————————
版权声明:本文为CSDN博主「魔城烟雨」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:/lixiaoweimashixiao/article/details/
å¦ä½ä½¿ç¨âArduPilot-Arduino-1.0.3-windowsâç¼è¯ï¼ä¸ä¼ APMç¨åº
APMé£æ§ç¨åºæ¯å¼æºçï¼æ们å¯ä»¥å¯¹æä¸çæ¬çAPMé£æ§ç¨åºè¿è¡ä¿®æ¹åäºæ¬¡å¼åãå½å¯¹APMç¨åºäºæ¬¡å¼åå®æåå°±è¦å°ä¿®æ¹çAPMç¨åºç¼è¯ä¸ä¼ å°APMé£æ§æ¿åä¸ãä¸é¢å°±æ¯ä»ç»å¦ä½ä½¿ç¨âArduPilot-Arduino-1.0.3-windowsâç¼è¯ãä¸ä¼ APMç¨åºã
1,ä¸è½½APMé£æ§ç¨åºç¼è¯ç¯å¢ï¼IDEï¼ï¼ï¼âArduPilot-Arduino-1.0.3-windowsâï¼æ³¨æä¸è¦ä½¿ç¨å ¶ä»çæ¬çArduinoç¼è¯ç¯å¢ã
2ï¼ä¸è½½ä¸ä¸ªAPMé£æ§ç¨åºï¼ä¾å¦ï¼ArduPlane-2.ãå¦å¤âArduPilot-Arduino-1.0.3-windowsâæ件夹ä¸é£æ§ç¨åºæ件夹æ¾å¨ä¸ä¸ªæ件夹ä¸ï¼æ件夹çå½åæ好é½æ¯ç¨è±æã
3ï¼ä½¿ç¨âArduPilot-Arduino-1.0.3-windowsâæ¶ï¼æå¼è¯¥æ件夹ï¼ç´æ¥åå»âarduinoâå¾æ 使ç¨ï¼æ éå®è£ ãâArduPilot-Arduino-1.0.3-windowsâç设置ï¼
tool-->board: Arduino mega or mega ADK;
tool-->programmer: AVRISP mkII
ArduPilot-->: HAL board: Ardupilot mega 2.Xï¼ç°å¨ä½¿ç¨çAPM2.0-2.5-2.6çï¼
File-->reference-->Sketchbook é为è¦ç¼è¯çé£æ§ç¨åºæ件夹ï¼æ¯æ¬¡æ´æ¹åç¹OKï¼å¹¶å ³æArduino 1.0.3 åæå¼ï¼è¿æ¶åéFile-->Sketchbookä¸ä¸é¡¹è¿è¡ç¼è¯ã
ä¾å¦ï¼å¯¹File-->Sketchbookä¸ArduPlaneè¿è¡ç¼è¯ï¼ç¹å»å·¥å ·æ ä¸â对å·âå¾æ å¼å§ç¼è¯ï¼æ²¡æé误大约1åéå°±è½ç¼è¯ç»æãç¼è¯å®æä¹åå°APMéè¿æ°æ®çº¿è¿æ¥å°çµèä¸ï¼ç¼è¯æ¶å°±è¿æ¥ä¹å¯ä»¥ï¼ï¼éæ©Board-->serial-port éæ©APMçæ¥å£ãä¹åç¹å»å·¥å ·æ çâåå³ç®å¤´âå¾æ å°ç¼è¯å¥½çç¨åºåè¿APMï¼è¿æ ·å°±å®æäºä¸æ¬¡å¯¹APMé£æ§ç¨åºçå·æ°ã
ç°å¨çAPMé£æ§ç¨åºä¸ç»å åï¼ç¼è¯åç大å°å¾å®¹æè¶ è¿KBï¼KBæ»å å-8KBçBootloaderå ç¨å åï¼ï¼å½ç¼è¯å大äºKBæ¶ä¸è½ä¸ä¼ å°APMä¸å¦åä¼ç ´åAPMçbootloader.
如何使用"ArduPilot-Arduino-1.0.3-windows"编译,上传APM程序
APM飞控程序是开源的,我们可以对某一版本的波段大王源码大全APM飞控程序进行修改做二次开发。当对APM程序二次开发完成后就要将修改的elements和网页源码APM程序编译上传到APM飞控板子中。下面就是介绍如何使用“ArduPilot-Arduino-1.0.3-windows”编译、上传APM程序。
1,下载APM飞控程序编译环境(IDE)--“ArduPilot-Arduino-1.0.3-windows”,注意不要使用其他版本的Arduino编译环境。
2,下载一个APM飞控程序,例如:ArduPlane-2.。ie猫源码网另外“ArduPilot-Arduino-1.0.3-windows”文件夹与飞控程序文件夹放在一个文件夹中,文件夹的命名最好都是用英文。
3,使用“ArduPilot-Arduino-1.0.3-windows”时,泽宇发卡源码打开该文件夹,直接双击“arduino”图标使用,无需安装。“ArduPilot-Arduino-1.0.3-windows”的lua脚本破解源码设置:
tool--board: Arduino mega or mega ADK;
tool--programmer: AVRISP mkII
ArduPilot--: HAL board: Ardupilot mega 2.X(现在使用的APM2.0-2.5-2.6等)
File--reference--Sketchbook 选为要编译的飞控程序文件夹,每次更改后点OK,并关掉Arduino 1.0.3 再打开,这时再选File--Sketchbook中一项进行编译。
例如,对File--Sketchbook中ArduPlane进行编译,点击工具栏中“对号”图标开始编译,没有错误大约1分钟就能编译结束。编译完成之后将APM通过数据线连接到电脑上(编译时就连接也可以),选择Board--serial-port 选择APM的接口。之后点击工具栏的“向右箭头”图标将编译好的程序写进APM,这样就完成了一次对APM飞控程序的刷新。
现在的APM飞控程序不经删减,编译后的大小很容易超过KB(KB总内存-8KB的Bootloader占用内存),当编译后大于KB时不能上传到APM中否则会破坏APM的bootloader.
2024-11-30 13:01
2024-11-30 12:44
2024-11-30 12:21
2024-11-30 11:31
2024-11-30 11:07