-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspline_trajectory_vis.c
267 lines (240 loc) · 7.45 KB
/
spline_trajectory_vis.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
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
/*
* This file is part of vis-o-mex.
*
* Copyright (C) 2010-2011 Greg Horn <ghorn@stanford.edu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* spline_trajectory_vis.c
* Manage spline trajectory visualization structs. Draw traj spline.
*/
#include <gsl/gsl_errno.h>
#include <gsl/gsl_spline.h>
#include <inttypes.h>
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <string.h>
#include <math.h>
#include <ap_types.h>
#include "spline_trajectory_vis.h"
#include "spline_utils.h"
#include <ap_telemetry.h>
#define NUM_INTERPOLATED_POINTS 1000
#define NUM_ANIMATION_SEGMENTS 4
typedef struct {
uint8_t draw;
uint8_t spline_allocated;
float x[NUM_INTERPOLATED_POINTS];
float y[NUM_INTERPOLATED_POINTS];
float z[NUM_INTERPOLATED_POINTS];
spline_knot_data_t knot_data;
splines_t splines;
} spline_vis_t;
float alpha_animation[NUM_INTERPOLATED_POINTS];
int alpha_head = 0;
spline_vis_t spline_vis_fm;
spline_vis_t spline_vis_ap_active;
spline_vis_t spline_vis_ap_queued;
spline_vis_t spline_vis_transition;
void
fill_spline_vis(spline_vis_t * sv, spline_knot_data_t * kd, int periodic)
{
memcpy( &(sv->knot_data), kd, sizeof( spline_knot_data_t ) );
if (sv->spline_allocated == 1)
free_splines( &(sv->splines) );
reticulate_splines_from_knots( &(sv->splines), &(sv->knot_data), periodic);
for (int k=0; k<NUM_INTERPOLATED_POINTS; k++){
sv->x[k] = gsl_spline_eval( sv->splines.x.spl, ((double)(k))/(NUM_INTERPOLATED_POINTS - 1.0), sv->splines.x.acc);
sv->y[k] = gsl_spline_eval( sv->splines.y.spl, ((double)(k))/(NUM_INTERPOLATED_POINTS - 1.0), sv->splines.y.acc);
sv->z[k] = gsl_spline_eval( sv->splines.z.spl, ((double)(k))/(NUM_INTERPOLATED_POINTS - 1.0), sv->splines.z.acc);
}
sv->spline_allocated = 1;
}
static void
command_ground_handler(const lcm_recv_buf_t *rbuf __attribute__((unused)),
const char *channel __attribute__((unused)),
const ap_command_t *msg,
void *user __attribute__((unused)))
{
switch ( msg->val ){
case VISUALIZER_HIDE_TRANSITION:
{
spline_vis_transition.draw = 0;
break;
}
case VISUALIZER_HIDE_ACTIVE:
{
spline_vis_ap_active.draw = 0;
break;
}
case VISUALIZER_HIDE_QUEUED:
{
spline_vis_ap_queued.draw = 0;
break;
}
case VISUALIZER_HIDE_FLIGHT_MANAGER:
{
spline_vis_fm.draw = 0;
break;
}
case VISUALIZER_SHOW_TRANSITION:
{
spline_vis_transition.draw = 1;
break;
}
case VISUALIZER_SHOW_ACTIVE:
{
spline_vis_ap_active.draw = 1;
break;
}
case VISUALIZER_SHOW_QUEUED:
{
spline_vis_ap_queued.draw = 1;
break;
}
case VISUALIZER_SHOW_FLIGHT_MANAGER:
{
spline_vis_fm.draw = 1;
break;
}
case VISUALIZER_SWAP_ACTIVE_QUEUED:
{
spline_vis_t spline_vis_temp;
memcpy( &spline_vis_temp, &spline_vis_ap_active, sizeof(spline_vis_t));
memcpy( &spline_vis_ap_active, &spline_vis_ap_queued, sizeof(spline_vis_t));
memcpy( &spline_vis_ap_queued, &spline_vis_temp, sizeof(spline_vis_t));
break;
}
}
}
static void
spline_command_ground_handler(const lcm_recv_buf_t *rbuf __attribute__((unused)),
const char *channel __attribute__((unused)),
const ap_spline_command_t *msg,
void *user __attribute__((unused)))
{
switch (msg->command.val){
case VISUALIZER_LOAD_FLIGHT_MANAGER:
{
spline_knot_data_t knot_data;
memcpy( &knot_data, &(msg->knot_data), sizeof(spline_knot_data_t) );
fill_spline_vis( &spline_vis_fm, &knot_data, 1 );
break;
}
case VISUALIZER_LOAD_ACTIVE:
{
spline_knot_data_t knot_data;
memcpy( &knot_data, &(msg->knot_data), sizeof(spline_knot_data_t) );
fill_spline_vis( &spline_vis_ap_active, &knot_data, 1 );
break;
}
case VISUALIZER_LOAD_QUEUED:
{
spline_knot_data_t knot_data;
memcpy( &knot_data, &(msg->knot_data), sizeof(spline_knot_data_t) );
fill_spline_vis( &spline_vis_ap_queued, &knot_data, 1 );
break;
}
case VISUALIZER_LOAD_TRANSITION:
{
spline_knot_data_t knot_data;
memcpy( &knot_data, &(msg->knot_data), sizeof(spline_knot_data_t) );
fill_spline_vis( &spline_vis_transition, &knot_data, 0 );
break;
}
}
}
void
init_spline_trajectory_vis()
{
spline_vis_fm.draw = 0;
spline_vis_ap_active.draw = 0;
spline_vis_ap_queued.draw = 0;
spline_vis_transition.draw = 0;
spline_vis_fm.spline_allocated = 0;
spline_vis_ap_active.spline_allocated = 0;
spline_vis_ap_queued.spline_allocated = 0;
spline_vis_transition.spline_allocated = 0;
for (int k=0; k<NUM_INTERPOLATED_POINTS; k++){
alpha_animation[k] = 0.5*(sin(NUM_ANIMATION_SEGMENTS* ((double)k)*2.0*M_PI/((double)NUM_INTERPOLATED_POINTS) ) + 1.0);
if (alpha_animation[k] < 0.1)
alpha_animation[k] = 0.2;
else
alpha_animation[k] = 1.0;
}
ap_lcm_subscribe_chan(command_t, &command_ground_handler, NULL, "ground_ap_command_t");
ap_lcm_subscribe_chan(spline_command_t, &spline_command_ground_handler, NULL, "ground_ap_spline_command_t");
}
void
draw_one_spline_trajectory( spline_vis_t * sv, float * RGBA)
{
if ( sv->draw == 0 )
return;
// just draw the knots
glPushMatrix();
glPointSize(5.0);
glBegin(GL_POINTS);
float fade = 1.0;
glColor4f(fade*RGBA[0],fade*RGBA[1],fade*RGBA[2],fade*RGBA[3]);
for (int k = 0; k < sv->knot_data.num_knots; k++)
glVertex3f( sv->knot_data.x[k], sv->knot_data.y[k], sv->knot_data.z[k]);
glEnd();
// now draw the interpolated spline
glLineWidth(2.0f);
glBegin(GL_LINE_STRIP);
for ( int k=0;k<NUM_INTERPOLATED_POINTS;k++){
int j = alpha_head + k;
if (j >= NUM_INTERPOLATED_POINTS)
j -= NUM_INTERPOLATED_POINTS;
if (alpha_animation[j] > 0.5)
glColor4f(RGBA[0],RGBA[1],RGBA[2],RGBA[3]);
else
glColor4f(1.0f-RGBA[0],1.0f-RGBA[1],1.0f-RGBA[2],RGBA[3]);
glVertex3f( sv->x[k], sv->y[k], sv->z[k] );
}
glEnd();
glPopMatrix();
}
void
draw_all_spline_trajectories()
{
float fm_RGBA[4];
float active_RGBA[4];
float queued_RGBA[4];
float transition_RGBA[4];
fm_RGBA[0] = 0.7f;
fm_RGBA[1] = 0.4f;
fm_RGBA[2] = 0.5f;
fm_RGBA[3] = 1.0f;
transition_RGBA[0] = 0.8f;
transition_RGBA[1] = 0.8f;
transition_RGBA[2] = 0.2f;
transition_RGBA[3] = 1.0f;
active_RGBA[0] = 0.2f;
active_RGBA[1] = 0.8f;
active_RGBA[2] = 0.3f;
active_RGBA[3] = 0.8f;
queued_RGBA[0] = 0.3f;
queued_RGBA[1] = 0.4f;
queued_RGBA[2] = 1.0f;
queued_RGBA[3] = 1.0f;
alpha_head -= 3;
if (alpha_head < 0)
alpha_head += NUM_INTERPOLATED_POINTS;
draw_one_spline_trajectory( &spline_vis_fm, fm_RGBA );
draw_one_spline_trajectory( &spline_vis_ap_active, active_RGBA );
draw_one_spline_trajectory( &spline_vis_ap_queued, queued_RGBA );
draw_one_spline_trajectory( &spline_vis_transition, transition_RGBA );
}