概览
这几天一直在做一个小车打算做好了再往下写的,但是由于我两年没写stm32的程序了,写好程序还是很吃力的。再加上这几天要准备考科目三(考驾照好辛苦 T_T
)准备开学考试(没错!开学就要考试,还要考三门我没学过的课 T_T
)事比较多,就停更了两个星期,下一篇我也不清楚什么时候发不过有时间会一点一点地写。
在视觉识别中一般是用妙算或者其他迷你电脑作上位机完成复杂的识别功能,在识别到目标后通过串口向下位机传送命令指挥小车云台运动。
妙算或者其他使用linux系统的机器直接用DJI或者东南大学开源代码中的串口部分就可以了,使用windows系统的机器可以参考东北林大的开源代码中的串口部分。
DJI开源代码串口部分
大疆的开源代码中串口部分写的比较简洁,主要就四个函数。openPort、configurePort、sendXYZ和praseDatafromCar。
串口的操作其实和文件读写类似,或者说IO相关的操作其实都差不多,都是先获取文件描述符fd再使用read和write函数进行读写操作。
#include <stdio.h> // standard input / output functions
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitionss
int openPort(const char \* dev_name){
int fd; // file description for the serial port
fd = open(dev_name, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1){ // if open is unsucessful
printf("open\_port: Unable to open /dev/ttyS0. \n");
}
else {
fcntl(fd, F_SETFL, 0);
printf("port is open.\n");
}
return(fd);
}
int configurePort(int fd){ // configure the port
struct termios port_settings; // structure to store the port settings
cfsetispeed(&port_settings, B115200); // set baud rates
cfsetospeed(&port_settings, B115200);
port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &port_