-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene_base.cpp
More file actions
171 lines (138 loc) · 4.98 KB
/
Copy pathscene_base.cpp
File metadata and controls
171 lines (138 loc) · 4.98 KB
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
#include "scene_base.h"
//display, virtual function
void SceneBase::showScene() {
BeginBatchDraw();
cleardevice();
road0->showRoad();
car0->showCar(BLACK);
if (SHOWCIRCLE && car0->p_center)//
{
car0->showCircle();
}
EndBatchDraw();
delay(DELAYTIME);
}
//Move straight:, moving in certain distance,// total_s positive, is total distance it travel
void SceneBase::uniformStraight(const double& total_s) {
car0->updateStraightInfo();//update the straight motion info
double s = 0.0; //how many distance do we need to travel
while (s < total_s)
{
s += fabs(car0->speed);
car0->moveStraightStep();
obsMoveStep();
showScene();//display
}
car0->coutInfo();
}
//Move straight:, acceralation, acceralate to certain speed, uniform acceleration (deceleration) linear motion.
void SceneBase::uniformAccBySpeed(const double& target_speed_y) {
while (car0->pmidr->y > 0.0)
{
car0->moveStraightStep();
obsMoveStep();
if (fabs(car0->speed_y - target_speed_y) > fabs(car0->a_y))//if current speed and expect speed are not equal
{
car0->speed_y += car0->a_y; //accelerating
}
else //if current speed is close enough to expect speed
{
car0->speed_y = target_speed_y; //we directly let the speed equal expect speed
car0->a_y = 0.0; //update the accelerating to 0
if (target_speed_y == 0.0)//if parking, break the loop,otherwise p0 can move to 0.0
{
break;
}
}
showScene();//display
}
car0->coutInfo();
}
/*Move straight : When traveling a specified distance,
the speed reaches the specified speed,uniform acceleration (deceleration) linear motion.)*/
void SceneBase::uniformAccByDis(const double& dis, const double& target_speed_y) {
//calculate the acceleration
car0->a_y = (pow(car0->speed_y, 2) - pow(target_speed_y, 2)) / dis / 2.0; //acceleration
cout << "a_y = " << car0->a_y << ", dis = " << dis << endl;//print
uniformAccBySpeed(target_speed_y);
}
//move straight:in specified time, reach to the specified speed,uniform acceleration (deceleration) linear motion.)
void SceneBase::uniformAccByTime(const double& target_speed_y, const double& target_time) {
//calculate the acceleration
double freq = target_time * 1000 / DELAYTIME;//how many loop do we need, count in frame, using the frame instead of real time, second.
car0->a_y = (target_speed_y - car0->speed_y) / freq;//acceleration //speed change during each loop
cout << "a_y = " << car0->a_y << endl; //print
uniformAccBySpeed(target_speed_y);
}
/////////////turn/////////////////////
//turn, use radius and angle,
void SceneBase::carTurn(const int& turn_state, const double& R, const double& total_theta) {
car0->updateTurnInfo(turn_state, R);//update the turning
double theta = 0.0;
while (theta < total_theta)
{
theta += fabs(car0->delta_theta);
correctAngleError(car0->delta_theta, theta - total_theta);//error fix
car0->carTurnStep();
obsMoveStep();
showScene();
}
car0->coutInfo();
}
//change lane
void SceneBase::laneChange(const Point& target_point, const int& type, const double& s) {
double dis = car0->pmidr->distanceTo(target_point);
Vec2d vec0(dis, car0->heading_theta + PI / 2.0);
Vec2d vec(*car0->pmidr, target_point);
double L = vec0.crossProd(vec) / dis / 2.0;
double H = vec0.innerProd(vec) / dis / 2.0;
if (fabs(L) < 1e-10)//target_point in front the car, not need to change lane
{
uniformStraight(car0->pmidr->distanceTo(target_point));
return;
}
double R = (pow(L, 2) + pow(H, 2)) / fabs(L) / 2.0;//turning radius, L =0 is return
double target_theta = asin(H / R);//target angle
double target_delta_theta = fabs(car0->speed / R);//angular speed abs
cout << "dis = " << dis << ", L = " << L << ", H = " << H << ", R = " << R << ", target_theta = " << target_theta / PI << ", target_delta_theta = " << target_delta_theta / PI << endl;
if (L > 0.0)//left lane
{
car0->delta_theta = target_delta_theta;
carTurn(TurnDirection::TurnLeft, R, target_theta);
if (type == singleType)
{
car0->delta_theta = -target_delta_theta;
carTurn(TurnDirection::TurnRight, R, target_theta);
}
else
{
car0->delta_theta = -target_delta_theta;
carTurn(TurnDirection::TurnRight, R, target_theta);
uniformStraight(s);
car0->delta_theta = -target_delta_theta;
carTurn(TurnDirection::TurnRight, R, target_theta);
car0->delta_theta = target_delta_theta;
carTurn(TurnDirection::TurnLeft, R, target_theta);
}
}
else if (L < 0.0)//right lane
{
car0->delta_theta = -target_delta_theta;
carTurn(TurnDirection::TurnRight, R, target_theta);
if (type == singleType)
{
car0->delta_theta = target_delta_theta;
carTurn(TurnDirection::TurnLeft, R, target_theta);
}
else
{
car0->delta_theta = target_delta_theta;
carTurn(TurnDirection::TurnLeft, R, target_theta);
uniformStraight(s);//if is overtaking, we need straight move for certain distance
car0->delta_theta = target_delta_theta;
carTurn(TurnDirection::TurnLeft, R, target_theta);
car0->delta_theta = -target_delta_theta;
carTurn(TurnDirection::TurnRight, R, target_theta);
}
}
}