From ad9b5363dd4ddfa9344625b5ef365f79185c59ea Mon Sep 17 00:00:00 2001 From: Stardust011 Date: Thu, 30 Mar 2023 17:55:53 +0800 Subject: [PATCH 1/5] dev test --- Inc/serial.h | 4 ++-- Src/serial.cpp | 6 ++++-- main.cpp | 36 +++++++++++++++++------------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Inc/serial.h b/Inc/serial.h index a3d2021..c229af0 100644 --- a/Inc/serial.h +++ b/Inc/serial.h @@ -48,8 +48,8 @@ class serial { }; bool serialInit(); -void serialPosData(int32_t x, int32_t y, int32_t z, int32_t time_interval); -void serialPosData(pos_data* data); +serialData serialPosData(int32_t x, int32_t y, int32_t z, int32_t time_interval); +serialData serialPosData(pos_data* data); void setDataCmd(int cmd); void serialClose(); void * serialRead(void *pVoid); diff --git a/Src/serial.cpp b/Src/serial.cpp index 617ff4e..b620c05 100644 --- a/Src/serial.cpp +++ b/Src/serial.cpp @@ -46,7 +46,7 @@ static unsigned short crc16(const unsigned char* data_p, unsigned char length){ * @param z * @param time_interval */ -void serialPosData(int32_t x, int32_t y, int32_t z, int32_t time_interval) { +serialData serialPosData(int32_t x, int32_t y, int32_t z, int32_t time_interval) { data1.data.x = x; data1.data.y = y; data1.data.z = z; @@ -55,13 +55,14 @@ void serialPosData(int32_t x, int32_t y, int32_t z, int32_t time_interval) { crc_data.crc = crc16((unsigned char*)&data1, sizeof(data1)-4); data1.crc[0] = crc_data.crc_char[0]; data1.crc[1] = crc_data.crc_char[1]; + return data1; } /*! * @brief 发送数据 * @param pos_data data */ -void serialPosData(pos_data* data) { +serialData serialPosData(pos_data* data) { data1.data.x = data->x; data1.data.y = data->y; data1.data.z = data->z; @@ -71,6 +72,7 @@ void serialPosData(pos_data* data) { data1.crc[0] = crc_data.crc_char[0]; data1.crc[1] = crc_data.crc_char[1]; serialPort.send((char *) &data1, sizeof(data1)); + return data1; } /*! diff --git a/main.cpp b/main.cpp index 74e8d91..a5a3f20 100644 --- a/main.cpp +++ b/main.cpp @@ -9,39 +9,37 @@ using namespace std; -//unsigned short crc16(const unsigned char* data_p, unsigned char length){ -// unsigned char x; -// unsigned short crc = 0xFFFF; -// -// while (length--){ -// x = crc >> 8 ^ *data_p++; -// x ^= x>>4; -// crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x); -// } -// return crc; -//} +unsigned short crc16(const unsigned char* data_p, unsigned char length){ + unsigned char x; + unsigned short crc = 0xFFFF; + + while (length--){ + x = crc >> 8 ^ *data_p++; + x ^= x>>4; + crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x); + } + return crc; +} int main() { WzSerialPortPlus serialPort; serialData serialData1; + serialPort.open("/dev/ttyUSB0", 115200, 1, 8, 'n'); + int i; - pos_data data1; - data1.x = 15565656; - data1.y = 698; - data1.z = 4; - data1.time_interval = 9; - serialData1.data = data1; cout.setf(ios::hex); union crc16 crc_data{}; + + serialData1 = serialPosData(1, 2, 3, 4); + crc_data.crc = crc16((unsigned char*)&serialData1, sizeof(serialData1)-4); serialData1.crc[0] = crc_data.crc_char[0]; serialData1.crc[1] = crc_data.crc_char[1]; - serialPosData(&data1); + serialPort.send((char*)&serialData1, sizeof(serialData1)); for(i=0;i Date: Thu, 30 Mar 2023 22:29:41 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=B8=B2=E5=8F=A3=20(#1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 测试串口通讯 * 测试串口通讯 * 串口通讯 * dev test --- CMakeLists.txt | 92 +++--- Inc/armour.h | 3 +- Inc/config.h | 2 + Inc/led.h | 5 +- Inc/new_uart_2stm32.h | 62 ---- Inc/new_uart_thread_init.h | 59 ---- Inc/serial.h | 58 ++++ Lib/WzSerialPortPlus/WzSerialPortPlus.h | 3 - Lib/libv4l2_opencv_mat/no_need | 1 - Src/armour.cpp | 187 ++++-------- Src/new_uart_thread_init.cpp | 391 ------------------------ Src/serial.cpp | 135 ++++++++ main.cpp | 46 ++- 13 files changed, 336 insertions(+), 708 deletions(-) delete mode 100644 Inc/new_uart_2stm32.h delete mode 100644 Inc/new_uart_thread_init.h create mode 100644 Inc/serial.h delete mode 160000 Lib/libv4l2_opencv_mat/no_need delete mode 100644 Src/new_uart_thread_init.cpp create mode 100644 Src/serial.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9003b71..7d6b09e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,43 +16,36 @@ include_directories(${OpenCV_INCLUDE_DIRS}) link_libraries(${OpenCV_LIBS}) #CUDA -find_package(CUDA) -if (${CUDA_FOUND}) - set(CUDA_SOURCE_PROPERTY_FORMAT OBJ) - set(CUDA_SEPARABLE_COMPILATION ON) - include_directories(${CUDA_INCLUDE_DIRS}) - set(CUDA_PROPAGATE_HOST_FLAGS OFF) - set(CUDA_NVCC_FLAGS -gencode arch=compute_20,code=sm_20;-G;-g)#根据具体GPU更改参数 - #arch=compute_30,code=sm_30 - #arch=compute_35,code=sm_35 - #arch=compute_50,code=[sm_50,compute_50] - #arch=compute_52,code=[sm_52,compute_52] - #SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_61,code=sm_61;-std=c++11;-O3;-G;-g) - link_directories($ENV{CUDA_PATH}/lib/x64) - CUDA_ADD_EXECUTABLE(cv_2022 ${CURRENT_HEADERS} ${CURRENT_SOURCES}) -else (${CUDA_FOUND}) - MESSAGE(STATUS "Skip CUDA!") -endif (${CUDA_FOUND}) +#find_package(CUDA) +#if (${CUDA_FOUND}) +# set(CUDA_SOURCE_PROPERTY_FORMAT OBJ) +# set(CUDA_SEPARABLE_COMPILATION ON) +# include_directories(${CUDA_INCLUDE_DIRS}) +# set(CUDA_PROPAGATE_HOST_FLAGS OFF) +# set(CUDA_NVCC_FLAGS -gencode arch=compute_20,code=sm_20;-G;-g)#根据具体GPU更改参数 +# #arch=compute_30,code=sm_30 +# #arch=compute_35,code=sm_35 +# #arch=compute_50,code=[sm_50,compute_50] +# #arch=compute_52,code=[sm_52,compute_52] +# #SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_61,code=sm_61;-std=c++11;-O3;-G;-g) +# link_directories($ENV{CUDA_PATH}/lib/x64) +# CUDA_ADD_EXECUTABLE(cv_2022 ${CURRENT_HEADERS} ${CURRENT_SOURCES}) +#else (${CUDA_FOUND}) +# MESSAGE(STATUS "Skip CUDA!") +#endif (${CUDA_FOUND}) -#if (WIN32) -# file(GLOB CURRENT_HEADERS Inc/*.h Inc/*.hpp Inc/*.cuh) -# file(GLOB CURRENT_SOURCES Src/*.cpp Src/*.cu) -# include_directories(win-unix/) -# file(GLOB WIN_UNIX win-unix/*.h win-unix/*.hpp) -#else() -# file(GLOB CURRENT_HEADERS Inc/*.h Inc/*.hpp Inc/*.cuh) -# file(GLOB CURRENT_SOURCES Src/*.cpp Src/*.cu) -#endif() -#source_group("Include" FILES ${CURRENT_HEADERS}) -#source_group("Source" FILES ${CURRENT_SOURCES}) -# -#include_directories(./) -#include_directories(Inc/) -#include_directories(Src/) -# -#add_executable(cv_test main.cpp ${CURRENT_HEADERS} ${CURRENT_SOURCES} ${WIN_UNIX}) +if (WIN32) + file(GLOB CURRENT_HEADERS Inc/*.h Inc/*.hpp Inc/*.cuh) + file(GLOB CURRENT_SOURCES Src/*.cpp Src/*.cu) + include_directories(win-unix/) + file(GLOB WIN_UNIX win-unix/*.h win-unix/*.hpp) +else() + file(GLOB CURRENT_HEADERS Inc/*.h Inc/*.hpp Inc/*.cuh) + file(GLOB CURRENT_SOURCES Src/*.cpp Src/*.cu) +endif() +source_group("Include" FILES ${CURRENT_HEADERS}) +source_group("Source" FILES ${CURRENT_SOURCES}) -#---------------------TEST--------------------- include_directories(./) include_directories(Inc/) include_directories(Src/) @@ -64,14 +57,33 @@ target_include_directories(libv4l2 PUBLIC "Lib/libv4l2_opencv_mat/inc") add_library(WzSerialPortPlus STATIC Lib/WzSerialPortPlus/WzSerialPortPlus.cpp ) target_include_directories(WzSerialPortPlus PUBLIC "Lib/WzSerialPortPlus") -set(SRC Src/armour.cpp Src/new_uart_thread_init.cpp Src/led.cpp Src/kalman.cpp ) -set(INC Inc/armour.h Inc/new_uart_thread_init.h Inc/led.h Inc/kalman.h ) -add_executable(cv_test ${SRC} ${INC}) + +add_executable(cv_test ${CURRENT_HEADERS} ${CURRENT_SOURCES} ${WIN_UNIX}) target_link_libraries(cv_test libv4l2) +target_link_libraries(cv_test WzSerialPortPlus) -#target_link_libraries(cv_test WzSerialPortPlus) +#---------------------TEST--------------------- +#include_directories(./) +#include_directories(Inc/) +#include_directories(Src/) +# +#aux_source_directory(Lib/libv4l2_opencv_mat/src libv4l2_SRC_FILES) +#add_library(libv4l2 STATIC ${libv4l2_SRC_FILES}) +#target_include_directories(libv4l2 PUBLIC "Lib/libv4l2_opencv_mat/inc") +# +#add_library(WzSerialPortPlus STATIC Lib/WzSerialPortPlus/WzSerialPortPlus.cpp ) +#target_include_directories(WzSerialPortPlus PUBLIC "Lib/WzSerialPortPlus") +# +#set(SRC Src/armour.cpp Src/led.cpp Src/kalman.cpp ) +#set(INC Inc/armour.h Inc/led.h Inc/kalman.h ) +##set(SRC Src/armour.cpp Src/led.cpp Src/kalman.cpp Src/serial.cpp) +##set(INC Inc/armour.h Inc/led.h Inc/kalman.h Inc/serial.h) +#add_executable(cv_test ${SRC} ${INC} Src/serial.cpp Inc/serial.h) +#target_link_libraries(cv_test libv4l2 WzSerialPortPlus) +# +##target_link_libraries(cv_test WzSerialPortPlus) #---------------------END---------------------- #! -add_executable(test_lib main.cpp) +add_executable(test_lib main.cpp Src/serial.cpp Inc/serial.h) target_link_libraries(test_lib WzSerialPortPlus) \ No newline at end of file diff --git a/Inc/armour.h b/Inc/armour.h index 6c4b92c..045c59d 100644 --- a/Inc/armour.h +++ b/Inc/armour.h @@ -28,7 +28,8 @@ using namespace cv; #endif -#include "new_uart_thread_init.h" +//#include "new_uart_thread_init.h" +#include "serial.h" #include "led.h" #include "kalman.h" #include "BufWu.h" diff --git a/Inc/config.h b/Inc/config.h index ca37850..8337beb 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -12,5 +12,7 @@ #define IMAGE_WIDTH 3264 #define IMAGE_HEIGHT 2448 #define FPS 30 +#define SERIAL_PORT "/dev/ttyUSB0" +#define SERIAL_BAUDRATE 115200 #endif //CV_TEST_CONFIG_H diff --git a/Inc/led.h b/Inc/led.h index 5d1cade..1642e54 100644 --- a/Inc/led.h +++ b/Inc/led.h @@ -8,10 +8,11 @@ #include //ÓëÉ豞Çý¶¯³ÌÐòÓÐ¹Ø #include //žÄ±äÒÑŽò¿ªµÄÎÄŒþÐÔÖÊ #include -#include //²ÎÊýÉèÖà +#include #include #include -#include "new_uart_thread_init.h" +//#include "new_uart_thread_init.h" +#include "serial.h" using namespace std; diff --git a/Inc/new_uart_2stm32.h b/Inc/new_uart_2stm32.h deleted file mode 100644 index 35b8f0a..0000000 --- a/Inc/new_uart_2stm32.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef _NEW_UART_2STM32 -#define _NEW_UART_2STM32 -/*: -#pragma pack(8): -*/ -#include -#include -#include -#include - -typedef struct { - - uint8_t Head; - uint8_t CmdID; - uint16_t Datalength; - -}__attribute__((packed, aligned(1))) Frame_hearder; - -typedef struct { - - int32_t x; - int32_t y; - int32_t z; - int32_t Time_Interval; - uint8_t Goal_State; - -}__attribute__((packed, aligned(1))) Armour_MOD; -typedef struct { - - uint8_t Mod_set_over; - -}__attribute__((packed, aligned(1))) Reply_MOD; - -typedef struct { - - float x; - float y; - float z; - uint8_t Target_state; - - -}__attribute__((packed, aligned(1))) Power_MOD; -typedef struct { - - uint8_t Target_state; - -}__attribute__((packed, aligned(1))) Error_MOD; -//ÊÓŸõÊýŸÝœá¹¹ -typedef struct { - Frame_hearder FH_data; - union - { - Armour_MOD Ar_data; - Reply_MOD Re_data; - Power_MOD PW_data; - Error_MOD Err_data; - } Data; - uint16_t CRC16; -}__attribute__((packed, aligned(1))) Date_message; - -void send_message_AR(float xdata, float ydata, float zdata, float tdata, uint8_t Cmdata); -#endif diff --git a/Inc/new_uart_thread_init.h b/Inc/new_uart_thread_init.h deleted file mode 100644 index 27d10ca..0000000 --- a/Inc/new_uart_thread_init.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef _NEW_UART_THREAD_INIT_H -#define _NEW_UART_THREAD_INIT_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "new_uart_2stm32.h" - -#define BUFF_REPEAT 1 -#define BUFF_UNREPEAT 2 -#define BUFF_CENTER 3 -#define BUFF_UNCENTER 4 - -#define BEGIN_LED 1 -#define BLUE_LED 2 -#define RED_LED 3 -//²Ëµ¥×÷ΪѡÏî -enum { - MOD_NULL = 0, - MOD_RED, //ºì·½ - MOD_BLUE, //À¶·½ - MOD_POWER_BIG, //´ò´ó·ù - MOD_POWER_SMALL,//С·û - MOD_AUTO, //×Ô¶¯´ò»÷ -}; -/*====send the mesage====*/ -//void send_message_PW(float xdata, float ydata, float zdata, uint8_t Cmdata); -void send_message_AR(float xdata, float ydata, float zdata, float tdata, uint8_t Cmdata); -//void send_message_VR( uint8_t Cmdata); -//void send_message_ER(uint8_t Cmdata); -/*=====the thread====*/ -void *thread_read(void *arg); - -void *thread_write(void *arg); -//void *thread_video(void * arg); -//void *thread_led(void *arg); -/*===== uart =====*/ -int INIT_UART(); - -int UART_WRITE_DATA(char *data, int data_sizeof); - - -#endif/*end the init*/ - diff --git a/Inc/serial.h b/Inc/serial.h new file mode 100644 index 0000000..c229af0 --- /dev/null +++ b/Inc/serial.h @@ -0,0 +1,58 @@ +// +// Created by Stardust on 2023/3/29. +// + +#ifndef CV_TEST_SERIAL_H +#define CV_TEST_SERIAL_H + +#include +#include "config.h" + +enum { + MOD_NULL = 0, + MOD_RED, + MOD_BLUE, + MOD_POWER_BIG, + MOD_POWER_SMALL, + MOD_AUTO, +}; + +union crc16 { + unsigned short crc; + unsigned char crc_char[2]; +}; + +struct pos_data { + int32_t x; + int32_t y; + int32_t z; + int32_t time_interval; + pos_data() { + x = 0; + y = 0; + z = 0; + time_interval = 0; + } +}; + +struct serialData { + unsigned char head[2] {0xAA, 0xAF}; + unsigned char cmd{0x01}; + unsigned char length{0x18}; + pos_data data; + unsigned char crc[2]{0x00, 0x00}; + unsigned char end[2]{0xEE, 0xEE}; +}; + +class serial { +}; + +bool serialInit(); +serialData serialPosData(int32_t x, int32_t y, int32_t z, int32_t time_interval); +serialData serialPosData(pos_data* data); +void setDataCmd(int cmd); +void serialClose(); +void * serialRead(void *pVoid); +void * serialSend(void *pVoid); + +#endif //CV_TEST_SERIAL_H diff --git a/Lib/WzSerialPortPlus/WzSerialPortPlus.h b/Lib/WzSerialPortPlus/WzSerialPortPlus.h index 89bc876..f1be3e9 100644 --- a/Lib/WzSerialPortPlus/WzSerialPortPlus.h +++ b/Lib/WzSerialPortPlus/WzSerialPortPlus.h @@ -1,6 +1,3 @@ -// -// Created by Stardust on 2023/3/25. -// /** * @file WzSerialPortPlus.h * @author ouyangwei diff --git a/Lib/libv4l2_opencv_mat/no_need b/Lib/libv4l2_opencv_mat/no_need deleted file mode 160000 index abd9ecd..0000000 --- a/Lib/libv4l2_opencv_mat/no_need +++ /dev/null @@ -1 +0,0 @@ -Subproject commit abd9ecdc3115c54a18f2584a6a7d0b03a27aa356 diff --git a/Src/armour.cpp b/Src/armour.cpp index 1631ff1..7bb4d50 100644 --- a/Src/armour.cpp +++ b/Src/armour.cpp @@ -1,5 +1,5 @@ #include "armour.h" -// send_message_AR +// serialPosData /* * this code was backup in 2019.07.06 * author: Mr.Monster @@ -398,9 +398,9 @@ vector> armour::get_point_contours(const Mat &src_image)//3 { vector> contour_vector; vector contour_hierachy; - cout << "hello1 " << endl; +// cout << "hello1 " << endl; findContours(src_image, contour_vector, contour_hierachy, RETR_CCOMP, CHAIN_APPROX_TC89_L1); - cout << "hello2" << endl; +// cout << "hello2" << endl; //cout << "contour_vector size:" << contour_vector.size() << endl; return contour_vector; } @@ -1405,40 +1405,6 @@ int armour::return_the_exsit_num(string folder) { return file_names.size(); } -/*int main() -{ - pthread_t Read_Uart; - pthread_t Send_Uart; - int ret; - - //sem_init(&sem_main,0,0);// busy - //sem_init(&sem_vedio,0,1); - if (INIT_UART() == -1) - { - std::cout<<"open file!"< pic_array_thread; -// -// int serial_camera = get_usb_camara_serial(BIG_OR_SMALL); -// if (serial_camera == 0) -//// cap_thread = set_v4l("/dev/video0"); -// V4L2DeviceParameters cap_thread("/dev/video0", V4L2_PIX_FMT_MJPEG , IMAGE_WIDTH, IMAGE_HEIGHT, FPS); -// else if (serial_camera == 1) -// V4L2DeviceParameters cap_thread("/dev/video1", V4L2_PIX_FMT_MJPEG , IMAGE_WIDTH, IMAGE_HEIGHT, FPS); -// else -// V4L2DeviceParameters cap_thread("/dev/video0", V4L2_PIX_FMT_MJPEG , IMAGE_WIDTH, IMAGE_HEIGHT, FPS); -// -// int thread_flags = 0; -// -// while (1) { -// if (true == process_finish && thread_flags != 0) { -// /*if(NULL == pic_array_thread.back().data) -// { -// cap >> *src_image; -// process_finish = false; -// }*/ -// pic_array_thread.back().copyTo(*src_image); -// pic_array_thread.clear(); -// process_finish = false; -// } -// if (true == thread_lauch_first) { -// Mat fram; -// fram = VideoCapture_v4l(cap_thread); -// fram.copyTo(*src_image); -// thread_lauch_first = false; -// continue; -// } -// Mat fram_temp; -// fram_temp = VideoCapture_v4l(cap_thread); -// // VideoPlayer(fram); -// if (fram_temp.data == NULL) { -// cout << "fram can't access" << endl; -// exit(-1); -// } -// pic_array_thread.push_back(fram_temp); -// thread_flags++; -// } -// cap_thread->stopCapture(); -// cap_thread->freeBuffers(); -// cap_thread->closeDevice(); -//} - - static void *get_fram_thread(void *arg) { Mat *src_image = (Mat *) arg; vector pic_array_thread; @@ -1585,20 +1501,38 @@ static void *get_fram_thread(void *arg) { } int main() { + //init the thread pthread_t Read_Uart; pthread_t Send_Uart; pthread_t led_thread; pthread_t get_fram; int ret; - + //init the sem sem_init(&finish_sem, 0, 0);//init the signal sem_init(&first_sem, 0, 1); //usleep(1000*10000); //sem_init(&sem_main,0,0);// busy //sem_init(&sem_vedio,0,1); - if (INIT_UART() == -1) { + + //! 重构 + if (serialInit() == false){ + cout << "serial init fail" << endl; +// return 0; + } + ret = pthread_create(&Read_Uart, NULL, serialRead, NULL); + if (ret != 0) { + cout << "read_fail" << endl; + } + ret = pthread_create(&Send_Uart, NULL, serialSend, NULL); + ret = pthread_create(&led_thread,NULL, led_on_thread, NULL); + if (ret != 0) { + std::cout << "led_fail" << std::endl; + } + Mat src_image(Size(640, 480), CV_8UC3); + + /*if (INIT_UART() == -1) { std::cout << "open file!" << std::endl; //return 0; } @@ -1615,7 +1549,7 @@ int main() { if (ret != 0) { std::cout << "led_fail" << std::endl; } - Mat src_image(Size(640, 480), CV_8UC3); + Mat src_image(Size(640, 480), CV_8UC3);*/ #ifdef thread_lauch ret = pthread_create(&get_fram,NULL,get_fram_thread,(void *)&src_image); if(ret != 0) @@ -1697,10 +1631,15 @@ int main() { if (src_image.data == NULL) { cout << "wrong read_data" << endl; } + + //装甲板 if (MOD_B_R != 3) { cout << "auto beat" << endl; + //串口数据在fire中发送 auto_beat.fire(src_image); } + + //能量机关? if (MOD_B_R == 3) { Rect buf_out_rect; @@ -1718,46 +1657,18 @@ int main() { Point(buf_out_point.x - 320, buf_out_point.y - 240), 800.0); if (ERROR_POINT_BUF == buf_out_point) { - send_message_AR(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0, 0); + //serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0, 0); + serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0); + setDataCmd(0); // cout << "debug ... " << endl; } cout << "yaw :" << angle_vector[0] << " , ph:" << angle_vector[1] << endl; // std::cout << "enter big buf mode" << std::endl; - send_message_AR(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0, 1); + //serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0, 1); + serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0); + setDataCmd(1); cout << "debug ..." << endl; } - /* if(MOD_B_R == 3) - { - // display("src",src_image); - Rect buf_out_rect ; - double never_used_weights = 0; - double img_l=0,real_l=0; - //cap >> src_image; - //if(src_image.data == NULL) cout << "wrong" << endl; - //imshow("debug",src_image); - //cap.release(); - Point2f buf_out_point; - buf_out_point=buf.Detect_Mainfunction(src_image,img_l,real_l,false); - // imshow("src_image", src_image); - waitKey(10); - // display("bufWu_Pic",src_image); - // cout << "debug .. " << endl; - cout << "buf_point (" << buf_out_point.x << "," << buf_out_point.y << ")" << endl; - vector angle_vector = Calculate_angle_buf(Point(0,0),Point(buf_out_point.x - 320,buf_out_point.y - 240),80.0); - if(ERROR_POINT_BUF == buf_out_point) - { - send_message_AR(angle_vector[0] * 1000,angle_vector[1] * 1000,1,0,0); - // cout << "debug ... " << endl; - cout << "never find target" << endl; - continue; - } - cout << "yaw :" << angle_vector[0] << " , ph:" << angle_vector[1] << endl; - // std::cout << "enter big buf mode" << std::endl; - send_message_AR(angle_vector[0] * 1000,angle_vector[1] * 1000,1,0,1); - cout << "debug ..." << endl; - }*/ - - } //cap_thread->stopCapture(); @@ -1991,7 +1902,9 @@ void armour::fire(Mat &src_image) { Scalar(0, 255, 255), 2, 8); circle(result_pic, send_point, 2, Scalar(255, 0, 255), 2, 8); cost_time = (get_sys_time() - cost_time) / getTickFrequency(); - //display("display" , result_pic); + + //! 输出 +// display("display" , result_pic); cout << "fps:" << 1. / cost_time << endl; //index_cap.open(0); if (send_point == ERROR_POINT) { @@ -1999,12 +1912,14 @@ void armour::fire(Mat &src_image) { kf.ResetKalmanFilter(320, 240); { cout << "x:" << 0 << " y:" << 0 << endl; - send_message_AR(0, 0, distance_single, cost_time, 1); + serialPosData(0, 0, distance_single, cost_time); + setDataCmd(1); } test.push_back(Point(320, 240)); + system("clear"); return; } - cout << "distance is : " << distance_single << endl; +// cout << "distance is : " << distance_single << endl; distance_single = pnp_Get_Distance_armour(select_rect); target_flags = true; cout << "distance : " << distance_single << endl; @@ -2021,6 +1936,7 @@ void armour::fire(Mat &src_image) { prev_distance = distance_single; prev_point_fire = send_point; + //验证角度 angle_vector = Calculate_angle(Point(50, 0), Point(send_point.x - 320, send_point.y - 240), distance_single * 10); vector angle_vector_predict_left = Calculate_angle(Point(50, 0), @@ -2028,8 +1944,8 @@ void armour::fire(Mat &src_image) { send_point.x + select_rect.width / 2.0 - 320), distance_single * 10); vector angle_vector_predict_right = Calculate_angle(Point(40, 0), - Point(send_point.x + select_rect.width / 2.0 - 320, - 0), distance_single * 10); + Point(send_point.x + select_rect.width / 2.0 - 320, 0), + distance_single * 10); cout << "angle_vector[1] : " << angle_vector[1] << " angle_vector[2]:" << angle_vector[2] << endl; @@ -2042,19 +1958,20 @@ void armour::fire(Mat &src_image) { cout << "yaw :" << angle_vector[0] << endl; angle_vector[0] = -2.0; } - send_message_AR(angle_vector[0] * 1000, angle_vector[1] * 1000, angle_vector_predict_left[0] * 1000, - angle_vector_predict_right[0] * 1000, 1); + serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, angle_vector_predict_left[0] * 1000, + angle_vector_predict_right[0] * 1000); + setDataCmd(1); vector kalman_angle_vector = Calculate_angle(Point(0, 0), Point(target_point_kalman.x - 320, target_point_kalman.y - 240), - distance_single); + distance_single); cout << "x_angle: " << angle_vector[0] << endl; cout << "PH_angle: " << angle_vector[1] << endl; cout << "kalman_angle: " << kalman_angle_vector[0] << endl; + //! DEBUG 图像 + display("display",src_image); + system("clear"); } - return; } - - diff --git a/Src/new_uart_thread_init.cpp b/Src/new_uart_thread_init.cpp deleted file mode 100644 index 5903556..0000000 --- a/Src/new_uart_thread_init.cpp +++ /dev/null @@ -1,391 +0,0 @@ -#include "new_uart_thread_init.h" -#include "new_uart_2stm32.h" -#include -#include -#include - -#define PATHNAME "/dev/ttyUSB0" -#define RandRate B115200 - -bool READ_DATA = false; -bool Set_Mod = true; -int MOD_B_R = 1; -int UART_ID; -int CMD_COLOR = 1; -int DATA_UP_Ar = 23; - - -Date_message DOWN_DATA; -Date_message DOWN_DATA_AR; - -static unsigned short int Get_CRC16_Check_Sum(char *pchMessage, int dwLength, int wCRC); - -void Append_CRC16_Check_Sum_Judge(char *pchMessage, int dwLength) { - //TODO by github copilot - unsigned short int wCRC = 0xffff; - wCRC = Get_CRC16_Check_Sum(pchMessage, dwLength, wCRC); - pchMessage[dwLength] = (char) (wCRC >> 8); - pchMessage[dwLength + 1] = (char) (wCRC & 0x00ff); -} - -int UART_WRITE_DATA(char *data, int data_sizeof) { - //TODO by github copilot - int ret = write(UART_ID, data, data_sizeof); - if (ret < 0) { - printf("write error!\r"); - } -} - -unsigned short int CRC_INIT_Judge = 0xffff; -typedef char u8; -const char CRC8_INIT_Judge = 0xff; -const unsigned char CRC8_TAB_Judge[256] = - { - 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41, - 0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e, 0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc, - 0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0, 0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62, - 0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d, 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff, - 0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5, 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07, - 0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58, 0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a, - 0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6, 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24, - 0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b, 0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9, - 0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f, 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd, - 0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92, 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50, - 0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c, 0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee, - 0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1, 0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73, - 0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49, 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b, - 0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16, - 0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8, - 0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35, - }; - -unsigned int Verify_CRC16_Check_Sum_Judge(char *pchMessage, int dwLength); - -void *thread_read(void *arg) { - //unsigned char coordinate_num_temp[16] = {0}; - char coordinate_num[17] = {}; - //char *coordinate_num_temp; - //coordinate_num_temp = (char *)malloc(sizeof(char) * 4); - int ret; - bool bule_or_red = true; - int a; - //int led_fd;//the auto led - - /*=========== debuging =========*/ - //bule_or_red = false; // unread the uart - /*=========== debuging ========= - led_fd = open("/sys/class/gpio/export ",O_WRONLY); - if(led_fd == -1) - { - printf("open1 led error!\n"); - } - write(led_fd,"48",sizeof("48")); - close(led_fd); - led_fd = open("/sys/class/gpio/gpio158/value",O_RDWR); - if(led_fd == -1) - { - printf("open led error!\n"); - } - */ - //memset(coordinate_num, 0, sizeof(coordinate_num)); - //memset(coordinate_num_temp, 0, sizeof(char) * 4); - //printf("original : %x\n",coordinate_num_temp); -// printf("original : %x\n",coordinate_num); - while (bule_or_red) { - //printf("coordinate_num size is : %d\n",sizeof(coordinate_num)); - //ret = read(UART_ID,coordinate_num,sizeof(coordinate_num));//½«´®¿ÚÊý¾Ý¸øcoordinate - ret = read(UART_ID, coordinate_num, sizeof(coordinate_num)); - //ret = read(UART_ID,coordinate_num,sizeof(coordinate_num)); - - - //printf("ret is:%d\n",ret); - //printf("recv: %x\n",coordinate_num); - //printf("recv: %4x\n",coordinate_num); - //a = Verify_CRC16_Check_Sum_Judge(coordinate_num,7); - //printf("the check is %d\n",a); - //printf("recv2: %02x\n",(unsigned char)coordinate_num[0]); - //printf("bb!\n"); - if ((unsigned char) coordinate_num[0] == 0xA5 && (unsigned char) coordinate_num[1] == 0x05) { - - //printf("recv data\n"); - //printf("bb!\n"); - if (coordinate_num[4] == 0x01) { - - MOD_B_R = MOD_RED; - READ_DATA = true; - Set_Mod = true; - CMD_COLOR = 1; - //bule_or_red = false; - } else if (coordinate_num[4] == 0x02) { - - MOD_B_R = MOD_BLUE; - READ_DATA = true; - Set_Mod = true; - CMD_COLOR = 2; - //bule_or_red = false; - } - /*else if(coordinate_num[4] == 0x03) - { - MOD_B_R = MOD_AUTO; - READ_DATA = true; - Set_Mod = true; - write(led_fd,LED_H,sizeof(LED_H)); - }*/ - else if (coordinate_num[4] == 0x04) { - - //printf("enter power big .." ); - MOD_B_R = MOD_POWER_BIG; - /*if(coordinate_num[5] == 0x01) - { - //return to the center - re_center = BUFF_CENTER; - - }else if(coordinate_num[5] == 0x02) - { - //re to play - re_buff = BUFF_REPEAT; - }*/ - READ_DATA = true; - Set_Mod = true; - - } - /*else if(coordinate_num[4] == 0x05) - { - //Ñ¡¶¨Ð¡·ù - MOD_B_R = MOD_POWER_SMALL; - if(coordinate_num[5] == 0x01) - { - //return to the center - re_center = BUFF_CENTER; - - }else if(coordinate_num[5] == 0x02) - { - //re to play - re_buff = BUFF_REPEAT; - } - READ_DATA = true; - Set_Mod = true; - }*/ - else { - - MOD_B_R = MOD_NULL; - } - - //Pitch = coordinate_num[4]/10+coordinate_num[4]%10;//Ôݶ¨********************** - } - } - return 0; -} - -const unsigned short int wCRC_Table_Judge[256] = - { - 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, - 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, - 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, - 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, - 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, - 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, - 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, - 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, - 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, - 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, - 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, - 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, - 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, - 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, - 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, - 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, - 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, - 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, - 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, - 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, - 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, - 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, - 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, - 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, - 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, - 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, - 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, - 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, - 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, - 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, - 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, - 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 - }; - - -void send_message_AR(float xdata, float ydata, float zdata, float tdata, uint8_t Cmdata) { - memset(&DOWN_DATA_AR, 0, sizeof(DOWN_DATA_AR)); - DOWN_DATA_AR.FH_data.Head = 0xA5; - DOWN_DATA_AR.FH_data.CmdID = 0x01; - DOWN_DATA_AR.FH_data.Datalength = 17; - - DOWN_DATA_AR.Data.Ar_data.x = (int32_t) xdata; - DOWN_DATA_AR.Data.Ar_data.y = (int32_t) ydata; - DOWN_DATA_AR.Data.Ar_data.z = (int32_t) zdata; - DOWN_DATA_AR.Data.Ar_data.Time_Interval = (int32_t) tdata; - DOWN_DATA_AR.Data.Ar_data.Goal_State = Cmdata; - Append_CRC16_Check_Sum_Judge((char *) &DOWN_DATA_AR, DATA_UP_Ar); - -} - - -int set_uart_mode(speed_t speed, int vtime, int vmin) { - struct termios newtio; - - if (UART_ID <= 0) { - printf("set_uart_mode failed---fd=%d\n", UART_ID); - return -1; - } - - bzero(&newtio, sizeof(newtio)); - newtio.c_iflag |= IGNBRK | BRKINT; - newtio.c_cflag |= CLOCAL | CREAD; - newtio.c_cflag &= ~CSIZE; - newtio.c_cflag |= CS8; - newtio.c_cflag &= ~PARENB; - cfsetispeed(&newtio, speed); - cfsetospeed(&newtio, speed); - newtio.c_cflag &= ~CSTOPB; - newtio.c_cc[VTIME] = vtime; - newtio.c_cc[VMIN] = vmin; - tcflush(UART_ID, TCIFLUSH); - - if ((tcsetattr(UART_ID, TCSANOW, &newtio)) != 0) - { - printf("set_uart_mode failed\n"); - return -1; - } - - printf("set_uart_mode\n"); - return 0; -} - -int UART_WRITE_DATA(unsigned char *data, int data_sizeof) { - int ret; - //printf("the data is %c\n",data); - ret = write(UART_ID, data, data_sizeof); - //printf("data is : %02x\n",data); - //printf("sizeof data: %d,data size of : %d,write data: %d\n",sizeof(data),data_sizeof,ret); - if (ret <= 0) { - return -1; - } - return ret; -} - -int INIT_UART() { - UART_ID = open(PATHNAME, O_RDWR | O_NOCTTY); - if (UART_ID < 0) { - printf("open UART3 fail\n"); - return -1; - } - printf("open UART3 success\n"); - set_uart_mode(RandRate, 0, 1); - return 1; -} - -void Append_CRC16_Check_Sum_Judge(unsigned char *pchMessage, int dwLength) { - int wCRC = 0; - if ((pchMessage == 0) || (dwLength <= 2)) { - return; - } - wCRC = Get_CRC16_Check_Sum((u8 *) pchMessage, dwLength - 2, CRC_INIT_Judge); - pchMessage[dwLength - 2] = 0x0d;//(u8)(wCRC & 0x00ff); - pchMessage[dwLength - 1] = 0x0a;//(u8)((wCRC >> 8) & 0x00ff); -} - -unsigned int Verify_CRC16_Check_Sum_Judge(char *pchMessage, int dwLength) { - int wExpected = 0; - if ((pchMessage == 0) || (dwLength <= 2)) { - return 0; - } - wExpected = Get_CRC16_Check_Sum(pchMessage, dwLength - 2, CRC_INIT_Judge); - return ((wExpected & 0xff) == pchMessage[dwLength - 2] && ((wExpected >> 8) & 0xff) == pchMessage[dwLength - 1]); -} - -static unsigned short int Get_CRC16_Check_Sum(char *pchMessage, int dwLength, int wCRC) { - char chData; - if (pchMessage == 0) { - return 0xFFFF; - } - while (dwLength--) { - chData = *pchMessage++; - (wCRC) = ((int) (wCRC) >> 8) ^ wCRC_Table_Judge[((int) (wCRC) ^ - (int) (chData)) & 0x00ff]; - } - return wCRC; -} - -void *thread_write(void *arg) { - int ret; - - unsigned char data_red_verify[] = {0xA5, 0x03, 0x01, 0x00, 0x01}; - unsigned char data_blue_verify[] = {0xA5, 0x03, 0x01, 0x00, 0x02}; - // char data_auto_verify[] = {0xA5,0x03,0x01,0x00,0x03}; - // char data_power_small_verify[] = {0xA5,0x03,0x01,0x00,0x05}; - // char data_power_big_verify[] = {0xA5,0x03,0x01,0x00,0x04}; - - - while (1) { - //if(READ_DATA == false) - //{ - // Append_CRC8_Check_Sum_Judge(data_write, 6);//¼ÓÉÏCR¼ìÑéλ - // ret = UART_WRITE_DATA(data_write,sizeof(data_write)); - // printf("write = %d\n",ret); - //} - //else if(READ_DATA == true) - //{ - /*=============debuging==============*/ - // MOD_B_R = MOD_RED ; //Direct into the red pattern - /*=============debuging==============*/ - switch (MOD_B_R) { - case MOD_RED: - if (Set_Mod == true) { - //·¢ËÍУÑéÊý¾ÝÖ»·¢ËÍÒ»´Î - Append_CRC16_Check_Sum_Judge(data_red_verify, sizeof(data_red_verify) + 2); - ret = UART_WRITE_DATA(data_red_verify, sizeof(data_red_verify) + 2); - Set_Mod = false; - break; - } - //Append_CRC8_Check_Sum_Judge(data_armour, sizeof(data_armour)); - /*====Red and blue doesn't write data===*/ - - ret = UART_WRITE_DATA((char *) &DOWN_DATA_AR, DATA_UP_Ar); - break; - case MOD_BLUE: - if (Set_Mod == true) { - Append_CRC16_Check_Sum_Judge(data_blue_verify, sizeof(data_blue_verify) + 2); - ret = UART_WRITE_DATA(data_blue_verify, sizeof(data_blue_verify) + 2); - Set_Mod = false; - break; - } - // Append_CRC8_Check_Sum_Judge(data_armour, sizeof(data_armour)); - /*====Red and blue doesn't write data===*/ - - ret = UART_WRITE_DATA((char *) &DOWN_DATA_AR, DATA_UP_Ar); - break; - case MOD_POWER_BIG: - if (Set_Mod == true) { - //·¢ËÍУÑéÊý¾ÝÖ»·¢ËÍÒ»´Î - Append_CRC16_Check_Sum_Judge(data_red_verify, sizeof(data_red_verify) + 2); - ret = UART_WRITE_DATA(data_red_verify, sizeof(data_red_verify) + 2); - Set_Mod = false; - break; - } - //Append_CRC8_Check_Sum_Judge(data_armour, sizeof(data_armour)); - /*====Red and blue doesn't write data===*/ - - ret = UART_WRITE_DATA((char *) &DOWN_DATA_AR, DATA_UP_Ar); - break; - - default: - break; - } - - usleep(1000 * 3);//us - - - } - return 0; -} - diff --git a/Src/serial.cpp b/Src/serial.cpp new file mode 100644 index 0000000..b620c05 --- /dev/null +++ b/Src/serial.cpp @@ -0,0 +1,135 @@ +// +// Created by Stardust on 2023/3/29. +// + +#include "serial.h" + +int MOD_B_R = 1; +bool READ_DATA; +bool Set_Mod = true; +int CMD_COLOR = 1; + +WzSerialPortPlus serialPort; +serialData data1; + +/*! + * @brief 初始化串口 + */ +bool serialInit() { + // Init serial port + bool ret = serialPort.open(SERIAL_PORT, SERIAL_BAUDRATE, 1, 8, 'n'); + return ret; +} + +/*! + * @brief CRC16校验 + * @param data_p + * @param length + * @return unsigned short crc16 + */ +static unsigned short crc16(const unsigned char* data_p, unsigned char length){ + unsigned char x; + unsigned short crc = 0xFFFF; + + while (length--){ + x = crc >> 8 ^ *data_p++; + x ^= x>>4; + crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x); + } + return crc; +} + +/*! + * @brief 发送数据 + * @param x + * @param y + * @param z + * @param time_interval + */ +serialData serialPosData(int32_t x, int32_t y, int32_t z, int32_t time_interval) { + data1.data.x = x; + data1.data.y = y; + data1.data.z = z; + data1.data.time_interval = time_interval; + union crc16 crc_data{}; + crc_data.crc = crc16((unsigned char*)&data1, sizeof(data1)-4); + data1.crc[0] = crc_data.crc_char[0]; + data1.crc[1] = crc_data.crc_char[1]; + return data1; +} + +/*! + * @brief 发送数据 + * @param pos_data data + */ +serialData serialPosData(pos_data* data) { + data1.data.x = data->x; + data1.data.y = data->y; + data1.data.z = data->z; + data1.data.time_interval = data->time_interval; + union crc16 crc_data{}; + crc_data.crc = crc16((unsigned char*)&data1, sizeof(data1)-4); + data1.crc[0] = crc_data.crc_char[0]; + data1.crc[1] = crc_data.crc_char[1]; + serialPort.send((char *) &data1, sizeof(data1)); + return data1; +} + +/*! + * @brief 关闭串口 + */ +void serialClose() { + serialPort.close(); +} + +/*! + * @brief 读取串口数据 + * @param char *data + * @param int length + * + * 有一说一 我根本不知道为什么这里是这样的 + */ +void *serialRead(void *pVoid){ + for(;;) { + serialPort.setReceiveCalback([&](char* data, int length){ + printf("received: %s\n",data); + if ((unsigned char) data[0] == 0xA5 && (unsigned char) data[1] == 0x05) { + + if (data[4] == 0x01) { + + MOD_B_R = MOD_RED; + READ_DATA = true; + Set_Mod = true; + CMD_COLOR = 1; + //bule_or_red = false; + } else if (data[4] == 0x02) { + + MOD_B_R = MOD_BLUE; + READ_DATA = true; + Set_Mod = true; + CMD_COLOR = 2; + //bule_or_red = false; + } else if (data[4] == 0x04) { + + //printf("enter power big .." ); + MOD_B_R = MOD_POWER_BIG; + + READ_DATA = true; + Set_Mod = true; + } else { + MOD_B_R = MOD_NULL; + } + } + }); + } +} + +void *serialSend(void *pVoid){ + for (;;) { + serialPort.send((char *) &data1, sizeof(data1)); + } +} + +void setDataCmd(int cmd){ + data1.cmd = cmd; +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index 32a1610..a5a3f20 100644 --- a/main.cpp +++ b/main.cpp @@ -5,23 +5,41 @@ #include #include #include +#include "serial.h" + +using namespace std; + +unsigned short crc16(const unsigned char* data_p, unsigned char length){ + unsigned char x; + unsigned short crc = 0xFFFF; + + while (length--){ + x = crc >> 8 ^ *data_p++; + x ^= x>>4; + crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x); + } + return crc; +} int main() { WzSerialPortPlus serialPort; - serialPort.setReceiveCalback([&](char* data, int length){ - printf("received: %s\n",data); - - std::string responsePrefix = "received: "; - std::string response(data,length); - response = responsePrefix + response; - - serialPort.send((char*)response.c_str(), response.length()); - }); - if(serialPort.open("/dev/ttyS1", 9600, 1, 8, 'n')) - { - getchar(); - serialPort.close(); - } + serialData serialData1; + serialPort.open("/dev/ttyUSB0", 115200, 1, 8, 'n'); + + int i; + cout.setf(ios::hex); + union crc16 crc_data{}; + serialData1 = serialPosData(1, 2, 3, 4); + crc_data.crc = crc16((unsigned char*)&serialData1, sizeof(serialData1)-4); + serialData1.crc[0] = crc_data.crc_char[0]; + serialData1.crc[1] = crc_data.crc_char[1]; + serialPort.send((char*)&serialData1, sizeof(serialData1)); + + for(i=0;i Date: Fri, 31 Mar 2023 00:10:56 +0800 Subject: [PATCH 3/5] =?UTF-8?q?dev=20=E5=88=86=E7=A6=BB=E9=A2=9C=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Inc/armour.h | 5 ++- Inc/config.h | 4 ++ Inc/serial.h | 16 ++++++- Src/armour.cpp | 111 ++++++++++++++++++++++++++++++++++++------------- Src/serial.cpp | 6 +++ 5 files changed, 110 insertions(+), 32 deletions(-) diff --git a/Inc/armour.h b/Inc/armour.h index 045c59d..3b19619 100644 --- a/Inc/armour.h +++ b/Inc/armour.h @@ -53,7 +53,6 @@ enum MODE_FLAG { MODE_NONE = 0, MODE_RED = 1, MODE_BLUE = 2 - }; struct combine_rect_height { @@ -63,6 +62,8 @@ struct combine_rect_height { extern int CMD_COLOR; +static pos_data data_send; + class armour { public: double cost_time; @@ -105,6 +106,8 @@ class armour { Mat HSV_to_gray(const Mat &, int, int, int, int, int, int, int); + auto judgeColor(const Mat &src_image); + void fire(Mat &); vector > judge_the_min_distance(const Mat &, const vector > &, double, double); diff --git a/Inc/config.h b/Inc/config.h index 8337beb..ecf52c2 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -15,4 +15,8 @@ #define SERIAL_PORT "/dev/ttyUSB0" #define SERIAL_BAUDRATE 115200 +// 1: red, 2: blue +#define BEAT_COLOR 1 + + #endif //CV_TEST_CONFIG_H diff --git a/Inc/serial.h b/Inc/serial.h index c229af0..1ca8e93 100644 --- a/Inc/serial.h +++ b/Inc/serial.h @@ -17,6 +17,12 @@ enum { MOD_AUTO, }; +enum COLOR{ + NONE = 0, + RED, + BLUE, +}; + union crc16 { unsigned short crc; unsigned char crc_char[2]; @@ -27,21 +33,26 @@ struct pos_data { int32_t y; int32_t z; int32_t time_interval; + int32_t x_c; + int32_t y_c; pos_data() { x = 0; y = 0; z = 0; time_interval = 0; + x_c = 0; + y_c = 0; } }; struct serialData { - unsigned char head[2] {0xAA, 0xAF}; + unsigned char head{0xAA}; + unsigned char id{0x01}; unsigned char cmd{0x01}; unsigned char length{0x18}; pos_data data; unsigned char crc[2]{0x00, 0x00}; - unsigned char end[2]{0xEE, 0xEE}; + unsigned char end[2]{0x0D, 0x0A}; }; class serial { @@ -54,5 +65,6 @@ void setDataCmd(int cmd); void serialClose(); void * serialRead(void *pVoid); void * serialSend(void *pVoid); +void setDataColor(int color); #endif //CV_TEST_SERIAL_H diff --git a/Src/armour.cpp b/Src/armour.cpp index 7bb4d50..8e2eb53 100644 --- a/Src/armour.cpp +++ b/Src/armour.cpp @@ -91,8 +91,8 @@ double pnp_Get_Distance_armour(Rect &roi) { Rodrigues(rvec, rotationMatrix); realistic_distance = tvec.at(2, 0) * 2.0; im_real_weights = (double) real_distance_height / roi.height; - cout << "im_real_weights : " << im_real_weights << endl; - cout << "pnp distance is:" << realistic_distance << endl; +// cout << "im_real_weights : " << im_real_weights << endl; +// cout << "pnp distance is:" << realistic_distance << endl; return realistic_distance; } @@ -379,6 +379,44 @@ Mat armour::rgb_to_gray(const Mat &src_image, int Mode, int R_min = 0, int R_max return result_pic; } +//TODO 判断颜色 +auto armour::judgeColor(const Mat &src_image) { + // 转换为Lab颜色空间 + Mat lab; + cvtColor(src_image, lab, COLOR_BGR2Lab); + + // 分离L、a、b通道 + vector channels; + split(lab, channels); + + // 计算直方图 + int histSize[] = {256}; + float lRanges[] = {0, 256}; + const float *ranges[] = {lRanges}; + cv::MatND hist; + cv::calcHist(&channels[0], 1, 0, cv::Mat(), hist, 1, histSize, ranges, true, false); + + // 找到最大值 + double maxVal = 0; + int maxIdx = 0; + for (int i = 0; i < histSize[0]; i++) { + double val = hist.at(i); + if (val > maxVal) { + maxVal = val; + maxIdx = i; + } + } + + // 判断颜色 + if (maxIdx >= 165 && maxIdx <= 255) { + return RED; + } else if (maxIdx >= 0 && maxIdx <= 95) { + return BLUE; + } else { + return NONE; + } +} + Mat armour::erode_pic(const Mat &src_image, int size_element = 1) { Mat dst_image(src_image.rows, src_image.cols, CV_8UC1); Mat kernel = getStructuringElement(MORPH_RECT, Size(size_element, size_element)); @@ -1458,7 +1496,7 @@ static void *get_fram_thread(void *arg) { } V4l2Capture *cap_thread = V4l2Capture::create(param); - //TODO 需要经过验证 + int thread_flags = 0; while (1) { if (true == process_finish && thread_flags != 0) { @@ -1611,6 +1649,7 @@ int main() { //double cost_time = get_sys_time(); auto_beat.cost_time = get_sys_time(); cap >> src_image; + //Mat HSV_to_gray(const Mat & src_image,int mode,int &h_min,int &h_max ,int &s_min,int &s_max ,int &v_min ,int &v_max ) //Mat dst_image = HSV_to_gray(src_image,h_min,h_max,s_min,s_max,v_min,v_max); //cout << "h_max:" << h_max << endl; @@ -1818,7 +1857,6 @@ void create_test_vector(vector &test) { } } - void armour::select_rect_by_angle(const Rect &select_rect, vector &angle_vector, double distance) { double y_angle = angle_vector[1]; if (fabs(angle_vector[0]) < 1.0); @@ -1836,7 +1874,6 @@ void armour::fire(Mat &src_image) { //int notarget_lable=0; static Point prev_point_fire = Point(0, 0); this->collect_flags = false; - int mode; ofstream file_stream; //AntiKalman::KalmanFilter kf(0,0); //double no_target_label=0; @@ -1860,21 +1897,18 @@ void armour::fire(Mat &src_image) { Rect Roi_rect; //while (MOD_B_R != 3) { - mode = MODE_BLUE; - +// mode = MODE_BLUE; Roi_rect = get_new_roi_rect(target_flags); src_image = src_image(Roi_rect); if (!src_image.data) { cout << "read data failed .. " << endl; exit(-1); } - Mat gray_image = HSV_to_gray(src_image, mode); + Mat gray_image = HSV_to_gray(src_image, BEAT_COLOR); //gray_image = erode_pic(gray_image, 1); - if (mode == MODE_BLUE) - gray_image = dilate_pic(gray_image, 12); - else - gray_image = dilate_pic(gray_image, 12); + gray_image = dilate_pic(gray_image, 12); //display("gray", gray_image); + setDataColor(BEAT_COLOR); vector> vector_Point; get_rect_pic_contour(gray_image, get_point_contours(gray_image), vector_Point); @@ -1897,10 +1931,12 @@ void armour::fire(Mat &src_image) { Point send_point = select_the_rect(result_pic, Rect_vector, armour_area, select_rect); // send_point = Point(send_point.x + Roi_rect.x, send_point.y + Roi_rect.y); + rectangle(result_pic, Rect(select_rect.x - Roi_rect.x, select_rect.y - Roi_rect.y, select_rect.width, select_rect.height), Scalar(0, 255, 255), 2, 8); - circle(result_pic, send_point, 2, Scalar(255, 0, 255), 2, 8); + +// circle(result_pic, send_point, 2, Scalar(255, 0, 255), 2, 8); cost_time = (get_sys_time() - cost_time) / getTickFrequency(); //! 输出 @@ -1922,17 +1958,30 @@ void armour::fire(Mat &src_image) { // cout << "distance is : " << distance_single << endl; distance_single = pnp_Get_Distance_armour(select_rect); target_flags = true; - cout << "distance : " << distance_single << endl; - std::cout << "x :" << send_point.x - 320 << "y:" << send_point.y - 240 << std::endl; + cout << "distance : " << distance_single << endl; //距离 +// std::cout << "x :" << send_point.x - 320 << "y:" << send_point.y - 240 << std::endl; Point armour_target = Point(send_point.x - 320, send_point.y - 240); cout << "armour_target.x:" << armour_target.x << "armour_target.y:" << armour_target.y << endl; + + //反 卡尔曼滤波 预测 Point kalman_point = kf.run(armour_target.x, armour_target.y); - Point anti_kalman_point = kf.Anti_KalmanFilter(armour_target, kalman_point, 0.9); + Point anti_kalman_point = kf.Anti_KalmanFilter(armour_target, kalman_point, 0.9); //0.9为参数 可调 + + //转换点坐标至屏幕左上角(ROI区域) Point2f target_point = Point2f(armour_target.x + 320, armour_target.y + 240); Point2f target_point_kalman = Point2f(anti_kalman_point.x + 320, anti_kalman_point.y + 240); + + //描绘装甲板中心 circle(src_image, target_point, 3, Scalar(0, 255, 0), 2, 8);//current point with green - circle(src_image, target_point_kalman, 3, Scalar(255, 0, 0), 2, 8);//predicted position with red * + //描绘卡尔曼滤波预测点 + circle(src_image, target_point_kalman, 3, Scalar(255, 0, 0), 2, 8);//predicted position with blue * test.push_back(send_point); + + //传入数据 + data_send.x = target_point.x; + data_send.y = target_point.y; + data_send.z = distance_single; + prev_distance = distance_single; prev_point_fire = send_point; @@ -1946,28 +1995,31 @@ void armour::fire(Mat &src_image) { vector angle_vector_predict_right = Calculate_angle(Point(40, 0), Point(send_point.x + select_rect.width / 2.0 - 320, 0), distance_single * 10); - cout << "angle_vector[1] : " << angle_vector[1] << " angle_vector[2]:" << angle_vector[2] << endl; - +// cout << "angle_vector[1] : " << angle_vector[1] << " angle_vector[2]:" << angle_vector[2] << endl; if (angle_vector[0] > 30.0) { - cout << "yaw:" << angle_vector[0] << endl; +// cout << "yaw:" << angle_vector[0] << endl; angle_vector[0] = 4.0; //cout << "enter "<< endl; // cout << "> 40.0" << endl; } else if (angle_vector[0] < -6) { - cout << "yaw :" << angle_vector[0] << endl; +// cout << "yaw :" << angle_vector[0] << endl; angle_vector[0] = -2.0; } - serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, angle_vector_predict_left[0] * 1000, - angle_vector_predict_right[0] * 1000); + + data_send.x_c = angle_vector[0] * 1000; + data_send.y_c = angle_vector[1] * 1000; + + serialPosData(&data_send); +// serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, angle_vector_predict_left[0] * 1000, angle_vector_predict_right[0] * 1000); setDataCmd(1); - vector kalman_angle_vector = Calculate_angle(Point(0, 0), Point(target_point_kalman.x - 320, - target_point_kalman.y - 240), - distance_single); - cout << "x_angle: " << angle_vector[0] << endl; - cout << "PH_angle: " << angle_vector[1] << endl; - cout << "kalman_angle: " << kalman_angle_vector[0] << endl; +// vector kalman_angle_vector = Calculate_angle(Point(0, 0), Point(target_point_kalman.x - 320, +// target_point_kalman.y - 240), +// distance_single); +// cout << "x_angle: " << angle_vector[0] << endl; +// cout << "PH_angle: " << angle_vector[1] << endl; +// cout << "kalman_angle: " << kalman_angle_vector[0] << endl; //! DEBUG 图像 display("display",src_image); @@ -1975,3 +2027,4 @@ void armour::fire(Mat &src_image) { } } + diff --git a/Src/serial.cpp b/Src/serial.cpp index b620c05..a49e400 100644 --- a/Src/serial.cpp +++ b/Src/serial.cpp @@ -67,6 +67,8 @@ serialData serialPosData(pos_data* data) { data1.data.y = data->y; data1.data.z = data->z; data1.data.time_interval = data->time_interval; + data1.data.x_c = data->x_c; + data1.data.y_c = data->y_c; union crc16 crc_data{}; crc_data.crc = crc16((unsigned char*)&data1, sizeof(data1)-4); data1.crc[0] = crc_data.crc_char[0]; @@ -132,4 +134,8 @@ void *serialSend(void *pVoid){ void setDataCmd(int cmd){ data1.cmd = cmd; +} + +void setDataColor(int color){ + data1.id = color; } \ No newline at end of file From 8e0f2549935e4147085d09ca1d996d74f9182119 Mon Sep 17 00:00:00 2001 From: Stardust011 Date: Sat, 1 Apr 2023 01:07:00 +0800 Subject: [PATCH 4/5] =?UTF-8?q?Revert=20"=E4=B8=B2=E5=8F=A3=20(#1)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 3075f8de --- CMakeLists.txt | 94 +++--- Inc/armour.h | 3 +- Inc/config.h | 2 - Inc/led.h | 5 +- Inc/new_uart_2stm32.h | 62 ++++ Inc/new_uart_thread_init.h | 59 ++++ Inc/serial.h | 58 ---- Lib/WzSerialPortPlus/WzSerialPortPlus.h | 7 +- Lib/libv4l2_opencv_mat/no_need | 1 + Src/armour.cpp | 187 ++++++++---- Src/new_uart_thread_init.cpp | 390 ++++++++++++++++++++++++ Src/serial.cpp | 135 -------- main.cpp | 45 +-- 13 files changed, 710 insertions(+), 338 deletions(-) create mode 100644 Inc/new_uart_2stm32.h create mode 100644 Inc/new_uart_thread_init.h delete mode 100644 Inc/serial.h create mode 160000 Lib/libv4l2_opencv_mat/no_need create mode 100644 Src/new_uart_thread_init.cpp delete mode 100644 Src/serial.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d6b09e..40576aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,36 +16,43 @@ include_directories(${OpenCV_INCLUDE_DIRS}) link_libraries(${OpenCV_LIBS}) #CUDA -#find_package(CUDA) -#if (${CUDA_FOUND}) -# set(CUDA_SOURCE_PROPERTY_FORMAT OBJ) -# set(CUDA_SEPARABLE_COMPILATION ON) -# include_directories(${CUDA_INCLUDE_DIRS}) -# set(CUDA_PROPAGATE_HOST_FLAGS OFF) -# set(CUDA_NVCC_FLAGS -gencode arch=compute_20,code=sm_20;-G;-g)#根据具体GPU更改参数 -# #arch=compute_30,code=sm_30 -# #arch=compute_35,code=sm_35 -# #arch=compute_50,code=[sm_50,compute_50] -# #arch=compute_52,code=[sm_52,compute_52] -# #SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_61,code=sm_61;-std=c++11;-O3;-G;-g) -# link_directories($ENV{CUDA_PATH}/lib/x64) -# CUDA_ADD_EXECUTABLE(cv_2022 ${CURRENT_HEADERS} ${CURRENT_SOURCES}) -#else (${CUDA_FOUND}) -# MESSAGE(STATUS "Skip CUDA!") -#endif (${CUDA_FOUND}) +find_package(CUDA) +if (${CUDA_FOUND}) + set(CUDA_SOURCE_PROPERTY_FORMAT OBJ) + set(CUDA_SEPARABLE_COMPILATION ON) + include_directories(${CUDA_INCLUDE_DIRS}) + set(CUDA_PROPAGATE_HOST_FLAGS OFF) + set(CUDA_NVCC_FLAGS -gencode arch=compute_20,code=sm_20;-G;-g)#根据具体GPU更改参数 + #arch=compute_30,code=sm_30 + #arch=compute_35,code=sm_35 + #arch=compute_50,code=[sm_50,compute_50] + #arch=compute_52,code=[sm_52,compute_52] + #SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_61,code=sm_61;-std=c++11;-O3;-G;-g) + link_directories($ENV{CUDA_PATH}/lib/x64) + CUDA_ADD_EXECUTABLE(cv_2022 ${CURRENT_HEADERS} ${CURRENT_SOURCES}) +else (${CUDA_FOUND}) + MESSAGE(STATUS "Skip CUDA!") +endif (${CUDA_FOUND}) -if (WIN32) - file(GLOB CURRENT_HEADERS Inc/*.h Inc/*.hpp Inc/*.cuh) - file(GLOB CURRENT_SOURCES Src/*.cpp Src/*.cu) - include_directories(win-unix/) - file(GLOB WIN_UNIX win-unix/*.h win-unix/*.hpp) -else() - file(GLOB CURRENT_HEADERS Inc/*.h Inc/*.hpp Inc/*.cuh) - file(GLOB CURRENT_SOURCES Src/*.cpp Src/*.cu) -endif() -source_group("Include" FILES ${CURRENT_HEADERS}) -source_group("Source" FILES ${CURRENT_SOURCES}) +#if (WIN32) +# file(GLOB CURRENT_HEADERS Inc/*.h Inc/*.hpp Inc/*.cuh) +# file(GLOB CURRENT_SOURCES Src/*.cpp Src/*.cu) +# include_directories(win-unix/) +# file(GLOB WIN_UNIX win-unix/*.h win-unix/*.hpp) +#else() +# file(GLOB CURRENT_HEADERS Inc/*.h Inc/*.hpp Inc/*.cuh) +# file(GLOB CURRENT_SOURCES Src/*.cpp Src/*.cu) +#endif() +#source_group("Include" FILES ${CURRENT_HEADERS}) +#source_group("Source" FILES ${CURRENT_SOURCES}) +# +#include_directories(./) +#include_directories(Inc/) +#include_directories(Src/) +# +#add_executable(cv_test main.cpp ${CURRENT_HEADERS} ${CURRENT_SOURCES} ${WIN_UNIX}) +#---------------------TEST--------------------- include_directories(./) include_directories(Inc/) include_directories(Src/) @@ -54,36 +61,17 @@ aux_source_directory(Lib/libv4l2_opencv_mat/src libv4l2_SRC_FILES) add_library(libv4l2 STATIC ${libv4l2_SRC_FILES}) target_include_directories(libv4l2 PUBLIC "Lib/libv4l2_opencv_mat/inc") -add_library(WzSerialPortPlus STATIC Lib/WzSerialPortPlus/WzSerialPortPlus.cpp ) +add_library(WzSerialPortPlus STATIC Lib/WzSerialPortPlus/WzSerialPortPlus.cpp) target_include_directories(WzSerialPortPlus PUBLIC "Lib/WzSerialPortPlus") - -add_executable(cv_test ${CURRENT_HEADERS} ${CURRENT_SOURCES} ${WIN_UNIX}) +set(SRC Src/armour.cpp Src/new_uart_thread_init.cpp Src/led.cpp Src/kalman.cpp) +set(INC Inc/armour.h Inc/new_uart_thread_init.h Inc/led.h Inc/kalman.h) +add_executable(cv_test ${SRC} ${INC}) target_link_libraries(cv_test libv4l2) -target_link_libraries(cv_test WzSerialPortPlus) -#---------------------TEST--------------------- -#include_directories(./) -#include_directories(Inc/) -#include_directories(Src/) -# -#aux_source_directory(Lib/libv4l2_opencv_mat/src libv4l2_SRC_FILES) -#add_library(libv4l2 STATIC ${libv4l2_SRC_FILES}) -#target_include_directories(libv4l2 PUBLIC "Lib/libv4l2_opencv_mat/inc") -# -#add_library(WzSerialPortPlus STATIC Lib/WzSerialPortPlus/WzSerialPortPlus.cpp ) -#target_include_directories(WzSerialPortPlus PUBLIC "Lib/WzSerialPortPlus") -# -#set(SRC Src/armour.cpp Src/led.cpp Src/kalman.cpp ) -#set(INC Inc/armour.h Inc/led.h Inc/kalman.h ) -##set(SRC Src/armour.cpp Src/led.cpp Src/kalman.cpp Src/serial.cpp) -##set(INC Inc/armour.h Inc/led.h Inc/kalman.h Inc/serial.h) -#add_executable(cv_test ${SRC} ${INC} Src/serial.cpp Inc/serial.h) -#target_link_libraries(cv_test libv4l2 WzSerialPortPlus) -# -##target_link_libraries(cv_test WzSerialPortPlus) +#target_link_libraries(cv_test WzSerialPortPlus) #---------------------END---------------------- #! -add_executable(test_lib main.cpp Src/serial.cpp Inc/serial.h) +add_executable(test_lib main.cpp) target_link_libraries(test_lib WzSerialPortPlus) \ No newline at end of file diff --git a/Inc/armour.h b/Inc/armour.h index 045c59d..6c4b92c 100644 --- a/Inc/armour.h +++ b/Inc/armour.h @@ -28,8 +28,7 @@ using namespace cv; #endif -//#include "new_uart_thread_init.h" -#include "serial.h" +#include "new_uart_thread_init.h" #include "led.h" #include "kalman.h" #include "BufWu.h" diff --git a/Inc/config.h b/Inc/config.h index 8337beb..ca37850 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -12,7 +12,5 @@ #define IMAGE_WIDTH 3264 #define IMAGE_HEIGHT 2448 #define FPS 30 -#define SERIAL_PORT "/dev/ttyUSB0" -#define SERIAL_BAUDRATE 115200 #endif //CV_TEST_CONFIG_H diff --git a/Inc/led.h b/Inc/led.h index 1642e54..5d1cade 100644 --- a/Inc/led.h +++ b/Inc/led.h @@ -8,11 +8,10 @@ #include //ÓëÉ豞Çý¶¯³ÌÐòÓÐ¹Ø #include //žÄ±äÒÑŽò¿ªµÄÎÄŒþÐÔÖÊ #include -#include +#include //²ÎÊýÉèÖà #include #include -//#include "new_uart_thread_init.h" -#include "serial.h" +#include "new_uart_thread_init.h" using namespace std; diff --git a/Inc/new_uart_2stm32.h b/Inc/new_uart_2stm32.h new file mode 100644 index 0000000..f1dbfc7 --- /dev/null +++ b/Inc/new_uart_2stm32.h @@ -0,0 +1,62 @@ +#ifndef _NEW_UART_2STM32 +#define _NEW_UART_2STM32 +/*: +#pragma pack(8): +*/ +#include +#include +#include +#include + +typedef struct { + + uint8_t Head; + uint8_t CmdID; + uint16_t Datalength; + +}__attribute__((packed, aligned(1))) Frame_hearder; + +typedef struct { + + int32_t x; + int32_t y; + int32_t z; + int32_t Time_Interval; + uint8_t Goal_State; + +}__attribute__((packed, aligned(1))) Armour_MOD; +typedef struct { + + uint8_t Mod_set_over; + +}__attribute__((packed, aligned(1))) Reply_MOD; + +typedef struct { + + float x; + float y; + float z; + uint8_t Target_state; + + +}__attribute__((packed, aligned(1))) Power_MOD; +typedef struct { + + uint8_t Target_state; + +}__attribute__((packed, aligned(1))) Error_MOD; +//ÊÓŸõÊýŸÝœá¹¹ +typedef struct { + Frame_hearder FH_data; + union { + Armour_MOD Ar_data; + Reply_MOD Re_data; + Power_MOD PW_data; + Error_MOD Err_data; + } Data; + uint16_t CRC16; +}__attribute__((packed, aligned(1))) Date_message; + +void send_message_AR(float xdata, float ydata, float zdata, float tdata, uint8_t Cmdata); + +#endif diff --git a/Inc/new_uart_thread_init.h b/Inc/new_uart_thread_init.h new file mode 100644 index 0000000..27d10ca --- /dev/null +++ b/Inc/new_uart_thread_init.h @@ -0,0 +1,59 @@ +#ifndef _NEW_UART_THREAD_INIT_H +#define _NEW_UART_THREAD_INIT_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "new_uart_2stm32.h" + +#define BUFF_REPEAT 1 +#define BUFF_UNREPEAT 2 +#define BUFF_CENTER 3 +#define BUFF_UNCENTER 4 + +#define BEGIN_LED 1 +#define BLUE_LED 2 +#define RED_LED 3 +//²Ëµ¥×÷ΪѡÏî +enum { + MOD_NULL = 0, + MOD_RED, //ºì·½ + MOD_BLUE, //À¶·½ + MOD_POWER_BIG, //´ò´ó·ù + MOD_POWER_SMALL,//С·û + MOD_AUTO, //×Ô¶¯´ò»÷ +}; +/*====send the mesage====*/ +//void send_message_PW(float xdata, float ydata, float zdata, uint8_t Cmdata); +void send_message_AR(float xdata, float ydata, float zdata, float tdata, uint8_t Cmdata); +//void send_message_VR( uint8_t Cmdata); +//void send_message_ER(uint8_t Cmdata); +/*=====the thread====*/ +void *thread_read(void *arg); + +void *thread_write(void *arg); +//void *thread_video(void * arg); +//void *thread_led(void *arg); +/*===== uart =====*/ +int INIT_UART(); + +int UART_WRITE_DATA(char *data, int data_sizeof); + + +#endif/*end the init*/ + diff --git a/Inc/serial.h b/Inc/serial.h deleted file mode 100644 index c229af0..0000000 --- a/Inc/serial.h +++ /dev/null @@ -1,58 +0,0 @@ -// -// Created by Stardust on 2023/3/29. -// - -#ifndef CV_TEST_SERIAL_H -#define CV_TEST_SERIAL_H - -#include -#include "config.h" - -enum { - MOD_NULL = 0, - MOD_RED, - MOD_BLUE, - MOD_POWER_BIG, - MOD_POWER_SMALL, - MOD_AUTO, -}; - -union crc16 { - unsigned short crc; - unsigned char crc_char[2]; -}; - -struct pos_data { - int32_t x; - int32_t y; - int32_t z; - int32_t time_interval; - pos_data() { - x = 0; - y = 0; - z = 0; - time_interval = 0; - } -}; - -struct serialData { - unsigned char head[2] {0xAA, 0xAF}; - unsigned char cmd{0x01}; - unsigned char length{0x18}; - pos_data data; - unsigned char crc[2]{0x00, 0x00}; - unsigned char end[2]{0xEE, 0xEE}; -}; - -class serial { -}; - -bool serialInit(); -serialData serialPosData(int32_t x, int32_t y, int32_t z, int32_t time_interval); -serialData serialPosData(pos_data* data); -void setDataCmd(int cmd); -void serialClose(); -void * serialRead(void *pVoid); -void * serialSend(void *pVoid); - -#endif //CV_TEST_SERIAL_H diff --git a/Lib/WzSerialPortPlus/WzSerialPortPlus.h b/Lib/WzSerialPortPlus/WzSerialPortPlus.h index f1be3e9..4839302 100644 --- a/Lib/WzSerialPortPlus/WzSerialPortPlus.h +++ b/Lib/WzSerialPortPlus/WzSerialPortPlus.h @@ -1,3 +1,6 @@ +// +// Created by Stardust on 2023/3/25. +// /** * @file WzSerialPortPlus.h * @author ouyangwei @@ -15,12 +18,13 @@ #include #include -using ReceiveCallback = std::function; +using ReceiveCallback = std::function; class WzSerialPortPlus { public: WzSerialPortPlus(); + /** * @param name: serialport name , such as: /dev/ttyS1 * @param baudrate: baud rate , valid: 2400,4800,9600,19200,38400,57600,115200,230400 @@ -33,6 +37,7 @@ class WzSerialPortPlus const int& stopbit, const int& databit, const int& paritybit); + ~WzSerialPortPlus(); bool open(); diff --git a/Lib/libv4l2_opencv_mat/no_need b/Lib/libv4l2_opencv_mat/no_need new file mode 160000 index 0000000..abd9ecd --- /dev/null +++ b/Lib/libv4l2_opencv_mat/no_need @@ -0,0 +1 @@ +Subproject commit abd9ecdc3115c54a18f2584a6a7d0b03a27aa356 diff --git a/Src/armour.cpp b/Src/armour.cpp index 7bb4d50..1631ff1 100644 --- a/Src/armour.cpp +++ b/Src/armour.cpp @@ -1,5 +1,5 @@ #include "armour.h" -// serialPosData +// send_message_AR /* * this code was backup in 2019.07.06 * author: Mr.Monster @@ -398,9 +398,9 @@ vector> armour::get_point_contours(const Mat &src_image)//3 { vector> contour_vector; vector contour_hierachy; -// cout << "hello1 " << endl; + cout << "hello1 " << endl; findContours(src_image, contour_vector, contour_hierachy, RETR_CCOMP, CHAIN_APPROX_TC89_L1); -// cout << "hello2" << endl; + cout << "hello2" << endl; //cout << "contour_vector size:" << contour_vector.size() << endl; return contour_vector; } @@ -1405,6 +1405,40 @@ int armour::return_the_exsit_num(string folder) { return file_names.size(); } +/*int main() +{ + pthread_t Read_Uart; + pthread_t Send_Uart; + int ret; + + //sem_init(&sem_main,0,0);// busy + //sem_init(&sem_vedio,0,1); + if (INIT_UART() == -1) + { + std::cout<<"open file!"< pic_array_thread; +// +// int serial_camera = get_usb_camara_serial(BIG_OR_SMALL); +// if (serial_camera == 0) +//// cap_thread = set_v4l("/dev/video0"); +// V4L2DeviceParameters cap_thread("/dev/video0", V4L2_PIX_FMT_MJPEG , IMAGE_WIDTH, IMAGE_HEIGHT, FPS); +// else if (serial_camera == 1) +// V4L2DeviceParameters cap_thread("/dev/video1", V4L2_PIX_FMT_MJPEG , IMAGE_WIDTH, IMAGE_HEIGHT, FPS); +// else +// V4L2DeviceParameters cap_thread("/dev/video0", V4L2_PIX_FMT_MJPEG , IMAGE_WIDTH, IMAGE_HEIGHT, FPS); +// +// int thread_flags = 0; +// +// while (1) { +// if (true == process_finish && thread_flags != 0) { +// /*if(NULL == pic_array_thread.back().data) +// { +// cap >> *src_image; +// process_finish = false; +// }*/ +// pic_array_thread.back().copyTo(*src_image); +// pic_array_thread.clear(); +// process_finish = false; +// } +// if (true == thread_lauch_first) { +// Mat fram; +// fram = VideoCapture_v4l(cap_thread); +// fram.copyTo(*src_image); +// thread_lauch_first = false; +// continue; +// } +// Mat fram_temp; +// fram_temp = VideoCapture_v4l(cap_thread); +// // VideoPlayer(fram); +// if (fram_temp.data == NULL) { +// cout << "fram can't access" << endl; +// exit(-1); +// } +// pic_array_thread.push_back(fram_temp); +// thread_flags++; +// } +// cap_thread->stopCapture(); +// cap_thread->freeBuffers(); +// cap_thread->closeDevice(); +//} + + static void *get_fram_thread(void *arg) { Mat *src_image = (Mat *) arg; vector pic_array_thread; @@ -1501,38 +1585,20 @@ static void *get_fram_thread(void *arg) { } int main() { - //init the thread pthread_t Read_Uart; pthread_t Send_Uart; pthread_t led_thread; pthread_t get_fram; int ret; - //init the sem + sem_init(&finish_sem, 0, 0);//init the signal sem_init(&first_sem, 0, 1); //usleep(1000*10000); //sem_init(&sem_main,0,0);// busy //sem_init(&sem_vedio,0,1); - - //! 重构 - if (serialInit() == false){ - cout << "serial init fail" << endl; -// return 0; - } - ret = pthread_create(&Read_Uart, NULL, serialRead, NULL); - if (ret != 0) { - cout << "read_fail" << endl; - } - ret = pthread_create(&Send_Uart, NULL, serialSend, NULL); - ret = pthread_create(&led_thread,NULL, led_on_thread, NULL); - if (ret != 0) { - std::cout << "led_fail" << std::endl; - } - Mat src_image(Size(640, 480), CV_8UC3); - - /*if (INIT_UART() == -1) { + if (INIT_UART() == -1) { std::cout << "open file!" << std::endl; //return 0; } @@ -1549,7 +1615,7 @@ int main() { if (ret != 0) { std::cout << "led_fail" << std::endl; } - Mat src_image(Size(640, 480), CV_8UC3);*/ + Mat src_image(Size(640, 480), CV_8UC3); #ifdef thread_lauch ret = pthread_create(&get_fram,NULL,get_fram_thread,(void *)&src_image); if(ret != 0) @@ -1631,15 +1697,10 @@ int main() { if (src_image.data == NULL) { cout << "wrong read_data" << endl; } - - //装甲板 if (MOD_B_R != 3) { cout << "auto beat" << endl; - //串口数据在fire中发送 auto_beat.fire(src_image); } - - //能量机关? if (MOD_B_R == 3) { Rect buf_out_rect; @@ -1657,18 +1718,46 @@ int main() { Point(buf_out_point.x - 320, buf_out_point.y - 240), 800.0); if (ERROR_POINT_BUF == buf_out_point) { - //serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0, 0); - serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0); - setDataCmd(0); + send_message_AR(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0, 0); // cout << "debug ... " << endl; } cout << "yaw :" << angle_vector[0] << " , ph:" << angle_vector[1] << endl; // std::cout << "enter big buf mode" << std::endl; - //serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0, 1); - serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0); - setDataCmd(1); + send_message_AR(angle_vector[0] * 1000, angle_vector[1] * 1000, 1, 0, 1); cout << "debug ..." << endl; } + /* if(MOD_B_R == 3) + { + // display("src",src_image); + Rect buf_out_rect ; + double never_used_weights = 0; + double img_l=0,real_l=0; + //cap >> src_image; + //if(src_image.data == NULL) cout << "wrong" << endl; + //imshow("debug",src_image); + //cap.release(); + Point2f buf_out_point; + buf_out_point=buf.Detect_Mainfunction(src_image,img_l,real_l,false); + // imshow("src_image", src_image); + waitKey(10); + // display("bufWu_Pic",src_image); + // cout << "debug .. " << endl; + cout << "buf_point (" << buf_out_point.x << "," << buf_out_point.y << ")" << endl; + vector angle_vector = Calculate_angle_buf(Point(0,0),Point(buf_out_point.x - 320,buf_out_point.y - 240),80.0); + if(ERROR_POINT_BUF == buf_out_point) + { + send_message_AR(angle_vector[0] * 1000,angle_vector[1] * 1000,1,0,0); + // cout << "debug ... " << endl; + cout << "never find target" << endl; + continue; + } + cout << "yaw :" << angle_vector[0] << " , ph:" << angle_vector[1] << endl; + // std::cout << "enter big buf mode" << std::endl; + send_message_AR(angle_vector[0] * 1000,angle_vector[1] * 1000,1,0,1); + cout << "debug ..." << endl; + }*/ + + } //cap_thread->stopCapture(); @@ -1902,9 +1991,7 @@ void armour::fire(Mat &src_image) { Scalar(0, 255, 255), 2, 8); circle(result_pic, send_point, 2, Scalar(255, 0, 255), 2, 8); cost_time = (get_sys_time() - cost_time) / getTickFrequency(); - - //! 输出 -// display("display" , result_pic); + //display("display" , result_pic); cout << "fps:" << 1. / cost_time << endl; //index_cap.open(0); if (send_point == ERROR_POINT) { @@ -1912,14 +1999,12 @@ void armour::fire(Mat &src_image) { kf.ResetKalmanFilter(320, 240); { cout << "x:" << 0 << " y:" << 0 << endl; - serialPosData(0, 0, distance_single, cost_time); - setDataCmd(1); + send_message_AR(0, 0, distance_single, cost_time, 1); } test.push_back(Point(320, 240)); - system("clear"); return; } -// cout << "distance is : " << distance_single << endl; + cout << "distance is : " << distance_single << endl; distance_single = pnp_Get_Distance_armour(select_rect); target_flags = true; cout << "distance : " << distance_single << endl; @@ -1936,7 +2021,6 @@ void armour::fire(Mat &src_image) { prev_distance = distance_single; prev_point_fire = send_point; - //验证角度 angle_vector = Calculate_angle(Point(50, 0), Point(send_point.x - 320, send_point.y - 240), distance_single * 10); vector angle_vector_predict_left = Calculate_angle(Point(50, 0), @@ -1944,8 +2028,8 @@ void armour::fire(Mat &src_image) { send_point.x + select_rect.width / 2.0 - 320), distance_single * 10); vector angle_vector_predict_right = Calculate_angle(Point(40, 0), - Point(send_point.x + select_rect.width / 2.0 - 320, 0), - distance_single * 10); + Point(send_point.x + select_rect.width / 2.0 - 320, + 0), distance_single * 10); cout << "angle_vector[1] : " << angle_vector[1] << " angle_vector[2]:" << angle_vector[2] << endl; @@ -1958,20 +2042,19 @@ void armour::fire(Mat &src_image) { cout << "yaw :" << angle_vector[0] << endl; angle_vector[0] = -2.0; } - serialPosData(angle_vector[0] * 1000, angle_vector[1] * 1000, angle_vector_predict_left[0] * 1000, - angle_vector_predict_right[0] * 1000); - setDataCmd(1); + send_message_AR(angle_vector[0] * 1000, angle_vector[1] * 1000, angle_vector_predict_left[0] * 1000, + angle_vector_predict_right[0] * 1000, 1); vector kalman_angle_vector = Calculate_angle(Point(0, 0), Point(target_point_kalman.x - 320, target_point_kalman.y - 240), - distance_single); + distance_single); cout << "x_angle: " << angle_vector[0] << endl; cout << "PH_angle: " << angle_vector[1] << endl; cout << "kalman_angle: " << kalman_angle_vector[0] << endl; - //! DEBUG 图像 - display("display",src_image); - system("clear"); } + return; } + + diff --git a/Src/new_uart_thread_init.cpp b/Src/new_uart_thread_init.cpp new file mode 100644 index 0000000..6189b48 --- /dev/null +++ b/Src/new_uart_thread_init.cpp @@ -0,0 +1,390 @@ +#include "new_uart_thread_init.h" +#include "new_uart_2stm32.h" +#include +#include +#include + +#define PATHNAME "/dev/ttyUSB0" +#define RandRate B115200 + +bool READ_DATA = false; +bool Set_Mod = true; +int MOD_B_R = 1; +int UART_ID; +int CMD_COLOR = 1; +int DATA_UP_Ar = 23; + + +Date_message DOWN_DATA; +Date_message DOWN_DATA_AR; + +static unsigned short int Get_CRC16_Check_Sum(char *pchMessage, int dwLength, int wCRC); + +void Append_CRC16_Check_Sum_Judge(char *pchMessage, int dwLength) { + //TODO by github copilot + unsigned short int wCRC = 0xffff; + wCRC = Get_CRC16_Check_Sum(pchMessage, dwLength, wCRC); + pchMessage[dwLength] = (char) (wCRC >> 8); + pchMessage[dwLength + 1] = (char) (wCRC & 0x00ff); +} + +int UART_WRITE_DATA(char *data, int data_sizeof) { + //TODO by github copilot + int ret = write(UART_ID, data, data_sizeof); + if (ret < 0) { + printf("write error!\r"); + } +} + +unsigned short int CRC_INIT_Judge = 0xffff; +typedef char u8; +const char CRC8_INIT_Judge = 0xff; +const unsigned char CRC8_TAB_Judge[256] = + { + 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41, + 0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e, 0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc, + 0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0, 0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62, + 0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d, 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff, + 0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5, 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07, + 0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58, 0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a, + 0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6, 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24, + 0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b, 0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9, + 0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f, 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd, + 0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92, 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50, + 0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c, 0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee, + 0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1, 0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73, + 0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49, 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b, + 0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16, + 0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8, + 0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35, + }; + +unsigned int Verify_CRC16_Check_Sum_Judge(char *pchMessage, int dwLength); + +void *thread_read(void *arg) { + //unsigned char coordinate_num_temp[16] = {0}; + char coordinate_num[17] = {}; + //char *coordinate_num_temp; + //coordinate_num_temp = (char *)malloc(sizeof(char) * 4); + int ret; + bool bule_or_red = true; + int a; + //int led_fd;//the auto led + + /*=========== debuging =========*/ + //bule_or_red = false; // unread the uart + /*=========== debuging ========= + led_fd = open("/sys/class/gpio/export ",O_WRONLY); + if(led_fd == -1) + { + printf("open1 led error!\n"); + } + write(led_fd,"48",sizeof("48")); + close(led_fd); + led_fd = open("/sys/class/gpio/gpio158/value",O_RDWR); + if(led_fd == -1) + { + printf("open led error!\n"); + } + */ + //memset(coordinate_num, 0, sizeof(coordinate_num)); + //memset(coordinate_num_temp, 0, sizeof(char) * 4); + //printf("original : %x\n",coordinate_num_temp); +// printf("original : %x\n",coordinate_num); + while (bule_or_red) { + //printf("coordinate_num size is : %d\n",sizeof(coordinate_num)); + //ret = read(UART_ID,coordinate_num,sizeof(coordinate_num));//½«´®¿ÚÊý¾Ý¸øcoordinate + ret = read(UART_ID, coordinate_num, sizeof(coordinate_num)); + //ret = read(UART_ID,coordinate_num,sizeof(coordinate_num)); + + + //printf("ret is:%d\n",ret); + //printf("recv: %x\n",coordinate_num); + //printf("recv: %4x\n",coordinate_num); + //a = Verify_CRC16_Check_Sum_Judge(coordinate_num,7); + //printf("the check is %d\n",a); + //printf("recv2: %02x\n",(unsigned char)coordinate_num[0]); + //printf("bb!\n"); + if ((unsigned char) coordinate_num[0] == 0xA5 && (unsigned char) coordinate_num[1] == 0x05) { + + //printf("recv data\n"); + //printf("bb!\n"); + if (coordinate_num[4] == 0x01) { + + MOD_B_R = MOD_RED; + READ_DATA = true; + Set_Mod = true; + CMD_COLOR = 1; + //bule_or_red = false; + } else if (coordinate_num[4] == 0x02) { + + MOD_B_R = MOD_BLUE; + READ_DATA = true; + Set_Mod = true; + CMD_COLOR = 2; + //bule_or_red = false; + } + /*else if(coordinate_num[4] == 0x03) + { + MOD_B_R = MOD_AUTO; + READ_DATA = true; + Set_Mod = true; + write(led_fd,LED_H,sizeof(LED_H)); + }*/ + else if (coordinate_num[4] == 0x04) { + + //printf("enter power big .." ); + MOD_B_R = MOD_POWER_BIG; + /*if(coordinate_num[5] == 0x01) + { + //return to the center + re_center = BUFF_CENTER; + + }else if(coordinate_num[5] == 0x02) + { + //re to play + re_buff = BUFF_REPEAT; + }*/ + READ_DATA = true; + Set_Mod = true; + + } + /*else if(coordinate_num[4] == 0x05) + { + //Ñ¡¶¨Ð¡·ù + MOD_B_R = MOD_POWER_SMALL; + if(coordinate_num[5] == 0x01) + { + //return to the center + re_center = BUFF_CENTER; + + }else if(coordinate_num[5] == 0x02) + { + //re to play + re_buff = BUFF_REPEAT; + } + READ_DATA = true; + Set_Mod = true; + }*/ + else { + + MOD_B_R = MOD_NULL; + } + + //Pitch = coordinate_num[4]/10+coordinate_num[4]%10;//Ôݶ¨********************** + } + } + return 0; +} + +const unsigned short int wCRC_Table_Judge[256] = + { + 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, + 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, + 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, + 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, + 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, + 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, + 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, + 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, + 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, + 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, + 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, + 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, + 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, + 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, + 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, + 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, + 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, + 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, + 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, + 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, + 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, + 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, + 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, + 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, + 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, + 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, + 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, + 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, + 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, + 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, + 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, + 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 + }; + + +void send_message_AR(float xdata, float ydata, float zdata, float tdata, uint8_t Cmdata) { + memset(&DOWN_DATA_AR, 0, sizeof(DOWN_DATA_AR)); + DOWN_DATA_AR.FH_data.Head = 0xA5; + DOWN_DATA_AR.FH_data.CmdID = 0x01; + DOWN_DATA_AR.FH_data.Datalength = 17; + + DOWN_DATA_AR.Data.Ar_data.x = (int32_t) xdata; + DOWN_DATA_AR.Data.Ar_data.y = (int32_t) ydata; + DOWN_DATA_AR.Data.Ar_data.z = (int32_t) zdata; + DOWN_DATA_AR.Data.Ar_data.Time_Interval = (int32_t) tdata; + DOWN_DATA_AR.Data.Ar_data.Goal_State = Cmdata; + Append_CRC16_Check_Sum_Judge((char *) &DOWN_DATA_AR, DATA_UP_Ar); + +} + + +int set_uart_mode(speed_t speed, int vtime, int vmin) { + struct termios newtio; + + if (UART_ID <= 0) { + printf("set_uart_mode failed---fd=%d\n", UART_ID); + return -1; + } + + bzero(&newtio, sizeof(newtio)); + newtio.c_iflag |= IGNBRK | BRKINT; + newtio.c_cflag |= CLOCAL | CREAD; + newtio.c_cflag &= ~CSIZE; + newtio.c_cflag |= CS8; + newtio.c_cflag &= ~PARENB; + cfsetispeed(&newtio, speed); + cfsetospeed(&newtio, speed); + newtio.c_cflag &= ~CSTOPB; + newtio.c_cc[VTIME] = vtime; + newtio.c_cc[VMIN] = vmin; + tcflush(UART_ID, TCIFLUSH); + + if ((tcsetattr(UART_ID, TCSANOW, &newtio)) != 0) { + printf("set_uart_mode failed\n"); + return -1; + } + + printf("set_uart_mode\n"); + return 0; +} + +int UART_WRITE_DATA(unsigned char *data, int data_sizeof) { + int ret; + //printf("the data is %c\n",data); + ret = write(UART_ID, data, data_sizeof); + //printf("data is : %02x\n",data); + //printf("sizeof data: %d,data size of : %d,write data: %d\n",sizeof(data),data_sizeof,ret); + if (ret <= 0) { + return -1; + } + return ret; +} + +int INIT_UART() { + UART_ID = open(PATHNAME, O_RDWR | O_NOCTTY); + if (UART_ID < 0) { + printf("open UART3 fail\n"); + return -1; + } + printf("open UART3 success\n"); + set_uart_mode(RandRate, 0, 1); + return 1; +} + +void Append_CRC16_Check_Sum_Judge(unsigned char *pchMessage, int dwLength) { + int wCRC = 0; + if ((pchMessage == 0) || (dwLength <= 2)) { + return; + } + wCRC = Get_CRC16_Check_Sum((u8 *) pchMessage, dwLength - 2, CRC_INIT_Judge); + pchMessage[dwLength - 2] = 0x0d;//(u8)(wCRC & 0x00ff); + pchMessage[dwLength - 1] = 0x0a;//(u8)((wCRC >> 8) & 0x00ff); +} + +unsigned int Verify_CRC16_Check_Sum_Judge(char *pchMessage, int dwLength) { + int wExpected = 0; + if ((pchMessage == 0) || (dwLength <= 2)) { + return 0; + } + wExpected = Get_CRC16_Check_Sum(pchMessage, dwLength - 2, CRC_INIT_Judge); + return ((wExpected & 0xff) == pchMessage[dwLength - 2] && ((wExpected >> 8) & 0xff) == pchMessage[dwLength - 1]); +} + +static unsigned short int Get_CRC16_Check_Sum(char *pchMessage, int dwLength, int wCRC) { + char chData; + if (pchMessage == 0) { + return 0xFFFF; + } + while (dwLength--) { + chData = *pchMessage++; + (wCRC) = ((int) (wCRC) >> 8) ^ wCRC_Table_Judge[((int) (wCRC) ^ + (int) (chData)) & 0x00ff]; + } + return wCRC; +} + +void *thread_write(void *arg) { + int ret; + + unsigned char data_red_verify[] = {0xA5, 0x03, 0x01, 0x00, 0x01}; + unsigned char data_blue_verify[] = {0xA5, 0x03, 0x01, 0x00, 0x02}; + // char data_auto_verify[] = {0xA5,0x03,0x01,0x00,0x03}; + // char data_power_small_verify[] = {0xA5,0x03,0x01,0x00,0x05}; + // char data_power_big_verify[] = {0xA5,0x03,0x01,0x00,0x04}; + + + while (1) { + //if(READ_DATA == false) + //{ + // Append_CRC8_Check_Sum_Judge(data_write, 6);//¼ÓÉÏCR¼ìÑéλ + // ret = UART_WRITE_DATA(data_write,sizeof(data_write)); + // printf("write = %d\n",ret); + //} + //else if(READ_DATA == true) + //{ + /*=============debuging==============*/ + // MOD_B_R = MOD_RED ; //Direct into the red pattern + /*=============debuging==============*/ + switch (MOD_B_R) { + case MOD_RED: + if (Set_Mod == true) { + //·¢ËÍУÑéÊý¾ÝÖ»·¢ËÍÒ»´Î + Append_CRC16_Check_Sum_Judge(data_red_verify, sizeof(data_red_verify) + 2); + ret = UART_WRITE_DATA(data_red_verify, sizeof(data_red_verify) + 2); + Set_Mod = false; + break; + } + //Append_CRC8_Check_Sum_Judge(data_armour, sizeof(data_armour)); + /*====Red and blue doesn't write data===*/ + + ret = UART_WRITE_DATA((char *) &DOWN_DATA_AR, DATA_UP_Ar); + break; + case MOD_BLUE: + if (Set_Mod == true) { + Append_CRC16_Check_Sum_Judge(data_blue_verify, sizeof(data_blue_verify) + 2); + ret = UART_WRITE_DATA(data_blue_verify, sizeof(data_blue_verify) + 2); + Set_Mod = false; + break; + } + // Append_CRC8_Check_Sum_Judge(data_armour, sizeof(data_armour)); + /*====Red and blue doesn't write data===*/ + + ret = UART_WRITE_DATA((char *) &DOWN_DATA_AR, DATA_UP_Ar); + break; + case MOD_POWER_BIG: + if (Set_Mod == true) { + //·¢ËÍУÑéÊý¾ÝÖ»·¢ËÍÒ»´Î + Append_CRC16_Check_Sum_Judge(data_red_verify, sizeof(data_red_verify) + 2); + ret = UART_WRITE_DATA(data_red_verify, sizeof(data_red_verify) + 2); + Set_Mod = false; + break; + } + //Append_CRC8_Check_Sum_Judge(data_armour, sizeof(data_armour)); + /*====Red and blue doesn't write data===*/ + + ret = UART_WRITE_DATA((char *) &DOWN_DATA_AR, DATA_UP_Ar); + break; + + default: + break; + } + + usleep(1000 * 3);//us + + + } + return 0; +} + diff --git a/Src/serial.cpp b/Src/serial.cpp deleted file mode 100644 index b620c05..0000000 --- a/Src/serial.cpp +++ /dev/null @@ -1,135 +0,0 @@ -// -// Created by Stardust on 2023/3/29. -// - -#include "serial.h" - -int MOD_B_R = 1; -bool READ_DATA; -bool Set_Mod = true; -int CMD_COLOR = 1; - -WzSerialPortPlus serialPort; -serialData data1; - -/*! - * @brief 初始化串口 - */ -bool serialInit() { - // Init serial port - bool ret = serialPort.open(SERIAL_PORT, SERIAL_BAUDRATE, 1, 8, 'n'); - return ret; -} - -/*! - * @brief CRC16校验 - * @param data_p - * @param length - * @return unsigned short crc16 - */ -static unsigned short crc16(const unsigned char* data_p, unsigned char length){ - unsigned char x; - unsigned short crc = 0xFFFF; - - while (length--){ - x = crc >> 8 ^ *data_p++; - x ^= x>>4; - crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x); - } - return crc; -} - -/*! - * @brief 发送数据 - * @param x - * @param y - * @param z - * @param time_interval - */ -serialData serialPosData(int32_t x, int32_t y, int32_t z, int32_t time_interval) { - data1.data.x = x; - data1.data.y = y; - data1.data.z = z; - data1.data.time_interval = time_interval; - union crc16 crc_data{}; - crc_data.crc = crc16((unsigned char*)&data1, sizeof(data1)-4); - data1.crc[0] = crc_data.crc_char[0]; - data1.crc[1] = crc_data.crc_char[1]; - return data1; -} - -/*! - * @brief 发送数据 - * @param pos_data data - */ -serialData serialPosData(pos_data* data) { - data1.data.x = data->x; - data1.data.y = data->y; - data1.data.z = data->z; - data1.data.time_interval = data->time_interval; - union crc16 crc_data{}; - crc_data.crc = crc16((unsigned char*)&data1, sizeof(data1)-4); - data1.crc[0] = crc_data.crc_char[0]; - data1.crc[1] = crc_data.crc_char[1]; - serialPort.send((char *) &data1, sizeof(data1)); - return data1; -} - -/*! - * @brief 关闭串口 - */ -void serialClose() { - serialPort.close(); -} - -/*! - * @brief 读取串口数据 - * @param char *data - * @param int length - * - * 有一说一 我根本不知道为什么这里是这样的 - */ -void *serialRead(void *pVoid){ - for(;;) { - serialPort.setReceiveCalback([&](char* data, int length){ - printf("received: %s\n",data); - if ((unsigned char) data[0] == 0xA5 && (unsigned char) data[1] == 0x05) { - - if (data[4] == 0x01) { - - MOD_B_R = MOD_RED; - READ_DATA = true; - Set_Mod = true; - CMD_COLOR = 1; - //bule_or_red = false; - } else if (data[4] == 0x02) { - - MOD_B_R = MOD_BLUE; - READ_DATA = true; - Set_Mod = true; - CMD_COLOR = 2; - //bule_or_red = false; - } else if (data[4] == 0x04) { - - //printf("enter power big .." ); - MOD_B_R = MOD_POWER_BIG; - - READ_DATA = true; - Set_Mod = true; - } else { - MOD_B_R = MOD_NULL; - } - } - }); - } -} - -void *serialSend(void *pVoid){ - for (;;) { - serialPort.send((char *) &data1, sizeof(data1)); - } -} - -void setDataCmd(int cmd){ - data1.cmd = cmd; -} \ No newline at end of file diff --git a/main.cpp b/main.cpp index a5a3f20..16a9f7b 100644 --- a/main.cpp +++ b/main.cpp @@ -5,41 +5,22 @@ #include #include #include -#include "serial.h" - -using namespace std; - -unsigned short crc16(const unsigned char* data_p, unsigned char length){ - unsigned char x; - unsigned short crc = 0xFFFF; - - while (length--){ - x = crc >> 8 ^ *data_p++; - x ^= x>>4; - crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x); - } - return crc; -} int main() { WzSerialPortPlus serialPort; - serialData serialData1; - serialPort.open("/dev/ttyUSB0", 115200, 1, 8, 'n'); - - int i; - cout.setf(ios::hex); - union crc16 crc_data{}; - - serialData1 = serialPosData(1, 2, 3, 4); - crc_data.crc = crc16((unsigned char*)&serialData1, sizeof(serialData1)-4); - serialData1.crc[0] = crc_data.crc_char[0]; - serialData1.crc[1] = crc_data.crc_char[1]; - serialPort.send((char*)&serialData1, sizeof(serialData1)); - - for(i=0;i Date: Sat, 1 Apr 2023 18:25:26 +0800 Subject: [PATCH 5/5] Create LICENSE --- LICENSE | 674 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 674 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +.