【发布时间】:2013-12-03 19:57:02
【问题描述】:
当我使用我的自定义固件时,我注意到我的 DIY 无人机板有一个奇怪的行为。 这是我在 Arduino 板上运行的固件中调用的示例函数:
void send_attitude(float roll, float pitch, float yaw) {
hal.console->printf("{\"type\":\"sens_attitude\",\"roll\":%.4f,\"pitch\":%.4f,\"yaw\":%.4f}\n",
roll, pitch, yaw);
}
如您所见,代码只是在 setup (hal.uartA) 中设置的串行端口中写入一条消息。 我每 0.5 秒调用一次这个函数:
inline void medium_loop() {
static int timer = 0;
int time = hal.scheduler->millis() - timer;
// send every 0.5 s
if(time > 500) {
send_attitude(OUT_PIT, OUT_ROL, OUT_YAW);
timer = hal.scheduler->millis();
}
}
现在来说奇怪的事情。如果我使用串行监视器或使用另一个程序或脚本读取板,一切都很好。每隔 0.5 秒,相应的 LED 会闪烁并显示消息。但如果我不把它读出来,appr 之后。 10 秒 LED 持续亮起,无法再进行连接/通信。那我得把板子拔掉。反过来观察到相同的行为。如果我通过串行端口(在我的情况下为 USB)发送到我的电路板并且不刷新输入缓冲区,则 LED 会持续刷新并且我会超时。以下代码有效:
def send_data(line):
# calc checksum
chk = chksum(line)
# concatenate msg and chksum
output = "%s*%x\r\n" % (line, chk)
try:
bytes = ser.write(output)
except serial.SerialTimeoutException as e:
logging.error("Write timeout on serial port '{}': {}".format(com_port, e))
# Flush input buffer, if there is still some unprocessed data left
# Otherwise the APM 2.5 control boards stucks after some command
ser.flush() # Try to send old message
ser.flushInput() # Delete what is still inside the buffer
如果我注释掉这一行:
ser.flushInput() # Delete what is still inside the buffer
我不使用更多设置然后这个。 我迟早会(取决于消息间隔)超时。在我的情况下,我每 20 毫秒发送一个信号,导致约 10 秒后超时。还取决于消息的长度。较大的消息比较小的消息更快。
我的设置显示在下面的 sn-ps 中。客户端python代码:
com_port = '/dev/ttyACM0'
baud_rate = '115200'
try:
ser = serial.Serial(com_port, baud_rate, timeout=0.1, writeTimeout=0.1, rtscts=1)
如果这些超时发生,那么如果我将超时设置为 2 秒,我也会得到一个。就我而言,我需要非常低的延迟,如果我继续阅读和刷新,这确实是可能的。来自我的 Arduino 的固件代码:
void setup() {
// Set baud rate when connected to RPi
hal.uartA->begin(115200);
hal.console->printf("Setup device ..\n");
// Followed by motor, compass, barometer initialization
我的问题是:
我的电路板到底发生了什么?
如果我只在串行端口中写入而不读取或刷新缓冲区,为什么它不再反应?
这真的是与这种奇怪行为相关的缓冲区或驱动程序问题吗?这个问题是否与所有 Arduino 板有关,或者可能只是我从 DIY 无人机上的 APM 2.5?
最后但同样重要的是:我在库中没有找到针对此类问题的函数。有没有我不知道的?
完整源码为@google 代码:https://code.google.com/p/rpicopter/source/browse/
【问题讨论】:
-
你能粘贴你的串口参数deceleration(baud, parity, timeout....)吗?
-
我编辑了我的问题,以回答这个问题
-
你有什么板子?请尽量详细说明所有技术细节并提供组件链接。
标签: python c serial-port usb arduino