Merge branch 'master' into win32
This commit is contained in:
@@ -5,6 +5,8 @@ SET(CMAKE_CXX_STANDARD 11)
|
|||||||
SET(CMAKE_BUILD_TYPE RELEASE)
|
SET(CMAKE_BUILD_TYPE RELEASE)
|
||||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
|
||||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPATH=\"\\\"${PROJECT_SOURCE_DIR}\\\"\"")
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPATH=\"\\\"${PROJECT_SOURCE_DIR}\\\"\"")
|
||||||
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D${CMAKE_SYSTEM_NAME}")
|
||||||
|
MESSAGE("${CMAKE_CXX_FLAGS}")
|
||||||
# Todo
|
# Todo
|
||||||
## 使用编译期固定选项,以略微提升性能。
|
## 使用编译期固定选项,以略微提升性能。
|
||||||
#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFIX_OPTIONS")
|
#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFIX_OPTIONS")
|
||||||
@@ -20,17 +22,28 @@ IF(CCACHE_FOUND)
|
|||||||
MESSAGE("< Use ccache for compiler >")
|
MESSAGE("< Use ccache for compiler >")
|
||||||
ENDIF()
|
ENDIF()
|
||||||
|
|
||||||
|
FILE(GLOB_RECURSE sourcefiles "others/src/*.cpp" "energy/src/*cpp" "armor/src/*.cpp")
|
||||||
|
ADD_EXECUTABLE(${BIN_NAME} main.cpp ${sourcefiles} )
|
||||||
|
|
||||||
|
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
|
||||||
|
MESSAGE(STATUS "current platform: Linux ")
|
||||||
|
FIND_PACKAGE(Eigen3 REQUIRED)
|
||||||
|
INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIR})
|
||||||
|
TARGET_LINK_LIBRARIES(${BIN_NAME} "${PROJECT_SOURCE_DIR}/others/libMVSDK.so")
|
||||||
|
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
|
||||||
|
MESSAGE(STATUS "current platform: Windows")
|
||||||
|
INCLUDE_DIRECTORIES("C:/Applications/eigen3")
|
||||||
|
TARGET_LINK_LIBRARIES(${BIN_NAME} "${PROJECT_SOURCE_DIR}/others/MVCAMSDK_X64.lib")
|
||||||
|
ELSE ()
|
||||||
|
MESSAGE(STATUS "Unsupport platform: ${CMAKE_SYSTEM_NAME}")
|
||||||
|
ENDIF()
|
||||||
FIND_PACKAGE(OpenCV 3 REQUIRED)
|
FIND_PACKAGE(OpenCV 3 REQUIRED)
|
||||||
FIND_PACKAGE(Threads)
|
FIND_PACKAGE(Threads)
|
||||||
|
|
||||||
INCLUDE_DIRECTORIES("C:\\Applications\\eigen3")
|
|
||||||
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/energy/include)
|
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/energy/include)
|
||||||
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/armor/include)
|
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/armor/include)
|
||||||
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/others/include)
|
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/others/include)
|
||||||
|
|
||||||
FILE(GLOB_RECURSE sourcefiles "others/src/*.cpp" "energy/src/*cpp" "armor/src/*.cpp")
|
|
||||||
ADD_EXECUTABLE(${BIN_NAME} main.cpp ${sourcefiles} )
|
|
||||||
|
|
||||||
TARGET_LINK_LIBRARIES(${BIN_NAME} ${CMAKE_THREAD_LIBS_INIT})
|
TARGET_LINK_LIBRARIES(${BIN_NAME} ${CMAKE_THREAD_LIBS_INIT})
|
||||||
TARGET_LINK_LIBRARIES(${BIN_NAME} ${OpenCV_LIBS})
|
TARGET_LINK_LIBRARIES(${BIN_NAME} ${OpenCV_LIBS})
|
||||||
TARGET_LINK_LIBRARIES(${BIN_NAME} "C:/Program\ Files\ (x86)/MindVision/SDK/X64/MVCAMSDK_X64.lib")
|
|
||||||
|
|||||||
@@ -68,11 +68,6 @@ bool sendTarget(Serial& serial, double x, double y, double z) {
|
|||||||
static int fps;
|
static int fps;
|
||||||
uint8_t buff[8];
|
uint8_t buff[8];
|
||||||
|
|
||||||
static SYSTEMTIME ts, te;
|
|
||||||
GetLocalTime(&te);
|
|
||||||
printf("Interval: %d\n", (te.wSecond - ts.wSecond) * 1000 + (te.wMilliseconds - ts.wMilliseconds));
|
|
||||||
ts = te;
|
|
||||||
|
|
||||||
time_t t = time(nullptr);
|
time_t t = time(nullptr);
|
||||||
if (last_time != t) {
|
if (last_time != t) {
|
||||||
last_time = t;
|
last_time = t;
|
||||||
|
|||||||
27
main.cpp
27
main.cpp
@@ -37,7 +37,7 @@ void uartReceive(Serial *uart);
|
|||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
process_options(argc, argv);
|
process_options(argc, argv);
|
||||||
Serial uart(3, 115200);
|
Serial uart(115200);
|
||||||
thread receive(uartReceive, &uart);
|
thread receive(uartReceive, &uart);
|
||||||
bool flag = true;
|
bool flag = true;
|
||||||
|
|
||||||
@@ -118,12 +118,11 @@ int main(int argc, char *argv[]) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define RECEIVE_LOG_LEVEL LOG_MSG
|
#define RECEIVE_LOG_LEVEL LOG_NOTHING
|
||||||
|
|
||||||
char uartReadByte(Serial &uart) {
|
char uartReadByte(Serial &uart) {
|
||||||
while (uart.GetBytesInCOM() == 0);
|
|
||||||
char byte;
|
char byte;
|
||||||
if (uart.ReadData((uint8_t*)& byte, 1) == false) {
|
if (uart.ReadData((uint8_t*)&byte, 1) == false) {
|
||||||
LOGE("serial error!");
|
LOGE("serial error!");
|
||||||
}
|
}
|
||||||
return byte;
|
return byte;
|
||||||
@@ -138,45 +137,45 @@ void uartReceive(Serial* uart) {
|
|||||||
while ((data = uartReadByte(*uart)) != '\n') {
|
while ((data = uartReadByte(*uart)) != '\n') {
|
||||||
buffer[cnt++] = data;
|
buffer[cnt++] = data;
|
||||||
if (cnt >= 100) {
|
if (cnt >= 100) {
|
||||||
//LOG(RECEIVE_LOG_LEVEL, "data receive over flow!");
|
LOG(RECEIVE_LOG_LEVEL, "data receive over flow!");
|
||||||
cnt = 0;
|
cnt = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//LOGM("%d", cnt);
|
LOGM("%d", cnt);
|
||||||
if (cnt == 12) {
|
if (cnt == 12) {
|
||||||
if (buffer[8] == 'e') {
|
if (buffer[8] == 'e') {
|
||||||
state = ENERGY_STATE;
|
state = ENERGY_STATE;
|
||||||
//LOG(RECEIVE_LOG_LEVEL, "Energy state");
|
LOG(RECEIVE_LOG_LEVEL, "Energy state");
|
||||||
} else if (buffer[8] == 'a') {
|
} else if (buffer[8] == 'a') {
|
||||||
state = ARMOR_STATE;
|
state = ARMOR_STATE;
|
||||||
//LOG(RECEIVE_LOG_LEVEL, "Armor state");
|
LOG(RECEIVE_LOG_LEVEL, "Armor state");
|
||||||
}
|
}
|
||||||
if (buffer[10] == 0){
|
if (buffer[10] == 0){
|
||||||
use_classifier = false;
|
use_classifier = false;
|
||||||
//LOG(RECEIVE_LOG_LEVEL, "Classifier off!");
|
LOG(RECEIVE_LOG_LEVEL, "Classifier off!");
|
||||||
} else if(buffer[10] == 1){
|
} else if(buffer[10] == 1){
|
||||||
use_classifier = true;
|
use_classifier = true;
|
||||||
//LOG(RECEIVE_LOG_LEVEL, "Classifier on!");
|
LOG(RECEIVE_LOG_LEVEL, "Classifier on!");
|
||||||
}
|
}
|
||||||
if (buffer[11] == ENEMY_BLUE) {
|
if (buffer[11] == ENEMY_BLUE) {
|
||||||
//LOG(RECEIVE_LOG_LEVEL, "ENEMY_BLUE!");
|
LOG(RECEIVE_LOG_LEVEL, "ENEMY_BLUE!");
|
||||||
ally_color = ALLY_RED;
|
ally_color = ALLY_RED;
|
||||||
enemy_color = ENEMY_BLUE;
|
enemy_color = ENEMY_BLUE;
|
||||||
} else if (buffer[11] == ENEMY_RED) {
|
} else if (buffer[11] == ENEMY_RED) {
|
||||||
//LOG(RECEIVE_LOG_LEVEL, "ENEMY_RED!");
|
LOG(RECEIVE_LOG_LEVEL, "ENEMY_RED!");
|
||||||
ally_color = ALLY_BLUE;
|
ally_color = ALLY_BLUE;
|
||||||
enemy_color = ENEMY_RED;
|
enemy_color = ENEMY_RED;
|
||||||
}
|
}
|
||||||
memcpy(&curr_yaw, buffer, 4);
|
memcpy(&curr_yaw, buffer, 4);
|
||||||
memcpy(&curr_pitch, buffer + 4, 4);
|
memcpy(&curr_pitch, buffer + 4, 4);
|
||||||
//LOG(RECEIVE_LOG_LEVEL, "Get yaw:%f pitch:%f", curr_yaw, curr_pitch);
|
LOG(RECEIVE_LOG_LEVEL, "Get yaw:%f pitch:%f", curr_yaw, curr_pitch);
|
||||||
if (buffer[9] == 1) {
|
if (buffer[9] == 1) {
|
||||||
if (mark == 0) {
|
if (mark == 0) {
|
||||||
mark = 1;
|
mark = 1;
|
||||||
mark_yaw = curr_yaw;
|
mark_yaw = curr_yaw;
|
||||||
mark_pitch = curr_pitch;
|
mark_pitch = curr_pitch;
|
||||||
}
|
}
|
||||||
//LOG(RECEIVE_LOG_LEVEL, "Marked");
|
LOG(RECEIVE_LOG_LEVEL, "Marked");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cnt = 0;
|
cnt = 0;
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ typedef unsigned char UCHAR;
|
|||||||
#define HMODULE void*
|
#define HMODULE void*
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define TRUE 1
|
#define TRUE 1
|
||||||
#define FALSE 0
|
#define FALSE 0
|
||||||
//图像查表变换的方式
|
//图像查表变换的方式
|
||||||
|
|||||||
@@ -14,7 +14,11 @@
|
|||||||
#include <opencv2/imgproc/imgproc.hpp>
|
#include <opencv2/imgproc/imgproc.hpp>
|
||||||
|
|
||||||
#include "camera/wrapper_head.h"
|
#include "camera/wrapper_head.h"
|
||||||
#include "camera/CameraApi.h"
|
#ifdef Windows
|
||||||
|
#include "camera/CameraApi.h"
|
||||||
|
#elif defined(Linux)
|
||||||
|
#include "camera/camera_api.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
class CameraWrapper: public WrapperHead {
|
class CameraWrapper: public WrapperHead {
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -124,8 +124,8 @@
|
|||||||
__FILE__, __LINE__, ##__VA_ARGS__)
|
__FILE__, __LINE__, ##__VA_ARGS__)
|
||||||
|
|
||||||
/******************** the time counter API ************************/
|
/******************** the time counter API ************************/
|
||||||
#ifndef DO_NOT_CNT_TIME && LOG_LEVEL > LOG_NONE
|
#if !defined(DO_NOT_CNT_TIME) && LOG_LEVEL > LOG_NONE
|
||||||
#ifdef WIN32
|
#ifdef Windows
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#define CNT_TIME(tag, codes, ...) do{ \
|
#define CNT_TIME(tag, codes, ...) do{ \
|
||||||
static SYSTEMTIME ts, te; \
|
static SYSTEMTIME ts, te; \
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#ifndef _ADDITIONS_H_
|
#ifndef _ADDITIONS_H_
|
||||||
#define _ADDITIONS_H_
|
#define _ADDITIONS_H_
|
||||||
|
|
||||||
#include <uart/uart.h>
|
#include <serial/serial.h>
|
||||||
#include <opencv2/core.hpp>
|
#include <opencv2/core.hpp>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
#ifndef _SERIAL_H_
|
#ifndef _SERIAL_H_
|
||||||
#define _SERIAL_H_
|
#define _SERIAL_H_
|
||||||
|
|
||||||
|
#ifdef Windows
|
||||||
|
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
class Serial
|
class Serial
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Serial(UINT portNo = 1, UINT baud = CBR_9600, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR);
|
Serial(UINT baud = CBR_115200, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR);
|
||||||
~Serial();
|
~Serial();
|
||||||
|
|
||||||
bool InitPort(UINT portNo = 1, UINT baud = CBR_9600, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR);
|
bool InitPort(UINT portNo = 1, UINT baud = CBR_9600, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR);
|
||||||
@@ -27,4 +29,33 @@ private:
|
|||||||
DWORD dwCommEvents;
|
DWORD dwCommEvents;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#elif defined(Linux)
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
class Serial {
|
||||||
|
private:
|
||||||
|
int fd;
|
||||||
|
int nSpeed;
|
||||||
|
char nEvent;
|
||||||
|
int nBits;
|
||||||
|
int nStop;
|
||||||
|
|
||||||
|
int set_opt(int fd, int nSpeed, char nEvent, int nBits, int nStop);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Serial(int nSpeed = 115200, char nEvent = 'N', int nBits = 8, int nStop = 1);
|
||||||
|
~Serial();
|
||||||
|
|
||||||
|
bool InitPort(int nSpeed = 115200, char nEvent = 'N', int nBits = 8, int nStop = 1);
|
||||||
|
// int GetBytesInCOM() const ;
|
||||||
|
bool WriteData(const unsigned char* pData, unsigned int length);
|
||||||
|
bool ReadData(unsigned char* buffer, unsigned int length);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* _SERIAL_H_ */
|
#endif /* _SERIAL_H_ */
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ bool CameraWrapper::init() {
|
|||||||
rgb_buffer = (unsigned char *)malloc(tCapability.sResolutionRange.iHeightMax *
|
rgb_buffer = (unsigned char *)malloc(tCapability.sResolutionRange.iHeightMax *
|
||||||
tCapability.sResolutionRange.iWidthMax * 3);
|
tCapability.sResolutionRange.iWidthMax * 3);
|
||||||
char filepath[200];
|
char filepath[200];
|
||||||
sprintf_s(filepath, PROJECT_DIR"/others/%s.Config", name.data());
|
sprintf(filepath, PROJECT_DIR"/others/%s.Config", name.data());
|
||||||
if (CameraReadParameterFromFile(h_camera, filepath) != CAMERA_STATUS_SUCCESS) {
|
if (CameraReadParameterFromFile(h_camera, filepath) != CAMERA_STATUS_SUCCESS) {
|
||||||
LOGE("Load parameter %s from file fail!", filepath);
|
LOGE("Load parameter %s from file fail!", filepath);
|
||||||
return false;
|
return false;
|
||||||
@@ -75,28 +75,6 @@ bool CameraWrapper::init() {
|
|||||||
double t;
|
double t;
|
||||||
CameraGetExposureTime(h_camera, &t);
|
CameraGetExposureTime(h_camera, &t);
|
||||||
LOGM("Exposure time: %lfms", t / 1000.0);
|
LOGM("Exposure time: %lfms", t / 1000.0);
|
||||||
/* if(mode == 0){
|
|
||||||
// 不使用自动曝光
|
|
||||||
CameraSetAeState(h_camera, false);
|
|
||||||
// 曝光时间10ms
|
|
||||||
CameraSetExposureTime(h_camera, 10000);
|
|
||||||
double t;
|
|
||||||
CameraGetExposureTime(h_camera, &t);
|
|
||||||
LOGM("Exposure time: %lfms", t/1000.0);
|
|
||||||
// 模拟增益4
|
|
||||||
CameraSetAnalogGain(h_camera, 64);
|
|
||||||
// 使用预设LUT表
|
|
||||||
CameraSetLutMode(h_camera, LUTMODE_PRESET);
|
|
||||||
// 抗频闪
|
|
||||||
// CameraSetAntiFlick(h_camera, true);
|
|
||||||
}
|
|
||||||
else if(mode == 1){
|
|
||||||
// 不使用自动曝光
|
|
||||||
CameraSetAeState(h_camera, false);
|
|
||||||
// 抗频闪
|
|
||||||
// CameraSetAntiFlick(h_camera, true);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
/*让SDK进入工作模式,开始接收来自相机发送的图像
|
/*让SDK进入工作模式,开始接收来自相机发送的图像
|
||||||
数据。如果当前相机是触发模式,则需要接收到
|
数据。如果当前相机是触发模式,则需要接收到
|
||||||
触发帧以后才会更新图像。 */
|
触发帧以后才会更新图像。 */
|
||||||
|
|||||||
@@ -4,9 +4,11 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
Serial::Serial(UINT portNo, UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) :
|
#ifdef Windows
|
||||||
|
|
||||||
|
Serial::Serial(UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) :
|
||||||
hComm(INVALID_HANDLE_VALUE),
|
hComm(INVALID_HANDLE_VALUE),
|
||||||
portNo(portNo),
|
portNo(3),
|
||||||
parity(parity),
|
parity(parity),
|
||||||
databits(databits),
|
databits(databits),
|
||||||
stopsbits(stopsbits),
|
stopsbits(stopsbits),
|
||||||
@@ -40,12 +42,12 @@ bool Serial::openPort(UINT portNo) {
|
|||||||
char szPort[50];
|
char szPort[50];
|
||||||
sprintf_s(szPort, "COM%d", portNo);
|
sprintf_s(szPort, "COM%d", portNo);
|
||||||
|
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD> */
|
/** 打开指定的串口 */
|
||||||
hComm = CreateFileA(szPort, /** <EFBFBD>豸<EFBFBD><EFBFBD>,COM1,COM2<EFBFBD><EFBFBD> */
|
hComm = CreateFileA(szPort, /** 设备名,COM1,COM2等 */
|
||||||
GENERIC_READ | GENERIC_WRITE, /** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ,<2C><>ͬʱ<CDAC><CAB1>д */
|
GENERIC_READ | GENERIC_WRITE, /** 访问模式,可同时读写 */
|
||||||
0, /** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ,0<><30>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
0, /** 共享模式,0表示不共享 */
|
||||||
NULL, /** <EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,һ<><D2BB>ʹ<EFBFBD><CAB9>NULL */
|
NULL, /** 安全性设置,一般使用NULL */
|
||||||
OPEN_EXISTING, /** <EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>豸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7> */
|
OPEN_EXISTING, /** 该参数表示设备必须存在,否则创建失败 */
|
||||||
0,
|
0,
|
||||||
0);
|
0);
|
||||||
|
|
||||||
@@ -53,7 +55,7 @@ bool Serial::openPort(UINT portNo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Serial::ClosePort() {
|
void Serial::ClosePort() {
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD> */
|
/** 如果有串口被打开,关闭它 */
|
||||||
if (hComm != INVALID_HANDLE_VALUE) {
|
if (hComm != INVALID_HANDLE_VALUE) {
|
||||||
CloseHandle(hComm);
|
CloseHandle(hComm);
|
||||||
hComm = INVALID_HANDLE_VALUE;
|
hComm = INVALID_HANDLE_VALUE;
|
||||||
@@ -61,7 +63,7 @@ void Serial::ClosePort() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Serial::InitPort(UINT portNo, UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) {
|
bool Serial::InitPort(UINT portNo, UINT baud, char parity, UINT databits, UINT stopsbits, DWORD dwCommEvents) {
|
||||||
/** <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʽ,<2C>Թ<EFBFBD><D4B9><EFBFBD>DCB<EFBFBD>ṹ */
|
/** 临时变量,将制定参数转化为字符串形式,以构造DCB结构 */
|
||||||
char szDCBparam[50];
|
char szDCBparam[50];
|
||||||
sprintf_s(szDCBparam, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopsbits);
|
sprintf_s(szDCBparam, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopsbits);
|
||||||
|
|
||||||
@@ -85,40 +87,40 @@ bool Serial::InitPort(UINT portNo, UINT baud, char parity, UINT databits, UINT
|
|||||||
|
|
||||||
DCB dcb;
|
DCB dcb;
|
||||||
if (bIsSuccess) {
|
if (bIsSuccess) {
|
||||||
/** <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ҹ<EFBFBD><D2B9>촮<EFBFBD><ECB4AE>DCB<43><42><EFBFBD><EFBFBD> */
|
/** 获取当前串口配置参数,并且构造串口DCB参数 */
|
||||||
bIsSuccess = GetCommState(hComm, &dcb);
|
bIsSuccess = GetCommState(hComm, &dcb);
|
||||||
bIsSuccess = BuildCommDCB(szDCBparam, &dcb);
|
bIsSuccess = BuildCommDCB(szDCBparam, &dcb);
|
||||||
if (!bIsSuccess) {
|
if (!bIsSuccess) {
|
||||||
|
|
||||||
cout << "Create dcb fail with "<< GetLastError() << endl;
|
cout << "Create dcb fail with "<< GetLastError() << endl;
|
||||||
}
|
}
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>RTS flow<EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 开启RTS flow控制 */
|
||||||
dcb.fRtsControl = RTS_CONTROL_ENABLE;
|
dcb.fRtsControl = RTS_CONTROL_ENABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bIsSuccess) {
|
if (bIsSuccess) {
|
||||||
/** ʹ<EFBFBD><EFBFBD>DCB<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD>״̬ */
|
/** 使用DCB参数配置串口状态 */
|
||||||
bIsSuccess = SetCommState(hComm, &dcb);
|
bIsSuccess = SetCommState(hComm, &dcb);
|
||||||
if (!bIsSuccess) {
|
if (!bIsSuccess) {
|
||||||
cout << "SetCommState error!" << endl;
|
cout << "SetCommState error!" << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 清空串口缓冲区 */
|
||||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
|
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
|
||||||
|
|
||||||
return bIsSuccess;
|
return bIsSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
UINT Serial::GetBytesInCOM() const {
|
UINT Serial::GetBytesInCOM() const {
|
||||||
DWORD dwError = 0; /** <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
DWORD dwError = 0; /** 错误码 */
|
||||||
COMSTAT comstat; /** COMSTAT<EFBFBD>ṹ<EFBFBD><EFBFBD>,<2C><>¼ͨ<C2BC><CDA8><EFBFBD>豸<EFBFBD><E8B1B8>״̬<D7B4><CCAC>Ϣ */
|
COMSTAT comstat; /** COMSTAT结构体,记录通信设备的状态信息 */
|
||||||
memset(&comstat, 0, sizeof(COMSTAT));
|
memset(&comstat, 0, sizeof(COMSTAT));
|
||||||
|
|
||||||
UINT BytesInQue = 0;
|
UINT BytesInQue = 0;
|
||||||
/** <EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD>ReadFile<EFBFBD><EFBFBD>WriteFile֮ǰ,ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>־ */
|
/** 在调用ReadFile和WriteFile之前,通过本函数清除以前遗留的错误标志 */
|
||||||
if (ClearCommError(hComm, &dwError, &comstat)) {
|
if (ClearCommError(hComm, &dwError, &comstat)) {
|
||||||
BytesInQue = comstat.cbInQue; /** <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뻺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD> */
|
BytesInQue = comstat.cbInQue; /** 获取在输入缓冲区中的字节数 */
|
||||||
}
|
}
|
||||||
|
|
||||||
return BytesInQue;
|
return BytesInQue;
|
||||||
@@ -130,13 +132,13 @@ bool Serial::WriteData(const unsigned char* pData, unsigned int length) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 向缓冲区写入指定量的数据 */
|
||||||
BOOL bResult = TRUE;
|
BOOL bResult = TRUE;
|
||||||
DWORD BytesToSend = 0;
|
DWORD BytesToSend = 0;
|
||||||
bResult = WriteFile(hComm, pData, length, &BytesToSend, NULL);
|
bResult = WriteFile(hComm, pData, length, &BytesToSend, NULL);
|
||||||
if (!bResult) {
|
if (!bResult) {
|
||||||
DWORD dwError = GetLastError();
|
DWORD dwError = GetLastError();
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 清空串口缓冲区 */
|
||||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
||||||
ErrorHandler();
|
ErrorHandler();
|
||||||
return false;
|
return false;
|
||||||
@@ -150,17 +152,17 @@ bool Serial::ReadData(unsigned char *buffer, unsigned int length) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** <EFBFBD>ӻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡlength<EFBFBD>ֽڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 从缓冲区读取length字节的数据 */
|
||||||
BOOL bResult = TRUE;
|
BOOL bResult = TRUE;
|
||||||
DWORD totalRead = 0, onceRead = 0;
|
DWORD totalRead = 0, onceRead = 0;
|
||||||
while (totalRead < length) {
|
while (totalRead < length) {
|
||||||
bResult = ReadFile(hComm, buffer, length-totalRead, &onceRead, NULL);
|
bResult = ReadFile(hComm, buffer, length-totalRead, &onceRead, NULL);
|
||||||
totalRead += onceRead;
|
totalRead += onceRead;
|
||||||
if ((!bResult)) {
|
if ((!bResult)) {
|
||||||
/** <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>Ը<EFBFBD><D4B8>ݸô<DDB8><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD> */
|
/** 获取错误码,可以根据该错误码查出错误原因 */
|
||||||
DWORD dwError = GetLastError();
|
DWORD dwError = GetLastError();
|
||||||
|
|
||||||
/** <EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
/** 清空串口缓冲区 */
|
||||||
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
|
||||||
ErrorHandler();
|
ErrorHandler();
|
||||||
return false;
|
return false;
|
||||||
@@ -168,3 +170,156 @@ bool Serial::ReadData(unsigned char *buffer, unsigned int length) {
|
|||||||
}
|
}
|
||||||
return bResult;
|
return bResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else defined(Linux)
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
string get_uart_dev_name(){
|
||||||
|
FILE* ls = popen("ls /dev/ttyUSB* --color=never", "r");
|
||||||
|
char name[20] = {0};
|
||||||
|
fscanf(ls, "%s", name);
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial::Serial(int nSpeed, char nEvent, int nBits, int nStop) :
|
||||||
|
nSpeed(nSpeed), nEvent(nEvent), nBits(nBits), nStop(nStop){
|
||||||
|
if(wait_uart){
|
||||||
|
LOGM("Wait for serial be ready!");
|
||||||
|
while(InitPort(nSpeed, nEvent, nBits, nStop) == false);
|
||||||
|
LOGM("Port set successfully!");
|
||||||
|
}else{
|
||||||
|
if(InitPort(nSpeed, nEvent, nBits, nStop)){
|
||||||
|
LOGM("Port set successfully!");
|
||||||
|
}else{
|
||||||
|
LOGE("Port set fail!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial::~Serial() {
|
||||||
|
close(fd);
|
||||||
|
fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Serial::InitPort(int nSpeed, char nEvent, int nBits, int nStop){
|
||||||
|
string name = get_uart_dev_name();
|
||||||
|
if(name == ""){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if((fd=open(name.data(), O_RDWR)) < 0){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(set_opt(fd, nSpeed, nEvent, nBits, nStop) < 0){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//int GetBytesInCOM() const {
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
||||||
|
bool Serial::WriteData(const unsigned char* pData, unsigned int length){
|
||||||
|
int cnt=0, curr=0;
|
||||||
|
while((curr=write(fd, pData+cnt, length-cnt))>0 && (cnt+=curr)<length);
|
||||||
|
if(cnt < 0){
|
||||||
|
LOGE("Serial offline!");
|
||||||
|
close(fd);
|
||||||
|
if(wait_uart){
|
||||||
|
InitPort(nSpeed, nEvent, nBits, nStop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Serial::ReadData(unsigned char* buffer, unsigned int length){
|
||||||
|
int cnt=0, curr=0;
|
||||||
|
while((curr=read(fd, buffer+cnt, length-cnt))>0 && (cnt+=curr)<length);
|
||||||
|
if(cnt < 0){
|
||||||
|
LOGE("Serial offline!");
|
||||||
|
close(fd);
|
||||||
|
if(wait_uart){
|
||||||
|
InitPort(nSpeed, nEvent, nBits, nStop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Serial::set_opt(int fd, int nSpeed, char nEvent, int nBits, int nStop) {
|
||||||
|
termios newtio{}, oldtio{};
|
||||||
|
if (tcgetattr(fd, &oldtio) != 0) {
|
||||||
|
perror("SetupSerial 1");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
bzero(&newtio, sizeof(newtio));
|
||||||
|
newtio.c_cflag |= CLOCAL | CREAD;
|
||||||
|
newtio.c_cflag &= ~CSIZE;
|
||||||
|
|
||||||
|
switch (nBits) {
|
||||||
|
case 7:
|
||||||
|
newtio.c_cflag |= CS7;break;
|
||||||
|
case 8:
|
||||||
|
newtio.c_cflag |= CS8;break;
|
||||||
|
default:break;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (nEvent) {
|
||||||
|
case 'O': //奇校验
|
||||||
|
newtio.c_cflag |= PARENB;
|
||||||
|
newtio.c_cflag |= PARODD;
|
||||||
|
newtio.c_iflag |= (INPCK | ISTRIP);
|
||||||
|
break;
|
||||||
|
case 'E': //偶校验
|
||||||
|
newtio.c_iflag |= (INPCK | ISTRIP);
|
||||||
|
newtio.c_cflag |= PARENB;
|
||||||
|
newtio.c_cflag &= ~PARODD;
|
||||||
|
break;
|
||||||
|
case 'N': //无校验
|
||||||
|
newtio.c_cflag &= ~PARENB;
|
||||||
|
break;
|
||||||
|
default:break;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (nSpeed) {
|
||||||
|
case 2400:
|
||||||
|
cfsetispeed(&newtio, B2400);
|
||||||
|
cfsetospeed(&newtio, B2400);
|
||||||
|
break;
|
||||||
|
case 4800:
|
||||||
|
cfsetispeed(&newtio, B4800);
|
||||||
|
cfsetospeed(&newtio, B4800);
|
||||||
|
break;
|
||||||
|
case 9600:
|
||||||
|
cfsetispeed(&newtio, B9600);
|
||||||
|
cfsetospeed(&newtio, B9600);
|
||||||
|
break;
|
||||||
|
case 115200:
|
||||||
|
cfsetispeed(&newtio, B115200);
|
||||||
|
cfsetospeed(&newtio, B115200);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
cfsetispeed(&newtio, B9600);
|
||||||
|
cfsetospeed(&newtio, B9600);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nStop == 1) {
|
||||||
|
newtio.c_cflag &= ~CSTOPB;
|
||||||
|
} else if (nStop == 2) {
|
||||||
|
newtio.c_cflag |= CSTOPB;
|
||||||
|
}
|
||||||
|
|
||||||
|
newtio.c_cc[VTIME] = 0;
|
||||||
|
newtio.c_cc[VMIN] = 0;
|
||||||
|
tcflush(fd, TCIFLUSH);
|
||||||
|
if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) {
|
||||||
|
perror("com set error");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
printf("set done!\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* switch os */
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ def train(dataset, show_bar=False):
|
|||||||
|
|
||||||
if i % 100 == 0:
|
if i % 100 == 0:
|
||||||
if i % 1000 == 0:
|
if i % 1000 == 0:
|
||||||
test_samples, test_labels = dataset.sample_test_sets(5000)
|
test_samples, test_labels = dataset.sample_test_sets(1000)
|
||||||
acc = sess.run(accuracy, feed_dict={x: test_samples, y_: test_labels})
|
acc = sess.run(accuracy, feed_dict={x: test_samples, y_: test_labels})
|
||||||
bar.set_postfix({"loss": loss_value, "acc": acc})
|
bar.set_postfix({"loss": loss_value, "acc": acc})
|
||||||
|
|
||||||
|
|||||||
@@ -72,13 +72,13 @@ def forward(x, regularizer=None):
|
|||||||
node = pool_shape[1] * pool_shape[2] * pool_shape[3]
|
node = pool_shape[1] * pool_shape[2] * pool_shape[3]
|
||||||
reshaped = tf.reshape(pool2, [-1, node])
|
reshaped = tf.reshape(pool2, [-1, node])
|
||||||
|
|
||||||
fc1_w = get_weight([node, FC1_OUTPUT_NODES], regularizer)
|
fc1_w = tf.nn.dropout(get_weight([node, FC1_OUTPUT_NODES], regularizer), 0.1)
|
||||||
fc1_b = get_bias([FC1_OUTPUT_NODES])
|
fc1_b = get_bias([FC1_OUTPUT_NODES])
|
||||||
fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_w) + fc1_b)
|
fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_w) + fc1_b)
|
||||||
vars.extend([fc1_w, fc1_b])
|
vars.extend([fc1_w, fc1_b])
|
||||||
nodes.extend([fc1])
|
nodes.extend([fc1])
|
||||||
|
|
||||||
fc2_w = get_weight([FC1_OUTPUT_NODES, FC2_OUTPUT_NODES], regularizer)
|
fc2_w = tf.nn.dropout(get_weight([FC1_OUTPUT_NODES, FC2_OUTPUT_NODES], regularizer), 0.1)
|
||||||
fc2_b = get_bias([FC2_OUTPUT_NODES])
|
fc2_b = get_bias([FC2_OUTPUT_NODES])
|
||||||
fc2 = tf.nn.softmax(tf.matmul(fc1, fc2_w) + fc2_b)
|
fc2 = tf.nn.softmax(tf.matmul(fc1, fc2_w) + fc2_b)
|
||||||
vars.extend([fc2_w, fc2_b])
|
vars.extend([fc2_w, fc2_b])
|
||||||
|
|||||||
Reference in New Issue
Block a user