-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcar.h
206 lines (178 loc) · 4.12 KB
/
car.h
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef CAR
#define CAR
#include <deque>
#include <GL/glut.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <cstdio>
//#define _USE_MATH_DEFINES
//#include <cmath>
#include "environment.h"
#include "Object.h"
#include <pthread.h>
#include <iostream>
//#define CARLENGTH 3.0
//#define CARWIDTH 1.6
class Car: public Object {
public:
Car():Object(), timeStop(0), isHorn(false){
pthread_mutex_init(&mutex_path, NULL);
carLength = CARLENGTH;
carWidth = CARWIDTH;
existVeryDangerous = false;
if (SOUND_ENABLED) SDL_init_load();
}
Car(dd l, dd w):Object(),timeStop(0), isHorn(false){
pthread_mutex_init(&mutex_path, NULL);
carLength = l;
carWidth = w;
existVeryDangerous = false;
if (SOUND_ENABLED) SDL_init_load();
};
Car(State astate, dd l, dd w):Object(astate), timeStop(0), isHorn(false){
pthread_mutex_init(&mutex_path, NULL);
carLength = l;
carWidth = w;
existVeryDangerous = false;
if (SOUND_ENABLED) SDL_init_load();
};
Car(State astate):Object(astate), timeStop(0), isHorn(false){
pthread_mutex_init(&mutex_path, NULL);
carLength = CARLENGTH;
carWidth = CARWIDTH;
existVeryDangerous = false;
if (SOUND_ENABLED) SDL_init_load();
};
Car(dd ax, dd ay, dd av, dd atheta, dd l, dd w):Object(ax,ay,av,atheta), timeStop(0), isHorn(false){
pthread_mutex_init(&mutex_path, NULL);
carLength = l;
carWidth = w;
existVeryDangerous = false;
if (SOUND_ENABLED) SDL_init_load();
};
void update_state(dd time_step){
if (HORN_ENABLED)
{
if (state.v < 1e-5 && state.v > -1e-5)
{
timeStop++;
if (timeStop > WAIT_TO_HORN)
{
if (existVeryDangerous)
{
isHorn = true;
if (SOUND_ENABLED && Mix_PlayChannel(-1, horn_sound, 0) ==-1)
{
printf("Failed to play car horn sound\n");
exit(0);
}
timeStop -= HORN_INTERVAL;
}
else
{
state.v = 0.1;
}
}
}
else
{
timeStop = 0;
isHorn = false;
}
}
state.x += getXDot()*time_step;
state.y += getYDot()*time_step;
if (state.y >= Y_MAX) state.y -= Y_MAX;
}
void control(){
Control curControl;
if (!path.empty()) {
pthread_mutex_lock (&mutex_path);
curControl = path.front();
path.pop_front();
pthread_mutex_unlock (&mutex_path);
}
else {
curControl.h1 = 0.0;
curControl.h2 = 0.0;
}
state.v += curControl.h1;
if ((state.v > 1e-6) && (curControl.h2 > 1e-8 || curControl.h2 < -1e-8) )
state.theta += state.v*tan(curControl.h2)/carLength;
else state.theta = state.theta;
if (state.v < 0) state.v = 0;
}
bool SDL_init_load()
{
if (SDL_Init( SDL_INIT_EVERYTHING ) == -1) return false;
if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1) return false;
horn_sound = Mix_LoadWAV("car_horn.wav");
if (horn_sound == NULL) return false;
return true;
}
bool SDL_cleanup()
{
Mix_FreeChunk( horn_sound );
Mix_CloseAudio();
SDL_Quit();
}
void control(dd h1, dd h2){
state.v += h1;
state.theta += state.v*tan(h2)/carLength/10;
}
dd getLength(){
return carLength;
}
dd getWidth(){
return carWidth;
}
std::deque <Control>* getPath()
{
return &path;
}
bool getHorn()
{
return isHorn;
}
bool setHornOff()
{
isHorn = false;
}
void setPath(std::deque <Control> &apath)
{
pthread_mutex_lock (&mutex_path);
path = apath;
pthread_mutex_unlock (&mutex_path);
}
void setExistVeryDangerous(bool a)
{
existVeryDangerous = a;
}
void draw()
{
glColor3f(0.0,0.75,1.0);
glPushMatrix();
glTranslatef(state.x,state.y,0);
glRotatef(state.theta*180.0/M_PI,0,0,1);
glBegin(GL_QUADS);
double hlength = carLength/2-0.1;
double hwidth = carWidth/2-0.1;
glVertex2f(-hlength,-hwidth);
glVertex2f( hlength,-hwidth);
glVertex2f( hlength, hwidth);
glVertex2f(-hlength, hwidth);
glEnd();
glPopMatrix();
}
public:
pthread_mutex_t mutex_path;
private:
dd carLength;
dd carWidth;
unsigned int timeStop;
bool isHorn;
bool existVeryDangerous;
std::deque <Control> path;
Mix_Chunk *horn_sound;
};
#endif