some refactor

This commit is contained in:
xinyang
2019-05-17 22:52:35 +08:00
parent 77a58511c9
commit 424cc724d8
13 changed files with 212 additions and 197 deletions

View File

@@ -32,7 +32,7 @@ INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/armor/include)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/others/include) INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/others/include)
FILE(GLOB_RECURSE sourcefiles "others/src/*.cpp" "energy/src/*cpp" "armor/src/*.cpp") FILE(GLOB_RECURSE sourcefiles "others/src/*.cpp" "energy/src/*cpp" "armor/src/*.cpp")
ADD_EXECUTABLE(${BIN_NAME} main.cpp ${sourcefiles} ) ADD_EXECUTABLE(${BIN_NAME} main.cpp ${sourcefiles} others/include/additions/additions.h)
TARGET_LINK_LIBRARIES(${BIN_NAME} ${CMAKE_THREAD_LIBS_INIT}) TARGET_LINK_LIBRARIES(${BIN_NAME} ${CMAKE_THREAD_LIBS_INIT})
TARGET_LINK_LIBRARIES(${BIN_NAME} ${OpenCV_LIBS}) TARGET_LINK_LIBRARIES(${BIN_NAME} ${OpenCV_LIBS})

View File

@@ -9,14 +9,11 @@
#include <opencv2/tracking.hpp> #include <opencv2/tracking.hpp>
#include <serial/serial.h> #include <serial/serial.h>
#include <armor_finder/classifier/classifier.h> #include <armor_finder/classifier/classifier.h>
#include "additions/additions.h"
typedef enum{
ENEMY_BLUE, ENEMY_RED
} EnemyColor;
class ArmorFinder{ class ArmorFinder{
public: public:
ArmorFinder(EnemyColor &color, Serial &u, string paras_folder, const bool &use); ArmorFinder(uint8_t &color, Serial &u, string paras_folder, const uint8_t &use);
~ArmorFinder() = default; ~ArmorFinder() = default;
private: private:
@@ -26,7 +23,7 @@ private:
SEARCHING_STATE, TRACKING_STATE, STANDBY_STATE SEARCHING_STATE, TRACKING_STATE, STANDBY_STATE
} State; } State;
const EnemyColor &enemy_color; const uint8_t &enemy_color;
State state; State state;
cv::Rect2d armor_box; cv::Rect2d armor_box;
cv::Ptr<cv::Tracker> tracker; cv::Ptr<cv::Tracker> tracker;
@@ -36,7 +33,7 @@ private:
int contour_area; int contour_area;
Serial &serial; Serial &serial;
const bool &use_classifier; const uint8_t &use_classifier;
bool stateSearchingTarget(cv::Mat &src); bool stateSearchingTarget(cv::Mat &src);
bool stateTrackingTarget(cv::Mat &src); bool stateTrackingTarget(cv::Mat &src);

View File

@@ -8,7 +8,7 @@
#include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
ArmorFinder::ArmorFinder(EnemyColor &color, Serial &u, string paras_folder, const bool &use) : ArmorFinder::ArmorFinder(uint8_t &color, Serial &u, string paras_folder, const uint8_t &use) :
serial(u), serial(u),
enemy_color(color), enemy_color(color),
state(STANDBY_STATE), state(STANDBY_STATE),

View File

@@ -21,7 +21,7 @@ static void splitBayerBG(cv::Mat &src, cv::Mat &blue, cv::Mat &red) {
} }
} }
void imageColorSplit(cv::Mat &src_input, cv::Mat &split, EnemyColor color) { void imageColorSplit(cv::Mat &src_input, cv::Mat &split, uint8_t color) {
cv::Mat blue(240, 320, CV_8UC1), red(240, 320, CV_8UC1); cv::Mat blue(240, 320, CV_8UC1), red(240, 320, CV_8UC1);
if(src_input.type() == CV_8UC1){ if(src_input.type() == CV_8UC1){
splitBayerBG(src_input, blue, red); splitBayerBG(src_input, blue, red);

View File

@@ -8,7 +8,7 @@
#include <opencv2/core.hpp> #include <opencv2/core.hpp>
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
void imageColorSplit(cv::Mat &src_input, cv::Mat &split, EnemyColor color); void imageColorSplit(cv::Mat &src_input, cv::Mat &split, uint8_t color);
void imagePreProcess(cv::Mat &src); void imagePreProcess(cv::Mat &src);
#endif /* _IMAGE_PROCESS_H_ */ #endif /* _IMAGE_PROCESS_H_ */

View File

@@ -4,10 +4,12 @@
#ifndef CONSTANT_H #ifndef CONSTANT_H
#define CONSTANT_H #define CONSTANT_H
#include "additions/additions.h"
#define d2r (CV_PI / 180.0) #define d2r (CV_PI / 180.0)
const int ALLY_BLUE = 123; const int ALLY_BLUE = ENEMY_RED;
const int ALLY_RED = 456; const int ALLY_RED = ENEMY_BLUE;
const int SRC_WIDTH_CAMERA = 640; const int SRC_WIDTH_CAMERA = 640;
const int SRC_HEIGHT_CAMERA = 480; const int SRC_HEIGHT_CAMERA = 480;
const int SRC_WIDTH = 320; const int SRC_WIDTH = 320;

View File

@@ -15,12 +15,13 @@
#include "energy/constant.h" #include "energy/constant.h"
#include "energy/param_struct_define.h" #include "energy/param_struct_define.h"
#include "serial/serial.h" #include "serial/serial.h"
#include "additions/additions.h"
using std::vector; using std::vector;
class Energy { class Energy {
public: public:
Energy(Serial &u, int &ally_color); Energy(Serial &u, uint8_t &enemy_color);
~Energy(); ~Energy();
int run(cv::Mat &src); int run(cv::Mat &src);
@@ -28,7 +29,7 @@ public:
clock_t start; clock_t start;
Serial &serial; Serial &serial;
void setAllyColor(int color); // void setAllyColor(int color);
void setRotation(int rotation); void setRotation(int rotation);
void extract(cv::Mat &src); void extract(cv::Mat &src);
@@ -51,7 +52,7 @@ private:
double last_target_position; double last_target_position;
double last_hit_position; double last_hit_position;
float target_armor; float target_armor;
int &ally_color_; uint8_t &ally_color;
int energy_part_rotation; int energy_part_rotation;
float attack_distance; float attack_distance;
int send_cnt; int send_cnt;

View File

@@ -28,10 +28,10 @@ void Energy::imagePreprocess(cv::Mat &src) {
if(src.type() == CV_8UC1) if(src.type() == CV_8UC1)
{ {
splitBayerBG(src, src_blue, src_red); splitBayerBG(src, src_blue, src_red);
if(ally_color_ == ALLY_RED) if(ally_color == ALLY_RED)
{ {
src = src_red - src_blue; src = src_red - src_blue;
}else if(ally_color_ == ALLY_BLUE){ }else if(ally_color == ALLY_BLUE){
src = src_blue - src_red; src = src_blue - src_red;
} }
} }
@@ -44,11 +44,11 @@ void Energy::imagePreprocess(cv::Mat &src) {
resize(channels.at(0), src_blue, Size(SRC_WIDTH, SRC_HEIGHT)); resize(channels.at(0), src_blue, Size(SRC_WIDTH, SRC_HEIGHT));
resize(channels.at(1), src_green, Size(SRC_WIDTH, SRC_HEIGHT)); resize(channels.at(1), src_green, Size(SRC_WIDTH, SRC_HEIGHT));
resize(channels.at(2), src_red, Size(SRC_WIDTH, SRC_HEIGHT)); resize(channels.at(2), src_red, Size(SRC_WIDTH, SRC_HEIGHT));
if(ally_color_ == ALLY_RED) if(ally_color == ALLY_RED)
{ {
src = src_red-src_blue; src = src_red-src_blue;
//src=src_red; //src=src_red;
}else if(ally_color_ == ALLY_BLUE){ }else if(ally_color == ALLY_BLUE){
src = src_blue-src_red; src = src_blue-src_red;
//src=src_blue; //src=src_blue;
} }

View File

@@ -8,7 +8,7 @@ using std::cout;
using std::endl; using std::endl;
using std::vector; using std::vector;
Energy::Energy(Serial &u, int &ally_color):serial(u),ally_color_(ally_color), Energy::Energy(Serial &u, uint8_t &enemy_color):serial(u),ally_color(enemy_color),
src_blue(SRC_HEIGHT, SRC_WIDTH, CV_8UC1), src_blue(SRC_HEIGHT, SRC_WIDTH, CV_8UC1),
src_red(SRC_HEIGHT, SRC_WIDTH, CV_8UC1) src_red(SRC_HEIGHT, SRC_WIDTH, CV_8UC1)
{ {
@@ -26,7 +26,7 @@ Energy::Energy(Serial &u, int &ally_color):serial(u),ally_color_(ally_color),
target_armor = -1; target_armor = -1;
radius = 0; radius = 0;
ally_color_ = ALLY_RED; // ally_color = ALLY_RED;
energy_part_rotation = CLOCKWISE; energy_part_rotation = CLOCKWISE;
attack_distance = ATTACK_DISTANCE; attack_distance = ATTACK_DISTANCE;
count = 1; count = 1;
@@ -58,11 +58,11 @@ Energy::Energy(Serial &u, int &ally_color):serial(u),ally_color_(ally_color),
Energy::~Energy() = default; Energy::~Energy() = default;
//
void Energy::setAllyColor(int color) //void Energy::setAllyColor(int color)
{ //{
ally_color_ = color; // ally_color_ = color;
} //}
void Energy::setRotation(int rotation){ void Energy::setRotation(int rotation){
energy_part_rotation = rotation; energy_part_rotation = rotation;

View File

@@ -8,8 +8,9 @@ using std::cout;
using std::endl; using std::endl;
using std::vector; using std::vector;
extern float curr_yaw, curr_pitch, mark_yaw, mark_pitch; //extern float curr_yaw, curr_pitch, mark_yaw, mark_pitch;
extern int mark; //extern int mark;
int Energy::run(cv::Mat &src){ int Energy::run(cv::Mat &src){
imshow("src",src); imshow("src",src);
@@ -78,11 +79,12 @@ int Energy::run(cv::Mat &src){
sendTargetByUart(yaw_rotation, pitch_rotation, attack_distance); sendTargetByUart(yaw_rotation, pitch_rotation, attack_distance);
cout<<"yaw: "<<yaw_rotation<<'\t'<<"pitch: "<<pitch_rotation<<endl; cout<<"yaw: "<<yaw_rotation<<'\t'<<"pitch: "<<pitch_rotation<<endl;
cout<<"curr_yaw: "<<curr_yaw<<'\t'<<"curr_pitch: "<<curr_pitch<<endl; cout<<"curr_yaw: "<<mcuData.curr_yaw<<'\t'<<"curr_pitch: "<<mcuData.curr_pitch<<endl;
cout<<"mark_yaw: "<<mark_yaw<<'\t'<<"mark_pitch: "<<mark_pitch<<endl; cout<<"mark_yaw: "<<mcuData.mark_yaw<<'\t'<<"mark_pitch: "<<mcuData.mark_pitch<<endl;
// cout<<"send_cnt: "<<send_cnt<<endl; // cout<<"send_cnt: "<<send_cnt<<endl;
} }
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------

215
main.cpp
View File

@@ -4,7 +4,6 @@
#include <iostream> #include <iostream>
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include <fstream> #include <fstream>
#include <energy/energy.h> #include <energy/energy.h>
#include <serial/serial.h> #include <serial/serial.h>
#include <energy/param_struct_define.h> #include <energy/param_struct_define.h>
@@ -14,79 +13,32 @@
#include <camera/wrapper_head.h> #include <camera/wrapper_head.h>
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
#include <options/options.h> #include <options/options.h>
#include <additions/additions.h>
#include <thread> #include <thread>
#define DO_NOT_CNT_TIME #define DO_NOT_CNT_TIME
#include <log.h> #include <log.h>
#define ENERGY_STATE 1 #define ENERGY_STATE 'e'
#define ARMOR_STATE 0 #define ARMOR_STATE 'a'
using namespace cv; using namespace cv;
using namespace std; using namespace std;
mcu_data mcuData = {
int state = ENERGY_STATE; .curr_yaw = 0,
float curr_yaw = 0, curr_pitch = 0; .curr_pitch = 0,
float mark_yaw = 0, mark_pitch = 0; .state = ENERGY_STATE,
int mark = 0; .mark = 0,
EnemyColor enemy_color = ENEMY_BLUE; .use_classifier = 0,
int ally_color = ALLY_RED; .enemy_color = ENEMY_BLUE
bool use_classifier = true; };
void uartReceive(Serial *uart);
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
process_options(argc, argv); process_options(argc, argv);
Serial uart(115200); Serial serial(115200);
thread receive(uartReceive, &uart); thread receive(uartReceive, &serial);
bool flag = true;
while (flag) {
VideoWriter armor_video_writer, energy_video_writer;
if (state == ARMOR_STATE) {
string armor_filename_prefix = "../armor_video/";
ifstream in_1(armor_filename_prefix + "cnt.txt");
int cnt_1 = 0;
if (in_1.is_open())
{
in_1 >> cnt_1;
in_1.close();
}
string armor_file_name = armor_filename_prefix + std::to_string(cnt_1) + ".avi";
//cout << "recording video to " << armor_file_name << endl;
cnt_1++;
ofstream out_1(armor_filename_prefix + "cnt.txt");
if (out_1.is_open()) {
out_1 << cnt_1 << endl;
out_1.close();
}
armor_video_writer.open(armor_file_name, CV_FOURCC('P', 'I', 'M', '1'), 90, cv::Size(640, 480), true);
}
if (state == ENERGY_STATE) {
string energy_filename_prefix = "../energy_video/";
ifstream in_2(energy_filename_prefix + "cnt.txt");
int cnt_2 = 0;
if (in_2.is_open())
{
in_2 >> cnt_2;
in_2.close();
}
string energy_file_name = energy_filename_prefix + std::to_string(cnt_2) + ".avi";
//cout << "recording video to " << armor_file_name << endl;
cnt_2++;
ofstream out_2(energy_filename_prefix + "cnt.txt");
if (out_2.is_open()) {
out_2 << cnt_2 << endl;
out_2.close();
}
energy_video_writer.open(energy_file_name, CV_FOURCC('P', 'I', 'M', '1'), 90, cv::Size(640, 480), false);
}
int energy_part_rotation = CLOCKWISE;
int from_camera = 1; int from_camera = 1;
if (!run_with_camera) { if (!run_with_camera) {
@@ -94,43 +46,66 @@ int main(int argc, char *argv[]) {
cin >> from_camera; cin >> from_camera;
} }
while (true) {
VideoWriter armor_video_writer, energy_video_writer;
if (save_video) {
initVideoWriter(armor_video_writer, PROJECT_DIR"/armor_video/");
initVideoWriter(energy_video_writer, PROJECT_DIR"/energy_video/");
}
WrapperHead *video_armor; WrapperHead *video_armor;
WrapperHead *video_energy; WrapperHead *video_energy;
if (from_camera) { if (from_camera) {
video_armor = new CameraWrapper(0, "armor"); video_armor = new CameraWrapper(0, "armor");
video_energy = new CameraWrapper(1, "energy"); video_energy = new CameraWrapper(1, "energy");
} else { } else {
video_armor = new VideoWrapper("r_l_640.avi"); video_armor = new VideoWrapper("/home/xinyang/Desktop/DataSets/video/blue_4.mp4");
video_energy = new VideoWrapper("r_l_640.avi"); video_energy = new VideoWrapper("/home/xinyang/Desktop/DataSets/video/blue_4.mp4");
} }
if (video_armor->init() && video_energy->init()) { if (video_armor->init()) {
cout << "Video source initialization successfully." << endl; LOGM("video_armor source initialization successfully.");
} else { } else {
LOGW("video_armor source unavailable!");
delete video_armor; delete video_armor;
video_armor = nullptr;
}
if (video_energy->init()) {
LOGM("video_energy source initialization successfully.");
} else {
LOGW("video_energy source unavailable!");
delete video_energy; delete video_energy;
cout << "Program fails. Restarting" << endl; video_energy = nullptr;
continue;
} }
Mat energy_src, armor_src; Mat energy_src, armor_src;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
if (video_armor) {
video_armor->read(armor_src); video_armor->read(armor_src);
}
if (video_energy) {
video_energy->read(armor_src); video_energy->read(armor_src);
} }
}
ArmorFinder armorFinder(enemy_color, uart, PROJECT_DIR"/tools/para/", use_classifier); ArmorFinder armorFinder(mcuData.enemy_color, serial, PROJECT_DIR"/tools/para/", mcuData.use_classifier);
Energy energy(uart, ally_color); Energy energy(serial, mcuData.enemy_color);
// energy.setAllyColor(ally_color); // energy.setAllyColor(ally_color);
energy.setRotation(energy_part_rotation); energy.setRotation(CLOCKWISE);
bool ok = video_armor->read(armor_src) && video_energy->read(armor_src); bool ok = true;
do {
while (ok) {
CNT_TIME("Total", { CNT_TIME("Total", {
if (state == ENERGY_STATE) { if (mcuData.state == ENERGY_STATE) {
if (video_energy) {
ok = video_energy->read(energy_src); ok = video_energy->read(energy_src);
if (!ok) {
delete video_energy;
video_energy = nullptr;
}
if(save_video){
energy_video_writer.write(energy_src); energy_video_writer.write(energy_src);
}
if (show_origin) { if (show_origin) {
imshow("energy src", energy_src); imshow("energy src", energy_src);
} }
@@ -139,8 +114,23 @@ int main(int argc, char *argv[]) {
} }
energy.run(energy_src); energy.run(energy_src);
} else { } else {
video_energy = new CameraWrapper(1, "energy");
if(!video_energy->init()){
delete video_energy;
video_energy = nullptr;
}
}
} else if (mcuData.state == ARMOR_STATE) {
if (video_armor) {
ok = video_armor->read(armor_src); ok = video_armor->read(armor_src);
if (!ok) {
delete video_armor;
video_armor = nullptr;
}
if(save_video){
armor_video_writer.write(armor_src); armor_video_writer.write(armor_src);
}
flip(armor_src, armor_src, 0); flip(armor_src, armor_src, 0);
if (show_origin) { if (show_origin) {
imshow("armor src", armor_src); imshow("armor src", armor_src);
@@ -148,80 +138,21 @@ int main(int argc, char *argv[]) {
CNT_TIME("Armor Time", { CNT_TIME("Armor Time", {
armorFinder.run(armor_src); armorFinder.run(armor_src);
}); });
} else {
video_armor = new CameraWrapper(0, "armor");
if(!video_armor->init()){
delete video_armor;
video_armor = nullptr;
}
} }
if (waitKey(1) == 'q') {
flag = false;
break;
} }
}); });
} } while (ok);
delete video_armor; delete video_armor;
delete video_energy; delete video_energy;
cout << "Program fails. Restarting" << endl; cout << "Program fails. Restarting" << endl;
} }
return 0; return 0;
} }
#define RECEIVE_LOG_LEVEL LOG_NOTHING
char uartReadByte(Serial &uart) {
char byte;
if (uart.ReadData((uint8_t*)&byte, 1) == false) {
LOGE("serial error!");
}
return byte;
}
void uartReceive(Serial* uart) {
char buffer[100];
int cnt = 0;
LOGM("data receive start!");
while (true) {
char data;
while ((data = uartReadByte(*uart)) != '\n') {
buffer[cnt++] = data;
if (cnt >= 100) {
LOG(RECEIVE_LOG_LEVEL, "data receive over flow!");
cnt = 0;
}
}
if (cnt == 12) {
if (buffer[8] == 'e') {
state = ENERGY_STATE;
LOG(RECEIVE_LOG_LEVEL, "Energy state");
} else if (buffer[8] == 'a') {
state = ARMOR_STATE;
LOG(RECEIVE_LOG_LEVEL, "Armor state");
}
if (buffer[10] == 0){
use_classifier = false;
LOG(RECEIVE_LOG_LEVEL, "Classifier off!");
} else if(buffer[10] == 1){
use_classifier = true;
LOG(RECEIVE_LOG_LEVEL, "Classifier on!");
}
if (buffer[11] == ENEMY_BLUE) {
LOG(RECEIVE_LOG_LEVEL, "ENEMY_BLUE!");
ally_color = ALLY_RED;
enemy_color = ENEMY_BLUE;
} else if (buffer[11] == ENEMY_RED) {
LOG(RECEIVE_LOG_LEVEL, "ENEMY_RED!");
ally_color = ALLY_BLUE;
enemy_color = ENEMY_RED;
}
memcpy(&curr_yaw, buffer, 4);
memcpy(&curr_pitch, buffer + 4, 4);
LOG(RECEIVE_LOG_LEVEL, "Get yaw:%f pitch:%f", curr_yaw, curr_pitch);
if (buffer[9] == 1) {
if (mark == 0) {
mark = 1;
mark_yaw = curr_yaw;
mark_pitch = curr_pitch;
}
LOG(RECEIVE_LOG_LEVEL, "Marked");
}
}
cnt = 0;
}
}

View File

@@ -0,0 +1,32 @@
//
// Created by sjturm on 19-5-17.
//
#ifndef _ADDITIONS_H_
#define _ADDITIONS_H_
#include <stdint.h>
#include <serial/serial.h>
#include <opencv2/videoio.hpp>
#include <camera/wrapper_head.h>
#define ENEMY_BLUE 0
#define ENEMY_RED 1
struct mcu_data{
float curr_yaw;
float curr_pitch;
uint8_t state;
uint8_t mark;
uint8_t use_classifier;
uint8_t enemy_color;
float mark_yaw;
float mark_pitch;
};
extern mcu_data mcuData;
void uartReceive(Serial *pSerial);
void initVideoWriter(cv::VideoWriter& video, const std::string &armor_filename_prefix);
#endif /* _ADDITIONS_H_ */

View File

@@ -0,0 +1,50 @@
//
// Created by sjturm on 19-5-17.
//
#include <cstring>
#include <fstream>
#include <additions/additions.h>
#include <log.h>
#include <opencv2/videoio/videoio_c.h>
#define RECEIVE_LOG_LEVEL LOG_NOTHING
void uartReceive(Serial* pSerial) {
char buffer[100];
int cnt = 0;
LOGM("data receive start!");
while (true) {
char byte;
while (pSerial->ReadData((uint8_t*)&byte, 1) && byte!='\n') {
buffer[cnt++] = byte;
if (cnt >= 100) {
LOG(RECEIVE_LOG_LEVEL, "data receive over flow!");
cnt = 0;
}
}
if (cnt == sizeof(mcuData)) {
memcpy(&mcuData, buffer, sizeof(mcuData));
}
cnt = 0;
}
}
void initVideoWriter(cv::VideoWriter& video, const std::string &armor_filename_prefix){
std::ifstream in_1(armor_filename_prefix + "cnt.txt");
int cnt_1 = 0;
if (in_1.is_open())
{
in_1 >> cnt_1;
in_1.close();
}
std::string armor_file_name = armor_filename_prefix + std::to_string(cnt_1) + ".avi";
cnt_1++;
std::ofstream out_1(armor_filename_prefix + "cnt.txt");
if (out_1.is_open()) {
out_1 << cnt_1 << std::endl;
out_1.close();
}
video.open(armor_file_name, CV_FOURCC('P', 'I', 'M', '1'), 90, cv::Size(640, 480), true);
}