/
main.c
193 lines (152 loc) · 4.01 KB
/
main.c
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
#include <SDL.h>
#include <SDL_timer.h>
#include <stdio.h>
#include <math.h>
#include "pid.h"
#include "physics.h"
#define TRUE 0
#define FALSE 1
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define POLICE_WIDTH 70
#define POLICE_HEIGHT 35
#define POLICE_MASS 10 //kg
int running = TRUE;
int mouseX = 0;
int mouseY = 0;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* police = NULL;
SDL_Rect policeDest;
struct pidGains gains;
struct pidState stateX;
struct pidState stateY;
struct physicalFrame frameX;
struct physicalFrame frameY;
int Init () {
if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) {
return 1;
}
if ((window = SDL_CreateWindow("PID Demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_FULLSCREEN)) == NULL) {
return 2;
}
if ((renderer = SDL_CreateRenderer(window, -1, 0)) == NULL) {
return 3;
}
SDL_Surface* policeBMP;
if ((policeBMP = SDL_LoadBMP("./police.bmp")) == NULL) {
return 4;
}
if ((police = SDL_CreateTextureFromSurface(renderer, policeBMP)) == NULL) {
return 5;
}
SDL_FreeSurface(policeBMP);
SDL_Cursor* cursor;
if ((cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR)) == NULL) {
return 6;
}
SDL_SetCursor(cursor);
policeDest.w = POLICE_WIDTH;
policeDest.h = POLICE_HEIGHT;
gains.p = 10;
gains.i = 1;
gains.d = 5;
frameX.p = (SCREEN_WIDTH / 2) - (POLICE_WIDTH / 2);
frameX.v = 0;
frameX.a = 0;
frameY.p = (SCREEN_HEIGHT / 2) - (POLICE_HEIGHT / 2);
frameY.v = 0;
frameY.a = 0;
stateX.error = 0;
stateX.integral = 0;
stateY.error = 0;
stateY.integral = 0;
return 0;
}
void Loop () {
static int lastTicks = 0;
static float forceX = 0;
static float forceY = 0;
float dt;
int ticks;
ticks = SDL_GetTicks();
dt = (ticks - lastTicks) / 1000.0;
lastTicks = ticks;
if (dt == 0) {
return;
}
frameX = calculateNextPhysicalFrame(frameX, POLICE_MASS, forceX, dt);
frameY = calculateNextPhysicalFrame(frameY, POLICE_MASS, forceY, dt);
forceX = calculatePIDResponse(&stateX, gains, mouseX, frameX.p, dt);
forceY = calculatePIDResponse(&stateY, gains, mouseY, frameY.p, dt);
}
void Render () {
policeDest.x = frameX.p - (POLICE_WIDTH / 2);
policeDest.y = frameY.p - (POLICE_HEIGHT / 2);
float angle = atan2(-frameY.v, -frameX.v) * (180.0 / M_PI);
// Clear frame to black
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
// Draw police car
SDL_RenderCopyEx(renderer, police, NULL, &policeDest, angle, NULL, SDL_FLIP_NONE);
// Draw vectors
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderDrawLine(renderer, frameX.p, frameY.p, frameX.p+(frameX.a/5), frameY.p+(frameY.a/5));
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderDrawLine(renderer, frameX.p, frameY.p, frameX.p+(frameX.v/5), frameY.p+(frameY.v/5));
// Flip frame
SDL_RenderPresent(renderer);
}
void OnEvent (SDL_Event* event) {
switch(event->type) {
case SDL_MOUSEMOTION:
mouseX = event->motion.x;
mouseY = event->motion.y;
break;
case SDL_KEYUP:
if (event->key.keysym.sym == SDLK_q) {
running = FALSE;
}
break;
case SDL_QUIT:
running = FALSE;
break;
}
}
void PrintError (int error) {
switch(error) {
case 1:
puts("Failed to init SDL.");
break;
case 2:
puts("Failed to create SDL window.");
break;
case 3:
puts("Failed to creater SDL renderer.");
break;
case 4:
puts("Failed to load police.bmp.");
break;
case 5:
puts("Failed to texturize police.png.");
break;
case 6:
puts("Failed to create cursor.");
}
}
int main (int argc, char *argv[]) {
SDL_Event event;
int error = Init();
if (error) {
PrintError(error);
SDL_Quit();
return -1;
}
while(running == TRUE) {
while(SDL_PollEvent(&event)) { OnEvent(&event); }
Loop();
Render();
}
SDL_Quit();
return 0;
}