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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "implot.h"
#include "control.h"
#include <stdio.h>
#include <string.h>
#include <memory.h>
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <GLES2/gl2.h>
#endif
#undef GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h> // Will drag system OpenGL headers
// [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to
// maximize ease of testing and compatibility with old VS compilers. To link
// with VS2010-era libraries, VS2015+ requires linking with
// legacy_stdio_definitions.lib, which we do using this pragma. Your own
// project should not be affected, as you are likely to link with a newer
// binary of GLFW that is adequate for your version of Visual Studio.
#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
#pragma comment(lib, "legacy_stdio_definitions")
#endif
#define MAX_ZP 64
struct GState
{
struct {
int time;
int bode;
int nyquist;
int rlocus;
} n_samples;
float gain_re;
float gain_im;
bool autogain;
float time_start;
float time_len;
float bode_start;
float bode_len;
float rlocus_start;
float rlocus_len;
size_t npoles, nzeros;
ImPlotPoint poles[MAX_ZP], zeros[MAX_ZP];
};
struct CState
{
bool proper;
ct::TimeSeries time;
ct::LocusSeries rlocus;
};
ImPlotPoint time_real_getter(int idx, void *data)
{
ct::TimeSeries *ts = (ct::TimeSeries *) data;
return ImPlotPoint(ts->time(idx), ts->out(idx).real());
}
ImPlotPoint time_imag_getter(int idx, void *data)
{
ct::TimeSeries *ts = (ct::TimeSeries *) data;
return ImPlotPoint(ts->time(idx), ts->out(idx).imag());
}
ImPlotPoint step_getter(int idx, void *data)
{
ct::TimeSeries *ts = (ct::TimeSeries *) data;
return ImPlotPoint(ts->time(idx), idx != 0);
}
ImPlotPoint rlocus_getter(int idx, void *data)
{
// return ImPlotPoint(ls->out(row, idx).real(), ls->out(row, idx).imag());
}
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
int main(int, char**)
{
// Setup window
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
const char* glsl_version = "#version 100";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#elif defined(__APPLE__)
// GL 3.2 + GLSL 150
const char* glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
// Create window with graphics context
GLFWwindow* window = glfwCreateWindow(1280, 720, "Fast SISO Tool", NULL, NULL);
if (window == NULL)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImPlot::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark();
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
// State variables
bool show_demo_window = true;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
// Graphics state
GState gstate = {
// settings
.n_samples = {
.time = 500,
.bode = 500,
.nyquist = 500,
.rlocus = 2000,
},
.gain_re = 1.,
.gain_im = 0.,
.autogain = false,
.time_start = 0,
.time_len = 10,
.bode_start = 0.01,
.bode_len = 10000,
.rlocus_start = 0.001,
.rlocus_len = 1000,
.npoles = 0,
.nzeros = 0,
};
GState prev_gstate = { };
// Computation state
CState cstate = {
.proper = false,
.time = ct::TimeSeries(gstate.time_start,
gstate.time_start + gstate.time_len, gstate.n_samples.time),
.rlocus = ct::LocusSeries(gstate.rlocus_start,
gstate.rlocus_start + gstate.rlocus_len, gstate.n_samples.rlocus),
};
// Main loop
while (!glfwWindowShouldClose(window))
{
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags
// to tell if dear imgui wants to use your inputs.
//
// - When io.WantCaptureMouse is true, do not dispatch mouse input data
// to your main application.
//
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard
// input data to your main application.
//
// Generally you may always pass all inputs to dear imgui, and hide
// them from your application based on those two flags.
glfwPollEvents();
// Recompute simulation if necessary
if (memcmp(&gstate, &prev_gstate, sizeof(GState)))
{
if (gstate.npoles || gstate.nzeros)
{
// Create new transfer function
ct::TransferFn tf;
for (int k = 0; k < gstate.npoles; k++)
tf.add_pole(ct::complex(gstate.poles[k].x, gstate.poles[k].y));
for (int k = 0; k < gstate.nzeros; k++)
tf.add_zero(ct::complex(gstate.zeros[k].x, gstate.zeros[k].y));
ct::TransferFn tf_cl = ct::feedback(tf);
// Autogain
if (gstate.autogain)
{
ct::complex gain = 1. / tf_cl.dc_gain();
tf_cl = gain * tf_cl;
gstate.gain_re = gain.real();
gstate.gain_im = gain.imag();
}
else
{
ct::complex gain(gstate.gain_re, gstate.gain_im);
tf_cl = gain * tf_cl;
}
if (tf_cl.is_proper())
{
// Convert to state space
ct::SSModel ss = ct::ctrb_form(ct::feedback(tf));
// New time series
cstate.time = ct::TimeSeries(gstate.time_start,
gstate.time_start + gstate.time_len, gstate.n_samples.time);
// Update time domain simulation
ct::step(ss, cstate.time);
// New root locus series
cstate.rlocus = ct::LocusSeries(gstate.rlocus_start,
gstate.rlocus_start + gstate.rlocus_len, gstate.n_samples.rlocus);
ct::rlocus(tf, cstate.rlocus);
}
}
// Update graphic state
memcpy(&prev_gstate, &gstate, sizeof(GState));
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if (show_demo_window)
{
ImGui::ShowDemoWindow(&show_demo_window);
ImPlot::ShowDemoWindow(&show_demo_window);
}
if (ImGui::Begin("Settings"))
{
static bool zero_index = 0, pole_index = 0;
static char buf[32];
ImGui::SeparatorText("Plant G(s)");
ImGui::Checkbox("Automatic", &gstate.autogain);
ImGui::SliderFloat("Gain (real)", &gstate.gain_re, -1000, 1000, NULL, ImGuiSliderFlags_Logarithmic);
ImGui::SliderFloat("Gain (imag)", &gstate.gain_im, -1000, 1000, NULL, ImGuiSliderFlags_Logarithmic);
ImGui::PushItemWidth(140);
if (ImGui::BeginListBox("Poles"))
{
for (int i = 0; i < gstate.npoles; i++)
{
ImGui::PushID(i);
snprintf(buf, sizeof(buf), "(%.2f, %.2fi)", gstate.poles[i].x, gstate.poles[i].y);
if (ImGui::Selectable(buf))
{
gstate.poles[i].y = 0;
}
ImGui::PopID();
}
ImGui::EndListBox();
}
ImGui::SameLine();
if (ImGui::BeginListBox("Zeros"))
{
for (int i = 0; i < gstate.nzeros; i++)
{
ImGui::PushID(i);
snprintf(buf, sizeof(buf), "(%.2f, %.2fi)", gstate.zeros[i].x, gstate.zeros[i].y);
if (ImGui::Selectable(buf))
{
gstate.zeros[i].y = 0;
}
ImGui::PopID();
}
ImGui::EndListBox();
}
ImGui::PopItemWidth();
ImGui::SeparatorText("Simulation Ranges");
ImGui::InputFloat("Time Start", &gstate.time_start, 0, 100);
ImGui::SliderFloat("Time Length", &gstate.time_len, 0, 100, NULL, ImGuiSliderFlags_Logarithmic);
ImGui::InputFloat("Bode Start", &gstate.bode_start, 0, 100);
ImGui::SliderFloat("Bode Length", &gstate.bode_len, 0, 100, NULL, ImGuiSliderFlags_Logarithmic);
ImGui::InputFloat("Root Locus Start", &gstate.rlocus_start, 0, 100);
ImGui::SliderFloat("Root Locus Length", &gstate.rlocus_len, 0.001, 10000, NULL, ImGuiSliderFlags_Logarithmic);
ImGui::SeparatorText("Number of Samples");
ImGui::SliderInt("Time", &gstate.n_samples.time, 50, 5000);
ImGui::SliderInt("Bode", &gstate.n_samples.bode, 50, 5000);
ImGui::SliderInt("Nyquist", &gstate.n_samples.nyquist, 50, 5000);
ImGui::SliderInt("Root Locus", &gstate.n_samples.rlocus, 50, 5000);
}
ImGui::End();
if (ImGui::Begin("Bode Diagrams"))
{
if (ImPlot::BeginPlot("Bode Magnitude", ImVec2(-1, 0)))
{
ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Log10);
ImPlot::SetupAxisScale(ImAxis_Y1, ImPlotScale_Log10);
ImPlot::SetupAxesLimits(0.01, 10000, 0.001, 10);
ImPlot::SetupAxis(ImAxis_X1, "Amplitude |G(iw)| dB");
ImPlot::SetupAxis(ImAxis_Y1, "Frequency w / (rad / s)");
ImPlot::EndPlot();
}
if (ImPlot::BeginPlot("Bode Phase", ImVec2(-1, 0)))
{
ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Linear);
ImPlot::SetupAxesLimits(0.1, 100, -180, 180);
ImPlot::SetupAxis(ImAxis_X1, "Phase arg G(iw) / deg");
ImPlot::SetupAxis(ImAxis_Y1, "Frequency w / (rad / s)");
ImPlot::EndPlot();
}
}
ImGui::End();
if (ImGui::Begin("Time Domain"))
{
if (ImPlot::BeginPlot("Step Response", ImVec2(-1, 0)))
{
ImPlot::SetupAxisScale(ImAxis_X1, ImPlotScale_Linear);
ImPlot::SetupAxis(ImAxis_X1, "Time t / s");
ImPlot::SetupAxisLimits(ImAxis_X1, gstate.time_start,
gstate.time_start + gstate.time_len, ImPlotCond_Always);
ImPlot::SetupAxis(ImAxis_Y1, "Response y(t)", ImPlotAxisFlags_AutoFit);
ImPlot::PlotLineG("Step", step_getter, &cstate.time, cstate.time.n_samples);
ImPlot::PlotLineG("G(s), real", time_real_getter, &cstate.time, cstate.time.n_samples);
ImPlot::PlotLineG("G(s), imag", time_imag_getter, &cstate.time, cstate.time.n_samples);
ImPlot::EndPlot();
}
}
ImGui::End();
if (ImGui::Begin("Stability"))
{
ImGui::Text("Use CTRL + R / L Click to add a pole / zero");
int rlflags = ImPlotFlags_Equal | ImPlotFlags_NoBoxSelect;
if (ImGui::GetIO().KeyCtrl)
rlflags |= ImPlotFlags_Crosshairs;
if (ImPlot::BeginPlot("Root Locus", ImVec2(-1, 0), rlflags))
{
ImPlot::SetupAxesLimits(-10, 10, -10, 10);
// Plot curren root locus
for (int i = 0; i < cstate.rlocus.out.n_rows; i++)
{
ImGui::PushID(i);
// ImPlot::PlotLineG("Root Locus", rlocus_getter, &cstate.rlocus.out.row(i), cstate.rlocus.n_samples);
ImGui::PopID();
}
if (ImPlot::IsPlotHovered() && ImGui::GetIO().KeyCtrl)
{
ImPlotPoint pt = ImPlot::GetPlotMousePos();
// Add a pole
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left))
{
if (gstate.npoles < MAX_ZP)
{
gstate.poles[gstate.npoles].x = pt.x;
gstate.poles[gstate.npoles].y = pt.y;
gstate.npoles += 1;
}
}
// Add a zero
else if (ImGui::IsMouseClicked(ImGuiMouseButton_Right))
{
if (gstate.npoles < MAX_ZP)
{
gstate.zeros[gstate.nzeros].x = pt.x;
gstate.zeros[gstate.nzeros].y = pt.y;
gstate.nzeros += 1;
}
}
}
// Plot zeros and poles
const ImVec4 poleCol(0,0.9f,0,1);
const ImVec4 zeroCol(1,0.5f,1,1);
const float size = 4;
const int flags = ImPlotDragToolFlags_Delayed;
for (int i = 0; i < gstate.nzeros; i++)
ImPlot::DragPoint(i, &gstate.zeros[i].x, &gstate.zeros[i].y, zeroCol, size, flags);
for (int i = 0; i < gstate.npoles; i++)
ImPlot::DragPoint(gstate.nzeros + i, &gstate.poles[i].x, &gstate.poles[i].y,
poleCol, size, flags);
ImPlot::EndPlot();
}
if (ImPlot::BeginPlot("Nyquist Diagram", ImVec2(-1, 0), ImPlotFlags_Equal))
{
ImPlot::SetupAxesLimits(-1, 1, -1, 1);
ImPlot::SetNextMarkerStyle(ImPlotMarker_Cross);
ImPlot::EndPlot();
}
}
ImGui::End();
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w,
clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImPlot::DestroyContext();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
// vim:ts=2 sw=2 noet:
|