-
Notifications
You must be signed in to change notification settings - Fork 6
/
gsyDqMain.py
2110 lines (1455 loc) · 69.3 KB
/
gsyDqMain.py
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Main script for dynamic visualisation of Clarke and Park Transforms.
Note that DO NOT USE matplotlib 3.0.2. It causes errors.
A working matplotlib version is 2.3.3.
Author : 高斯羽 博士 (Dr. GAO, Siyu)
Version : 0.2.0
Last modified : 2019-02-28
List of functions
------------------
* animate_
* help_on_clicked_
* init_
* load_ffmpeg_on_clicked_
* make_ani_
* video_play_on_clicked_
* video_save_on_clicked_
* video_stop_on_clicked_
Function definitions
----------------------
"""
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import tkinter as tk
import tkinter.messagebox as msgbox
import threading
import os
import sys
from numpy import sin, cos
from matplotlib.widgets import Button, TextBox
from tkinter import filedialog
# custom modules
from gsyDqLib import date_time_now
from gsyDqLib import cal_ABDQ
from gsyDqLib import find_pll_direction, find_sequences
from gsyDqLib import set_font_size
from gsyDqLib import collect_tb, load_ffmpeg
from gsyDqLib import check_file_saved, save_animation_to_disk
from gsyIO import save_txt_on_event, search_file_and_start
from gsyINI import read_ini_to_tb, write_ini
# matplotlib font settings
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['font.serif'] = 'Times New Roman'
mpl.rcParams['mathtext.fontset'] = 'cm'
# constants
CONST_INI_FILENAME = 'gsy_dq.ini'
CONST_ORANGE = (255/255, 165/255, 0/255) # color orange
CONST_PURPLE = (204/255, 51/255, 255/255) # color purple
CONST_WIDTH = 1280
CONST_HEIGHT = 720
CONST_DPI = 100
CONST_STR_DOCT_FILENAME = 'index.html'
CONST_STR_COPYRIGHT = ('\u00a9 $Dr. GAO, \ Siyu. 2017$'
+ '\n' + '$siyu.gao@outlook.com$')
CONST_STR_VER = '1.1'
CONST_STR_HELP = u'''
This program is intended to visualise the Clarke and Park transforms.
Written by : Dr. GAO, Siyu
Version : 1.1
Input Harmonic Order :
This sets the order of harmonic to be analysed. 1st order is the fundamental. This input needs to be a float.
PLL Order :
This sets the angular velocity of the PLL as multiples of the fundamental frequency. This input needs to be a float.
Positive number means the PLL is rotating anti-clockwise.
Negative number means the PLL is rotating clockwise.
Samples :
This sets how many samples are taken within one fundamental period. This also sets the total frames for the video.
Increasing this number would make the curves smoother. But the program would consume more resource.
FPS :
This sets the frame rate for saving video. This frame rate is not applied when runing on-the-fly.
Base Freq :
This sets the fundamental frequency.
NOTE : you'd better to stop the video first before changing the above input field settings.
Stop :
This button would stop the video. You can ONLY change the input fields when the video is stopped.
Play :
This button would refresh the video. Any updates made to the input fileds would be applied.
Save video :
This button would trigger the video to be saved. Only limited progress would be displayed
due to the use of maplotlib's built-in save function. A message would be prompted when the save is finished.
The codec FFmpeg is required. It's free to download and use. The FFmpeg binary (ffmpeg.exe) is required.
It's usually located in the "bin" folder. The video length equals Samples divided by FPS.
Browse :
This button would allow you to browse for the FFmpeg binary.
The path would be saved to an INI file and loaded on next program start-up.
'''
print(date_time_now() + 'Started')
# =============================================================================
# <Help figure>
# =============================================================================
fig_help = plt.figure(figsize=(720/CONST_DPI, 640/CONST_DPI), dpi=CONST_DPI, num='Help')
ax_button_save_help = plt.axes([0.85, 0.025, 0.12, 0.06])
plt.close(fig_help)
text_help = fig_help.text(0.02, 1, CONST_STR_HELP, va='top')
text_help.set_family('Segoe UI')
text_help.set_fontweight('normal')
text_help.set_fontsize(9)
button_save_help = Button(ax_button_save_help, 'Save as txt', color='gold')
button_save_help.label.set_family('Arial')
button_save_help.label.set_fontweight('bold')
button_save_help.on_clicked(lambda x: save_txt_on_event(x, CONST_STR_HELP))
# =============================================================================
# </Help figure>
# =============================================================================
# =============================================================================
# <Main figure>
# =============================================================================
fig_main = plt.figure(figsize=(CONST_WIDTH/CONST_DPI, CONST_HEIGHT/CONST_DPI),
dpi=CONST_DPI,
num='Visualisation of Clarke and Park Transforms')
fig_main.suptitle(r'Clarke and Park Transforms',
fontsize=16, fontweight='bold', family='Arial', y = 0.95)
fig_main.text(0.88, 0.95, CONST_STR_COPYRIGHT, va='top')
# -----------------------------------------------------------------------------
# <Main figure interactive controls>
# -----------------------------------------------------------------------------
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# <Interactive, input texts>
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# text box, input harmonic order
ax_tb_input_harmonic = plt.axes([0.1, 0.04, 0.03, 0.03])
textbox_input_harmonic = TextBox(ax_tb_input_harmonic,
'Input \u0020 \n Harmonic Order : ',
initial='', color='w')
# text box, input PLL order
ax_tb_pll_order = plt.axes([0.1, 0.005, 0.03, 0.03])
textbox_pll_order = TextBox(ax_tb_pll_order, 'Input PLL Order : ',
initial='', color='w')
# text box, sampling points
ax_tb_samples = plt.axes([0.19, 0.04, 0.03, 0.03])
textbox_samples = TextBox(ax_tb_samples, 'Samples : ',
initial='', color='w')
# text box, frames per second (FPS), only truely while in saved video, on the fly animation may not
ax_tb_fps = plt.axes([0.19, 0.005, 0.03, 0.03])
textbox_fps = TextBox(ax_tb_fps, 'FPS : ', initial='', color='w')
# text box, base frequency
ax_tb_base_freq = plt.axes([0.262, 0.04, 0.03, 0.03])
textbox_base_freq = TextBox(ax_tb_base_freq, 'Base \u0020 \n Freq : ',
initial='', color='w')
# text box, FFmpeg binary path
ax_tb_ffmpeg_path = plt.axes([0.6, 0.005, 0.305, 0.03])
textbox_ffmpeg_path = TextBox(ax_tb_ffmpeg_path, 'FFmpeg path :',
initial='', color='w')
# set text boxes' font family and font weight
list_textbox = [textbox_input_harmonic, textbox_pll_order,
textbox_samples, textbox_fps, textbox_base_freq,
textbox_ffmpeg_path]
for item in list_textbox:
item.label.set_family('Arial')
item.label.set_fontweight('bold')
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# </Interactive, input texts>
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# <Interactive, buttons>
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# button, stop the video
ax_button_stop = plt.axes([0.315, 0.04, 0.03, 0.03])
button_stop = Button(ax_button_stop, 'Stop', color='pink')
# button, play the video
ax_button_play = plt.axes([0.365, 0.04, 0.03, 0.03])
button_play = Button(ax_button_play, 'Play', color='gold')
# button, save the video
ax_button_save_video = plt.axes([0.315, 0.005, 0.08, 0.03])
button_save_video = Button(ax_button_save_video, 'Save Video', color='grey')
button_save_video.label.set_color('w')
# button, browse for FFmpeg binary
ax_button_browse = plt.axes([0.91, 0.005, 0.05, 0.03])
button_browse = Button(ax_button_browse, 'Browse', color='lightgrey')
# button, display help
ax_button_help = plt.axes([0.4, 0.04, 0.08, 0.03])
button_help = Button(ax_button_help, 'Help', color='lime')
button_help.label.set_fontsize(12)
# button, open documentation
ax_button_doct = plt.axes([0.4, 0.005, 0.08, 0.03])
button_doct = Button(ax_button_doct, 'DOCT', color='skyblue')
button_doct.label.set_fontsize(12)
# set buttons' font family and font weight
list_button = [button_stop, button_play,
button_save_video,
button_help, button_doct,
button_browse]
for item in list_button:
item.label.set_family('Arial')
item.label.set_fontweight('bold')
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# </Interactive, buttons>
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# -----------------------------------------------------------------------------
# </Main figure interactive controls>
# -----------------------------------------------------------------------------
# get current work folder path
str_cwd = os.getcwd()
# system independent path join
str_ini_file_path = os.path.join(str_cwd, CONST_INI_FILENAME)
# assign the values stored in the INI file to the textboxes
bool_ini_exist, config = read_ini_to_tb(str_ini_file_path, list_textbox)
# <Condition>
if bool_ini_exist == True:
# if the INI file found, use it, else, use default values
# on exception, use default values
try:
dbl_harmonic_order = float(textbox_input_harmonic.text)
dbl_harmonic_order = abs(dbl_harmonic_order)
dbl_pll_order = float(textbox_pll_order.text)
dbl_base_freq = float(textbox_base_freq.text)
dbl_base_freq = abs(dbl_base_freq)
if dbl_base_freq == 0:
dbl_base_freq = 50
else:
dbl_base_freq = dbl_base_freq
dbl_base_period = 1 / dbl_base_freq
int_samples = int(textbox_samples.text)
int_samples = abs(int_samples)
int_fps = int(textbox_fps.text)
int_fps = abs(int_fps)
str_ffmpeg_path = textbox_ffmpeg_path.text
except:
# default settings
dbl_harmonic_order = 1
dbl_pll_order = 1
dbl_base_freq = 50
dbl_base_period = 1 / dbl_base_freq
int_samples = 200
int_fps = 30
str_ffmpeg_path = ''
else:
# default settings
dbl_base_freq = 50
dbl_base_period = 1 / dbl_base_freq
dbl_pll_order = 1
dbl_harmonic_order = 1
int_samples = 200
int_fps = 30
str_ffmpeg_path = ''
# </Condition>
# make the data
(time, theta,
alpha_vector, beta_vector,
d_vector, q_vector,
d_ax_on_x, d_ax_on_y,
q_ax_on_x, q_ax_on_y,
d_vector_on_x, d_vector_on_y,
q_vector_on_x, q_vector_on_y) = cal_ABDQ(int_samples, dbl_base_freq,
dbl_harmonic_order, dbl_pll_order)
# get pll frequency info
str_freq_pll = find_pll_direction(dbl_base_freq, dbl_pll_order)
# get sequence info
(str_freq_harmonic,
str_freq_clarke,
str_freq_park,
dbl_period_clarke,
dbl_period_park) = find_sequences(dbl_base_freq, dbl_harmonic_order, dbl_pll_order)
# set font size
dbl_font_size = set_font_size(dbl_harmonic_order)
# -----------------------------------------------------------------------------
# <Coordinate 1, the rotating vectors>
# -----------------------------------------------------------------------------
# static, set coordinate axes
ax1 = plt.subplot(1, 2, 1)
# make axes equal
plt.axis('equal')
# axes limits
plt.axis([-3, 3, -3, 3])
# static, grid lines
ax1.grid(visible=True, zorder=0)
# static, unit circle
ax1.plot(cos(theta), sin(theta), linewidth=2, color='k',zorder=3, linestyle=':')
# static, alpha axis
ax1.annotate("",
xy=(2, 0), xytext=(-2, 0),
arrowprops=dict(shrink=0, fc='black', ec='black', lw=0.1))
# static, alpha axis label
ax1.text(2, -0.3, r'$\alpha$', fontsize=15, horizontalalignment='center')
# static, beta axis
ax1.annotate("",
xy=(0, 2), xytext=(0, -2),
arrowprops=dict(shrink=0, fc='black', ec='black', lw=0.1))
# static, beta axis label
ax1.text(0.2, 2, r'$\beta$', fontsize=15, verticalalignment='center')
# static, frequency info
ax1_text_freq_harmonic = ax1.text(-4.07, 2.95, str_freq_harmonic,
zorder=10, fontsize=10.5,
va='top', ha='left',
bbox=dict(facecolor='white', edgecolor='red',
boxstyle='round'))
ax1_text_freq_pll = ax1.text(-4.07, 1.5, str_freq_pll,
zorder=10, fontsize=10.5,
va='top', ha='left',
bbox=dict(facecolor='white', edgecolor='blue',
boxstyle='round'))
# static, set legend
# make some empty plots, set the colours and labels and display the legends
# that's why "pseudo"
ax1_pseudo_harmonic, = ax1.plot([], [], label=r'$Input$' + '\n' + r'$harmonic$',
color='blue', lw=3)
ax1_pseudo_alpha, = ax1.plot([], [], label=r'$\alpha$', color='red', lw=3)
ax1_pseudo_beta, = ax1.plot([], [], label=r'$\beta$', color='green', lw=3)
ax1_pseudo_d, = ax1.plot([], [], label=r'$d$', color=CONST_ORANGE, lw=3)
ax1_pseudo_q, = ax1.plot([], [], label=r'$q$', color=CONST_PURPLE, lw=3)
ax1.legend(handles=[ax1_pseudo_harmonic,
ax1_pseudo_alpha, ax1_pseudo_beta,
ax1_pseudo_d, ax1_pseudo_q],
fontsize=11,
loc = 'lower left', shadow=True, fancybox=True,
bbox_to_anchor=(-0.36, -0.01))
# to be animated, info strings
str_time = ''
str_harmonic_theta = ''
str_pll_theta = ''
ax1_text_info = ax1.text(-2.1, -2.8, '',
fontsize=15,
bbox=dict(facecolor='white', edgecolor='white'))
# to be animated, pll locked/not locked on text
ax1_text_pll_locked = ax1.text(0, 2.7, '',
fontsize=12, color='red', ha='center', va='top',
bbox=dict(facecolor='white', edgecolor='white'))
# to be animated, ellipse
ax1_ellipse, = ax1.plot([], [],
color='blue', linewidth=3)
# to be animated, d axis
# I am actually combining an arrow and a line here.
# Because I found that the annotation arrow cannot go to the negative in
# saved videos.
# It can be displayedfine in animation, but for some reason,
# not in saved videos.
# Therefore, one annotation arrow + one line then.
ax1_d_ax_pos = ax1.annotate('',
xy=(0,0), xytext=(0,0),
arrowprops=dict(shrink=0,
fc='slategrey', ec='slategrey',
lw=0.1))
ax1_d_ax_neg, = ax1.plot([], [], '', color='slategrey', lw=4)
# to be animated, d axis label
ax1_d_label = ax1.text(0, 0, '', fontsize=15, ha='center')
# to be animated, q axis
ax1_q_ax_pos = ax1.annotate('',
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc='slategrey', ec='slategrey',
lw=0.1))
ax1_q_ax_neg, = ax1.plot([], [], '', color='slategrey', lw=4)
# to be animated, q axis label
ax1_q_label = ax1.text(0, 0, '', fontsize=15, ha='center')
# to be animated, alpha arrow
ax1_alpha_arrow = ax1.annotate("",
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc='red', ec='red',
lw=0.1))
# to be animated, beta arrow
ax1_beta_arrow = ax1.annotate("",
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc='green', ec='green',
lw=0.1))
# to be animated, harmonic arrow
ax1_harmonic_arrow = ax1.annotate("",
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc='blue', ec='blue',
lw=0.1))
# to be animated, d arrow
ax1_d_vector_arrow = ax1.annotate("",
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc=CONST_ORANGE, ec=CONST_ORANGE,
lw=0.1))
# to be animated, q arrow
ax1_q_vector_arrow = ax1.annotate("",
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc=CONST_PURPLE, ec=CONST_PURPLE,
lw=0.1))
# to be animated, helping lines
ax1_help_line_alpha, = ax1.plot([], [],
color='black', linewidth=2, linestyle='--')
ax1_help_line_beta, = ax1.plot([], [],
color='black', linewidth=2, linestyle='--')
ax1_help_line_d, = ax1.plot([], [],
color='blue', linewidth=3, linestyle='--')
ax1_help_line_q, = ax1.plot([], [],
color='blue', linewidth=3, linestyle='--')
# -----------------------------------------------------------------------------
# </Coordinate 1, the rotating vectors>
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# <Coordinate 2, alpha vs time and beta vs time>
# -----------------------------------------------------------------------------
# static, set coordinate
ax2 = plt.subplot(2, 2, 2)
# set x axis limits, 0 to the base period
ax2.set_xlim([0, dbl_base_period])
# set y axis limits
# find the maximum of all data points and add 0.05 to it
ylim_max = (max(max(alpha_vector), max(beta_vector), max(d_vector), max(q_vector))
+ 0.05)
ylim_min = -1 * ylim_max
ax2.set_ylim([ylim_min, ylim_max])
# static, horizontal middle line
ax2.axhline(y=0, color='k', lw=3)
# static, grid lines
ax2.grid(True)
# to be animated, period helping lines (auxiliary lines) and text
ax2_period_lines = []
ax2_period_text = []
# <for-loop, make 100 ax2 helping lines>
# 100 because I need to dynamically update them and 100 should be more than needed
# dynamic update mainly due to the refreshing the animate according to new
# user config, not because animating itself
for item in np.arange(0, 100, 1):
temp_plot, = ax2.plot([], [], linestyle='--', linewidth=1.5, color='k')
ax2_period_lines.append(temp_plot)
temp_text = ax2.text(0, 0, '',
ha='right', va='top',
family='Arial', fontsize=dbl_font_size, fontweight='bold')
ax2_period_text.append(temp_text)
# </for-loop, make 100 ax2 helping lines>
# to be animated, alpha line
ax2_alpha_vs_time, = ax2.plot([], [], label = r'$\alpha$',
color='r', linewidth=3)
# to be animated, beta line
ax2_beta_vs_time, = ax2.plot([], [], label = r'$\beta$',
color='g', linewidth=3)
# to be animated, alpha helping line
ax2_alpha_help_line, = ax2.plot([], [],
color='black', linewidth=1.5, linestyle='--')
# to be animated, beta helping line
ax2_beta_help_line, = ax2.plot([], [],
color='black', linewidth=1.5, linestyle='--')
# static, set legend
ax2Legend = plt.legend(handles=[ax2_alpha_vs_time, ax2_beta_vs_time],
title=str_freq_clarke,
loc = 'upper right', shadow=True, fancybox=True,
bbox_to_anchor=(1.28, 1))
# set legend title font size
ax2Legend.get_title().set_fontsize('10')
# set legend text font size
plt.setp(plt.gca().get_legend().get_texts(), fontsize='10')
# -----------------------------------------------------------------------------
# </Coordinate 2, alpha vs time and beta vs time>
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# <Coordinate 3, d vs time and q vs time>
# -----------------------------------------------------------------------------
# the codes here are similar to Coordinate 2
# static, set coordinate
ax3 = plt.subplot(2, 2, 4)
ax3.set_xlim([0, dbl_base_period])
ax3.set_ylim([ylim_min, ylim_max])
# static, horizontal middle line
ax3.axhline(y=0, color='k', lw=3)
# static, grid lines
ax3.grid(True)
# to be animated, period helping lines and text
ax3_period_lines = []
ax3_period_text = []
for item in np.arange(0, 100, 1):
temp_plot, = ax3.plot([], [], linestyle='--', linewidth=1.5, color='k')
ax3_period_lines.append(temp_plot)
temp_text = ax3.text(0, 0, '',
ha='right', va='top', color='blue',
family='Arial', fontsize=dbl_font_size, fontweight='bold')
ax3_period_text.append(temp_text)
# to be animated, pll locked/not locked on text
ax3_text_pll_locked = ax3.text(0.01, -0.375, '',
fontsize=15, color='red', ha='center', va='top',
bbox=dict(facecolor='white', edgecolor='white'))
# to be animated, d line
ax3_d_vs_time, = ax3.plot([], [], label = r'$d$',
color=CONST_ORANGE, linewidth=3)
# to be animated, q line
ax3_q_vs_time, = ax3.plot([], [], label = r'$q$',
color=CONST_PURPLE, linewidth=3)
# static, set legend
ax3Legend = plt.legend(handles=[ax3_d_vs_time, ax3_q_vs_time],
title=str_freq_park,
loc = 'upper right', shadow=True, fancybox=True,
bbox_to_anchor=(1.28, 1))
ax3Legend.get_title().set_fontsize('9')
plt.setp(plt.gca().get_legend().get_texts(), fontsize='10')
# static, set x label
ax3.set_xlabel(r'Time (s)', fontweight='bold', fontsize=12, family='Arial')
# to be animated, d helping line
ax3_d_help_line, = ax3.plot([], [],
color='blue', linewidth=1.5, linestyle='--')
# to be animated, q helping line
ax3_q_help_line, = ax3.plot([], [],
color='blue', linewidth=1.5, linestyle='--')
# -----------------------------------------------------------------------------
# </Coordinate 3, d vs time and q vs time>
# -----------------------------------------------------------------------------
# =============================================================================
# </Main figure>
# =============================================================================
# =============================================================================
# <Function: initialisation for animation>
# =============================================================================
def init():
"""
.. _init :
matplotlib's documentation:
https://matplotlib.org/api/animation_api.html
This function initialise the animation.
Parameters
----------
None
Returns
-------
::
tuple(ax2_period_lines) : a tuple of a list of matplotlib plot object
tuple(ax2_period_text) : a tuple of a list of matplotlib plot object
tuple(ax3_period_lines) : a tuple of a list of matplotlib plot object
tuple(ax3_period_text) : a tuple of a list of matplotlib plot object
ax1_text_info : matplotlib text object
ax1_text_pll_locked : matplotlib text object
ax1_d_ax_pos : matplotlib annotatiton object
ax1_d_ax_neg : matplotlib annotatiton object
ax1_d_label : matplotlib text object
ax1_q_ax_pos : matplotlib annotatiton object
ax1_q_ax_neg : matplotlib annotatiton object
ax1_q_label : matplotlib text object
ax1_alpha_arrow : matplotlib annotatiton object
ax1_beta_arrow : matplotlib annotatiton object
ax1_harmonic_arrow : matplotlib annotatiton object
ax1_d_vector_arrow : matplotlib annotatiton object
ax1_q_vector_arrow : matplotlib annotatiton object
ax1_help_line_alpha : matplotlib plot object
ax1_help_line_beta : matplotlib plot object
ax1_help_line_d : matplotlib plot object
ax1_ellipse : matplotlib plot object
ax2_alpha_vs_time : matplotlib plot object
ax2_beta_vs_time : matplotlib plot object
ax2_alpha_help_line : matplotlib plot object
ax2_beta_help_line : matplotlib plot object
ax3_d_vs_time : matplotlib plot object
ax3_q_vs_time : matplotlib plot object
ax3_d_help_line : matplotlib plot object
ax3_q_help_line : matplotlib plot object
ax3_text_pll_locked : matplotlib text object
Examples
--------
.. code:: python
animation.FuncAnimation(locFig, animate, np.arange(0, locInt_samples, 1), interval=1/locInt_fps*1e3, blit=True, init_func=init)
"""
# ax1 info strings
ax1_text_info.set_text('')
ax1_text_pll_locked.set_text('')
# ax1 d axis
ax1_d_ax_pos.xy = (0, 0)
ax1_d_ax_pos.xytext = (0, 0)
ax1_d_ax_neg.set_xdata([])
ax1_d_ax_neg.set_ydata([])
# ax1 d axis label
ax1_d_label = ax1.text(0, 0, '')
# ax1 q axis
ax1_q_ax_pos = ax1.annotate('',
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc='slategrey',
ec='slategrey',
lw=0.1))
ax1_q_ax_neg.set_xdata([])
ax1_q_ax_neg.set_ydata([])
# ax1 q axis label
ax1_q_label = ax1.text(0, 0, '')
# ax1 arrows
ax1_alpha_arrow = ax1.annotate('',
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc='r', ec='r',
lw=0.1))
ax1_beta_arrow = ax1.annotate("",
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc='green', ec='green',
lw=0.1))
ax1_harmonic_arrow = ax1.annotate("",
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc='blue', ec='blue',
lw=0.1))
ax1_d_vector_arrow = ax1.annotate("",
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc=CONST_ORANGE, ec=CONST_ORANGE,
lw=0.1))
ax1_q_vector_arrow = ax1.annotate("",
xy=(0, 0), xytext=(0, 0),
arrowprops=dict(shrink=0,
fc=CONST_PURPLE, ec=CONST_PURPLE,
lw=0.1))
# ax1 helping lines
ax1_help_line_alpha.set_xdata([])
ax1_help_line_alpha.set_ydata([])
ax1_help_line_beta.set_xdata([])
ax1_help_line_beta.set_ydata([])
ax1_help_line_d.set_xdata([])
ax1_help_line_d.set_ydata([])
ax1_help_line_q.set_xdata([])
ax1_help_line_q.set_ydata([])
# for interharmonics
ax1_ellipse, = ax1.plot([], [],
color='pink', linewidth=3, linestyle=':')
# ax2 period helping lines
for item in ax2_period_lines:
item.set_xdata([])
item.set_ydata([])
# ax2 period text
for item in ax2_period_text:
item.set_text('')
# ax2 alpha and beta lines
ax2_alpha_vs_time.set_xdata([])
ax2_alpha_vs_time.set_ydata([])
ax2_beta_vs_time.set_xdata([])
ax2_beta_vs_time.set_ydata([])
# ax2 alpha and beta helping lines
ax2_alpha_help_line.set_xdata([])
ax2_alpha_help_line.set_ydata([])
ax2_beta_help_line.set_xdata([])
ax2_beta_help_line.set_ydata([])
# ax3 period helping lines
for item in ax3_period_lines:
item.set_xdata([])
item.set_ydata([])
# ax3 period text
for item in ax3_period_text:
item.set_text('')
# ax3 d and q lines
ax3_d_vs_time.set_xdata([])
ax3_d_vs_time.set_ydata([])
ax3_q_vs_time.set_xdata([])
ax3_q_vs_time.set_ydata([])
# ax3 d and q helping lines
ax3_d_help_line.set_xdata([])
ax3_d_help_line.set_ydata([])
ax3_q_help_line.set_xdata([])
ax3_q_help_line.set_ydata([])
ax3_text_pll_locked.set_text('')
# to return a list of objects for animation, you need "tuple"
return (tuple(ax2_period_lines)
+ tuple(ax2_period_text)
+ tuple(ax3_period_lines)
+ tuple(ax3_period_text)
+ (ax1_text_info, ax1_text_pll_locked,
ax1_d_ax_pos,
ax1_d_ax_neg, ax1_d_label,
ax1_q_ax_pos, ax1_q_ax_neg, ax1_q_label,
ax1_alpha_arrow, ax1_beta_arrow, ax1_harmonic_arrow,
ax1_d_vector_arrow, ax1_q_vector_arrow,
ax1_help_line_alpha, ax1_help_line_beta, ax1_help_line_d,
ax1_ellipse,
ax2_alpha_vs_time, ax2_beta_vs_time,
ax2_alpha_help_line, ax2_beta_help_line,
ax3_d_vs_time, ax3_q_vs_time,
ax3_d_help_line, ax3_q_help_line,
ax3_text_pll_locked))
# =============================================================================
# </Function: initialisation for animation>
# =============================================================================
# =============================================================================
# <Function: updates for animation>
# =============================================================================
def animate(item):
"""
.. _animate :
matplotlib's documentation:
https://matplotlib.org/api/animation_api.html
This function updates the animation.
Parameters
----------
item : int
This parameter is used as a frame number to update the animation.
Returns
-------
::
tuple(ax2_period_lines) : a tuple of a list of matplotlib plot object
tuple(ax2_period_text) : a tuple of a list of matplotlib plot object
tuple(ax3_period_lines) : a tuple of a list of matplotlib plot object
tuple(ax3_period_text) : a tuple of a list of matplotlib plot object
ax1_text_info : matplotlib text object
ax1_text_pll_locked : matplotlib text object
ax1_d_ax_pos : matplotlib annotatiton object
ax1_d_ax_neg : matplotlib annotatiton object
ax1_d_label : matplotlib text object
ax1_q_ax_pos : matplotlib annotatiton object
ax1_q_ax_neg : matplotlib annotatiton object
ax1_q_label : matplotlib text object
ax1_alpha_arrow : matplotlib annotatiton object
ax1_beta_arrow : matplotlib annotatiton object
ax1_harmonic_arrow : matplotlib annotatiton object
ax1_d_vector_arrow : matplotlib annotatiton object
ax1_q_vector_arrow : matplotlib annotatiton object
ax1_help_line_alpha : matplotlib plot object
ax1_help_line_beta : matplotlib plot object
ax1_help_line_d : matplotlib plot object
ax1_ellipse : matplotlib plot object
ax2_alpha_vs_time : matplotlib plot object
ax2_beta_vs_time : matplotlib plot object
ax2_alpha_help_line : matplotlib plot object
ax2_beta_help_line : matplotlib plot object
ax3_d_vs_time : matplotlib plot object
ax3_q_vs_time : matplotlib plot object
ax3_d_help_line : matplotlib plot object
ax3_q_help_line : matplotlib plot object
ax3_text_pll_locked : matplotlib text object
Examples
--------
.. code:: python
animation.FuncAnimation(locFig, animate, np.arange(0, locInt_samples, 1), interval=1/locInt_fps*1e3, blit=True, init_func=init)
"""
# update ax1 info strings
str_time = r'$t = {:0.5f}'.format(time[item]) + '\ s$'
str_harmonic_theta = (r'$\theta_{Harmonic} = $'
+ ('${:0.3f}'
.format(2 * 180
* dbl_harmonic_order
* dbl_base_freq
* time[item]))
+ '^{\circ}$')
str_pll_theta = (r'$\theta_{PLL} = $'
+ ('${:0.3f}'
.format(2 * 180
* dbl_pll_order
* dbl_base_freq
* time[item]))
+ '^{\circ}$')
ax1_text_info.set_text(str_time
+ '\n' + str_harmonic_theta
+ '\n' + str_pll_theta)
# update ax1 d axis
ax1_d_ax_pos.xy = (2 * d_ax_on_x[item], 2 * d_ax_on_y[item])
ax1_d_ax_neg.set_xdata([0, -2 * d_ax_on_x[item]])
ax1_d_ax_neg.set_ydata([0, -2 * d_ax_on_y[item]])
# update ax1 d axis label
ax1_d_label.set_x(2.1 * d_ax_on_x[item])
ax1_d_label.set_y(2.1 * d_ax_on_y[item])
if d_ax_on_y[item] > 0:
ax1_d_label.set_va('bottom')
elif d_ax_on_y[item] < 0:
ax1_d_label.set_va('top')
ax1_d_label.set_text('$d$')
# update ax1 q axis
ax1_q_ax_pos.xy=(2 * q_ax_on_x[item], 2 * q_ax_on_y[item])
ax1_q_ax_neg.set_xdata([0, -2 * q_ax_on_x[item]])
ax1_q_ax_neg.set_ydata([0, -2 * q_ax_on_y[item]])
# update ax1 q axis label
ax1_q_label.set_x(2.1 * q_ax_on_x[item])
ax1_q_label.set_y(2.1 * q_ax_on_y[item])