-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainGUI.cs
3218 lines (2612 loc) · 128 KB
/
mainGUI.cs
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
/*
* MultiWii Windows GUI by Andras Schaffer (EOSBandi)
* February 2012 V1.0 Beta
* 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 3 of the License, or
* any later version. see <http://www.gnu.org/licenses/>
*
* LogBrowser is based on ArduPlanner Mega code written by Michael Oborne http://www.diydrones.com
* Instrument controls are based on AvionicsInstrument Controls written by Guillaume CHOUTEAU http://www.codeproject.com/Articles/27411/C-Avionic-Instrument-Controls
* Video capture code is using Aforge.Net Framework http://www.aforgenet.com
* Graph parts are using ZedGraph control http://sourceforge.net/projects/zedgraph/
*
*/
using System;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using MultiWiiGUIControls;
using ZedGraph;
using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using GMap.NET.MapProviders;
using System.Globalization;
namespace MultiWiiWinGUI
{
public partial class mainGUI : Form
{
#region Common variables (properties)
const string sVersion = "2.1";
const string sVersionUrl = "http://mw-wingui.googlecode.com/svn/trunk/WinGui2/version.xml";
private string sVersionFromSVN;
private XDocument doc;
static string sOptionsConfigFilename = "optionsconfig";
const string sGuiSettingsFilename = "gui_settings.xml";
enum CopterType { Tri = 1, QuadP, QuadX, BI, Gimbal, Y6, Hex6, FlyWing, Y4, Hex6X, Octo8Coax, Octo8P, Octo8X };
string[] sSerialSpeeds = { "115200", "57600", "38400", "19200", "9600" };
string[] sRefreshSpeeds = { "20 Hz", "10 Hz", "5 Hz", "2 Hz", "1 Hz" };
int[] iRefreshIntervals = { 50, 100, 200, 500, 1000 };
const int rcLow = 1300;
const int rcMid = 1700;
const string sRelName = "2.1";
//PID values
static PID[] Pid;
static SerialPort serialPort;
static bool isConnected = false; //is port connected or not ?
static bool bSerialError = false;
static bool isPaused = false;
static int iRefreshDivider = 20; //This used to force slower refresh for certain parameters
static int iSelectedTabIndex = 0; //Contains the actually selected tab
static double xTimeStamp = 0;
static byte[] bSerialBuffer;
static int iCheckBoxItems = 0; //number of checkboxItems (readed from optionsconfig.xml
static int iPidItems = 0; //number if Pid items (const definition)
static mw_data_gui mw_gui;
static mw_settings mw_params;
static GUI_settings gui_settings;
static bool bOptions_needs_refresh = true;
static bool bRestartNeeded = false; //FC software version changed, must restart
static string[] option_names;
static string[] option_indicators;
static string[] option_desc;
static LineItem curve_acc_roll, curve_acc_pitch, curve_acc_z;
static LineItem curve_gyro_roll, curve_gyro_pitch, curve_gyro_yaw;
static LineItem curve_mag_roll, curve_mag_pitch, curve_mag_yaw;
static LineItem curve_alt, curve_head;
static LineItem curve_dbg1, curve_dbg2, curve_dbg3, curve_dbg4;
static RollingPointPairList list_acc_roll, list_acc_pitch, list_acc_z;
static RollingPointPairList list_gyro_roll, list_gyro_pitch, list_gyro_yaw;
static RollingPointPairList list_mag_roll, list_mag_pitch, list_mag_yaw;
static RollingPointPairList list_alt, list_head;
static RollingPointPairList list_dbg1, list_dbg2, list_dbg3, list_dbg4;
static Scale xScale;
CheckBoxEx[, ,] aux;
indicator_lamp[] indicators;
System.Windows.Forms.Label[] cb_labels;
System.Windows.Forms.Label[] aux_labels;
System.Windows.Forms.Label[,] lmh_labels;
CultureInfo culture = new CultureInfo("en-US");
XmlTextReader reader;
int z;
//For video capture
static bool bVideoRecording = false;
static bool bVideoConnected = false;
static VideoFileWriter vfwWriter;
static FilterInfoCollection videoDevices;
static VideoCaptureDevice videoSource;
static TimeSpan tsFrameTimeStamp;
static TimeSpan tsFrameRate;
static Pen drawPen;
static System.Drawing.SolidBrush drawBrush;
static System.Drawing.Font drawFont;
//For logging
StreamWriter wLogStream;
StreamWriter wKMLLogStream;
static bool bLogRunning = false;
static bool bKMLLogRunning = false;
static int GPS_lat_old, GPS_lon_old;
static bool GPSPresent = true;
//Map Overlays
GMapOverlay overlayCopterPosition;
GMapOverlay drawnpolygons;
static GMapOverlay routes;// static so can update from gcs
GMapOverlay markers;
GMapOverlay polygons;
GMapOverlay positions;
static GMapProvider[] mapProviders;
static PointLatLng copterPos = new PointLatLng(47.402489, 19.071558); //Just the corrds of my flying place
static bool isMouseDown = false;
static bool isMouseDraging = false;
static bool bPosholdRecorded = false;
static bool bHomeRecorded = false;
// marker
GMapMarker currentMarker;
GMapMarkerRect CurentRectMarker = null;
GMapMarker center = new GMapMarkerCross(new PointLatLng(0.0, 0.0));
GMapPolygon drawnpolygon;
GMapPolygon polygon;
// layers
static GMapRoute Grout;
List<PointLatLng> points = new List<PointLatLng>();
GMapMarkerCross copterPosMarker;
PointLatLng GPS_pos;
PointLatLng end;
PointLatLng start;
//Commands
const int MSP_IDENT = 100;
const int MSP_STATUS = 101;
const int MSP_RAW_IMU = 102;
const int MSP_SERVO = 103;
const int MSP_MOTOR = 104;
const int MSP_RC = 105;
const int MSP_RAW_GPS = 106;
const int MSP_COMP_GPS = 107;
const int MSP_ATTITUDE = 108;
const int MSP_ALTITUDE = 109;
const int MSP_BAT = 110;
const int MSP_RC_TUNING = 111;
const int MSP_PID = 112;
const int MSP_BOX = 113;
const int MSP_MISC = 114;
const int MSP_MOTOR_PINS = 115;
const int MSP_BOXNAMES = 116;
const int MSP_PIDNAMES = 117;
const int MSP_WP = 118;
const int MSP_SET_RAW_RC = 200;
const int MSP_SET_RAW_GPS = 201;
const int MSP_SET_PID = 202;
const int MSP_SET_BOX = 203;
const int MSP_SET_RC_TUNING = 204;
const int MSP_ACC_CALIBRATION = 205;
const int MSP_MAG_CALIBRATION = 206;
const int MSP_SET_MISC = 207;
const int MSP_RESET_CONF = 208;
const int MSP_SET_WP = 209;
const int MSP_EEPROM_WRITE = 250;
const int MSP_DEBUG = 254;
const byte IDLE = 0;
const byte HEADER_START = 1;
const byte HEADER_M = 2;
const byte HEADER_ARROW = 3;
const byte HEADER_SIZE = 4;
const byte HEADER_CMD = 5;
const byte HEADER_ERR = 6;
static byte[] inBuf;
static int AUX_CHANNELS = 4;
static byte c_state = IDLE;
static Boolean err_rcvd = false;
static byte offset = 0;
static byte dataSize = 0;
static byte checksum = 0;
static byte cmd;
static int serial_error_count = 0;
static int serial_packet_count = 0;
#endregion
public mainGUI()
{
InitializeComponent();
#region map_setup
// config map
MainMap.MinZoom = 1;
MainMap.MaxZoom = 20;
MainMap.CacheLocation = Path.GetDirectoryName(Application.ExecutablePath) + "/mapcache/";
mapProviders = new GMapProvider[6];
mapProviders[0] = GMapProviders.BingHybridMap;
mapProviders[1] = GMapProviders.BingSatelliteMap;
mapProviders[2] = GMapProviders.GoogleHybridMap;
mapProviders[3] = GMapProviders.GoogleSatelliteMap;
mapProviders[4] = GMapProviders.OviHybridMap;
mapProviders[5] = GMapProviders.OviSatelliteMap;
for (int i = 0; i < 6; i++)
{
cbMapProviders.Items.Add(mapProviders[i]);
}
// map events
MainMap.OnPositionChanged += new PositionChanged(MainMap_OnCurrentPositionChanged);
//MainMap.OnTileLoadStart += new TileLoadStart(MainMap_OnTileLoadStart);
//MainMap.OnTileLoadComplete += new TileLoadComplete(MainMap_OnTileLoadComplete);
//MainMap.OnMarkerClick += new MarkerClick(MainMap_OnMarkerClick);
MainMap.OnMapZoomChanged += new MapZoomChanged(MainMap_OnMapZoomChanged);
//MainMap.OnMapTypeChanged += new MapTypeChanged(MainMap_OnMapTypeChanged);
MainMap.MouseMove += new MouseEventHandler(MainMap_MouseMove);
MainMap.MouseDown += new MouseEventHandler(MainMap_MouseDown);
MainMap.MouseUp += new MouseEventHandler(MainMap_MouseUp);
MainMap.OnMarkerEnter += new MarkerEnter(MainMap_OnMarkerEnter);
MainMap.OnMarkerLeave += new MarkerLeave(MainMap_OnMarkerLeave);
currentMarker = new GMapMarkerGoogleRed(MainMap.Position);
MainMap.MapScaleInfoEnabled = true;
MainMap.ForceDoubleBuffer = true;
MainMap.Manager.Mode = AccessMode.ServerAndCache;
MainMap.Position = copterPos;
Pen penRoute = new Pen(Color.Yellow, 3);
Pen penScale = new Pen(Color.Blue, 3);
MainMap.ScalePen = penScale;
routes = new GMapOverlay(MainMap, "routes");
MainMap.Overlays.Add(routes);
drawnpolygons = new GMapOverlay(MainMap, "drawnpolygons");
MainMap.Overlays.Add(drawnpolygons);
markers = new GMapOverlay(MainMap, "objects");
MainMap.Overlays.Add(markers);
polygons = new GMapOverlay(MainMap, "polygons");
MainMap.Overlays.Add(polygons);
positions = new GMapOverlay(MainMap, "positions");
MainMap.Overlays.Add(positions);
positions.Markers.Clear();
positions.Markers.Add(new GMapMarkerQuad(copterPos, 0, 0, 0));
Grout = new GMapRoute(points, "track");
Grout.Stroke = penRoute;
routes.Routes.Add(Grout);
center = new GMapMarkerCross(MainMap.Position);
#endregion
}
private void create_RC_Checkboxes(string[] names)
{
//Build indicator lamps array
indicators = new indicator_lamp[iCheckBoxItems];
int row = 0; int col = 0;
int startx = 800; int starty = 3;
for (int i = 0; i < iCheckBoxItems; i++)
{
indicators[i] = new indicator_lamp();
indicators[i].Location = new Point(startx + col * 52, starty + row * 19);
indicators[i].Visible = true;
indicators[i].Text = names[i];
indicators[i].indicator_color = 1;
indicators[i].Anchor = AnchorStyles.Right;
this.splitContainer2.Panel2.Controls.Add(indicators[i]);
col++;
if (col == 3) { col = 0; row++; }
}
//Build the RC control checkboxes structure
aux = new CheckBoxEx[4, 4, iCheckBoxItems];
startx = 200;
starty = 60;
int a, b, c;
for (c = 0; c < 4; c++)
{
for (a = 0; a < 3; a++)
{
for (b = 0; b < iCheckBoxItems; b++)
{
aux[c, a, b] = new CheckBoxEx();
aux[c, a, b].Location = new Point(startx + a * 18 + c * 70, starty + b * 25);
aux[c, a, b].Visible = true;
aux[c, a, b].Text = "";
aux[c, a, b].AutoSize = true;
aux[c, a, b].Size = new Size(16, 16);
aux[c, a, b].UseVisualStyleBackColor = true;
aux[c, a, b].CheckedChanged += new System.EventHandler(this.aux_checked_changed_event);
//Set info on the given checkbox position
aux[c, a, b].aux = c; //Which aux channel
aux[c, a, b].rclevel = a; //which rc level
aux[c, a, b].item = b; //Which item
this.tabPageRC.Controls.Add(aux[c, a, b]);
}
}
}
aux_labels = new System.Windows.Forms.Label[4];
lmh_labels = new System.Windows.Forms.Label[4, 3]; // aux1-4, L,M,H
string strlmh = "LMH";
for (a = 0; a < 4; a++)
{
aux_labels[a] = new System.Windows.Forms.Label();
aux_labels[a].Text = "AUX" + String.Format("{0:0}", a + 1);
aux_labels[a].Location = new Point(startx + a * 70 + 8, starty - 35);
aux_labels[a].AutoSize = true;
aux_labels[a].ForeColor = Color.White;
this.tabPageRC.Controls.Add(aux_labels[a]);
for (b = 0; b < 3; b++)
{
lmh_labels[a, b] = new System.Windows.Forms.Label();
lmh_labels[a, b].Text = strlmh.Substring(b, 1); ;
lmh_labels[a, b].Location = new Point(startx + a * 70 + b * 18, starty - 20);
lmh_labels[a, b].AutoSize = true;
lmh_labels[a, b].ForeColor = Color.White;
this.tabPageRC.Controls.Add(lmh_labels[a, b]);
}
}
cb_labels = new System.Windows.Forms.Label[20];
for (z = 0; z < iCheckBoxItems; z++)
{
cb_labels[z] = new System.Windows.Forms.Label();
cb_labels[z].Text = names[z];
cb_labels[z].Location = new Point(10, starty + z * 25);
cb_labels[z].Visible = true;
cb_labels[z].AutoSize = true;
cb_labels[z].ForeColor = Color.White;
cb_labels[z].TextAlign = ContentAlignment.MiddleRight;
this.tabPageRC.Controls.Add(cb_labels[z]);
}
}
private void delete_RC_Checkboxes()
{
int a, b, c;
if (aux != null)
{
for (c = 0; c < 4; c++)
{
for (a = 0; a < 3; a++)
{
for (b = 0; b < iCheckBoxItems; b++)
{
this.tabPageRC.Controls.Remove(aux[c, a, b]);
aux[c, a, b].CheckedChanged -= new System.EventHandler(this.aux_checked_changed_event);
}
}
}
for (int i = 0; i < iCheckBoxItems; i++)
{
this.tabPageRC.Controls.Remove(cb_labels[i]);
this.splitContainer2.Panel2.Controls.Remove(indicators[i]);
}
}
}
private void mainGUI_Load(object sender, EventArgs e)
{
//First step, check it gui_settings file is exists or not, if not then start settings wizard
if (!File.Exists(sGuiSettingsFilename))
{
setup_wizard panelSetupWizard = new setup_wizard();
panelSetupWizard.ShowDialog();
}
//Now there must be a valid settings file, so we can continue with normal execution
splash_screen splash = new splash_screen();
splash.sVersionLabel = sVersion;
splash.Show();
splash.Refresh();
//Start with Settings file read, and parse exit if unsuccessfull
gui_settings = new GUI_settings();
if (!gui_settings.read_from_xml(sGuiSettingsFilename))
{
Environment.Exit(-1);
}
sOptionsConfigFilename = sOptionsConfigFilename + gui_settings.iSoftwareVersion + ".xml";
read_options_config(); //read and parse optionsconfig.xml file. sets iCheckBoxItems
mw_gui = new mw_data_gui(iPidItems, iCheckBoxItems, gui_settings.iSoftwareVersion);
mw_params = new mw_settings(iPidItems, iCheckBoxItems, gui_settings.iSoftwareVersion);
splash.sFcVersionLabel = "MultiWii version " + sRelName;
splash.sStatus = "Connecting to MAP server...";
splash.Refresh();
//Quick hack to get pid names to mw_params untill redo the structures
for (int i = 0; i < iPidItems; i++)
{
mw_params.pidnames[i] = Pid[i].name;
}
cbMapProviders.SelectedIndex = gui_settings.iMapProviderSelectedIndex;
MainMap.MapProvider = mapProviders[gui_settings.iMapProviderSelectedIndex];
tb_mapzoom.Value = MainMap.MaxZoom;
MainMap.Zoom = MainMap.MaxZoom;
splash.sStatus = "Building up GUI elements...";
splash.Refresh();
bSerialBuffer = new byte[65];
inBuf = new byte[300]; //init input buffer
ToolTip toolTip1 = new ToolTip();
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
toolTip1.ShowAlways = true;
//rcOptions1 = new byte[iCheckBoxItems];
//rcOptions2 = new byte[iCheckBoxItems];
//Fill out settings tab
l_Capture_folder.Text = gui_settings.sCaptureFolder;
l_LogFolder.Text = gui_settings.sLogFolder;
l_Settings_folder.Text = gui_settings.sSettingsFolder;
cb_Logging_enabled.Checked = gui_settings.bEnableLogging;
//Set log enties checkboxes
cb_Log1.Checked = gui_settings.logGraw;
cb_Log2.Checked = gui_settings.logGatt;
cb_Log3.Checked = gui_settings.logGmag;
cb_Log4.Checked = gui_settings.logGrcc;
cb_Log5.Checked = gui_settings.logGrcx;
cb_Log6.Checked = gui_settings.logGmot;
cb_Log7.Checked = gui_settings.logGsrv;
cb_Log8.Checked = gui_settings.logGnav;
cb_Log9.Checked = gui_settings.logGpar;
cb_Log10.Checked = gui_settings.logGdbg;
//Build PID control structure based on the Pid structure.
const int iLineSpace = 36;
const int iRow1 = 30;
const int iRow2 = 125;
const int iRow3 = 220;
const int iTopY = 25;
Font fontField = new Font("Tahoma", 9, FontStyle.Bold);
Size fieldSize = new Size(70, 25);
for (int i = 0; i < iPidItems; i++)
{
Pid[i].pidLabel = new System.Windows.Forms.Label();
Pid[i].pidLabel.Text = Pid[i].name;
Pid[i].pidLabel.Location = new Point(iRow1, 10 + i * iLineSpace);
Pid[i].pidLabel.Visible = true;
Pid[i].pidLabel.AutoSize = true;
Pid[i].pidLabel.ForeColor = Color.White;
Pid[i].pidLabel.TextAlign = ContentAlignment.MiddleRight;
toolTip1.SetToolTip(Pid[i].pidLabel, Pid[i].description);
this.tabPagePID.Controls.Add(Pid[i].pidLabel);
if (Pid[i].Pshown)
{
Pid[i].Pfield = new System.Windows.Forms.NumericUpDown();
Pid[i].Pfield.ValueChanged += new EventHandler(pfield_valuechange);
Pid[i].Pfield.Location = new Point(iRow1, iTopY + i * iLineSpace);
Pid[i].Pfield.Size = fieldSize;
Pid[i].Pfield.Font = fontField;
Pid[i].Pfield.BorderStyle = BorderStyle.None;
Pid[i].Pfield.Maximum = Pid[i].Pmax;
Pid[i].Pfield.Minimum = Pid[i].Pmin;
Pid[i].Pfield.DecimalPlaces = decimals(Pid[i].Pprec);
Pid[i].Pfield.Increment = 1 / (decimal)Pid[i].Pprec;
this.tabPagePID.Controls.Add(Pid[i].Pfield);
Pid[i].Plabel = new System.Windows.Forms.Label();
Pid[i].Plabel.Text = "P";
Pid[i].Plabel.Font = fontField;
Pid[i].Plabel.ForeColor = Color.White;
Pid[i].Plabel.Location = new Point(iRow1 - 20, iTopY + i * iLineSpace);
this.tabPagePID.Controls.Add(Pid[i].Plabel);
}
if (Pid[i].Ishown)
{
Pid[i].Ifield = new System.Windows.Forms.NumericUpDown();
Pid[i].Ifield.ValueChanged += new EventHandler(ifield_valuechange);
Pid[i].Ifield.Location = new Point(iRow2, iTopY + i * iLineSpace);
Pid[i].Ifield.Size = fieldSize;
Pid[i].Ifield.Font = fontField;
Pid[i].Ifield.BorderStyle = BorderStyle.None;
Pid[i].Ifield.Maximum = Pid[i].Imax;
Pid[i].Ifield.Minimum = Pid[i].Imin;
Pid[i].Ifield.DecimalPlaces = decimals(Pid[i].Iprec);
Pid[i].Ifield.Increment = 1 / (decimal)Pid[i].Iprec;
this.tabPagePID.Controls.Add(Pid[i].Ifield);
Pid[i].Ilabel = new System.Windows.Forms.Label();
Pid[i].Ilabel.Text = "I";
Pid[i].Ilabel.Font = fontField;
Pid[i].Ilabel.ForeColor = Color.White;
Pid[i].Ilabel.Location = new Point(iRow2 - 20, iTopY + i * iLineSpace);
this.tabPagePID.Controls.Add(Pid[i].Ilabel);
}
if (Pid[i].Dshown)
{
Pid[i].Dfield = new System.Windows.Forms.NumericUpDown();
Pid[i].Dfield.ValueChanged += new EventHandler(dfield_valuechange);
Pid[i].Dfield.Location = new Point(iRow3, iTopY + i * iLineSpace);
Pid[i].Dfield.Size = fieldSize;
Pid[i].Dfield.Font = fontField;
Pid[i].Dfield.BorderStyle = BorderStyle.None;
Pid[i].Dfield.Maximum = Pid[i].Dmax;
Pid[i].Dfield.Minimum = Pid[i].Dmin;
Pid[i].Dfield.DecimalPlaces = decimals(Pid[i].Dprec);
Pid[i].Dfield.Increment = 1 / (decimal)Pid[i].Dprec;
this.tabPagePID.Controls.Add(Pid[i].Dfield);
Pid[i].Dlabel = new System.Windows.Forms.Label();
Pid[i].Dlabel.Text = "D";
Pid[i].Dlabel.Font = fontField;
Pid[i].Dlabel.ForeColor = Color.White;
Pid[i].Dlabel.Location = new Point(iRow3 - 20, iTopY + i * iLineSpace);
this.tabPagePID.Controls.Add(Pid[i].Dlabel);
}
}
toolTip1.SetToolTip(b_check_all_ACC, "Select all ACC values");
toolTip1.SetToolTip(b_uncheck_all_ACC, "Deselect all ACC values");
this.Refresh();
serial_ports_enumerate();
foreach (string speed in sSerialSpeeds)
{
cb_serial_speed.Items.Add(speed);
}
cb_serial_speed.SelectedItem = gui_settings.sPreferedSerialSpeed;
if (cb_serial_port.Items.Count == 0)
{
b_connect.Enabled = false; //Nos serial port, disable connect
}
//Init serial port object
serialPort = new SerialPort();
//Set up serial port parameters (at least the ones what we know upfront
serialPort.DataBits = 8;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.Handshake = Handshake.None;
serialPort.DtrEnable = false; //??
serialPort.ReadBufferSize = 4096; //4K byte of read buffer
serialPort.ReadTimeout = 500; // 500msec timeout;
//Init Realtime Monitor panel controls
foreach (string rate in sRefreshSpeeds)
{
cb_monitor_rate.Items.Add(rate);
}
cb_monitor_rate.SelectedIndex = 0; //20Hz is the default
//Setup timers
timer_realtime.Tick += new EventHandler(timer_realtime_Tick);
timer_realtime.Interval = iRefreshIntervals[cb_monitor_rate.SelectedIndex];
timer_realtime.Enabled = true;
timer_realtime.Stop();
//Set up zgMonitor control for real time monitoring
GraphPane myPane = zgMonitor.GraphPane;
// Set the titles and axis labels
myPane.Title.Text = "";
myPane.XAxis.Title.Text = "";
myPane.YAxis.Title.Text = "";
//Set up pointlists and curves
list_acc_roll = new RollingPointPairList(300);
curve_acc_roll = myPane.AddCurve("acc_roll", list_acc_roll, Color.Red, SymbolType.None);
list_acc_pitch = new RollingPointPairList(300);
curve_acc_pitch = myPane.AddCurve("acc_pitch", list_acc_pitch, Color.Green, SymbolType.None);
list_acc_z = new RollingPointPairList(300);
curve_acc_z = myPane.AddCurve("acc_z", list_acc_z, Color.Blue, SymbolType.None);
list_gyro_roll = new RollingPointPairList(300);
curve_gyro_roll = myPane.AddCurve("gyro_roll", list_gyro_roll, Color.Khaki, SymbolType.None);
list_gyro_pitch = new RollingPointPairList(300);
curve_gyro_pitch = myPane.AddCurve("gyro_pitch", list_gyro_pitch, Color.Cyan, SymbolType.None);
list_gyro_yaw = new RollingPointPairList(300);
curve_gyro_yaw = myPane.AddCurve("gyro_yaw", list_gyro_yaw, Color.Magenta, SymbolType.None);
list_mag_roll = new RollingPointPairList(300);
curve_mag_roll = myPane.AddCurve("mag_roll", list_mag_roll, Color.CadetBlue, SymbolType.None);
list_mag_pitch = new RollingPointPairList(300);
curve_mag_pitch = myPane.AddCurve("mag_pitch", list_mag_pitch, Color.MediumPurple, SymbolType.None);
list_mag_yaw = new RollingPointPairList(300);
curve_mag_yaw = myPane.AddCurve("mag_yaw", list_mag_yaw, Color.DarkGoldenrod, SymbolType.None);
list_alt = new RollingPointPairList(300);
curve_alt = myPane.AddCurve("alt", list_alt, Color.White, SymbolType.None);
list_head = new RollingPointPairList(300);
curve_head = myPane.AddCurve("head", list_head, Color.Orange, SymbolType.None);
list_dbg1 = new RollingPointPairList(300);
curve_dbg1 = myPane.AddCurve("dbg1", list_dbg1, Color.PaleTurquoise, SymbolType.None);
list_dbg2 = new RollingPointPairList(300);
curve_dbg2 = myPane.AddCurve("dbg2", list_dbg2, Color.PaleTurquoise, SymbolType.None);
list_dbg3 = new RollingPointPairList(300);
curve_dbg3 = myPane.AddCurve("dbg3", list_dbg3, Color.PaleTurquoise, SymbolType.None);
list_dbg4 = new RollingPointPairList(300);
curve_dbg4 = myPane.AddCurve("dbg4", list_dbg4, Color.PaleTurquoise, SymbolType.None);
// Show the x axis grid
myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.XAxis.Scale.IsVisible = false;
// Make the Y axis scale red
myPane.YAxis.Scale.FontSpec.FontColor = Color.White;
myPane.YAxis.Title.FontSpec.FontColor = Color.White;
// turn off the opposite tics so the Y tics don't show up on the Y2 axis
myPane.YAxis.MajorTic.IsOpposite = false;
myPane.YAxis.MinorTic.IsOpposite = false;
// Don't display the Y zero line
myPane.YAxis.MajorGrid.IsZeroLine = true;
// Align the Y axis labels so they are flush to the axis
myPane.YAxis.Scale.Align = AlignP.Inside;
myPane.YAxis.Scale.IsVisible = false;
// Manually set the axis range
myPane.YAxis.Scale.Min = -150;
myPane.YAxis.Scale.Max = 150;
myPane.Chart.Fill = new Fill(Color.DimGray, Color.DarkGray, 45.0f);
myPane.Fill = new Fill(Color.DimGray, Color.DimGray, 45.0f);
myPane.Legend.IsVisible = false;
myPane.XAxis.Scale.IsVisible = false;
myPane.YAxis.Scale.IsVisible = true;
myPane.XAxis.Scale.MagAuto = true;
myPane.YAxis.Scale.MagAuto = false;
zgMonitor.IsEnableHPan = true;
zgMonitor.IsEnableHZoom = true;
foreach (ZedGraph.LineItem li in myPane.CurveList)
{
li.Line.Width = 1;
}
myPane.YAxis.Title.FontSpec.FontColor = Color.White;
myPane.XAxis.Title.FontSpec.FontColor = Color.White;
myPane.XAxis.Scale.Min = 0;
myPane.XAxis.Scale.Max = 300;
myPane.XAxis.Type = AxisType.Linear;
zgMonitor.ScrollGrace = 0;
xScale = zgMonitor.GraphPane.XAxis.Scale;
zgMonitor.AxisChange();
//Init video capture dev
try
{
// enumerate video devices
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
throw new ApplicationException();
// add all devices to combo
foreach (FilterInfo device in videoDevices)
{
dropdown_devices.Items.Add(device.Name);
}
}
catch (ApplicationException)
{
dropdown_devices.Items.Add("No local capture devices");
dropdown_devices.Enabled = false;
b_video_connect.Enabled = false;
}
dropdown_devices.SelectedIndex = 0;
cb_codec.SelectedIndex = 0;
//Drawing stuff for OSD
drawPen = new Pen(Color.White, 1);
drawFont = new System.Drawing.Font(FontFamily.GenericMonospace, 16.0F);
drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White);
//Disable buttons that are not working till connected
b_reset.Enabled = false;
b_cal_acc.Enabled = false;
b_cal_mag.Enabled = false;
b_read_settings.Enabled = false;
b_write_settings.Enabled = false;
//System.Threading.Thread.Sleep(2000);
splash.Close();
}
private void timer_realtime_Tick(object sender, EventArgs e)
{
if (serialPort.BytesToRead == 0)
{
if ((iRefreshDivider % gui_settings.MSP_STATUS_rate_divider) == 0) MSPquery(MSP_STATUS);
if ((iRefreshDivider % gui_settings.MSP_RAW_IMU_rate_divider) == 0) MSPquery(MSP_RAW_IMU);
if ((iRefreshDivider % gui_settings.MSP_SERVO_rate_divider) == 0) MSPquery(MSP_SERVO);
if ((iRefreshDivider % gui_settings.MSP_MOTOR_rate_divider) == 0) MSPquery(MSP_MOTOR);
if ((iRefreshDivider % gui_settings.MSP_RAW_GPS_rate_divider) == 0) MSPquery(MSP_RAW_GPS);
if ((iRefreshDivider % gui_settings.MSP_COMP_GPS_rate_divider) == 0) MSPquery(MSP_COMP_GPS);
if ((iRefreshDivider % gui_settings.MSP_ATTITUDE_rate_divider) == 0) MSPquery(MSP_ATTITUDE);
if ((iRefreshDivider % gui_settings.MSP_ALTITUDE_rate_divider) == 0) MSPquery(MSP_ALTITUDE);
if ((iRefreshDivider % gui_settings.MSP_BAT_rate_divider) == 0) MSPquery(MSP_BAT);
if ((iRefreshDivider % gui_settings.MSP_RC_rate_divider) == 0) MSPquery(MSP_RC);
if ((iRefreshDivider % gui_settings.MSP_MISC_rate_divider) == 0) MSPquery(MSP_MISC);
if ((iRefreshDivider % gui_settings.MSP_DEBUG_rate_divider) == 0) MSPquery(MSP_DEBUG);
if ((mw_gui.mode & (1 << 5)) > 0)
{ //armed
if ((iRefreshDivider % 20) == 0) MSPqueryWP(0); //get home position
}
else { mw_gui.GPS_home_lon = 0; mw_gui.GPS_home_lat = 0; bHomeRecorded = false; }
if ((mw_gui.mode & (1 << 7)) > 0)
{ //poshold
if ((iRefreshDivider % 20) == 0) MSPqueryWP(16); //get hold position
}
else { mw_gui.GPS_poshold_lon = 0; mw_gui.GPS_poshold_lat = 0; bPosholdRecorded = false; }
}
update_gui();
iRefreshDivider--;
if (iRefreshDivider == 0) iRefreshDivider = 20; //reset
}
private void b_connect_Click(object sender, EventArgs e)
{
//Check if we at GUI Settings, go to first screen when connect
if (tabMain.SelectedIndex == 4) { tabMain.SelectedIndex = 0; }
if (serialPort.IsOpen) //Disconnect
{
delete_RC_Checkboxes();
b_connect.Text = "Connect";
b_connect.Image = Properties.Resources.connect;
isConnected = false;
timer_realtime.Stop(); //Stop timer(s), whatever it takes
//timer_rc.Stop();
bkgWorker.CancelAsync();
System.Threading.Thread.Sleep(500); //Wait bkworker to finish
serialPort.Close();
if (bLogRunning)
{
closeLog();
}
//Disable buttons that are not working here
b_reset.Enabled = false;
b_cal_acc.Enabled = false;
b_cal_mag.Enabled = false;
b_read_settings.Enabled = false;
b_write_settings.Enabled = false;
}
else //Connect
{
if (cb_serial_port.Text == "") { return; } //if no port selected then do nothin' at connect
//Assume that the selection in the combobox for port is still valid
serialPort.PortName = cb_serial_port.Text;
serialPort.BaudRate = int.Parse(cb_serial_speed.Text);
try
{
serialPort.Open();
}
catch
{
//WRONG, it seems that the combobox selection pointed to a port which is no longer available
MessageBoxEx.Show(this, "Please check that your USB cable is still connected.\r\nAfter you press OK, Serial ports will be re-enumerated", "Error opening COM port", MessageBoxButtons.OK, MessageBoxIcon.Error);
serial_ports_enumerate();
return; //Exit without connecting;
}
//Set button text and status
b_connect.Text = "Disconnect";
b_connect.Image = Properties.Resources.disconnect;
isConnected = true;
//Open Log file if it is enabled
if (gui_settings.bEnableLogging)
{
openLog();
}
serial_packet_count = 0;
serial_error_count = 0;
//Enable buttons that are not working here
b_reset.Enabled = true;
b_cal_acc.Enabled = true;
b_cal_mag.Enabled = true;
b_read_settings.Enabled = true;
b_write_settings.Enabled = true;
//We have to do it for a couple of times to ensure that we will have parameters loaded
for (int i = 0; i < 10; i++)
{
MSPquery(MSP_PID);
MSPquery(MSP_RC_TUNING);
MSPquery(MSP_IDENT);
MSPquery(MSP_BOX);
MSPquery(MSP_BOXNAMES);
MSPquery(MSP_MISC);
}
//Run BackgroundWorker
if (!bkgWorker.IsBusy) { bkgWorker.RunWorkerAsync(); }
//if (tabMain.SelectedIndex == 2 && !isPaused) timer_realtime.Start(); //If we are standing at the monitor page, start timer
//if (tabMain.SelectedIndex == 1 && !isPausedRC) timer_rc.Start(); //And start it if we stays on rc settings page
//if (tabMain.SelectedIndex == 3 && !isPausedGPS) timer_GPS.Start();
System.Threading.Thread.Sleep(1000);
int x = 0;
while (mw_gui.bUpdateBoxNames == false)
{
x++;
System.Threading.Thread.Sleep(1);
if (x > 1000)
{
MessageBoxEx.Show(this, "Please check if you have selected the right com port", "Error device not responding", MessageBoxButtons.OK, MessageBoxIcon.Error);
b_connect.Text = "Connect";
b_connect.Image = Properties.Resources.connect;
isConnected = false;
timer_realtime.Stop(); //Stop timer(s), whatever it takes
//timer_rc.Stop();
bkgWorker.CancelAsync();
System.Threading.Thread.Sleep(500); //Wait bkworker to finish
serialPort.Close();
if (bLogRunning)
{
closeLog();
}
return;
}
}
timer_realtime.Start();
bOptions_needs_refresh = true;
create_RC_Checkboxes(mw_gui.sBoxNames);
update_gui();
}
}
private void cb_monitor_rate_SelectedIndexChanged(object sender, EventArgs e)
{
//Change refresh rate
timer_realtime.Interval = iRefreshIntervals[cb_monitor_rate.SelectedIndex];
}