yy1717
2020-01-15 eced3d013f06b623a49cb9deaba42218c4e37bb8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// Created by YY on 2019/11/4.
//
 
#include "turn_a90.h"
#include "../Geometry.h"
#include "../driver_test.h"
#include "../common/apptimer.h"
#include "../jni_log.h"
 
#include <vector>
#include <cstdlib>
 
#define DEBUG(fmt, args...)     LOGD("<turn_a90> <%s>: " fmt, __func__, ##args)
 
using namespace std;
 
enum {
    TURN_ANGLE_90,
    TURN_ANGLE_90_CMP
};
 
const uint32_t STOP_CAR_TIME = D_SEC(2);
 
static bool TA90Testing;
static bool carStopEvent;
static bool checked;
static int currTarget;
static int azimuth;
static uint32_t stopTimepoint = 0;
 
static bool CrashRedLine(const Polygon *map, const car_model_cache_t *car);
static bool ExitTestArea(const Polygon *map, const car_model_cache_t *car);
static bool Turned(const Polygon *map, const car_model_cache_t *car);
 
void StartTurnA90(double heading)
{
    azimuth = (int) heading;
    checked = false;
    TA90Testing = true;
    carStopEvent = false;
 
    currTarget = TURN_ANGLE_90;
}
 
void StopTurnA90(void)
{
    TA90Testing = false;
}
 
int TestTurnA90(vector<int>&err, const Polygon *map, const car_model_cache_t *car, double speed, int run_status, double heading)
{
    int status = 0;
 
    if (!TA90Testing)
        return -2;
 
    DEBUG("TestTurnA90 %d", run_status);
 
    if (CrashRedLine(map, car)) {
        // 压线了
        err.push_back(29);
        status = -1;
        DEBUG("错误 压线");
    }
 
    if (run_status != 0) {
        if (carStopEvent)
            DEBUG("TURN_ANGLE_90 停车时间 %ld", AppTimer_GetTickCount() - stopTimepoint);
 
        if (carStopEvent && AppTimer_GetTickCount() - stopTimepoint > STOP_CAR_TIME) {
            // 中途停车
            err.push_back(31);
            DEBUG("错误 停车1");
        }
        carStopEvent = false;
    } else if (!carStopEvent){
        carStopEvent = true;
        stopTimepoint = AppTimer_GetTickCount();
    }
 
    if (ExitTestArea(map, car)) {
        // 测试结束
        status = 1;
    }
 
    if (currTarget == TURN_ANGLE_90) {
        // 转向灯开启
        int az = (int) heading;
 
        if (abs(az - azimuth) > 180) {
            az = 360 - abs(az-azimuth);
        } else {
            az = abs(az - azimuth);
        }
 
        if (az >= 10 && !checked) {
            checked = true;
            // 转向灯未开启
            err.push_back(30);
            DEBUG("错误 灯没看");
        }
 
        if (Turned(map, car)) {
            currTarget = TURN_ANGLE_90_CMP;
            checked = false;
        }
        DEBUG("TURN_ANGLE_90 %d %d",run_status, az);
    } else {
        // 关闭转向灯
        Line line;
 
        MakeLine(&line, &map->point[1], &map->point[2]);
 
        // 大于2.5米后检查车灯
        if (!checked && DistanceOf(car->points[0], line) >= 2.5) {
            checked = true;
            // 转向灯未关闭
            err.push_back(30);
            DEBUG("错误 灯没管");
        }
        DEBUG("TURN_ANGLE_90_CMP");
    }
 
    if (status != 0) {
        StopTurnA90();
    }
 
    return status;
}
 
// 车轮是否压边线
static bool CrashRedLine(const Polygon *map, const car_model_cache_t *car)
{
    bool ret = false;
 
    Line red_line;
    const int red_lines[][2] = {{0, 5}, {5, 4}, {1, 2}, {2, 3}};
 
    Line frontAxle, rearAxle;
 
    // 仅看车轮外侧
    MakeLine(&frontAxle, &car->points[car->desc->front_left_tire[TIRE_OUTSIDE]], &car->points[car->desc->front_right_tire[TIRE_OUTSIDE]]);
    MakeLine(&rearAxle, &car->points[car->desc->rear_left_tire[TIRE_OUTSIDE]], &car->points[car->desc->rear_right_tire[TIRE_OUTSIDE]]);
 
    for (int i = 0; i < sizeof(red_lines) / sizeof(red_lines[0]); ++i) {
        MakeLine(&red_line, &map->point[red_lines[i][0]], &map->point[red_lines[i][1]]);
        if (IntersectionOf(red_line, frontAxle) == GM_Intersection ||
            IntersectionOf(red_line, rearAxle) == GM_Intersection) {
            ret = true;
            break;
        }
    }
 
    return ret;
}
 
// 整个车辆都要驶离该测试区域
static bool ExitTestArea(const Polygon *map, const car_model_cache_t *car)
{
    for (int i = 0; i < car->point_num; ++i) {
        if (IntersectionOfLine(map->point[3], map->point[4], car->points[i]) != 1)
            return false;
    }
    return true;
}
 
static bool Turned(const Polygon *map, const car_model_cache_t *car)
{
    for (int i = 0; i < car->point_num; ++i) {
        if (IntersectionOfLine(map->point[1], map->point[2], car->points[i]) != 1)
            return false;
    }
    return true;
}