-
Notifications
You must be signed in to change notification settings - Fork 11
/
ahrs-visualizer.cpp
359 lines (294 loc) · 10.1 KB
/
ahrs-visualizer.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <wordexp.h>
#include <fcntl.h>
#include <bcm_host.h>
#include <GLES/gl.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <boost/program_options.hpp>
namespace opts = boost::program_options;
#include "asset_dir.h"
#include "model_board/model_board.h"
#include "version.h"
/* OpenGL rotation matrix that converts board coordinates
* into ground coordinates (x=north, y=east, z=down). It is
* column-major so do matrix[COL][ROW]. */
float matrix[4][4];
// Acceleration vector in units of g (9.8 m/s^2).
float acceleration[3];
// Magnetic field vector where each component is approximately between -1 and 1.
float magnetic_field[3];
// 0 = Screen faces south, 90 = West, 180 = North, 270 = East
// TODO: read screen_orientation from environment
float screen_orientation = 0;
uint32_t screen_width, screen_height;
EGLDisplay display;
EGLSurface surface;
EGLContext context;
static inline VC_RECT_T rect_width_height(int width, int height)
{
VC_RECT_T r;
r.x = r.y = 0;
r.width = width;
r.height = height;
return r;
}
static void nice_bcm_host_init()
{
int fd = open("/dev/vchiq", O_RDWR | O_LARGEFILE);
if (fd == -1)
{
char buffer[256];
char * msg = strerror_r(errno, buffer, sizeof(buffer) - 1);
if (msg)
{
std::cerr << "Warning: Could not open /dev/vchiq: "
<< msg << "." << std::endl;
}
else
{
std::cerr << "Warning: Could not open /dev/vchiq." << std::endl;
}
}
else
{
close(fd);
}
bcm_host_init();
}
// Sets the display, OpenGL|ES context and screen stuff
static void opengl_init(void)
{
EGLBoolean result;
EGLint num_config;
static EGL_DISPMANX_WINDOW_T nativewindow;
DISPMANX_ELEMENT_HANDLE_T dispman_element;
DISPMANX_DISPLAY_HANDLE_T dispman_display;
DISPMANX_UPDATE_HANDLE_T dispman_update;
static const EGLint attribute_list[] =
{
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE
};
EGLConfig config;
// get an EGL display connection
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (display == EGL_NO_DISPLAY)
{
throw std::runtime_error("Failed to get display. eglGetDisplay failed.");
}
// initialize the EGL display connection
result = eglInitialize(display, NULL, NULL);
if (result == EGL_FALSE)
{
throw std::runtime_error("Failed to initialize display. eglInitialize failed.");
}
// get an appropriate EGL frame buffer configuration
result = eglChooseConfig(display, attribute_list, &config, 1, &num_config);
if (result == EGL_FALSE)
{
throw std::runtime_error("Failed to choose config. eglChooseConfig failed.");
}
// create an EGL rendering context
context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
if (context == EGL_NO_CONTEXT)
{
throw std::runtime_error("Failed to create context. eglCreateContext failed.");
}
// create an EGL window surface
int32_t success = graphics_get_display_size(0 /* LCD */, &screen_width, &screen_height);
if (success < 0)
{
throw std::runtime_error("Failed to get display size.");
}
VC_RECT_T dst_rect = rect_width_height(screen_width, screen_height);
VC_RECT_T src_rect = rect_width_height(screen_width<<16, screen_height<<16);
dispman_display = vc_dispmanx_display_open(0 /* LCD */);
dispman_update = vc_dispmanx_update_start(0);
dispman_element = vc_dispmanx_element_add (dispman_update, dispman_display,
0/*layer*/, &dst_rect, 0/*src*/,
&src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, (DISPMANX_TRANSFORM_T)0/*transform*/);
nativewindow.element = dispman_element;
nativewindow.width = screen_width;
nativewindow.height = screen_height;
vc_dispmanx_update_submit_sync(dispman_update);
surface = eglCreateWindowSurface(display, config, &nativewindow, NULL);
if(surface == EGL_NO_SURFACE)
{
fprintf(stderr, "eglCreateWindowSurface returned ELG_NO_SURFACE. "
"Try closing other OpenGL programs.\n");
exit(1);
}
// connect the context to the surface
result = eglMakeCurrent(display, surface, surface, context);
if (result == EGL_FALSE)
{
throw std::runtime_error("Failed to connect to the surface.");
}
glClearColor(0, 0, 0, 0.5); // set background colors
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear buffers
glShadeModel(GL_FLAT);
// Enable back face culling.
glEnable(GL_CULL_FACE);
}
// Description: Sets the OpenGL|ES model to default values
static void projection_init()
{
float nearp = 1, farp = 500.0f, hht, hwd;
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glViewport(0, 0, (GLsizei)screen_width, (GLsizei)screen_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
hht = nearp * tan(45.0 / 2.0 / 180.0 * M_PI);
hwd = hht * screen_width / screen_height;
glFrustumf(-hwd, hwd, -hht, hht, nearp, farp);
}
static void textures_init(void)
{
// Enable alpha blending so what we see through transparent
// parts of the model is the same as the background.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
}
static void redraw_scene()
{
// Start with a clear screen
glClear(GL_COLOR_BUFFER_BIT);
// Move the camera back so we can see the board.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -30);
// Convert screen coords (right, up, out) to ground coords (north, east, down).
glRotatef(90, 1, 0, 0);
glRotatef(-90, 0, 0, 1);
glRotatef(-screen_orientation, 0, 0, 1);
// Convert ground coords to board coordinates.
glMultMatrixf(matrix[0]);
model_board_redraw(acceleration, magnetic_field);
eglSwapBuffers(display, surface);
}
static void opengl_deinit(void)
{
// clear screen
glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(display, surface);
// Release OpenGL resources
eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroySurface(display, surface);
eglDestroyContext(display, context);
eglTerminate(display);
}
/* The matrix expected on the standrd input is a
* ROW-major matrix that converts a vector from board coordinates
* to ground coordinates. */
static void read_input(void)
{
// Set the translation part to be the identity.
matrix[3][0] = matrix[3][1] = matrix[3][2] = 0;
matrix[0][3] = matrix[1][3] = matrix[2][3] = 0;
matrix[3][3] = 1;
while(1)
{
// Read the rotation matrix from the standard input.
// The input values are ROW-major but we need to make a
// COLUMN-major matrix for OpenGL.
int result = scanf("%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
&matrix[0][0], &matrix[1][0], &matrix[2][0],
&matrix[0][1], &matrix[1][1], &matrix[2][1],
&matrix[0][2], &matrix[1][2], &matrix[2][2],
&acceleration[0], &acceleration[1], &acceleration[2],
&magnetic_field[0], &magnetic_field[1], &magnetic_field[2]);
// Read to the end of the line so that we don't get stuck forever on one invalid line.
while(getc(stdin) != '\n');
if (result >= 9)
{
break; // Success
}
fprintf(stderr, "unrecognized input: only regonized %d items.\n", result);
}
}
static void read_args(int argc, char *argv[])
{
try
{
opts::options_description generic("Generic options");
generic.add_options()
("help,h", "produce help message")
("version,v", "print version number")
;
opts::options_description config("Configuration");
config.add_options()
("screen-angle,a", opts::value<float>(&screen_orientation)->default_value(0),
"specifies your screen orientation. "
"0 = screen faces South, 90 = West, 180 = North, 270 = East\n")
;
opts::options_description cmdline_options;
cmdline_options.add(generic);
cmdline_options.add(config);
// Read options from command line.
opts::variables_map options;
opts::store(opts::command_line_parser(argc, argv).options(cmdline_options).run(), options);
// Read options form config file, ~/.ahrs-visualizer
{
wordexp_t expansion_result;
wordexp("~/.ahrs-visualizer", &expansion_result, 0);
std::ifstream file(expansion_result.we_wordv[0]);
opts::store(opts::parse_config_file(file, config), options);
}
opts::notify(options);
if(options.count("help"))
{
std::cout << cmdline_options;
std::cout << "For more information, run: man ahrs-visualizer" << std::endl;
exit(0);
}
if (options.count("version"))
{
std::cout << VERSION << std::endl;
exit(0);
}
}
catch(const opts::multiple_occurrences & error)
{
std::cerr << "Error: " << error.what() << " of " << error.get_option_name() << " option." << std::endl;
exit(1);
}
}
int main(int argc, char *argv[])
{
try
{
read_args(argc, argv);
asset_dir_init();
nice_bcm_host_init();
opengl_init();
projection_init();
textures_init();
model_board_init();
while(1)
{
read_input();
redraw_scene();
}
opengl_deinit();
bcm_host_deinit();
return 0;
}
catch(const std::exception & error)
{
std::cerr << "Error: " << error.what() << std::endl;
exit(9);
}
}