diff --git a/energy/include/energy/energy.h b/energy/include/energy/energy.h index 122f3e2..71a5455 100644 --- a/energy/include/energy/energy.h +++ b/energy/include/energy/energy.h @@ -60,6 +60,8 @@ private: double phi;//电机yaw轴应旋转的角度 float yaw_rotation; float pitch_rotation; + int position_mode; + int last_position_mode; int isLeftVertexFound, isTopVertexFound, isRightVertexFound, isBottomVertexFound; std::vector fans; diff --git a/energy/src/energy/calculate/cycle_calculate.cpp b/energy/src/energy/calculate/cycle_calculate.cpp index b8d44d2..7593555 100644 --- a/energy/src/energy/calculate/cycle_calculate.cpp +++ b/energy/src/energy/calculate/cycle_calculate.cpp @@ -58,7 +58,7 @@ void Energy::cycleLeastFit() radius = 0.0f; if (all_armor_centers.size() < 3) { - cout<<"Cannot calculate a circle"< &armors, int &last_ std::vector > armor_contours_external;//用总轮廓减去外轮廓,只保留内轮廓,除去流动条的影响。 StructingElementErodeDilate(src_bin); +// StructingElementClose(src_bin,10,10); imshow("armor struct",src_bin); findContours(src_bin, armor_contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE); diff --git a/energy/src/energy/get/gimble_rotation_get.cpp b/energy/src/energy/get/gimble_rotation_get.cpp index 6042706..977648c 100644 --- a/energy/src/energy/get/gimble_rotation_get.cpp +++ b/energy/src/energy/get/gimble_rotation_get.cpp @@ -24,7 +24,28 @@ void Energy::gimbleRotation(){ // pitch_rotation = static_cast(180 / PI * atan2((ATTACK_DISTANCE*tan(PITCH_ORIGIN_RAD)-real_hit_point.y), ATTACK_DISTANCE)); yaw_rotation = static_cast(180 / PI * atan2((attack_distance*tan(mark_yaw*PI/180)-real_hit_point.x), attack_distance)); - pitch_rotation = 5.5+static_cast(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance)); + if(position_mode == 1){ + pitch_rotation = 5.5+static_cast(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance)); + } + if(position_mode == 2){ + pitch_rotation = 5.5+static_cast(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance)); + } + if(position_mode == 3){ + pitch_rotation = 4.5+static_cast(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance)); + } + if(position_mode == 4){ + pitch_rotation = 4.5+static_cast(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance)); + } + if(position_mode == 5){ + pitch_rotation = 5.5+static_cast(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance)); + } + if(position_mode == 6){ + pitch_rotation = 5.5+static_cast(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance)); + } +// else{ +// pitch_rotation = 5.5+static_cast(180 / PI * atan2((attack_distance*tan(mark_pitch*PI/180)-real_hit_point.y), attack_distance)); +// } + // yaw_rotation = static_cast(180 / PI * atan2((ATTACK_DISTANCE*tan(3.5*PI/180)-STRETCH*(hit_point.x-cycle_center.x)), ATTACK_DISTANCE)); // pitch_rotation = static_cast(180 / PI * atan2((ATTACK_DISTANCE*tan(9.1*PI/180)-STRETCH*(hit_point.y-cycle_center.y)), ATTACK_DISTANCE)); diff --git a/energy/src/energy/get/hit_point_get.cpp b/energy/src/energy/get/hit_point_get.cpp index c5221e2..552c5a1 100644 --- a/energy/src/energy/get/hit_point_get.cpp +++ b/energy/src/energy/get/hit_point_get.cpp @@ -16,38 +16,47 @@ void Energy::getHitPoint(){ int angle_interval = 60; if(energy_part_rotation==1){ //顺时针 - if(target_armor>=15 && target_armor<=15+limit_angle){ + if(target_armor>=0 && target_armor<=limit_angle){ hit_point.x = cycle_center.x + static_cast(radius / 2); hit_point.y = cycle_center.y + static_cast(radius * sqrt(3) / 2); hit_position = -1*angle_interval; + position_mode = 1; +// cout<<"666"<<'\t'<=angle_interval && target_armor(radius); + hit_point.y = cycle_center.y; + hit_position = 0; + position_mode = 2; + } + else if(target_armor>=angle_interval*2 && target_armor<=angle_interval*2+limit_angle){ + hit_point.x = cycle_center.x + static_cast(radius / 2); + hit_point.y = cycle_center.y - static_cast(radius * sqrt(3) / 2); + hit_position = angle_interval; + position_mode = 3; + } + else if(target_armor>=-180 && target_armor<-180+limit_angle){ + hit_point.x = cycle_center.x - static_cast(radius / 2); + hit_point.y = cycle_center.y - static_cast(radius * sqrt(3) / 2); + hit_position = 2*angle_interval; + position_mode = 4; + } + else if(target_armor>=-2*angle_interval&&target_armor<-2*angle_interval+limit_angle){ + hit_point.x = cycle_center.x - static_cast(radius); + hit_point.y = cycle_center.y; + hit_position = 180; + position_mode = 5; + } + else if(target_armor>=-1*angle_interval&&target_armor<-1*angle_interval+limit_angle) { + hit_point.x = cycle_center.x - static_cast(radius / 2); + hit_point.y = cycle_center.y + static_cast(radius * sqrt(3) / 2); + hit_position = -2 * angle_interval; + position_mode = 6; } -// if(target_armor>=angle_interval && target_armor(radius); -// hit_point.y = cycle_center.y; -// hit_position = 0; -// } -// if(target_armor>=angle_interval*2 && target_armor<=angle_interval*2+limit_angle){ -// hit_point.x = cycle_center.x + static_cast(radius / 2); -// hit_point.y = cycle_center.y - static_cast(radius * sqrt(3) / 2); -// hit_position = angle_interval; -// } -// if(target_armor>=-180 && target_armor<-180+limit_angle){ -// hit_point.x = cycle_center.x - static_cast(radius / 2); -// hit_point.y = cycle_center.y - static_cast(radius * sqrt(3) / 2); -// hit_position = 2*angle_interval; -// } -// if(target_armor>=-2*angle_interval&&target_armor<-2*angle_interval+limit_angle){ -// hit_point.x = cycle_center.x - static_cast(radius); -// hit_point.y = cycle_center.y; -// hit_position = 180; -// } -// if(target_armor>=-1*angle_interval&&target_armor<-1*angle_interval+limit_angle) { -// hit_point.x = cycle_center.x - static_cast(radius / 2); -// hit_point.y = cycle_center.y + static_cast(radius * sqrt(3) / 2); -// hit_position = -2 * angle_interval; -// } else{ - last_hit_position = 2000; +// last_hit_position = 2000; + position_mode = 0; return; } } @@ -57,54 +66,84 @@ void Energy::getHitPoint(){ hit_point.x = cycle_center.x - static_cast(radius / 2); hit_point.y = cycle_center.y - static_cast(radius * sqrt(3) / 2); hit_position = 2*angle_interval; + position_mode = 1; } - if(target_armor>=angle_interval && target_armor=angle_interval && target_armor(radius); hit_point.y = cycle_center.y; hit_position = 180; + position_mode = 2; } - if(target_armor>=angle_interval*2 && target_armor<=angle_interval*2+limit_angle){ + else if(target_armor>=angle_interval*2 && target_armor<=angle_interval*2+limit_angle){ hit_point.x = cycle_center.x - static_cast(radius / 2); hit_point.y = cycle_center.y + static_cast(radius * sqrt(3) / 2); hit_position = -2*angle_interval; + position_mode = 3; } - if(target_armor>=-180 && target_armor<-180+limit_angle){ + else if(target_armor>=-180 && target_armor<-180+limit_angle){ hit_point.x = cycle_center.x + static_cast(radius / 2); hit_point.y = cycle_center.y + static_cast(radius * sqrt(3) / 2); hit_position = -1*angle_interval; + position_mode = 4; } - if(target_armor>=-2*angle_interval&&target_armor<-2*angle_interval+limit_angle){ + else if(target_armor>=-2*angle_interval&&target_armor<-2*angle_interval+limit_angle){ hit_point.x = cycle_center.x + static_cast(radius); hit_point.y = cycle_center.y; hit_position = 0; + position_mode = 5; } - if(target_armor>=-1*angle_interval&&target_armor<-1*angle_interval+limit_angle){ + else if(target_armor>=-1*angle_interval&&target_armor<-1*angle_interval+limit_angle){ hit_point.x = cycle_center.x + static_cast(radius / 2); hit_point.y = cycle_center.y - static_cast(radius * sqrt(3) / 2); hit_position = angle_interval; + position_mode = 6; } else{ - last_hit_position = 2000; +// last_hit_position = 2000; + position_mode = 0; return; } } - if(hit_position>360&&last_hit_position>360){ - isSendTarget = false; - return; - } - if(fabs(hit_position - last_hit_position) < 30||fabs(hit_position - last_hit_position) > 330 && fabs(hit_position - last_hit_position) <= 360){ - last_hit_position = hit_position; - isSendTarget = false; - return; - } - else{ - last_hit_position = hit_position; +// cout<<"last: "<360&&last_hit_position>360){ +// isSendTarget = false; +// return; +// } + +// if(fabs(hit_position - last_hit_position) < 15||fabs(hit_position - last_hit_position) > 345 && fabs(hit_position - last_hit_position) <= 360){ +// last_hit_position = hit_position; +// isSendTarget = false; +// return; +// } +// else{ +// last_hit_position = hit_position; +// isSendTarget = true; +// cout<<"hit position: "<(energy_part_rotation * energy_part_param_.RPM // * energy_part_param_.HIT_TIME * 360 / 60); diff --git a/energy/src/energy/run.cpp b/energy/src/energy/run.cpp index 783b484..9bae15a 100644 --- a/energy/src/energy/run.cpp +++ b/energy/src/energy/run.cpp @@ -12,7 +12,7 @@ extern float curr_yaw, curr_pitch, mark_yaw, mark_pitch; extern int mark; int Energy::run(cv::Mat &src){ -// imshow("src",src); + imshow("src",src); fans.clear(); armors.clear(); fanPosition.clear(); @@ -52,14 +52,14 @@ int Energy::run(cv::Mat &src){ // cout<<"all_armor_centers.size(): "<> from_camera; } - WrapperHead *video_armor; + WrapperHead *video_armor; WrapperHead *video_energy; - if (from_camera) { - video_armor = new CameraWrapper(0); + if(from_camera) { +// video_armor = new CameraWrapper(0); video_energy = new CameraWrapper(1); - } else { - video_armor = new VideoWrapper("/home/xinyang/Desktop/Video.mp4"); - video_energy = new VideoWrapper("/home/xinyang/Desktop/Video.mp4"); - } - if (video_armor->init()) { - cout << "Video source initialization successfully." << endl; + }else { + video_armor = new VideoWrapper("r_l_640.avi"); + video_energy = new VideoWrapper("r_l_640.avi"); } + if (video_energy->init()) { + cout << "Video source initialization successfully." << endl; + } - Mat energy_src, armor_src; - ArmorFinder armorFinder(ENEMY_BLUE, uart, PROJECT_DIR"/tools/para/"); + Mat energy_src, armor_src; + + ArmorFinder armorFinder(ENEMY_BLUE, uart, PROJECT_DIR"/tools/para/"); Energy energy(uart); energy.setAllyColor(ally_color); energy.setRotation(energy_part_rotation); - bool ok = true; + bool ok=true; - while (ok) { - CNT_TIME(WORD_LIGHT_CYAN, "Total", { + while (ok){ +// CNT_TIME(WORD_LIGHT_CYAN, "Total", { +// ok = video_energy->read(energy_src) && video_energy->read(armor_src); + ok = video_energy->read(energy_src); + if (show_origin) { + imshow("enery src", energy_src); + imshow("armor src", armor_src); + } if (state == ENERGY_STATE) { - ok = video_energy->read(energy_src); - if (show_origin) { - imshow("energy src", energy_src); - } if (from_camera == 0) { energy.extract(energy_src); } energy.run(energy_src); } else { - ok = video_armor->read(armor_src); - if (show_origin) { - imshow("armor src", armor_src); - } - CNT_TIME(WORD_LIGHT_BLUE, "Armor Time", { - armorFinder.run(armor_src); - }); +// CNT_TIME(WORD_LIGHT_BLUE, "Armor Time", { +// armorFinder.run(armor_src); +// }); } if (waitKey(1) == 'q') { flag = false; break; } - }); - } - delete video_armor; - cout << "Program fails. Restarting" << endl; - } +// }); + } + delete video_energy; + cout << "Program fails. Restarting" << endl; + } - return 0; + return 0; } -#define RECEIVE_LOG_LEVEL LOG_NOTHING +#define RECEIVE_LOG_LEVEL LOG_MSG -void uartReceive(Uart *uart) { +void uartReceive(Uart* uart){ char buffer[100]; - int cnt = 0; - while (true) { + int cnt=0; + while(true){ char data; - while ((data = uart->receive()) != '\n') { + while((data=uart->receive()) != '\n'){ buffer[cnt++] = data; - if (cnt >= 100) { + if(cnt >= 100){ LOG(RECEIVE_LOG_LEVEL, "data receive over flow!"); - cnt = 0; + cnt = 0; } } - if (cnt == 10) { - if (buffer[8] == 'e') { + if(cnt == 10){ + if(buffer[8] == 'e'){ state = ENERGY_STATE; - LOG(RECEIVE_LOG_LEVEL, "Energy state"); - } else if (buffer[8] == 'a') { +// LOG(RECEIVE_LOG_LEVEL, "Energy state"); + }else if(buffer[8] == 'a'){ state = ARMOR_STATE; - LOG(RECEIVE_LOG_LEVEL, "Armor state"); +// LOG(RECEIVE_LOG_LEVEL, "Armor state"); } 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) { + 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"); +// LOG(RECEIVE_LOG_LEVEL, "Marked"); } } cnt = 0;