在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → AHRS(Mini4Rotors)

AHRS(Mini4Rotors)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:8.08M
  • 下载次数:15
  • 浏览次数:266
  • 发布时间:2019-05-08
  • 实例类别:C#语言基础
  • 发 布 人:mark1232019
  • 文件格式:.zip
  • 所需积分:2
 相关标签: AHRS

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】


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
/*****************************************************************************/
/* Project  : AvionicsInstrumentControlDemo                                  */
/* File     : DemoWondow.cs                                                  */
/* Version  : 1                                                              */
/* Language : C#                                                             */
/* Summary  : Start window of the project, use to test the instruments       */
/* Creation : 30/06/2008                                                     */
/* Autor    : Guillaume CHOUTEAU                                             */
/* History  :                                                                */
/*****************************************************************************/
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Runtime.InteropServices;
 
 
namespace AvionicsInstrumentControlDemo
{
    public partial class DemoWinow : Form
    {
        #region /*********************变量定义****************************/
        private Form_3Dcuboid form_3Dcuboid;
        private SerialPort Serial_Port = new SerialPort();
        private bool closing = false;
        private bool listening = false;
        private Color[] MsgTypeColor = new Color[] { Color.Blue, Color.Green, Color.Black, Color.Orange, Color.Red };
        private bool StopDisFlag = false;
        private long ReceiveCount = 0L;
        private long SendCount = 0L;
        private long FrameCount = 0L;
        private List<byte> Data_Buffer = new List<byte>(0x800);
        private byte[] Euler_Data = new byte[16];
        private byte[] Acc_Gyr_Data = new byte[23];
        private byte[] RC_Data = new byte[25];
        private byte[] Temp_Data = new byte[50];
        public Int16 Rol_P, Rol_I, Rol_D, Pit_P, Pit_I, Pit_D, Yaw_P, Yaw_I, Yaw_D;
        public Int16 S_acc_x, S_acc_y, S_acc_z, S_gyr_x, S_gyr_y, S_gyr_z, S_mag_x, S_mag_y, S_mag_z;
        public float D_ang_rol, D_ang_pit, D_ang_yaw, D_alt_csb, D_alt_prs;
        public Int16 Rc_thr, Rc_yaw, Rc_rol, Rc_pit, Rc_aux_1, Rc_aux_2, Rc_aux_3, Rc_aux_4, Rc_aux_5, Rc_aux_6;
        public Int16 Moto_1, Moto_2, Moto_3, Moto_4, Moto_5, Moto_6, Moto_7, Moto_8;
        string CMD_Acc = "AAAF0101015C", CMD_Gyro = "AAAF0101025D", CMD_Acc_Gyro = "AAAF0101035E", CMD_Mag = "", CMD_Pid = "AAAF0201015D", CMD_Offset = "AAAF0201025E";
        private float fly_roll;
        public bool B_armed = true;//FLY
        private bool GesShowFlag = false;
        private bool Pid_flag = false;
        private int xCount = 0;
        private bool ZedCollectFlag = false;
        private string s;
        private int t;
        #endregion
        #region /*******************写入数据文件**************************/
        //声明读写INI文件的API函数
        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
        static string FileName = Application.StartupPath   "\\Mini4RotorsSetUPConfig.ini";
        //写INI文件
        public void WriteIni(string Section, string Ident, string Value)
        {
            if (!WritePrivateProfileString(Section, Ident, Value, FileName))
            {
 
                throw (new ApplicationException("写入配置文件出错"));
            }
 
        }
        //读取INI文件指定
        public string ReadIni(string Section, string Ident, string Default)
        {
            Byte[] Buffer = new Byte[65535];
            int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
            //必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
            string s = Encoding.GetEncoding(0).GetString(Buffer);
            s = s.Substring(0, bufLen);
            return s.Trim();
        }
        private void GetIni()
        {
            roll_P.Value = int.Parse(ReadIni("Roll", "P", ""));
            roll_I.Value = int.Parse(ReadIni("Roll", "I", ""));
            roll_D.Value = int.Parse(ReadIni("Roll", "D", ""));
            pitch_P.Value=int.Parse(ReadIni("Pit", "P", ""));
            pitch_I.Value=int.Parse(ReadIni("Pit", "I", ""));
            pitch_D.Value=int.Parse(ReadIni("Pit", "D", ""));
            yaw_P.Value=int.Parse(ReadIni("Yaw", "P", ""));
            yaw_I.Value=int.Parse(ReadIni("Yaw", "I", ""));
            yaw_D.Value=int.Parse(ReadIni("Yaw", "D", ""));
        }
        private void SaveSetting()
        {
            WriteIni("Roll", "P", roll_P.Value.ToString());
            WriteIni("Roll", "I", roll_I.Value.ToString());
            WriteIni("Roll", "D", roll_D.Value.ToString());
            WriteIni("Pit", "P", pitch_P.Value.ToString());
            WriteIni("Pit", "I", pitch_I.Value.ToString());
            WriteIni("Pit", "D", pitch_D.Value.ToString());
            WriteIni("Yaw", "P", yaw_P.Value.ToString());
            WriteIni("Yaw", "I", yaw_I.Value.ToString());
            WriteIni("Yaw", "D", yaw_D.Value.ToString());
        }
        #endregion
        #region /*******************曲线变量定义**************************/
        public List<float> x1 = new List<float>();
        public List<float> y1 = new List<float>();
        public List<float> x2 = new List<float>();
        public List<float> y2 = new List<float>();
        public List<float> x3 = new List<float>();
        public List<float> y3 = new List<float>();
        public List<float> x4 = new List<float>();
        public List<float> y4 = new List<float>();
        public List<float> x5 = new List<float>();
        public List<float> y5 = new List<float>();
        public List<float> x6 = new List<float>();
        public List<float> y6 = new List<float>();
        #endregion
        public DemoWinow()
        {
            InitializeComponent();
        }
        private void trackBarVerticalSpeed_Scroll(object sender, EventArgs e)
        {
            verticalSpeedInstrumentControl1.SetVerticalSpeedIndicatorParameters(trackBarVerticalSpeed.Value);
        }
 
        private void trackPitchAngle_Scroll(object sender, EventArgs e)
        {
            horizonInstrumentControl1.SetAttitudeIndicatorParameters(trackPitchAngle.Value, trackBarRollAngle.Value);
        }
 
        private void trackBarRollAngle_Scroll(object sender, EventArgs e)
        {
            horizonInstrumentControl1.SetAttitudeIndicatorParameters(trackPitchAngle.Value, trackBarRollAngle.Value);
        }
 
        private void trackBarHeading_Scroll(object sender, EventArgs e)
        {
            headingIndicatorInstrumentControl1.SetHeadingIndicatorParameters(trackBarHeading.Value);
        }
        private void DemoWinow_Load(object sender, EventArgs e)
        {
            this.form_3Dcuboid = new Form_3Dcuboid();
            this.form_3Dcuboid.MinimizeInsteadOfClose = true;
            this.InitCtlVal();
            //SendData.Enabled = false;
            //this.ZGraph = new ZGraph();         
            this.trackBarHeading.Scroll  = new EventHandler(this.trackBar_Scroll);
            this.trackPitchAngle.Scroll  = new EventHandler(this.trackBar_Scroll);
            this.trackBarRollAngle.Scroll  = new EventHandler(this.trackBar_Scroll);
 
            this.Serial_Port.DataReceived  = new SerialDataReceivedEventHandler(this.SerialDataDis);
 
            StartShowCube.Enabled = false;
            StartCollect.Enabled = false;
 
            dlBaudRate.SelectedIndex = 11;
            dlDataByte.SelectedIndex = 0;
            dlParity.SelectedIndex = 0;
            dlStopByte.SelectedIndex = 0;
            dlControl.SelectedIndex = 0;
        }
        private void InitCtlVal()
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                dlPortName.Items.Add(port);
            }
            if (this.dlPortName.Items.Count > 0)
            {
                dlPortName.SelectedIndex = 0;
                dlBaudRate.SelectedIndex = 11;
                dlDataByte.SelectedIndex = 0;
                dlParity.SelectedIndex = 0;
                dlStopByte.SelectedIndex = 0;
                dlControl.SelectedIndex = 0;
            }
            else
            {
                this.AppStateDis(MsgType.Outgoing, "计算机没有串口");
            }
 
            this.SetCtlState();
        }
        private void SetCtlState()
        {
            this.SerialPortSet.Enabled = !this.Serial_Port.IsOpen;
           // this.OpenPort.Text = this.Serial_Port.IsOpen ? "&关闭串口" : "&打开串口";
        }
        private void trackBar_Scroll(object sender, EventArgs e)
        {
            form_3Dcuboid.Roll = this.trackBarRollAngle.Value;
            form_3Dcuboid.Pitch = this.trackPitchAngle.Value;
            form_3Dcuboid.Yaw = this.trackBarHeading.Value;
            this.horizonInstrumentControl1.SetAttitudeIndicatorParameters((double)this.form_3Dcuboid.Pitch, (double)this.form_3Dcuboid.Roll);
            //this.attitudeIndicatorInstrumentControl1.SetAttitudeIndicatorParameters((double)this.trackBar1.Value, (double)this.trackBar2.Value);
            
        }
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabPage3)
            {
 
                form_3Dcuboid.FormBorderStyle = FormBorderStyle.None;
                form_3Dcuboid.Visible = true;
                form_3Dcuboid.TopLevel = false;
                form_3Dcuboid.Parent = tabPage3;
                form_3Dcuboid.ControlBox = false;
                form_3Dcuboid.Dock = System.Windows.Forms.DockStyle.Fill;
                form_3Dcuboid.Show();
                
            }
        }
        private void DemoWinow_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.closing = true;
            while (this.listening)
            {
                Application.DoEvents();
            }
            this.Serial_Port.Close();
        }
        private void AppStateDis(MsgType msgtype, string msg)
        {
            this.AppState.ForeColor = this.MsgTypeColor[(int)msgtype];
            this.AppState.Text = msg;
        }
        public enum MsgType
        {
            Incoming,
            Outgoing,
            Normal,
            Warning,
            Error
        }
        private string ByteArrayToHexString(byte[] data)
        {
            StringBuilder builder = new StringBuilder(data.Length * 3);
            foreach (byte num in data)
            {
                builder.Append(Convert.ToString(num, 0x10).PadLeft(2, '0').PadRight(3, ' '));
            }
            return builder.ToString().ToUpper();
        }
        private byte[] HexStringToByteArray(string s)
        {
            s = s.Replace(" ", "");
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i  = 2)
            {
                buffer[i / 2] = Convert.ToByte(s.Substring(i, 2), 0x10);
            }
            return buffer;
        }
        private void SerialDataDis(object sender, SerialDataReceivedEventArgs e)
        {
            EventHandler method = null;
            if (!this.StopDisFlag && !this.closing)
            {
                try
                {
                    this.listening = true;//设置标记,说明我已经开始处理数据,一会儿要使用系统UI的。
                    int bytesToRead = this.Serial_Port.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
                    this.ReceiveCount  = bytesToRead;//增加接收计数
                    byte[] buffer = new byte[bytesToRead];//声明一个临时数组存储当前来的串口数据
                    this.Serial_Port.Read(buffer, 0, bytesToRead);//读取缓冲数据
                    bool flag = false;
                    this.Data_Buffer.AddRange(buffer); //1.缓存数据
                    //2.完整性判断
                    while (this.Data_Buffer.Count >= 5)//至少要包含头(2字节) FUN(1字节) 长度(1字节) 校验(1字节)
                    {
                        //2.1 查找数据头
                        if (Data_Buffer[0] == 0xAA && Data_Buffer[1] == 0xAA && Data_Buffer[3] < 51)
                        {
                            int len = this.Data_Buffer[3];//数据长度
                            int fun = this.Data_Buffer[2];
                            //数据完整判断第一步,长度是否足够
                            //len是数据段长度,4个字节是while行注释的3部分长度
                            if (Data_Buffer.Count < len   5) break;
                            //2.3 校验数据,确认数据正确
                            //异或校验,逐个字节异或得到校验码
                            byte checksum = 0;
                            for (int i = 0; i <= (len   3); i  )//checksum表示校验之前的求和
                            {
                                checksum  = Data_Buffer[i];
                            }
                            if (checksum == Data_Buffer[len   4]) //如果数据校验正确,继续分析FUN字节
                            {
                                this.Data_Buffer.CopyTo(0, Temp_Data, 0, len   5);//复制一条完整数据到具体的数据缓存 
                                this.Data_Buffer.RemoveRange(0, len   5);//正确分析一条数据,从缓存中移除数据。
                                switch (fun)
                                {
                                    case 1://欧拉角
                                        Euler_Data = Temp_Data;
                                        D_ang_rol = (float)((Int16)(Temp_Data[4] << 8 | Temp_Data[5])) / 100;
                                        D_ang_pit = (float)((Int16)(Temp_Data[6] << 8 | Temp_Data[7])) / 100;
                                        D_ang_yaw = (float)((Int16)(Temp_Data[8] << 8 | Temp_Data[9])) / 100;
                                        D_alt_csb = (Int16)(Temp_Data[10] << 8 | Temp_Data[11]);
                                        D_alt_prs = (Int32)(Temp_Data[12] << 24 | Temp_Data[13] << 16 | Temp_Data[14] << 8 | Temp_Data[15]);
                                        if (Temp_Data[16] == 0xa1)
                                            B_armed = false;
                                        else
                                            B_armed = true;
                                        break;
                                    case 2://传感器数据
                                        Acc_Gyr_Data = Temp_Data;
                                        S_acc_x = (Int16)(Temp_Data[4] << 8 | Temp_Data[5]);
                                        S_acc_y = (Int16)(Temp_Data[6] << 8 | Temp_Data[7]);
                                        S_acc_z = (Int16)(Temp_Data[8] << 8 | Temp_Data[9]);
                                        S_gyr_x = (Int16)(Temp_Data[10] << 8 | Temp_Data[11]);
                                        S_gyr_y = (Int16)(Temp_Data[12] << 8 | Temp_Data[13]);
                                        S_gyr_z = (Int16)(Temp_Data[14] << 8 | Temp_Data[15]);
                                        S_mag_x = (Int16)(Temp_Data[16] << 8 | Temp_Data[17]);
                                        S_mag_y = (Int16)(Temp_Data[18] << 8 | Temp_Data[19]);
                                        S_mag_z = (Int16)(Temp_Data[20] << 8 | Temp_Data[21]);
                                        break;
                                    case 3:
                                        RC_Data = Temp_Data;
                                        Rc_thr = (Int16)(Temp_Data[4] << 8 | Temp_Data[5]);
                                        Rc_yaw = (Int16)(Temp_Data[6] << 8 | Temp_Data[7]);
                                        Rc_rol = (Int16)(Temp_Data[8] << 8 | Temp_Data[9]);
                                        Rc_pit = (Int16)(Temp_Data[10] << 8 | Temp_Data[11]);
                                        Rc_aux_1 = (Int16)(Temp_Data[12] << 8 | Temp_Data[13]);
                                        Rc_aux_2 = (Int16)(Temp_Data[14] << 8 | Temp_Data[15]);
                                        Rc_aux_3 = (Int16)(Temp_Data[16] << 8 | Temp_Data[17]);
                                        Rc_aux_4 = (Int16)(Temp_Data[18] << 8 | Temp_Data[19]);
                                        Rc_aux_5 = (Int16)(Temp_Data[20] << 8 | Temp_Data[21]);
                                        Rc_aux_6 = (Int16)(Temp_Data[22] << 8 | Temp_Data[23]);
                                        break;
                                    case 16:
                                        Rol_P = (Int16)(Temp_Data[4] << 8 | Temp_Data[5]);
                                        Rol_I = (Int16)(Temp_Data[6] << 8 | Temp_Data[7]);
                                        Rol_D = (Int16)(Temp_Data[8] << 8 | Temp_Data[9]);
                                        Pit_P = (Int16)(Temp_Data[10] << 8 | Temp_Data[11]);
                                        Pit_I = (Int16)(Temp_Data[12] << 8 | Temp_Data[13]);
                                        Pit_D = (Int16)(Temp_Data[14] << 8 | Temp_Data[15]);
                                        Yaw_P = (Int16)(Temp_Data[16] << 8 | Temp_Data[17]);
                                        Yaw_I = (Int16)(Temp_Data[18] << 8 | Temp_Data[19]);
                                        Yaw_D = (Int16)(Temp_Data[20] << 8 | Temp_Data[21]);
                                        Pid_flag = true;
                                        break;
                                    default:
                                        break;
                                }
                                flag = true;
                            }
                            //至此,已经被找到了一条完整数据。我们将数据直接分析,或是缓存起来一起分析
                            //我们这里采用的办法是缓存一次,好处就是如果你某种原因,数据堆积在缓存buffer中
                            //已经很多了,那你需要循环的找到最后一组,只分析最新数据,过往数据你已经处理不及时
                            //了,就不要浪费更多时间了,这也是考虑到系统负载能够降低。
                            else
                                this.Data_Buffer.RemoveRange(0, len   5);//从缓存中删除错误数据
 
                        }
                        else
                        {
                            //这里是很重要的,如果数据开始不是头,则删除数据
                            Data_Buffer.RemoveAt(0);
                        }
                    }
                    if (flag)
                    {
                        if (method == null)
                        {
                            method = delegate
                            {
                                this.FrameCount  = 1L;
                                Application.DoEvents();
                                this.FrameNum.Text = this.FrameCount.ToString();
                                string Euler_data = "    roll: "   this.D_ang_rol.ToString()   "    pitch; "   this.D_ang_pit.ToString()   "    yaw: "   this.D_ang_yaw.ToString();
                                string Acc_data = "    Acc_x: "   this.S_acc_x.ToString()   "    Acc_y; "   this.S_acc_y.ToString()   "    Acc_z: "   this.S_acc_z.ToString();
                                string Gyr_data = "    Gyr_x: "   this.S_gyr_x.ToString()   "    Gyr_y; "   this.S_gyr_y.ToString()   "    Gyr_z: "   this.S_gyr_z.ToString();
                                this.Invoke((EventHandler)(delegate { eulerdatalabel.Text = Euler_data; }));
                                this.Invoke((EventHandler)(delegate { accdatalabel.Text = Acc_data; }));
                                this.Invoke((EventHandler)(delegate { gyrdatalabel.Text = Gyr_data; }));
                                horizonInstrumentControl1.SetAttitudeIndicatorParameters((double)D_ang_pit, (double)D_ang_rol);
                                headingIndicatorInstrumentControl1.SetHeadingIndicatorParameters(((int)D_ang_yaw)   180);
                                Application.DoEvents();
                                fly_roll = D_ang_rol;
                                if (fly_roll <= 0)
                                {
                                    if (fly_roll <= -60)
                                    {
                                        fly_roll = 60;
                                        turnCoordinatorInstrumentControl1.SetTurnCoordinatorParameters(-6, 0);
                                    }
                                    else
                                        turnCoordinatorInstrumentControl1.SetTurnCoordinatorParameters((fly_roll / 10), 0);
                                }
                                else if (fly_roll >= 60)
                                {
                                    fly_roll = 60;
                                    turnCoordinatorInstrumentControl1.SetTurnCoordinatorParameters(6, 0);
                                }
                                else
                                    turnCoordinatorInstrumentControl1.SetTurnCoordinatorParameters((fly_roll / 10), 0);
                                if (GesShowFlag)
                                {
                                    form_3Dcuboid.Roll = D_ang_pit;
                                    form_3Dcuboid.Yaw = D_ang_yaw;
                                    form_3Dcuboid.Pitch = -D_ang_rol;
                                    Application.DoEvents();
                                }
                                if (ZedCollectFlag)
                                {
 
                                    if (xCount >= 400)
                                    {
                                        xCount = 0;
                                        if (eulerwaveradio.Checked)
                                        {
                                            x1.Clear();
                                            y1.Clear();
                                            x2.Clear();
                                            y2.Clear();
                                            x3.Clear();
                                            y3.Clear();
                                        }
                                        else
                                        {
                                            x1.Clear();
                                            y1.Clear();
                                            x2.Clear();
                                            y2.Clear();
                                            x3.Clear();
                                            y3.Clear();
                                            x4.Clear();
                                            y4.Clear();
                                            x5.Clear();
                                            y5.Clear();
                                            x6.Clear();
                                            y6.Clear();
                                        }
 
                                    }
                                    else
                                    {
 
                                        xCount  ;
                                        if (eulerwaveradio.Checked)
                                        {
                                            x1.Add((float)xCount);
                                            x2.Add((float)xCount);
                                            x3.Add((float)xCount);
                                        }
                                        else
                                        {
                                            x1.Add((float)xCount);
                                            x2.Add((float)xCount);
                                            x3.Add((float)xCount);
                                            x4.Add((float)xCount);
                                            x5.Add((float)xCount);
                                            x6.Add((float)xCount);
                                        }
                                    }
                                    if (eulerwaveradio.Checked)
                                    {
                                        y1.Add((float)D_ang_rol);
                                        y2.Add((float)D_ang_pit);
                                        y3.Add((float)D_ang_yaw);
                                    }
                                    else
                                    {
                                        y1.Add((float)S_acc_x);
                                        y2.Add((float)S_acc_y);
                                        y3.Add((float)S_acc_z);
                                        y4.Add((float)S_gyr_x);
                                        y5.Add((float)S_gyr_y);
                                        y6.Add((float)S_gyr_z);
                                    }
                                    ZGraph.f_Refresh();
 
                                }
                            };
                        }
                        base.Invoke(method);
                    }
                    base.Invoke(new MethodInvoker(delegate
                    {
                        if (SerialCheckOutButton.Checked)
                        {
                            if (HexDis.Checked)
                            {
                                DataDisBox.AppendText(ByteArrayToHexString(buffer)   "\r\n");
                                DataDisBox.ScrollToCaret();//可以将滚动条定位到当前光标处
                            }
                            else
                            {
                                DataDisBox.AppendText(Encoding.ASCII.GetString(buffer));
                                DataDisBox.ScrollToCaret();
                            }
                        }
                        else
                            ReceiveNum.Text = ReceiveCount.ToString();
                    }));
 
                }
                finally
                {
                    listening = false;
                }
            }
        }
        private void Start_Collect()
        {
            if (eulerwaveradio.Checked)
            {
                x1.Clear();
                y1.Clear();
                x2.Clear();
                y2.Clear();
                x3.Clear();
                y3.Clear();
                ZGraph.f_ClearAllPix();
                ZGraph.f_reXY();
                ZGraph.f_LoadOnePix(ref x1, ref y1, Color.FromArgb(255, 0, 0), 1);//ROLL 
                ZGraph.f_AddPix(ref x2, ref y2, Color.FromArgb(0, 255, 0), 1);//YAW
                ZGraph.f_AddPix(ref x3, ref y3, Color.FromArgb(0, 0, 255), 1);//PITCH
                Application.DoEvents();
            }
            else
            {
                x1.Clear();
                y1.Clear();
                x2.Clear();
                y2.Clear();
                x3.Clear();
                y3.Clear();
                x4.Clear();
                y4.Clear();
                x5.Clear();
                y5.Clear();
                x6.Clear();
                y6.Clear();
                ZGraph.f_ClearAllPix();
                ZGraph.f_reXY();
                ZGraph.f_LoadOnePix(ref x1, ref y1, Color.FromArgb(255, 128, 0), 1);//acc_x
                ZGraph.f_AddPix(ref x2, ref y2, Color.FromArgb(255, 255, 0), 1);//acc_y
                ZGraph.f_AddPix(ref x3, ref y3, Color.FromArgb(154, 205, 50), 1);//acc_z
                ZGraph.f_AddPix(ref x4, ref y4, Color.FromArgb(0, 255, 255), 1);//gyr_x 
                ZGraph.f_AddPix(ref x5, ref y5, Color.FromArgb(65, 105, 225), 1);//gyr_y
                ZGraph.f_AddPix(ref x6, ref y6, Color.FromArgb(186, 85, 211), 1);//gyr_z
                Application.DoEvents();
            }
 
        }
        private void SerialSendData()
        {
            if (this.HexSend.Checked)
            {
                try
                {
                    byte[] buffer = this.HexStringToByteArray(this.SendDataBox.Text);
                    this.Serial_Port.Write(buffer, 0, buffer.Length);
                }
                catch (FormatException)
                {
                    this.AppStateDis(MsgType.Error, "数据格式不正确 ");
                }
            }
            else
            {
                this.Serial_Port.Write(this.SendDataBox.Text);
                this.AppStateDis(MsgType.Outgoing, "消息已发送");
            }
            int length = this.SendDataBox.Text.Length;
            this.SendCount  = length;
            this.SendNum.Text = this.SendCount.ToString();
            this.SendDataBox.SelectAll();
        }
 
        private void StopSend_Click(object sender, EventArgs e)
        {
            this.StopDisFlag = !this.StopDisFlag;
            if (this.StopDisFlag)
            {
                StopSend.Text = "继续显示";
                this.AppStateDis(MsgType.Error, "已停止显示");
            }
            else
            {
                StopSend.Text = "停止显示";
                this.AppStateDis(MsgType.Error, "继续显示数据");
            }
        }
 
        public int cubeX { get; set; }
        public int cubeY { get; set; }
        public int cubeZ { get; set; }
        /*USB事件处理函数*/
        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
        public bool IsCopy = false;
        protected override void WndProc(ref Message m)
        {
            try
            {
                if (m.Msg == WM_DEVICECHANGE)
                {
                    switch (m.WParam.ToInt32())
                    {
                        // USB插上
                        case DBT_DEVICEARRIVAL:
                            if (!IsCopy) // 如果不用这个判断,会被多次调用,原因不明
                            {
                                IsCopy = true;
                                // 其他处理
                                InitCtlVal();
                            }
                            break;
                        // USB移除
                        case DBT_DEVICEREMOVECOMPLETE:
                            IsCopy = false;
                            //InitCtlVal();
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("出错!"   ex.Message, "请检查串口", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            base.WndProc(ref m);
        }
 
        private void TimingSend_CheckedChanged(object sender, EventArgs e)
        {
            if (this.TimingSend.Checked)
            {
                this.timer_send.Start();
                this.AppStateDis(MsgType.Error, "已开启定时发送 ");
            }
            else
            {
                this.timer_send.Stop();
                this.AppStateDis(MsgType.Error, "关闭定时发送");
            }
        }
        private void clearRecBtn_Click(object sender, EventArgs e)
        {
            this.DataDisBox.Clear();
        }
        private void button9_Click(object sender, EventArgs e)
        {
            Application.DoEvents();
            tabControl1.SelectedTab = tabPage3; 
        }
 
        private void button8_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage1;
        }
 
        private void button7_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage2;
        }
        private void AccCheck_Click(object sender, EventArgs e)
        {
            byte[] AccBuffer = this.HexStringToByteArray(CMD_Acc);
            this.Serial_Port.Write(AccBuffer, 0, AccBuffer.Length);
            this.AppStateDis(MsgType.Error, "加速度校正 ");
        }
 
        private void GyroCheck_Click(object sender, EventArgs e)
        {
            byte[] GyroBuffer = this.HexStringToByteArray(CMD_Gyro);
            this.Serial_Port.Write(GyroBuffer, 0, GyroBuffer.Length);
            this.AppStateDis(MsgType.Error, "陀螺仪校正 ");
        }
        private void MagCheck_Click(object sender, EventArgs e)
        {
            byte[] GyroBuffer = this.HexStringToByteArray(CMD_Mag);
            this.Serial_Port.Write(GyroBuffer, 0, GyroBuffer.Length);
            this.AppStateDis(MsgType.Error, "磁力计校正 ");
        }
 
        private void OpenPort_Click(object sender, EventArgs e)
        {
 
            if (this.dlPortName.Items.Count > 0)
            {
                if (this.Serial_Port.IsOpen)
                {
                    this.closing = true;
                    while (this.listening)
                    {
                        Application.DoEvents();
                    }
                    this.Serial_Port.DiscardInBuffer();
                    this.Serial_Port.Close();
                    SendData.Enabled = false;
                    this.OpenPort.BackgroundImage = Properties.Resources.usb;
                    this.AppStateDis(MsgType.Outgoing, "串口关闭");
                    this.serialPortlabel.Text = "串口关闭";
                }
                else
                {
                    this.closing = false;
                    this.Serial_Port.BaudRate = int.Parse(this.dlBaudRate.Text);
                    this.Serial_Port.DataBits = int.Parse(this.dlDataByte.Text);
                    this.Serial_Port.StopBits = (StopBits)Enum.Parse(typeof(StopBits), this.dlStopByte.Text);
                    this.Serial_Port.Parity = (Parity)Enum.Parse(typeof(Parity), this.dlParity.Text);
                    this.Serial_Port.PortName = this.dlPortName.Text;
                    this.Serial_Port.Open();
                    StartShowCube.Enabled = true;
                    StartCollect.Enabled = true;
                    SendData.Enabled = true;
                    //Reset3DButton.Enabled = true;
                    this.OpenPort.BackgroundImage = Properties.Resources.usbon;
                    this.AppStateDis(MsgType.Outgoing, "串口打开");
                    this.serialPortlabel.Text = "串口打开";
                }
                this.SetCtlState();
            }
            else
            {
                MessageBox.Show(this, "计算机没有串口,请连接设备");
                this.InitCtlVal();
            }
        }
 
        private void FlySetButton_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage4;
        }
 
        private void StartCollect_Click(object sender, EventArgs e)
        {
            if (!this.ZedCollectFlag)
            {
                this.ZedCollectFlag = true;
                this.Start_Collect();
                this.waveLabel.Text = "停止采集";
            }
            else
            {
                this.ZedCollectFlag = false;
                this.waveLabel.Text = "开始采集";
                xCount = 0;
                ZGraph.f_ClearAllPix();
                //ZGraph.f_reXY();
            }
        }
 
        private void StartShowCube_Click(object sender, EventArgs e)
        {
            if (!this.GesShowFlag)
            {
                this.GesShowFlag = true;
                this.plane3dLabel.Text = "停止显示";
            }
            else
            {
                this.GesShowFlag = false;
                this.plane3dLabel.Text = "开始显示";
            }
        }
 
        private void Reset3DButton_Click(object sender, EventArgs e)
        {
            this.cubeX = 0;
            this.cubeY = 0;
            this.cubeZ = 0;
            trackBarVerticalSpeed.Value = 0;
            verticalSpeedInstrumentControl1.SetVerticalSpeedIndicatorParameters(0);
 
            this.form_3Dcuboid.Roll = this.trackBarRollAngle.Value = 0;
            this.form_3Dcuboid.Pitch = this.trackPitchAngle.Value = 0;
            horizonInstrumentControl1.SetAttitudeIndicatorParameters(0, 0);
            this.form_3Dcuboid.Yaw = this.trackBarHeading.Value = 0;
            headingIndicatorInstrumentControl1.SetHeadingIndicatorParameters(0);
        }
 
        private void SendData_Click(object sender, EventArgs e)
        {
            try
            {
                this.SerialSendData();
            }
            catch
            {
                this.AppStateDis(MsgType.Error, "数据格式不正确或串口没有打开");
            }
        }
 
        private void clearRXTXbutton_Click(object sender, EventArgs e)
        {
            this.ReceiveCount = 0L;
            this.ReceiveNum.Text = this.ReceiveCount.ToString();
            this.SendCount = 0L;
            this.SendNum.Text = this.SendCount.ToString();
        }
 
        private void clearSendBoxbtn_Click(object sender, EventArgs e)
        {
            SendDataBox.Text = "";
        }
 
        private void timer_UI_Tick(object sender, EventArgs e)
        {
            if (eulerwaveradio.Checked)
            {
                rol_label.Text = "roll :一";
                pit_label.Text = "pitch :一";
                yaw_label.Text = "yaw :一";
                acc_x_label.Text = "";
                acc_y_label.Text = "";
                acc_z_label.Text = "";
                gyr_x_label.Text = "";
                gyr_y_label.Text = "";
                gyr_z_label.Text = "";
            }
            if (basicwaveradio.Checked)
            {
                rol_label.Text = "";
                pit_label.Text = "";
                yaw_label.Text = "";
                acc_x_label.Text = "Acc_x :一";
                acc_y_label.Text = "Acc_y :一";
                acc_z_label.Text = "Acc_z :一";
                gyr_x_label.Text = "Gyr_x :一";
                gyr_y_label.Text = "Gyr_y :一";
                gyr_z_label.Text = "Gyr_z :一";
            }
            //修改接收计数 
            acc_x.Text = S_acc_x.ToString();
            acc_y.Text = S_acc_y.ToString();
            acc_z.Text = S_acc_z.ToString();
 
            gyr_x.Text = S_gyr_x.ToString();
            gyr_y.Text = S_gyr_y.ToString();
            gyr_z.Text = S_gyr_z.ToString();
 
            mag_x.Text = S_mag_x.ToString();
            mag_y.Text = S_mag_y.ToString();
            mag_z.Text = S_mag_z.ToString();
            if(Pid_flag)
            {
                roll_P.Value = Rol_P;
                roll_I.Value = Rol_I;
                roll_D.Value = Rol_D;
                pitch_P.Value = Pit_P;
                pitch_I.Value = Pit_I;
                pitch_D.Value = Pit_D;
                yaw_P.Value = Yaw_P;
                yaw_I.Value = Yaw_I;
                yaw_D.Value = Yaw_D;
                Pid_flag = false;
            }
            //alt_csb.Text = D_alt_csb.ToString();
            //alt_prs.Text = D_alt_prs.ToString();
 
            if (B_armed)
            {
                plane3dLabel.Text = "飞机锁定";
                lockctrlfly.Checked = false;
            }
            else
            {
                plane3dLabel.Text = "飞机解锁";
                lockctrlfly.Checked = true;
            }
        }
 
        private void save_data_button_Click(object sender, EventArgs e)
        {
            SaveSetting();
        }
 
        private void read_data_button_Click(object sender, EventArgs e)
        {
            GetIni();
        }
 
        private void timer_send_Tick(object sender, EventArgs e)
        {
            if (this.SendSet.Enabled)
            {
                this.s = this.SendTim.Text;
                if (this.s == "")
                {
                    this.t = 1;
                }
                else
                {
                    int.TryParse(this.s, out this.t);
                }
                this.timer_send.Interval = this.t;
                this.SerialSendData();
            }
            else
            {
                this.timer_send.Stop();
            }
        }
 
        private void Read_Pid_button_Click(object sender, EventArgs e)
        {
            byte[] PidBuffer = this.HexStringToByteArray(CMD_Pid);
            this.Serial_Port.Write(PidBuffer, 0, PidBuffer.Length);
            this.AppStateDis(MsgType.Error, "读取PID");
        }
 
        private byte[] CreateData(Int16 rol_p, Int16 rol_i, Int16 rol_d, Int16 pit_p, Int16 pit_i, Int16 pit_d, Int16 yaw_p, Int16 yaw_i, Int16 yaw_d)   //构建数据包
        {
            byte checksum = 0;
            byte[] data = new byte[23];
            data[0] = 0xaa;
            data[1] = 0xaf;
            data[2] = 0x10;
            data[3] = 0x12;
            data[4] = (byte)((rol_p & 0xff00)>>8);
            data[5] = (byte)(rol_p & 0xff);
            data[6] = (byte)((rol_i & 0xff00) >> 8);
            data[7] = (byte)(rol_i & 0xff);
            data[8] = (byte)((rol_d & 0xff00) >> 8);
            data[9] = (byte)(rol_d & 0xff);
            data[10] = (byte)((pit_p & 0xff00) >> 8);
            data[11] = (byte)(pit_p & 0xff);
            data[12] = (byte)((pit_i & 0xff00) >> 8);
            data[13] = (byte)(pit_i & 0xff);
            data[14] = (byte)((pit_d & 0xff00) >> 8);
            data[15] = (byte)(pit_d & 0xff);
            data[16] = (byte)((yaw_p & 0xff00) >> 8);
            data[17] = (byte)(yaw_p & 0xff);
            data[18] = (byte)((yaw_i & 0xff00) >> 8);
            data[19] = (byte)(yaw_i & 0xff);
            data[20] = (byte)((yaw_d & 0xff00) >> 8);
            data[21] = (byte)(yaw_d & 0xff);
            for (int i = 0; i < 18; i  )
                checksum  = data[i 4];
            data[22] = checksum;
            return data;
        }
        private void Save_Pid_button_Click(object sender, EventArgs e)
        {
            byte[] Pid_data = CreateData(
            Convert.ToInt16(roll_P.Value),Convert.ToInt16(roll_I.Value),Convert.ToInt16( roll_D.Value),
            Convert.ToInt16( pitch_P.Value),Convert.ToInt16( pitch_I.Value),Convert.ToInt16( pitch_D.Value),
            Convert.ToInt16( yaw_P.Value),Convert.ToInt16( yaw_I.Value),Convert.ToInt16( yaw_D.Value));
            this.Serial_Port.Write(Pid_data, 0, Pid_data.Length);
            this.AppStateDis(MsgType.Error, "写入PID数据到飞控 ");
        }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    
 
    
 
 
 
  
 
 
 
  
 
 
 
 
    }
}


标签: AHRS

实例下载地址

AHRS(Mini4Rotors)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警