inttarget() { int front = MF_GetDigitInput(infrared_f_m); int behind = MF_GetDigitInput(infrared_b_m); int left = MF_GetDigitInput(infrared_m_l); int right = MF_GetDigitInput(infrared_m_r);
int front_left = MF_GetDigitInput(infrared_f_l); int front_right = MF_GetDigitInput(infrared_f_r); int behind_left = MF_GetDigitInput(infrared_b_l); int behind_right = MF_GetDigitInput(infrared_b_r);
if (front == 0 && behind == 0) return5; // 前后都有目标 if (left == 0 && right == 0) return6; // 左右都有目标 if (front == 0 && (left == 0 || right == 0)) return7; // 前方+左方/右方 有目标 if (behind == 0 && (left == 0 || right == 0)) return8; // 后方+左方/右方 有目标 if (front == 1 && right == 1 && behind == 1 && left == 1) return0; // 没有目标 if (front == 0 && right == 1 && behind == 1 && left == 1) return1; // 前方有目标 if (front == 1 && right == 0 && behind == 1 && left == 1) return2; // 右侧有目标 if (front == 1 && right == 1 && behind == 0 && left == 1) return3; // 后方有目标 if (front == 1 && right == 1 && behind == 1 && left == 0) return4; // 左方有目标 return0; }
// Average sliding filtering algorithm // to get the true AD value // filter_buf is a global variable voidfilter_ad(signedlongint *avr, signedlongint *sd) { int i; int filter_sum = 0; filter_buf[FILTER_N] = MF_GetAD(1); for (i = 0; i < FILTER_N; i++) { filter_buf[i] = filter_buf[i + 1]; filter_sum += filter_buf[i]; } signedlongint average = filter_sum / FILTER_N; signedlongint sum = 0; for ( i = 0; i < FILTER_N; i++) { sum += (filter_buf[i] - average) * (filter_buf[i] - average); } signedlongint deviation = sqrt(sum / FILTER_N);