Files
QDAC-firmware/app/gl_tracking.c

263 lines
8.7 KiB
C
Raw Normal View History

#include "zf_common_headfile.h"
#include "gl_headfile.h"
2024-03-30 11:43:43 +08:00
#include "jj_blueteeth.h"
float (*mid_track)[2];
int32_t mid_track_count;
2024-03-10 08:35:37 +08:00
float pure_angle;
2024-05-27 21:17:40 +08:00
float pure_angle_half;
2024-03-08 19:58:36 +08:00
float dx_near;
2024-06-16 21:52:42 +08:00
float curvature;
float (*rpts)[2];
int rpts_num;
2024-05-24 20:42:56 +08:00
float last_pure_angle = 0.0f;
2024-06-16 21:52:42 +08:00
int8_t turn_flag = 0;
2024-05-24 20:42:56 +08:00
// 计算最小二乘法斜率的函数
float leastSquaresSlope(float points[][2], int n)
{
float sum_x = 0;
float sum_y = 0;
float sum_xy = 0;
float sum_x_squared = 0;
// 计算各项和
for (int i = 0; i < n; i++) {
sum_x += points[i][1];
sum_y += points[i][0];
sum_xy += points[i][1] * points[i][0];
sum_x_squared += points[i][1] * points[i][1];
}
// 计算斜率
float numerator = (float)n * sum_xy - sum_x * sum_y;
float denominator = (float)n * sum_x_squared - sum_x * sum_x;
if (denominator == 0) {
// 避免除以零错误
printf("Error: denominator is zero.\n");
return 0;
}
float temp = denominator / numerator;
return temp;
}
2024-06-16 21:52:42 +08:00
float calculateX(float a_x, float a_y, float slope, float b_y)
{
2024-05-24 20:42:56 +08:00
float b_x = a_x - (b_y - a_y) * slope;
return b_x;
}
2024-03-08 20:58:16 +08:00
void tracking()
{
if (pts_resample_left_count < pts_resample_right_count / 2 && pts_resample_left_count < 50) {
2024-03-08 20:58:16 +08:00
track_type = TRACK_RIGHT;
} else if (pts_resample_right_count < pts_resample_left_count / 2 && pts_resample_right_count < 58) {
track_type = TRACK_LEFT;
} else if (pts_resample_left_count < 20 && pts_resample_right_count > pts_resample_left_count) {
track_type = TRACK_RIGHT;
} else if (pts_resample_right_count < 20 && pts_resample_left_count > pts_resample_right_count) {
track_type = TRACK_LEFT;
}
}
2024-03-22 20:28:49 +08:00
void aim_distance_select(void)
{
if (cross_type != CROSS_NONE) {
aim_distance = cross_aim;
2024-06-16 21:52:42 +08:00
} else if (circle_type != CIRCLE_NONE) {
aim_distance = cricle_aim;
2024-03-23 17:37:31 +08:00
} else if (barrier_type != BARRIER_NONE) {
2024-03-23 18:26:03 +08:00
aim_distance = barrier_aim;
2024-03-22 20:28:49 +08:00
}
}
2024-03-08 20:58:16 +08:00
void ElementJudge()
{
CheckGarage();
if (garage_type == GARAGE_NONE) {
CheckCross();
if (cross_type == CROSS_NONE) {
2024-03-23 09:43:06 +08:00
CheckBarrier();
2024-03-23 17:37:31 +08:00
if (barrier_type == BARRIER_NONE) {
2024-03-23 09:43:06 +08:00
CheckCircle();
2024-06-16 21:52:42 +08:00
if (circle_type == CIRCLE_NONE) {
Check_s();
}
2024-03-23 09:43:06 +08:00
}
2024-03-08 20:58:16 +08:00
}
}
2024-03-08 20:58:16 +08:00
if (garage_type != GARAGE_NONE) {
cross_type = CROSS_NONE;
circle_type = CIRCLE_NONE;
}
2024-03-23 17:37:31 +08:00
if (cross_type != CROSS_NONE) {
circle_type = CIRCLE_NONE;
2024-03-23 09:43:06 +08:00
barrier_type = BARRIER_NONE;
2024-03-23 09:15:53 +08:00
}
2024-03-23 17:37:31 +08:00
if (barrier_type != BARRIER_NONE) {
2024-03-23 09:43:06 +08:00
circle_type = CIRCLE_NONE;
}
}
2024-03-08 20:58:16 +08:00
void ElementRun()
{
if (garage_type != GARAGE_NONE) {
RunGarage();
}
2024-03-08 20:58:16 +08:00
else if (cross_type != CROSS_NONE) {
RunCross();
}
2024-03-08 20:58:16 +08:00
else if (circle_type != CIRCLE_NONE) {
RunCircle();
2024-03-23 17:37:31 +08:00
} else if (barrier_type != BARRIER_NONE) {
RunBarrier();
2024-06-16 21:52:42 +08:00
} else if (s_type != S_NONE) {
RunS();
2024-03-08 20:58:16 +08:00
}
}
2024-03-08 20:58:16 +08:00
void MidLineTrack()
{
if (cross_type == CROSS_IN) {
if (track_type == TRACK_LEFT) {
mid_track = mid_left; // 这是为了预先分配内存
GetMidLine_Left(pts_far_resample_left + far_Lpt0_rpts0s_id, pts_far_resample_left_count - far_Lpt0_rpts0s_id, mid_left, (int)round(ANGLEDIST / RESAMPLEDIST), PIXPERMETER * ROADWIDTH / 2);
mid_track_count = pts_far_resample_left_count - far_Lpt0_rpts0s_id;
} else {
mid_track = mid_right; // 这是为了预先分配内存
GetMidLine_Right(pts_far_resample_right + far_Lpt1_rpts1s_id, pts_far_resample_right_count - far_Lpt1_rpts1s_id, mid_right, (int)round(ANGLEDIST / RESAMPLEDIST), PIXPERMETER * ROADWIDTH / 2);
mid_track_count = pts_far_resample_right_count - far_Lpt1_rpts1s_id;
}
} else {
2024-03-08 20:58:16 +08:00
if (track_type == TRACK_LEFT) {
mid_track = mid_left;
mid_track_count = mid_left_count;
} else {
mid_track = mid_right;
mid_track_count = mid_right_count;
}
}
2024-03-08 20:58:16 +08:00
// 车轮对应点 (纯跟踪起始点)
2024-05-24 20:42:56 +08:00
float cx = InverseMapW[(int)(IMAGE_H * 0.8f)][70];
float cy = InverseMapH[(int)(IMAGE_H * 0.8f)][70];
int neary = mid_track[0][0];
int nearx = mid_track[0][1];
2024-03-08 20:58:16 +08:00
// 找最近点 (起始点中线归一化)
float min_dist = 1e10;
int begin_id = -1;
for (int i = 0; i < mid_track_count; i++) {
float dx = mid_track[i][1] - cx;
float dy = mid_track[i][0] - cy;
float dist = Q_sqrt(dx * dx + dy * dy);
2024-03-08 20:58:16 +08:00
if (dist < min_dist) {
min_dist = dist;
begin_id = i;
}
}
2024-03-08 20:58:16 +08:00
if (begin_id >= 0 && mid_track_count - begin_id >= 3) {
// 归一化中线
mid_track[begin_id][0] = cy;
mid_track[begin_id][1] = cx;
rptsn_num = sizeof(rptsn) / sizeof(rptsn[0]);
GetLinesResample(mid_track + begin_id, mid_track_count - begin_id, rptsn, &rptsn_num, RESAMPLEDIST * PIXPERMETER);
// 远预锚点位置-
2024-05-24 20:42:56 +08:00
int aim_idx = clip(round(aim_distance / RESAMPLEDIST), 0, rptsn_num - 1);
int aim_idx_judge = clip(round(aim_judge_far / RESAMPLEDIST), 0, mid_track_count - 1);
2024-03-08 20:58:16 +08:00
// 近锚点位置
2024-05-27 21:17:40 +08:00
int aim_idx_near = clip(round(aim_distance / 2 / RESAMPLEDIST), 0, rptsn_num - 1);
2024-03-08 20:58:16 +08:00
2024-05-24 20:42:56 +08:00
float dx1 = mid_track[3 * (mid_track_count / 4)][1] - mid_track[aim_idx_judge][1];
float dy1 = mid_track[3 * (mid_track_count / 4)][0] - mid_track[aim_idx_judge][0];
2024-06-16 21:52:42 +08:00
float dn1 = Q_sqrt(dx1 * dx1 + dy1 * dy1);
2024-05-24 20:42:56 +08:00
float dx2 = mid_track[aim_idx_judge][1] - nearx;
float dy2 = mid_track[aim_idx_judge][0] - neary;
2024-03-30 11:43:43 +08:00
float dn2 = Q_sqrt(dx2 * dx2 + dy2 * dy2);
float c1 = dx1 / dn1;
float s1 = dy1 / dn1;
float c2 = dx2 / dn2;
float s2 = dy2 / dn2;
float angle_1 = atan2f(c1 * s2 - c2 * s1, c2 * c1 + s2 * s1);
2024-06-16 21:52:42 +08:00
2024-05-24 20:42:56 +08:00
if (angle_1 >= 0.2f || angle_1 <= -0.2f) {
2024-03-29 10:50:37 +08:00
state_type = TURN_STATE;
2024-03-30 11:43:43 +08:00
} else {
2024-03-29 10:50:37 +08:00
state_type = STRAIGHT_STATE;
}
2024-03-08 20:58:16 +08:00
// 计算远锚点偏差值
2024-06-16 21:52:42 +08:00
float dx = rptsn[aim_idx][1] - cx;
float dy = cy - rptsn[aim_idx][0]; // + 0.2f * PIXPERMETER;
float dn = (dx * dx + dy * dy);
2024-05-24 20:42:56 +08:00
float temp_near = 0;
if (barrier_type == BARRIER_LEFT_BEGIN || barrier_type == BARRIER_LEFT_RUNNING) {
dx_near = mid_track[aim_idx_near][1] - cx + barrier_offset;
2024-05-25 21:24:21 +08:00
pure_angle = -atanf(PIXPERMETER * 2.0f * 0.2f * dx / dn) / PI32 * 180.0f - 20;
2024-06-16 21:52:42 +08:00
} else if (barrier_type == BARRIER_RIGHT_BEGIN || barrier_type == BARRIER_RIGHT_RUNNING) {
2024-05-24 20:42:56 +08:00
dx_near = mid_track[aim_idx_near][1] - cx - barrier_offset;
2024-05-25 21:24:21 +08:00
pure_angle = -atanf(PIXPERMETER * 2.0f * 0.2f * dx / dn) / PI32 * 180.0f + 20;
2024-03-23 17:37:31 +08:00
} else {
2024-06-16 21:52:42 +08:00
// pure_angle = -atanf(PIXPERMETER * 2.0f * 0.2f * 0.5f * dx / dn) / PI32 * 180.0f;
2024-05-24 20:42:56 +08:00
if (dy > 0) {
2024-06-16 21:52:42 +08:00
pure_angle = -atanf(dx / dy) / PI32 * 180.0f;
2024-05-24 20:42:56 +08:00
last_pure_angle = pure_angle;
2024-06-16 21:55:43 +08:00
//last_pure_angle_half = pure_angle_half;
2024-05-24 20:42:56 +08:00
} else {
pure_angle = last_pure_angle;
2024-06-16 21:55:43 +08:00
//pure_angle_half = last_pure_angle_half;
2024-05-24 20:42:56 +08:00
}
2024-03-23 17:37:31 +08:00
}
2024-06-16 21:52:42 +08:00
2024-03-23 17:37:31 +08:00
// // 计算近锚点偏差值
// dx_near = rptsn[aim_idx_near][1] - cx;
// // float dy_near = cy - rptsn[aim_idx_near][0] + 0.2 * PIXPERMETER;
// // float dn_near = Q_sqrt(dx_near * dx_near + dy_near * dy_near);
// // float error_near = -atan2f(dx_near, dy_near) * 180 / PI32;
2024-03-08 20:58:16 +08:00
}
2024-06-16 21:52:42 +08:00
if (circle_type == CIRCLE_LEFT_IN || circle_type == CIRCLE_LEFT_OUT || circle_type == CIRCLE_RIGHT_IN || circle_type == CIRCLE_RIGHT_OUT || circle_type == CIRCLE_LEFT_RUNNING || circle_type == CIRCLE_RIGHT_RUNNING || circle_type == CIRCLE_LEFT_BEGIN || circle_type == CIRCLE_RIGHT_BEGIN) {
2024-05-24 20:42:56 +08:00
state_type = CIRCLE_STATE;
2024-03-30 11:43:43 +08:00
}
2024-05-24 20:42:56 +08:00
if (cross_type == CROSS_BEGIN || cross_type == CROSS_IN) {
2024-04-03 16:20:56 +08:00
state_type = STRAIGHT_STATE;
}
2024-06-16 21:52:42 +08:00
last_state = state_type;
if (state_type == STRAIGHT_STATE) {
aim_distance = straight_aim;
} else if (state_type == TURN_STATE) {
aim_distance = mid_aim;
}
// }else{
// aim_distance = common_aim;
// }
if (last_state == STRAIGHT_STATE && (state_type == TURN_STATE || state_type == CIRCLE_STATE)) {
turn_flag = 1;
timer_clear(TIM_3);
timer_start(TIM_3);
}
if (turn_flag == 1) {
aim_distance = turn_aim;
uint16 ti = timer_get(TIM_3);
if (ti >= 2000) {
turn_flag = 0;
timer_stop(TIM_3);
timer_clear(TIM_3);
}
}
2024-03-08 20:58:16 +08:00
}